summaryrefslogtreecommitdiff
path: root/usr.sbin/bootpd/bootpef.c
blob: f41fd97b56119e2e62c61c81ff54553ba86114fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/************************************************************************
          Copyright 1988, 1991 by Carnegie Mellon University

                          All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of Carnegie Mellon University not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission.

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
************************************************************************/

#ifndef lint
static char rcsid[] = "$Id: bootpef.c,v 1.2 1996/08/22 09:24:28 deraadt Exp $";
#endif


/*
 * bootpef - BOOTP Extension File generator
 *	Makes an "Extension File" for each host entry that
 *	defines an and Extension File. (See RFC1497, tag 18.)
 *
 * HISTORY
 *	See ./Changes
 *
 * BUGS
 *	See ./ToDo
 */



#ifdef	__STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#include <sys/types.h>
#include <sys/time.h>

#include <netinet/in.h>
#include <arpa/inet.h>			/* inet_ntoa */

#ifndef	NO_UNISTD
#include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <syslog.h>

#ifndef	USE_BFUNCS
#include <memory.h>
/* Yes, memcpy is OK here (no overlapped copies). */
#define bcopy(a,b,c)    memcpy(b,a,c)
#define bzero(p,l)      memset(p,0,l)
#define bcmp(a,b,c)     memcmp(a,b,c)
#endif

#include "bootp.h"
#include "hash.h"
#include "hwaddr.h"
#include "bootpd.h"
#include "dovend.h"
#include "readfile.h"
#include "report.h"
#include "tzone.h"
#include "patchlevel.h"

#define	BUFFERSIZE   		0x4000

#ifndef CONFIG_FILE
#define CONFIG_FILE		"/etc/bootptab"
#endif



/*
 * Externals, forward declarations, and global variables
 */

#ifdef	__STDC__
#define P(args) args
#else
#define P(args) ()
#endif

static void dovend_rfc1048 P((struct bootp *, struct host *, int32));
static void mktagfile P((struct host *));
static void usage P((void));

#undef P


/*
 * General
 */

char *progname;
char *chdir_path;
int debug = 0;					/* Debugging flag (level) */
byte *buffer;

/*
 * Globals below are associated with the bootp database file (bootptab).
 */

char *bootptab = CONFIG_FILE;


/*
 * Print "usage" message and exit
 */
static void
usage()
{
	fprintf(stderr,
	   "usage:  $s [ -c chdir ] [-d level] [-f configfile] [host...]\n");
	fprintf(stderr, "\t -c n\tset current directory\n");
	fprintf(stderr, "\t -d n\tset debug level\n");
	fprintf(stderr, "\t -f n\tconfig file name\n");
	exit(1);
}


/*
 * Initialization such as command-line processing is done and then the
 * main server loop is started.
 */
int
main(argc, argv)
	int argc;
	char **argv;
{
	struct host *hp;
	char *stmp;
	int n;

	progname = strrchr(argv[0], '/');
	if (progname) progname++;
	else progname = argv[0];

	/* Get work space for making tag 18 files. */
	buffer = (byte *) malloc(BUFFERSIZE);
	if (!buffer) {
		report(LOG_ERR, "malloc failed");
		exit(1);
	}
	/*
	 * Set defaults that might be changed by option switches.
	 */
	stmp = NULL;

	/*
	 * Read switches.
	 */
	for (argc--, argv++; argc > 0; argc--, argv++) {
		if (argv[0][0] != '-')
			break;
		switch (argv[0][1]) {

		case 'c':				/* chdir_path */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			if (!stmp || (stmp[0] != '/')) {
				fprintf(stderr,
						"bootpd: invalid chdir specification\n");
				break;
			}
			chdir_path = stmp;
			break;

		case 'd':				/* debug */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else if (argv[1] && argv[1][0] == '-') {
				/*
				 * Backwards-compatible behavior:
				 * no parameter, so just increment the debug flag.
				 */
				debug++;
				break;
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
				fprintf(stderr,
						"bootpd: invalid debug level\n");
				break;
			}
			debug = n;
			break;

		case 'f':				/* config file */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			bootptab = stmp;
			break;

		default:
			fprintf(stderr, "bootpd: unknown switch: -%c\n",
					argv[0][1]);
			usage();
			break;
		}
	}

	/* Get the timezone. */
	tzone_init();

	/* Allocate hash tables. */
	rdtab_init();

	/*
	 * Read the bootptab file.
	 */
	readtab(1);					/* force read */

	/* Set the cwd (i.e. to /tftpboot) */
	if (chdir_path) {
		if (chdir(chdir_path) < 0)
			report(LOG_ERR, "%s: chdir failed", chdir_path);
	}
	/* If there are host names on the command line, do only those. */
	if (argc > 0) {
		unsigned int tlen, hashcode;

		while (argc) {
			tlen = strlen(argv[0]);
			hashcode = hash_HashFunction((u_char *)argv[0], tlen);
			hp = (struct host *) hash_Lookup(nmhashtable,
											 hashcode,
											 nmcmp, argv[0]);
			if (!hp) {
				printf("%s: no matching entry\n", argv[0]);
				exit(1);
			}
			if (!hp->flags.exten_file) {
				printf("%s: no extension file\n", argv[0]);
				exit(1);
			}
			mktagfile(hp);
			argv++;
			argc--;
		}
		exit(0);
	}
	/* No host names specified.  Do them all. */
	hp = (struct host *) hash_FirstEntry(nmhashtable);
	while (hp != NULL) {
		mktagfile(hp);
		hp = (struct host *) hash_NextEntry(nmhashtable);
	}
}



/*
 * Make a "TAG 18" file for this host.
 * (Insert the RFC1497 options.)
 */

static void
mktagfile(hp)
	struct host *hp;
{
	FILE *fp;
	int bytesleft, len;
	byte *vp;
	char *tmpstr;

	if (!hp->flags.exten_file)
		return;

	vp = buffer;
	bytesleft = BUFFERSIZE;
	bcopy(vm_rfc1048, vp, 4);	/* Copy in the magic cookie */
	vp += 4;
	bytesleft -= 4;

	/*
	 * The "extension file" options are appended by the following
	 * function (which is shared with bootpd.c).
	 */
	len = dovend_rfc1497(hp, vp, bytesleft);
	vp += len;
	bytesleft -= len;

	if (bytesleft < 1) {
		report(LOG_ERR, "%s: too much option data",
			   hp->exten_file->string);
		return;
	}
	*vp++ = TAG_END;
	bytesleft--;

	/* Write the buffer to the extension file. */
	printf("Updating \"%s\"\n", hp->exten_file->string);
	if ((fp = fopen(hp->exten_file->string, "w")) == NULL) {
		report(LOG_ERR, "error opening \"%s\": %s",
			   hp->exten_file->string, get_errmsg());
		return;
	}
	len = vp - buffer;
	if (len != fwrite(buffer, 1, len, fp)) {
		report(LOG_ERR, "write failed on \"%s\" : %s",
			   hp->exten_file->string, get_errmsg());
	}
	fclose(fp);

} /* dovend_rfc1048 */

/*
 * Local Variables:
 * tab-width: 4
 * c-indent-level: 4
 * c-argdecl-indent: 4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: -4
 * c-label-offset: -4
 * c-brace-offset: 0
 * End:
 */