chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nis / nss_nisplus / nisplus-proto.c
1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <atomic.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <nss.h>
26 #include <string.h>
27 #include <rpcsvc/nis.h>
28 #include <bits/libc-lock.h>
29
30 #include "nss-nisplus.h"
31
32 __libc_lock_define_initialized (static, lock)
33
34 static nis_result *result;
35 static nis_name tablename_val;
36 static u_long tablename_len;
37
38 #define NISENTRYVAL(idx, col, res) \
39         (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
40
41 #define NISENTRYLEN(idx, col, res) \
42         (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
43
44
45 static int
46 _nss_nisplus_parse_protoent (nis_result *result, struct protoent *proto,
47                              char *buffer, size_t buflen, int *errnop)
48 {
49   char *first_unused = buffer;
50   size_t room_left = buflen;
51   unsigned int i;
52
53   if (result == NULL)
54     return 0;
55
56   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
57       || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
58       || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
59                  "protocols_tbl") != 0
60       || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
61     return 0;
62
63   /* Generate the protocols entry format and use the normal parser */
64   if (NISENTRYLEN (0, 0, result) + 1 > room_left)
65     {
66     no_more_room:
67       *errnop = ERANGE;
68       return -1;
69     }
70   strncpy (first_unused, NISENTRYVAL (0, 0, result),
71            NISENTRYLEN (0, 0, result));
72   first_unused[NISENTRYLEN (0, 0, result)] = '\0';
73   proto->p_name = first_unused;
74   size_t len = strlen (first_unused) + 1;
75   room_left -= len;
76   first_unused += len;
77
78
79   proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
80
81   /* XXX Rewrite at some point to allocate the array first and then
82      copy the strings.  It wasteful to first concatenate the strings
83      to just split them again later.  */
84   char *line = first_unused;
85   for (i = 0; i < NIS_RES_NUMOBJ (result); ++i)
86     {
87       if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
88         {
89           if (NISENTRYLEN (i, 1, result) + 2 > room_left)
90             goto no_more_room;
91           *first_unused++ = ' ';
92           first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
93                                     NISENTRYLEN (i, 1, result));
94           room_left -= NISENTRYLEN (i, 1, result) + 1;
95         }
96     }
97   *first_unused++ = '\0';
98
99   /* Adjust the pointer so it is aligned for
100      storing pointers.  */
101   size_t adjust = ((__alignof__ (char *)
102                     - (first_unused - (char *) 0) % __alignof__ (char *))
103                    % __alignof__ (char *));
104   if (room_left < adjust + sizeof (char *))
105     goto no_more_room;
106   first_unused += adjust;
107   room_left -= adjust;
108   proto->p_aliases = (char **) first_unused;
109
110   /* For the terminating NULL pointer.  */
111   room_left -= sizeof (char *);
112
113   i = 0;
114   while (*line != '\0')
115     {
116       /* Skip leading blanks.  */
117       while (isspace (*line))
118         line++;
119       if (*line == '\0')
120         break;
121
122       if (room_left < sizeof (char *))
123         goto no_more_room;
124
125       room_left -= sizeof (char *);
126       proto->p_aliases[i++] = line;
127
128       while (*line != '\0' && *line != ' ')
129         ++line;
130
131       if (*line == ' ')
132         *line++ = '\0';
133     }
134   proto->p_aliases[i] = NULL;
135
136   return 1;
137 }
138
139 static enum nss_status
140 _nss_create_tablename (int *errnop)
141 {
142   if (tablename_val == NULL)
143     {
144       const char *local_dir = nis_local_directory ();
145       size_t local_dir_len = strlen (local_dir);
146       static const char prefix[] = "protocols.org_dir.";
147
148       char *p = malloc (sizeof (prefix) + local_dir_len);
149       if (p == NULL)
150         {
151           *errnop = errno;
152           return NSS_STATUS_TRYAGAIN;
153         }
154
155       memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
156
157       tablename_len = sizeof (prefix) - 1 + local_dir_len;
158
159       atomic_write_barrier ();
160
161       tablename_val = p;
162     }
163
164   return NSS_STATUS_SUCCESS;
165 }
166
167 enum nss_status
168 _nss_nisplus_setprotoent (int stayopen)
169 {
170   enum nss_status status = NSS_STATUS_SUCCESS;
171
172   __libc_lock_lock (lock);
173
174   if (result != NULL)
175     {
176       nis_freeresult (result);
177       result = NULL;
178     }
179
180   if (tablename_val == NULL)
181     {
182       int err;
183       status = _nss_create_tablename (&err);
184     }
185
186   __libc_lock_unlock (lock);
187
188   return status;
189 }
190
191 enum nss_status
192 _nss_nisplus_endprotoent (void)
193 {
194   __libc_lock_lock (lock);
195
196   if (result != NULL)
197     {
198       nis_freeresult (result);
199       result = NULL;
200     }
201
202   __libc_lock_unlock (lock);
203
204   return NSS_STATUS_SUCCESS;
205 }
206
207 static enum nss_status
208 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
209                                 size_t buflen, int *errnop)
210 {
211   int parse_res;
212
213   /* Get the next entry until we found a correct one. */
214   do
215     {
216       nis_result *saved_res;
217
218       if (result == NULL)
219         {
220           saved_res = NULL;
221           if (tablename_val == NULL)
222             {
223               enum nss_status status = _nss_create_tablename (errnop);
224
225               if (status != NSS_STATUS_SUCCESS)
226                 return status;
227             }
228
229           result = nis_first_entry (tablename_val);
230           if (result == NULL)
231             {
232               *errnop = errno;
233               return NSS_STATUS_TRYAGAIN;
234             }
235           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
236             return niserr2nss (result->status);
237         }
238       else
239         {
240           saved_res = result;
241           result = nis_next_entry (tablename_val, &result->cookie);
242           if (result == NULL)
243             {
244               *errnop = errno;
245               return NSS_STATUS_TRYAGAIN;
246             }
247           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
248             {
249               nis_freeresult (saved_res);
250               return niserr2nss (result->status);
251             }
252         }
253
254       parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
255                                                buflen, errnop);
256       if (parse_res == -1)
257         {
258           nis_freeresult (result);
259           result = saved_res;
260           *errnop = ERANGE;
261           return NSS_STATUS_TRYAGAIN;
262         }
263       else
264         {
265           if (saved_res)
266             nis_freeresult (saved_res);
267         }
268     }
269   while (!parse_res);
270
271   return NSS_STATUS_SUCCESS;
272 }
273
274 enum nss_status
275 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
276                             size_t buflen, int *errnop)
277 {
278   int status;
279
280   __libc_lock_lock (lock);
281
282   status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
283
284   __libc_lock_unlock (lock);
285
286   return status;
287 }
288
289 enum nss_status
290 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
291                                char *buffer, size_t buflen, int *errnop)
292 {
293   int parse_res;
294
295   if (tablename_val == NULL)
296     {
297       __libc_lock_lock (lock);
298
299       enum nss_status status = _nss_create_tablename (errnop);
300
301       __libc_lock_unlock (lock);
302
303       if (status != NSS_STATUS_SUCCESS)
304         return status;
305     }
306
307   if (name == NULL)
308     return NSS_STATUS_NOTFOUND;
309
310   char buf[strlen (name) + 10 + tablename_len];
311   int olderr = errno;
312
313   /* Search at first in the alias list, and use the correct name
314      for the next search */
315   snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
316   nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
317
318   if (result != NULL)
319     {
320       char *bufptr = buf;
321
322       /* If we did not find it, try it as original name. But if the
323          database is correct, we should find it in the first case, too */
324       if ((result->status != NIS_SUCCESS
325            && result->status != NIS_S_SUCCESS)
326           || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
327           || strcmp (result->objects.objects_val->EN_data.en_type,
328                          "protocols_tbl") != 0
329           || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
330         snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
331       else
332         {
333           /* We need to allocate a new buffer since there is no
334              guarantee the returned name has a length limit.  */
335           const char *entryval = NISENTRYVAL (0, 0, result);
336           size_t buflen = strlen (entryval) + 10 + tablename_len;
337           bufptr = alloca (buflen);
338           snprintf (bufptr, buflen, "[cname=%s],%s",
339                     entryval, tablename_val);
340         }
341
342       nis_freeresult (result);
343       result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
344     }
345
346   if (result == NULL)
347     {
348       __set_errno (ENOMEM);
349       return NSS_STATUS_TRYAGAIN;
350     }
351
352   if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
353     {
354       enum nss_status status = niserr2nss (result->status);
355
356       __set_errno (olderr);
357
358       nis_freeresult (result);
359       return status;
360     }
361
362   parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
363                                            errnop);
364
365   nis_freeresult (result);
366
367   if (parse_res < 1)
368     {
369       if (parse_res == -1)
370         {
371           *errnop = ERANGE;
372           return NSS_STATUS_TRYAGAIN;
373         }
374       else
375         {
376           __set_errno (olderr);
377           return NSS_STATUS_NOTFOUND;
378         }
379     }
380
381   return NSS_STATUS_SUCCESS;
382 }
383
384 enum nss_status
385 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
386                                  char *buffer, size_t buflen, int *errnop)
387 {
388   if (tablename_val == NULL)
389     {
390       __libc_lock_lock (lock);
391
392       enum nss_status status = _nss_create_tablename (errnop);
393
394       __libc_lock_unlock (lock);
395
396       if (status != NSS_STATUS_SUCCESS)
397         return status;
398     }
399
400   char buf[12 + 3 * sizeof (number) + tablename_len];
401   int olderr = errno;
402
403   snprintf (buf, sizeof (buf), "[number=%d],%s", number, tablename_val);
404
405   nis_result *result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
406
407   if (result == NULL)
408     {
409       __set_errno (ENOMEM);
410       return NSS_STATUS_TRYAGAIN;
411     }
412
413   if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
414     {
415       enum nss_status status = niserr2nss (result->status);
416
417       __set_errno (olderr);
418
419       nis_freeresult (result);
420       return status;
421     }
422
423   int parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
424                                                errnop);
425
426   nis_freeresult (result);
427
428   if (parse_res < 1)
429     {
430       if (parse_res == -1)
431         {
432           *errnop = ERANGE;
433           return NSS_STATUS_TRYAGAIN;
434         }
435       else
436         {
437           __set_errno (olderr);
438           return NSS_STATUS_NOTFOUND;
439         }
440     }
441
442   return NSS_STATUS_SUCCESS;
443 }