1 /*- 2 * Copyright (c) 1999-2001 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/libexec/pppoed/pppoed.c,v 1.2.6.8 2002/06/17 02:21:25 brian Exp $ 27 * $DragonFly: src/libexec/pppoed/pppoed.c,v 1.4 2007/06/04 00:40:31 swildner Exp $ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/socket.h> 32 #include <sys/un.h> 33 #include <netinet/in.h> 34 #include <arpa/inet.h> 35 #include <netdb.h> 36 #include <netgraph.h> 37 #include <net/ethernet.h> 38 #include <netinet/in_systm.h> 39 #include <netinet/ip.h> 40 #include <netgraph/ether/ng_ether.h> 41 #include <netgraph/ng_message.h> 42 #include <netgraph/pppoe/ng_pppoe.h> 43 #include <netgraph/socket/ng_socket.h> 44 45 #include <errno.h> 46 #include <paths.h> 47 #include <signal.h> 48 #include <stdio.h> 49 #include <stdarg.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <sysexits.h> 53 #include <sys/fcntl.h> 54 #ifndef NOKLDLOAD 55 #include <sys/linker.h> 56 #include <sys/module.h> 57 #endif 58 #include <sys/uio.h> 59 #include <sys/wait.h> 60 #include <syslog.h> 61 #include <termios.h> 62 #include <unistd.h> 63 64 65 #define DEFAULT_EXEC_PREFIX "exec /usr/sbin/ppp -direct " 66 #define HISMACADDR "HISMACADDR" 67 #define SESSION_ID "SESSION_ID" 68 69 static void nglog(const char *, ...) __printflike(1, 2); 70 static void nglogx(const char *, ...) __printflike(1, 2); 71 72 static int ReceivedSignal; 73 74 static int 75 usage(const char *prog) 76 { 77 fprintf(stderr, "usage: %s [-Fd] [-P pidfile] [-a name] [-e exec | -l label]" 78 " [-p provider] interface\n", prog); 79 return EX_USAGE; 80 } 81 82 static void 83 Farewell(int sig) 84 { 85 ReceivedSignal = sig; 86 } 87 88 static int 89 ConfigureNode(const char *prog __unused, const char *iface, const char *provider, 90 int cs, int ds __unused, int debug, struct ngm_connect *ngc) 91 { 92 /* 93 * We're going to do this with the passed `ds' & `cs' descriptors: 94 * 95 * .---------. 96 * | ether | 97 * | <iface> | 98 * `---------' 99 * (orphan) ds cs 100 * | | | 101 * | | | 102 * (ethernet) | | 103 * .---------. .-----------. 104 * | pppoe | | socket | 105 * | <iface> |(pppoe-<pid>)<---->(pppoe-<pid>)| <unnamed> | 106 * `--------- `-----------' 107 * (exec-<pid>) 108 * ^ .-----------. .-------------. 109 * | | socket | | ppp -direct | 110 * `--->(exec-<pid>)| <unnamed> |--fd--| provider | 111 * `-----------' `-------------' 112 * 113 * where there are potentially many ppp processes running off of the 114 * same PPPoE node. 115 * The exec-<pid> hook isn't made 'till we Spawn(). 116 */ 117 118 char *epath, *spath; 119 struct ngpppoe_init_data *data; 120 const struct hooklist *hlist; 121 const struct nodeinfo *ninfo; 122 const struct linkinfo *nlink; 123 struct ngm_mkpeer mkp; 124 struct ng_mesg *resp; 125 u_char rbuf[2048]; 126 uint32_t f; 127 int plen; 128 129 /* 130 * Ask for a list of hooks attached to the "ether" node. This node should 131 * magically exist as a way of hooking stuff onto an ethernet device 132 */ 133 epath = (char *)alloca(strlen(iface) + 2); 134 sprintf(epath, "%s:", iface); 135 136 if (debug) 137 fprintf(stderr, "Sending NGM_LISTHOOKS to %s\n", epath); 138 139 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0) < 0) { 140 if (errno == ENOENT) 141 fprintf(stderr, "%s Cannot send a netgraph message: Invalid interface\n", 142 epath); 143 else 144 fprintf(stderr, "%s Cannot send a netgraph message: %s\n", 145 epath, strerror(errno)); 146 return EX_UNAVAILABLE; 147 } 148 149 /* Get our list back */ 150 resp = (struct ng_mesg *)rbuf; 151 if (NgRecvMsg(cs, resp, sizeof rbuf, NULL) <= 0) { 152 perror("Cannot get netgraph response"); 153 return EX_UNAVAILABLE; 154 } 155 156 hlist = (const struct hooklist *)resp->data; 157 ninfo = &hlist->nodeinfo; 158 159 if (debug) 160 fprintf(stderr, "Got reply from id [%x]: Type %s with %d hooks\n", 161 ninfo->id, ninfo->type, ninfo->hooks); 162 163 /* Make sure we've got the right type of node */ 164 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE, sizeof NG_ETHER_NODE_TYPE - 1)) { 165 fprintf(stderr, "%s Unexpected node type ``%s'' (wanted ``" 166 NG_ETHER_NODE_TYPE "'')\n", epath, ninfo->type); 167 return EX_DATAERR; 168 } 169 170 /* look for a hook already attached. */ 171 for (f = 0; f < ninfo->hooks; f++) { 172 nlink = &hlist->link[f]; 173 174 if (debug) 175 fprintf(stderr, " Got [%x]:%s -> [%x]:%s\n", ninfo->id, 176 nlink->ourhook, nlink->nodeinfo.id, nlink->peerhook); 177 178 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) || 179 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) { 180 /* 181 * Something is using the data coming out of this `ether' node. 182 * If it's a PPPoE node, we use that node, otherwise we complain that 183 * someone else is using the node. 184 */ 185 if (strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE)) { 186 fprintf(stderr, "%s Node type %s is currently active\n", 187 epath, nlink->nodeinfo.type); 188 return EX_UNAVAILABLE; 189 } 190 break; 191 } 192 } 193 194 if (f == ninfo->hooks) { 195 /* 196 * Create a new PPPoE node connected to the `ether' node using 197 * the magic `orphan' and `ethernet' hooks 198 */ 199 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE); 200 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN); 201 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET); 202 203 if (debug) 204 fprintf(stderr, "Send MKPEER: %s%s -> [type %s]:%s\n", epath, 205 mkp.ourhook, mkp.type, mkp.peerhook); 206 207 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, 208 NGM_MKPEER, &mkp, sizeof mkp) < 0) { 209 fprintf(stderr, "%s Cannot create a peer PPPoE node: %s\n", 210 epath, strerror(errno)); 211 return EX_OSERR; 212 } 213 } 214 215 /* Connect the PPPoE node to our socket node. */ 216 snprintf(ngc->path, sizeof ngc->path, "%s%s", epath, NG_ETHER_HOOK_ORPHAN); 217 snprintf(ngc->ourhook, sizeof ngc->ourhook, "pppoe-%ld", (long)getpid()); 218 memcpy(ngc->peerhook, ngc->ourhook, sizeof ngc->peerhook); 219 220 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, 221 NGM_CONNECT, ngc, sizeof *ngc) < 0) { 222 perror("Cannot CONNECT PPPoE and socket nodes"); 223 return EX_OSERR; 224 } 225 226 plen = strlen(provider); 227 228 data = (struct ngpppoe_init_data *)alloca(sizeof *data + plen); 229 snprintf(data->hook, sizeof data->hook, "%s", ngc->peerhook); 230 memcpy(data->data, provider, plen); 231 data->data_len = plen; 232 233 spath = (char *)alloca(strlen(ngc->peerhook) + 3); 234 strcpy(spath, ".:"); 235 strcpy(spath + 2, ngc->ourhook); 236 237 if (debug) { 238 if (provider) 239 fprintf(stderr, "Sending PPPOE_LISTEN to %s, provider %s\n", 240 spath, provider); 241 else 242 fprintf(stderr, "Sending PPPOE_LISTEN to %s\n", spath); 243 } 244 245 if (NgSendMsg(cs, spath, NGM_PPPOE_COOKIE, NGM_PPPOE_LISTEN, 246 data, sizeof *data + plen) == -1) { 247 fprintf(stderr, "%s: Cannot LISTEN on netgraph node: %s\n", 248 spath, strerror(errno)); 249 return EX_OSERR; 250 } 251 252 return 0; 253 } 254 255 static void 256 Spawn(const char *prog __unused, const char *acname, const char *provider, 257 const char *exec, struct ngm_connect ngc, int cs, int ds, void *request, 258 size_t sz, int debug) 259 { 260 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)]; 261 struct ng_mesg *rep = (struct ng_mesg *)msgbuf; 262 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep); 263 struct ngpppoe_init_data *data; 264 char env[18], unknown[14], sessionid[5], *path; 265 unsigned char *macaddr; 266 const char *msg; 267 int ret, slen; 268 269 switch ((ret = fork())) { 270 case -1: 271 syslog(LOG_ERR, "fork: %m"); 272 break; 273 274 case 0: 275 switch (fork()) { 276 case 0: 277 break; 278 case -1: 279 _exit(errno); 280 default: 281 _exit(0); 282 } 283 close(cs); 284 close(ds); 285 286 /* Create a new socket node */ 287 if (debug) 288 syslog(LOG_INFO, "Creating a new socket node"); 289 290 if (NgMkSockNode(NULL, &cs, &ds) == -1) { 291 syslog(LOG_ERR, "Cannot create netgraph socket node: %m"); 292 _exit(EX_CANTCREAT); 293 } 294 295 /* Connect the PPPoE node to our new socket node. */ 296 snprintf(ngc.ourhook, sizeof ngc.ourhook, "exec-%ld", (long)getpid()); 297 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook); 298 299 if (debug) 300 syslog(LOG_INFO, "Sending CONNECT from .:%s -> %s.%s", 301 ngc.ourhook, ngc.path, ngc.peerhook); 302 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, 303 NGM_CONNECT, &ngc, sizeof ngc) < 0) { 304 syslog(LOG_ERR, "Cannot CONNECT PPPoE and socket nodes: %m"); 305 _exit(EX_OSERR); 306 } 307 308 /* 309 * If we tell the socket node not to LINGER, it will go away when 310 * the last hook is removed. 311 */ 312 if (debug) 313 syslog(LOG_INFO, "Sending NGM_SOCK_CMD_NOLINGER to socket"); 314 if (NgSendMsg(cs, ".:", NGM_SOCKET_COOKIE, 315 NGM_SOCK_CMD_NOLINGER, NULL, 0) < 0) { 316 syslog(LOG_ERR, "Cannot send NGM_SOCK_CMD_NOLINGER: %m"); 317 _exit(EX_OSERR); 318 } 319 320 /* Put the PPPoE node into OFFER mode */ 321 slen = strlen(acname); 322 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen); 323 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook); 324 memcpy(data->data, acname, slen); 325 data->data_len = slen; 326 327 path = (char *)alloca(strlen(ngc.ourhook) + 3); 328 strcpy(path, ".:"); 329 strcpy(path + 2, ngc.ourhook); 330 331 syslog(LOG_INFO, "Offering to %s as access concentrator %s", 332 path, acname); 333 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_OFFER, 334 data, sizeof *data + slen) == -1) { 335 syslog(LOG_INFO, "%s: Cannot OFFER on netgraph node: %m", path); 336 _exit(EX_OSERR); 337 } 338 /* If we have a provider code, set it */ 339 if (provider) { 340 slen = strlen(provider); 341 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen); 342 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook); 343 memcpy(data->data, provider, slen); 344 data->data_len = slen; 345 346 syslog(LOG_INFO, "adding to %s as offered service %s", 347 path, acname); 348 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_SERVICE, 349 data, sizeof *data + slen) == -1) { 350 syslog(LOG_INFO, "%s: Cannot add service on netgraph node: %m", path); 351 _exit(EX_OSERR); 352 } 353 } 354 355 /* Put the peer's MAC address in the environment */ 356 if (sz >= sizeof(struct ether_header)) { 357 macaddr = ((struct ether_header *)request)->ether_shost; 358 snprintf(env, sizeof(env), "%x:%x:%x:%x:%x:%x", 359 macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], 360 macaddr[5]); 361 if (setenv(HISMACADDR, env, 1) != 0) 362 syslog(LOG_INFO, "setenv: cannot set %s: %m", HISMACADDR); 363 } 364 365 /* And send our request data to the waiting node */ 366 if (debug) 367 syslog(LOG_INFO, "Sending original request to %s (%zu bytes)", path, sz); 368 if (NgSendData(ds, ngc.ourhook, request, sz) == -1) { 369 syslog(LOG_ERR, "Cannot send original request to %s: %m", path); 370 _exit(EX_OSERR); 371 } 372 373 /* Then wait for a success indication */ 374 375 if (debug) 376 syslog(LOG_INFO, "Waiting for a SUCCESS reply %s", path); 377 378 do { 379 if ((ret = NgRecvMsg(cs, rep, sizeof msgbuf, NULL)) < 0) { 380 syslog(LOG_ERR, "%s: Cannot receive a message: %m", path); 381 _exit(EX_OSERR); 382 } 383 384 if (ret == 0) { 385 /* The socket has been closed */ 386 syslog(LOG_INFO, "%s: Client timed out", path); 387 _exit(EX_TEMPFAIL); 388 } 389 390 if (rep->header.version != NG_VERSION) { 391 syslog(LOG_ERR, "%ld: Unexpected netgraph version, expected %ld", 392 (long)rep->header.version, (long)NG_VERSION); 393 _exit(EX_PROTOCOL); 394 } 395 396 if (rep->header.typecookie != NGM_PPPOE_COOKIE) { 397 syslog(LOG_INFO, "%ld: Unexpected netgraph cookie, expected %ld", 398 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE); 399 continue; 400 } 401 402 switch (rep->header.cmd) { 403 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break; 404 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break; 405 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break; 406 case NGM_PPPOE_OFFER: msg = "OFFER"; break; 407 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break; 408 case NGM_PPPOE_FAIL: msg = "FAIL"; break; 409 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break; 410 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break; 411 case NGM_PPPOE_ACNAME: 412 msg = "ACNAME"; 413 if (setenv("ACNAME", sts->hook, 1) != 0) 414 syslog(LOG_WARNING, "setenv: cannot set ACNAME=%s: %m", 415 sts->hook); 416 break; 417 case NGM_PPPOE_SESSIONID: 418 msg = "SESSIONID"; 419 snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts); 420 if (setenv("SESSIONID", sessionid, 1) != 0) 421 syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m", 422 sessionid); 423 break; 424 default: 425 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd); 426 msg = unknown; 427 break; 428 } 429 430 switch (rep->header.cmd) { 431 case NGM_PPPOE_FAIL: 432 case NGM_PPPOE_CLOSE: 433 syslog(LOG_ERR, "Received NGM_PPPOE_%s (hook \"%s\")", 434 msg, sts->hook); 435 _exit(0); 436 } 437 438 syslog(LOG_INFO, "Received NGM_PPPOE_%s (hook \"%s\")", msg, sts->hook); 439 } while (rep->header.cmd != NGM_PPPOE_SUCCESS); 440 441 dup2(ds, STDIN_FILENO); 442 dup2(ds, STDOUT_FILENO); 443 close(ds); 444 close(cs); 445 446 setsid(); 447 syslog(LOG_INFO, "Executing: %s", exec); 448 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", exec, NULL); 449 syslog(LOG_ERR, "execlp failed: %m"); 450 _exit(EX_OSFILE); 451 452 default: 453 wait(&ret); 454 errno = ret; 455 if (errno) 456 syslog(LOG_ERR, "Second fork failed: %m"); 457 break; 458 } 459 } 460 461 #ifndef NOKLDLOAD 462 static int 463 LoadModules(void) 464 { 465 const char *module[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" }; 466 size_t f; 467 468 for (f = 0; f < sizeof module / sizeof *module; f++) 469 if (modfind(module[f]) == -1 && kldload(module[f]) == -1) { 470 fprintf(stderr, "kldload: %s: %s\n", module[f], strerror(errno)); 471 return 0; 472 } 473 474 return 1; 475 } 476 #endif 477 478 static void 479 nglog(const char *fmt, ...) 480 { 481 char nfmt[256]; 482 va_list ap; 483 484 snprintf(nfmt, sizeof nfmt, "%s: %s", fmt, strerror(errno)); 485 va_start(ap, fmt); 486 vsyslog(LOG_INFO, nfmt, ap); 487 va_end(ap); 488 } 489 490 static void 491 nglogx(const char *fmt, ...) 492 { 493 va_list ap; 494 495 va_start(ap, fmt); 496 vsyslog(LOG_INFO, fmt, ap); 497 va_end(ap); 498 } 499 500 int 501 main(int argc, char *argv[]) 502 { 503 char hostname[MAXHOSTNAMELEN], *exec, rhook[NG_HOOKSIZ]; 504 unsigned char response[1024]; 505 const char *label, *prog, *provider, *acname; 506 struct ngm_connect ngc; 507 struct sigaction act; 508 int ch, cs, ds, ret, optF, optd, optn, sz, f; 509 const char *pidfile; 510 511 prog = strrchr(argv[0], '/'); 512 prog = prog ? prog + 1 : argv[0]; 513 pidfile = NULL; 514 exec = NULL; 515 label = NULL; 516 acname = NULL; 517 provider = ""; 518 optF = optd = optn = 0; 519 520 while ((ch = getopt(argc, argv, "FP:a:de:l:n:p:")) != -1) { 521 switch (ch) { 522 case 'F': 523 optF = 1; 524 break; 525 526 case 'P': 527 pidfile = optarg; 528 break; 529 530 case 'a': 531 acname = optarg; 532 break; 533 534 case 'd': 535 optd = 1; 536 break; 537 538 case 'e': 539 exec = optarg; 540 break; 541 542 case 'l': 543 label = optarg; 544 break; 545 546 case 'n': 547 optn = 1; 548 NgSetDebug(atoi(optarg)); 549 break; 550 551 case 'p': 552 provider = optarg; 553 break; 554 555 default: 556 return usage(prog); 557 } 558 } 559 560 if (optind >= argc || optind + 2 < argc) 561 return usage(prog); 562 563 if (exec != NULL && label != NULL) 564 return usage(prog); 565 566 if (exec == NULL) { 567 if (label == NULL) 568 label = provider; 569 if (label == NULL) { 570 fprintf(stderr, "%s: Either a provider, a label or an exec command" 571 " must be given\n", prog); 572 return usage(prog); 573 } 574 exec = (char *)alloca(sizeof DEFAULT_EXEC_PREFIX + strlen(label)); 575 if (exec == NULL) { 576 fprintf(stderr, "%s: Cannot allocate %zu bytes\n", prog, 577 sizeof DEFAULT_EXEC_PREFIX + strlen(label)); 578 return EX_OSERR; 579 } 580 strcpy(exec, DEFAULT_EXEC_PREFIX); 581 strcpy(exec + sizeof DEFAULT_EXEC_PREFIX - 1, label); 582 } 583 584 if (acname == NULL) { 585 char *dot; 586 587 if (gethostname(hostname, sizeof hostname)) 588 strcpy(hostname, "localhost"); 589 else if ((dot = strchr(hostname, '.'))) 590 *dot = '\0'; 591 592 acname = hostname; 593 } 594 595 #ifndef NOKLDLOAD 596 if (!LoadModules()) 597 return EX_UNAVAILABLE; 598 #endif 599 600 /* Create a socket node */ 601 if (NgMkSockNode(NULL, &cs, &ds) == -1) { 602 perror("Cannot create netgraph socket node"); 603 return EX_CANTCREAT; 604 } 605 606 /* Connect it up (and fill in `ngc') */ 607 if ((ret = ConfigureNode(prog, argv[optind], provider, cs, ds, 608 optd, &ngc)) != 0) { 609 close(cs); 610 close(ds); 611 return ret; 612 } 613 614 if (!optF && daemon(1, 0) == -1) { 615 perror("daemon()"); 616 close(cs); 617 close(ds); 618 return EX_OSERR; 619 } 620 621 622 if (pidfile != NULL) { 623 FILE *fp; 624 625 if ((fp = fopen(pidfile, "w")) == NULL) { 626 perror(pidfile); 627 close(cs); 628 close(ds); 629 return EX_CANTCREAT; 630 } else { 631 fprintf(fp, "%d\n", (int)getpid()); 632 fclose(fp); 633 } 634 } 635 636 openlog(prog, LOG_PID | (optF ? LOG_PERROR : 0), LOG_DAEMON); 637 if (!optF && optn) 638 NgSetErrLog(nglog, nglogx); 639 640 memset(&act, '\0', sizeof act); 641 act.sa_handler = Farewell; 642 act.sa_flags = 0; 643 sigemptyset(&act.sa_mask); 644 sigaction(SIGHUP, &act, NULL); 645 sigaction(SIGINT, &act, NULL); 646 sigaction(SIGQUIT, &act, NULL); 647 sigaction(SIGTERM, &act, NULL); 648 649 while (!ReceivedSignal) { 650 if (*provider) 651 syslog(LOG_INFO, "Listening as provider %s", provider); 652 else 653 syslog(LOG_INFO, "Listening"); 654 655 switch (sz = NgRecvData(ds, response, sizeof response, rhook)) { 656 case -1: 657 syslog(LOG_INFO, "NgRecvData: %m"); 658 break; 659 case 0: 660 syslog(LOG_INFO, "NgRecvData: socket closed"); 661 break; 662 default: 663 if (optd) { 664 char *dbuf, *ptr; 665 666 ptr = dbuf = alloca(sz * 2 + 1); 667 for (f = 0; f < sz; f++, ptr += 2) 668 sprintf(ptr, "%02x", (u_char)response[f]); 669 *ptr = '\0'; 670 syslog(LOG_INFO, "Got %d bytes of data: %s", sz, dbuf); 671 } 672 } 673 if (sz <= 0) { 674 ret = EX_UNAVAILABLE; 675 break; 676 } 677 Spawn(prog, acname, provider, exec, ngc, cs, ds, response, sz, optd); 678 } 679 680 if (pidfile) 681 remove(pidfile); 682 683 if (ReceivedSignal) { 684 syslog(LOG_INFO, "Received signal %d, exiting", ReceivedSignal); 685 686 signal(ReceivedSignal, SIG_DFL); 687 raise(ReceivedSignal); 688 689 /* NOTREACHED */ 690 691 ret = -ReceivedSignal; 692 } 693 694 return ret; 695 } 696