getnetconfig.c revision 1.4 1 /* $NetBSD: getnetconfig.c,v 1.4 2001/01/04 14:42:19 lukem Exp $ */
2
3 /*
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.
11 *
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.
15 *
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.
19 *
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.
23 *
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.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32 /*
33 #ifndef lint
34 static char sccsid[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
35 #endif
36 */
37
38 /*
39 * Copyright (c) 1989 by Sun Microsystems, Inc.
40 */
41
42 #include "namespace.h"
43 #include <sys/cdefs.h>
44 #include <stdio.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <netconfig.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <rpc/rpc.h>
51 #include "rpc_com.h"
52
53 #ifdef __weak_alias
54 __weak_alias(getnetconfig,_getnetconfig)
55 __weak_alias(setnetconfig,_setnetconfig)
56 __weak_alias(endnetconfig,_endnetconfig)
57 __weak_alias(getnetconfigent,_getnetconfigent)
58 __weak_alias(freenetconfigent,_freenetconfigent)
59 __weak_alias(nc_perror,_nc_perror)
60 __weak_alias(nc_sperror,_nc_sperror)
61 #endif
62
63 /*
64 * The five library routines in this file provide application access to the
65 * system network configuration database, /etc/netconfig. In addition to the
66 * netconfig database and the routines for accessing it, the environment
67 * variable NETPATH and its corresponding routines in getnetpath.c may also be
68 * used to specify the network transport to be used.
69 */
70
71
72 /*
73 * netconfig errors
74 */
75
76 #define NC_NONETCONFIG ENOENT
77 #define NC_NOMEM ENOMEM
78 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
79 #define NC_BADFILE EBADF /* format for netconfig file is bad */
80
81 /*
82 * semantics as strings (should be in netconfig.h)
83 */
84 #define NC_TPI_CLTS_S "tpi_clts"
85 #define NC_TPI_COTS_S "tpi_cots"
86 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
87 #define NC_TPI_RAW_S "tpi_raw"
88
89 /*
90 * flags as characters (also should be in netconfig.h)
91 */
92 #define NC_NOFLAG_C '-'
93 #define NC_VISIBLE_C 'v'
94 #define NC_BROADCAST_C 'b'
95
96 /*
97 * Character used to indicate there is no name-to-address lookup library
98 */
99 #define NC_NOLOOKUP "-"
100
101 static char *_nc_errors[] = {
102 "Netconfig database not found",
103 "Not enough memory",
104 "Not initialized",
105 "Netconfig database has invalid format"
106 };
107
108 struct netconfig_info {
109 int eof; /* all entries has been read */
110 int ref; /* # of times setnetconfig() has been called */
111 struct netconfig_list *head; /* head of the list */
112 struct netconfig_list *tail; /* last of the list */
113 };
114
115 struct netconfig_list {
116 char *linep; /* hold line read from netconfig */
117 struct netconfig *ncp;
118 struct netconfig_list *next;
119 };
120
121 struct netconfig_vars {
122 int valid; /* token that indicates a valid netconfig_vars */
123 int flag; /* first time flag */
124 struct netconfig_list *nc_configs; /* pointer to the current netconfig entry */
125 };
126
127 #define NC_VALID 0xfeed
128 #define NC_STORAGE 0xf00d
129 #define NC_INVALID 0
130
131
132 static int *__nc_error __P((void));
133 static int parse_ncp __P((char *, struct netconfig *));
134 static struct netconfig *dup_ncp __P((struct netconfig *));
135
136
137 static FILE *nc_file; /* for netconfig db */
138 static struct netconfig_info ni = { 0, 0, NULL, NULL};
139
140 #define MAXNETCONFIGLINE 1000
141
142 static int *
143 __nc_error()
144 {
145 #ifdef __REENT
146 static thread_key_t nc_key = 0;
147 int *nc_addr = NULL;
148 #endif
149 static int nc_error = 0;
150
151 #ifdef __REENT
152 if (_thr_getspecific(nc_key, (void **) &nc_addr) != 0) {
153 mutex_lock(&nc_lock);
154 if (_thr_keycreate(&rce_key, free) != 0) {
155 mutex_unlock(&nc_lock);
156 return nc_addr;
157 }
158 mutex_unlock(&nc_lock);
159 }
160 if (nc_addr == NULL) {
161 nc_addr = (int *)malloc(sizeof (int));
162 if (_thr_setspecific(nc_key, (void *) nc_addr) != 0) {
163 if (nc_addr)
164 free(nc_addr);
165 return &nc_error;
166 }
167 *nc_addr = 0;
168 return nc_addr;
169 }
170 return nc_addr;
171 #else
172 return &nc_error;
173 #endif
174 }
175
176 #define nc_error (*(__nc_error()))
177 /*
178 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
179 * "handle" is returned on a successful call. At the start of a session (after
180 * a call to setnetconfig()) searches through the /etc/netconfig database will
181 * proceed from the start of the file. The session handle must be passed to
182 * getnetconfig() to parse the file. Each call to getnetconfig() using the
183 * current handle will process one subsequent entry in /etc/netconfig.
184 * setnetconfig() must be called before the first call to getnetconfig().
185 * (Handles are used to allow for nested calls to setnetpath()).
186 *
187 * A new session is established with each call to setnetconfig(), with a new
188 * handle being returned on each call. Previously established sessions remain
189 * active until endnetconfig() is called with that session's handle as an
190 * argument.
191 *
192 * setnetconfig() need *not* be called before a call to getnetconfigent().
193 * setnetconfig() returns a NULL pointer on failure (for example, if
194 * the netconfig database is not present).
195 */
196 void *
197 setnetconfig()
198 {
199 struct netconfig_vars *nc_vars;
200
201 if ((nc_vars = (struct netconfig_vars *)malloc(sizeof
202 (struct netconfig_vars))) == NULL) {
203 return(NULL);
204 }
205
206 /*
207 * For multiple calls, i.e. nc_file is not NULL, we just return the
208 * handle without reopening the netconfig db.
209 */
210 ni.ref++;
211 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
212 nc_vars->valid = NC_VALID;
213 nc_vars->flag = 0;
214 nc_vars->nc_configs = ni.head;
215 return ((void *)nc_vars);
216 }
217 ni.ref--;
218 nc_error = NC_NONETCONFIG;
219 free(nc_vars);
220 return (NULL);
221 }
222
223
224 /*
225 * When first called, getnetconfig() returns a pointer to the first entry in
226 * the netconfig database, formatted as a struct netconfig. On each subsequent
227 * call, getnetconfig() returns a pointer to the next entry in the database.
228 * getnetconfig() can thus be used to search the entire netconfig file.
229 * getnetconfig() returns NULL at end of file.
230 */
231
232 struct netconfig *
233 getnetconfig(handlep)
234 void *handlep;
235 {
236 struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
237 char *stringp; /* tmp string pointer */
238 struct netconfig_list *list;
239 struct netconfig *np;
240
241 /*
242 * Verify that handle is valid
243 */
244 if (ncp == NULL || nc_file == NULL) {
245 nc_error = NC_NOTINIT;
246 return (NULL);
247 }
248
249 switch (ncp->valid) {
250 case NC_VALID:
251 /*
252 * If entry has already been read into the list,
253 * we return the entry in the linked list.
254 * If this is the first time call, check if there are any entries in
255 * linked list. If no entries, we need to read the netconfig db.
256 * If we have been here and the next entry is there, we just return
257 * it.
258 */
259 if (ncp->flag == 0) { /* first time */
260 ncp->flag = 1;
261 ncp->nc_configs = ni.head;
262 if (ncp->nc_configs != NULL) /* entry already exist */
263 return(ncp->nc_configs->ncp);
264 }
265 else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
266 ncp->nc_configs = ncp->nc_configs->next;
267 return(ncp->nc_configs->ncp);
268 }
269
270 /*
271 * If we cannot find the entry in the list and is end of file,
272 * we give up.
273 */
274 if (ni.eof == 1) return(NULL);
275 break;
276 default:
277 nc_error = NC_NOTINIT;
278 return (NULL);
279 }
280
281 stringp = (char *) malloc(MAXNETCONFIGLINE);
282 if (stringp == NULL)
283 return (NULL);
284
285 #ifdef MEM_CHK
286 if (malloc_verify() == 0) {
287 fprintf(stderr, "memory heap corrupted in getnetconfig\n");
288 exit(1);
289 }
290 #endif
291
292 /*
293 * Read a line from netconfig file.
294 */
295 do {
296 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
297 free(stringp);
298 ni.eof = 1;
299 return (NULL);
300 }
301 } while (*stringp == '#');
302
303 list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
304 if (list == NULL) {
305 free(stringp);
306 return(NULL);
307 }
308 np = (struct netconfig *) malloc(sizeof (struct netconfig));
309 if (np == NULL) {
310 free(stringp);
311 free(list);
312 return(NULL);
313 }
314 list->ncp = np;
315 list->next = NULL;
316 list->ncp->nc_lookups = NULL;
317 list->linep = stringp;
318 if (parse_ncp(stringp, list->ncp) == -1) {
319 free(stringp);
320 free(np);
321 free(list);
322 return (NULL);
323 }
324 else {
325 /*
326 * If this is the first entry that's been read, it is the head of
327 * the list. If not, put the entry at the end of the list.
328 * Reposition the current pointer of the handle to the last entry
329 * in the list.
330 */
331 if (ni.head == NULL) { /* first entry */
332 ni.head = ni.tail = list;
333 }
334 else {
335 ni.tail->next = list;
336 ni.tail = ni.tail->next;
337 }
338 ncp->nc_configs = ni.tail;
339 return(ni.tail->ncp);
340 }
341 }
342
343 /*
344 * endnetconfig() may be called to "unbind" or "close" the netconfig database
345 * when processing is complete, releasing resources for reuse. endnetconfig()
346 * may not be called before setnetconfig(). endnetconfig() returns 0 on
347 * success and -1 on failure (for example, if setnetconfig() was not called
348 * previously).
349 */
350 int
351 endnetconfig(handlep)
352 void *handlep;
353 {
354 struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
355
356 struct netconfig_list *q, *p;
357
358 /*
359 * Verify that handle is valid
360 */
361 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
362 nc_handlep->valid != NC_STORAGE)) {
363 nc_error = NC_NOTINIT;
364 return (-1);
365 }
366
367 /*
368 * Return 0 if anyone still needs it.
369 */
370 nc_handlep->valid = NC_INVALID;
371 nc_handlep->flag = 0;
372 nc_handlep->nc_configs = NULL;
373 if (--ni.ref > 0) {
374 free(nc_handlep);
375 return(0);
376 }
377
378 /*
379 * Noone needs these entries anymore, then frees them.
380 * Make sure all info in netconfig_info structure has been reinitialized.
381 */
382 q = p = ni.head;
383 ni.eof = ni.ref = 0;
384 ni.head = NULL;
385 ni.tail = NULL;
386 while (q) {
387 p = q->next;
388 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
389 free(q->ncp);
390 free(q->linep);
391 free(q);
392 q = p;
393 }
394 free(nc_handlep);
395
396 fclose(nc_file);
397 nc_file = NULL;
398 return (0);
399 }
400
401 /*
402 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
403 * corresponding to netid. It returns NULL if netid is invalid (that is, does
404 * not name an entry in the netconfig database). It returns NULL and sets
405 * errno in case of failure (for example, if the netconfig database cannot be
406 * opened).
407 */
408
409 struct netconfig *
410 getnetconfigent(netid)
411 char *netid;
412 {
413 FILE *file; /* NETCONFIG db's file pointer */
414 char *linep; /* holds current netconfig line */
415 char *stringp; /* temporary string pointer */
416 struct netconfig *ncp = NULL; /* returned value */
417 struct netconfig_list *list; /* pointer to cache list */
418
419 if (netid == NULL || strlen(netid) == 0) {
420 return (NULL);
421 }
422
423 /*
424 * Look up table if the entries have already been read and parsed in
425 * getnetconfig(), then copy this entry into a buffer and return it.
426 * If we cannot find the entry in the current list and there are more
427 * entries in the netconfig db that has not been read, we then read the
428 * db and try find the match netid.
429 * If all the netconfig db has been read and placed into the list and
430 * there is no match for the netid, return NULL.
431 */
432 if (ni.head != NULL) {
433 for (list = ni.head; list; list = list->next) {
434 if (strcmp(list->ncp->nc_netid, netid) == 0) {
435 return(dup_ncp(list->ncp));
436 }
437 }
438 if (ni.eof == 1) /* that's all the entries */
439 return(NULL);
440 }
441
442
443 if ((file = fopen(NETCONFIG, "r")) == NULL) {
444 return (NULL);
445 }
446
447 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
448 fclose(file);
449 return (NULL);
450 }
451 do {
452 int len;
453 char *tmpp; /* tmp string pointer */
454
455 do {
456 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
457 break;
458 }
459 } while (*stringp == '#');
460 if (stringp == NULL) { /* eof */
461 break;
462 }
463 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */
464 nc_error = NC_BADFILE;
465 break;
466 }
467 if (strlen(netid) == (len = tmpp - stringp) && /* a match */
468 strncmp(stringp, netid, (size_t)len) == 0) {
469 if ((ncp = (struct netconfig *)
470 malloc(sizeof (struct netconfig))) == NULL) {
471 break;
472 }
473 ncp->nc_lookups = NULL;
474 if (parse_ncp(linep, ncp) == -1) {
475 free(ncp);
476 ncp = NULL;
477 }
478 break;
479 }
480 } while (stringp != NULL);
481 if (ncp == NULL) {
482 free(linep);
483 }
484 fclose(file);
485 return(ncp);
486 }
487
488 /*
489 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
490 * netconfigp (previously returned by getnetconfigent()).
491 */
492
493 void
494 freenetconfigent(netconfigp)
495 struct netconfig *netconfigp;
496 {
497 if (netconfigp != NULL) {
498 free(netconfigp->nc_netid); /* holds all netconfigp's strings */
499 if (netconfigp->nc_lookups != NULL)
500 free(netconfigp->nc_lookups);
501 free(netconfigp);
502 }
503 return;
504 }
505
506 /*
507 * Parse line and stuff it in a struct netconfig
508 * Typical line might look like:
509 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
510 *
511 * We return -1 if any of the tokens don't parse, or malloc fails.
512 *
513 * Note that we modify stringp (putting NULLs after tokens) and
514 * we set the ncp's string field pointers to point to these tokens within
515 * stringp.
516 */
517
518 static int
519 parse_ncp(stringp, ncp)
520 char *stringp; /* string to parse */
521 struct netconfig *ncp; /* where to put results */
522 {
523 char *tokenp; /* for processing tokens */
524 char *lasts;
525
526 _DIAGASSERT(stringp != NULL);
527 _DIAGASSERT(ncp != NULL);
528
529 nc_error = NC_BADFILE; /* nearly anything that breaks is for this reason */
530 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
531 /* netid */
532 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
533 return (-1);
534 }
535
536 /* semantics */
537 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
538 return (-1);
539 }
540 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
541 ncp->nc_semantics = NC_TPI_COTS_ORD;
542 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
543 ncp->nc_semantics = NC_TPI_COTS;
544 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
545 ncp->nc_semantics = NC_TPI_CLTS;
546 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
547 ncp->nc_semantics = NC_TPI_RAW;
548 else
549 return (-1);
550
551 /* flags */
552 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
553 return (-1);
554 }
555 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
556 tokenp++) {
557 switch (*tokenp) {
558 case NC_NOFLAG_C:
559 break;
560 case NC_VISIBLE_C:
561 ncp->nc_flag |= NC_VISIBLE;
562 break;
563 case NC_BROADCAST_C:
564 ncp->nc_flag |= NC_BROADCAST;
565 break;
566 default:
567 return (-1);
568 }
569 }
570 /* protocol family */
571 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
572 return (-1);
573 }
574 /* protocol name */
575 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
576 return (-1);
577 }
578 /* network device */
579 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
580 return (-1);
581 }
582 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
583 return (-1);
584 }
585 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
586 ncp->nc_nlookups = 0;
587 ncp->nc_lookups = NULL;
588 } else {
589 char *cp; /* tmp string */
590
591 if (ncp->nc_lookups != NULL) /* from last visit */
592 free(ncp->nc_lookups);
593 /* preallocate one string pointer */
594 ncp->nc_lookups = (char **)malloc(sizeof (char *));
595 ncp->nc_nlookups = 0;
596 while ((cp = tokenp) != NULL) {
597 tokenp = _get_next_token(cp, ',');
598 ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
599 ncp->nc_lookups = (char **)realloc(ncp->nc_lookups,
600 (size_t)(ncp->nc_nlookups+1) *sizeof(char *)); /* for next loop */
601 }
602 }
603 return (0);
604 }
605
606
607 /*
608 * Returns a string describing the reason for failure.
609 */
610 char *
611 nc_sperror()
612 {
613 char *message;
614
615 switch(nc_error) {
616 case NC_NONETCONFIG:
617 message = _nc_errors[0];
618 break;
619 case NC_NOMEM:
620 message = _nc_errors[1];
621 break;
622 case NC_NOTINIT:
623 message = _nc_errors[2];
624 break;
625 case NC_BADFILE:
626 message = _nc_errors[3];
627 break;
628 default:
629 message = "Unknown network selection error";
630 }
631 return (message);
632 }
633
634 /*
635 * Prints a message onto standard error describing the reason for failure.
636 */
637 void
638 nc_perror(s)
639 const char *s;
640 {
641
642 _DIAGASSERT(s != NULL);
643
644 fprintf(stderr, "%s: %s", s, nc_sperror());
645 }
646
647 /*
648 * Duplicates the matched netconfig buffer.
649 */
650 static struct netconfig *
651 dup_ncp(ncp)
652 struct netconfig *ncp;
653 {
654 struct netconfig *p;
655 char *tmp;
656 int i;
657
658 _DIAGASSERT(ncp != NULL);
659
660 if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
661 return(NULL);
662 if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
663 free(tmp);
664 return(NULL);
665 }
666 /*
667 * First we dup all the data from matched netconfig buffer. Then we
668 * adjust some of the member pointer to a pre-allocated buffer where
669 * contains part of the data.
670 * To follow the convention used in parse_ncp(), we store all the
671 * neccessary information in the pre-allocated buffer and let each
672 * of the netconfig char pointer member point to the right address
673 * in the buffer.
674 */
675 *p = *ncp;
676 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
677 tmp = strchr(tmp, NULL) + 1;
678 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
679 tmp = strchr(tmp, NULL) + 1;
680 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
681 tmp = strchr(tmp, NULL) + 1;
682 p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
683 p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
684 if (p->nc_lookups == NULL) {
685 free(p->nc_netid);
686 return(NULL);
687 }
688 for (i=0; i < p->nc_nlookups; i++) {
689 tmp = strchr(tmp, NULL) + 1;
690 p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);
691 }
692 return(p);
693 }
694