2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 * @(#)getnetconfig.c 1.12 91/12/19 SMI
31 * $NetBSD: getnetconfig.c,v 1.3 2000/07/06 03:10:34 christos Exp $
32 * $FreeBSD: src/lib/libc/rpc/getnetconfig.c,v 1.14 2007/09/20 22:35:24 matteo Exp $
36 * Copyright (c) 1989 by Sun Microsystems, Inc.
39 #include "namespace.h"
40 #include "reentrant.h"
43 #include <netconfig.h>
49 #include "un-namespace.h"
53 * The five library routines in this file provide application access to the
54 * system network configuration database, /etc/netconfig. In addition to the
55 * netconfig database and the routines for accessing it, the environment
56 * variable NETPATH and its corresponding routines in getnetpath.c may also be
57 * used to specify the network transport to be used.
65 #define NC_NONETCONFIG ENOENT
66 #define NC_NOMEM ENOMEM
67 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
68 #define NC_BADFILE EBADF /* format for netconfig file is bad */
69 #define NC_NOTFOUND ENOPROTOOPT /* specified netid was not found */
72 * semantics as strings (should be in netconfig.h)
74 #define NC_TPI_CLTS_S "tpi_clts"
75 #define NC_TPI_COTS_S "tpi_cots"
76 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
77 #define NC_TPI_RAW_S "tpi_raw"
80 * flags as characters (also should be in netconfig.h)
82 #define NC_NOFLAG_C '-'
83 #define NC_VISIBLE_C 'v'
84 #define NC_BROADCAST_C 'b'
87 * Character used to indicate there is no name-to-address lookup library
89 #define NC_NOLOOKUP "-"
91 static const char * const _nc_errors[] = {
92 "Netconfig database not found",
95 "Netconfig database has invalid format",
96 "Netid not found in netconfig database"
99 struct netconfig_info {
100 int eof; /* all entries has been read */
101 int ref; /* # of times setnetconfig() has been called */
102 struct netconfig_list *head; /* head of the list */
103 struct netconfig_list *tail; /* last of the list */
106 struct netconfig_list {
107 char *linep; /* hold line read from netconfig */
108 struct netconfig *ncp;
109 struct netconfig_list *next;
112 struct netconfig_vars {
113 int valid; /* token that indicates a valid netconfig_vars */
114 int flag; /* first time flag */
115 struct netconfig_list *nc_configs; /* pointer to the current netconfig entry */
118 #define NC_VALID 0xfeed
119 #define NC_STORAGE 0xf00d
123 static int *__nc_error(void);
124 static int parse_ncp(char *, struct netconfig *);
125 static struct netconfig *dup_ncp(struct netconfig *);
128 static FILE *nc_file; /* for netconfig db */
129 static struct netconfig_info ni = { 0, 0, NULL, NULL};
131 #define MAXNETCONFIGLINE 1000
136 static pthread_mutex_t nc_lock = PTHREAD_MUTEX_INITIALIZER;
137 static thread_key_t nc_key = 0;
138 static int nc_error = 0;
142 * Use the static `nc_error' if we are the main thread
143 * (including non-threaded programs), or if an allocation
150 mutex_lock(&nc_lock);
152 error = thr_keycreate(&nc_key, free);
153 mutex_unlock(&nc_lock);
157 if ((nc_addr = (int *)thr_getspecific(nc_key)) == NULL) {
158 nc_addr = (int *)malloc(sizeof (int));
159 if (thr_setspecific(nc_key, (void *) nc_addr) != 0) {
169 #define nc_error (*(__nc_error()))
171 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
172 * "handle" is returned on a successful call. At the start of a session (after
173 * a call to setnetconfig()) searches through the /etc/netconfig database will
174 * proceed from the start of the file. The session handle must be passed to
175 * getnetconfig() to parse the file. Each call to getnetconfig() using the
176 * current handle will process one subsequent entry in /etc/netconfig.
177 * setnetconfig() must be called before the first call to getnetconfig().
178 * (Handles are used to allow for nested calls to setnetpath()).
180 * A new session is established with each call to setnetconfig(), with a new
181 * handle being returned on each call. Previously established sessions remain
182 * active until endnetconfig() is called with that session's handle as an
185 * setnetconfig() need *not* be called before a call to getnetconfigent().
186 * setnetconfig() returns a NULL pointer on failure (for example, if
187 * the netconfig database is not present).
192 struct netconfig_vars *nc_vars;
194 if ((nc_vars = (struct netconfig_vars *)malloc(sizeof
195 (struct netconfig_vars))) == NULL) {
200 * For multiple calls, i.e. nc_file is not NULL, we just return the
201 * handle without reopening the netconfig db.
204 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
205 nc_vars->valid = NC_VALID;
207 nc_vars->nc_configs = ni.head;
208 return ((void *)nc_vars);
211 nc_error = NC_NONETCONFIG;
218 * When first called, getnetconfig() returns a pointer to the first entry in
219 * the netconfig database, formatted as a struct netconfig. On each subsequent
220 * call, getnetconfig() returns a pointer to the next entry in the database.
221 * getnetconfig() can thus be used to search the entire netconfig file.
222 * getnetconfig() returns NULL at end of file.
226 getnetconfig(void *handlep)
228 struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
229 char *stringp; /* tmp string pointer */
230 struct netconfig_list *list;
231 struct netconfig *np;
234 * Verify that handle is valid
236 if (ncp == NULL || nc_file == NULL) {
237 nc_error = NC_NOTINIT;
241 switch (ncp->valid) {
244 * If entry has already been read into the list,
245 * we return the entry in the linked list.
246 * If this is the first time call, check if there are any entries in
247 * linked list. If no entries, we need to read the netconfig db.
248 * If we have been here and the next entry is there, we just return
251 if (ncp->flag == 0) { /* first time */
253 ncp->nc_configs = ni.head;
254 if (ncp->nc_configs != NULL) /* entry already exist */
255 return(ncp->nc_configs->ncp);
257 else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
258 ncp->nc_configs = ncp->nc_configs->next;
259 return(ncp->nc_configs->ncp);
263 * If we cannot find the entry in the list and is end of file,
266 if (ni.eof == 1) return(NULL);
269 nc_error = NC_NOTINIT;
273 stringp = (char *) malloc(MAXNETCONFIGLINE);
278 if (malloc_verify() == 0) {
279 fprintf(stderr, "memory heap corrupted in getnetconfig\n");
285 * Read a line from netconfig file.
288 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
293 } while (*stringp == '#');
295 list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
300 np = (struct netconfig *) malloc(sizeof (struct netconfig));
308 list->ncp->nc_lookups = NULL;
309 list->linep = stringp;
310 if (parse_ncp(stringp, list->ncp) == -1) {
318 * If this is the first entry that's been read, it is the head of
319 * the list. If not, put the entry at the end of the list.
320 * Reposition the current pointer of the handle to the last entry
323 if (ni.head == NULL) { /* first entry */
324 ni.head = ni.tail = list;
327 ni.tail->next = list;
328 ni.tail = ni.tail->next;
330 ncp->nc_configs = ni.tail;
331 return(ni.tail->ncp);
336 * endnetconfig() may be called to "unbind" or "close" the netconfig database
337 * when processing is complete, releasing resources for reuse. endnetconfig()
338 * may not be called before setnetconfig(). endnetconfig() returns 0 on
339 * success and -1 on failure (for example, if setnetconfig() was not called
343 endnetconfig(void *handlep)
345 struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
347 struct netconfig_list *q, *p;
350 * Verify that handle is valid
352 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
353 nc_handlep->valid != NC_STORAGE)) {
354 nc_error = NC_NOTINIT;
359 * Return 0 if anyone still needs it.
361 nc_handlep->valid = NC_INVALID;
362 nc_handlep->flag = 0;
363 nc_handlep->nc_configs = NULL;
370 * Noone needs these entries anymore, then frees them.
371 * Make sure all info in netconfig_info structure has been reinitialized.
379 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
393 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
394 * corresponding to netid. It returns NULL if netid is invalid (that is, does
395 * not name an entry in the netconfig database). It returns NULL and sets
396 * errno in case of failure (for example, if the netconfig database cannot be
401 getnetconfigent(const char *netid)
403 FILE *file; /* NETCONFIG db's file pointer */
404 char *linep; /* holds current netconfig line */
405 char *stringp; /* temporary string pointer */
406 struct netconfig *ncp = NULL; /* returned value */
407 struct netconfig_list *list; /* pointer to cache list */
409 nc_error = NC_NOTFOUND; /* default error. */
410 if (netid == NULL || strlen(netid) == 0) {
414 if (strcmp(netid, "unix") == 0) {
415 fprintf(stderr, "The local transport is called \"unix\" ");
416 fprintf(stderr, "in /etc/netconfig.\n");
417 fprintf(stderr, "Please change this to \"local\" manually ");
418 fprintf(stderr, "or run mergemaster(8).\n");
419 fprintf(stderr, "See UPDATING entry 20021216 for details.\n");
420 fprintf(stderr, "Continuing in 10 seconds\n\n");
421 fprintf(stderr, "This warning will be removed 20030301\n");
427 * Look up table if the entries have already been read and parsed in
428 * getnetconfig(), then copy this entry into a buffer and return it.
429 * If we cannot find the entry in the current list and there are more
430 * entries in the netconfig db that has not been read, we then read the
431 * db and try find the match netid.
432 * If all the netconfig db has been read and placed into the list and
433 * there is no match for the netid, return NULL.
435 if (ni.head != NULL) {
436 for (list = ni.head; list; list = list->next) {
437 if (strcmp(list->ncp->nc_netid, netid) == 0) {
438 return(dup_ncp(list->ncp));
441 if (ni.eof == 1) /* that's all the entries */
446 if ((file = fopen(NETCONFIG, "r")) == NULL) {
447 nc_error = NC_NONETCONFIG;
451 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
458 char *tmpp; /* tmp string pointer */
461 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
464 } while (*stringp == '#');
465 if (stringp == NULL) { /* eof */
468 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */
469 nc_error = NC_BADFILE;
472 if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */
473 strncmp(stringp, netid, (size_t)len) == 0) {
474 if ((ncp = (struct netconfig *)
475 malloc(sizeof (struct netconfig))) == NULL) {
478 ncp->nc_lookups = NULL;
479 if (parse_ncp(linep, ncp) == -1) {
485 } while (stringp != NULL);
494 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
495 * netconfigp (previously returned by getnetconfigent()).
499 freenetconfigent(struct netconfig *netconfigp)
501 if (netconfigp != NULL) {
502 free(netconfigp->nc_netid); /* holds all netconfigp's strings */
503 if (netconfigp->nc_lookups != NULL)
504 free(netconfigp->nc_lookups);
511 * Parse line and stuff it in a struct netconfig
512 * Typical line might look like:
513 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
515 * We return -1 if any of the tokens don't parse, or malloc fails.
517 * Note that we modify stringp (putting NULLs after tokens) and
518 * we set the ncp's string field pointers to point to these tokens within
523 parse_ncp(char *stringp, /* string to parse */
524 struct netconfig *ncp) /* where to put results */
526 char *tokenp; /* for processing tokens */
530 nc_error = NC_BADFILE; /* nearly anything that breaks is for this reason */
531 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
533 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
538 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
541 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
542 ncp->nc_semantics = NC_TPI_COTS_ORD;
543 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
544 ncp->nc_semantics = NC_TPI_COTS;
545 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
546 ncp->nc_semantics = NC_TPI_CLTS;
547 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
548 ncp->nc_semantics = NC_TPI_RAW;
553 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
556 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
562 ncp->nc_flag |= NC_VISIBLE;
565 ncp->nc_flag |= NC_BROADCAST;
571 /* protocol family */
572 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
576 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
580 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
583 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
586 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
587 ncp->nc_nlookups = 0;
588 ncp->nc_lookups = NULL;
590 char *cp; /* tmp string */
592 if (ncp->nc_lookups != NULL) /* from last visit */
593 free(ncp->nc_lookups);
594 ncp->nc_lookups = NULL;
595 ncp->nc_nlookups = 0;
596 while ((cp = tokenp) != NULL) {
597 if ((nc_lookups = realloc(ncp->nc_lookups,
598 (ncp->nc_nlookups + 1) * sizeof *ncp->nc_lookups)) == NULL) {
599 free(ncp->nc_lookups);
600 ncp->nc_lookups = NULL;
603 tokenp = _get_next_token(cp, ',');
604 ncp->nc_lookups = nc_lookups;
605 ncp->nc_lookups[ncp->nc_nlookups++] = cp;
613 * Returns a string describing the reason for failure.
622 message = _nc_errors[0];
625 message = _nc_errors[1];
628 message = _nc_errors[2];
631 message = _nc_errors[3];
634 message = _nc_errors[4];
637 message = "Unknown network selection error";
639 /* LINTED const castaway */
640 return ((char *)message);
644 * Prints a message onto standard error describing the reason for failure.
647 nc_perror(const char *s)
649 fprintf(stderr, "%s: %s\n", s, nc_sperror());
653 * Duplicates the matched netconfig buffer.
655 static struct netconfig *
656 dup_ncp(struct netconfig *ncp)
662 if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
664 if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
669 * First we dup all the data from matched netconfig buffer. Then we
670 * adjust some of the member pointer to a pre-allocated buffer where
671 * contains part of the data.
672 * To follow the convention used in parse_ncp(), we store all the
673 * necessary information in the pre-allocated buffer and let each
674 * of the netconfig char pointer member point to the right address
678 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
679 tmp = strchr(tmp, '\0') + 1;
680 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
681 tmp = strchr(tmp, '\0') + 1;
682 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
683 tmp = strchr(tmp, '\0') + 1;
684 p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
685 p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
686 if (p->nc_lookups == NULL) {
691 for (i=0; i < p->nc_nlookups; i++) {
692 tmp = strchr(tmp, '\0') + 1;
693 p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);