1 /* $NetBSD: getnetconfig.c,v 1.25 2017/06/30 10:03:34 christos Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char sccsid[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
38 #else
39 __RCSID("$NetBSD: getnetconfig.c,v 1.25 2017/06/30 10:03:34 christos Exp $");
40 #endif
41 #endif
42
43 /*
44 * Copyright (c) 1989 by Sun Microsystems, Inc.
45 */
46
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <stdio.h>
50 #include <assert.h>
51 #include <errno.h>
52 #include <netconfig.h>
53 #include <stddef.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <rpc/rpc.h>
57 #include "rpc_internal.h"
58
59 #ifdef __weak_alias
60 __weak_alias(getnetconfig,_getnetconfig)
61 __weak_alias(setnetconfig,_setnetconfig)
62 __weak_alias(endnetconfig,_endnetconfig)
63 __weak_alias(getnetconfigent,_getnetconfigent)
64 __weak_alias(freenetconfigent,_freenetconfigent)
65 __weak_alias(nc_perror,_nc_perror)
66 __weak_alias(nc_sperror,_nc_sperror)
67 #endif
68
69 /*
70 * The five library routines in this file provide application access to the
71 * system network configuration database, /etc/netconfig. In addition to the
72 * netconfig database and the routines for accessing it, the environment
73 * variable NETPATH and its corresponding routines in getnetpath.c may also be
74 * used to specify the network transport to be used.
75 */
76
77
78 /*
79 * netconfig errors
80 */
81
82 #define NC_NONETCONFIG ENOENT
83 #define NC_NOMEM ENOMEM
84 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
85 #define NC_BADFILE EBADF /* format for netconfig file is bad */
86
87 /*
88 * semantics as strings (should be in netconfig.h)
89 */
90 #define NC_TPI_CLTS_S "tpi_clts"
91 #define NC_TPI_COTS_S "tpi_cots"
92 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
93 #define NC_TPI_RAW_S "tpi_raw"
94
95 /*
96 * flags as characters (also should be in netconfig.h)
97 */
98 #define NC_NOFLAG_C '-'
99 #define NC_VISIBLE_C 'v'
100 #define NC_BROADCAST_C 'b'
101
102 /*
103 * Character used to indicate there is no name-to-address lookup library
104 */
105 #define NC_NOLOOKUP "-"
106
107 static const char * const _nc_errors[] = {
108 "Netconfig database not found",
109 "Not enough memory",
110 "Not initialized",
111 "Netconfig database has invalid format"
112 };
113
114 struct netconfig_info {
115 int eof; /* all entries has been read */
116 int ref; /* # of times setnetconfig() has been called */
117 struct netconfig_list *head; /* head of the list */
118 struct netconfig_list *tail; /* last of the list */
119 };
120
121 struct netconfig_list {
122 char *linep; /* hold line read from netconfig */
123 struct netconfig *ncp;
124 struct netconfig_list *next;
125 };
126
127 struct netconfig_vars {
128 int valid; /* token that indicates valid netconfig_vars */
129 int flag; /* first time flag */
130 struct netconfig_list *nc_configs;
131 /* pointer to the current netconfig entry */
132 };
133
134 #define NC_VALID 0xfeed
135 #define NC_STORAGE 0xf00d
136 #define NC_INVALID 0
137
138
139 static int *__nc_error(void);
140 static int parse_ncp(char *, struct netconfig *);
141 static struct netconfig *dup_ncp(struct netconfig *);
142
143
144 static FILE *nc_file; /* for netconfig db */
145 static struct netconfig_info ni = { 0, 0, NULL, NULL};
146
147 #define MAXNETCONFIGLINE 1000
148
149 #ifdef _REENTRANT
150 static thread_key_t nc_key;
151 static once_t nc_once = ONCE_INITIALIZER;
152
153 static void
__nc_error_setup(void)154 __nc_error_setup(void)
155 {
156 thr_keycreate(&nc_key, free);
157 }
158 #endif
159
160 static int *
__nc_error(void)161 __nc_error(void)
162 {
163 #ifdef _REENTRANT
164 int *nc_addr = NULL;
165 #endif
166 static int nc_error = 0;
167
168 #ifdef _REENTRANT
169 if (__isthreaded == 0)
170 return &nc_error;
171 thr_once(&nc_once, __nc_error_setup);
172 nc_addr = thr_getspecific(nc_key) ;
173 if (nc_addr == NULL) {
174 nc_addr = malloc(sizeof (int));
175 if (nc_addr == NULL)
176 return &nc_error;
177 if (thr_setspecific(nc_key, nc_addr) != 0) {
178 if (nc_addr)
179 free(nc_addr);
180 return &nc_error;
181 }
182 *nc_addr = 0;
183 }
184 return nc_addr;
185 #else
186 return &nc_error;
187 #endif
188 }
189
190 #define nc_error (*(__nc_error()))
191 /*
192 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
193 * "handle" is returned on a successful call. At the start of a session (after
194 * a call to setnetconfig()) searches through the /etc/netconfig database will
195 * proceed from the start of the file. The session handle must be passed to
196 * getnetconfig() to parse the file. Each call to getnetconfig() using the
197 * current handle will process one subsequent entry in /etc/netconfig.
198 * setnetconfig() must be called before the first call to getnetconfig().
199 * (Handles are used to allow for nested calls to setnetpath()).
200 *
201 * A new session is established with each call to setnetconfig(), with a new
202 * handle being returned on each call. Previously established sessions remain
203 * active until endnetconfig() is called with that session's handle as an
204 * argument.
205 *
206 * setnetconfig() need *not* be called before a call to getnetconfigent().
207 * setnetconfig() returns a NULL pointer on failure (for example, if
208 * the netconfig database is not present).
209 */
210 void *
setnetconfig(void)211 setnetconfig(void)
212 {
213 struct netconfig_vars *nc_vars;
214
215 if ((nc_vars = malloc(sizeof(*nc_vars))) == NULL) {
216 return NULL;
217 }
218
219 /*
220 * For multiple calls, i.e. nc_file is not NULL, we just return the
221 * handle without reopening the netconfig db.
222 */
223 ni.ref++;
224 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "re")) != NULL) {
225 nc_vars->valid = NC_VALID;
226 nc_vars->flag = 0;
227 nc_vars->nc_configs = ni.head;
228 return nc_vars;
229 }
230 ni.ref--;
231 nc_error = NC_NONETCONFIG;
232 free(nc_vars);
233 return NULL;
234 }
235
236
237 /*
238 * When first called, getnetconfig() returns a pointer to the first entry in
239 * the netconfig database, formatted as a struct netconfig. On each subsequent
240 * call, getnetconfig() returns a pointer to the next entry in the database.
241 * getnetconfig() can thus be used to search the entire netconfig file.
242 * getnetconfig() returns NULL at end of file.
243 */
244
245 struct netconfig *
getnetconfig(void * handlep)246 getnetconfig(void *handlep)
247 {
248 struct netconfig_vars *ncp = handlep;
249 char *stringp; /* tmp string pointer */
250 struct netconfig_list *list;
251 struct netconfig *np;
252
253 /*
254 * Verify that handle is valid
255 */
256 if (ncp == NULL || nc_file == NULL) {
257 nc_error = NC_NOTINIT;
258 return NULL;
259 }
260
261 switch (ncp->valid) {
262 case NC_VALID:
263 /*
264 * If entry has already been read into the list,
265 * we return the entry in the linked list.
266 * If this is the first time call, check if there are any
267 * entries in linked list. If no entries, we need to read the
268 * netconfig db.
269 * If we have been here and the next entry is there, we just
270 * return it.
271 */
272 if (ncp->flag == 0) { /* first time */
273 ncp->flag = 1;
274 ncp->nc_configs = ni.head;
275 if (ncp->nc_configs != NULL) /* entry already exist */
276 return ncp->nc_configs->ncp;
277 }
278 else if (ncp->nc_configs != NULL &&
279 ncp->nc_configs->next != NULL) {
280 ncp->nc_configs = ncp->nc_configs->next;
281 return ncp->nc_configs->ncp;
282 }
283
284 /*
285 * If we cannot find the entry in the list and is end of file,
286 * we give up.
287 */
288 if (ni.eof == 1)
289 return NULL;
290 break;
291 default:
292 nc_error = NC_NOTINIT;
293 return NULL;
294 }
295
296 stringp = malloc(MAXNETCONFIGLINE);
297 if (stringp == NULL)
298 return NULL;
299
300 #ifdef MEM_CHK
301 if (malloc_verify() == 0) {
302 fprintf(stderr, "memory heap corrupted in getnetconfig\n");
303 exit(1);
304 }
305 #endif
306
307 /*
308 * Read a line from netconfig file.
309 */
310 do {
311 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
312 free(stringp);
313 ni.eof = 1;
314 return NULL;
315 }
316 } while (*stringp == '#');
317
318 list = malloc(sizeof(*list));
319 if (list == NULL) {
320 free(stringp);
321 return NULL;
322 }
323 np = malloc(sizeof(*np));
324 if (np == NULL) {
325 free(stringp);
326 free(list);
327 return NULL;
328 }
329 list->ncp = np;
330 list->next = NULL;
331 list->ncp->nc_lookups = NULL;
332 list->linep = stringp;
333 if (parse_ncp(stringp, list->ncp) == -1) {
334 free(stringp);
335 free(np);
336 free(list);
337 return NULL;
338 } else {
339 /*
340 * If this is the first entry that's been read, it is the
341 * head of the list. If not, put the entry at the end of
342 * the list. Reposition the current pointer of the handle to
343 * the last entry in the list.
344 */
345 if (ni.head == NULL) /* first entry */
346 ni.head = ni.tail = list;
347 else {
348 ni.tail->next = list;
349 ni.tail = ni.tail->next;
350 }
351 ncp->nc_configs = ni.tail;
352 return ni.tail->ncp;
353 }
354 }
355
356 /*
357 * endnetconfig() may be called to "unbind" or "close" the netconfig database
358 * when processing is complete, releasing resources for reuse. endnetconfig()
359 * may not be called before setnetconfig(). endnetconfig() returns 0 on
360 * success and -1 on failure (for example, if setnetconfig() was not called
361 * previously).
362 */
363 int
endnetconfig(void * handlep)364 endnetconfig(void *handlep)
365 {
366 struct netconfig_vars *nc_handlep = handlep;
367
368 struct netconfig_list *q, *p;
369
370 /*
371 * Verify that handle is valid
372 */
373 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
374 nc_handlep->valid != NC_STORAGE)) {
375 nc_error = NC_NOTINIT;
376 return -1;
377 }
378
379 /*
380 * Return 0 if anyone still needs it.
381 */
382 nc_handlep->valid = NC_INVALID;
383 nc_handlep->flag = 0;
384 nc_handlep->nc_configs = NULL;
385 if (--ni.ref > 0) {
386 free(nc_handlep);
387 return 0;
388 }
389
390 /*
391 * Noone needs these entries anymore, then frees them.
392 * Make sure all info in netconfig_info structure has been
393 * reinitialized.
394 */
395 q = p = ni.head;
396 ni.eof = ni.ref = 0;
397 ni.head = NULL;
398 ni.tail = NULL;
399 while (q) {
400 p = q->next;
401 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
402 free(q->ncp);
403 free(q->linep);
404 free(q);
405 q = p;
406 }
407 free(nc_handlep);
408
409 fclose(nc_file);
410 nc_file = NULL;
411 return 0;
412 }
413
414 /*
415 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
416 * corresponding to netid. It returns NULL if netid is invalid (that is, does
417 * not name an entry in the netconfig database). It returns NULL and sets
418 * errno in case of failure (for example, if the netconfig database cannot be
419 * opened).
420 */
421
422 struct netconfig *
getnetconfigent(const char * netid)423 getnetconfigent(const char *netid)
424 {
425 FILE *file; /* NETCONFIG db's file pointer */
426 char *linep; /* holds current netconfig line */
427 char *stringp; /* temporary string pointer */
428 struct netconfig *ncp = NULL; /* returned value */
429 struct netconfig_list *list; /* pointer to cache list */
430
431 if (netid == NULL || strlen(netid) == 0)
432 return NULL;
433
434 /*
435 * Look up table if the entries have already been read and parsed in
436 * getnetconfig(), then copy this entry into a buffer and return it.
437 * If we cannot find the entry in the current list and there are more
438 * entries in the netconfig db that has not been read, we then read the
439 * db and try find the match netid.
440 * If all the netconfig db has been read and placed into the list and
441 * there is no match for the netid, return NULL.
442 */
443 if (ni.head != NULL) {
444 for (list = ni.head; list; list = list->next) {
445 if (strcmp(list->ncp->nc_netid, netid) == 0)
446 return dup_ncp(list->ncp);
447 }
448 if (ni.eof == 1) /* that's all the entries */
449 return NULL;
450 }
451
452 if ((file = fopen(NETCONFIG, "re")) == NULL)
453 return NULL;
454
455 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
456 fclose(file);
457 return NULL;
458 }
459 do {
460 size_t len;
461 char *tmpp; /* tmp string pointer */
462
463 do {
464 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file))
465 == NULL)
466 break;
467 } while (*stringp == '#');
468 if (stringp == NULL) /* eof */
469 break;
470 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) {
471 /* can't parse file */
472 nc_error = NC_BADFILE;
473 break;
474 }
475 if (strlen(netid) == (len = (size_t)(tmpp - stringp)) &&
476 strncmp(stringp, netid, len) == 0) {
477 /* a match */
478 if ((ncp = malloc(sizeof(*ncp))) == NULL)
479 break;
480 ncp->nc_lookups = NULL;
481 if (parse_ncp(linep, ncp) == -1) {
482 free(ncp);
483 ncp = NULL;
484 }
485 break;
486 }
487 } while (stringp != NULL);
488 if (ncp == NULL)
489 free(linep);
490 fclose(file);
491 return ncp;
492 }
493
494 /*
495 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
496 * netconfigp (previously returned by getnetconfigent()).
497 */
498
499 void
freenetconfigent(struct netconfig * netconfigp)500 freenetconfigent(struct netconfig *netconfigp)
501 {
502 if (netconfigp != NULL) {
503 /* holds all netconfigp's strings */
504 free(netconfigp->nc_netid);
505 if (netconfigp->nc_lookups != NULL)
506 free(netconfigp->nc_lookups);
507 free(netconfigp);
508 }
509 }
510
511 /*
512 * Parse line and stuff it in a struct netconfig
513 * Typical line might look like:
514 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
515 *
516 * We return -1 if any of the tokens don't parse, or malloc fails.
517 *
518 * Note that we modify stringp (putting NULLs after tokens) and
519 * we set the ncp's string field pointers to point to these tokens within
520 * stringp.
521 */
522
523 static int
parse_ncp(char * stringp,struct netconfig * ncp)524 parse_ncp(
525 char *stringp, /* string to parse */
526 struct netconfig *ncp) /* where to put results */
527 {
528 char *tokenp; /* for processing tokens */
529 char *lasts;
530
531 _DIAGASSERT(stringp != NULL);
532 _DIAGASSERT(ncp != NULL);
533
534 nc_error = NC_BADFILE;
535 /* nearly anything that breaks is for this reason */
536 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
537 /* netid */
538 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL)
539 return -1;
540
541 /* semantics */
542 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
543 return -1;
544 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
545 ncp->nc_semantics = NC_TPI_COTS_ORD;
546 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
547 ncp->nc_semantics = NC_TPI_COTS;
548 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
549 ncp->nc_semantics = NC_TPI_CLTS;
550 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
551 ncp->nc_semantics = NC_TPI_RAW;
552 else
553 return -1;
554
555 /* flags */
556 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
557 return -1;
558 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0'; tokenp++) {
559 switch (*tokenp) {
560 case NC_NOFLAG_C:
561 break;
562 case NC_VISIBLE_C:
563 ncp->nc_flag |= NC_VISIBLE;
564 break;
565 case NC_BROADCAST_C:
566 ncp->nc_flag |= NC_BROADCAST;
567 break;
568 default:
569 return -1;
570 }
571 }
572 /* protocol family */
573 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL)
574 return -1;
575 /* protocol name */
576 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL)
577 return -1;
578 /* network device */
579 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL)
580 return -1;
581 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
582 return -1;
583 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
584 ncp->nc_nlookups = 0;
585 ncp->nc_lookups = NULL;
586 } else {
587 char *cp; /* tmp string */
588
589 if (ncp->nc_lookups != NULL) /* from last visit */
590 free(ncp->nc_lookups);
591 /* preallocate one string pointer */
592 ncp->nc_lookups = malloc(sizeof(*ncp->nc_lookups));
593 ncp->nc_nlookups = 0;
594 while ((cp = tokenp) != NULL) {
595 tokenp = _get_next_token(cp, ',');
596 ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
597 ncp->nc_lookups = (char **)
598 realloc(ncp->nc_lookups,
599 (size_t)(ncp->nc_nlookups+1) *sizeof(char *));
600 /* for next loop */
601 }
602 }
603 return 0;
604 }
605
606 /*
607 * Returns a string describing the reason for failure.
608 */
609 char *
nc_sperror(void)610 nc_sperror(void)
611 {
612 const char *message;
613
614 switch(nc_error) {
615 case NC_NONETCONFIG:
616 message = _nc_errors[0];
617 break;
618 case NC_NOMEM:
619 message = _nc_errors[1];
620 break;
621 case NC_NOTINIT:
622 message = _nc_errors[2];
623 break;
624 case NC_BADFILE:
625 message = _nc_errors[3];
626 break;
627 default:
628 message = "Unknown network selection error";
629 }
630 return __UNCONST(message);
631 }
632
633 /*
634 * Prints a message onto standard error describing the reason for failure.
635 */
636 void
nc_perror(const char * s)637 nc_perror(const char *s)
638 {
639
640 _DIAGASSERT(s != NULL);
641
642 fprintf(stderr, "%s: %s", s, nc_sperror());
643 }
644
645 /*
646 * Duplicates the matched netconfig buffer.
647 */
648 static struct netconfig *
dup_ncp(struct netconfig * ncp)649 dup_ncp(struct netconfig *ncp)
650 {
651 struct netconfig *p;
652 char *tmp;
653 u_int i;
654
655 _DIAGASSERT(ncp != NULL);
656
657 if ((tmp = malloc(MAXNETCONFIGLINE)) == NULL)
658 return NULL;
659 if ((p = malloc(sizeof(*p))) == NULL) {
660 free(tmp);
661 return NULL;
662 }
663 /*
664 * First we dup all the data from matched netconfig buffer. Then we
665 * adjust some of the member pointer to a pre-allocated buffer where
666 * contains part of the data.
667 * To follow the convention used in parse_ncp(), we store all the
668 * necessary information in the pre-allocated buffer and let each
669 * of the netconfig char pointer member point to the right address
670 * in the buffer.
671 */
672 *p = *ncp;
673 p->nc_netid = strcpy(tmp, ncp->nc_netid);
674 tmp = strchr(tmp, '\0') + 1;
675 p->nc_protofmly = strcpy(tmp, ncp->nc_protofmly);
676 tmp = strchr(tmp, '\0') + 1;
677 p->nc_proto = strcpy(tmp, ncp->nc_proto);
678 tmp = strchr(tmp, '\0') + 1;
679 p->nc_device = strcpy(tmp, ncp->nc_device);
680 p->nc_lookups = calloc((size_t)(p->nc_nlookups + 1), sizeof(char *));
681 if (p->nc_lookups == NULL) {
682 free(p->nc_netid);
683 free(p);
684 return NULL;
685 }
686 for (i = 0; i < p->nc_nlookups; i++) {
687 tmp = strchr(tmp, '\0') + 1;
688 p->nc_lookups[i] = strcpy(tmp, ncp->nc_lookups[i]);
689 }
690 return p;
691 }
692