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