Update.
[platform/upstream/linaro-glibc.git] / nis / nss_nisplus / nisplus-proto.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 <netdb.h>
24 #include <string.h>
25 #include <bits/libc-lock.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_protoent (nis_result * result, struct protoent *proto,
44                              char *buffer, size_t buflen, int *errnop)
45 {
46   char *first_unused = buffer;
47   size_t room_left = buflen;
48   unsigned int i;
49   char *p, *line;
50
51   if (result == NULL)
52     return 0;
53
54   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
55       || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
56       || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
57                  "protocols_tbl") != 0
58       || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
59     return 0;
60
61   /* Generate the protocols entry format and use the normal parser */
62   if (NISENTRYLEN (0, 0, result) + 1 > room_left)
63     {
64     no_more_room:
65       *errnop = 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   proto->p_name = first_unused;
72   room_left -= (strlen (first_unused) +1);
73   first_unused += strlen (first_unused) +1;
74
75
76   if (NISENTRYLEN (0, 2, result) + 1 > room_left)
77     goto no_more_room;
78   proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
79   p = first_unused;
80
81   line = p;
82   for (i = 0; i < result->objects.objects_len; i++)
83     {
84       if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
85         {
86           if (NISENTRYLEN (i, 1, result) + 2 > room_left)
87             goto no_more_room;
88           *p++ = ' ';
89           p = __stpncpy (p, NISENTRYVAL (i, 1, result),
90                          NISENTRYLEN (i, 1, result));
91           *p = '\0';
92           room_left -= (NISENTRYLEN (i, 1, result) + 1);
93         }
94     }
95   ++p;
96   first_unused = p;
97
98   /* Adjust the pointer so it is aligned for
99      storing pointers.  */
100   first_unused += __alignof__ (char *) - 1;
101   first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
102   proto->p_aliases = (char **) first_unused;
103   if (room_left < sizeof (char *))
104     goto no_more_room;
105   room_left -= sizeof (char *);
106   proto->p_aliases[0] = NULL;
107
108   i = 0;
109   while (*line != '\0')
110     {
111       /* Skip leading blanks.  */
112       while (isspace (*line))
113         line++;
114       if (*line == '\0')
115         break;
116
117       if (room_left < sizeof (char *))
118         goto no_more_room;
119
120       room_left -= sizeof (char *);
121       proto->p_aliases[i] = line;
122
123       while (*line != '\0' && *line != ' ')
124         ++line;
125
126       if (*line == ' ')
127         {
128           *line = '\0';
129           ++line;
130           ++i;
131         }
132       else
133         proto->p_aliases[i+1] = NULL;
134     }
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       char buf [40 + strlen (nis_local_directory ())];
145       char *p;
146
147       p = __stpcpy (buf, "protocols.org_dir.");
148       p = __stpcpy (p, nis_local_directory ());
149       tablename_val = __strdup (buf);
150       if (tablename_val == NULL)
151         {
152           *errnop = errno;
153           return NSS_STATUS_TRYAGAIN;
154         }
155       tablename_len = strlen (tablename_val);
156     }
157   return NSS_STATUS_SUCCESS;
158 }
159
160 enum nss_status
161 _nss_nisplus_setprotoent (void)
162 {
163   enum nss_status status = NSS_STATUS_SUCCESS;
164
165   __libc_lock_lock (lock);
166
167   if (result)
168     nis_freeresult (result);
169   result = NULL;
170
171   if (tablename_val == NULL)
172     {
173       int err;
174       status = _nss_create_tablename (&err);
175     }
176
177   __libc_lock_unlock (lock);
178
179   return status;
180 }
181
182 enum nss_status
183 _nss_nisplus_endprotoent (void)
184 {
185   __libc_lock_lock (lock);
186
187   if (result)
188     nis_freeresult (result);
189   result = NULL;
190
191   __libc_lock_unlock (lock);
192
193   return NSS_STATUS_SUCCESS;
194 }
195
196 static enum nss_status
197 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
198                                 size_t buflen, int *errnop)
199 {
200   int parse_res;
201
202   /* Get the next entry until we found a correct one. */
203   do
204     {
205       nis_result *saved_res;
206
207       if (result == NULL)
208         {
209           saved_res = NULL;
210           if (tablename_val == NULL)
211             {
212               enum nss_status status = _nss_create_tablename (errnop);
213
214               if (status != NSS_STATUS_SUCCESS)
215                 return status;
216             }
217
218           result = nis_first_entry (tablename_val);
219           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
220             return niserr2nss (result->status);
221         }
222       else
223         {
224           nis_result *res;
225
226           saved_res = result;
227           res = nis_next_entry (tablename_val, &result->cookie);
228           result = res;
229
230           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
231             {
232               nis_freeresult (saved_res);
233               return niserr2nss (result->status);
234             }
235         }
236
237       parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
238                                                buflen, errnop);
239       if (parse_res == -1)
240         {
241           nis_freeresult (result);
242           result = saved_res;
243           *errnop = ERANGE;
244           return NSS_STATUS_TRYAGAIN;
245         }
246       else
247         {
248           if (saved_res)
249             nis_freeresult (saved_res);
250         }
251     }
252   while (!parse_res);
253
254   return NSS_STATUS_SUCCESS;
255 }
256
257 enum nss_status
258 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
259                             size_t buflen, int *errnop)
260 {
261   int status;
262
263   __libc_lock_lock (lock);
264
265   status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
266
267   __libc_lock_unlock (lock);
268
269   return status;
270 }
271
272 enum nss_status
273 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
274                                char *buffer, size_t buflen, int *errnop)
275 {
276   int parse_res;
277
278   if (tablename_val == NULL)
279     {
280       enum nss_status status = _nss_create_tablename (errnop);
281
282       if (status != NSS_STATUS_SUCCESS)
283         return status;
284     }
285
286   if (name == NULL)
287     return NSS_STATUS_NOTFOUND;
288   else
289     {
290       nis_result *result;
291       char buf[strlen (name) + 255 + tablename_len];
292
293       /* Search at first in the alias list, and use the correct name
294          for the next search */
295       sprintf (buf, "[name=%s],%s", name, tablename_val);
296       result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
297
298       /* If we do not find it, try it as original name. But if the
299          database is correct, we should find it in the first case, too */
300       if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
301           || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
302           || strcmp (result->objects.objects_val->EN_data.en_type,
303                      "protocols_tbl") != 0
304           || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
305         sprintf (buf, "[cname=%s],%s", name, tablename_val);
306       else
307         sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
308                  tablename_val);
309
310       nis_freeresult (result);
311       result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
312
313       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
314         {
315           enum nss_status status = niserr2nss (result->status);
316
317           nis_freeresult (result);
318           return status;
319         }
320
321       parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
322                                                errnop);
323
324       nis_freeresult (result);
325
326       if (parse_res == -1)
327         {
328           *errnop = ERANGE;
329           return NSS_STATUS_TRYAGAIN;
330         }
331       if (parse_res)
332         return NSS_STATUS_SUCCESS;
333
334       return NSS_STATUS_NOTFOUND;
335     }
336 }
337
338 enum nss_status
339 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
340                                  char *buffer, size_t buflen, int *errnop)
341 {
342   if (tablename_val == NULL)
343     {
344       enum nss_status status = _nss_create_tablename (errnop);
345
346       if (status != NSS_STATUS_SUCCESS)
347         return status;
348     }
349
350   {
351     int parse_res;
352     nis_result *result;
353     char buf[46 + tablename_len];
354
355     sprintf (buf, "[number=%d],%s", number, tablename_val);
356
357     result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
358
359     if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
360       {
361         enum nss_status status = niserr2nss (result->status);
362
363         nis_freeresult (result);
364         return status;
365       }
366
367     parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
368                                              errnop);
369
370     nis_freeresult (result);
371
372     if (parse_res == -1)
373       {
374         *errnop = ERANGE;
375         return NSS_STATUS_TRYAGAIN;
376       }
377
378     if (parse_res)
379       return NSS_STATUS_SUCCESS;
380
381     return NSS_STATUS_NOTFOUND;
382   }
383 }