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 / +77 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
166
			blist[blist_size-1] = optarg;
167
			fprintf(stderr,"made it.\n");
168
			break;
169
155
		case 'c':
170
		case 'c':
156
			/* log all kinds of connection-errors to syslog */
171
			/* log all kinds of connection-errors to syslog */
157
			ch_options |= LPD_LOGCONNERR;
172
			ch_options |= LPD_LOGCONNERR;
Lines 204-210 main(int argc, char **argv) Link Here
204
		 * listed here to "reserve" them, because the option-letters
219
		 * listed here to "reserve" them, because the option-letters
205
		 * are used by either NetBSD or OpenBSD (as of July 2001).
220
		 * are used by either NetBSD or OpenBSD (as of July 2001).
206
		 */ 
221
		 */ 
207
		case 'b':		/* set bind-addr */
208
		case 'n':		/* set max num of children */
222
		case 'n':		/* set max num of children */
209
		case 'r':		/* allow 'of' for remote ptrs */
223
		case 'r':		/* allow 'of' for remote ptrs */
210
					/* ...[not needed in freebsd] */
224
					/* ...[not needed in freebsd] */
Lines 220-231 main(int argc, char **argv) Link Here
220
		usage();
234
		usage();
221
235
222
	if (argc == 1) {
236
	if (argc == 1) {
223
		if ((i = atoi(argv[0])) == 0)
237
		int requested_port = atoi(argv[0]);
238
		if (requested_port == 0)
224
			usage();
239
			usage();
225
		if (i < 0 || i > USHRT_MAX)
240
		if (requested_port < 0 || i > USHRT_MAX)
226
			errx(EX_USAGE, "port # %d is invalid", i);
241
			errx(EX_USAGE, "port # %d is invalid", requested_port);
227
242
228
		serv.s_port = htons(i);
243
		serv.s_port = htons(requested_port);
229
		sp = &serv;
244
		sp = &serv;
230
		argc--;
245
		argc--;
231
	} else {
246
	} else {
Lines 339-345 main(int argc, char **argv) Link Here
339
	FD_SET(funix, &defreadfds);
354
	FD_SET(funix, &defreadfds);
340
	listen(funix, 5);
355
	listen(funix, 5);
341
	if (sflag == 0) {
356
	if (sflag == 0) {
342
		finet = socksetup(family, socket_debug);
357
		finet = socksetup(family, blist_size, blist, socket_debug);
358
359
		/* Now we're done with blist. */
360
		if (blist)
361
			free(blist);
343
	} else
362
	} else
344
		finet = NULL;	/* pretend we couldn't open TCP socket. */
363
		finet = NULL;	/* pretend we couldn't open TCP socket. */
345
	if (finet) {
364
	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 */
874
/* 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 */
875
/* the returned list is dynamically allocated, so caller needs to free it */
857
static int *
876
static int *
858
socksetup(int af, int debuglvl)
877
socksetup(int af, size_t blist_size, char *blist[blist_size], int debuglvl)
859
{
878
{
860
	struct addrinfo hints, *res, *r;
879
	struct addrinfo hints, *res, *r;
861
	int error, maxs, *s, *socks;
880
	int error, maxs, *s, *socks;
Lines 865-922 socksetup(int af, int debuglvl) Link Here
865
	hints.ai_flags = AI_PASSIVE;
884
	hints.ai_flags = AI_PASSIVE;
866
	hints.ai_family = af;
885
	hints.ai_family = af;
867
	hints.ai_socktype = SOCK_STREAM;
886
	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
887
874
	/* Count max number of sockets we may open */
888
	if (blist_size == 0 || blist == NULL) {
875
	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
889
		blist_size = 1;
876
		;
890
		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
	}
891
	}
882
892
883
	*socks = 0;   /* num of sockets counter at start of array */
893
	for (size_t baddr = 0; baddr < blist_size; baddr++ ) {
884
	s = socks + 1;
894
		error = getaddrinfo(blist[baddr], "printer", &hints, &res);
885
	for (r = res; r; r = r->ai_next) {
895
		if (error) {
886
		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
896
			syslog(LOG_ERR, "%s", gai_strerror(error));
887
		if (*s < 0) {
897
			mcleanup(0);
888
			syslog(LOG_DEBUG, "socket(): %m");
889
			continue;
890
		}
898
		}
891
		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
899
892
		    < 0) {
900
		/* Count max number of sockets we may open */
893
			syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
901
		for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
894
			close(*s);
902
			;
895
			continue;
903
		socks = malloc((maxs + 1) * sizeof(int));
904
		if (!socks) {
905
			syslog(LOG_ERR, "couldn't allocate memory for sockets");
906
			mcleanup(0);
896
		}
907
		}
897
		if (debuglvl)
908
898
			if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl,
909
		*socks = 0;   /* num of sockets counter at start of array */
899
			    sizeof(debuglvl)) < 0) {
910
		s = socks + 1;
900
				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
911
912
		for (r = res; r; r = r->ai_next) {
913
			*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
914
			if (*s < 0) {
915
				syslog(LOG_DEBUG, "socket(): %m");
916
				continue;
917
			}
918
			if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
919
					< 0) {
920
				syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
901
				close(*s);
921
				close(*s);
902
				continue;
922
				continue;
903
			}
923
			}
904
		if (r->ai_family == AF_INET6) {
924
			if (debuglvl)
905
			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
925
				if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl,
906
				       &on, sizeof(on)) < 0) {
926
						sizeof(debuglvl)) < 0) {
907
				syslog(LOG_ERR,
927
					syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
908
				       "setsockopt (IPV6_V6ONLY): %m");
928
					close(*s);
929
					continue;
930
				}
931
			if (r->ai_family == AF_INET6) {
932
				if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
933
								 &on, sizeof(on)) < 0) {
934
					syslog(LOG_ERR,
935
								 "setsockopt (IPV6_V6ONLY): %m");
936
					close(*s);
937
					continue;
938
				}
939
			}
940
			if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
941
				syslog(LOG_DEBUG, "bind(): %m");
909
				close(*s);
942
				close(*s);
910
				continue;
943
				continue;
911
			}
944
			}
945
			(*socks)++;
946
			s++;
912
		}
947
		}
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
	}
948
	}
921
949
922
	if (res)
950
	if (res)

Return to bug 189053