1 /* $NetBSD: getnetconfig.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Copyright (c) 1989 by Sun Microsystems, Inc.
41 #include "namespace.h"
42 #include "reentrant.h"
45 #include <netconfig.h>
51 #include "un-namespace.h"
55 * The five library routines in this file provide application access to the
56 * system network configuration database, /etc/netconfig. In addition to the
57 * netconfig database and the routines for accessing it, the environment
58 * variable NETPATH and its corresponding routines in getnetpath.c may also be
59 * used to specify the network transport to be used.
67 #define NC_NONETCONFIG ENOENT
68 #define NC_NOMEM ENOMEM
69 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
70 #define NC_BADFILE EBADF /* format for netconfig file is bad */
71 #define NC_NOTFOUND ENOPROTOOPT /* specified netid was not found */
74 * semantics as strings (should be in netconfig.h)
76 #define NC_TPI_CLTS_S "tpi_clts"
77 #define NC_TPI_COTS_S "tpi_cots"
78 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
79 #define NC_TPI_RAW_S "tpi_raw"
82 * flags as characters (also should be in netconfig.h)
84 #define NC_NOFLAG_C '-'
85 #define NC_VISIBLE_C 'v'
86 #define NC_BROADCAST_C 'b'
89 * Character used to indicate there is no name-to-address lookup library
91 #define NC_NOLOOKUP "-"
93 static const char * const _nc_errors[] = {
94 "Netconfig database not found",
97 "Netconfig database has invalid format",
98 "Netid not found in netconfig database"
101 struct netconfig_info {
102 int eof; /* all entries has been read */
103 int ref; /* # of times setnetconfig() has been called */
104 struct netconfig_list *head; /* head of the list */
105 struct netconfig_list *tail; /* last of the list */
108 struct netconfig_list {
109 char *linep; /* hold line read from netconfig */
110 struct netconfig *ncp;
111 struct netconfig_list *next;
114 struct netconfig_vars {
115 int valid; /* token that indicates a valid netconfig_vars */
116 int flag; /* first time flag */
117 struct netconfig_list *nc_configs; /* pointer to the current netconfig entry */
120 #define NC_VALID 0xfeed
121 #define NC_STORAGE 0xf00d
125 static int *__nc_error(void);
126 static int parse_ncp(char *, struct netconfig *);
127 static struct netconfig *dup_ncp(struct netconfig *);
130 static FILE *nc_file; /* for netconfig db */
131 static mutex_t nc_file_lock = MUTEX_INITIALIZER;
133 static struct netconfig_info ni = { 0, 0, NULL, NULL};
134 static mutex_t ni_lock = MUTEX_INITIALIZER;
136 static thread_key_t nc_key;
137 static once_t nc_once = ONCE_INITIALIZER;
138 static int nc_key_error;
144 nc_key_error = thr_keycreate(&nc_key, free);
147 #define MAXNETCONFIGLINE 1000
152 static int nc_error = 0;
156 * Use the static `nc_error' if we are the main thread
157 * (including non-threaded programs), or if an allocation
162 if (thr_once(&nc_once, nc_key_init) != 0 || nc_key_error != 0)
164 if ((nc_addr = (int *)thr_getspecific(nc_key)) == NULL) {
165 nc_addr = (int *)malloc(sizeof (int));
166 if (thr_setspecific(nc_key, (void *) nc_addr) != 0) {
176 #define nc_error (*(__nc_error()))
178 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
179 * "handle" is returned on a successful call. At the start of a session (after
180 * a call to setnetconfig()) searches through the /etc/netconfig database will
181 * proceed from the start of the file. The session handle must be passed to
182 * getnetconfig() to parse the file. Each call to getnetconfig() using the
183 * current handle will process one subsequent entry in /etc/netconfig.
184 * setnetconfig() must be called before the first call to getnetconfig().
185 * (Handles are used to allow for nested calls to setnetpath()).
187 * A new session is established with each call to setnetconfig(), with a new
188 * handle being returned on each call. Previously established sessions remain
189 * active until endnetconfig() is called with that session's handle as an
192 * setnetconfig() need *not* be called before a call to getnetconfigent().
193 * setnetconfig() returns a NULL pointer on failure (for example, if
194 * the netconfig database is not present).
199 struct netconfig_vars *nc_vars;
201 if ((nc_vars = (struct netconfig_vars *)malloc(sizeof
202 (struct netconfig_vars))) == NULL) {
207 * For multiple calls, i.e. nc_file is not NULL, we just return the
208 * handle without reopening the netconfig db.
210 mutex_lock(&ni_lock);
212 mutex_unlock(&ni_lock);
214 mutex_lock(&nc_file_lock);
215 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
216 nc_vars->valid = NC_VALID;
218 nc_vars->nc_configs = ni.head;
219 mutex_unlock(&nc_file_lock);
220 return ((void *)nc_vars);
222 mutex_unlock(&nc_file_lock);
224 mutex_lock(&ni_lock);
226 mutex_unlock(&ni_lock);
228 nc_error = NC_NONETCONFIG;
235 * When first called, getnetconfig() returns a pointer to the first entry in
236 * the netconfig database, formatted as a struct netconfig. On each subsequent
237 * call, getnetconfig() returns a pointer to the next entry in the database.
238 * getnetconfig() can thus be used to search the entire netconfig file.
239 * getnetconfig() returns NULL at end of file.
243 getnetconfig(void *handlep)
245 struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
246 char *stringp; /* tmp string pointer */
247 struct netconfig_list *list;
248 struct netconfig *np;
249 struct netconfig *result;
252 * Verify that handle is valid
254 mutex_lock(&nc_file_lock);
255 if (ncp == NULL || nc_file == NULL) {
256 nc_error = NC_NOTINIT;
257 mutex_unlock(&nc_file_lock);
260 mutex_unlock(&nc_file_lock);
262 switch (ncp->valid) {
265 * If entry has already been read into the list,
266 * we return the entry in the linked list.
267 * If this is the first time call, check if there are any entries in
268 * linked list. If no entries, we need to read the netconfig db.
269 * If we have been here and the next entry is there, we just return
272 if (ncp->flag == 0) { /* first time */
274 mutex_lock(&ni_lock);
275 ncp->nc_configs = ni.head;
276 mutex_unlock(&ni_lock);
277 if (ncp->nc_configs != NULL) /* entry already exist */
278 return(ncp->nc_configs->ncp);
280 else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
281 ncp->nc_configs = ncp->nc_configs->next;
282 return(ncp->nc_configs->ncp);
286 * If we cannot find the entry in the list and is end of file,
289 mutex_lock(&ni_lock);
291 mutex_unlock(&ni_lock);
294 mutex_unlock(&ni_lock);
298 nc_error = NC_NOTINIT;
302 stringp = (char *) malloc(MAXNETCONFIGLINE);
307 if (malloc_verify() == 0) {
308 fprintf(stderr, "memory heap corrupted in getnetconfig\n");
314 * Read a line from netconfig file.
316 mutex_lock(&nc_file_lock);
318 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
320 mutex_lock(&ni_lock);
322 mutex_unlock(&ni_lock);
323 mutex_unlock(&nc_file_lock);
326 } while (*stringp == '#');
327 mutex_unlock(&nc_file_lock);
329 list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
334 np = (struct netconfig *) malloc(sizeof (struct netconfig));
342 list->ncp->nc_lookups = NULL;
343 list->linep = stringp;
344 if (parse_ncp(stringp, list->ncp) == -1) {
352 * If this is the first entry that's been read, it is the head of
353 * the list. If not, put the entry at the end of the list.
354 * Reposition the current pointer of the handle to the last entry
357 mutex_lock(&ni_lock);
358 if (ni.head == NULL) { /* first entry */
359 ni.head = ni.tail = list;
362 ni.tail->next = list;
363 ni.tail = ni.tail->next;
365 ncp->nc_configs = ni.tail;
366 result = ni.tail->ncp;
367 mutex_unlock(&ni_lock);
373 * endnetconfig() may be called to "unbind" or "close" the netconfig database
374 * when processing is complete, releasing resources for reuse. endnetconfig()
375 * may not be called before setnetconfig(). endnetconfig() returns 0 on
376 * success and -1 on failure (for example, if setnetconfig() was not called
380 endnetconfig(void *handlep)
382 struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
384 struct netconfig_list *q, *p;
387 * Verify that handle is valid
389 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
390 nc_handlep->valid != NC_STORAGE)) {
391 nc_error = NC_NOTINIT;
396 * Return 0 if anyone still needs it.
398 nc_handlep->valid = NC_INVALID;
399 nc_handlep->flag = 0;
400 nc_handlep->nc_configs = NULL;
401 mutex_lock(&ni_lock);
403 mutex_unlock(&ni_lock);
409 * Noone needs these entries anymore, then frees them.
410 * Make sure all info in netconfig_info structure has been reinitialized.
416 mutex_unlock(&ni_lock);
420 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
428 mutex_lock(&nc_file_lock);
431 mutex_unlock(&nc_file_lock);
437 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
438 * corresponding to netid. It returns NULL if netid is invalid (that is, does
439 * not name an entry in the netconfig database). It returns NULL and sets
440 * errno in case of failure (for example, if the netconfig database cannot be
445 getnetconfigent(const char *netid)
447 FILE *file; /* NETCONFIG db's file pointer */
448 char *linep; /* holds current netconfig line */
449 char *stringp; /* temporary string pointer */
450 struct netconfig *ncp = NULL; /* returned value */
451 struct netconfig_list *list; /* pointer to cache list */
453 nc_error = NC_NOTFOUND; /* default error. */
454 if (netid == NULL || strlen(netid) == 0) {
459 * Look up table if the entries have already been read and parsed in
460 * getnetconfig(), then copy this entry into a buffer and return it.
461 * If we cannot find the entry in the current list and there are more
462 * entries in the netconfig db that has not been read, we then read the
463 * db and try find the match netid.
464 * If all the netconfig db has been read and placed into the list and
465 * there is no match for the netid, return NULL.
467 mutex_lock(&ni_lock);
468 if (ni.head != NULL) {
469 for (list = ni.head; list; list = list->next) {
470 if (strcmp(list->ncp->nc_netid, netid) == 0) {
471 mutex_unlock(&ni_lock);
472 return(dup_ncp(list->ncp));
475 if (ni.eof == 1) { /* that's all the entries */
476 mutex_unlock(&ni_lock);
480 mutex_unlock(&ni_lock);
483 if ((file = fopen(NETCONFIG, "r")) == NULL) {
484 nc_error = NC_NONETCONFIG;
488 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
495 char *tmpp; /* tmp string pointer */
498 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
501 } while (*stringp == '#');
502 if (stringp == NULL) { /* eof */
505 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */
506 nc_error = NC_BADFILE;
509 if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */
510 strncmp(stringp, netid, (size_t)len) == 0) {
511 if ((ncp = (struct netconfig *)
512 malloc(sizeof (struct netconfig))) == NULL) {
515 ncp->nc_lookups = NULL;
516 if (parse_ncp(linep, ncp) == -1) {
522 } while (stringp != NULL);
531 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
532 * netconfigp (previously returned by getnetconfigent()).
536 freenetconfigent(struct netconfig *netconfigp)
538 if (netconfigp != NULL) {
539 free(netconfigp->nc_netid); /* holds all netconfigp's strings */
540 if (netconfigp->nc_lookups != NULL)
541 free(netconfigp->nc_lookups);
548 * Parse line and stuff it in a struct netconfig
549 * Typical line might look like:
550 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
552 * We return -1 if any of the tokens don't parse, or malloc fails.
554 * Note that we modify stringp (putting NULLs after tokens) and
555 * we set the ncp's string field pointers to point to these tokens within
558 * stringp - string to parse
559 * ncp - where to put results
563 parse_ncp(char *stringp, struct netconfig *ncp)
565 char *tokenp; /* for processing tokens */
569 nc_error = NC_BADFILE; /* nearly anything that breaks is for this reason */
570 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
572 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
577 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
580 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
581 ncp->nc_semantics = NC_TPI_COTS_ORD;
582 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
583 ncp->nc_semantics = NC_TPI_COTS;
584 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
585 ncp->nc_semantics = NC_TPI_CLTS;
586 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
587 ncp->nc_semantics = NC_TPI_RAW;
592 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
595 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
601 ncp->nc_flag |= NC_VISIBLE;
604 ncp->nc_flag |= NC_BROADCAST;
610 /* protocol family */
611 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
615 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
619 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
622 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
625 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
626 ncp->nc_nlookups = 0;
627 ncp->nc_lookups = NULL;
629 char *cp; /* tmp string */
631 if (ncp->nc_lookups != NULL) /* from last visit */
632 free(ncp->nc_lookups);
633 ncp->nc_lookups = NULL;
634 ncp->nc_nlookups = 0;
635 while ((cp = tokenp) != NULL) {
636 if ((nc_lookups = realloc(ncp->nc_lookups,
637 (ncp->nc_nlookups + 1) * sizeof *ncp->nc_lookups)) == NULL) {
638 free(ncp->nc_lookups);
639 ncp->nc_lookups = NULL;
642 tokenp = _get_next_token(cp, ',');
643 ncp->nc_lookups = nc_lookups;
644 ncp->nc_lookups[ncp->nc_nlookups++] = cp;
652 * Returns a string describing the reason for failure.
661 message = _nc_errors[0];
664 message = _nc_errors[1];
667 message = _nc_errors[2];
670 message = _nc_errors[3];
673 message = _nc_errors[4];
676 message = "Unknown network selection error";
678 /* LINTED const castaway */
679 return ((char *)message);
683 * Prints a message onto standard error describing the reason for failure.
686 nc_perror(const char *s)
688 fprintf(stderr, "%s: %s\n", s, nc_sperror());
692 * Duplicates the matched netconfig buffer.
694 static struct netconfig *
695 dup_ncp(struct netconfig *ncp)
701 if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
703 if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
708 * First we dup all the data from matched netconfig buffer. Then we
709 * adjust some of the member pointer to a pre-allocated buffer where
710 * contains part of the data.
711 * To follow the convention used in parse_ncp(), we store all the
712 * necessary information in the pre-allocated buffer and let each
713 * of the netconfig char pointer member point to the right address
717 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
718 tmp = strchr(tmp, '\0') + 1;
719 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
720 tmp = strchr(tmp, '\0') + 1;
721 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
722 tmp = strchr(tmp, '\0') + 1;
723 p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
724 p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
725 if (p->nc_lookups == NULL) {
730 for (i=0; i < p->nc_nlookups; i++) {
731 tmp = strchr(tmp, '\0') + 1;
732 p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);