Home | History | Annotate | Line # | Download | only in rpc.bootparamd
bootparamd.c revision 1.33.2.3
      1  1.33.2.3     enami /*	$NetBSD: bootparamd.c,v 1.33.2.3 2000/08/07 01:20:20 enami 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.12   thorpej #include <sys/cdefs.h>
     13      1.12   thorpej #ifndef lint
     14  1.33.2.3     enami __RCSID("$NetBSD: bootparamd.c,v 1.33.2.3 2000/08/07 01:20:20 enami Exp $");
     15      1.12   thorpej #endif
     16      1.12   thorpej 
     17       1.1   deraadt #include <sys/types.h>
     18       1.1   deraadt #include <sys/ioctl.h>
     19       1.1   deraadt #include <sys/stat.h>
     20       1.1   deraadt #include <sys/socket.h>
     21      1.10   mycroft 
     22  1.33.2.1   thorpej #include <assert.h>
     23      1.10   mycroft #include <ctype.h>
     24      1.21  christos #include <errno.h>
     25      1.10   mycroft #include <err.h>
     26  1.33.2.1   thorpej #include <fnmatch.h>
     27      1.10   mycroft #include <netdb.h>
     28      1.12   thorpej #include <stdlib.h>
     29       1.1   deraadt #include <stdio.h>
     30      1.10   mycroft #include <string.h>
     31       1.1   deraadt #include <syslog.h>
     32      1.12   thorpej #include <unistd.h>
     33      1.20   thorpej #include <util.h>
     34      1.22    itojun #include <ifaddrs.h>
     35      1.10   mycroft 
     36      1.23    itojun #include <net/if.h>
     37      1.23    itojun 
     38      1.10   mycroft #include <netinet/in.h>
     39       1.9       cgd #include <arpa/inet.h>
     40      1.10   mycroft 
     41      1.10   mycroft #include <rpc/rpc.h>
     42      1.12   thorpej #include <rpc/pmap_clnt.h>
     43      1.10   mycroft #include <rpcsvc/bootparam_prot.h>
     44      1.12   thorpej #include <rpcsvc/ypclnt.h>
     45      1.10   mycroft 
     46       1.2   deraadt #include "pathnames.h"
     47       1.1   deraadt 
     48       1.1   deraadt #define MAXLEN 800
     49       1.1   deraadt 
     50       1.1   deraadt static char hostname[MAX_MACHINE_NAME];
     51       1.1   deraadt static char askname[MAX_MACHINE_NAME];
     52       1.1   deraadt static char domain_name[MAX_MACHINE_NAME];
     53       1.1   deraadt 
     54       1.5        pk extern void bootparamprog_1 __P((struct svc_req *, SVCXPRT *));
     55       1.1   deraadt 
     56       1.5        pk int	_rpcsvcdirty = 0;
     57       1.5        pk int	_rpcpmstart = 0;
     58       1.1   deraadt int     debug = 0;
     59       1.1   deraadt int     dolog = 0;
     60      1.12   thorpej struct in_addr route_addr;
     61       1.1   deraadt struct sockaddr_in my_addr;
     62      1.11   mycroft extern char *__progname;
     63       1.2   deraadt char   *bootpfile = _PATH_BOOTPARAMS;
     64       1.1   deraadt 
     65      1.12   thorpej int	main __P((int, char *[]));
     66      1.12   thorpej int	lookup_bootparam __P((char *, char *, char *, char **, char **));
     67      1.12   thorpej void	usage __P((void));
     68      1.22    itojun static int get_localaddr __P((const char *, struct sockaddr_in *));
     69       1.1   deraadt 
     70       1.1   deraadt 
     71       1.1   deraadt /*
     72       1.1   deraadt  * ever familiar
     73       1.1   deraadt  */
     74       1.1   deraadt int
     75       1.1   deraadt main(argc, argv)
     76       1.1   deraadt 	int     argc;
     77      1.12   thorpej 	char  *argv[];
     78       1.1   deraadt {
     79       1.1   deraadt 	SVCXPRT *transp;
     80       1.1   deraadt 	struct hostent *he;
     81       1.1   deraadt 	struct stat buf;
     82       1.6      mark 	int    c;
     83       1.1   deraadt 
     84       1.6      mark 	while ((c = getopt(argc, argv, "dsr:f:")) != -1)
     85       1.1   deraadt 		switch (c) {
     86       1.1   deraadt 		case 'd':
     87       1.1   deraadt 			debug = 1;
     88       1.1   deraadt 			break;
     89       1.1   deraadt 		case 'r':
     90       1.1   deraadt 			if (isdigit(*optarg)) {
     91      1.12   thorpej 				if (inet_aton(optarg, &route_addr) != 0)
     92      1.12   thorpej 					break;
     93       1.1   deraadt 			}
     94       1.1   deraadt 			he = gethostbyname(optarg);
     95      1.12   thorpej 			if (he == 0) {
     96      1.13     mikel 				warnx("no such host: %s", optarg);
     97       1.1   deraadt 				usage();
     98       1.1   deraadt 			}
     99      1.15     lukem 			memmove(&route_addr.s_addr, he->h_addr, he->h_length);
    100       1.1   deraadt 			break;
    101       1.1   deraadt 		case 'f':
    102       1.1   deraadt 			bootpfile = optarg;
    103       1.1   deraadt 			break;
    104       1.1   deraadt 		case 's':
    105       1.1   deraadt 			dolog = 1;
    106       1.1   deraadt #ifndef LOG_DAEMON
    107      1.11   mycroft 			openlog(__progname, 0, 0);
    108       1.1   deraadt #else
    109      1.11   mycroft 			openlog(__progname, 0, LOG_DAEMON);
    110       1.1   deraadt 			setlogmask(LOG_UPTO(LOG_NOTICE));
    111       1.1   deraadt #endif
    112       1.1   deraadt 			break;
    113       1.1   deraadt 		default:
    114       1.1   deraadt 			usage();
    115       1.1   deraadt 		}
    116       1.1   deraadt 
    117      1.10   mycroft 	if (stat(bootpfile, &buf))
    118      1.10   mycroft 		err(1, "%s", bootpfile);
    119      1.10   mycroft 
    120      1.12   thorpej 	if (route_addr.s_addr == 0) {
    121      1.22    itojun 		if (get_localaddr(NULL, &my_addr) != 0)
    122      1.22    itojun 			errx(1, "router address not found");
    123      1.12   thorpej 		route_addr.s_addr = my_addr.sin_addr.s_addr;
    124       1.1   deraadt 	}
    125      1.10   mycroft 	if (!debug) {
    126      1.10   mycroft 		if (daemon(0, 0))
    127      1.10   mycroft 			err(1, "can't detach from terminal");
    128      1.10   mycroft 	}
    129      1.20   thorpej 	pidfile(NULL);
    130       1.1   deraadt 
    131       1.1   deraadt 	(void) pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
    132       1.1   deraadt 
    133       1.1   deraadt 	transp = svcudp_create(RPC_ANYSOCK);
    134      1.10   mycroft 	if (transp == NULL)
    135      1.10   mycroft 		errx(1, "can't create udp service");
    136      1.10   mycroft 
    137      1.10   mycroft 	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1,
    138      1.10   mycroft 	    IPPROTO_UDP))
    139      1.33      tron /*
    140      1.33      tron  * Do NOT change the "%u" in the format string below to "%lu". If your
    141      1.33      tron  * build fails update the "rpcgen" program and use "make cleandir" and
    142      1.33      tron  * "make includes" in "src/lib/librpcsvc" afterwards.
    143      1.33      tron  */
    144      1.32  explorer 		errx(1, "unable to register BOOTPARAMPROG version %u, udp",
    145       1.1   deraadt 		    BOOTPARAMVERS);
    146      1.10   mycroft 
    147       1.1   deraadt 	svc_run();
    148      1.10   mycroft 	errx(1, "svc_run returned");
    149       1.1   deraadt }
    150       1.1   deraadt 
    151       1.1   deraadt bp_whoami_res *
    152       1.5        pk bootparamproc_whoami_1_svc(whoami, rqstp)
    153       1.1   deraadt 	bp_whoami_arg *whoami;
    154       1.5        pk 	struct svc_req *rqstp;
    155       1.1   deraadt {
    156       1.9       cgd 	static bp_whoami_res res;
    157       1.9       cgd 	struct hostent *he;
    158      1.18   nathanw 	struct in_addr haddr;
    159      1.21  christos 	int e;
    160       1.2   deraadt 
    161       1.1   deraadt 	if (debug)
    162      1.13     mikel 		warnx("whoami got question for %d.%d.%d.%d",
    163       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
    164       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
    165       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
    166       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
    167       1.1   deraadt 	if (dolog)
    168      1.13     mikel 		syslog(LOG_NOTICE, "whoami got question for %d.%d.%d.%d",
    169       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
    170       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
    171       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
    172       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
    173       1.1   deraadt 
    174      1.15     lukem 	memmove((char *) &haddr,
    175      1.15     lukem 	    (char *) &whoami->client_address.bp_address_u.ip_addr,
    176      1.15     lukem 	    sizeof(haddr));
    177       1.1   deraadt 	he = gethostbyaddr((char *) &haddr, sizeof(haddr), AF_INET);
    178      1.14       mrg 	if (he) {
    179      1.14       mrg 		strncpy(askname, he->h_name, sizeof(askname));
    180      1.14       mrg 		askname[sizeof(askname)-1] = 0;
    181      1.14       mrg 	} else {
    182      1.18   nathanw 		strncpy(askname, inet_ntoa(haddr), sizeof(askname));
    183      1.14       mrg 		askname[sizeof(askname)-1] = 0;
    184       1.9       cgd 	}
    185       1.1   deraadt 
    186       1.1   deraadt 	if (debug)
    187      1.13     mikel 		warnx("This is host %s", askname);
    188       1.1   deraadt 	if (dolog)
    189      1.13     mikel 		syslog(LOG_NOTICE, "This is host %s", askname);
    190       1.1   deraadt 
    191      1.21  christos 	if ((e = lookup_bootparam(askname, hostname, NULL, NULL, NULL)) == 0) {
    192       1.1   deraadt 		res.client_name = hostname;
    193       1.1   deraadt 		getdomainname(domain_name, MAX_MACHINE_NAME);
    194       1.1   deraadt 		res.domain_name = domain_name;
    195       1.1   deraadt 
    196       1.1   deraadt 		if (res.router_address.address_type != IP_ADDR_TYPE) {
    197       1.1   deraadt 			res.router_address.address_type = IP_ADDR_TYPE;
    198      1.15     lukem 			memmove(&res.router_address.bp_address_u.ip_addr,
    199      1.15     lukem 			    &route_addr.s_addr,4);
    200       1.1   deraadt 		}
    201       1.1   deraadt 		if (debug)
    202      1.13     mikel 			warnx("Returning %s   %s    %d.%d.%d.%d",
    203       1.1   deraadt 			    res.client_name, res.domain_name,
    204       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.net,
    205       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.host,
    206       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.lh,
    207      1.14       mrg 			    255 &res.router_address.bp_address_u.ip_addr.impno);
    208       1.1   deraadt 		if (dolog)
    209      1.13     mikel 			syslog(LOG_NOTICE, "Returning %s   %s    %d.%d.%d.%d",
    210       1.1   deraadt 			    res.client_name, res.domain_name,
    211       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.net,
    212       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.host,
    213       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.lh,
    214      1.14       mrg 			    255 &res.router_address.bp_address_u.ip_addr.impno);
    215       1.1   deraadt 
    216       1.1   deraadt 		return (&res);
    217       1.1   deraadt 	}
    218      1.21  christos 	errno = e;
    219       1.1   deraadt 	if (debug)
    220      1.21  christos 		warn("whoami failed");
    221       1.1   deraadt 	if (dolog)
    222      1.21  christos 		syslog(LOG_NOTICE, "whoami failed %m");
    223       1.1   deraadt 	return (NULL);
    224       1.1   deraadt }
    225       1.1   deraadt 
    226       1.1   deraadt 
    227       1.1   deraadt bp_getfile_res *
    228       1.5        pk bootparamproc_getfile_1_svc(getfile, rqstp)
    229       1.1   deraadt 	bp_getfile_arg *getfile;
    230       1.5        pk 	struct svc_req *rqstp;
    231       1.1   deraadt {
    232       1.1   deraadt 	static bp_getfile_res res;
    233       1.9       cgd 	struct hostent *he;
    234       1.2   deraadt 	int     err;
    235       1.1   deraadt 
    236       1.1   deraadt 	if (debug)
    237      1.13     mikel 		warnx("getfile got question for \"%s\" and file \"%s\"",
    238       1.1   deraadt 		    getfile->client_name, getfile->file_id);
    239       1.1   deraadt 
    240       1.1   deraadt 	if (dolog)
    241      1.13     mikel 		syslog(LOG_NOTICE,
    242      1.13     mikel 		    "getfile got question for \"%s\" and file \"%s\"",
    243       1.1   deraadt 		    getfile->client_name, getfile->file_id);
    244       1.1   deraadt 
    245       1.1   deraadt 	he = NULL;
    246       1.1   deraadt 	he = gethostbyname(getfile->client_name);
    247       1.1   deraadt 	if (!he)
    248       1.1   deraadt 		goto failed;
    249       1.1   deraadt 
    250      1.14       mrg 	strncpy(askname, he->h_name, sizeof(askname));
    251      1.14       mrg 	askname[sizeof(askname)-1] = 0;
    252       1.2   deraadt 	err = lookup_bootparam(askname, NULL, getfile->file_id,
    253       1.2   deraadt 	    &res.server_name, &res.server_path);
    254       1.2   deraadt 	if (err == 0) {
    255       1.2   deraadt 		he = gethostbyname(res.server_name);
    256       1.2   deraadt 		if (!he)
    257       1.2   deraadt 			goto failed;
    258      1.15     lukem 		memmove(&res.server_address.bp_address_u.ip_addr,
    259      1.15     lukem 		    he->h_addr, 4);
    260       1.2   deraadt 		res.server_address.address_type = IP_ADDR_TYPE;
    261       1.2   deraadt 	} else if (err == ENOENT && !strcmp(getfile->file_id, "dump")) {
    262       1.2   deraadt 		/* Special for dump, answer with null strings. */
    263       1.2   deraadt 		res.server_name[0] = '\0';
    264       1.2   deraadt 		res.server_path[0] = '\0';
    265      1.15     lukem 		memset(&res.server_address.bp_address_u.ip_addr, 0, 4);
    266       1.2   deraadt 	} else {
    267       1.2   deraadt failed:
    268       1.1   deraadt 		if (debug)
    269      1.13     mikel 			warnx("getfile failed for %s",
    270       1.2   deraadt 			    getfile->client_name);
    271       1.1   deraadt 		if (dolog)
    272       1.1   deraadt 			syslog(LOG_NOTICE,
    273      1.13     mikel 			    "getfile failed for %s", getfile->client_name);
    274       1.2   deraadt 		return (NULL);
    275       1.1   deraadt 	}
    276       1.2   deraadt 
    277       1.1   deraadt 	if (debug)
    278      1.10   mycroft 		warnx(
    279      1.13     mikel 		    "returning server:%s path:%s address: %d.%d.%d.%d",
    280       1.2   deraadt 		    res.server_name, res.server_path,
    281       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.net,
    282       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.host,
    283       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.lh,
    284       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.impno);
    285       1.1   deraadt 	if (dolog)
    286       1.1   deraadt 		syslog(LOG_NOTICE,
    287      1.13     mikel 		    "returning server:%s path:%s address: %d.%d.%d.%d",
    288       1.2   deraadt 		    res.server_name, res.server_path,
    289       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.net,
    290       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.host,
    291       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.lh,
    292       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.impno);
    293       1.2   deraadt 	return (&res);
    294       1.1   deraadt }
    295       1.1   deraadt 
    296       1.1   deraadt 
    297       1.2   deraadt int
    298       1.2   deraadt lookup_bootparam(client, client_canonical, id, server, path)
    299       1.2   deraadt 	char	*client;
    300       1.2   deraadt 	char	*client_canonical;
    301       1.2   deraadt 	char	*id;
    302       1.2   deraadt 	char	**server;
    303       1.2   deraadt 	char	**path;
    304       1.1   deraadt {
    305       1.2   deraadt 	FILE   *f = fopen(bootpfile, "r");
    306       1.2   deraadt #ifdef YP
    307       1.2   deraadt 	static char *ypbuf = NULL;
    308       1.2   deraadt 	static int ypbuflen = 0;
    309       1.2   deraadt #endif
    310       1.2   deraadt 	static char buf[BUFSIZ];
    311  1.33.2.1   thorpej 	char   *canon = NULL, *bp, *word = NULL;
    312       1.2   deraadt 	size_t  idlen = id == NULL ? 0 : strlen(id);
    313       1.2   deraadt 	int     contin = 0;
    314       1.2   deraadt 	int     found = 0;
    315       1.2   deraadt 
    316       1.2   deraadt 	if (f == NULL)
    317       1.2   deraadt 		return EINVAL;	/* ? */
    318       1.2   deraadt 
    319       1.2   deraadt 	while (fgets(buf, sizeof buf, f)) {
    320       1.2   deraadt 		int     wascontin = contin;
    321       1.2   deraadt 		contin = buf[strlen(buf) - 2] == '\\';
    322       1.2   deraadt 		bp = buf + strspn(buf, " \t\n");
    323       1.2   deraadt 
    324       1.2   deraadt 		switch (wascontin) {
    325       1.2   deraadt 		case -1:
    326       1.2   deraadt 			/* Continuation of uninteresting line */
    327       1.2   deraadt 			contin *= -1;
    328       1.2   deraadt 			continue;
    329       1.2   deraadt 		case 0:
    330       1.2   deraadt 			/* New line */
    331       1.2   deraadt 			contin *= -1;
    332       1.2   deraadt 			if (*bp == '#')
    333       1.2   deraadt 				continue;
    334       1.2   deraadt 			if ((word = strsep(&bp, " \t\n")) == NULL)
    335       1.2   deraadt 				continue;
    336       1.2   deraadt #ifdef YP
    337       1.2   deraadt 			/* A + in the file means try YP now */
    338       1.2   deraadt 			if (!strcmp(word, "+")) {
    339       1.2   deraadt 				char   *ypdom;
    340       1.2   deraadt 
    341       1.2   deraadt 				if (yp_get_default_domain(&ypdom) ||
    342       1.2   deraadt 				    yp_match(ypdom, "bootparams", client,
    343       1.2   deraadt 					strlen(client), &ypbuf, &ypbuflen))
    344       1.2   deraadt 					continue;
    345       1.2   deraadt 				bp = ypbuf;
    346       1.2   deraadt 				word = client;
    347       1.2   deraadt 				contin *= -1;
    348       1.2   deraadt 				break;
    349       1.2   deraadt 			}
    350       1.2   deraadt #endif
    351      1.21  christos 			if (debug)
    352      1.21  christos 				warnx("match %s with %s", word, client);
    353  1.33.2.1   thorpej 
    354  1.33.2.1   thorpej #define	HASGLOB(str) \
    355  1.33.2.1   thorpej 	(strchr(str, '*') != NULL || \
    356  1.33.2.1   thorpej 	 strchr(str, '?') != NULL || \
    357  1.33.2.1   thorpej 	 strchr(str, '[') != NULL || \
    358  1.33.2.1   thorpej 	 strchr(str, ']') != NULL)
    359  1.33.2.1   thorpej 
    360       1.2   deraadt 			/* See if this line's client is the one we are
    361       1.2   deraadt 			 * looking for */
    362  1.33.2.1   thorpej 			if (fnmatch(word, client, FNM_CASEFOLD) == 0) {
    363  1.33.2.1   thorpej 				/*
    364  1.33.2.1   thorpej 				 * Match.  The token may be globbed, we
    365  1.33.2.1   thorpej 				 * can't just return that as the canonical
    366  1.33.2.1   thorpej 				 * name.  Check to see if the token has any
    367  1.33.2.1   thorpej 				 * globbing characters in it (*, ?, [, ]).
    368  1.33.2.1   thorpej 				 * If so, just return the name we already
    369  1.33.2.1   thorpej 				 * have.  Otherwise, return the token.
    370  1.33.2.1   thorpej 				 */
    371  1.33.2.1   thorpej 				if (HASGLOB(word))
    372  1.33.2.1   thorpej 					canon = client;
    373  1.33.2.1   thorpej 				else
    374  1.33.2.1   thorpej 					canon = word;
    375  1.33.2.1   thorpej 			} else {
    376  1.33.2.3     enami 				struct hostent *hp;
    377       1.2   deraadt 				/*
    378       1.2   deraadt 				 * If it didn't match, try getting the
    379       1.2   deraadt 				 * canonical host name of the client
    380  1.33.2.3     enami 				 * on this line, if it's not a glob,
    381  1.33.2.3     enami 				 * and comparing it to the client we
    382  1.33.2.3     enami 				 * are looking up.
    383       1.2   deraadt 				 */
    384  1.33.2.3     enami 				if (HASGLOB(word)) {
    385  1.33.2.3     enami 					if (debug)
    386  1.33.2.3     enami 						warnx("Skipping non-match: %s",
    387  1.33.2.3     enami 						    word);
    388  1.33.2.3     enami 					continue;
    389  1.33.2.3     enami 				}
    390  1.33.2.3     enami 				if ((hp = gethostbyname(word)) == NULL) {
    391      1.19       abs 					if (debug)
    392      1.19       abs 						warnx(
    393  1.33.2.3     enami 					    "Unknown bootparams host %s",
    394  1.33.2.3     enami 					    word);
    395      1.19       abs 					if (dolog)
    396      1.19       abs 						syslog(LOG_NOTICE,
    397  1.33.2.3     enami 					    "Unknown bootparams host %s",
    398  1.33.2.3     enami 					    word);
    399      1.19       abs 					continue;
    400      1.19       abs 				}
    401  1.33.2.3     enami 				if (strcasecmp(hp->h_name, client) != 0)
    402  1.33.2.2     enami 					continue;
    403  1.33.2.3     enami 				canon = hp->h_name;
    404       1.1   deraadt 			}
    405  1.33.2.1   thorpej 
    406  1.33.2.1   thorpej #undef HASGLOB
    407  1.33.2.1   thorpej 
    408       1.2   deraadt 			contin *= -1;
    409       1.2   deraadt 			break;
    410       1.2   deraadt 		case 1:
    411       1.2   deraadt 			/* Continued line we want to parse below */
    412       1.1   deraadt 			break;
    413       1.1   deraadt 		}
    414       1.1   deraadt 
    415  1.33.2.1   thorpej 		assert(canon != NULL);
    416       1.2   deraadt 		if (client_canonical)
    417  1.33.2.1   thorpej 			strncpy(client_canonical, canon, MAX_MACHINE_NAME);
    418       1.1   deraadt 
    419       1.2   deraadt 		/* We have found a line for CLIENT */
    420       1.4     glass 		if (id == NULL) {
    421       1.4     glass 			(void) fclose(f);
    422       1.2   deraadt 			return 0;
    423       1.4     glass 		}
    424       1.2   deraadt 
    425       1.2   deraadt 		/* Look for a value for the parameter named by ID */
    426       1.2   deraadt 		while ((word = strsep(&bp, " \t\n")) != NULL) {
    427       1.2   deraadt 			if (!strncmp(word, id, idlen) && word[idlen] == '=') {
    428       1.2   deraadt 				/* We have found the entry we want */
    429       1.2   deraadt 				*server = &word[idlen + 1];
    430       1.2   deraadt 				*path = strchr(*server, ':');
    431       1.2   deraadt 				if (*path == NULL)
    432       1.2   deraadt 					/* Malformed entry */
    433       1.2   deraadt 					continue;
    434       1.2   deraadt 				*(*path)++ = '\0';
    435       1.2   deraadt 				(void) fclose(f);
    436       1.2   deraadt 				return 0;
    437       1.2   deraadt 			}
    438       1.1   deraadt 		}
    439       1.2   deraadt 
    440       1.2   deraadt 		found = 1;
    441       1.1   deraadt 	}
    442       1.1   deraadt 
    443       1.2   deraadt 	(void) fclose(f);
    444       1.2   deraadt 	return found ? ENOENT : EPERM;
    445      1.12   thorpej }
    446      1.12   thorpej 
    447      1.12   thorpej void
    448      1.12   thorpej usage()
    449      1.12   thorpej {
    450      1.12   thorpej 	fprintf(stderr,
    451      1.12   thorpej 	    "usage: %s [-d] [-s] [-r router] [-f bootparmsfile]\n", __progname);
    452      1.12   thorpej 	exit(1);
    453      1.22    itojun }
    454      1.22    itojun 
    455      1.22    itojun static int
    456      1.22    itojun get_localaddr(ifname, sin)
    457      1.22    itojun 	const char *ifname;
    458      1.22    itojun 	struct sockaddr_in *sin;
    459      1.22    itojun {
    460      1.22    itojun 	struct ifaddrs *ifap, *ifa;
    461      1.22    itojun 
    462      1.22    itojun 	if (getifaddrs(&ifap) != 0)
    463      1.22    itojun 		return -1;
    464      1.22    itojun 
    465      1.22    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    466      1.22    itojun 		if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
    467      1.22    itojun 			continue;
    468      1.22    itojun 		if (ifa->ifa_addr->sa_family != AF_INET)
    469      1.22    itojun 			continue;
    470      1.22    itojun 		if (ifa->ifa_addr->sa_len != sizeof(*sin))
    471      1.22    itojun 			continue;
    472      1.22    itojun 
    473      1.22    itojun 		/* no loopback please */
    474      1.22    itojun #ifdef IFF_LOOPBACK
    475      1.22    itojun 		if (ifa->ifa_flags & IFF_LOOPBACK)
    476      1.22    itojun 			continue;
    477      1.22    itojun #else
    478      1.23    itojun 		if (strncmp(ifa->ifa_name, "lo", 2) == 0 &&
    479      1.23    itojun 		    (isdigit(ifa->ifa_name[2]) || ifa->ifa_name[2] == '\0'))
    480      1.22    itojun 			continue;
    481      1.22    itojun #endif
    482      1.22    itojun 
    483      1.22    itojun 		/* XXX more sanity checks? */
    484      1.22    itojun 
    485      1.22    itojun 		/* candidate found */
    486      1.22    itojun 		memcpy(sin, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    487      1.22    itojun 		freeifaddrs(ifap);
    488      1.22    itojun 		return 0;
    489      1.22    itojun 	}
    490      1.22    itojun 
    491      1.22    itojun 	/* no candidate */
    492      1.22    itojun 	freeifaddrs(ifap);
    493      1.22    itojun 	return -1;
    494       1.1   deraadt }
    495