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