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 $
37 * Copyright (c) 1989 by Sun Microsystems, Inc.
40 #include "namespace.h"
41 #include "reentrant.h"
44 #include <netconfig.h>
50 #include "un-namespace.h"
54 * The five library routines in this file provide application access to the
55 * system network configuration database, /etc/netconfig. In addition to the
56 * netconfig database and the routines for accessing it, the environment
57 * variable NETPATH and its corresponding routines in getnetpath.c may also be
58 * used to specify the network transport to be used.
66 #define NC_NONETCONFIG ENOENT
67 #define NC_NOMEM ENOMEM
68 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
69 #define NC_BADFILE EBADF /* format for netconfig file is bad */
70 #define NC_NOTFOUND ENOPROTOOPT /* specified netid was not found */
73 * semantics as strings (should be in netconfig.h)
75 #define NC_TPI_CLTS_S "tpi_clts"
76 #define NC_TPI_COTS_S "tpi_cots"
77 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
78 #define NC_TPI_RAW_S "tpi_raw"
81 * flags as characters (also should be in netconfig.h)
83 #define NC_NOFLAG_C '-'
84 #define NC_VISIBLE_C 'v'
85 #define NC_BROADCAST_C 'b'
88 * Character used to indicate there is no name-to-address lookup library
90 #define NC_NOLOOKUP "-"
92 static const char * const _nc_errors
[] = {
93 "Netconfig database not found",
96 "Netconfig database has invalid format",
97 "Netid not found in netconfig database"
100 struct netconfig_info
{
101 int eof
; /* all entries has been read */
102 int ref
; /* # of times setnetconfig() has been called */
103 struct netconfig_list
*head
; /* head of the list */
104 struct netconfig_list
*tail
; /* last of the list */
107 struct netconfig_list
{
108 char *linep
; /* hold line read from netconfig */
109 struct netconfig
*ncp
;
110 struct netconfig_list
*next
;
113 struct netconfig_vars
{
114 int valid
; /* token that indicates a valid netconfig_vars */
115 int flag
; /* first time flag */
116 struct netconfig_list
*nc_configs
; /* pointer to the current netconfig entry */
119 #define NC_VALID 0xfeed
120 #define NC_STORAGE 0xf00d
124 static int *__nc_error(void);
125 static int parse_ncp(char *, struct netconfig
*);
126 static struct netconfig
*dup_ncp(struct netconfig
*);
129 static FILE *nc_file
; /* for netconfig db */
130 static struct netconfig_info ni
= { 0, 0, NULL
, NULL
};
132 #define MAXNETCONFIGLINE 1000
137 static pthread_mutex_t nc_lock
= PTHREAD_MUTEX_INITIALIZER
;
138 static thread_key_t nc_key
= 0;
139 static int nc_error
= 0;
143 * Use the static `nc_error' if we are the main thread
144 * (including non-threaded programs), or if an allocation
151 mutex_lock(&nc_lock
);
153 error
= thr_keycreate(&nc_key
, free
);
154 mutex_unlock(&nc_lock
);
158 if ((nc_addr
= (int *)thr_getspecific(nc_key
)) == NULL
) {
159 nc_addr
= (int *)malloc(sizeof (int));
160 if (thr_setspecific(nc_key
, (void *) nc_addr
) != 0) {
170 #define nc_error (*(__nc_error()))
172 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
173 * "handle" is returned on a successful call. At the start of a session (after
174 * a call to setnetconfig()) searches through the /etc/netconfig database will
175 * proceed from the start of the file. The session handle must be passed to
176 * getnetconfig() to parse the file. Each call to getnetconfig() using the
177 * current handle will process one subsequent entry in /etc/netconfig.
178 * setnetconfig() must be called before the first call to getnetconfig().
179 * (Handles are used to allow for nested calls to setnetpath()).
181 * A new session is established with each call to setnetconfig(), with a new
182 * handle being returned on each call. Previously established sessions remain
183 * active until endnetconfig() is called with that session's handle as an
186 * setnetconfig() need *not* be called before a call to getnetconfigent().
187 * setnetconfig() returns a NULL pointer on failure (for example, if
188 * the netconfig database is not present).
193 struct netconfig_vars
*nc_vars
;
195 if ((nc_vars
= (struct netconfig_vars
*)malloc(sizeof
196 (struct netconfig_vars
))) == NULL
) {
201 * For multiple calls, i.e. nc_file is not NULL, we just return the
202 * handle without reopening the netconfig db.
205 if ((nc_file
!= NULL
) || (nc_file
= fopen(NETCONFIG
, "r")) != NULL
) {
206 nc_vars
->valid
= NC_VALID
;
208 nc_vars
->nc_configs
= ni
.head
;
209 return ((void *)nc_vars
);
212 nc_error
= NC_NONETCONFIG
;
219 * When first called, getnetconfig() returns a pointer to the first entry in
220 * the netconfig database, formatted as a struct netconfig. On each subsequent
221 * call, getnetconfig() returns a pointer to the next entry in the database.
222 * getnetconfig() can thus be used to search the entire netconfig file.
223 * getnetconfig() returns NULL at end of file.
227 getnetconfig(void *handlep
)
229 struct netconfig_vars
*ncp
= (struct netconfig_vars
*)handlep
;
230 char *stringp
; /* tmp string pointer */
231 struct netconfig_list
*list
;
232 struct netconfig
*np
;
235 * Verify that handle is valid
237 if (ncp
== NULL
|| nc_file
== NULL
) {
238 nc_error
= NC_NOTINIT
;
242 switch (ncp
->valid
) {
245 * If entry has already been read into the list,
246 * we return the entry in the linked list.
247 * If this is the first time call, check if there are any entries in
248 * linked list. If no entries, we need to read the netconfig db.
249 * If we have been here and the next entry is there, we just return
252 if (ncp
->flag
== 0) { /* first time */
254 ncp
->nc_configs
= ni
.head
;
255 if (ncp
->nc_configs
!= NULL
) /* entry already exist */
256 return(ncp
->nc_configs
->ncp
);
258 else if (ncp
->nc_configs
!= NULL
&& ncp
->nc_configs
->next
!= NULL
) {
259 ncp
->nc_configs
= ncp
->nc_configs
->next
;
260 return(ncp
->nc_configs
->ncp
);
264 * If we cannot find the entry in the list and is end of file,
267 if (ni
.eof
== 1) return(NULL
);
270 nc_error
= NC_NOTINIT
;
274 stringp
= (char *) malloc(MAXNETCONFIGLINE
);
279 if (malloc_verify() == 0) {
280 fprintf(stderr
, "memory heap corrupted in getnetconfig\n");
286 * Read a line from netconfig file.
289 if (fgets(stringp
, MAXNETCONFIGLINE
, nc_file
) == NULL
) {
294 } while (*stringp
== '#');
296 list
= (struct netconfig_list
*) malloc(sizeof (struct netconfig_list
));
301 np
= (struct netconfig
*) malloc(sizeof (struct netconfig
));
309 list
->ncp
->nc_lookups
= NULL
;
310 list
->linep
= stringp
;
311 if (parse_ncp(stringp
, list
->ncp
) == -1) {
319 * If this is the first entry that's been read, it is the head of
320 * the list. If not, put the entry at the end of the list.
321 * Reposition the current pointer of the handle to the last entry
324 if (ni
.head
== NULL
) { /* first entry */
325 ni
.head
= ni
.tail
= list
;
328 ni
.tail
->next
= list
;
329 ni
.tail
= ni
.tail
->next
;
331 ncp
->nc_configs
= ni
.tail
;
332 return(ni
.tail
->ncp
);
337 * endnetconfig() may be called to "unbind" or "close" the netconfig database
338 * when processing is complete, releasing resources for reuse. endnetconfig()
339 * may not be called before setnetconfig(). endnetconfig() returns 0 on
340 * success and -1 on failure (for example, if setnetconfig() was not called
344 endnetconfig(void *handlep
)
346 struct netconfig_vars
*nc_handlep
= (struct netconfig_vars
*)handlep
;
348 struct netconfig_list
*q
, *p
;
351 * Verify that handle is valid
353 if (nc_handlep
== NULL
|| (nc_handlep
->valid
!= NC_VALID
&&
354 nc_handlep
->valid
!= NC_STORAGE
)) {
355 nc_error
= NC_NOTINIT
;
360 * Return 0 if anyone still needs it.
362 nc_handlep
->valid
= NC_INVALID
;
363 nc_handlep
->flag
= 0;
364 nc_handlep
->nc_configs
= NULL
;
371 * Noone needs these entries anymore, then frees them.
372 * Make sure all info in netconfig_info structure has been reinitialized.
380 if (q
->ncp
->nc_lookups
!= NULL
) free(q
->ncp
->nc_lookups
);
394 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
395 * corresponding to netid. It returns NULL if netid is invalid (that is, does
396 * not name an entry in the netconfig database). It returns NULL and sets
397 * errno in case of failure (for example, if the netconfig database cannot be
402 getnetconfigent(const char *netid
)
404 FILE *file
; /* NETCONFIG db's file pointer */
405 char *linep
; /* holds current netconfig line */
406 char *stringp
; /* temporary string pointer */
407 struct netconfig
*ncp
= NULL
; /* returned value */
408 struct netconfig_list
*list
; /* pointer to cache list */
410 nc_error
= NC_NOTFOUND
; /* default error. */
411 if (netid
== NULL
|| strlen(netid
) == 0) {
415 if (strcmp(netid
, "unix") == 0) {
416 fprintf(stderr
, "The local transport is called \"unix\" ");
417 fprintf(stderr
, "in /etc/netconfig.\n");
418 fprintf(stderr
, "Please change this to \"local\" manually ");
419 fprintf(stderr
, "or run mergemaster(8).\n");
420 fprintf(stderr
, "See UPDATING entry 20021216 for details.\n");
421 fprintf(stderr
, "Continuing in 10 seconds\n\n");
422 fprintf(stderr
, "This warning will be removed 20030301\n");
428 * Look up table if the entries have already been read and parsed in
429 * getnetconfig(), then copy this entry into a buffer and return it.
430 * If we cannot find the entry in the current list and there are more
431 * entries in the netconfig db that has not been read, we then read the
432 * db and try find the match netid.
433 * If all the netconfig db has been read and placed into the list and
434 * there is no match for the netid, return NULL.
436 if (ni
.head
!= NULL
) {
437 for (list
= ni
.head
; list
; list
= list
->next
) {
438 if (strcmp(list
->ncp
->nc_netid
, netid
) == 0) {
439 return(dup_ncp(list
->ncp
));
442 if (ni
.eof
== 1) /* that's all the entries */
447 if ((file
= fopen(NETCONFIG
, "r")) == NULL
) {
448 nc_error
= NC_NONETCONFIG
;
452 if ((linep
= malloc(MAXNETCONFIGLINE
)) == NULL
) {
459 char *tmpp
; /* tmp string pointer */
462 if ((stringp
= fgets(linep
, MAXNETCONFIGLINE
, file
)) == NULL
) {
465 } while (*stringp
== '#');
466 if (stringp
== NULL
) { /* eof */
469 if ((tmpp
= strpbrk(stringp
, "\t ")) == NULL
) { /* can't parse file */
470 nc_error
= NC_BADFILE
;
473 if (strlen(netid
) == (size_t) (len
= tmpp
- stringp
) && /* a match */
474 strncmp(stringp
, netid
, (size_t)len
) == 0) {
475 if ((ncp
= (struct netconfig
*)
476 malloc(sizeof (struct netconfig
))) == NULL
) {
479 ncp
->nc_lookups
= NULL
;
480 if (parse_ncp(linep
, ncp
) == -1) {
486 } while (stringp
!= NULL
);
495 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
496 * netconfigp (previously returned by getnetconfigent()).
500 freenetconfigent(struct netconfig
*netconfigp
)
502 if (netconfigp
!= NULL
) {
503 free(netconfigp
->nc_netid
); /* holds all netconfigp's strings */
504 if (netconfigp
->nc_lookups
!= NULL
)
505 free(netconfigp
->nc_lookups
);
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
516 * We return -1 if any of the tokens don't parse, or malloc fails.
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
524 parse_ncp(char *stringp
, /* string to parse */
525 struct netconfig
*ncp
) /* where to put results */
527 char *tokenp
; /* for processing tokens */
531 nc_error
= NC_BADFILE
; /* nearly anything that breaks is for this reason */
532 stringp
[strlen(stringp
)-1] = '\0'; /* get rid of newline */
534 if ((ncp
->nc_netid
= strtok_r(stringp
, "\t ", &lasts
)) == NULL
) {
539 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
) {
542 if (strcmp(tokenp
, NC_TPI_COTS_ORD_S
) == 0)
543 ncp
->nc_semantics
= NC_TPI_COTS_ORD
;
544 else if (strcmp(tokenp
, NC_TPI_COTS_S
) == 0)
545 ncp
->nc_semantics
= NC_TPI_COTS
;
546 else if (strcmp(tokenp
, NC_TPI_CLTS_S
) == 0)
547 ncp
->nc_semantics
= NC_TPI_CLTS
;
548 else if (strcmp(tokenp
, NC_TPI_RAW_S
) == 0)
549 ncp
->nc_semantics
= NC_TPI_RAW
;
554 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
) {
557 for (ncp
->nc_flag
= NC_NOFLAG
; *tokenp
!= '\0';
563 ncp
->nc_flag
|= NC_VISIBLE
;
566 ncp
->nc_flag
|= NC_BROADCAST
;
572 /* protocol family */
573 if ((ncp
->nc_protofmly
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
) {
577 if ((ncp
->nc_proto
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
) {
581 if ((ncp
->nc_device
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
) {
584 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
) {
587 if (strcmp(tokenp
, NC_NOLOOKUP
) == 0) {
588 ncp
->nc_nlookups
= 0;
589 ncp
->nc_lookups
= NULL
;
591 char *cp
; /* tmp string */
593 if (ncp
->nc_lookups
!= NULL
) /* from last visit */
594 free(ncp
->nc_lookups
);
595 ncp
->nc_lookups
= NULL
;
596 ncp
->nc_nlookups
= 0;
597 while ((cp
= tokenp
) != NULL
) {
598 if ((nc_lookups
= realloc(ncp
->nc_lookups
,
599 (ncp
->nc_nlookups
+ 1) * sizeof *ncp
->nc_lookups
)) == NULL
) {
600 free(ncp
->nc_lookups
);
601 ncp
->nc_lookups
= NULL
;
604 tokenp
= _get_next_token(cp
, ',');
605 ncp
->nc_lookups
= nc_lookups
;
606 ncp
->nc_lookups
[ncp
->nc_nlookups
++] = cp
;
614 * Returns a string describing the reason for failure.
623 message
= _nc_errors
[0];
626 message
= _nc_errors
[1];
629 message
= _nc_errors
[2];
632 message
= _nc_errors
[3];
635 message
= _nc_errors
[4];
638 message
= "Unknown network selection error";
640 /* LINTED const castaway */
641 return ((char *)message
);
645 * Prints a message onto standard error describing the reason for failure.
648 nc_perror(const char *s
)
650 fprintf(stderr
, "%s: %s\n", s
, nc_sperror());
654 * Duplicates the matched netconfig buffer.
656 static struct netconfig
*
657 dup_ncp(struct netconfig
*ncp
)
663 if ((tmp
=malloc(MAXNETCONFIGLINE
)) == NULL
)
665 if ((p
=(struct netconfig
*)malloc(sizeof(struct netconfig
))) == NULL
) {
670 * First we dup all the data from matched netconfig buffer. Then we
671 * adjust some of the member pointer to a pre-allocated buffer where
672 * contains part of the data.
673 * To follow the convention used in parse_ncp(), we store all the
674 * necessary information in the pre-allocated buffer and let each
675 * of the netconfig char pointer member point to the right address
679 p
->nc_netid
= (char *)strcpy(tmp
,ncp
->nc_netid
);
680 tmp
= strchr(tmp
, '\0') + 1;
681 p
->nc_protofmly
= (char *)strcpy(tmp
,ncp
->nc_protofmly
);
682 tmp
= strchr(tmp
, '\0') + 1;
683 p
->nc_proto
= (char *)strcpy(tmp
,ncp
->nc_proto
);
684 tmp
= strchr(tmp
, '\0') + 1;
685 p
->nc_device
= (char *)strcpy(tmp
,ncp
->nc_device
);
686 p
->nc_lookups
= (char **)malloc((size_t)(p
->nc_nlookups
+1) * sizeof(char *));
687 if (p
->nc_lookups
== NULL
) {
692 for (i
=0; i
< p
->nc_nlookups
; i
++) {
693 tmp
= strchr(tmp
, '\0') + 1;
694 p
->nc_lookups
[i
] = (char *)strcpy(tmp
,ncp
->nc_lookups
[i
]);