1 /* $NetBSD: getnetconfig.c,v 1.22 2014/09/18 13:58:20 christos Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid
[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
39 __RCSID("$NetBSD: getnetconfig.c,v 1.22 2014/09/18 13:58:20 christos Exp $");
44 * Copyright (c) 1989 by Sun Microsystems, Inc.
47 #include "namespace.h"
48 #include "reentrant.h"
52 #include <netconfig.h>
57 #include "rpc_internal.h"
60 __weak_alias(getnetconfig
,_getnetconfig
)
61 __weak_alias(setnetconfig
,_setnetconfig
)
62 __weak_alias(endnetconfig
,_endnetconfig
)
63 __weak_alias(getnetconfigent
,_getnetconfigent
)
64 __weak_alias(freenetconfigent
,_freenetconfigent
)
65 __weak_alias(nc_perror
,_nc_perror
)
66 __weak_alias(nc_sperror
,_nc_sperror
)
70 * The five library routines in this file provide application access to the
71 * system network configuration database, /etc/netconfig. In addition to the
72 * netconfig database and the routines for accessing it, the environment
73 * variable NETPATH and its corresponding routines in getnetpath.c may also be
74 * used to specify the network transport to be used.
82 #define NC_NONETCONFIG ENOENT
83 #define NC_NOMEM ENOMEM
84 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
85 #define NC_BADFILE EBADF /* format for netconfig file is bad */
88 * semantics as strings (should be in netconfig.h)
90 #define NC_TPI_CLTS_S "tpi_clts"
91 #define NC_TPI_COTS_S "tpi_cots"
92 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
93 #define NC_TPI_RAW_S "tpi_raw"
96 * flags as characters (also should be in netconfig.h)
98 #define NC_NOFLAG_C '-'
99 #define NC_VISIBLE_C 'v'
100 #define NC_BROADCAST_C 'b'
103 * Character used to indicate there is no name-to-address lookup library
105 #define NC_NOLOOKUP "-"
107 static const char * const _nc_errors
[] = {
108 "Netconfig database not found",
111 "Netconfig database has invalid format"
114 struct netconfig_info
{
115 int eof
; /* all entries has been read */
116 int ref
; /* # of times setnetconfig() has been called */
117 struct netconfig_list
*head
; /* head of the list */
118 struct netconfig_list
*tail
; /* last of the list */
121 struct netconfig_list
{
122 char *linep
; /* hold line read from netconfig */
123 struct netconfig
*ncp
;
124 struct netconfig_list
*next
;
127 struct netconfig_vars
{
128 int valid
; /* token that indicates valid netconfig_vars */
129 int flag
; /* first time flag */
130 struct netconfig_list
*nc_configs
;
131 /* pointer to the current netconfig entry */
134 #define NC_VALID 0xfeed
135 #define NC_STORAGE 0xf00d
139 static int *__nc_error(void);
140 static int parse_ncp(char *, struct netconfig
*);
141 static struct netconfig
*dup_ncp(struct netconfig
*);
144 static FILE *nc_file
; /* for netconfig db */
145 static struct netconfig_info ni
= { 0, 0, NULL
, NULL
};
147 #define MAXNETCONFIGLINE 1000
150 static thread_key_t nc_key
;
151 static once_t nc_once
= ONCE_INITIALIZER
;
154 __nc_error_setup(void)
156 thr_keycreate(&nc_key
, free
);
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
, "re")) != 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(void *handlep
)
248 struct netconfig_vars
*ncp
= (struct netconfig_vars
*)handlep
;
249 char *stringp
; /* tmp string pointer */
250 struct netconfig_list
*list
;
251 struct netconfig
*np
;
254 * Verify that handle is valid
256 if (ncp
== NULL
|| nc_file
== NULL
) {
257 nc_error
= NC_NOTINIT
;
261 switch (ncp
->valid
) {
264 * If entry has already been read into the list,
265 * we return the entry in the linked list.
266 * If this is the first time call, check if there are any
267 * entries in linked list. If no entries, we need to read the
269 * If we have been here and the next entry is there, we just
272 if (ncp
->flag
== 0) { /* first time */
274 ncp
->nc_configs
= ni
.head
;
275 if (ncp
->nc_configs
!= NULL
) /* entry already exist */
276 return(ncp
->nc_configs
->ncp
);
278 else if (ncp
->nc_configs
!= NULL
&&
279 ncp
->nc_configs
->next
!= NULL
) {
280 ncp
->nc_configs
= ncp
->nc_configs
->next
;
281 return(ncp
->nc_configs
->ncp
);
285 * If we cannot find the entry in the list and is end of file,
292 nc_error
= NC_NOTINIT
;
296 stringp
= malloc(MAXNETCONFIGLINE
);
301 if (malloc_verify() == 0) {
302 fprintf(stderr
, "memory heap corrupted in getnetconfig\n");
308 * Read a line from netconfig file.
311 if (fgets(stringp
, MAXNETCONFIGLINE
, nc_file
) == NULL
) {
316 } while (*stringp
== '#');
318 list
= malloc(sizeof(*list
));
323 np
= malloc(sizeof(*np
));
331 list
->ncp
->nc_lookups
= NULL
;
332 list
->linep
= stringp
;
333 if (parse_ncp(stringp
, list
->ncp
) == -1) {
340 * If this is the first entry that's been read, it is the
341 * head of the list. If not, put the entry at the end of
342 * the list. Reposition the current pointer of the handle to
343 * the last entry in the list.
345 if (ni
.head
== NULL
) /* first entry */
346 ni
.head
= ni
.tail
= list
;
348 ni
.tail
->next
= list
;
349 ni
.tail
= ni
.tail
->next
;
351 ncp
->nc_configs
= ni
.tail
;
352 return(ni
.tail
->ncp
);
357 * endnetconfig() may be called to "unbind" or "close" the netconfig database
358 * when processing is complete, releasing resources for reuse. endnetconfig()
359 * may not be called before setnetconfig(). endnetconfig() returns 0 on
360 * success and -1 on failure (for example, if setnetconfig() was not called
364 endnetconfig(void *handlep
)
366 struct netconfig_vars
*nc_handlep
= (struct netconfig_vars
*)handlep
;
368 struct netconfig_list
*q
, *p
;
371 * Verify that handle is valid
373 if (nc_handlep
== NULL
|| (nc_handlep
->valid
!= NC_VALID
&&
374 nc_handlep
->valid
!= NC_STORAGE
)) {
375 nc_error
= NC_NOTINIT
;
380 * Return 0 if anyone still needs it.
382 nc_handlep
->valid
= NC_INVALID
;
383 nc_handlep
->flag
= 0;
384 nc_handlep
->nc_configs
= NULL
;
391 * Noone needs these entries anymore, then frees them.
392 * Make sure all info in netconfig_info structure has been
401 if (q
->ncp
->nc_lookups
!= NULL
) free(q
->ncp
->nc_lookups
);
415 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
416 * corresponding to netid. It returns NULL if netid is invalid (that is, does
417 * not name an entry in the netconfig database). It returns NULL and sets
418 * errno in case of failure (for example, if the netconfig database cannot be
423 getnetconfigent(const char *netid
)
425 FILE *file
; /* NETCONFIG db's file pointer */
426 char *linep
; /* holds current netconfig line */
427 char *stringp
; /* temporary string pointer */
428 struct netconfig
*ncp
= NULL
; /* returned value */
429 struct netconfig_list
*list
; /* pointer to cache list */
431 if (netid
== NULL
|| strlen(netid
) == 0)
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
));
448 if (ni
.eof
== 1) /* that's all the entries */
452 if ((file
= fopen(NETCONFIG
, "re")) == NULL
)
455 if ((linep
= malloc(MAXNETCONFIGLINE
)) == NULL
) {
461 char *tmpp
; /* tmp string pointer */
464 if ((stringp
= fgets(linep
, MAXNETCONFIGLINE
, file
))
467 } while (*stringp
== '#');
468 if (stringp
== NULL
) /* eof */
470 if ((tmpp
= strpbrk(stringp
, "\t ")) == NULL
) {
471 /* can't parse file */
472 nc_error
= NC_BADFILE
;
475 if (strlen(netid
) == (size_t) (len
= tmpp
- stringp
) && /* a match */
476 strncmp(stringp
, netid
, (size_t)len
) == 0) {
477 if ((ncp
= malloc(sizeof(*ncp
))) == NULL
)
479 ncp
->nc_lookups
= NULL
;
480 if (parse_ncp(linep
, ncp
) == -1) {
486 } 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 /* holds all netconfigp's strings */
503 free(netconfigp
->nc_netid
);
504 if (netconfigp
->nc_lookups
!= NULL
)
505 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
524 char *stringp
, /* string to parse */
525 struct netconfig
*ncp
) /* where to put results */
527 char *tokenp
; /* for processing tokens */
530 _DIAGASSERT(stringp
!= NULL
);
531 _DIAGASSERT(ncp
!= NULL
);
533 nc_error
= NC_BADFILE
;
534 /* nearly anything that breaks is for this reason */
535 stringp
[strlen(stringp
)-1] = '\0'; /* get rid of newline */
537 if ((ncp
->nc_netid
= strtok_r(stringp
, "\t ", &lasts
)) == NULL
)
541 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
543 if (strcmp(tokenp
, NC_TPI_COTS_ORD_S
) == 0)
544 ncp
->nc_semantics
= NC_TPI_COTS_ORD
;
545 else if (strcmp(tokenp
, NC_TPI_COTS_S
) == 0)
546 ncp
->nc_semantics
= NC_TPI_COTS
;
547 else if (strcmp(tokenp
, NC_TPI_CLTS_S
) == 0)
548 ncp
->nc_semantics
= NC_TPI_CLTS
;
549 else if (strcmp(tokenp
, NC_TPI_RAW_S
) == 0)
550 ncp
->nc_semantics
= NC_TPI_RAW
;
555 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
557 for (ncp
->nc_flag
= NC_NOFLAG
; *tokenp
!= '\0'; tokenp
++) {
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
)
575 if ((ncp
->nc_proto
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
578 if ((ncp
->nc_device
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
580 if ((tokenp
= strtok_r(NULL
, "\t ", &lasts
)) == NULL
)
582 if (strcmp(tokenp
, NC_NOLOOKUP
) == 0) {
583 ncp
->nc_nlookups
= 0;
584 ncp
->nc_lookups
= NULL
;
586 char *cp
; /* tmp string */
588 if (ncp
->nc_lookups
!= NULL
) /* from last visit */
589 free(ncp
->nc_lookups
);
590 /* preallocate one string pointer */
591 ncp
->nc_lookups
= malloc(sizeof(*ncp
->nc_lookups
));
592 ncp
->nc_nlookups
= 0;
593 while ((cp
= tokenp
) != NULL
) {
594 tokenp
= _get_next_token(cp
, ',');
595 ncp
->nc_lookups
[(size_t)ncp
->nc_nlookups
++] = cp
;
596 ncp
->nc_lookups
= (char **)
597 realloc(ncp
->nc_lookups
,
598 (size_t)(ncp
->nc_nlookups
+1) *sizeof(char *));
606 * Returns a string describing the reason for failure.
615 message
= _nc_errors
[0];
618 message
= _nc_errors
[1];
621 message
= _nc_errors
[2];
624 message
= _nc_errors
[3];
627 message
= "Unknown network selection error";
629 return __UNCONST(message
);
633 * Prints a message onto standard error describing the reason for failure.
636 nc_perror(const char *s
)
639 _DIAGASSERT(s
!= NULL
);
641 fprintf(stderr
, "%s: %s", s
, nc_sperror());
645 * Duplicates the matched netconfig buffer.
647 static struct netconfig
*
648 dup_ncp(struct netconfig
*ncp
)
654 _DIAGASSERT(ncp
!= NULL
);
656 if ((tmp
= malloc(MAXNETCONFIGLINE
)) == NULL
)
658 if ((p
= malloc(sizeof(*p
))) == NULL
) {
663 * First we dup all the data from matched netconfig buffer. Then we
664 * adjust some of the member pointer to a pre-allocated buffer where
665 * contains part of the data.
666 * To follow the convention used in parse_ncp(), we store all the
667 * necessary information in the pre-allocated buffer and let each
668 * of the netconfig char pointer member point to the right address
672 p
->nc_netid
= (char *)strcpy(tmp
,ncp
->nc_netid
);
673 tmp
= strchr(tmp
, '\0') + 1;
674 p
->nc_protofmly
= (char *)strcpy(tmp
,ncp
->nc_protofmly
);
675 tmp
= strchr(tmp
, '\0') + 1;
676 p
->nc_proto
= (char *)strcpy(tmp
,ncp
->nc_proto
);
677 tmp
= strchr(tmp
, '\0') + 1;
678 p
->nc_device
= (char *)strcpy(tmp
,ncp
->nc_device
);
679 p
->nc_lookups
= malloc((size_t)(p
->nc_nlookups
+1) * sizeof(char *));
680 if (p
->nc_lookups
== NULL
) {
685 for (i
=0; i
< p
->nc_nlookups
; i
++) {
686 tmp
= strchr(tmp
, '\0') + 1;
687 p
->nc_lookups
[i
] = strcpy(tmp
,ncp
->nc_lookups
[i
]);