lpd.c revision 1.29 1 /* $NetBSD: lpd.c,v 1.29 2001/06/25 15:29:12 mrg 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.29 2001/06/25 15:29:12 mrg 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, const char *));
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;
147 const char *port = "printer";
148
149 euid = geteuid(); /* these shouldn't be different */
150 uid = getuid();
151 gethostname(host, sizeof(host));
152 host[sizeof(host) - 1] = '\0';
153 name = argv[0];
154
155 errs = 0;
156 while ((i = getopt(argc, argv, "b:dln:srw:")) != -1)
157 switch (i) {
158 case 'b':
159 if (blist_addrs >= blist_size) {
160 blist_size += sizeof(char *) * 4;
161 if (blist == NULL)
162 blist = malloc(blist_size);
163 else
164 blist = realloc(blist, blist_size);
165 if (blist == NULL)
166 err(1, "cant allocate bind addr list");
167 }
168 blist[blist_addrs++] = strdup(optarg);
169 break;
170 case 'd':
171 options |= SO_DEBUG;
172 break;
173 case 'l':
174 lflag++;
175 break;
176 case 'n':
177 child_max = atoi(optarg);
178 if (child_max < 0 || child_max > 1024)
179 errx(1, "invalid number of children: %s",
180 optarg);
181 break;
182 case 'r':
183 rflag++;
184 break;
185 case 's':
186 sflag++;
187 break;
188 case 'w':
189 wait_time = atoi(optarg);
190 if (wait_time < 0)
191 errx(1, "wait time must be postive: %s",
192 optarg);
193 if (wait_time < 30)
194 warnx("warning: wait time less than 30 seconds");
195 break;
196 default:
197 errs++;
198 }
199 argc -= optind;
200 argv += optind;
201 if (errs)
202 usage();
203
204 switch (argc) {
205 case 1:
206 if ((i = atoi(argv[0])) == 0)
207 usage();
208 if (i < 0 || i > USHRT_MAX)
209 errx(1, "port # %d is invalid", i);
210
211 port = argv[0];
212 break;
213 case 0:
214 sp = getservbyname(port, "tcp");
215 if (sp == NULL)
216 errx(1, "%s/tcp: unknown service", port);
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, port);
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 if (*printer == '\0')
463 printer = DEFLP;
464 printjob();
465 break;
466 case '\2': /* receive files to be queued */
467 if (!from_remote) {
468 syslog(LOG_INFO, "illegal request (%d)", *cp);
469 exit(1);
470 }
471 printer = cp;
472 if (*printer == '\0')
473 printer = DEFLP;
474 recvjob();
475 break;
476 case '\3': /* display the queue (short form) */
477 case '\4': /* display the queue (long form) */
478 printer = cp;
479 if (*printer == '\0')
480 printer = DEFLP;
481 while (*cp) {
482 if (*cp != ' ') {
483 cp++;
484 continue;
485 }
486 *cp++ = '\0';
487 while (isspace(*cp))
488 cp++;
489 if (*cp == '\0')
490 break;
491 if (isdigit(*cp)) {
492 if (requests >= MAXREQUESTS)
493 fatal("Too many requests");
494 requ[requests++] = atoi(cp);
495 } else {
496 if (users >= MAXUSERS)
497 fatal("Too many users");
498 user[users++] = cp;
499 }
500 }
501 displayq(cbuf[0] - '\3');
502 exit(0);
503 case '\5': /* remove a job from the queue */
504 if (!from_remote) {
505 syslog(LOG_INFO, "illegal request (%d)", *cp);
506 exit(1);
507 }
508 printer = cp;
509 if (*printer == '\0')
510 printer = DEFLP;
511 while (*cp && *cp != ' ')
512 cp++;
513 if (!*cp)
514 break;
515 *cp++ = '\0';
516 person = cp;
517 while (*cp) {
518 if (*cp != ' ') {
519 cp++;
520 continue;
521 }
522 *cp++ = '\0';
523 while (isspace(*cp))
524 cp++;
525 if (*cp == '\0')
526 break;
527 if (isdigit(*cp)) {
528 if (requests >= MAXREQUESTS)
529 fatal("Too many requests");
530 requ[requests++] = atoi(cp);
531 } else {
532 if (users >= MAXUSERS)
533 fatal("Too many users");
534 user[users++] = cp;
535 }
536 }
537 rmjob();
538 break;
539 }
540 fatal("Illegal service request");
541 }
542 }
543
544 /*
545 * Make a pass through the printcap database and start printing any
546 * files left from the last time the machine went down.
547 */
548 static void
549 startup()
550 {
551 char *buf;
552 char *cp;
553
554 /*
555 * Restart the daemons.
556 */
557 while (cgetnext(&buf, printcapdb) > 0) {
558 if (ckqueue(buf) <= 0) {
559 free(buf);
560 continue; /* no work to do for this printer */
561 }
562 for (cp = buf; *cp; cp++)
563 if (*cp == '|' || *cp == ':') {
564 *cp = '\0';
565 break;
566 }
567 if (lflag)
568 syslog(LOG_INFO, "work for %s", buf);
569 switch (fork()) {
570 case -1:
571 syslog(LOG_WARNING, "startup: cannot fork");
572 mcleanup(0);
573 case 0:
574 printer = buf;
575 setproctitle("working on printer %s", printer);
576 cgetclose();
577 printjob();
578 /* NOTREACHED */
579 default:
580 child_count++;
581 free(buf);
582 }
583 }
584 }
585
586 /*
587 * Make sure there's some work to do before forking off a child
588 */
589 static int
590 ckqueue(cap)
591 char *cap;
592 {
593 struct dirent *d;
594 DIR *dirp;
595 char *spooldir;
596
597 if (cgetstr(cap, "sd", &spooldir) == -1)
598 spooldir = _PATH_DEFSPOOL;
599 if ((dirp = opendir(spooldir)) == NULL)
600 return (-1);
601 while ((d = readdir(dirp)) != NULL) {
602 if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
603 continue; /* daemon control files only */
604 closedir(dirp);
605 return (1); /* found something */
606 }
607 closedir(dirp);
608 return (0);
609 }
610
611 #define DUMMY ":nobody::"
612
613 /*
614 * Check to see if the from host has access to the line printer.
615 */
616 static void
617 chkhost(f)
618 struct sockaddr *f;
619 {
620 struct addrinfo hints, *res, *r;
621 FILE *hostf;
622 int first = 1, good = 0;
623 char host[NI_MAXHOST], ip[NI_MAXHOST];
624 char serv[NI_MAXSERV];
625 int error;
626
627 error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv),
628 NI_NUMERICSERV);
629 if (error || atoi(serv) >= IPPORT_RESERVED)
630 fatal("Malformed from address");
631
632 /* Need real hostname for temporary filenames */
633 error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
634 NI_NAMEREQD);
635 if (error) {
636 error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
637 NI_NUMERICHOST);
638 if (error)
639 fatal("Host name for your address unknown");
640 else
641 fatal("Host name for your address (%s) unknown", host);
642 }
643
644 (void)strncpy(fromb, host, sizeof(fromb) - 1);
645 fromb[sizeof(fromb) - 1] = '\0';
646 from = fromb;
647
648 /* need address in stringform for comparison (no DNS lookup here) */
649 error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
650 NI_NUMERICHOST);
651 if (error)
652 fatal("Cannot print address");
653
654 /* Check for spoof, ala rlogind */
655 memset(&hints, 0, sizeof(hints));
656 hints.ai_family = PF_UNSPEC;
657 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
658 error = getaddrinfo(fromb, NULL, &hints, &res);
659 if (error) {
660 fatal("hostname for your address (%s) unknown: %s", host,
661 gai_strerror(error));
662 }
663 good = 0;
664 for (r = res; good == 0 && r; r = r->ai_next) {
665 error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip),
666 NULL, 0, NI_NUMERICHOST);
667 if (!error && !strcmp(host, ip))
668 good = 1;
669 }
670 if (res)
671 freeaddrinfo(res);
672 if (good == 0)
673 fatal("address for your hostname (%s) not matched", host);
674 setproctitle("serving %s", from);
675 hostf = fopen(_PATH_HOSTSEQUIV, "r");
676 again:
677 if (hostf) {
678 if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) {
679 (void)fclose(hostf);
680 return;
681 }
682 (void)fclose(hostf);
683 }
684 if (first == 1) {
685 first = 0;
686 hostf = fopen(_PATH_HOSTSLPD, "r");
687 goto again;
688 }
689 fatal("Your host does not have line printer access");
690 /*NOTREACHED*/
691 }
692
693 static void
694 usage()
695 {
696
697 fprintf(stderr, "usage: %s [-dlrs] [-b bind-address] [-n maxchild] "
698 "[-w maxwait] [port]\n", getprogname());
699 exit(1);
700 }
701
702 /* setup server socket for specified address family */
703 /* if af is PF_UNSPEC more than one socket may be returned */
704 /* the returned list is dynamically allocated, so caller needs to free it */
705 int *
706 socksetup(af, options, port)
707 int af, options;
708 const char *port;
709 {
710 struct addrinfo hints, *res, *r;
711 int error, maxs = 0, *s, *socks = NULL, blidx = 0;
712 const int on = 1;
713
714 do {
715 memset(&hints, 0, sizeof(hints));
716 hints.ai_flags = AI_PASSIVE;
717 hints.ai_family = af;
718 hints.ai_socktype = SOCK_STREAM;
719 error = getaddrinfo((blist_addrs == 0) ? NULL : blist[blidx],
720 port ? port : "printer", &hints, &res);
721 if (error) {
722 if (blist_addrs)
723 syslog(LOG_ERR, "%s: %s", blist[blidx],
724 gai_strerror(error));
725 else
726 syslog(LOG_ERR, "%s", gai_strerror(error));
727 mcleanup(0);
728 }
729
730 /* Count max number of sockets we may open */
731 for (r = res; r; r = r->ai_next, maxs++)
732 ;
733 if (socks == NULL) {
734 socks = malloc((maxs + 1) * sizeof(int));
735 if (socks)
736 *socks = 0; /* num of sockets ctr at start */
737 } else
738 socks = realloc(socks, (maxs + 1) * sizeof(int));
739 if (!socks) {
740 syslog(LOG_ERR, "couldn't allocate memory for sockets");
741 mcleanup(0);
742 }
743
744 s = socks + *socks + 1;
745 for (r = res; r; r = r->ai_next) {
746 *s = socket(r->ai_family, r->ai_socktype,
747 r->ai_protocol);
748 if (*s < 0) {
749 syslog(LOG_DEBUG, "socket(): %m");
750 continue;
751 }
752 if (options & SO_DEBUG)
753 if (setsockopt(*s, SOL_SOCKET, SO_DEBUG,
754 &on, sizeof(on)) < 0) {
755 syslog(LOG_ERR,
756 "setsockopt (SO_DEBUG): %m");
757 close (*s);
758 continue;
759 }
760 if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
761 syslog(LOG_DEBUG, "bind(): %m");
762 close (*s);
763 continue;
764 }
765 *socks = *socks + 1;
766 s++;
767 }
768
769 if (res)
770 freeaddrinfo(res);
771 } while (++blidx < blist_addrs);
772
773 if (socks == NULL || *socks == 0) {
774 syslog(LOG_ERR, "Couldn't bind to any socket");
775 if (socks != NULL)
776 free(socks);
777 mcleanup(0);
778 }
779 return(socks);
780 }
781