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