Update.
[platform/upstream/glibc.git] / nis / nss_nisplus / nisplus-ethers.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 <string.h>
24 #include <bits/libc-lock.h>
25 #include <netdb.h>
26 #include <netinet/ether.h>
27 #include <rpcsvc/nis.h>
28 #include <netinet/if_ether.h>
29
30 #include "nss-nisplus.h"
31
32 __libc_lock_define_initialized (static, lock)
33
34 static nis_result *result = NULL;
35 static nis_name tablename_val = NULL;
36 static u_long tablename_len = 0;
37
38 /* Because the `ethers' lookup does not fit so well in the scheme so
39    we define a dummy struct here which helps us to use the available
40    functions.  */
41 struct etherent
42 {
43   const char *e_name;
44   struct ether_addr e_addr;
45 };
46 struct etherent_data {};
47
48 #define NISENTRYVAL(idx,col,res) \
49         ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
50
51 #define NISENTRYLEN(idx,col,res) \
52         ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
53
54 static int
55 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
56                            char *buffer, size_t buflen)
57 {
58   char *p = buffer;
59   size_t room_left = buflen;
60
61   if (result == NULL)
62     return 0;
63
64   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
65       result->objects.objects_len != 1 ||
66       __type_of (NIS_RES_OBJECT (result)) != ENTRY_OBJ ||
67       strcmp(NIS_RES_OBJECT (result)->EN_data.en_type,
68              "ethers_tbl") != 0 ||
69       NIS_RES_OBJECT(result)->EN_data.en_cols.en_cols_len < 2)
70     return 0;
71
72   /* Generate the ether entry format and use the normal parser */
73   if (NISENTRYLEN (0, 0, result) +1 > room_left)
74     {
75       __set_errno (ERANGE);
76       return -1;
77     }
78   strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
79   room_left -= (NISENTRYLEN (0, 0, result) +1);
80   ether->e_name = p;
81
82   ether->e_addr = *ether_aton (NISENTRYVAL (0, 1, result));
83
84   return 1;
85 }
86
87 static enum nss_status
88 _nss_create_tablename (void)
89 {
90   if (tablename_val == NULL)
91     {
92       char buf [40 + strlen (nis_local_directory ())];
93       char *p;
94
95       p = __stpcpy (buf, "ethers.org_dir.");
96       p = __stpcpy (p, nis_local_directory ());
97       tablename_val = __strdup (buf);
98       if (tablename_val == NULL)
99         return NSS_STATUS_TRYAGAIN;
100       tablename_len = strlen (tablename_val);
101     }
102   return NSS_STATUS_SUCCESS;
103 }
104
105
106 enum nss_status
107 _nss_nisplus_setetherent (void)
108 {
109   enum nss_status status;
110
111   status = NSS_STATUS_SUCCESS;
112
113   __libc_lock_lock (lock);
114
115   if (result)
116     nis_freeresult (result);
117   result = NULL;
118
119   if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
120     status = NSS_STATUS_UNAVAIL;
121
122   __libc_lock_unlock (lock);
123
124   return NSS_STATUS_SUCCESS;
125 }
126
127 enum nss_status
128 _nss_nisplus_endetherent (void)
129 {
130   __libc_lock_lock (lock);
131
132   if (result)
133     nis_freeresult (result);
134   result = NULL;
135
136   __libc_lock_unlock (lock);
137
138   return NSS_STATUS_SUCCESS;
139 }
140
141 static enum nss_status
142 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
143                                 size_t buflen)
144 {
145   int parse_res;
146
147   if (tablename_val == NULL)
148     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
149       return NSS_STATUS_UNAVAIL;
150
151   /* Get the next entry until we found a correct one. */
152   do
153     {
154       nis_result *saved_result;
155
156       if (result == NULL)
157         {
158           saved_result = NULL;
159           result = nis_first_entry(tablename_val);
160           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
161             return niserr2nss (result->status);
162         }
163       else
164         {
165           nis_result *res2;
166
167           res2 = nis_next_entry(tablename_val, &result->cookie);
168           saved_result = result;
169           result = res2;
170           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
171             {
172               nis_freeresult (saved_result);
173               return niserr2nss (result->status);
174             }
175         }
176
177       if ((parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
178                                                     buflen)) == -1)
179         {
180           nis_freeresult (result);
181           result = saved_result;
182           return NSS_STATUS_TRYAGAIN;
183         }
184       else
185         {
186           if (saved_result != NULL)
187             nis_freeresult (saved_result);
188         }
189
190     } while (!parse_res);
191
192   return NSS_STATUS_SUCCESS;
193 }
194
195 enum nss_status
196 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
197                             size_t buflen)
198 {
199   int status;
200
201   __libc_lock_lock (lock);
202
203   status = internal_nisplus_getetherent_r (result, buffer, buflen);
204
205   __libc_lock_unlock (lock);
206
207   return status;
208 }
209
210 enum nss_status
211 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
212                            char *buffer, size_t buflen)
213 {
214   int parse_res;
215
216   if (tablename_val == NULL)
217     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
218       return NSS_STATUS_UNAVAIL;
219
220   if (name != NULL)
221     {
222       nis_result *result;
223       char buf[strlen (name) + 40 + tablename_len];
224
225       sprintf(buf, "[name=%s],%s", name, tablename_val);
226
227       result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
228
229       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
230         {
231           enum nss_status status = niserr2nss (result->status);
232           nis_freeresult (result);
233           return status;
234         }
235
236       if ((parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
237                                                     buflen)) == -1)
238         {
239           nis_freeresult (result);
240           return NSS_STATUS_TRYAGAIN;
241         }
242
243       if (parse_res)
244         return NSS_STATUS_SUCCESS;
245     }
246   return NSS_STATUS_NOTFOUND;
247 }
248
249 enum nss_status
250 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
251                            struct etherent *eth,
252                            char *buffer, size_t buflen)
253 {
254   if (tablename_val == NULL)
255     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
256       return NSS_STATUS_UNAVAIL;
257
258   if (addr == NULL)
259     {
260       __set_errno (EINVAL);
261       return NSS_STATUS_UNAVAIL;
262     }
263   else
264     {
265       int parse_res;
266       nis_result *result;
267       char buf[255 + tablename_len];
268
269       memset (&buf, '\0', sizeof (buf));
270       sprintf(buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
271               addr->ether_addr_octet[0], addr->ether_addr_octet[1],
272               addr->ether_addr_octet[2], addr->ether_addr_octet[3],
273               addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
274
275       result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
276
277       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
278         {
279           enum nss_status status = niserr2nss (result->status);
280           nis_freeresult (result);
281           return status;
282         }
283
284       if ((parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
285                                                     buflen)) == -1)
286         {
287           nis_freeresult (result);
288           return NSS_STATUS_TRYAGAIN;
289         }
290
291       if (parse_res)
292         return NSS_STATUS_SUCCESS;
293     }
294   return NSS_STATUS_NOTFOUND;
295 }