lpd.c revision 1.24 1 /* $NetBSD: lpd.c,v 1.24 2000/10/03 13:28:27 scw Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38
39 #ifndef lint
40 __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n");
42 #endif /* not lint */
43
44 #ifndef lint
45 #if 0
46 static char sccsid[] = "@(#)lpd.c 8.7 (Berkeley) 5/10/95";
47 #else
48 __RCSID("$NetBSD: lpd.c,v 1.24 2000/10/03 13:28:27 scw Exp $");
49 #endif
50 #endif /* not lint */
51
52 /*
53 * lpd -- line printer daemon.
54 *
55 * Listen for a connection and perform the requested operation.
56 * Operations are:
57 * \1printer\n
58 * check the queue for jobs and print any found.
59 * \2printer\n
60 * receive a job from another machine and queue it.
61 * \3printer [users ...] [jobs ...]\n
62 * return the current state of the queue (short form).
63 * \4printer [users ...] [jobs ...]\n
64 * return the current state of the queue (long form).
65 * \5printer person [users ...] [jobs ...]\n
66 * remove jobs from the queue.
67 *
68 * Strategy to maintain protected spooling area:
69 * 1. Spooling area is writable only by daemon and spooling group
70 * 2. lpr runs setuid root and setgrp spooling group; it uses
71 * root to access any file it wants (verifying things before
72 * with an access call) and group id to know how it should
73 * set up ownership of files in the spooling area.
74 * 3. Files in spooling area are owned by root, group spooling
75 * group, with mode 660.
76 * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
77 * access files and printer. Users can't get to anything
78 * w/o help of lpq and lprm programs.
79 */
80
81 #include <sys/param.h>
82 #include <sys/wait.h>
83 #include <sys/types.h>
84 #include <sys/socket.h>
85 #include <sys/un.h>
86 #include <sys/stat.h>
87 #include <sys/file.h>
88 #include <netinet/in.h>
89
90 #include <err.h>
91 #include <netdb.h>
92 #include <unistd.h>
93 #include <syslog.h>
94 #include <signal.h>
95 #include <errno.h>
96 #include <fcntl.h>
97 #include <dirent.h>
98 #include <stdio.h>
99 #include <stdlib.h>
100 #include <string.h>
101 #include <ctype.h>
102 #include <arpa/inet.h>
103
104 #include "lp.h"
105 #include "lp.local.h"
106 #include "pathnames.h"
107 #include "extern.h"
108
109 /* XXX from libc/net/rcmd.c */
110 extern int __ivaliduser_sa __P((FILE *, struct sockaddr *, socklen_t,
111 const char *, const char *));
112
113 int lflag; /* log requests flag */
114 int rflag; /* allow of for remote printers */
115 int sflag; /* secure (no inet) flag */
116 int from_remote; /* from remote socket */
117 char **blist; /* list of addresses to bind(2) to */
118 int blist_size;
119 int blist_addrs;
120
121 int main __P((int, char **));
122 static void reapchild __P((int));
123 static void mcleanup __P((int));
124 static void doit __P((void));
125 static void startup __P((void));
126 static void chkhost __P((struct sockaddr *));
127 static int ckqueue __P((char *));
128 static void usage __P((void));
129 static int *socksetup __P((int, int));
130
131 uid_t uid, euid;
132 int child_count;
133
134 int
135 main(argc, argv)
136 int argc;
137 char **argv;
138 {
139 fd_set defreadfds;
140 struct sockaddr_un un, fromunix;
141 struct sockaddr_storage frominet;
142 sigset_t nmask, omask;
143 int lfd, errs, i, f, funix, *finet;
144 int child_max = 32; /* more then enough to hose the system */
145 int options = 0;
146 struct servent *sp, serv;
147
148 euid = geteuid(); /* these shouldn't be different */
149 uid = getuid();
150 gethostname(host, sizeof(host));
151 host[sizeof(host) - 1] = '\0';
152 name = argv[0];
153
154 errs = 0;
155 while ((i = getopt(argc, argv, "b:dln:srw:")) != -1)
156 switch (i) {
157 case 'b':
158 if (blist_addrs >= blist_size) {
159 blist_size += sizeof(char *) * 4;
160 if (blist == NULL)
161 blist = malloc(blist_size);
162 else
163 blist = realloc(blist, blist_size);
164 if (blist == NULL)
165 err(1, "cant allocate bind addr list");
166 }
167 blist[blist_addrs++] = strdup(optarg);
168 break;
169 case 'd':
170 options |= SO_DEBUG;
171 break;
172 case 'l':
173 lflag++;
174 break;
175 case 'n':
176 child_max = atoi(optarg);
177 if (child_max < 0 || child_max > 1024)
178 errx(1, "invalid number of children: %s",
179 optarg);
180 break;
181 case 'r':
182 rflag++;
183 break;
184 case 's':
185 sflag++;
186 break;
187 case 'w':
188 wait_time = atoi(optarg);
189 if (wait_time < 0)
190 errx(1, "wait time must be postive: %s",
191 optarg);
192 if (wait_time < 30)
193 warnx("warning: wait time less than 30 seconds");
194 break;
195 default:
196 errs++;
197 }
198 argc -= optind;
199 argv += optind;
200 if (errs)
201 usage();
202
203 switch (argc) {
204 case 1:
205 if ((i = atoi(argv[0])) == 0)
206 usage();
207 if (i < 0 || i > USHRT_MAX)
208 errx(1, "port # %d is invalid", i);
209
210 serv.s_port = htons(i);
211 sp = &serv;
212 break;
213 case 0:
214 sp = getservbyname("printer", "tcp");
215 if (sp == NULL)
216 errx(1, "printer/tcp: unknown service");
217 break;
218 default:
219 usage();
220 }
221
222 #ifndef DEBUG
223 /*
224 * Set up standard environment by detaching from the parent.
225 */
226 daemon(0, 0);
227 #endif
228
229 openlog("lpd", LOG_PID, LOG_LPR);
230 syslog(LOG_INFO, "restarted");
231 (void)umask(0);
232 lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
233 if (lfd < 0) {
234 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
235 exit(1);
236 }
237 if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
238 if (errno == EWOULDBLOCK) /* active deamon present */
239 exit(0);
240 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
241 exit(1);
242 }
243 ftruncate(lfd, 0);
244 /*
245 * write process id for others to know
246 */
247 (void)snprintf(line, sizeof(line), "%u\n", getpid());
248 f = strlen(line);
249 if (write(lfd, line, f) != f) {
250 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
251 exit(1);
252 }
253 signal(SIGCHLD, reapchild);
254 /*
255 * Restart all the printers.
256 */
257 startup();
258 (void)unlink(_PATH_SOCKETNAME);
259 funix = socket(AF_LOCAL, SOCK_STREAM, 0);
260 if (funix < 0) {
261 syslog(LOG_ERR, "socket: %m");
262 exit(1);
263 }
264
265 sigemptyset(&nmask);
266 sigaddset(&nmask, SIGHUP);
267 sigaddset(&nmask, SIGINT);
268 sigaddset(&nmask, SIGQUIT);
269 sigaddset(&nmask, SIGTERM);
270 sigprocmask(SIG_BLOCK, &nmask, &omask);
271
272 (void) umask(07);
273 signal(SIGHUP, mcleanup);
274 signal(SIGINT, mcleanup);
275 signal(SIGQUIT, mcleanup);
276 signal(SIGTERM, mcleanup);
277 memset(&un, 0, sizeof(un));
278 un.sun_family = AF_LOCAL;
279 strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
280 #ifndef SUN_LEN
281 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
282 #endif
283 if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
284 syslog(LOG_ERR, "ubind: %m");
285 exit(1);
286 }
287 (void) umask(0);
288 sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
289 FD_ZERO(&defreadfds);
290 FD_SET(funix, &defreadfds);
291 listen(funix, 5);
292 if (!sflag || blist_addrs)
293 finet = socksetup(PF_UNSPEC, options);
294 else
295 finet = NULL; /* pretend we couldn't open TCP socket. */
296
297 if (blist != NULL) {
298 for (i = 0; i < blist_addrs; i++)
299 free(blist[i]);
300 free(blist);
301 }
302
303 if (finet) {
304 for (i = 1; i <= *finet; i++) {
305 FD_SET(finet[i], &defreadfds);
306 listen(finet[i], 5);
307 }
308 }
309 /*
310 * Main loop: accept, do a request, continue.
311 */
312 memset(&frominet, 0, sizeof(frominet));
313 memset(&fromunix, 0, sizeof(fromunix));
314 for (;;) {
315 int domain, nfds, s, fromlen;
316 fd_set readfds;
317 /* "short" so it overflows in about 2 hours */
318 short sleeptime = 10;
319
320 while (child_max < child_count) {
321 syslog(LOG_WARNING,
322 "too many children, sleeping for %d seconds",
323 sleeptime);
324 sleep(sleeptime);
325 sleeptime <<= 1;
326 if (sleeptime < 0) {
327 syslog(LOG_CRIT, "sleeptime overflowed! help!");
328 sleeptime = 10;
329 }
330 }
331
332 FD_COPY(&defreadfds, &readfds);
333 nfds = select(20, &readfds, 0, 0, 0);
334 if (nfds <= 0) {
335 if (nfds < 0 && errno != EINTR)
336 syslog(LOG_WARNING, "select: %m");
337 continue;
338 }
339 if (FD_ISSET(funix, &readfds)) {
340 domain = AF_LOCAL;
341 fromlen = sizeof(fromunix);
342 s = accept(funix,
343 (struct sockaddr *)&fromunix, &fromlen);
344 } else {
345 for (i = 1; i <= *finet; i++)
346 if (FD_ISSET(finet[i], &readfds)) {
347 domain = AF_INET, fromlen = sizeof(frominet);
348 s = accept(finet[i], (struct sockaddr *)&frominet, &fromlen);
349 }
350 }
351 if (s < 0) {
352 if (errno != EINTR)
353 syslog(LOG_WARNING, "accept: %m");
354 continue;
355 }
356
357 switch (fork()) {
358 case 0:
359 signal(SIGCHLD, SIG_IGN);
360 signal(SIGHUP, SIG_IGN);
361 signal(SIGINT, SIG_IGN);
362 signal(SIGQUIT, SIG_IGN);
363 signal(SIGTERM, SIG_IGN);
364 (void)close(funix);
365 if (!sflag && finet)
366 for (i = 1; i <= *finet; i++)
367 (void)close(finet[i]);
368 dup2(s, 1);
369 (void)close(s);
370 if (domain == AF_INET) {
371 /* for both AF_INET and AF_INET6 */
372 from_remote = 1;
373 chkhost((struct sockaddr *)&frominet);
374 } else
375 from_remote = 0;
376 doit();
377 exit(0);
378 case -1:
379 syslog(LOG_WARNING, "fork: %m, sleeping for 10 seconds...");
380 sleep(10);
381 continue;
382 default:
383 child_count++;
384 }
385 (void)close(s);
386 }
387 }
388
389 static void
390 reapchild(signo)
391 int signo;
392 {
393 union wait status;
394
395 while (wait3((int *)&status, WNOHANG, 0) > 0)
396 child_count--;
397 }
398
399 static void
400 mcleanup(signo)
401 int signo;
402 {
403 if (lflag)
404 syslog(LOG_INFO, "exiting");
405 unlink(_PATH_SOCKETNAME);
406 exit(0);
407 }
408
409 /*
410 * Stuff for handling job specifications
411 */
412 char *user[MAXUSERS]; /* users to process */
413 int users; /* # of users in user array */
414 int requ[MAXREQUESTS]; /* job number of spool entries */
415 int requests; /* # of spool requests */
416 char *person; /* name of person doing lprm */
417
418 char fromb[NI_MAXHOST]; /* buffer for client's machine name */
419 char cbuf[BUFSIZ]; /* command line buffer */
420 char *cmdnames[] = {
421 "null",
422 "printjob",
423 "recvjob",
424 "displayq short",
425 "displayq long",
426 "rmjob"
427 };
428
429 static void
430 doit()
431 {
432 char *cp;
433 int n;
434
435 for (;;) {
436 cp = cbuf;
437 do {
438 if (cp >= &cbuf[sizeof(cbuf) - 1])
439 fatal("Command line too long");
440 if ((n = read(1, cp, 1)) != 1) {
441 if (n < 0)
442 fatal("Lost connection");
443 return;
444 }
445 } while (*cp++ != '\n');
446 *--cp = '\0';
447 cp = cbuf;
448 if (lflag) {
449 if (*cp >= '\1' && *cp <= '\5') {
450 syslog(LOG_INFO, "%s requests %s %s",
451 from, cmdnames[(int)*cp], cp+1);
452 setproctitle("serving %s: %s %s", from,
453 cmdnames[(int)*cp], cp+1);
454 }
455 else
456 syslog(LOG_INFO, "bad request (%d) from %s",
457 *cp, from);
458 }
459 switch (*cp++) {
460 case '\1': /* check the queue and print any jobs there */
461 printer = cp;
462 printjob();
463 break;
464 case '\2': /* receive files to be queued */
465 if (!from_remote) {
466 syslog(LOG_INFO, "illegal request (%d)", *cp);
467 exit(1);
468 }
469 printer = cp;
470 recvjob();
471 break;
472 case '\3': /* display the queue (short form) */
473 case '\4': /* display the queue (long form) */
474 printer = cp;
475 while (*cp) {
476 if (*cp != ' ') {
477 cp++;
478 continue;
479 }
480 *cp++ = '\0';
481 while (isspace(*cp))
482 cp++;
483 if (*cp == '\0')
484 break;
485 if (isdigit(*cp)) {
486 if (requests >= MAXREQUESTS)
487 fatal("Too many requests");
488 requ[requests++] = atoi(cp);
489 } else {
490 if (users >= MAXUSERS)
491 fatal("Too many users");
492 user[users++] = cp;
493 }
494 }
495 displayq(cbuf[0] - '\3');
496 exit(0);
497 case '\5': /* remove a job from the queue */
498 if (!from_remote) {
499 syslog(LOG_INFO, "illegal request (%d)", *cp);
500 exit(1);
501 }
502 printer = cp;
503 while (*cp && *cp != ' ')
504 cp++;
505 if (!*cp)
506 break;
507 *cp++ = '\0';
508 person = cp;
509 while (*cp) {
510 if (*cp != ' ') {
511 cp++;
512 continue;
513 }
514 *cp++ = '\0';
515 while (isspace(*cp))
516 cp++;
517 if (*cp == '\0')
518 break;
519 if (isdigit(*cp)) {
520 if (requests >= MAXREQUESTS)
521 fatal("Too many requests");
522 requ[requests++] = atoi(cp);
523 } else {
524 if (users >= MAXUSERS)
525 fatal("Too many users");
526 user[users++] = cp;
527 }
528 }
529 rmjob();
530 break;
531 }
532 fatal("Illegal service request");
533 }
534 }
535
536 /*
537 * Make a pass through the printcap database and start printing any
538 * files left from the last time the machine went down.
539 */
540 static void
541 startup()
542 {
543 char *buf;
544 char *cp;
545
546 /*
547 * Restart the daemons.
548 */
549 while (cgetnext(&buf, printcapdb) > 0) {
550 if (ckqueue(buf) <= 0) {
551 free(buf);
552 continue; /* no work to do for this printer */
553 }
554 for (cp = buf; *cp; cp++)
555 if (*cp == '|' || *cp == ':') {
556 *cp = '\0';
557 break;
558 }
559 if (lflag)
560 syslog(LOG_INFO, "work for %s", buf);
561 switch (fork()) {
562 case -1:
563 syslog(LOG_WARNING, "startup: cannot fork");
564 mcleanup(0);
565 case 0:
566 printer = buf;
567 setproctitle("working on printer %s", printer);
568 cgetclose();
569 printjob();
570 /* NOTREACHED */
571 default:
572 child_count++;
573 free(buf);
574 }
575 }
576 }
577
578 /*
579 * Make sure there's some work to do before forking off a child
580 */
581 static int
582 ckqueue(cap)
583 char *cap;
584 {
585 struct dirent *d;
586 DIR *dirp;
587 char *spooldir;
588
589 if (cgetstr(cap, "sd", &spooldir) == -1)
590 spooldir = _PATH_DEFSPOOL;
591 if ((dirp = opendir(spooldir)) == NULL)
592 return (-1);
593 while ((d = readdir(dirp)) != NULL) {
594 if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
595 continue; /* daemon control files only */
596 closedir(dirp);
597 return (1); /* found something */
598 }
599 closedir(dirp);
600 return (0);
601 }
602
603 #define DUMMY ":nobody::"
604
605 /*
606 * Check to see if the from host has access to the line printer.
607 */
608 static void
609 chkhost(f)
610 struct sockaddr *f;
611 {
612 struct addrinfo hints, *res, *r;
613 FILE *hostf;
614 int first = 1, good = 0;
615 char host[NI_MAXHOST], ip[NI_MAXHOST];
616 char serv[NI_MAXSERV];
617 int error;
618
619 error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv),
620 NI_NUMERICSERV);
621 if (error || atoi(serv) >= IPPORT_RESERVED)
622 fatal("Malformed from address");
623
624 /* Need real hostname for temporary filenames */
625 error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
626 NI_NAMEREQD);
627 if (error) {
628 error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
629 NI_NUMERICHOST);
630 if (error)
631 fatal("Host name for your address unknown");
632 else
633 fatal("Host name for your address (%s) unknown", host);
634 }
635
636 (void)strncpy(fromb, host, sizeof(fromb) - 1);
637 fromb[sizeof(fromb) - 1] = '\0';
638 from = fromb;
639
640 /* need address in stringform for comparison (no DNS lookup here) */
641 error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
642 NI_NUMERICHOST);
643 if (error)
644 fatal("Cannot print address");
645
646 /* Check for spoof, ala rlogind */
647 memset(&hints, 0, sizeof(hints));
648 hints.ai_family = PF_UNSPEC;
649 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
650 error = getaddrinfo(fromb, NULL, &hints, &res);
651 if (error) {
652 fatal("hostname for your address (%s) unknown: %s", host,
653 gai_strerror(error));
654 }
655 good = 0;
656 for (r = res; good == 0 && r; r = r->ai_next) {
657 error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip),
658 NULL, 0, NI_NUMERICHOST);
659 if (!error && !strcmp(host, ip))
660 good = 1;
661 }
662 if (res)
663 freeaddrinfo(res);
664 if (good == 0)
665 fatal("address for your hostname (%s) not matched", host);
666 setproctitle("serving %s", from);
667 hostf = fopen(_PATH_HOSTSEQUIV, "r");
668 again:
669 if (hostf) {
670 if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) {
671 (void)fclose(hostf);
672 return;
673 }
674 (void)fclose(hostf);
675 }
676 if (first == 1) {
677 first = 0;
678 hostf = fopen(_PATH_HOSTSLPD, "r");
679 goto again;
680 }
681 fatal("Your host does not have line printer access");
682 /*NOTREACHED*/
683 }
684
685 static void
686 usage()
687 {
688 extern char *__progname; /* XXX */
689
690 fprintf(stderr, "usage: %s [-dlrs] [-b bind-address] [-n maxchild] [-w maxwait] [port]\n",
691 __progname);
692 exit(1);
693 }
694
695 /* setup server socket for specified address family */
696 /* if af is PF_UNSPEC more than one socket may be returned */
697 /* the returned list is dynamically allocated, so caller needs to free it */
698 int *
699 socksetup(af, options)
700 int af, options;
701 {
702 struct addrinfo hints, *res, *r;
703 int error, maxs = 0, *s, *socks = NULL, blidx = 0;
704 const int on = 1;
705
706 do {
707 memset(&hints, 0, sizeof(hints));
708 hints.ai_flags = AI_PASSIVE;
709 hints.ai_family = af;
710 hints.ai_socktype = SOCK_STREAM;
711 error = getaddrinfo((blist_addrs == 0) ? NULL : blist[blidx],
712 "printer", &hints, &res);
713 if (error) {
714 if (blist_addrs)
715 syslog(LOG_ERR, "%s: %s", blist[blidx],
716 (gai_strerror(error)));
717 else
718 syslog(LOG_ERR, (gai_strerror(error)));
719 mcleanup(0);
720 }
721
722 /* Count max number of sockets we may open */
723 for (r = res; r; r = r->ai_next, maxs++)
724 ;
725 if (socks == NULL) {
726 socks = malloc((maxs + 1) * sizeof(int));
727 if (socks)
728 *socks = 0; /* num of sockets ctr at start */
729 } else
730 socks = realloc(socks, (maxs + 1) * sizeof(int));
731 if (!socks) {
732 syslog(LOG_ERR, "couldn't allocate memory for sockets");
733 mcleanup(0);
734 }
735
736 s = socks + *socks + 1;
737 for (r = res; r; r = r->ai_next) {
738 *s = socket(r->ai_family, r->ai_socktype,
739 r->ai_protocol);
740 if (*s < 0) {
741 syslog(LOG_DEBUG, "socket(): %m");
742 continue;
743 }
744 if (options & SO_DEBUG)
745 if (setsockopt(*s, SOL_SOCKET, SO_DEBUG,
746 &on, sizeof(on)) < 0) {
747 syslog(LOG_ERR,
748 "setsockopt (SO_DEBUG): %m");
749 close (*s);
750 continue;
751 }
752 if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
753 syslog(LOG_DEBUG, "bind(): %m");
754 close (*s);
755 continue;
756 }
757 *socks = *socks + 1;
758 s++;
759 }
760
761 if (res)
762 freeaddrinfo(res);
763 } while (++blidx < blist_addrs);
764
765 if (socks == NULL || *socks == 0) {
766 syslog(LOG_ERR, "Couldn't bind to any socket");
767 if (socks != NULL)
768 free(socks);
769 mcleanup(0);
770 }
771 return(socks);
772 }
773