Home | History | Annotate | Line # | Download | only in rpc.bootparamd
bootparamd.c revision 1.11
      1  1.11  mycroft /*	$NetBSD: bootparamd.c,v 1.11 1996/12/08 13:53:25 mycroft Exp $	*/
      2   1.7  thorpej 
      3   1.1  deraadt /*
      4   1.1  deraadt  * This code is not copyright, and is placed in the public domain.
      5   1.1  deraadt  * Feel free to use and modify. Please send modifications and/or
      6   1.1  deraadt  * suggestions + bug fixes to Klas Heggemann <klas (at) nada.kth.se>
      7   1.2  deraadt  *
      8   1.1  deraadt  * Various small changes by Theo de Raadt <deraadt (at) fsa.ca>
      9   1.2  deraadt  * Parser rewritten (adding YP support) by Roland McGrath <roland (at) frob.com>
     10   1.1  deraadt  */
     11   1.1  deraadt 
     12   1.1  deraadt #include <sys/types.h>
     13   1.1  deraadt #include <sys/ioctl.h>
     14   1.1  deraadt #include <sys/stat.h>
     15   1.1  deraadt #include <sys/socket.h>
     16  1.10  mycroft 
     17  1.10  mycroft #include <ctype.h>
     18  1.10  mycroft #include <err.h>
     19  1.10  mycroft #include <netdb.h>
     20   1.1  deraadt #include <stdio.h>
     21  1.10  mycroft #include <string.h>
     22   1.1  deraadt #include <syslog.h>
     23  1.10  mycroft 
     24  1.10  mycroft #include <netinet/in.h>
     25   1.9      cgd #include <arpa/inet.h>
     26  1.10  mycroft 
     27  1.10  mycroft #include <rpc/rpc.h>
     28  1.10  mycroft #include <rpcsvc/bootparam_prot.h>
     29  1.10  mycroft 
     30   1.2  deraadt #include "pathnames.h"
     31   1.1  deraadt 
     32   1.1  deraadt #define MAXLEN 800
     33   1.1  deraadt 
     34   1.1  deraadt static char hostname[MAX_MACHINE_NAME];
     35   1.1  deraadt static char askname[MAX_MACHINE_NAME];
     36   1.1  deraadt static char domain_name[MAX_MACHINE_NAME];
     37   1.1  deraadt 
     38   1.5       pk extern void bootparamprog_1 __P((struct svc_req *, SVCXPRT *));
     39   1.1  deraadt 
     40   1.5       pk int	_rpcsvcdirty = 0;
     41   1.5       pk int	_rpcpmstart = 0;
     42   1.1  deraadt int     debug = 0;
     43   1.1  deraadt int     dolog = 0;
     44   1.1  deraadt unsigned long route_addr, inet_addr();
     45   1.1  deraadt struct sockaddr_in my_addr;
     46  1.11  mycroft extern char *__progname;
     47   1.2  deraadt char   *bootpfile = _PATH_BOOTPARAMS;
     48   1.1  deraadt 
     49   1.1  deraadt extern char *optarg;
     50   1.1  deraadt extern int optind;
     51   1.1  deraadt 
     52   1.1  deraadt void
     53   1.1  deraadt usage()
     54   1.1  deraadt {
     55   1.1  deraadt 	fprintf(stderr,
     56   1.2  deraadt 	    "usage: rpc.bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n");
     57  1.10  mycroft 	exit(1);
     58   1.1  deraadt }
     59   1.1  deraadt 
     60   1.1  deraadt 
     61   1.1  deraadt /*
     62   1.1  deraadt  * ever familiar
     63   1.1  deraadt  */
     64   1.1  deraadt int
     65   1.1  deraadt main(argc, argv)
     66   1.1  deraadt 	int     argc;
     67   1.1  deraadt 	char  **argv;
     68   1.1  deraadt {
     69   1.1  deraadt 	SVCXPRT *transp;
     70   1.1  deraadt 	struct hostent *he;
     71   1.1  deraadt 	struct stat buf;
     72   1.6     mark 	int    c;
     73   1.1  deraadt 
     74   1.6     mark 	while ((c = getopt(argc, argv, "dsr:f:")) != -1)
     75   1.1  deraadt 		switch (c) {
     76   1.1  deraadt 		case 'd':
     77   1.1  deraadt 			debug = 1;
     78   1.1  deraadt 			break;
     79   1.1  deraadt 		case 'r':
     80   1.1  deraadt 			if (isdigit(*optarg)) {
     81   1.1  deraadt 				route_addr = inet_addr(optarg);
     82   1.1  deraadt 				break;
     83   1.1  deraadt 			}
     84   1.1  deraadt 			he = gethostbyname(optarg);
     85   1.1  deraadt 			if (!he) {
     86  1.10  mycroft 				warnx("no such host: %s\n", optarg);
     87   1.1  deraadt 				usage();
     88   1.1  deraadt 			}
     89   1.1  deraadt 			bcopy(he->h_addr, (char *) &route_addr, sizeof(route_addr));
     90   1.1  deraadt 			break;
     91   1.1  deraadt 		case 'f':
     92   1.1  deraadt 			bootpfile = optarg;
     93   1.1  deraadt 			break;
     94   1.1  deraadt 		case 's':
     95   1.1  deraadt 			dolog = 1;
     96   1.1  deraadt #ifndef LOG_DAEMON
     97  1.11  mycroft 			openlog(__progname, 0, 0);
     98   1.1  deraadt #else
     99  1.11  mycroft 			openlog(__progname, 0, LOG_DAEMON);
    100   1.1  deraadt 			setlogmask(LOG_UPTO(LOG_NOTICE));
    101   1.1  deraadt #endif
    102   1.1  deraadt 			break;
    103   1.1  deraadt 		default:
    104   1.1  deraadt 			usage();
    105   1.1  deraadt 		}
    106   1.1  deraadt 
    107  1.10  mycroft 	if (stat(bootpfile, &buf))
    108  1.10  mycroft 		err(1, "%s", bootpfile);
    109  1.10  mycroft 
    110   1.1  deraadt 	if (!route_addr) {
    111   1.1  deraadt 		get_myaddress(&my_addr);
    112   1.1  deraadt 		bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof(route_addr));
    113   1.1  deraadt 	}
    114  1.10  mycroft 	if (!debug) {
    115  1.10  mycroft 		if (daemon(0, 0))
    116  1.10  mycroft 			err(1, "can't detach from terminal");
    117  1.10  mycroft 	}
    118   1.1  deraadt 
    119   1.1  deraadt 	(void) pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
    120   1.1  deraadt 
    121   1.1  deraadt 	transp = svcudp_create(RPC_ANYSOCK);
    122  1.10  mycroft 	if (transp == NULL)
    123  1.10  mycroft 		errx(1, "can't create udp service");
    124  1.10  mycroft 
    125  1.10  mycroft 	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1,
    126  1.10  mycroft 	    IPPROTO_UDP))
    127  1.10  mycroft 		errx(1, "unable to register BOOTPARAMPROG version %d, udp",
    128   1.1  deraadt 		    BOOTPARAMVERS);
    129  1.10  mycroft 
    130   1.1  deraadt 	svc_run();
    131  1.10  mycroft 	errx(1, "svc_run returned");
    132   1.1  deraadt }
    133   1.1  deraadt 
    134   1.1  deraadt bp_whoami_res *
    135   1.5       pk bootparamproc_whoami_1_svc(whoami, rqstp)
    136   1.1  deraadt 	bp_whoami_arg *whoami;
    137   1.5       pk 	struct svc_req *rqstp;
    138   1.1  deraadt {
    139   1.9      cgd 	static bp_whoami_res res;
    140   1.9      cgd 	struct hostent *he;
    141   1.9      cgd 	struct in_addr inaddr;
    142   1.1  deraadt 	long    haddr;
    143   1.2  deraadt 
    144   1.1  deraadt 	if (debug)
    145  1.10  mycroft 		warnx("whoami got question for %d.%d.%d.%d\n",
    146   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
    147   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
    148   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
    149   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
    150   1.1  deraadt 	if (dolog)
    151   1.1  deraadt 		syslog(LOG_NOTICE, "whoami got question for %d.%d.%d.%d\n",
    152   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
    153   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
    154   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
    155   1.1  deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
    156   1.1  deraadt 
    157   1.1  deraadt 	bcopy((char *) &whoami->client_address.bp_address_u.ip_addr, (char *) &haddr,
    158   1.1  deraadt 	    sizeof(haddr));
    159   1.1  deraadt 	he = gethostbyaddr((char *) &haddr, sizeof(haddr), AF_INET);
    160   1.9      cgd 	if (he)
    161   1.9      cgd 		strcpy(askname, he->h_name);
    162   1.9      cgd 	else {
    163   1.9      cgd 		inaddr.s_addr = haddr;
    164   1.9      cgd 		strcpy(askname, inet_ntoa(inaddr));
    165   1.9      cgd 	}
    166   1.1  deraadt 
    167   1.1  deraadt 	if (debug)
    168  1.10  mycroft 		warnx("This is host %s\n", askname);
    169   1.1  deraadt 	if (dolog)
    170   1.9      cgd 		syslog(LOG_NOTICE, "This is host %s\n", askname);
    171   1.1  deraadt 
    172   1.2  deraadt 	if (!lookup_bootparam(askname, hostname, NULL, NULL, NULL)) {
    173   1.1  deraadt 		res.client_name = hostname;
    174   1.1  deraadt 		getdomainname(domain_name, MAX_MACHINE_NAME);
    175   1.1  deraadt 		res.domain_name = domain_name;
    176   1.1  deraadt 
    177   1.1  deraadt 		if (res.router_address.address_type != IP_ADDR_TYPE) {
    178   1.1  deraadt 			res.router_address.address_type = IP_ADDR_TYPE;
    179   1.1  deraadt 			bcopy(&route_addr, &res.router_address.bp_address_u.ip_addr, 4);
    180   1.1  deraadt 		}
    181   1.1  deraadt 		if (debug)
    182  1.10  mycroft 			warnx("Returning %s   %s    %d.%d.%d.%d\n",
    183   1.1  deraadt 			    res.client_name, res.domain_name,
    184   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.net,
    185   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.host,
    186   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.lh,
    187   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.impno);
    188   1.1  deraadt 		if (dolog)
    189   1.1  deraadt 			syslog(LOG_NOTICE, "Returning %s   %s    %d.%d.%d.%d\n",
    190   1.1  deraadt 			    res.client_name, res.domain_name,
    191   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.net,
    192   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.host,
    193   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.lh,
    194   1.1  deraadt 			    255 & res.router_address.bp_address_u.ip_addr.impno);
    195   1.1  deraadt 
    196   1.1  deraadt 		return (&res);
    197   1.1  deraadt 	}
    198   1.1  deraadt 	if (debug)
    199  1.10  mycroft 		warnx("whoami failed\n");
    200   1.1  deraadt 	if (dolog)
    201   1.1  deraadt 		syslog(LOG_NOTICE, "whoami failed\n");
    202   1.1  deraadt 	return (NULL);
    203   1.1  deraadt }
    204   1.1  deraadt 
    205   1.1  deraadt 
    206   1.1  deraadt bp_getfile_res *
    207   1.5       pk bootparamproc_getfile_1_svc(getfile, rqstp)
    208   1.1  deraadt 	bp_getfile_arg *getfile;
    209   1.5       pk 	struct svc_req *rqstp;
    210   1.1  deraadt {
    211   1.1  deraadt 	static bp_getfile_res res;
    212   1.9      cgd 	struct hostent *he;
    213   1.2  deraadt 	int     err;
    214   1.1  deraadt 
    215   1.1  deraadt 	if (debug)
    216  1.10  mycroft 		warnx("getfile got question for \"%s\" and file \"%s\"\n",
    217   1.1  deraadt 		    getfile->client_name, getfile->file_id);
    218   1.1  deraadt 
    219   1.1  deraadt 	if (dolog)
    220   1.1  deraadt 		syslog(LOG_NOTICE, "getfile got question for \"%s\" and file \"%s\"\n",
    221   1.1  deraadt 		    getfile->client_name, getfile->file_id);
    222   1.1  deraadt 
    223   1.1  deraadt 	he = NULL;
    224   1.1  deraadt 	he = gethostbyname(getfile->client_name);
    225   1.1  deraadt 	if (!he)
    226   1.1  deraadt 		goto failed;
    227   1.1  deraadt 
    228   1.1  deraadt 	strcpy(askname, he->h_name);
    229   1.2  deraadt 	err = lookup_bootparam(askname, NULL, getfile->file_id,
    230   1.2  deraadt 	    &res.server_name, &res.server_path);
    231   1.2  deraadt 	if (err == 0) {
    232   1.2  deraadt 		he = gethostbyname(res.server_name);
    233   1.2  deraadt 		if (!he)
    234   1.2  deraadt 			goto failed;
    235   1.2  deraadt 		bcopy(he->h_addr, &res.server_address.bp_address_u.ip_addr, 4);
    236   1.2  deraadt 		res.server_address.address_type = IP_ADDR_TYPE;
    237   1.2  deraadt 	} else if (err == ENOENT && !strcmp(getfile->file_id, "dump")) {
    238   1.2  deraadt 		/* Special for dump, answer with null strings. */
    239   1.2  deraadt 		res.server_name[0] = '\0';
    240   1.2  deraadt 		res.server_path[0] = '\0';
    241   1.2  deraadt 		bzero(&res.server_address.bp_address_u.ip_addr, 4);
    242   1.2  deraadt 	} else {
    243   1.2  deraadt failed:
    244   1.1  deraadt 		if (debug)
    245  1.10  mycroft 			warnx("getfile failed for %s\n",
    246   1.2  deraadt 			    getfile->client_name);
    247   1.1  deraadt 		if (dolog)
    248   1.1  deraadt 			syslog(LOG_NOTICE,
    249   1.2  deraadt 			    "getfile failed for %s\n", getfile->client_name);
    250   1.2  deraadt 		return (NULL);
    251   1.1  deraadt 	}
    252   1.2  deraadt 
    253   1.1  deraadt 	if (debug)
    254  1.10  mycroft 		warnx(
    255   1.2  deraadt 		    "returning server:%s path:%s address: %d.%d.%d.%d\n",
    256   1.2  deraadt 		    res.server_name, res.server_path,
    257   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.net,
    258   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.host,
    259   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.lh,
    260   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.impno);
    261   1.1  deraadt 	if (dolog)
    262   1.1  deraadt 		syslog(LOG_NOTICE,
    263   1.2  deraadt 		    "returning server:%s path:%s address: %d.%d.%d.%d\n",
    264   1.2  deraadt 		    res.server_name, res.server_path,
    265   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.net,
    266   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.host,
    267   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.lh,
    268   1.2  deraadt 		    255 & res.server_address.bp_address_u.ip_addr.impno);
    269   1.2  deraadt 	return (&res);
    270   1.1  deraadt }
    271   1.1  deraadt 
    272   1.1  deraadt 
    273   1.2  deraadt int
    274   1.2  deraadt lookup_bootparam(client, client_canonical, id, server, path)
    275   1.2  deraadt 	char	*client;
    276   1.2  deraadt 	char	*client_canonical;
    277   1.2  deraadt 	char	*id;
    278   1.2  deraadt 	char	**server;
    279   1.2  deraadt 	char	**path;
    280   1.1  deraadt {
    281   1.2  deraadt 	FILE   *f = fopen(bootpfile, "r");
    282   1.2  deraadt #ifdef YP
    283   1.2  deraadt 	static char *ypbuf = NULL;
    284   1.2  deraadt 	static int ypbuflen = 0;
    285   1.2  deraadt #endif
    286   1.2  deraadt 	static char buf[BUFSIZ];
    287   1.2  deraadt 	char   *bp, *word;
    288   1.2  deraadt 	size_t  idlen = id == NULL ? 0 : strlen(id);
    289   1.2  deraadt 	int     contin = 0;
    290   1.2  deraadt 	int     found = 0;
    291   1.2  deraadt 
    292   1.2  deraadt 	if (f == NULL)
    293   1.2  deraadt 		return EINVAL;	/* ? */
    294   1.2  deraadt 
    295   1.2  deraadt 	while (fgets(buf, sizeof buf, f)) {
    296   1.2  deraadt 		int     wascontin = contin;
    297   1.2  deraadt 		contin = buf[strlen(buf) - 2] == '\\';
    298   1.2  deraadt 		bp = buf + strspn(buf, " \t\n");
    299   1.2  deraadt 
    300   1.2  deraadt 		switch (wascontin) {
    301   1.2  deraadt 		case -1:
    302   1.2  deraadt 			/* Continuation of uninteresting line */
    303   1.2  deraadt 			contin *= -1;
    304   1.2  deraadt 			continue;
    305   1.2  deraadt 		case 0:
    306   1.2  deraadt 			/* New line */
    307   1.2  deraadt 			contin *= -1;
    308   1.2  deraadt 			if (*bp == '#')
    309   1.2  deraadt 				continue;
    310   1.2  deraadt 			if ((word = strsep(&bp, " \t\n")) == NULL)
    311   1.2  deraadt 				continue;
    312   1.2  deraadt #ifdef YP
    313   1.2  deraadt 			/* A + in the file means try YP now */
    314   1.2  deraadt 			if (!strcmp(word, "+")) {
    315   1.2  deraadt 				char   *ypdom;
    316   1.2  deraadt 
    317   1.2  deraadt 				if (yp_get_default_domain(&ypdom) ||
    318   1.2  deraadt 				    yp_match(ypdom, "bootparams", client,
    319   1.2  deraadt 					strlen(client), &ypbuf, &ypbuflen))
    320   1.2  deraadt 					continue;
    321   1.2  deraadt 				bp = ypbuf;
    322   1.2  deraadt 				word = client;
    323   1.2  deraadt 				contin *= -1;
    324   1.2  deraadt 				break;
    325   1.2  deraadt 			}
    326   1.2  deraadt #endif
    327   1.2  deraadt 			/* See if this line's client is the one we are
    328   1.2  deraadt 			 * looking for */
    329   1.8      cgd 			if (strcasecmp(word, client) != 0) {
    330   1.2  deraadt 				/*
    331   1.2  deraadt 				 * If it didn't match, try getting the
    332   1.2  deraadt 				 * canonical host name of the client
    333   1.2  deraadt 				 * on this line and comparing that to
    334   1.2  deraadt 				 * the client we are looking for
    335   1.2  deraadt 				 */
    336   1.2  deraadt 				struct hostent *hp = gethostbyname(word);
    337   1.8      cgd 				if (hp == NULL ||
    338   1.8      cgd 				    strcasecmp(hp->h_name, client))
    339   1.2  deraadt 					continue;
    340   1.1  deraadt 			}
    341   1.2  deraadt 			contin *= -1;
    342   1.2  deraadt 			break;
    343   1.2  deraadt 		case 1:
    344   1.2  deraadt 			/* Continued line we want to parse below */
    345   1.1  deraadt 			break;
    346   1.1  deraadt 		}
    347   1.1  deraadt 
    348   1.2  deraadt 		if (client_canonical)
    349   1.2  deraadt 			strncpy(client_canonical, word, MAX_MACHINE_NAME);
    350   1.1  deraadt 
    351   1.2  deraadt 		/* We have found a line for CLIENT */
    352   1.4    glass 		if (id == NULL) {
    353   1.4    glass 			(void) fclose(f);
    354   1.2  deraadt 			return 0;
    355   1.4    glass 		}
    356   1.2  deraadt 
    357   1.2  deraadt 		/* Look for a value for the parameter named by ID */
    358   1.2  deraadt 		while ((word = strsep(&bp, " \t\n")) != NULL) {
    359   1.2  deraadt 			if (!strncmp(word, id, idlen) && word[idlen] == '=') {
    360   1.2  deraadt 				/* We have found the entry we want */
    361   1.2  deraadt 				*server = &word[idlen + 1];
    362   1.2  deraadt 				*path = strchr(*server, ':');
    363   1.2  deraadt 				if (*path == NULL)
    364   1.2  deraadt 					/* Malformed entry */
    365   1.2  deraadt 					continue;
    366   1.2  deraadt 				*(*path)++ = '\0';
    367   1.2  deraadt 				(void) fclose(f);
    368   1.2  deraadt 				return 0;
    369   1.2  deraadt 			}
    370   1.1  deraadt 		}
    371   1.2  deraadt 
    372   1.2  deraadt 		found = 1;
    373   1.1  deraadt 	}
    374   1.1  deraadt 
    375   1.2  deraadt 	(void) fclose(f);
    376   1.2  deraadt 	return found ? ENOENT : EPERM;
    377   1.1  deraadt }
    378