Update.
[platform/upstream/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)
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)) != ENTRY_OBJ ||
56       strcmp (NIS_RES_OBJECT (result)->EN_data.en_type, "protocols_tbl") != 0
57       || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
58     return 0;
59
60   /* Generate the protocols entry format and use the normal parser */
61   if (NISENTRYLEN (0, 0, result) + 1 > room_left)
62     {
63     no_more_room:
64       __set_errno (ERANGE);
65       return -1;
66     }
67   strncpy (first_unused, NISENTRYVAL (0, 0, result),
68            NISENTRYLEN (0, 0, result));
69   first_unused[NISENTRYLEN (0, 0, result)] = '\0';
70   proto->p_name = first_unused;
71   room_left -= (strlen (first_unused) +1);
72   first_unused += strlen (first_unused) +1;
73
74
75   if (NISENTRYLEN (0, 2, result) + 1 > room_left)
76     goto no_more_room;
77   proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
78   p = first_unused;
79
80   line = p;
81   for (i = 0; i < result->objects.objects_len; i++)
82     {
83       if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
84         {
85           if (NISENTRYLEN (i, 1, result) + 2 > room_left)
86             goto no_more_room;
87           *p++ = ' ';
88           p = __stpncpy (p, NISENTRYVAL (i, 1, result),
89                          NISENTRYLEN (i, 1, result));
90           *p = '\0';
91           room_left -= (NISENTRYLEN (i, 1, result) + 1);
92         }
93     }
94   ++p;
95   first_unused = p;
96
97   /* Adjust the pointer so it is aligned for
98      storing pointers.  */
99   first_unused += __alignof__ (char *) - 1;
100   first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
101   proto->p_aliases = (char **) first_unused;
102   if (room_left < sizeof (char *))
103     goto no_more_room;
104   room_left -= (sizeof (char *));
105   proto->p_aliases[0] = NULL;
106
107   i = 0;
108   while (*line != '\0')
109     {
110       /* Skip leading blanks.  */
111       while (isspace (*line))
112         line++;
113       if (*line == '\0')
114         break;
115
116       if (room_left < sizeof (char *))
117         goto no_more_room;
118
119       room_left -= sizeof (char *);
120       proto->p_aliases[i] = line;
121
122       while (*line != '\0' && *line != ' ')
123         ++line;
124
125       if (*line == ' ')
126         {
127           *line = '\0';
128           ++line;
129           ++i;
130         }
131       else
132         proto->p_aliases[i+1] = NULL;
133     }
134
135   return 1;
136 }
137
138 static enum nss_status
139 _nss_create_tablename (void)
140 {
141   if (tablename_val == NULL)
142     {
143       char buf [40 + strlen (nis_local_directory ())];
144       char *p;
145
146       p = __stpcpy (buf, "protocols.org_dir.");
147       p = __stpcpy (p, nis_local_directory ());
148       tablename_val = __strdup (buf);
149       if (tablename_val == NULL)
150         return NSS_STATUS_TRYAGAIN;
151       tablename_len = strlen (tablename_val);
152     }
153   return NSS_STATUS_SUCCESS;
154 }
155
156 enum nss_status
157 _nss_nisplus_setprotoent (void)
158 {
159   enum nss_status status = NSS_STATUS_SUCCESS;
160
161   __libc_lock_lock (lock);
162
163   if (result)
164     nis_freeresult (result);
165   result = NULL;
166
167   if (tablename_val == NULL)
168     status = _nss_create_tablename ();
169
170   __libc_lock_unlock (lock);
171
172   return status;
173 }
174
175 enum nss_status
176 _nss_nisplus_endprotoent (void)
177 {
178   __libc_lock_lock (lock);
179
180   if (result)
181     nis_freeresult (result);
182   result = NULL;
183
184   __libc_lock_unlock (lock);
185
186   return NSS_STATUS_SUCCESS;
187 }
188
189 static enum nss_status
190 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
191                                 size_t buflen)
192 {
193   int parse_res;
194
195   /* Get the next entry until we found a correct one. */
196   do
197     {
198       nis_result *saved_res;
199
200       if (result == NULL)
201         {
202           saved_res = NULL;
203           if (tablename_val == NULL)
204             if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
205               return NSS_STATUS_UNAVAIL;
206
207           result = nis_first_entry (tablename_val);
208           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
209             return niserr2nss (result->status);
210         }
211       else
212         {
213           nis_result *res;
214
215           saved_res = result;
216           res = nis_next_entry (tablename_val, &result->cookie);
217           result = res;
218
219           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
220             {
221               nis_freeresult (saved_res);
222               return niserr2nss (result->status);
223             }
224         }
225
226       if ((parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
227                                                     buflen)) == -1)
228         {
229           nis_freeresult (result);
230           result = saved_res;
231           return NSS_STATUS_TRYAGAIN;
232         }
233       else
234         {
235           if (saved_res)
236             nis_freeresult (saved_res);
237         }
238     }
239   while (!parse_res);
240
241   return NSS_STATUS_SUCCESS;
242 }
243
244 enum nss_status
245 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
246                             size_t buflen)
247 {
248   int status;
249
250   __libc_lock_lock (lock);
251
252   status = internal_nisplus_getprotoent_r (result, buffer, buflen);
253
254   __libc_lock_unlock (lock);
255
256   return status;
257 }
258
259 enum nss_status
260 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
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                   "protocols_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_protoent (result, proto, buffer, buflen);
306
307       nis_freeresult (result);
308
309       if (parse_res == -1)
310         return NSS_STATUS_TRYAGAIN;
311       if (parse_res)
312         return NSS_STATUS_SUCCESS;
313
314       return NSS_STATUS_NOTFOUND;
315     }
316 }
317
318 enum nss_status
319 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
320                                  char *buffer, size_t buflen)
321 {
322   if (tablename_val == NULL)
323     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
324       return NSS_STATUS_UNAVAIL;
325   {
326     int parse_res;
327     nis_result *result;
328     char buf[46 + tablename_len];
329
330     sprintf (buf, "[number=%d],%s", number, tablename_val);
331
332     result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
333
334     if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
335       {
336         enum nss_status status = niserr2nss (result->status);
337
338         nis_freeresult (result);
339         return status;
340       }
341
342     parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
343
344     nis_freeresult (result);
345
346     if (parse_res == -1)
347       return NSS_STATUS_TRYAGAIN;
348
349     if (parse_res)
350       return NSS_STATUS_SUCCESS;
351
352     return NSS_STATUS_NOTFOUND;
353   }
354 }