1 /* $NetBSD: getnetconfig.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Copyright (c) 1989 by Sun Microsystems, Inc.
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/cdefs.h>
48 #include <netconfig.h>
54 #include "un-namespace.h"
58 * The five library routines in this file provide application access to the
59 * system network configuration database, /etc/netconfig. In addition to the
60 * netconfig database and the routines for accessing it, the environment
61 * variable NETPATH and its corresponding routines in getnetpath.c may also be
62 * used to specify the network transport to be used.
70 #define NC_NONETCONFIG ENOENT
71 #define NC_NOMEM ENOMEM
72 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
73 #define NC_BADFILE EBADF /* format for netconfig file is bad */
74 #define NC_NOTFOUND ENOPROTOOPT /* specified netid was not found */
77 * semantics as strings (should be in netconfig.h)
79 #define NC_TPI_CLTS_S "tpi_clts"
80 #define NC_TPI_COTS_S "tpi_cots"
81 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
82 #define NC_TPI_RAW_S "tpi_raw"
85 * flags as characters (also should be in netconfig.h)
87 #define NC_NOFLAG_C '-'
88 #define NC_VISIBLE_C 'v'
89 #define NC_BROADCAST_C 'b'
92 * Character used to indicate there is no name-to-address lookup library
94 #define NC_NOLOOKUP "-"
96 static const char * const _nc_errors[] = {
97 "Netconfig database not found",
100 "Netconfig database has invalid format",
101 "Netid not found in netconfig database"
104 struct netconfig_info {
105 int eof; /* all entries has been read */
106 int ref; /* # of times setnetconfig() has been called */
107 struct netconfig_list *head; /* head of the list */
108 struct netconfig_list *tail; /* last of the list */
111 struct netconfig_list {
112 char *linep; /* hold line read from netconfig */
113 struct netconfig *ncp;
114 struct netconfig_list *next;
117 struct netconfig_vars {
118 int valid; /* token that indicates a valid netconfig_vars */
119 int flag; /* first time flag */
120 struct netconfig_list *nc_configs; /* pointer to the current netconfig entry */
123 #define NC_VALID 0xfeed
124 #define NC_STORAGE 0xf00d
128 static int *__nc_error(void);
129 static int parse_ncp(char *, struct netconfig *);
130 static struct netconfig *dup_ncp(struct netconfig *);
133 static FILE *nc_file; /* for netconfig db */
134 static struct netconfig_info ni = { 0, 0, NULL, NULL};
136 #define MAXNETCONFIGLINE 1000
141 static pthread_mutex_t nc_lock = PTHREAD_MUTEX_INITIALIZER;
142 static thread_key_t nc_key = 0;
143 static int nc_error = 0;
147 * Use the static `nc_error' if we are the main thread
148 * (including non-threaded programs), or if an allocation
155 mutex_lock(&nc_lock);
157 error = thr_keycreate(&nc_key, free);
158 mutex_unlock(&nc_lock);
162 if ((nc_addr = (int *)thr_getspecific(nc_key)) == NULL) {
163 nc_addr = (int *)malloc(sizeof (int));
164 if (thr_setspecific(nc_key, (void *) nc_addr) != 0) {
174 #define nc_error (*(__nc_error()))
176 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
177 * "handle" is returned on a successful call. At the start of a session (after
178 * a call to setnetconfig()) searches through the /etc/netconfig database will
179 * proceed from the start of the file. The session handle must be passed to
180 * getnetconfig() to parse the file. Each call to getnetconfig() using the
181 * current handle will process one subsequent entry in /etc/netconfig.
182 * setnetconfig() must be called before the first call to getnetconfig().
183 * (Handles are used to allow for nested calls to setnetpath()).
185 * A new session is established with each call to setnetconfig(), with a new
186 * handle being returned on each call. Previously established sessions remain
187 * active until endnetconfig() is called with that session's handle as an
190 * setnetconfig() need *not* be called before a call to getnetconfigent().
191 * setnetconfig() returns a NULL pointer on failure (for example, if
192 * the netconfig database is not present).
197 struct netconfig_vars *nc_vars;
199 if ((nc_vars = (struct netconfig_vars *)malloc(sizeof
200 (struct netconfig_vars))) == NULL) {
205 * For multiple calls, i.e. nc_file is not NULL, we just return the
206 * handle without reopening the netconfig db.
209 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
210 nc_vars->valid = NC_VALID;
212 nc_vars->nc_configs = ni.head;
213 return ((void *)nc_vars);
216 nc_error = NC_NONETCONFIG;
223 * When first called, getnetconfig() returns a pointer to the first entry in
224 * the netconfig database, formatted as a struct netconfig. On each subsequent
225 * call, getnetconfig() returns a pointer to the next entry in the database.
226 * getnetconfig() can thus be used to search the entire netconfig file.
227 * getnetconfig() returns NULL at end of file.
231 getnetconfig(handlep)
234 struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
235 char *stringp; /* tmp string pointer */
236 struct netconfig_list *list;
237 struct netconfig *np;
240 * Verify that handle is valid
242 if (ncp == NULL || nc_file == NULL) {
243 nc_error = NC_NOTINIT;
247 switch (ncp->valid) {
250 * If entry has already been read into the list,
251 * we return the entry in the linked list.
252 * If this is the first time call, check if there are any entries in
253 * linked list. If no entries, we need to read the netconfig db.
254 * If we have been here and the next entry is there, we just return
257 if (ncp->flag == 0) { /* first time */
259 ncp->nc_configs = ni.head;
260 if (ncp->nc_configs != NULL) /* entry already exist */
261 return(ncp->nc_configs->ncp);
263 else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
264 ncp->nc_configs = ncp->nc_configs->next;
265 return(ncp->nc_configs->ncp);
269 * If we cannot find the entry in the list and is end of file,
272 if (ni.eof == 1) return(NULL);
275 nc_error = NC_NOTINIT;
279 stringp = (char *) malloc(MAXNETCONFIGLINE);
284 if (malloc_verify() == 0) {
285 fprintf(stderr, "memory heap corrupted in getnetconfig\n");
291 * Read a line from netconfig file.
294 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
299 } while (*stringp == '#');
301 list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
306 np = (struct netconfig *) malloc(sizeof (struct netconfig));
314 list->ncp->nc_lookups = NULL;
315 list->linep = stringp;
316 if (parse_ncp(stringp, list->ncp) == -1) {
324 * If this is the first entry that's been read, it is the head of
325 * the list. If not, put the entry at the end of the list.
326 * Reposition the current pointer of the handle to the last entry
329 if (ni.head == NULL) { /* first entry */
330 ni.head = ni.tail = list;
333 ni.tail->next = list;
334 ni.tail = ni.tail->next;
336 ncp->nc_configs = ni.tail;
337 return(ni.tail->ncp);
342 * endnetconfig() may be called to "unbind" or "close" the netconfig database
343 * when processing is complete, releasing resources for reuse. endnetconfig()
344 * may not be called before setnetconfig(). endnetconfig() returns 0 on
345 * success and -1 on failure (for example, if setnetconfig() was not called
349 endnetconfig(handlep)
352 struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
354 struct netconfig_list *q, *p;
357 * Verify that handle is valid
359 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
360 nc_handlep->valid != NC_STORAGE)) {
361 nc_error = NC_NOTINIT;
366 * Return 0 if anyone still needs it.
368 nc_handlep->valid = NC_INVALID;
369 nc_handlep->flag = 0;
370 nc_handlep->nc_configs = NULL;
377 * Noone needs these entries anymore, then frees them.
378 * Make sure all info in netconfig_info structure has been reinitialized.
386 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
400 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
401 * corresponding to netid. It returns NULL if netid is invalid (that is, does
402 * not name an entry in the netconfig database). It returns NULL and sets
403 * errno in case of failure (for example, if the netconfig database cannot be
408 getnetconfigent(netid)
411 FILE *file; /* NETCONFIG db's file pointer */
412 char *linep; /* holds current netconfig line */
413 char *stringp; /* temporary string pointer */
414 struct netconfig *ncp = NULL; /* returned value */
415 struct netconfig_list *list; /* pointer to cache list */
417 nc_error = NC_NOTFOUND; /* default error. */
418 if (netid == NULL || strlen(netid) == 0) {
422 if (strcmp(netid, "unix") == 0) {
423 fprintf(stderr, "The local transport is called \"unix\" ");
424 fprintf(stderr, "in /etc/netconfig.\n");
425 fprintf(stderr, "Please change this to \"local\" manually ");
426 fprintf(stderr, "or run mergemaster(8).\n");
427 fprintf(stderr, "See UPDATING entry 20021216 for details.\n");
428 fprintf(stderr, "Continuing in 10 seconds\n\n");
429 fprintf(stderr, "This warning will be removed 20030301\n");
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.
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));
449 if (ni.eof == 1) /* that's all the entries */
454 if ((file = fopen(NETCONFIG, "r")) == NULL) {
455 nc_error = NC_NONETCONFIG;
459 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
466 char *tmpp; /* tmp string pointer */
469 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
472 } while (*stringp == '#');
473 if (stringp == NULL) { /* eof */
476 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */
477 nc_error = NC_BADFILE;
480 if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */
481 strncmp(stringp, netid, (size_t)len) == 0) {
482 if ((ncp = (struct netconfig *)
483 malloc(sizeof (struct netconfig))) == NULL) {
486 ncp->nc_lookups = NULL;
487 if (parse_ncp(linep, ncp) == -1) {
493 } while (stringp != NULL);
502 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
503 * netconfigp (previously returned by getnetconfigent()).
507 freenetconfigent(netconfigp)
508 struct netconfig *netconfigp;
510 if (netconfigp != NULL) {
511 free(netconfigp->nc_netid); /* holds all netconfigp's strings */
512 if (netconfigp->nc_lookups != NULL)
513 free(netconfigp->nc_lookups);
520 * Parse line and stuff it in a struct netconfig
521 * Typical line might look like:
522 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
524 * We return -1 if any of the tokens don't parse, or malloc fails.
526 * Note that we modify stringp (putting NULLs after tokens) and
527 * we set the ncp's string field pointers to point to these tokens within
532 parse_ncp(stringp, ncp)
533 char *stringp; /* string to parse */
534 struct netconfig *ncp; /* where to put results */
536 char *tokenp; /* for processing tokens */
539 nc_error = NC_BADFILE; /* nearly anything that breaks is for this reason */
540 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
542 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
547 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
550 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
551 ncp->nc_semantics = NC_TPI_COTS_ORD;
552 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
553 ncp->nc_semantics = NC_TPI_COTS;
554 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
555 ncp->nc_semantics = NC_TPI_CLTS;
556 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
557 ncp->nc_semantics = NC_TPI_RAW;
562 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
565 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
571 ncp->nc_flag |= NC_VISIBLE;
574 ncp->nc_flag |= NC_BROADCAST;
580 /* protocol family */
581 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
585 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
589 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
592 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
595 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
596 ncp->nc_nlookups = 0;
597 ncp->nc_lookups = NULL;
599 char *cp; /* tmp string */
601 if (ncp->nc_lookups != NULL) /* from last visit */
602 free(ncp->nc_lookups);
603 /* preallocate one string pointer */
604 ncp->nc_lookups = (char **)malloc(sizeof (char *));
605 ncp->nc_nlookups = 0;
606 while ((cp = tokenp) != NULL) {
607 tokenp = _get_next_token(cp, ',');
608 ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
609 ncp->nc_lookups = (char **)realloc(ncp->nc_lookups,
610 (size_t)(ncp->nc_nlookups+1) *sizeof(char *)); /* for next loop */
618 * Returns a string describing the reason for failure.
627 message = _nc_errors[0];
630 message = _nc_errors[1];
633 message = _nc_errors[2];
636 message = _nc_errors[3];
639 message = _nc_errors[4];
642 message = "Unknown network selection error";
644 /* LINTED const castaway */
645 return ((char *)message);
649 * Prints a message onto standard error describing the reason for failure.
655 fprintf(stderr, "%s: %s\n", s, nc_sperror());
659 * Duplicates the matched netconfig buffer.
661 static struct netconfig *
663 struct netconfig *ncp;
669 if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
671 if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
676 * First we dup all the data from matched netconfig buffer. Then we
677 * adjust some of the member pointer to a pre-allocated buffer where
678 * contains part of the data.
679 * To follow the convention used in parse_ncp(), we store all the
680 * necessary information in the pre-allocated buffer and let each
681 * of the netconfig char pointer member point to the right address
685 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
686 tmp = strchr(tmp, '\0') + 1;
687 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
688 tmp = strchr(tmp, '\0') + 1;
689 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
690 tmp = strchr(tmp, '\0') + 1;
691 p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
692 p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
693 if (p->nc_lookups == NULL) {
697 for (i=0; i < p->nc_nlookups; i++) {
698 tmp = strchr(tmp, '\0') + 1;
699 p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);