1 /* Copyright (c) 1997-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <string.h>
19 #include <rpcsvc/nis.h>
20 #include <shlib-compat.h>
21 
22 #include "nis_xdr.h"
23 #include "nis_intern.h"
24 
25 nis_server **
nis_getservlist(const_nis_name dir)26 nis_getservlist (const_nis_name dir)
27 {
28   nis_result *res;
29   nis_server **serv;
30 
31   res = nis_lookup (dir, FOLLOW_LINKS);
32 
33   if (res != NULL && NIS_RES_STATUS (res) == NIS_SUCCESS)
34     {
35       unsigned long i;
36       nis_server *server;
37 
38       serv =
39 	malloc (sizeof (nis_server *) *
40 		(NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len + 1));
41       if (__glibc_unlikely (serv == NULL))
42 	{
43 	  nis_freeresult (res);
44 	  return NULL;
45 	}
46 
47       for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len;
48 	   ++i)
49 	{
50 	  server =
51 	    &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i];
52 	  serv[i] = calloc (1, sizeof (nis_server));
53 	  if (__glibc_unlikely (serv[i] == NULL))
54 	    {
55 	    free_all:
56 	      while (i-- > 0)
57 		{
58 		  free (serv[i]->pkey.n_bytes);
59 		  if (serv[i]->ep.ep_val != NULL)
60 		    {
61 		      unsigned long int j;
62 		      for (j = 0; j < serv[i]->ep.ep_len; ++j)
63 			{
64 			  free (serv[i]->ep.ep_val[j].proto);
65 			  free (serv[i]->ep.ep_val[j].family);
66 			  free (serv[i]->ep.ep_val[j].uaddr);
67 			}
68 		      free (serv[i]->ep.ep_val);
69 		    }
70 		  free (serv[i]->name);
71 		  free (serv[i]);
72 		}
73 
74 	      free (serv);
75 
76 	      nis_freeresult (res);
77 
78 	      return NULL;
79 	    }
80 
81 	  if (server->name != NULL)
82 	    {
83 	      serv[i]->name = strdup (server->name);
84 	      if (__glibc_unlikely (serv[i]->name == NULL))
85 		{
86 		  ++i;
87 		  goto free_all;
88 		}
89 	    }
90 
91           serv[i]->ep.ep_len = server->ep.ep_len;
92           if (serv[i]->ep.ep_len > 0)
93             {
94               unsigned long int j;
95 
96               serv[i]->ep.ep_val =
97 		malloc (server->ep.ep_len * sizeof (endpoint));
98 	      if (__glibc_unlikely (serv[i]->ep.ep_val == NULL))
99 		{
100 		  ++i;
101 		  goto free_all;
102 		}
103 
104               for (j = 0; j < serv[i]->ep.ep_len; ++j)
105                 {
106                   if (server->ep.ep_val[j].uaddr)
107                     serv[i]->ep.ep_val[j].uaddr =
108 		      strdup (server->ep.ep_val[j].uaddr);
109                   else
110                     serv[i]->ep.ep_val[j].uaddr = NULL;
111                   if (server->ep.ep_val[j].family)
112 		    serv[i]->ep.ep_val[j].family =
113 		      strdup (server->ep.ep_val[j].family);
114                   else
115                     serv[i]->ep.ep_val[j].family = NULL;
116                   if (server->ep.ep_val[j].proto)
117 		    serv[i]->ep.ep_val[j].proto =
118 		      strdup (server->ep.ep_val[j].proto);
119                   else
120 		    serv[i]->ep.ep_val[j].proto = NULL;
121                 }
122             }
123 
124           serv[i]->key_type = server->key_type;
125           serv[i]->pkey.n_len = server->pkey.n_len;
126           if (server->pkey.n_len > 0)
127             {
128               serv[i]->pkey.n_bytes = malloc (server->pkey.n_len);
129               if (__glibc_unlikely (serv[i]->pkey.n_bytes == NULL))
130 		{
131 		  ++i;
132 		  goto free_all;
133 		}
134               memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes,
135                       server->pkey.n_len);
136             }
137         }
138       serv[i] = NULL;
139     }
140   else
141     {
142       serv = malloc (sizeof (nis_server *));
143       if (__glibc_unlikely (serv != NULL))
144 	serv[0] = NULL;
145     }
146 
147   nis_freeresult (res);
148 
149   return serv;
150 }
libnsl_hidden_nolink_def(nis_getservlist,GLIBC_2_1)151 libnsl_hidden_nolink_def (nis_getservlist, GLIBC_2_1)
152 
153 void
154 nis_freeservlist (nis_server **serv)
155 {
156   int i;
157 
158   if (serv == NULL)
159     return;
160 
161   i = 0;
162   while (serv[i] != NULL)
163     {
164       xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
165       free (serv[i]);
166       ++i;
167     }
168   free (serv);
169 }
170 libnsl_hidden_nolink_def (nis_freeservlist, GLIBC_2_1)
171