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