Modules included will be used by the next modifications to
retrieve hostname and port information to setup client
connections to the tcsd.

Signed-off-by: Richard Maciel <[email protected]>
---
 src/include/Makefile.am      |   2 +-
 src/include/tsp_tcsi_param.h |  19 +++++
 src/tspi/Makefile.am         |   3 +-
 src/tspi/tsp_tcsi_param.c    | 161 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 183 insertions(+), 2 deletions(-)
 create mode 100644 src/include/tsp_tcsi_param.h
 create mode 100644 src/tspi/tsp_tcsi_param.c

diff --git a/src/include/Makefile.am b/src/include/Makefile.am
index 2adec8a..7be09e7 100644
--- a/src/include/Makefile.am
+++ b/src/include/Makefile.am
@@ -39,5 +39,5 @@ noinst_HEADERS = auth_mgr.h authsess.h biosem.h 
capabilities.h \
        tcslog.h tcsps.h tcs_tsp.h tcs_utils.h \
        tddl.h threads.h trousers_types.h tsp_audit.h \
        tsp_delegate.h tsplog.h tspps.h tsp_seal.h \
-       linux/tpm.h
+       linux/tpm.h tsp_tcsi_param.h
 
diff --git a/src/include/tsp_tcsi_param.h b/src/include/tsp_tcsi_param.h
new file mode 100644
index 0000000..1628250
--- /dev/null
+++ b/src/include/tsp_tcsi_param.h
@@ -0,0 +1,19 @@
+
+/*
+ * Licensed Materials - Property of IBM
+ *
+ * trousers - An open source TCG Software Stack
+ *
+ * (C) Copyright International Business Machines Corp. 2005, 2007, 2013
+ *
+ */
+
+/* Defines which environment var is responsible for setting the port to
+ * which the client will connect */
+#define PORT_ENV_VAR "TSS_TCSD_PORT"
+
+/* Defines which envionment var is responsible for setting the hostname to
+ * which the client will connect */
+#define HOSTNAME_ENV_VAR "TSS_TCSD_HOSTNAME"
+
+#define TCP_PORT_STR_MAX_LEN 6
diff --git a/src/tspi/Makefile.am b/src/tspi/Makefile.am
index 6b31e51..0dd257b 100644
--- a/src/tspi/Makefile.am
+++ b/src/tspi/Makefile.am
@@ -33,7 +33,8 @@ libtspi_la_SOURCES=log.c \
                    rpc/@RPC@/rpc_context.c \
                    rpc/tcs_api.c \
                    rpc/hosttable.c \
-                   rpc/@RPC@/rpc.c
+                   rpc/@RPC@/rpc.c \
+                   tsp_tcsi_param.c
 
 if TSS_BUILD_ASYM_CRYPTO
 libtspi_la_SOURCES+=tsp_asym.c
diff --git a/src/tspi/tsp_tcsi_param.c b/src/tspi/tsp_tcsi_param.c
new file mode 100644
index 0000000..670f86f
--- /dev/null
+++ b/src/tspi/tsp_tcsi_param.c
@@ -0,0 +1,161 @@
+
+/*
+ * Licensed Materials - Property of IBM
+ *
+ * trousers - An open source TCG Software Stack
+ *
+ * (C) Copyright International Business Machines Corp. 2005, 2007, 2013
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <bits/local_lim.h>
+#include "trousers/tss.h"
+#include "trousers/trousers.h"
+#include "trousers_types.h"
+#include "tsplog.h"
+#include "spi_utils.h"
+#include "tsp_tcsi_param.h"
+
+#define RV_OK 0
+#define RV_NO_VALUE -1
+#define RV_NO_MEM -2
+#define RV_WRONG_VALUE -3
+#define RV_UNKNOWN_ERR -4
+
+int
+get_port_from_env(int *port)
+{
+       char *env_port;
+       char *raw_port_str;
+
+       env_port = getenv(PORT_ENV_VAR);
+       if (env_port == NULL)
+               return RV_NO_VALUE;
+
+       raw_port_str = strdup(env_port);
+       if (raw_port_str == NULL)
+               return RV_NO_MEM;
+
+       LogDebug("Found data in %s environment var: %s", PORT_ENV_VAR, 
raw_port_str);
+
+       *port = atoi(raw_port_str);
+       free(raw_port_str);
+       if (*port < 0 || *port > 65535) {
+               LogError("Environment var %s contains invalid port value!", 
PORT_ENV_VAR);
+               return RV_WRONG_VALUE;
+       }
+
+       return RV_OK;
+}
+
+TSS_RESULT
+convert_port_to_str(int port, char port_str[TCP_PORT_STR_MAX_LEN])
+{
+       if (snprintf(port_str, TCP_PORT_STR_MAX_LEN, "%d", port) < 0)
+               return TSPERR(TSS_E_INTERNAL_ERROR);
+
+       return TSS_SUCCESS;
+}
+
+TSS_RESULT
+get_tcsd_port(char port_str[TCP_PORT_STR_MAX_LEN])
+{
+       int rv, port = 0;
+
+       // Retrieves port from env var first
+       rv = get_port_from_env(&port);
+       switch(rv) {
+               case RV_OK:
+                       return convert_port_to_str(port, port_str);
+               case RV_WRONG_VALUE:
+                       return TSPERR(TSS_E_BAD_PARAMETER);
+               case RV_NO_MEM:
+                       return TSPERR(TSS_E_OUTOFMEMORY);
+               case RV_NO_VALUE:
+                       break;
+       }
+
+       // TODO: Future work retrieves port from config file.
+
+       // Last case, retrieve default port used by server.
+       return convert_port_to_str(TCSD_DEFAULT_PORT, port_str);
+}
+
+/**
+ *  Allocates a string with up to HOST_NAME_MAX chars which contains
+ *  the hostname extracted from the env var
+ */
+int
+get_hostname_from_env(char **host_str, unsigned *len)
+{
+       char *env_host, *tmp_str = NULL;
+       unsigned env_len;
+
+       // Tries to retrieve from env var first.
+       env_host = getenv(HOSTNAME_ENV_VAR);
+       if (env_host == NULL) {
+               *host_str = NULL;
+               *len = 0;
+               LogDebug("Got no value inside environment var %s.", 
HOSTNAME_ENV_VAR);
+               return RV_NO_VALUE;
+       }
+
+       tmp_str = strdup(env_host);
+       if (tmp_str == NULL)
+               return RV_NO_MEM;
+
+       LogDebug("Environment var %s got value: %s", HOSTNAME_ENV_VAR, tmp_str);
+       env_len = strlen(tmp_str);
+       if (env_len > HOST_NAME_MAX) {
+               *len = HOST_NAME_MAX + 1;
+       } else {
+               *len = env_len + 1;
+       }
+
+       *host_str = (char *)malloc(*len);
+       if (*host_str == NULL) {
+               LogError("Not enough memory when allocating string to retrieve 
host     name from environment var");
+               free(tmp_str);
+               return RV_NO_MEM;
+       }
+
+       strncpy(*host_str, tmp_str, *len);
+       free(tmp_str);
+       return RV_OK;
+}
+
+TSS_RESULT
+get_tcsd_hostname(char **host_str, unsigned *len)
+{
+       int rv;
+
+       // Retrieve from environment var
+       rv = get_hostname_from_env(host_str, len);
+       switch(rv) {
+       case RV_OK:
+               LogDebug("Hostname %s will be used", *host_str);
+               return TSS_SUCCESS;
+       case RV_NO_MEM:
+               return TSPERR(TSS_E_OUTOFMEMORY);
+       case RV_NO_VALUE: // Tente obter de outra maneira
+               break;
+       default:
+               return TSPERR(TSS_E_INTERNAL_ERROR);
+       }
+
+       // TODO: Future work - Retrieve from config file
+
+       // Use localhost in last case.
+       *host_str = strdup(TSS_LOCALHOST_STRING);
+       if (*host_str == NULL)
+               return TSPERR(TSS_E_OUTOFMEMORY);
+
+       *len = sizeof(TSS_LOCALHOST_STRING);
+
+       LogDebug("Hostname %s will be used", *host_str);
+       return TSS_SUCCESS;
+}
+
-- 
1.8.1.4


------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134791&iu=/4140/ostg.clktrk
_______________________________________________
TrouSerS-tech mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/trousers-tech

Reply via email to