1 /* $NetBSD: getnetconfig.c,v 1.16 2007/01/17 23:24:22 hubertf 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 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid
[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
38 __RCSID("$NetBSD: getnetconfig.c,v 1.16 2007/01/17 23:24:22 hubertf Exp $");
43 * Copyright (c) 1989 by Sun Microsystems, Inc.
46 #include "namespace.h"
47 #include "reentrant.h"
51 #include <netconfig.h>
56 #include "rpc_internal.h"
59 __weak_alias(getnetconfig
,_getnetconfig
)
60 __weak_alias(setnetconfig
,_setnetconfig
)
61 __weak_alias(endnetconfig
,_endnetconfig
)
62 __weak_alias(getnetconfigent
,_getnetconfigent
)
63 __weak_alias(freenetconfigent
,_freenetconfigent
)
64 __weak_alias(nc_perror
,_nc_perror
)
65 __weak_alias(nc_sperror
,_nc_sperror
)
69 * The five library routines in this file provide application access to the
70 * system network configuration database, /etc/netconfig. In addition to the
71 * netconfig database and the routines for accessing it, the environment
72 * variable NETPATH and its corresponding routines in getnetpath.c may also be
73 * used to specify the network transport to be used.
81 #define NC_NONETCONFIG ENOENT
82 #define NC_NOMEM ENOMEM
83 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
84 #define NC_BADFILE EBADF /* format for netconfig file is bad */
87 * semantics as strings (should be in netconfig.h)
89 #define NC_TPI_CLTS_S "tpi_clts"
90 #define NC_TPI_COTS_S "tpi_cots"
91 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
92 #define NC_TPI_RAW_S "tpi_raw"
95 * flags as characters (also should be in netconfig.h)
97 #define NC_NOFLAG_C '-'
98 #define NC_VISIBLE_C 'v'
99 #define NC_BROADCAST_C 'b'
102 * Character used to indicate there is no name-to-address lookup library
104 #define NC_NOLOOKUP "-"
106 static const char * const _nc_errors
[] = {
107 "Netconfig database not found",
110 "Netconfig database has invalid format"
113 struct netconfig_info
{
114 int eof
; /* all entries has been read */
115 int ref
; /* # of times setnetconfig() has been called */
116 struct netconfig_list
*head
; /* head of the list */
117 struct netconfig_list
*tail
; /* last of the list */
120 struct netconfig_list
{
121 char *linep
; /* hold line read from netconfig */
122 struct netconfig
*ncp
;
123 struct netconfig_list
*next
;
126 struct netconfig_vars
{
127 int valid
; /* token that indicates valid netconfig_vars */
128 int flag
; /* first time flag */
129 struct netconfig_list
*nc_configs
;
130 /* pointer to the current netconfig entry */
133 #define NC_VALID 0xfeed
134 #define NC_STORAGE 0xf00d
138 static int *__nc_error
__P((void));
139 static int parse_ncp
__P((char *, struct netconfig
*));
140 static struct netconfig
*dup_ncp
__P((struct netconfig
*));
143 static FILE *nc_file
; /* for netconfig db */
144 static struct netconfig_info ni
= { 0, 0, NULL
, NULL
};
146 #define MAXNETCONFIGLINE 1000
149 static thread_key_t nc_key
;
150 static once_t nc_once
= ONCE_INITIALIZER
;
153 __nc_error_setup(void)
155 thr_keycreate(&nc_key
, free
);
163 extern int __isthreaded
;
166 static int nc_error
= 0;
169 if (__isthreaded
== 0)
171 thr_once(&nc_once
, __nc_error_setup
);
172 nc_addr
= thr_getspecific(nc_key
) ;
173 if (nc_addr
== NULL
) {
174 nc_addr
= malloc(sizeof (int));
177 if (thr_setspecific(nc_key
, (void *) nc_addr
) != 0) {
190 #define nc_error (*(__nc_error()))
192 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
193 * "handle" is returned on a successful call. At the start of a session (after
194 * a call to setnetconfig()) searches through the /etc/netconfig database will
195 * proceed from the start of the file. The session handle must be passed to
196 * getnetconfig() to parse the file. Each call to getnetconfig() using the
197 * current handle will process one subsequent entry in /etc/netconfig.
198 * setnetconfig() must be called before the first call to getnetconfig().
199 * (Handles are used to allow for nested calls to setnetpath()).
201 * A new session is established with each call to setnetconfig(), with a new
202 * handle being returned on each call. Previously established sessions remain
203 * active until endnetconfig() is called with that session's handle as an
206 * setnetconfig() need *not* be called before a call to getnetconfigent().
207 * setnetconfig() returns a NULL pointer on failure (for example, if
208 * the netconfig database is not present).
213 struct netconfig_vars
*nc_vars
;
215 if ((nc_vars
= malloc(sizeof(*nc_vars
))) == NULL
) {
220 * For multiple calls, i.e. nc_file is not NULL, we just return the
221 * handle without reopening the netconfig db.
224 if ((nc_file
!= NULL
) || (nc_file
= fopen(NETCONFIG
, "r")) != NULL
) {
225 nc_vars
->valid
= NC_VALID
;
227 nc_vars
->nc_configs
= ni
.head
;
228 return ((void *)nc_vars
);
231 nc_error
= NC_NONETCONFIG
;
238 * When first called, getnetconfig() returns a pointer to the first entry in
239 * the netconfig database, formatted as a struct netconfig. On each subsequent
240 * call, getnetconfig() returns a pointer to the next entry in the database.
241 * getnetconfig() can thus be used to search the entire netconfig file.
242 * getnetconfig() returns NULL at end of file.
246 getnetconfig(handlep
)
249 struct netconfig_vars
*ncp
= (struct netconfig_vars
*)handlep
;
250 char *stringp
; /* tmp string pointer */
251 struct netconfig_list
*list
;
252 struct netconfig
*np
;
255 * Verify that handle is valid
257 if (ncp
== NULL
|| nc_file
== NULL
) {
258 nc_error
= NC_NOTINIT
;
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
268 * entries in linked list. If no entries, we need to read the
270 * If we have been here and the next entry is there, we just
273 if (ncp
->flag
== 0) { /* first time */
275 ncp
->nc_configs
= ni
.head
;
276 if (ncp
->nc_configs
!= NULL
) /* entry already exist */
277 return(ncp
->nc_configs
->ncp
);
279 else if (ncp
->nc_configs
!= NULL
&&
280 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,
293 nc_error
= NC_NOTINIT
;
297 stringp
= malloc(MAXNETCONFIGLINE
);
302 if (malloc_verify() == 0) {
303 fprintf(stderr
, "memory heap corrupted in getnetconfig\n");
309 * Read a line from netconfig file.
312 if (fgets(stringp
, MAXNETCONFIGLINE
, nc_file
) == NULL
) {
317 } while (*stringp
== '#');
319 list
= malloc(sizeof(*list
));
324 np
= malloc(sizeof(*np
));
332 list
->ncp
->nc_lookups
= NULL
;
333 list
->linep
= stringp
;
334 if (parse_ncp(stringp
, list
->ncp
) == -1) {
341 * If this is the first entry that's been read, it is the
342 * head of the list. If not, put the entry at the end of
343 * the list. Reposition the current pointer of the handle to
344 * the last entry in the list.
346 if (ni
.head
== NULL
) /* first entry */
347 ni
.head
= ni
.tail
= list
;
349 ni
.tail
->next
= list
;
350 ni
.tail
= ni
.tail
->next
;
352 ncp
->nc_configs
= ni
.tail
;
353 return(ni
.tail
->ncp
);
358 * endnetconfig() may be called to "unbind" or "close" the netconfig database
359 * when processing is complete, releasing resources for reuse. endnetconfig()
360 * may not be called before setnetconfig(). endnetconfig() returns 0 on
361 * success and -1 on failure (for example, if setnetconfig() was not called
365 endnetconfig(handlep
)
368 struct netconfig_vars
*nc_handlep
= (struct netconfig_vars
*)handlep
;
370 struct netconfig_list
*q
, *p
;
373 * Verify that handle is valid
375 if (nc_handlep
== NULL
|| (nc_handlep
->valid
!= NC_VALID
&&
376 nc_handlep
->valid
!= NC_STORAGE
)) {
377 nc_error
= NC_NOTINIT
;
382 * Return 0 if anyone still needs it.
384 nc_handlep
->valid
= NC_INVALID
;
385 nc_handlep
->flag
= 0;
386 nc_handlep
->nc_configs
= NULL
;
393 * Noone needs these entries anymore, then frees them.
394 * Make sure all info in netconfig_info structure has been
403 if (q
->ncp
->nc_lookups
!= NULL
) free(q
->ncp
->nc_lookups
);
417 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
418 * corresponding to netid. It returns NULL if netid is invalid (that is, does
419 * not name an entry in the netconfig database). It returns NULL and sets
420 * errno in case of failure (for example, if the netconfig database cannot be
425 getnetconfigent(netid
)
428 FILE *file
; /* NETCONFIG db's file pointer */
429 char *linep
; /* holds current netconfig line */
430 char *stringp
; /* temporary string pointer */
431 struct netconfig
*ncp
= NULL
; /* returned value */
432 struct netconfig_list
*list
; /* pointer to cache list */
434 if (netid
== NULL
|| strlen(netid
) == 0)
438 * Look up table if the entries have already been read and parsed in
439 * getnetconfig(), then copy this entry into a buffer and return it.
440 * If we cannot find the entry in the current list and there are more
441 * entries in the netconfig db that has not been read, we then read the
442 * db and try find the match netid.
443 * If all the netconfig db has been read and placed into the list and
444 * there is no match for the netid, return NULL.
446 if (ni
.head
!= NULL
) {
447 for (list
= ni
.head
; list
; list
= list
->next
) {
448 if (strcmp(list
->ncp
->nc_netid
, netid
) == 0)
449 return(dup_ncp(list
->ncp
));
451 if (ni
.eof
== 1) /* that's all the entries */
455 if ((file
= fopen(NETCONFIG
, "r")) == NULL
)
458 if ((linep
= malloc(MAXNETCONFIGLINE
)) == NULL
) {
464 char *tmpp
; /* tmp string pointer */
467 if ((stringp
= fgets(linep
, MAXNETCONFIGLINE
, file
))
470 } while (*stringp
== '#');
471 if (stringp
== NULL
) /* eof */
473 if ((tmpp
= strpbrk(stringp
, "\t ")) == NULL
) {
474 /* can't parse file */
475 nc_error
= NC_BADFILE
;
478 if (strlen(netid
) == (size_t) (len
= tmpp
- stringp
) && /* a match */
479 strncmp(stringp
, netid
, (size_t)len
) == 0) {
480 if ((ncp
= malloc(sizeof(*ncp
))) == NULL
)
482 ncp
->nc_lookups
= NULL
;
483 if (parse_ncp(linep
, ncp
) == -1) {
489 } while (stringp
!= NULL
);
497 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
498 * netconfigp (previously returned by getnetconfigent()).
502 freenetconfigent(netconfigp
)
503 struct netconfig
*netconfigp
;
505 if (netconfigp
!= NULL
) {
506 /* holds all netconfigp's strings */
507 free(netconfigp
->nc_netid
);
508 if (netconfigp
->nc_lookups
!= NULL
)
509 free(netconfigp
->nc_lookups
);
515 * Parse line and stuff it in a struct netconfig
516 * Typical line might look like:
517 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
519 * We return -1 if any of the tokens don't parse, or malloc fails.
521 * Note that we modify stringp (putting NULLs after tokens) and
522 * we set the ncp's string field pointers to point to these tokens within
527 parse_ncp(stringp
, ncp
)
528 char *stringp
; /* string to parse */
529 struct netconfig
*ncp
; /* where to put results */
531 char *tokenp
; /* for processing tokens */
534 _DIAGASSERT(stringp
!= NULL
);
535 _DIAGASSERT(ncp
!= NULL
);
537 nc_error
= NC_BADFILE
;
538 /* nearly anything that breaks is for this reason */
539 stringp
[strlen(stringp
)-1] = '\0'; /* get rid of newline */
541 if ((ncp
->nc_netid
= strtok_r(stringp
, "\t ", &lasts
)) == NULL
)
545 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
547 if (strcmp(tokenp
, NC_TPI_COTS_ORD_S
) == 0)
548 ncp
->nc_semantics
= NC_TPI_COTS_ORD
;
549 else if (strcmp(tokenp
, NC_TPI_COTS_S
) == 0)
550 ncp
->nc_semantics
= NC_TPI_COTS
;
551 else if (strcmp(tokenp
, NC_TPI_CLTS_S
) == 0)
552 ncp
->nc_semantics
= NC_TPI_CLTS
;
553 else if (strcmp(tokenp
, NC_TPI_RAW_S
) == 0)
554 ncp
->nc_semantics
= NC_TPI_RAW
;
559 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
561 for (ncp
->nc_flag
= NC_NOFLAG
; *tokenp
!= '\0'; tokenp
++) {
566 ncp
->nc_flag
|= NC_VISIBLE
;
569 ncp
->nc_flag
|= NC_BROADCAST
;
575 /* protocol family */
576 if ((ncp
->nc_protofmly
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
579 if ((ncp
->nc_proto
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
582 if ((ncp
->nc_device
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
584 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 /* preallocate one string pointer */
595 ncp
->nc_lookups
= malloc(sizeof(*ncp
->nc_lookups
));
596 ncp
->nc_nlookups
= 0;
597 while ((cp
= tokenp
) != NULL
) {
598 tokenp
= _get_next_token(cp
, ',');
599 ncp
->nc_lookups
[(size_t)ncp
->nc_nlookups
++] = cp
;
600 ncp
->nc_lookups
= (char **)
601 realloc(ncp
->nc_lookups
,
602 (size_t)(ncp
->nc_nlookups
+1) *sizeof(char *));
610 * Returns a string describing the reason for failure.
619 message
= _nc_errors
[0];
622 message
= _nc_errors
[1];
625 message
= _nc_errors
[2];
628 message
= _nc_errors
[3];
631 message
= "Unknown network selection error";
633 return __UNCONST(message
);
637 * Prints a message onto standard error describing the reason for failure.
644 _DIAGASSERT(s
!= NULL
);
646 fprintf(stderr
, "%s: %s", s
, nc_sperror());
650 * Duplicates the matched netconfig buffer.
652 static struct netconfig
*
654 struct netconfig
*ncp
;
660 _DIAGASSERT(ncp
!= NULL
);
662 if ((tmp
= malloc(MAXNETCONFIGLINE
)) == NULL
)
664 if ((p
= malloc(sizeof(*p
))) == 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
= 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
] = strcpy(tmp
,ncp
->nc_lookups
[i
]);