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