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