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