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