6feae632585c440d6d23964f1b4402c35dc7c09e
[platform/upstream/linaro-glibc.git] / nis / nss_nis / nis-proto.c
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
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 <netdb.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28
29 #include "nss-nis.h"
30
31 __libc_lock_define_initialized (static, lock)
32
33 static bool_t new_start = 1;
34 static char *oldkey = NULL;
35 static int oldkeylen = 0;
36
37 enum nss_status
38 _nss_nis_setprotoent (void)
39 {
40   __libc_lock_lock (lock);
41
42   new_start = 1;
43   if (oldkey != NULL)
44     {
45       free (oldkey);
46       oldkey = NULL;
47       oldkeylen = 0;
48     }
49
50   __libc_lock_unlock (lock);
51
52   return NSS_STATUS_SUCCESS;
53 }
54
55 enum nss_status
56 _nss_nis_endprotoent (void)
57 {
58   __libc_lock_lock (lock);
59
60   new_start = 1;
61   if (oldkey != NULL)
62     {
63       free (oldkey);
64       oldkey = NULL;
65       oldkeylen = 0;
66     }
67
68   __libc_lock_unlock (lock);
69
70   return NSS_STATUS_SUCCESS;
71 }
72
73 static enum nss_status
74 internal_nis_getprotoent_r (struct protoent *proto,
75                             char *buffer, size_t buflen)
76 {
77   char *domain, *result, *outkey;
78   int len, keylen, parse_res;
79
80   if (yp_get_default_domain (&domain))
81     return NSS_STATUS_UNAVAIL;
82
83   /* Get the next entry until we found a correct one. */
84   do
85     {
86       enum nss_status retval;
87       char *p;
88
89       if (new_start)
90         retval = yperr2nss (yp_first (domain, "protocols.bynumber",
91                                       &outkey, &keylen, &result, &len));
92       else
93         retval = yperr2nss ( yp_next (domain, "protocols.bynumber",
94                                       oldkey, oldkeylen,
95                                       &outkey, &keylen, &result, &len));
96
97       if (retval != NSS_STATUS_SUCCESS)
98         {
99           if (retval == NSS_STATUS_TRYAGAIN)
100             __set_errno (EAGAIN);
101           return retval;
102         }
103
104       if ((size_t) (len + 1) > buflen)
105         {
106           free (result);
107           __set_errno (ERANGE);
108           return NSS_STATUS_TRYAGAIN;
109         }
110
111       p = strncpy (buffer, result, len);
112       buffer[len] = '\0';
113       while (isspace (*p))
114         ++p;
115       free (result);
116
117       parse_res = _nss_files_parse_protoent (p, proto, buffer, buflen);
118       if (!parse_res && errno == ERANGE)
119         return NSS_STATUS_TRYAGAIN;
120
121       free (oldkey);
122       oldkey = outkey;
123       oldkeylen = keylen;
124       new_start = 0;
125     }
126   while (!parse_res);
127
128   return NSS_STATUS_SUCCESS;
129 }
130
131 enum nss_status
132 _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen)
133 {
134   enum nss_status status;
135
136   __libc_lock_lock (lock);
137
138   status = internal_nis_getprotoent_r (proto, buffer, buflen);
139
140   __libc_lock_unlock (lock);
141
142   return status;
143 }
144
145 enum nss_status
146 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
147                            char *buffer, size_t buflen)
148 {
149   enum nss_status retval;
150   char *domain, *result, *p;
151   int len, parse_res;
152
153   if (name == NULL)
154     {
155       __set_errno (EINVAL);
156       return NSS_STATUS_UNAVAIL;
157     }
158
159   if (yp_get_default_domain (&domain))
160     return NSS_STATUS_UNAVAIL;
161
162   retval = yperr2nss (yp_match (domain, "protocols.byname", name,
163                                 strlen (name), &result, &len));
164
165   if (retval != NSS_STATUS_SUCCESS)
166     {
167       if (retval == NSS_STATUS_TRYAGAIN)
168         __set_errno (EAGAIN);
169       return retval;
170     }
171
172   if ((size_t) (len + 1) > buflen)
173     {
174       free (result);
175       __set_errno (ERANGE);
176       return NSS_STATUS_TRYAGAIN;
177     }
178
179   p = strncpy (buffer, result, len);
180   buffer[len] = '\0';
181   while (isspace (*p))
182     ++p;
183   free (result);
184
185   parse_res = _nss_files_parse_protoent (p, proto, buffer, buflen);
186
187   if (!parse_res)
188     {
189       if (errno == ERANGE)
190         return NSS_STATUS_TRYAGAIN;
191       else
192         return NSS_STATUS_NOTFOUND;
193     }
194   else
195     return NSS_STATUS_SUCCESS;
196 }
197
198 enum nss_status
199 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
200                              char *buffer, size_t buflen)
201 {
202   enum nss_status retval;
203   char *domain, *result, *p;
204   int len, nlen, parse_res;
205   char buf[32];
206
207   if (yp_get_default_domain (&domain))
208     return NSS_STATUS_UNAVAIL;
209
210   nlen = sprintf (buf, "%d", number);
211
212   retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf,
213                                 nlen, &result, &len));
214
215   if (retval != NSS_STATUS_SUCCESS)
216     {
217       if (retval == NSS_STATUS_TRYAGAIN)
218         __set_errno (EAGAIN);
219       return retval;
220     }
221
222   if ((size_t) (len + 1) > buflen)
223     {
224       free (result);
225       __set_errno (ERANGE);
226       return NSS_STATUS_TRYAGAIN;
227     }
228
229   p = strncpy (buffer, result, len);
230   buffer[len] = '\0';
231   while (isspace (*p))
232     ++p;
233   free (result);
234
235   parse_res = _nss_files_parse_protoent (p, proto, buffer, buflen);
236
237   if (!parse_res)
238     {
239       if (errno == ERANGE)
240         return NSS_STATUS_TRYAGAIN;
241       else
242         return NSS_STATUS_NOTFOUND;
243     }
244   else
245     return NSS_STATUS_SUCCESS;
246 }