]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - lib/libc/rpc/getnetconfig.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / lib / libc / rpc / getnetconfig.c
1 /*      $NetBSD: getnetconfig.c,v 1.3 2000/07/06 03:10:34 christos 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 #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 pthread_mutex_t nc_file_lock = PTHREAD_MUTEX_INITIALIZER;
134
135 static struct netconfig_info    ni = { 0, 0, NULL, NULL};
136 static pthread_mutex_t ni_lock = PTHREAD_MUTEX_INITIALIZER;
137
138
139 #define MAXNETCONFIGLINE    1000
140
141 static int *
142 __nc_error()
143 {
144         static pthread_mutex_t nc_lock = PTHREAD_MUTEX_INITIALIZER;
145         static thread_key_t nc_key = 0;
146         static int nc_error = 0;
147         int error, *nc_addr;
148
149         /*
150          * Use the static `nc_error' if we are the main thread
151          * (including non-threaded programs), or if an allocation
152          * fails.
153          */
154         if (thr_main())
155                 return (&nc_error);
156         if (nc_key == 0) {
157                 error = 0;
158                 mutex_lock(&nc_lock);
159                 if (nc_key == 0)
160                         error = thr_keycreate(&nc_key, free);
161                 mutex_unlock(&nc_lock);
162                 if (error)
163                         return (&nc_error);
164         }
165         if ((nc_addr = (int *)thr_getspecific(nc_key)) == NULL) {
166                 nc_addr = (int *)malloc(sizeof (int));
167                 if (thr_setspecific(nc_key, (void *) nc_addr) != 0) {
168                         if (nc_addr)
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()
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(handlep)
245 void *handlep;
246 {
247     struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
248     char *stringp;              /* tmp string pointer */
249     struct netconfig_list       *list;
250     struct netconfig *np;
251     struct netconfig *result;
252
253     /*
254      * Verify that handle is valid
255      */
256     mutex_lock(&nc_file_lock);
257     if (ncp == NULL || nc_file == NULL) {
258         nc_error = NC_NOTINIT;
259         mutex_unlock(&nc_file_lock);
260         return (NULL);
261     }
262     mutex_unlock(&nc_file_lock);
263
264     switch (ncp->valid) {
265     case NC_VALID:
266         /*
267          * If entry has already been read into the list,
268          * we return the entry in the linked list.
269          * If this is the first time call, check if there are any entries in
270          * linked list.  If no entries, we need to read the netconfig db.
271          * If we have been here and the next entry is there, we just return
272          * it.
273          */
274         if (ncp->flag == 0) {   /* first time */
275             ncp->flag = 1;
276             mutex_lock(&ni_lock);
277             ncp->nc_configs = ni.head;
278             mutex_unlock(&ni_lock);
279             if (ncp->nc_configs != NULL)        /* entry already exist */
280                 return(ncp->nc_configs->ncp);
281         }
282         else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
283             ncp->nc_configs = ncp->nc_configs->next;
284             return(ncp->nc_configs->ncp);
285         }
286
287         /*
288          * If we cannot find the entry in the list and is end of file,
289          * we give up.
290          */
291         mutex_lock(&ni_lock);
292         if (ni.eof == 1) {
293                 mutex_unlock(&ni_lock);
294                 return(NULL);
295         }
296         mutex_unlock(&ni_lock);
297
298         break;
299     default:
300         nc_error = NC_NOTINIT;
301         return (NULL);
302     }
303
304     stringp = (char *) malloc(MAXNETCONFIGLINE);
305     if (stringp == NULL)
306         return (NULL);
307
308 #ifdef MEM_CHK
309     if (malloc_verify() == 0) {
310         fprintf(stderr, "memory heap corrupted in getnetconfig\n");
311         exit(1);
312     }
313 #endif
314
315     /*
316      * Read a line from netconfig file.
317      */
318     mutex_lock(&nc_file_lock);
319     do {
320         if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
321             free(stringp);
322             mutex_lock(&ni_lock);
323             ni.eof = 1;
324             mutex_unlock(&ni_lock);
325             mutex_unlock(&nc_file_lock);
326             return (NULL);
327         }
328     } while (*stringp == '#');
329     mutex_unlock(&nc_file_lock);
330
331     list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
332     if (list == NULL) {
333         free(stringp);
334         return(NULL);
335     }
336     np = (struct netconfig *) malloc(sizeof (struct netconfig));
337     if (np == NULL) {
338         free(stringp);
339         free(list);
340         return(NULL);
341     }
342     list->ncp = np;
343     list->next = NULL;
344     list->ncp->nc_lookups = NULL;
345     list->linep = stringp;
346     if (parse_ncp(stringp, list->ncp) == -1) {
347         free(stringp);
348         free(np);
349         free(list);
350         return (NULL);
351     }
352     else {
353         /*
354          * If this is the first entry that's been read, it is the head of
355          * the list.  If not, put the entry at the end of the list.
356          * Reposition the current pointer of the handle to the last entry
357          * in the list.
358          */
359         mutex_lock(&ni_lock);
360         if (ni.head == NULL) {  /* first entry */
361             ni.head = ni.tail = list;
362         }
363         else {
364             ni.tail->next = list;
365             ni.tail = ni.tail->next;
366         }
367         ncp->nc_configs = ni.tail;
368         result = ni.tail->ncp;
369         mutex_unlock(&ni_lock);
370         return(result);
371     }
372 }
373
374 /*
375  * endnetconfig() may be called to "unbind" or "close" the netconfig database
376  * when processing is complete, releasing resources for reuse.  endnetconfig()
377  * may not be called before setnetconfig().  endnetconfig() returns 0 on
378  * success and -1 on failure (for example, if setnetconfig() was not called
379  * previously).
380  */
381 int
382 endnetconfig(handlep)
383 void *handlep;
384 {
385     struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
386
387     struct netconfig_list *q, *p;
388
389     /*
390      * Verify that handle is valid
391      */
392     if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
393             nc_handlep->valid != NC_STORAGE)) {
394         nc_error = NC_NOTINIT;
395         return (-1);
396     }
397
398     /*
399      * Return 0 if anyone still needs it.
400      */
401     nc_handlep->valid = NC_INVALID;
402     nc_handlep->flag = 0;
403     nc_handlep->nc_configs = NULL;
404     mutex_lock(&ni_lock);
405     if (--ni.ref > 0) {
406         mutex_unlock(&ni_lock);
407         free(nc_handlep);
408         return(0);
409     }
410
411     /*
412      * Noone needs these entries anymore, then frees them.
413      * Make sure all info in netconfig_info structure has been reinitialized.
414      */
415     q = p = ni.head;
416     ni.eof = ni.ref = 0;
417     ni.head = NULL;
418     ni.tail = NULL;
419     mutex_unlock(&ni_lock);
420
421     while (q) {
422         p = q->next;
423         if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
424         free(q->ncp);
425         free(q->linep);
426         free(q);
427         q = p;
428     }
429     free(nc_handlep);
430
431     mutex_lock(&nc_file_lock);
432     fclose(nc_file);
433     nc_file = NULL;
434     mutex_unlock(&nc_file_lock);
435
436     return (0);
437 }
438
439 /*
440  * getnetconfigent(netid) returns a pointer to the struct netconfig structure
441  * corresponding to netid.  It returns NULL if netid is invalid (that is, does
442  * not name an entry in the netconfig database).  It returns NULL and sets
443  * errno in case of failure (for example, if the netconfig database cannot be
444  * opened).
445  */
446
447 struct netconfig *
448 getnetconfigent(netid)
449         const char *netid;
450 {
451     FILE *file;         /* NETCONFIG db's file pointer */
452     char *linep;        /* holds current netconfig line */
453     char *stringp;      /* temporary string pointer */
454     struct netconfig *ncp = NULL;   /* returned value */
455     struct netconfig_list *list;        /* pointer to cache list */
456
457     nc_error = NC_NOTFOUND;     /* default error. */
458     if (netid == NULL || strlen(netid) == 0) {
459         return (NULL);
460     }
461
462     /*
463      * Look up table if the entries have already been read and parsed in
464      * getnetconfig(), then copy this entry into a buffer and return it.
465      * If we cannot find the entry in the current list and there are more
466      * entries in the netconfig db that has not been read, we then read the
467      * db and try find the match netid.
468      * If all the netconfig db has been read and placed into the list and
469      * there is no match for the netid, return NULL.
470      */
471     mutex_lock(&ni_lock);
472     if (ni.head != NULL) {
473         for (list = ni.head; list; list = list->next) {
474             if (strcmp(list->ncp->nc_netid, netid) == 0) {
475                 mutex_unlock(&ni_lock);
476                 return(dup_ncp(list->ncp));
477             }
478         }
479         if (ni.eof == 1) {      /* that's all the entries */
480                 mutex_unlock(&ni_lock);
481                 return(NULL);
482         }
483     }
484     mutex_unlock(&ni_lock);
485
486
487     if ((file = fopen(NETCONFIG, "r")) == NULL) {
488         nc_error = NC_NONETCONFIG;
489         return (NULL);
490     }
491
492     if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
493         fclose(file);
494         nc_error = NC_NOMEM;
495         return (NULL);
496     }
497     do {
498         ptrdiff_t len;
499         char *tmpp;     /* tmp string pointer */
500
501         do {
502             if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
503                 break;
504             }
505         } while (*stringp == '#');
506         if (stringp == NULL) {      /* eof */
507             break;
508         }
509         if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */
510             nc_error = NC_BADFILE;
511             break;
512         }
513         if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */
514                 strncmp(stringp, netid, (size_t)len) == 0) {
515             if ((ncp = (struct netconfig *)
516                     malloc(sizeof (struct netconfig))) == NULL) {
517                 break;
518             }
519             ncp->nc_lookups = NULL;
520             if (parse_ncp(linep, ncp) == -1) {
521                 free(ncp);
522                 ncp = NULL;
523             }
524             break;
525         }
526     } while (stringp != NULL);
527     if (ncp == NULL) {
528         free(linep);
529     }
530     fclose(file);
531     return(ncp);
532 }
533
534 /*
535  * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
536  * netconfigp (previously returned by getnetconfigent()).
537  */
538
539 void
540 freenetconfigent(netconfigp)
541         struct netconfig *netconfigp;
542 {
543     if (netconfigp != NULL) {
544         free(netconfigp->nc_netid);     /* holds all netconfigp's strings */
545         if (netconfigp->nc_lookups != NULL)
546             free(netconfigp->nc_lookups);
547         free(netconfigp);
548     }
549     return;
550 }
551
552 /*
553  * Parse line and stuff it in a struct netconfig
554  * Typical line might look like:
555  *      udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
556  *
557  * We return -1 if any of the tokens don't parse, or malloc fails.
558  *
559  * Note that we modify stringp (putting NULLs after tokens) and
560  * we set the ncp's string field pointers to point to these tokens within
561  * stringp.
562  */
563
564 static int
565 parse_ncp(stringp, ncp)
566 char *stringp;          /* string to parse */
567 struct netconfig *ncp;  /* where to put results */
568 {
569     char    *tokenp;    /* for processing tokens */
570     char    *lasts;
571     char    **nc_lookups;
572
573     nc_error = NC_BADFILE;      /* nearly anything that breaks is for this reason */
574     stringp[strlen(stringp)-1] = '\0';  /* get rid of newline */
575     /* netid */
576     if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
577         return (-1);
578     }
579
580     /* semantics */
581     if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
582         return (-1);
583     }
584     if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
585         ncp->nc_semantics = NC_TPI_COTS_ORD;
586     else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
587         ncp->nc_semantics = NC_TPI_COTS;
588     else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
589         ncp->nc_semantics = NC_TPI_CLTS;
590     else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
591         ncp->nc_semantics = NC_TPI_RAW;
592     else
593         return (-1);
594
595     /* flags */
596     if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
597         return (-1);
598     }
599     for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
600             tokenp++) {
601         switch (*tokenp) {
602         case NC_NOFLAG_C:
603             break;
604         case NC_VISIBLE_C:
605             ncp->nc_flag |= NC_VISIBLE;
606             break;
607         case NC_BROADCAST_C:
608             ncp->nc_flag |= NC_BROADCAST;
609             break;
610         default:
611             return (-1);
612         }
613     }
614     /* protocol family */
615     if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
616         return (-1);
617     }
618     /* protocol name */
619     if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
620         return (-1);
621     }
622     /* network device */
623     if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
624         return (-1);
625     }
626     if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
627         return (-1);
628     }
629     if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
630         ncp->nc_nlookups = 0;
631         ncp->nc_lookups = NULL;
632     } else {
633         char *cp;           /* tmp string */
634
635         if (ncp->nc_lookups != NULL)    /* from last visit */
636             free(ncp->nc_lookups);
637         ncp->nc_lookups = NULL;
638         ncp->nc_nlookups = 0;
639         while ((cp = tokenp) != NULL) {
640             if ((nc_lookups = realloc(ncp->nc_lookups,
641                 (ncp->nc_nlookups + 1) * sizeof *ncp->nc_lookups)) == NULL) {
642                     free(ncp->nc_lookups);
643                     ncp->nc_lookups = NULL;
644                     return (-1);
645             }
646             tokenp = _get_next_token(cp, ',');
647             ncp->nc_lookups = nc_lookups;
648             ncp->nc_lookups[ncp->nc_nlookups++] = cp;
649         }
650     }
651     return (0);
652 }
653
654
655 /*
656  * Returns a string describing the reason for failure.
657  */
658 char *
659 nc_sperror()
660 {
661     const char *message;
662
663     switch(nc_error) {
664     case NC_NONETCONFIG:
665         message = _nc_errors[0];
666         break;
667     case NC_NOMEM:
668         message = _nc_errors[1];
669         break;
670     case NC_NOTINIT:
671         message = _nc_errors[2];
672         break;
673     case NC_BADFILE:
674         message = _nc_errors[3];
675         break;
676     case NC_NOTFOUND:
677         message = _nc_errors[4];
678         break;
679     default:
680         message = "Unknown network selection error";
681     }
682     /* LINTED const castaway */
683     return ((char *)message);
684 }
685
686 /*
687  * Prints a message onto standard error describing the reason for failure.
688  */
689 void
690 nc_perror(s)
691         const char *s;
692 {
693     fprintf(stderr, "%s: %s\n", s, nc_sperror());
694 }
695
696 /*
697  * Duplicates the matched netconfig buffer.
698  */
699 static struct netconfig *
700 dup_ncp(ncp)
701 struct netconfig        *ncp;
702 {
703     struct netconfig    *p;
704     char        *tmp;
705     u_int       i;
706
707     if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
708         return(NULL);
709     if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
710         free(tmp);
711         return(NULL);
712     }
713     /*
714      * First we dup all the data from matched netconfig buffer.  Then we
715      * adjust some of the member pointer to a pre-allocated buffer where
716      * contains part of the data.
717      * To follow the convention used in parse_ncp(), we store all the
718      * necessary information in the pre-allocated buffer and let each
719      * of the netconfig char pointer member point to the right address
720      * in the buffer.
721      */
722     *p = *ncp;
723     p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
724     tmp = strchr(tmp, '\0') + 1;
725     p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
726     tmp = strchr(tmp, '\0') + 1;
727     p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
728     tmp = strchr(tmp, '\0') + 1;
729     p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
730     p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
731     if (p->nc_lookups == NULL) {
732         free(p->nc_netid);
733         free(p);
734         return(NULL);
735     }
736     for (i=0; i < p->nc_nlookups; i++) {
737         tmp = strchr(tmp, '\0') + 1;
738         p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);
739     }
740     return(p);
741 }