Update.
[platform/upstream/glibc.git] / nis / nss_nisplus / nisplus-rpc.c
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpc/netdb.h>
26 #include <rpcsvc/nis.h>
27
28 #include "nss-nisplus.h"
29
30 __libc_lock_define_initialized (static, lock)
31
32 static nis_result *result = NULL;
33 static nis_name tablename_val = NULL;
34 static u_long tablename_len = 0;
35
36 #define NISENTRYVAL(idx,col,res) \
37         ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
38
39 #define NISENTRYLEN(idx,col,res) \
40         ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
41
42 static int
43 _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
44                            char *buffer, size_t buflen)
45 {
46   char *first_unused = buffer;
47   size_t room_left = buflen;
48   unsigned int i;
49   char *p, *line;
50
51
52   if (result == NULL)
53     return 0;
54
55   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
56       __type_of (result->objects.objects_val) != ENTRY_OBJ ||
57       strcmp(result->objects.objects_val[0].EN_data.en_type,
58              "rpc_tbl") != 0 ||
59       result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
60     return 0;
61
62   if (NISENTRYLEN (0, 0, result) >= room_left)
63     {
64     no_more_room:
65       __set_errno (ERANGE);
66       return -1;
67     }
68   strncpy (first_unused, NISENTRYVAL (0, 0, result),
69            NISENTRYLEN (0, 0, result));
70   first_unused[NISENTRYLEN (0, 0, result)] = '\0';
71   rpc->r_name = first_unused;
72   room_left -= (strlen (first_unused) +1);
73   first_unused += strlen (first_unused) +1;
74   rpc->r_number = atoi (NISENTRYVAL (0, 2, result));
75   p = first_unused;
76
77   line = p;
78   for (i = 0; i < result->objects.objects_len; i++)
79     {
80       if (strcmp (NISENTRYVAL (i, 1, result), rpc->r_name) != 0)
81         {
82           if (NISENTRYLEN (i, 1, result) + 2 > room_left)
83             goto no_more_room;
84           *p++ = ' ';
85           p = __stpncpy (p, NISENTRYVAL (i, 1, result),
86                          NISENTRYLEN (i, 1, result));
87           *p = '\0';
88           room_left -= (NISENTRYLEN (i, 1, result) + 1);
89         }
90     }
91   ++p;
92   first_unused = p;
93
94   /* Adjust the pointer so it is aligned for
95      storing pointers.  */
96   first_unused += __alignof__ (char *) - 1;
97   first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
98   rpc->r_aliases = (char **) first_unused;
99   if (room_left < sizeof (char *))
100     goto no_more_room;
101   room_left -= (sizeof (char *));
102   rpc->r_aliases[0] = NULL;
103
104   i = 0;
105   while (*line != '\0')
106     {
107       /* Skip leading blanks.  */
108       while (isspace (*line))
109         line++;
110
111       if (*line == '\0')
112         break;
113
114       if (room_left < sizeof (char *))
115         goto no_more_room;
116
117       room_left -= sizeof (char *);
118       rpc->r_aliases[i] = line;
119
120       while (*line != '\0' && *line != ' ')
121         ++line;
122
123       if (line != rpc->r_aliases[i])
124         {
125           if (*line != '\0')
126             {
127               *line = '\0';
128               ++line;
129             }
130           ++i;
131         }
132       else
133         rpc->r_aliases[i] = NULL;
134     }
135
136   return 1;
137 }
138
139 static enum nss_status
140 _nss_create_tablename (void)
141 {
142   if (tablename_val == NULL)
143     {
144       char buf [40 + strlen (nis_local_directory ())];
145       char *p;
146
147       p = __stpcpy (buf, "rpc.org_dir.");
148       p = __stpcpy (p, nis_local_directory ());
149       tablename_val = __strdup (buf);
150       if (tablename_val == NULL)
151         return NSS_STATUS_TRYAGAIN;
152       tablename_len = strlen (tablename_val);
153     }
154   return NSS_STATUS_SUCCESS;
155 }
156
157
158 enum nss_status
159 _nss_nisplus_setrpcent (void)
160 {
161   enum nss_status status = NSS_STATUS_SUCCESS;
162
163   __libc_lock_lock (lock);
164
165   if (result)
166     nis_freeresult (result);
167   result = NULL;
168
169   if (tablename_val == NULL)
170     status = _nss_create_tablename ();
171
172   __libc_lock_unlock (lock);
173
174   return status;
175 }
176
177 enum nss_status
178 _nss_nisplus_endrpcent (void)
179 {
180   __libc_lock_lock (lock);
181
182   if (result)
183     nis_freeresult (result);
184   result = NULL;
185
186   __libc_lock_unlock (lock);
187
188   return NSS_STATUS_SUCCESS;
189 }
190
191 static enum nss_status
192 internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
193                               size_t buflen)
194 {
195   int parse_res;
196
197   /* Get the next entry until we found a correct one. */
198   do
199     {
200       nis_result *saved_res;
201
202       if (result == NULL)
203         {
204           saved_res = NULL;
205           if (tablename_val == NULL)
206             if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
207               return NSS_STATUS_UNAVAIL;
208
209           result = nis_first_entry(tablename_val);
210           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
211             return niserr2nss (result->status);
212         }
213       else
214         {
215           nis_result *res;
216
217           saved_res = result;
218           res = nis_next_entry (tablename_val, &result->cookie);
219           result = res;
220           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
221             {
222               nis_freeresult (saved_res);
223               return niserr2nss (result->status);
224             }
225         }
226
227       if ((parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer,
228                                                   buflen)) == -1)
229         {
230           nis_freeresult (result);
231           result = saved_res;
232           return NSS_STATUS_TRYAGAIN;
233         }
234       else
235         {
236           if (saved_res)
237             nis_freeresult (saved_res);
238         }
239     } while (!parse_res);
240
241   return NSS_STATUS_SUCCESS;
242 }
243
244 enum nss_status
245 _nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer,
246                           size_t buflen)
247 {
248   int status;
249
250   __libc_lock_lock (lock);
251
252   status = internal_nisplus_getrpcent_r (result, buffer, buflen);
253
254   __libc_lock_unlock (lock);
255
256   return status;
257 }
258
259 enum nss_status
260 _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
261                              char *buffer, size_t buflen)
262 {
263   int parse_res;
264
265   if (tablename_val == NULL)
266     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
267       return NSS_STATUS_UNAVAIL;
268
269   if (name == NULL)
270     return NSS_STATUS_NOTFOUND;
271   else
272     {
273       nis_result *result;
274       char buf[strlen (name) + 255 + tablename_len];
275
276       /* Search at first in the alias list, and use the correct name
277          for the next search */
278       sprintf (buf, "[name=%s],%s", name, tablename_val);
279       result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
280
281       /* If we do not find it, try it as original name. But if the
282          database is correct, we should find it in the first case, too */
283       if ((result->status != NIS_SUCCESS &&
284            result->status != NIS_S_SUCCESS) ||
285           __type_of (result->objects.objects_val) != ENTRY_OBJ ||
286           strcmp (result->objects.objects_val->EN_data.en_type,
287                   "rpc_tbl") != 0 ||
288           result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
289         sprintf (buf, "[cname=%s],%s", name, tablename_val);
290       else
291         sprintf (buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
292                  tablename_val);
293
294       nis_freeresult (result);
295       result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS , NULL, NULL);
296
297       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
298         {
299           enum nss_status status = niserr2nss (result->status);
300
301           nis_freeresult (result);
302           return status;
303         }
304
305       parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
306
307       nis_freeresult (result);
308
309       if (parse_res == -1)
310         return NSS_STATUS_TRYAGAIN;
311
312       if (parse_res)
313         return NSS_STATUS_SUCCESS;
314
315       return NSS_STATUS_NOTFOUND;
316     }
317 }
318
319 enum nss_status
320 _nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc,
321                                 char *buffer, size_t buflen)
322 {
323   if (tablename_val == NULL)
324     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
325       return NSS_STATUS_UNAVAIL;
326
327   {
328     int parse_res;
329     nis_result *result;
330     char buf[100 + tablename_len];
331
332     sprintf (buf, "[number=%d],%s", number, tablename_val);
333
334     result = nis_list(buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
335
336     if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
337       {
338         enum nss_status status = niserr2nss (result->status);
339
340         nis_freeresult (result);
341         return status;
342       }
343
344     parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
345
346     nis_freeresult (result);
347
348     if (parse_res == -1)
349       return NSS_STATUS_TRYAGAIN;
350
351     if (parse_res)
352       return NSS_STATUS_SUCCESS;
353
354     return NSS_STATUS_NOTFOUND;
355   }
356 }