File: tsp_tcsi_param.c

package info (click to toggle)
trousers 0.3.14%2Bfixed1-1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 9,296 kB
  • ctags: 6,418
  • sloc: ansic: 76,936; sh: 10,308; makefile: 570
file content (169 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

/*
 * 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>


#ifndef __APPLE__
#include <limits.h>
#else
#define HOST_NAME_MAX 64
#endif

#include <bits/posix1_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 _POSIX_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 > _POSIX_HOST_NAME_MAX) {
		*len = _POSIX_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;
}