OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Licensed Materials - Property of IBM | 3 * Licensed Materials - Property of IBM |
4 * | 4 * |
5 * trousers - An open source TCG Software Stack | 5 * trousers - An open source TCG Software Stack |
6 * | 6 * |
7 * (C) Copyright International Business Machines Corp. 2004 | 7 * (C) Copyright International Business Machines Corp. 2004 |
8 * | 8 * |
9 */ | 9 */ |
10 | 10 |
11 | 11 |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 #include <string.h> | 15 #include <string.h> |
16 #include <sys/types.h> | 16 #include <sys/types.h> |
17 #include <sys/time.h> | 17 #include <sys/time.h> |
18 #include <sys/resource.h> | 18 #include <sys/resource.h> |
19 #include <sys/wait.h> | 19 #include <sys/wait.h> |
20 #include <sys/stat.h> | 20 #include <sys/stat.h> |
21 #include <sys/socket.h> | 21 #include <sys/socket.h> |
| 22 #include <sys/un.h> |
22 #include <netdb.h> | 23 #include <netdb.h> |
23 #include <pwd.h> | 24 #include <pwd.h> |
24 #if (defined (__OpenBSD__) || defined (__FreeBSD__)) | 25 #if (defined (__OpenBSD__) || defined (__FreeBSD__)) |
25 #include <netinet/in.h> | 26 #include <netinet/in.h> |
26 #endif | 27 #endif |
27 #include <arpa/inet.h> | 28 #include <arpa/inet.h> |
28 #include <errno.h> | 29 #include <errno.h> |
29 #include <getopt.h> | 30 #include <getopt.h> |
30 #include "trousers/tss.h" | 31 #include "trousers/tss.h" |
31 #include "trousers_types.h" | 32 #include "trousers_types.h" |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // FIXME: reload the config - work in progress | 205 // FIXME: reload the config - work in progress |
205 result = TSS_SUCCESS; | 206 result = TSS_SUCCESS; |
206 | 207 |
207 return result; | 208 return result; |
208 } | 209 } |
209 | 210 |
210 | 211 |
211 int | 212 int |
212 main(int argc, char **argv) | 213 main(int argc, char **argv) |
213 { | 214 { |
214 » struct sockaddr_in serv_addr, client_addr; | 215 » struct sockaddr_un serv_addr, client_addr; |
215 TSS_RESULT result; | 216 TSS_RESULT result; |
216 int sd, newsd, c, option_index = 0; | 217 int sd, newsd, c, option_index = 0; |
217 unsigned client_len; | 218 unsigned client_len; |
218 char *hostname = NULL; | |
219 struct passwd *pwd; | 219 struct passwd *pwd; |
220 struct hostent *client_hostent = NULL; | 220 struct hostent *client_hostent = NULL; |
221 struct option long_options[] = { | 221 struct option long_options[] = { |
222 {"help", 0, NULL, 'h'}, | 222 {"help", 0, NULL, 'h'}, |
223 {"foreground", 0, NULL, 'f'}, | 223 {"foreground", 0, NULL, 'f'}, |
224 {0, 0, 0, 0} | 224 {0, 0, 0, 0} |
225 }; | 225 }; |
226 | 226 |
227 unsetenv("TCSD_USE_TCP_DEVICE"); | 227 unsetenv("TCSD_USE_TCP_DEVICE"); |
228 while ((c = getopt_long(argc, argv, "fhe", long_options, &option_index))
!= -1) { | 228 while ((c = getopt_long(argc, argv, "fhe", long_options, &option_index))
!= -1) { |
229 switch (c) { | 229 switch (c) { |
230 case 'f': | 230 case 'f': |
231 setenv("TCSD_FOREGROUND", "1", 1); | 231 setenv("TCSD_FOREGROUND", "1", 1); |
232 break; | 232 break; |
233 case 'h': | 233 case 'h': |
234 /* fall through */ | 234 /* fall through */ |
235 case 'e': | 235 case 'e': |
236 setenv("TCSD_USE_TCP_DEVICE", "1", 1); | 236 setenv("TCSD_USE_TCP_DEVICE", "1", 1); |
237 break; | 237 break; |
238 default: | 238 default: |
239 usage(); | 239 usage(); |
240 return -1; | 240 return -1; |
241 break; | 241 break; |
242 } | 242 } |
243 } | 243 } |
244 | 244 |
245 if ((result = tcsd_startup())) | 245 if ((result = tcsd_startup())) |
246 return (int)result; | 246 return (int)result; |
247 | 247 |
248 » sd = socket(AF_INET, SOCK_STREAM, 0); | 248 » sd = socket(AF_UNIX, SOCK_STREAM, 0); |
249 if (sd < 0) { | 249 if (sd < 0) { |
250 LogError("Failed socket: %s", strerror(errno)); | 250 LogError("Failed socket: %s", strerror(errno)); |
251 return -1; | 251 return -1; |
252 } | 252 } |
253 | 253 |
254 memset(&serv_addr, 0, sizeof (serv_addr)); | 254 memset(&serv_addr, 0, sizeof (serv_addr)); |
255 » serv_addr.sin_family = AF_INET; | 255 » serv_addr.sun_family = AF_UNIX; |
256 » serv_addr.sin_port = htons(tcsd_options.port); | 256 » strcpy(serv_addr.sun_path, TCSD_UNIX_SOCKET); |
257 | 257 unlink(serv_addr.sun_path); |
258 » /* If no remote_ops are defined, restrict connections to localhost | |
259 » * only at the socket. */ | |
260 » if (tcsd_options.remote_ops[0] == 0) | |
261 » » serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
262 » else | |
263 » » serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
264 | 258 |
265 c = 1; | 259 c = 1; |
266 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &c, sizeof(c)); | 260 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &c, sizeof(c)); |
267 » if (bind(sd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { | 261 » if (bind(sd, (struct sockaddr *) &serv_addr, |
| 262 strlen(serv_addr.sun_path) + sizeof (serv_addr.sun_family)) |
| 263 < 0) { |
268 LogError("Failed bind: %s", strerror(errno)); | 264 LogError("Failed bind: %s", strerror(errno)); |
269 return -1; | 265 return -1; |
270 } | 266 } |
| 267 chmod(serv_addr.sun_path, |
| 268 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
271 #ifndef SOLARIS | 269 #ifndef SOLARIS |
272 pwd = getpwnam(TSS_USER_NAME); | 270 pwd = getpwnam(TSS_USER_NAME); |
273 if (pwd == NULL) { | 271 if (pwd == NULL) { |
274 if (errno == 0) { | 272 if (errno == 0) { |
275 LogError("User \"%s\" not found, please add this user" | 273 LogError("User \"%s\" not found, please add this user" |
276 " manually.", TSS_USER_NAME); | 274 " manually.", TSS_USER_NAME); |
277 } else { | 275 } else { |
278 LogError("getpwnam(%s): %s", TSS_USER_NAME, strerror(err
no)); | 276 LogError("getpwnam(%s): %s", TSS_USER_NAME, strerror(err
no)); |
279 } | 277 } |
280 return TCSERR(TSS_E_INTERNAL_ERROR); | 278 return TCSERR(TSS_E_INTERNAL_ERROR); |
(...skipping 26 matching lines...) Expand all Loading... |
307 LogError("Failed reloading confi
g"); | 305 LogError("Failed reloading confi
g"); |
308 } | 306 } |
309 continue; | 307 continue; |
310 } else { | 308 } else { |
311 LogError("Failed accept: %s", strerror(errno)); | 309 LogError("Failed accept: %s", strerror(errno)); |
312 continue; | 310 continue; |
313 } | 311 } |
314 } | 312 } |
315 LogDebug("accepted socket %i", newsd); | 313 LogDebug("accepted socket %i", newsd); |
316 | 314 |
317 » » if ((client_hostent = gethostbyaddr((char *) &client_addr.sin_ad
dr, | 315 // We're listening on a domain socket, so just use "localhost" |
318 » » » » » » sizeof(client_addr.sin_addr)
, | 316 » » tcsd_thread_create(newsd, strdup("localhost")); |
319 » » » » » » AF_INET)) == NULL) { | |
320 » » » char buf[16]; | |
321 uint32_t addr = htonl(client_addr.sin_addr.s_addr); | |
322 | |
323 snprintf(buf, 16, "%d.%d.%d.%d", (addr & 0xff000000) >>
24, | |
324 (addr & 0x00ff0000) >> 16, (addr & 0x0000ff00)
>> 8, | |
325 addr & 0x000000ff); | |
326 | |
327 » » » LogWarn("Host name for connecting IP %s could not be res
olved", buf); | |
328 » » » hostname = strdup(buf); | |
329 » » } else { | |
330 » » » hostname = strdup(client_hostent->h_name); | |
331 » » } | |
332 | |
333 » » tcsd_thread_create(newsd, hostname); | |
334 » » hostname = NULL; | |
335 if (hup) { | 317 if (hup) { |
336 if (reload_config() != TSS_SUCCESS) | 318 if (reload_config() != TSS_SUCCESS) |
337 LogError("Failed reloading config"); | 319 LogError("Failed reloading config"); |
338 } | 320 } |
339 } while (term ==0); | 321 } while (term ==0); |
340 | 322 |
341 /* To close correctly, we must receive a SIGTERM */ | 323 /* To close correctly, we must receive a SIGTERM */ |
342 return 0; | 324 return 0; |
343 } | 325 } |
OLD | NEW |