View | Details | Raw Unified | Return to bug 189053 | Differences between
and this patch

Collapse All | Expand All

(-)b/usr.sbin/lpr/lpd/lpd.c (-49 / +76 lines)
Lines 112-118 static void startup(void); Link Here
112
static void	 chkhost(struct sockaddr *_f, int _ch_opts);
112
static void	 chkhost(struct sockaddr *_f, int _ch_opts);
113
static int	 ckqueue(struct printer *_pp);
113
static int	 ckqueue(struct printer *_pp);
114
static void	 fhosterr(int _ch_opts, char *_sysmsg, char *_usermsg);
114
static void	 fhosterr(int _ch_opts, char *_sysmsg, char *_usermsg);
115
static int	*socksetup(int _af, int _debuglvl);
115
static int *socksetup(int af, size_t blist_size, char *blist[blist_size], int debuglvl);
116
static void	 usage(void);
116
static void	 usage(void);
117
117
118
/* XXX from libc/net/rcmd.c */
118
/* XXX from libc/net/rcmd.c */
Lines 144-157 main(int argc, char **argv) Link Here
144
	socket_debug = 0;
144
	socket_debug = 0;
145
	gethostname(local_host, sizeof(local_host));
145
	gethostname(local_host, sizeof(local_host));
146
146
147
	char **blist = NULL;
148
	size_t blist_size = 0;
149
147
	progname = "lpd";
150
	progname = "lpd";
148
151
149
	if (euid != 0)
152
	if (euid != 0)
150
		errx(EX_NOPERM,"must run as root");
153
		errx(EX_NOPERM,"must run as root");
151
154
152
	errs = 0;
155
	errs = 0;
153
	while ((i = getopt(argc, argv, "cdlpswW46")) != -1)
156
	while ((i = getopt(argc, argv, "b:cdlpswW46")) != -1)
154
		switch (i) {
157
		switch (i) {
158
		case 'b':
159
			/* add bind address to list. */
160
			blist_size++;
161
			blist = realloc(blist, sizeof (char *) * blist_size);
162
163
			if (blist == NULL)
164
				errx(EX_SOFTWARE, "Could not allocate memory for bind list.");		
165
			blist[blist_size-1] = optarg;
166
			fprintf(stderr,"made it.\n");
167
			break;
168
155
		case 'c':
169
		case 'c':
156
			/* log all kinds of connection-errors to syslog */
170
			/* log all kinds of connection-errors to syslog */
157
			ch_options |= LPD_LOGCONNERR;
171
			ch_options |= LPD_LOGCONNERR;
Lines 204-210 main(int argc, char **argv) Link Here
204
		 * listed here to "reserve" them, because the option-letters
218
		 * listed here to "reserve" them, because the option-letters
205
		 * are used by either NetBSD or OpenBSD (as of July 2001).
219
		 * are used by either NetBSD or OpenBSD (as of July 2001).
206
		 */ 
220
		 */ 
207
		case 'b':		/* set bind-addr */
208
		case 'n':		/* set max num of children */
221
		case 'n':		/* set max num of children */
209
		case 'r':		/* allow 'of' for remote ptrs */
222
		case 'r':		/* allow 'of' for remote ptrs */
210
					/* ...[not needed in freebsd] */
223
					/* ...[not needed in freebsd] */
Lines 220-231 main(int argc, char **argv) Link Here
220
		usage();
233
		usage();
221
234
222
	if (argc == 1) {
235
	if (argc == 1) {
223
		if ((i = atoi(argv[0])) == 0)
236
		int requested_port = atoi(argv[0]);
237
		if (requested_port == 0)
224
			usage();
238
			usage();
225
		if (i < 0 || i > USHRT_MAX)
239
		if (requested_port < 0 || requested_port > USHRT_MAX)
226
			errx(EX_USAGE, "port # %d is invalid", i);
240
			errx(EX_USAGE, "port # %d is invalid", requested_port);
227
241
228
		serv.s_port = htons(i);
242
		serv.s_port = htons(requested_port);
229
		sp = &serv;
243
		sp = &serv;
230
		argc--;
244
		argc--;
231
	} else {
245
	} else {
Lines 339-345 main(int argc, char **argv) Link Here
339
	FD_SET(funix, &defreadfds);
353
	FD_SET(funix, &defreadfds);
340
	listen(funix, 5);
354
	listen(funix, 5);
341
	if (sflag == 0) {
355
	if (sflag == 0) {
342
		finet = socksetup(family, socket_debug);
356
		finet = socksetup(family, blist_size, blist, socket_debug);
357
358
		/* Now we're done with blist. */
359
		if (blist)
360
			free(blist);
343
	} else
361
	} else
344
		finet = NULL;	/* pretend we couldn't open TCP socket. */
362
		finet = NULL;	/* pretend we couldn't open TCP socket. */
345
	if (finet) {
363
	if (finet) {
Lines 855-861 fhosterr(int ch_opts, char *sysmsg, char *usermsg) Link Here
855
/* if af is PF_UNSPEC more than one socket may be returned */
873
/* if af is PF_UNSPEC more than one socket may be returned */
856
/* the returned list is dynamically allocated, so caller needs to free it */
874
/* the returned list is dynamically allocated, so caller needs to free it */
857
static int *
875
static int *
858
socksetup(int af, int debuglvl)
876
socksetup(int af, size_t blist_size, char *blist[blist_size], int debuglvl)
859
{
877
{
860
	struct addrinfo hints, *res, *r;
878
	struct addrinfo hints, *res, *r;
861
	int error, maxs, *s, *socks;
879
	int error, maxs, *s, *socks;
Lines 865-922 socksetup(int af, int debuglvl) Link Here
865
	hints.ai_flags = AI_PASSIVE;
883
	hints.ai_flags = AI_PASSIVE;
866
	hints.ai_family = af;
884
	hints.ai_family = af;
867
	hints.ai_socktype = SOCK_STREAM;
885
	hints.ai_socktype = SOCK_STREAM;
868
	error = getaddrinfo(NULL, "printer", &hints, &res);
869
	if (error) {
870
		syslog(LOG_ERR, "%s", gai_strerror(error));
871
		mcleanup(0);
872
	}
873
886
874
	/* Count max number of sockets we may open */
887
	if (blist_size == 0 || blist == NULL) {
875
	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
888
		blist_size = 1;
876
		;
889
		blist = (char *[]){ "0.0.0.0" };
877
	socks = malloc((maxs + 1) * sizeof(int));
878
	if (!socks) {
879
		syslog(LOG_ERR, "couldn't allocate memory for sockets");
880
		mcleanup(0);
881
	}
890
	}
882
891
883
	*socks = 0;   /* num of sockets counter at start of array */
892
	for (size_t baddr = 0; baddr < blist_size; baddr++ ) {
884
	s = socks + 1;
893
		error = getaddrinfo(blist[baddr], "printer", &hints, &res);
885
	for (r = res; r; r = r->ai_next) {
894
		if (error) {
886
		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
895
			syslog(LOG_ERR, "%s", gai_strerror(error));
887
		if (*s < 0) {
896
			mcleanup(0);
888
			syslog(LOG_DEBUG, "socket(): %m");
889
			continue;
890
		}
897
		}
891
		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
898
892
		    < 0) {
899
		/* Count max number of sockets we may open */
893
			syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
900
		for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
894
			close(*s);
901
			;
895
			continue;
902
		socks = malloc((maxs + 1) * sizeof(int));
903
		if (!socks) {
904
			syslog(LOG_ERR, "couldn't allocate memory for sockets");
905
			mcleanup(0);
896
		}
906
		}
897
		if (debuglvl)
907
898
			if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl,
908
		*socks = 0;   /* num of sockets counter at start of array */
899
			    sizeof(debuglvl)) < 0) {
909
		s = socks + 1;
900
				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
910
911
		for (r = res; r; r = r->ai_next) {
912
			*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
913
			if (*s < 0) {
914
				syslog(LOG_DEBUG, "socket(): %m");
915
				continue;
916
			}
917
			if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
918
					< 0) {
919
				syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
901
				close(*s);
920
				close(*s);
902
				continue;
921
				continue;
903
			}
922
			}
904
		if (r->ai_family == AF_INET6) {
923
			if (debuglvl)
905
			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
924
				if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl,
906
				       &on, sizeof(on)) < 0) {
925
						sizeof(debuglvl)) < 0) {
907
				syslog(LOG_ERR,
926
					syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
908
				       "setsockopt (IPV6_V6ONLY): %m");
927
					close(*s);
928
					continue;
929
				}
930
			if (r->ai_family == AF_INET6) {
931
				if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
932
								 &on, sizeof(on)) < 0) {
933
					syslog(LOG_ERR,
934
								 "setsockopt (IPV6_V6ONLY): %m");
935
					close(*s);
936
					continue;
937
				}
938
			}
939
			if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
940
				syslog(LOG_DEBUG, "bind(): %m");
909
				close(*s);
941
				close(*s);
910
				continue;
942
				continue;
911
			}
943
			}
944
			(*socks)++;
945
			s++;
912
		}
946
		}
913
		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
914
			syslog(LOG_DEBUG, "bind(): %m");
915
			close(*s);
916
			continue;
917
		}
918
		(*socks)++;
919
		s++;
920
	}
947
	}
921
948
922
	if (res)
949
	if (res)

Return to bug 189053