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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* Licensed Materials - Property of IBM
*
* trousers - An open source TCG Software Stack
*
* (C) Copyright International Business Machines Corp. 2004
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <pwd.h>
#if (defined (__OpenBSD__) || defined (__FreeBSD__))
#include <netinet/in.h>
#endif
#include <arpa/inet.h>
#include <errno.h>
#include <getopt.h>
#include "trousers/tss.h"
#include "trousers_types.h"
#include "tcs_tsp.h"
#include "tcs_utils.h"
#include "tcs_int_literals.h"
#include "capabilities.h"
#include "tcslog.h"
#include "tcsd_wrap.h"
#include "tcsps.h"
#include "tcsd.h"
#include "req_mgr.h"
struct tcsd_config tcsd_options;
struct tpm_properties tpm_metrics;
static volatile int hup = 0, term = 0;
extern char *optarg;
int sd;
char *tcsd_config_file = NULL;
static void
tcsd_shutdown(void)
{
/* order is important here:
* allow all threads to complete their current request */
tcsd_threads_final();
PS_close_disk_cache();
auth_mgr_final();
(void)req_mgr_final();
conf_file_final(&tcsd_options);
EVENT_LOG_final();
}
static void
tcsd_signal_term(int signal)
{
term = 1;
close(sd);
}
void
tcsd_signal_hup(int signal)
{
hup = 1;
}
static TSS_RESULT
signals_init(void)
{
int rc;
sigset_t sigmask;
struct sigaction sa;
sigemptyset(&sigmask);
if ((rc = sigaddset(&sigmask, SIGTERM))) {
LogError("sigaddset: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
if ((rc = sigaddset(&sigmask, SIGHUP))) {
LogError("sigaddset: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
if ((rc = THREAD_SET_SIGNAL_MASK(SIG_UNBLOCK, &sigmask, NULL))) {
LogError("Setting thread signal mask: %s", strerror(rc));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = tcsd_signal_term;
if ((rc = sigaction(SIGTERM, &sa, NULL))) {
LogError("signal SIGTERM not registered: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
sa.sa_handler = tcsd_signal_hup;
if ((rc = sigaction(SIGHUP, &sa, NULL))) {
LogError("signal SIGHUP not registered: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
return TSS_SUCCESS;
}
static TSS_RESULT
tcsd_startup(void)
{
TSS_RESULT result;
#ifdef TSS_DEBUG
/* Set stdout to be unbuffered to match stderr and interleave output correctly */
setvbuf(stdout, (char *)NULL, _IONBF, 0);
#endif
if ((result = signals_init()))
return result;
if ((result = conf_file_init(&tcsd_options)))
return result;
if ((result = tcsd_threads_init())) {
conf_file_final(&tcsd_options);
return result;
}
if ((result = req_mgr_init())) {
conf_file_final(&tcsd_options);
return result;
}
if ((result = ps_dirs_init())) {
conf_file_final(&tcsd_options);
(void)req_mgr_final();
return result;
}
result = PS_init_disk_cache();
if (result != TSS_SUCCESS) {
conf_file_final(&tcsd_options);
(void)req_mgr_final();
return result;
}
if ((result = get_tpm_metrics(&tpm_metrics))) {
conf_file_final(&tcsd_options);
PS_close_disk_cache();
(void)req_mgr_final();
return result;
}
/* must happen after get_tpm_metrics() */
if ((result = auth_mgr_init())) {
conf_file_final(&tcsd_options);
PS_close_disk_cache();
(void)req_mgr_final();
return result;
}
result = EVENT_LOG_init();
if (result != TSS_SUCCESS) {
auth_mgr_final();
conf_file_final(&tcsd_options);
PS_close_disk_cache();
(void)req_mgr_final();
return result;
}
result = owner_evict_init();
if (result != TSS_SUCCESS) {
auth_mgr_final();
conf_file_final(&tcsd_options);
PS_close_disk_cache();
(void)req_mgr_final();
return result;
}
return TSS_SUCCESS;
}
void
usage(void)
{
fprintf(stderr, "\tusage: tcsd [-f] [-e] [-c <config file> [-h]\n\n");
fprintf(stderr, "\t-f|--foreground\trun in the foreground. Logging goes to stderr "
"instead of syslog.\n");
fprintf(stderr, "\t-e| attempts to connect to software TPMs over TCP\n");
fprintf(stderr, "\t-c|--config\tpath to configuration file\n");
fprintf(stderr, "\t-h|--help\tdisplay this help message\n");
fprintf(stderr, "\n");
}
static TSS_RESULT
reload_config(void)
{
TSS_RESULT result;
hup = 0;
// FIXME: reload the config - work in progress
result = TSS_SUCCESS;
return result;
}
int
main(int argc, char **argv)
{
struct sockaddr_in serv_addr, client_addr;
TSS_RESULT result;
int newsd, c, option_index = 0;
unsigned client_len;
char *hostname = NULL;
struct passwd *pwd;
struct hostent *client_hostent = NULL;
struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"foreground", 0, NULL, 'f'},
{"config", 1, NULL, 'c'},
{0, 0, 0, 0}
};
unsetenv("TCSD_USE_TCP_DEVICE");
while ((c = getopt_long(argc, argv, "fhec:", long_options, &option_index)) != -1) {
switch (c) {
case 'f':
setenv("TCSD_FOREGROUND", "1", 1);
break;
case 'c':
tcsd_config_file = optarg;
break;
case 'e':
setenv("TCSD_USE_TCP_DEVICE", "1", 1);
break;
case 'h':
/* fall through */
default:
usage();
return -1;
break;
}
}
if (!tcsd_config_file)
tcsd_config_file = TCSD_DEFAULT_CONFIG_FILE;
if ((result = tcsd_startup()))
return (int)result;
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) {
LogError("Failed socket: %s", strerror(errno));
return -1;
}
memset(&serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(tcsd_options.port);
/* If no remote_ops are defined, restrict connections to localhost
* only at the socket. */
if (tcsd_options.remote_ops[0] == 0)
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
else
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
c = 1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &c, sizeof(c));
if (bind(sd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
LogError("Failed bind: %s", strerror(errno));
return -1;
}
#ifndef SOLARIS
pwd = getpwnam(TSS_USER_NAME);
if (pwd == NULL) {
if (errno == 0) {
LogError("User \"%s\" not found, please add this user"
" manually.", TSS_USER_NAME);
} else {
LogError("getpwnam(%s): %s", TSS_USER_NAME, strerror(errno));
}
return TCSERR(TSS_E_INTERNAL_ERROR);
}
setuid(pwd->pw_uid);
#endif
if (listen(sd, TCSD_MAX_SOCKETS_QUEUED) < 0) {
LogError("Failed listen: %s", strerror(errno));
return -1;
}
client_len = (unsigned)sizeof(client_addr);
if (getenv("TCSD_FOREGROUND") == NULL) {
if (daemon(0, 0) == -1) {
perror("daemon");
tcsd_shutdown();
return -1;
}
}
LogInfo("%s: TCSD up and running.", PACKAGE_STRING);
do {
newsd = accept(sd, (struct sockaddr *) &client_addr, &client_len);
if (newsd < 0) {
if (errno == EINTR) {
if (term)
break;
else if (hup) {
if (reload_config() != TSS_SUCCESS)
LogError("Failed reloading config");
}
continue;
} else {
LogError("Failed accept: %s", strerror(errno));
continue;
}
}
LogDebug("accepted socket %i", newsd);
if ((client_hostent = gethostbyaddr((char *) &client_addr.sin_addr,
sizeof(client_addr.sin_addr),
AF_INET)) == NULL) {
char buf[16];
uint32_t addr = htonl(client_addr.sin_addr.s_addr);
snprintf(buf, 16, "%d.%d.%d.%d", (addr & 0xff000000) >> 24,
(addr & 0x00ff0000) >> 16, (addr & 0x0000ff00) >> 8,
addr & 0x000000ff);
LogWarn("Host name for connecting IP %s could not be resolved", buf);
hostname = strdup(buf);
} else {
hostname = strdup(client_hostent->h_name);
}
tcsd_thread_create(newsd, hostname);
hostname = NULL;
if (hup) {
if (reload_config() != TSS_SUCCESS)
LogError("Failed reloading config");
}
} while (term ==0);
/* To close correctly, we must receive a SIGTERM */
tcsd_shutdown();
return 0;
}
|