1 /************************************************************************
2 Copyright 1988, 1991 by Carnegie Mellon University
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted, provided
8 that the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation, and that the name of Carnegie Mellon University not be used
11 in advertising or publicity pertaining to distribution of the software
12 without specific, written prior permission.
14 CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
15 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
16 IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
17 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 ************************************************************************/
27 * bootpef - BOOTP Extension File generator
28 * Makes an "Extension File" for each host entry that
29 * defines an and Extension File. (See RFC1497, tag 18.)
42 #include <sys/types.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h> /* inet_ntoa */
60 /* Yes, memcpy is OK here (no overlapped copies). */
61 #define bcopy(a,b,c) memcpy(b,a,c)
62 #define bzero(p,l) memset(p,0,l)
63 #define bcmp(a,b,c) memcmp(a,b,c)
74 #include "patchlevel.h"
76 #define BUFFERSIZE 0x4000
79 #define CONFIG_FILE "/etc/bootptab"
85 * Externals, forward declarations, and global variables
88 static void mktagfile(struct host *);
89 static void usage(void);
97 int debug = 0; /* Debugging flag (level) */
101 * Globals below are associated with the bootp database file (bootptab).
104 char *bootptab = CONFIG_FILE;
108 * Print "usage" message and exit
114 "usage: $s [ -c chdir ] [-d level] [-f configfile] [host...]\n");
115 fprintf(stderr, "\t -c n\tset current directory\n");
116 fprintf(stderr, "\t -d n\tset debug level\n");
117 fprintf(stderr, "\t -f n\tconfig file name\n");
123 * Initialization such as command-line processing is done and then the
124 * main server loop is started.
135 progname = strrchr(argv[0], '/');
136 if (progname) progname++;
137 else progname = argv[0];
139 /* Get work space for making tag 18 files. */
140 buffer = (byte *) malloc(BUFFERSIZE);
142 report(LOG_ERR, "malloc failed");
146 * Set defaults that might be changed by option switches.
153 for (argc--, argv++; argc > 0; argc--, argv++) {
154 if (argv[0][0] != '-')
156 switch (argv[0][1]) {
158 case 'c': /* chdir_path */
160 stmp = &(argv[0][2]);
166 if (!stmp || (stmp[0] != '/')) {
168 "bootpd: invalid chdir specification\n");
174 case 'd': /* debug */
176 stmp = &(argv[0][2]);
177 } else if (argv[1] && argv[1][0] == '-') {
179 * Backwards-compatible behavior:
180 * no parameter, so just increment the debug flag.
189 if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
191 "bootpd: invalid debug level\n");
197 case 'f': /* config file */
199 stmp = &(argv[0][2]);
209 fprintf(stderr, "bootpd: unknown switch: -%c\n",
216 /* Get the timezone. */
219 /* Allocate hash tables. */
223 * Read the bootptab file.
225 readtab(1); /* force read */
227 /* Set the cwd (i.e. to /tftpboot) */
229 if (chdir(chdir_path) < 0)
230 report(LOG_ERR, "%s: chdir failed", chdir_path);
232 /* If there are host names on the command line, do only those. */
234 unsigned int tlen, hashcode;
237 tlen = strlen(argv[0]);
238 hashcode = hash_HashFunction((u_char *)argv[0], tlen);
239 hp = (struct host *) hash_Lookup(nmhashtable,
243 printf("%s: no matching entry\n", argv[0]);
246 if (!hp->flags.exten_file) {
247 printf("%s: no extension file\n", argv[0]);
256 /* No host names specified. Do them all. */
257 hp = (struct host *) hash_FirstEntry(nmhashtable);
260 hp = (struct host *) hash_NextEntry(nmhashtable);
268 * Make a "TAG 18" file for this host.
269 * (Insert the RFC1497 options.)
280 if (!hp->flags.exten_file)
284 bytesleft = BUFFERSIZE;
285 bcopy(vm_rfc1048, vp, 4); /* Copy in the magic cookie */
290 * The "extension file" options are appended by the following
291 * function (which is shared with bootpd.c).
293 len = dovend_rfc1497(hp, vp, bytesleft);
298 report(LOG_ERR, "%s: too much option data",
299 hp->exten_file->string);
305 /* Write the buffer to the extension file. */
306 printf("Updating \"%s\"\n", hp->exten_file->string);
307 if ((fp = fopen(hp->exten_file->string, "w")) == NULL) {
308 report(LOG_ERR, "error opening \"%s\": %s",
309 hp->exten_file->string, get_errmsg());
313 if (len != fwrite(buffer, 1, len, fp)) {
314 report(LOG_ERR, "write failed on \"%s\" : %s",
315 hp->exten_file->string, get_errmsg());
325 * c-argdecl-indent: 4
326 * c-continued-statement-offset: 4
327 * c-continued-brace-offset: -4