xref: /freebsd/libexec/bootpd/dovend.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*
2  * dovend.c : Inserts all but the first few vendor options.
3  */
4 
5 #include <sys/types.h>
6 
7 #include <netinet/in.h>
8 #include <arpa/inet.h>			/* inet_ntoa */
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <syslog.h>
15 
16 #ifndef USE_BFUNCS
17 # include <memory.h>
18 /* Yes, memcpy is OK here (no overlapped copies). */
19 # define bcopy(a,b,c)    memcpy(b,a,c)
20 # define bzero(p,l)      memset(p,0,l)
21 # define bcmp(a,b,c)     memcmp(a,b,c)
22 # define index           strchr
23 #endif
24 
25 #include "bootp.h"
26 #include "bootpd.h"
27 #include "report.h"
28 #include "dovend.h"
29 
30 #ifdef	__STDC__
31 #define P(args) args
32 #else
33 #define P(args) ()
34 #endif
35 
36 PRIVATE int insert_generic P((struct shared_bindata *, byte **, int *));
37 
38 /*
39  * Insert the 2nd part of the options into an option buffer.
40  * Return amount of space used.
41  *
42  * This inserts everything EXCEPT:
43  *   magic cookie, subnet mask, gateway, bootsize, extension file
44  * Those are handled separately (in bootpd.c) to allow this function
45  * to be shared between bootpd and bootpef.
46  *
47  * When an "extension file" is in use, the options inserted by
48  * this function go into the exten_file, not the bootp response.
49  */
50 
51 int
52 dovend_rfc1497(hp, buf, len)
53 	struct host *hp;
54 	byte *buf;
55 	int len;
56 {
57 	int bytesleft = len;
58 	byte *vp = buf;
59 	char *tmpstr;
60 
61 	static char noroom[] = "%s: No room for \"%s\" option";
62 #define	NEED(LEN, MSG) do                       \
63 		if (bytesleft < (LEN)) {         	    \
64 			report(LOG_NOTICE, noroom,          \
65 				   hp->hostname->string, MSG);  \
66 			return (vp - buf);                  \
67 		} while (0)
68 
69 	/*
70 	 * Note that the following have already been inserted:
71 	 *   magic_cookie, subnet_mask, gateway, bootsize
72 	 *
73 	 * The remaining options are inserted in order of importance.
74 	 * (Of course the importance of each is a matter of opinion.)
75 	 * The option insertion order should probably be configurable.
76 	 *
77 	 * This is the order used in the NetBSD version.  Can anyone
78 	 * explain why the time_offset and swap_server are first?
79 	 * Also, why is the hostname so far down the list?  -gwr
80 	 */
81 
82 	if (hp->flags.time_offset) {
83 		NEED(6, "to");
84 		*vp++ = TAG_TIME_OFFSET;/* -1 byte  */
85 		*vp++ = 4;				/* -1 byte  */
86 		insert_u_long(htonl(hp->time_offset), &vp);	/* -4 bytes */
87 		bytesleft -= 6;
88 	}
89 	/*
90 	 * swap server, root path, dump path
91 	 */
92 	if (hp->flags.swap_server) {
93 		NEED(6, "sw");
94 		/* There is just one SWAP_SERVER, so it is not an iplist. */
95 		*vp++ = TAG_SWAP_SERVER;/* -1 byte  */
96 		*vp++ = 4;				/* -1 byte  */
97 		insert_u_long(hp->swap_server.s_addr, &vp);	/* -4 bytes */
98 		bytesleft -= 6;			/* Fix real count */
99 	}
100 	if (hp->flags.root_path) {
101 		/*
102 		 * Check for room for root_path.  Add 2 to account for
103 		 * TAG_ROOT_PATH and length.
104 		 */
105 		len = strlen(hp->root_path->string);
106 		NEED((len + 2), "rp");
107 		*vp++ = TAG_ROOT_PATH;
108 		*vp++ = (byte) (len & 0xFF);
109 		bcopy(hp->root_path->string, vp, len);
110 		vp += len;
111 		bytesleft -= len + 2;
112 	}
113 	if (hp->flags.dump_file) {
114 		/*
115 		 * Check for room for dump_file.  Add 2 to account for
116 		 * TAG_DUMP_FILE and length.
117 		 */
118 		len = strlen(hp->dump_file->string);
119 		NEED((len + 2), "df");
120 		*vp++ = TAG_DUMP_FILE;
121 		*vp++ = (byte) (len & 0xFF);
122 		bcopy(hp->dump_file->string, vp, len);
123 		vp += len;
124 		bytesleft -= len + 2;
125 	}
126 	/*
127 	 * DNS server and domain
128 	 */
129 	if (hp->flags.domain_server) {
130 		if (insert_ip(TAG_DOMAIN_SERVER,
131 					  hp->domain_server,
132 					  &vp, &bytesleft))
133 			NEED(8, "ds");
134 	}
135 	if (hp->flags.domain_name) {
136 		/*
137 		 * Check for room for domain_name.  Add 2 to account for
138 		 * TAG_DOMAIN_NAME and length.
139 		 */
140 		len = strlen(hp->domain_name->string);
141 		NEED((len + 2), "dn");
142 		*vp++ = TAG_DOMAIN_NAME;
143 		*vp++ = (byte) (len & 0xFF);
144 		bcopy(hp->domain_name->string, vp, len);
145 		vp += len;
146 		bytesleft -= len + 2;
147 	}
148 	/*
149 	 * NIS (YP) server and domain
150 	 */
151 	if (hp->flags.nis_server) {
152 		if (insert_ip(TAG_NIS_SERVER,
153 					  hp->nis_server,
154 					  &vp, &bytesleft))
155 			NEED(8, "ds");
156 	}
157 	if (hp->flags.nis_domain) {
158 		/*
159 		 * Check for room for nis_domain.  Add 2 to account for
160 		 * TAG_NIS_DOMAIN and length.
161 		 */
162 		len = strlen(hp->nis_domain->string);
163 		NEED((len + 2), "dn");
164 		*vp++ = TAG_NIS_DOMAIN;
165 		*vp++ = (byte) (len & 0xFF);
166 		bcopy(hp->nis_domain->string, vp, len);
167 		vp += len;
168 		bytesleft -= len + 2;
169 	}
170 	/* IEN 116 name server */
171 	if (hp->flags.name_server) {
172 		if (insert_ip(TAG_NAME_SERVER,
173 					  hp->name_server,
174 					  &vp, &bytesleft))
175 			NEED(8, "ns");
176 	}
177 	if (hp->flags.rlp_server) {
178 		if (insert_ip(TAG_RLP_SERVER,
179 					  hp->rlp_server,
180 					  &vp, &bytesleft))
181 			NEED(8, "rl");
182 	}
183 	/* Time server (RFC 868) */
184 	if (hp->flags.time_server) {
185 		if (insert_ip(TAG_TIME_SERVER,
186 					  hp->time_server,
187 					  &vp, &bytesleft))
188 			NEED(8, "ts");
189 	}
190 	/* NTP (time) Server (RFC 1129) */
191 	if (hp->flags.ntp_server) {
192 		if (insert_ip(TAG_NTP_SERVER,
193 					  hp->ntp_server,
194 					  &vp, &bytesleft))
195 			NEED(8, "ts");
196 	}
197 	/*
198 	 * I wonder:  If the hostname were "promoted" into the BOOTP
199 	 * response part, might these "extension" files possibly be
200 	 * shared between several clients?
201 	 *
202 	 * Also, why not just use longer BOOTP packets with all the
203 	 * additional length used as option data.  This bootpd version
204 	 * already supports that feature by replying with the same
205 	 * packet length as the client request packet. -gwr
206 	 */
207 	if (hp->flags.name_switch && hp->flags.send_name) {
208 		/*
209 		 * Check for room for hostname.  Add 2 to account for
210 		 * TAG_HOST_NAME and length.
211 		 */
212 		len = strlen(hp->hostname->string);
213 #if 0
214 		/*
215 		 * XXX - Too much magic.  The user can always set the hostname
216 		 * to the short version in the bootptab file. -gwr
217 		 */
218 		if ((len + 2) > bytesleft) {
219 			/*
220 			 * Not enough room for full (domain-qualified) hostname, try
221 			 * stripping it down to just the first field (host).
222 			 */
223 			tmpstr = hp->hostname->string;
224 			len = 0;
225 			while (*tmpstr && (*tmpstr != '.')) {
226 				tmpstr++;
227 				len++;
228 			}
229 		}
230 #endif
231 		NEED((len + 2), "hn");
232 		*vp++ = TAG_HOST_NAME;
233 		*vp++ = (byte) (len & 0xFF);
234 		bcopy(hp->hostname->string, vp, len);
235 		vp += len;
236 		bytesleft -= len + 2;
237 	}
238 	/*
239 	 * The rest of these are less important, so they go last.
240 	 */
241 	if (hp->flags.lpr_server) {
242 		if (insert_ip(TAG_LPR_SERVER,
243 					  hp->lpr_server,
244 					  &vp, &bytesleft))
245 			NEED(8, "lp");
246 	}
247 	if (hp->flags.cookie_server) {
248 		if (insert_ip(TAG_COOKIE_SERVER,
249 					  hp->cookie_server,
250 					  &vp, &bytesleft))
251 			NEED(8, "cs");
252 	}
253 	if (hp->flags.log_server) {
254 		if (insert_ip(TAG_LOG_SERVER,
255 					  hp->log_server,
256 					  &vp, &bytesleft))
257 			NEED(8, "lg");
258 	}
259 	/*
260 	 * XXX - Add new tags here (to insert options)
261 	 */
262 	if (hp->flags.generic) {
263 		if (insert_generic(hp->generic, &vp, &bytesleft))
264 			NEED(64, "(generic)");
265 	}
266 	/*
267 	 * The end marker is inserted by the caller.
268 	 */
269 	return (vp - buf);
270 #undef	NEED
271 }								/* dovend_rfc1497 */
272 
273 
274 
275 /*
276  * Insert a tag value, a length value, and a list of IP addresses into the
277  * memory buffer indirectly pointed to by "dest".  "tag" is the RFC1048 tag
278  * number to use, "iplist" is a pointer to a list of IP addresses
279  * (struct in_addr_list), and "bytesleft" points to an integer which
280  * indicates the size of the "dest" buffer.
281  *
282  * Return zero if everything fits.
283  *
284  * This is used to fill the vendor-specific area of a bootp packet in
285  * conformance to RFC1048.
286  */
287 
288 int
289 insert_ip(tag, iplist, dest, bytesleft)
290 	byte tag;
291 	struct in_addr_list *iplist;
292 	byte **dest;
293 	int *bytesleft;
294 {
295 	struct in_addr *addrptr;
296 	unsigned addrcount = 1;
297 	byte *d;
298 
299 	if (iplist == NULL)
300 		return (0);
301 
302 	if (*bytesleft >= 6) {
303 		d = *dest;				/* Save pointer for later */
304 		**dest = tag;
305 		(*dest) += 2;
306 		(*bytesleft) -= 2;		/* Account for tag and length */
307 		addrptr = iplist->addr;
308 		addrcount = iplist->addrcount;
309 		while ((*bytesleft >= 4) && (addrcount > 0)) {
310 			insert_u_long(addrptr->s_addr, dest);
311 			addrptr++;
312 			addrcount--;
313 			(*bytesleft) -= 4;	/* Four bytes per address */
314 		}
315 		d[1] = (byte) ((*dest - d - 2) & 0xFF);
316 	}
317 	return (addrcount);
318 }
319 
320 
321 
322 /*
323  * Insert generic data into a bootp packet.  The data is assumed to already
324  * be in RFC1048 format.  It is inserted using a first-fit algorithm which
325  * attempts to insert as many tags as possible.  Tags and data which are
326  * too large to fit are skipped; any remaining tags are tried until they
327  * have all been exhausted.
328  * Return zero if everything fits.
329  */
330 
331 static int
332 insert_generic(gendata, buff, bytesleft)
333 	struct shared_bindata *gendata;
334 	byte **buff;
335 	int *bytesleft;
336 {
337 	byte *srcptr;
338 	int length, numbytes;
339 	int skipped = 0;
340 
341 	if (gendata == NULL)
342 		return (0);
343 
344 	srcptr = gendata->data;
345 	length = gendata->length;
346 	while ((length > 0) && (*bytesleft > 0)) {
347 		switch (*srcptr) {
348 		case TAG_END:
349 			length = 0;			/* Force an exit on next iteration */
350 			break;
351 		case TAG_PAD:
352 			*(*buff)++ = *srcptr++;
353 			(*bytesleft)--;
354 			length--;
355 			break;
356 		default:
357 			numbytes = srcptr[1] + 2;
358 			if (*bytesleft < numbytes)
359 				skipped += numbytes;
360 			else {
361 				bcopy(srcptr, *buff, numbytes);
362 				(*buff) += numbytes;
363 				(*bytesleft) -= numbytes;
364 			}
365 			srcptr += numbytes;
366 			length -= numbytes;
367 			break;
368 		}
369 	} /* while */
370 	return (skipped);
371 }
372 
373 /*
374  * Insert the unsigned long "value" into memory starting at the byte
375  * pointed to by the byte pointer (*dest).  (*dest) is updated to
376  * point to the next available byte.
377  *
378  * Since it is desirable to internally store network addresses in network
379  * byte order (in struct in_addr's), this routine expects longs to be
380  * passed in network byte order.
381  *
382  * However, due to the nature of the main algorithm, the long must be in
383  * host byte order, thus necessitating the use of ntohl() first.
384  */
385 
386 void
387 insert_u_long(value, dest)
388 	u_int32 value;
389 	byte **dest;
390 {
391 	byte *temp;
392 	int n;
393 
394 	value = ntohl(value);		/* Must use host byte order here */
395 	temp = (*dest += 4);
396 	for (n = 4; n > 0; n--) {
397 		*--temp = (byte) (value & 0xFF);
398 		value >>= 8;
399 	}
400 	/* Final result is network byte order */
401 }
402 
403 /*
404  * Local Variables:
405  * tab-width: 4
406  * c-indent-level: 4
407  * c-argdecl-indent: 4
408  * c-continued-statement-offset: 4
409  * c-continued-brace-offset: -4
410  * c-label-offset: -4
411  * c-brace-offset: 0
412  * End:
413  */
414