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