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