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