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) {
415 * Look up table if the entries have already been read and parsed in
416 * getnetconfig(), then copy this entry into a buffer and return it.
417 * If we cannot find the entry in the current list and there are more
418 * entries in the netconfig db that has not been read, we then read the
419 * db and try find the match netid.
420 * If all the netconfig db has been read and placed into the list and
421 * there is no match for the netid, return NULL.
423 if (ni.head != NULL) {
424 for (list = ni.head; list; list = list->next) {
425 if (strcmp(list->ncp->nc_netid, netid) == 0) {
426 return(dup_ncp(list->ncp));
429 if (ni.eof == 1) /* that's all the entries */
434 if ((file = fopen(NETCONFIG, "r")) == NULL) {
435 nc_error = NC_NONETCONFIG;
439 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
446 char *tmpp; /* tmp string pointer */
449 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
452 } while (*stringp == '#');
453 if (stringp == NULL) { /* eof */
456 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */
457 nc_error = NC_BADFILE;
460 if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */
461 strncmp(stringp, netid, (size_t)len) == 0) {
462 if ((ncp = (struct netconfig *)
463 malloc(sizeof (struct netconfig))) == NULL) {
466 ncp->nc_lookups = NULL;
467 if (parse_ncp(linep, ncp) == -1) {
473 } while (stringp != NULL);
482 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
483 * netconfigp (previously returned by getnetconfigent()).
487 freenetconfigent(struct netconfig *netconfigp)
489 if (netconfigp != NULL) {
490 free(netconfigp->nc_netid); /* holds all netconfigp's strings */
491 if (netconfigp->nc_lookups != NULL)
492 free(netconfigp->nc_lookups);
499 * Parse line and stuff it in a struct netconfig
500 * Typical line might look like:
501 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
503 * We return -1 if any of the tokens don't parse, or malloc fails.
505 * Note that we modify stringp (putting NULLs after tokens) and
506 * we set the ncp's string field pointers to point to these tokens within
511 parse_ncp(char *stringp, /* string to parse */
512 struct netconfig *ncp) /* where to put results */
514 char *tokenp; /* for processing tokens */
518 nc_error = NC_BADFILE; /* nearly anything that breaks is for this reason */
519 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
521 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
526 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
529 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
530 ncp->nc_semantics = NC_TPI_COTS_ORD;
531 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
532 ncp->nc_semantics = NC_TPI_COTS;
533 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
534 ncp->nc_semantics = NC_TPI_CLTS;
535 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
536 ncp->nc_semantics = NC_TPI_RAW;
541 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
544 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
550 ncp->nc_flag |= NC_VISIBLE;
553 ncp->nc_flag |= NC_BROADCAST;
559 /* protocol family */
560 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
564 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
568 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
571 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
574 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
575 ncp->nc_nlookups = 0;
576 ncp->nc_lookups = NULL;
578 char *cp; /* tmp string */
580 if (ncp->nc_lookups != NULL) /* from last visit */
581 free(ncp->nc_lookups);
582 ncp->nc_lookups = NULL;
583 ncp->nc_nlookups = 0;
584 while ((cp = tokenp) != NULL) {
585 if ((nc_lookups = realloc(ncp->nc_lookups,
586 (ncp->nc_nlookups + 1) * sizeof *ncp->nc_lookups)) == NULL) {
587 free(ncp->nc_lookups);
588 ncp->nc_lookups = NULL;
591 tokenp = _get_next_token(cp, ',');
592 ncp->nc_lookups = nc_lookups;
593 ncp->nc_lookups[ncp->nc_nlookups++] = cp;
601 * Returns a string describing the reason for failure.
610 message = _nc_errors[0];
613 message = _nc_errors[1];
616 message = _nc_errors[2];
619 message = _nc_errors[3];
622 message = _nc_errors[4];
625 message = "Unknown network selection error";
627 /* LINTED const castaway */
628 return ((char *)message);
632 * Prints a message onto standard error describing the reason for failure.
635 nc_perror(const char *s)
637 fprintf(stderr, "%s: %s\n", s, nc_sperror());
641 * Duplicates the matched netconfig buffer.
643 static struct netconfig *
644 dup_ncp(struct netconfig *ncp)
650 if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
652 if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
657 * First we dup all the data from matched netconfig buffer. Then we
658 * adjust some of the member pointer to a pre-allocated buffer where
659 * contains part of the data.
660 * To follow the convention used in parse_ncp(), we store all the
661 * necessary information in the pre-allocated buffer and let each
662 * of the netconfig char pointer member point to the right address
666 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
667 tmp = strchr(tmp, '\0') + 1;
668 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
669 tmp = strchr(tmp, '\0') + 1;
670 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
671 tmp = strchr(tmp, '\0') + 1;
672 p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
673 p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
674 if (p->nc_lookups == NULL) {
679 for (i=0; i < p->nc_nlookups; i++) {
680 tmp = strchr(tmp, '\0') + 1;
681 p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);