]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/pppoed/pppoed.c
This commit was generated by cvs2svn to compensate for changes in r90926,
[FreeBSD/FreeBSD.git] / libexec / pppoed / pppoed.c
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           default:
409             snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
410             msg = unknown;
411             break;
412         }
413
414         switch (rep->header.cmd) {
415           case NGM_PPPOE_FAIL:
416           case NGM_PPPOE_CLOSE:
417             syslog(LOG_ERR, "Received NGM_PPPOE_%s (hook \"%s\")",
418                    msg, sts->hook);
419             _exit(0);
420         }
421
422         syslog(LOG_INFO, "Received NGM_PPPOE_%s (hook \"%s\")", msg, sts->hook);
423       } while (rep->header.cmd != NGM_PPPOE_SUCCESS);
424
425       dup2(ds, STDIN_FILENO);
426       dup2(ds, STDOUT_FILENO);
427       close(ds);
428       close(cs);
429
430       setsid();
431       syslog(LOG_INFO, "Executing: %s", exec);
432       execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", exec, (char *)NULL);
433       syslog(LOG_ERR, "execlp failed: %m");
434       _exit(EX_OSFILE);
435
436     default:
437       wait(&ret);
438       errno = ret;
439       if (errno)
440         syslog(LOG_ERR, "Second fork failed: %m");
441       break;
442   }
443 }
444
445 #ifndef NOKLDLOAD
446 static int
447 LoadModules(void)
448 {
449   const char *module[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" };
450   int f;
451
452   for (f = 0; f < sizeof module / sizeof *module; f++)
453     if (modfind(module[f]) == -1 && kldload(module[f]) == -1) {
454       fprintf(stderr, "kldload: %s: %s\n", module[f], strerror(errno));
455       return 0;
456     }
457
458   return 1;
459 }
460 #endif
461
462 static void
463 nglog(const char *fmt, ...)
464 {
465   char nfmt[256];
466   va_list ap;
467
468   snprintf(nfmt, sizeof nfmt, "%s: %s", fmt, strerror(errno));
469   va_start(ap, fmt);
470   vsyslog(LOG_INFO, nfmt, ap);
471   va_end(ap);
472 }
473
474 static void
475 nglogx(const char *fmt, ...)
476 {
477   va_list ap;
478
479   va_start(ap, fmt);
480   vsyslog(LOG_INFO, fmt, ap);
481   va_end(ap);
482 }
483
484 int
485 main(int argc, char *argv[])
486 {
487   char hostname[MAXHOSTNAMELEN], *exec, rhook[NG_HOOKLEN + 1];
488   unsigned char response[1024];
489   const char *label, *prog, *provider, *acname;
490   struct ngm_connect ngc;
491   struct sigaction act;
492   int ch, cs, ds, ret, optF, optd, optn, sz, f;
493   const char *pidfile;
494
495   prog = strrchr(argv[0], '/');
496   prog = prog ? prog + 1 : argv[0];
497   pidfile = NULL;
498   exec = NULL;
499   label = NULL;
500   acname = NULL;
501   provider = "";
502   optF = optd = optn = 0;
503
504   while ((ch = getopt(argc, argv, "FP:a:de:l:n:p:")) != -1) {
505     switch (ch) {
506       case 'F':
507         optF = 1;
508         break;
509
510       case 'P':
511         pidfile = optarg;
512         break;
513
514       case 'a':
515         acname = optarg;
516         break;
517
518       case 'd':
519         optd = 1;
520         break;
521
522       case 'e':
523         exec = optarg;
524         break;
525
526       case 'l':
527         label = optarg;
528         break;
529
530       case 'n':
531         optn = 1;
532         NgSetDebug(atoi(optarg));
533         break;
534
535       case 'p':
536         provider = optarg;
537         break;
538
539       default:
540         return usage(prog);
541     }
542   }
543
544   if (optind >= argc || optind + 2 < argc)
545     return usage(prog);
546
547   if (exec != NULL && label != NULL)
548     return usage(prog);
549
550   if (exec == NULL) {
551     if (label == NULL)
552       label = provider;
553     if (label == NULL) {
554       fprintf(stderr, "%s: Either a provider, a label or an exec command"
555               " must be given\n", prog);
556       return usage(prog);
557     }
558     exec = (char *)alloca(sizeof DEFAULT_EXEC_PREFIX + strlen(label));
559     if (exec == NULL) {
560       fprintf(stderr, "%s: Cannot allocate %d bytes\n", prog,
561               (int)(sizeof DEFAULT_EXEC_PREFIX) + strlen(label));
562       return EX_OSERR;
563     }
564     strcpy(exec, DEFAULT_EXEC_PREFIX);
565     strcpy(exec + sizeof DEFAULT_EXEC_PREFIX - 1, label);
566   }
567
568   if (acname == NULL) {
569     char *dot;
570
571     if (gethostname(hostname, sizeof hostname))
572       strcpy(hostname, "localhost");
573     else if ((dot = strchr(hostname, '.')))
574       *dot = '\0';
575
576     acname = hostname;
577   }
578
579 #ifndef NOKLDLOAD
580   if (!LoadModules())
581     return EX_UNAVAILABLE;
582 #endif
583
584   /* Create a socket node */
585   if (NgMkSockNode(NULL, &cs, &ds) == -1) {
586     perror("Cannot create netgraph socket node");
587     return EX_CANTCREAT;
588   }
589
590   /* Connect it up (and fill in `ngc') */
591   if ((ret = ConfigureNode(prog, argv[optind], provider, cs, ds,
592                            optd, &ngc)) != 0) {
593     close(cs);
594     close(ds);
595     return ret;
596   }
597
598   if (!optF && daemon(1, 0) == -1) {
599     perror("daemon()");
600     close(cs);
601     close(ds);
602     return EX_OSERR;
603   }
604
605
606   if (pidfile != NULL) {
607     FILE *fp;
608
609     if ((fp = fopen(pidfile, "w")) == NULL) {
610       perror(pidfile);
611       close(cs);
612       close(ds);
613       return EX_CANTCREAT;
614     } else {
615       fprintf(fp, "%d\n", (int)getpid());
616       fclose(fp);
617     }
618   }
619
620   openlog(prog, LOG_PID | (optF ? LOG_PERROR : 0), LOG_DAEMON);
621   if (!optF && optn)
622     NgSetErrLog(nglog, nglogx);
623
624   memset(&act, '\0', sizeof act);
625   act.sa_handler = Farewell;
626   act.sa_flags = 0;
627   sigemptyset(&act.sa_mask);
628   sigaction(SIGHUP, &act, NULL);
629   sigaction(SIGINT, &act, NULL);
630   sigaction(SIGQUIT, &act, NULL);
631   sigaction(SIGTERM, &act, NULL);
632
633   while (!ReceivedSignal) {
634     if (*provider)
635       syslog(LOG_INFO, "Listening as provider %s", provider);
636     else
637       syslog(LOG_INFO, "Listening");
638
639     switch (sz = NgRecvData(ds, response, sizeof response, rhook)) {
640       case -1:
641         syslog(LOG_INFO, "NgRecvData: %m");
642         break;
643       case 0:
644         syslog(LOG_INFO, "NgRecvData: socket closed");
645         break;
646       default:
647         if (optd) {
648           char *dbuf, *ptr;
649
650           ptr = dbuf = alloca(sz * 2 + 1);
651           for (f = 0; f < sz; f++, ptr += 2)
652             sprintf(ptr, "%02x", (u_char)response[f]);
653           *ptr = '\0';
654           syslog(LOG_INFO, "Got %d bytes of data: %s", sz, dbuf);
655         }
656     }
657     if (sz <= 0) {
658       ret = EX_UNAVAILABLE;
659       break;
660     }
661     Spawn(prog, acname, provider, exec, ngc, cs, ds, response, sz, optd);
662   }
663
664   if (pidfile)
665     remove(pidfile);
666
667   if (ReceivedSignal) {
668     syslog(LOG_INFO, "Received signal %d, exiting", ReceivedSignal);
669
670     signal(ReceivedSignal, SIG_DFL);
671     raise(ReceivedSignal);
672
673     /* NOTREACHED */
674
675     ret = -ReceivedSignal;
676   }
677
678   return ret;
679 }