1
2 /*
3 * Licensed Materials - Property of IBM
4 *
5 * trousers - An open source TCG Software Stack
6 *
7 * (C) Copyright International Business Machines Corp. 2005, 2007, 2013
8 *
9 */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14
15
16 #ifndef __APPLE__
17 #include <limits.h>
18 #endif
19 #ifndef HOST_NAME_MAX
20 #define HOST_NAME_MAX 64
21 #endif
22
23 #include "trousers/tss.h"
24 #include "trousers/trousers.h"
25 #include "trousers_types.h"
26 #include "tsplog.h"
27 #include "spi_utils.h"
28 #include "tsp_tcsi_param.h"
29
30 #define RV_OK 0
31 #define RV_NO_VALUE -1
32 #define RV_NO_MEM -2
33 #define RV_WRONG_VALUE -3
34 #define RV_UNKNOWN_ERR -4
35
36 int
get_port_from_env(int * port)37 get_port_from_env(int *port)
38 {
39 char *env_port;
40 char *raw_port_str;
41
42 env_port = getenv(PORT_ENV_VAR);
43 if (env_port == NULL)
44 return RV_NO_VALUE;
45
46 raw_port_str = strdup(env_port);
47 if (raw_port_str == NULL)
48 return RV_NO_MEM;
49
50 LogDebug("Found data in %s environment var: %s", PORT_ENV_VAR, raw_port_str);
51
52 *port = atoi(raw_port_str);
53 free(raw_port_str);
54 if (*port < 0 || *port > 65535) {
55 LogError("Environment var %s contains invalid port value!", PORT_ENV_VAR);
56 return RV_WRONG_VALUE;
57 }
58
59 return RV_OK;
60 }
61
62 TSS_RESULT
convert_port_to_str(int port,char port_str[TCP_PORT_STR_MAX_LEN])63 convert_port_to_str(int port, char port_str[TCP_PORT_STR_MAX_LEN])
64 {
65 if (snprintf(port_str, TCP_PORT_STR_MAX_LEN, "%d", port) < 0)
66 return TSPERR(TSS_E_INTERNAL_ERROR);
67
68 return TSS_SUCCESS;
69 }
70
71 TSS_RESULT
get_tcsd_port(char port_str[TCP_PORT_STR_MAX_LEN])72 get_tcsd_port(char port_str[TCP_PORT_STR_MAX_LEN])
73 {
74 int rv, port = 0;
75
76 // Retrieves port from env var first
77 rv = get_port_from_env(&port);
78 switch(rv) {
79 case RV_OK:
80 return convert_port_to_str(port, port_str);
81 case RV_WRONG_VALUE:
82 return TSPERR(TSS_E_BAD_PARAMETER);
83 case RV_NO_MEM:
84 return TSPERR(TSS_E_OUTOFMEMORY);
85 case RV_NO_VALUE:
86 break;
87 }
88
89 // TODO: Future work retrieves port from config file.
90
91 // Last case, retrieve default port used by server.
92 return convert_port_to_str(TCSD_DEFAULT_PORT, port_str);
93 }
94
95 /**
96 * Allocates a string with up to HOST_NAME_MAX chars which contains
97 * the hostname extracted from the env var
98 */
99 int
get_hostname_from_env(char ** host_str,unsigned * len)100 get_hostname_from_env(char **host_str, unsigned *len)
101 {
102 char *env_host, *tmp_str = NULL;
103 size_t env_len;
104
105 // Tries to retrieve from env var first.
106 env_host = getenv(HOSTNAME_ENV_VAR);
107 if (env_host == NULL) {
108 *host_str = NULL;
109 *len = 0;
110 LogDebug("Got no value inside environment var %s.", HOSTNAME_ENV_VAR);
111 return RV_NO_VALUE;
112 }
113
114 tmp_str = strdup(env_host);
115 if (tmp_str == NULL)
116 return RV_NO_MEM;
117
118 LogDebug("Environment var %s got value: %s", HOSTNAME_ENV_VAR, tmp_str);
119 env_len = strlen(tmp_str);
120 if (env_len > HOST_NAME_MAX) {
121 *len = HOST_NAME_MAX + 1;
122 } else {
123 *len = env_len + 1;
124 }
125
126 *host_str = (char *)malloc(*len);
127 if (*host_str == NULL) {
128 LogError("Not enough memory when allocating string to retrieve host name from environment var");
129 free(tmp_str);
130 return RV_NO_MEM;
131 }
132
133 strncpy(*host_str, tmp_str, *len);
134 free(tmp_str);
135 return RV_OK;
136 }
137
138 TSS_RESULT
get_tcsd_hostname(char ** host_str,unsigned * len)139 get_tcsd_hostname(char **host_str, unsigned *len)
140 {
141 int rv;
142
143 // Retrieve from environment var
144 rv = get_hostname_from_env(host_str, len);
145 switch(rv) {
146 case RV_OK:
147 LogDebug("Hostname %s will be used", *host_str);
148 return TSS_SUCCESS;
149 case RV_NO_MEM:
150 return TSPERR(TSS_E_OUTOFMEMORY);
151 case RV_NO_VALUE: // Tente obter de outra maneira
152 break;
153 default:
154 return TSPERR(TSS_E_INTERNAL_ERROR);
155 }
156
157 // TODO: Future work - Retrieve from config file
158
159 // Use localhost in last case.
160 *host_str = strdup(TSS_LOCALHOST_STRING);
161 if (*host_str == NULL)
162 return TSPERR(TSS_E_OUTOFMEMORY);
163
164 *len = sizeof(TSS_LOCALHOST_STRING);
165
166 LogDebug("Hostname %s will be used", *host_str);
167 return TSS_SUCCESS;
168 }
169
170