View | Details | Raw Unified | Return to bug 102162
Collapse All | Expand All

(-)/usr/src/libexec/tftpd/tftpd.c (-17 / +36 lines)
Lines 130-140 Link Here
130
	char *chroot_dir = NULL;
130
	char *chroot_dir = NULL;
131
	struct passwd *nobody;
131
	struct passwd *nobody;
132
	const char *chuser = "nobody";
132
	const char *chuser = "nobody";
133
	char *pp;
134
	u_short startport = 0, endport = 0;
133
135
134
	tzset();			/* syslog in localtime */
136
	tzset();			/* syslog in localtime */
135
137
136
	openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
138
	openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
137
	while ((ch = getopt(argc, argv, "cClns:u:U:w")) != -1) {
139
	while ((ch = getopt(argc, argv, "cClnp:s:u:U:w")) != -1) {
138
		switch (ch) {
140
		switch (ch) {
139
		case 'c':
141
		case 'c':
140
			ipchroot = 1;
142
			ipchroot = 1;
Lines 148-153 Link Here
148
		case 'n':
150
		case 'n':
149
			suppress_naks = 1;
151
			suppress_naks = 1;
150
			break;
152
			break;
153
		case 'p':
154
			pp = strchr(optarg, (int)'-');
155
			if (pp == NULL) {
156
				startport = atoi(optarg);
157
				endport = startport;
158
			} else {
159
				*pp++ = (char)0;
160
				startport = atoi(optarg);
161
				endport = atoi(pp);
162
			}
163
			break;
151
		case 's':
164
		case 's':
152
			chroot_dir = optarg;
165
			chroot_dir = optarg;
153
			break;
166
			break;
Lines 289-307 Link Here
289
	}
302
	}
290
303
291
	len = sizeof(me);
304
	len = sizeof(me);
292
	if (getsockname(0, (struct sockaddr *)&me, &len) == 0) {
305
	if (getsockname(0, (struct sockaddr *)&me, &len) != 0) {
293
		switch (me.ss_family) {
294
		case AF_INET:
295
			((struct sockaddr_in *)&me)->sin_port = 0;
296
			break;
297
		case AF_INET6:
298
			((struct sockaddr_in6 *)&me)->sin6_port = 0;
299
			break;
300
		default:
301
			/* unsupported */
302
			break;
303
		}
304
	} else {
305
		memset(&me, 0, sizeof(me));
306
		memset(&me, 0, sizeof(me));
306
		me.ss_family = from.ss_family;
307
		me.ss_family = from.ss_family;
307
		me.ss_len = from.ss_len;
308
		me.ss_len = from.ss_len;
Lines 314-322 Link Here
314
		syslog(LOG_ERR, "socket: %m");
315
		syslog(LOG_ERR, "socket: %m");
315
		exit(1);
316
		exit(1);
316
	}
317
	}
317
	if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) {
318
	while (1) {
318
		syslog(LOG_ERR, "bind: %m");
319
		switch (me.ss_family) {
319
		exit(1);
320
		case AF_INET:
321
			((struct sockaddr_in *)&me)->sin_port = htons(startport);
322
			break;
323
		case AF_INET6:
324
			((struct sockaddr_in6 *)&me)->sin6_port = htons(startport);
325
			break;
326
		default:
327
			/* unsupported */
328
			break;
329
		}
330
		if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) {
331
			if (errno == EADDRINUSE && startport < endport) {
332
				startport++;
333
				continue;
334
			}
335
			syslog(LOG_ERR, "bind: %m");
336
			exit(1);
337
		}
338
		break;
320
	}
339
	}
321
	if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) {
340
	if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) {
322
		syslog(LOG_ERR, "connect: %m");
341
		syslog(LOG_ERR, "connect: %m");
(-)/usr/src/libexec/tftpd/tftpd.8 (+15 lines)
Lines 147-152 Link Here
147
.It Fl n
147
.It Fl n
148
Suppress negative acknowledgement of requests for nonexistent
148
Suppress negative acknowledgement of requests for nonexistent
149
relative filenames.
149
relative filenames.
150
.It Fl p Ar port-range
151
Force
152
.Nm
153
to bind to a local udp port within the specified
154
.Pa port-range .
155
.Nm
156
This is useful when the server is behind a firewall because it
157
limits the size of hole that must be opened in the firewall for
158
to function.  You can specify either a single port number, or
159
a port range (two numbers separated by the '-' dash character).
160
If you only use a single port, your tftp server will only be
161
able to handle a single tftp session at a time.  If you use
162
this option, it is recommended that you choose a large enough
163
range to support as many concurrent tftp sessions as you
164
ever expect to encounter.
150
.It Fl s Ar directory
165
.It Fl s Ar directory
151
Cause
166
Cause
152
.Nm
167
.Nm

Return to bug 102162