/************************************************************************
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.
************************************************************************/
/*
* 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
*/
/*
* Initialization such as command-line processing is done and then the
* main server loop is started.
*/
int
main(int argc, char **argv)
{
struct host *hp;
char *stmp;
int n;
/* 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 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);
}
exit(0);
}
/*
* Make a "TAG 18" file for this host.
* (Insert the RFC1497 options.)
*/
/*
* 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 ((size_t)len != fwrite(buffer, 1, len, fp)) {
report(LOG_ERR, "write failed on \"%s\" : %s",
hp->exten_file->string, get_errmsg());
}
fclose(fp);