Update.
[platform/upstream/linaro-glibc.git] / nis / nss_nisplus / nisplus-grp.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 <grp.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
27
28 #include "nss-nisplus.h"
29 #include "nisplus-parser.h"
30
31 __libc_lock_define_initialized (static, lock);
32
33 static nis_result *result = NULL;
34 static unsigned long next_entry = 0;
35 static nis_name tablename_val = NULL;
36 static u_long tablename_len = 0;
37
38 static enum nss_status
39 _nss_create_tablename (int *errnop)
40 {
41   if (tablename_val == NULL)
42     {
43       char buf [40 + strlen (nis_local_directory ())];
44       char *p;
45
46       p = __stpcpy (buf, "group.org_dir.");
47       p = __stpcpy (p, nis_local_directory ());
48       tablename_val = __strdup (buf);
49       if (tablename_val == NULL)
50         {
51           *errnop = errno;
52           return NSS_STATUS_TRYAGAIN;
53         }
54       tablename_len = strlen (tablename_val);
55     }
56   return NSS_STATUS_SUCCESS;
57 }
58
59 static enum nss_status
60 internal_setgrent (void)
61 {
62   enum nss_status status;
63   int err;
64
65   if (result)
66     nis_freeresult (result);
67   result = NULL;
68   next_entry = 0;
69
70   if (tablename_val == NULL)
71     if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
72       return NSS_STATUS_UNAVAIL;
73
74   result = nis_list (tablename_val, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
75   status = niserr2nss (result->status);
76   if (status != NSS_STATUS_SUCCESS)
77     {
78       nis_freeresult (result);
79       result = NULL;
80     }
81   return status;
82 }
83
84 enum nss_status
85 _nss_nisplus_setgrent (void)
86 {
87   enum nss_status status;
88
89   __libc_lock_lock (lock);
90
91   status = internal_setgrent ();
92
93   __libc_lock_unlock (lock);
94
95   return status;
96 }
97
98 enum nss_status
99 _nss_nisplus_endgrent (void)
100 {
101   __libc_lock_lock (lock);
102
103   if (result)
104     nis_freeresult (result);
105   result = NULL;
106
107   __libc_lock_unlock (lock);
108
109   return NSS_STATUS_SUCCESS;
110 }
111
112 static enum nss_status
113 internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
114                              int *errnop)
115 {
116   int parse_res;
117
118   if (result == NULL)
119     {
120       enum nss_status status;
121
122       status = internal_setgrent ();
123       if (result == NULL || status != NSS_STATUS_SUCCESS)
124         return status;
125     }
126
127   /* Get the next entry until we found a correct one. */
128   do
129     {
130       if (next_entry >= result->objects.objects_len)
131         return NSS_STATUS_NOTFOUND;
132
133       parse_res = _nss_nisplus_parse_grent (result, next_entry, gr,
134                                             buffer, buflen, errnop);
135       if (parse_res == -1)
136         return NSS_STATUS_TRYAGAIN;
137
138       ++next_entry;
139     }
140   while (!parse_res);
141
142   return NSS_STATUS_SUCCESS;
143 }
144
145 enum nss_status
146 _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
147                          int *errnop)
148 {
149   int status;
150
151   __libc_lock_lock (lock);
152
153   status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
154
155   __libc_lock_unlock (lock);
156
157   return status;
158 }
159
160 enum nss_status
161 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
162                          char *buffer, size_t buflen, int *errnop)
163 {
164   int parse_res;
165
166   if (tablename_val == NULL)
167     {
168       enum nss_status status = _nss_create_tablename (errnop);
169
170       if (status != NSS_STATUS_SUCCESS)
171         return status;
172     }
173
174   if (name == NULL || strlen (name) > 8)
175     return NSS_STATUS_NOTFOUND;
176   else
177     {
178       nis_result *result;
179       char buf[strlen (name) + 24 + tablename_len];
180
181       sprintf (buf, "[name=%s],%s", name, tablename_val);
182
183       result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
184
185       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
186         {
187           enum nss_status status = niserr2nss (result->status);
188
189           nis_freeresult (result);
190           return status;
191         }
192
193       parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen,
194                                             errnop);
195       nis_freeresult (result);
196
197       if (parse_res == -1)
198         {
199           *errnop = ERANGE;
200           return NSS_STATUS_TRYAGAIN;
201         }
202       if (parse_res)
203         return NSS_STATUS_SUCCESS;
204
205       return NSS_STATUS_NOTFOUND;
206     }
207 }
208
209 enum nss_status
210 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
211                          char *buffer, size_t buflen, int *errnop)
212 {
213   if (tablename_val == NULL)
214     {
215       enum nss_status status = _nss_create_tablename (errnop);
216
217       if (status != NSS_STATUS_SUCCESS)
218         return status;
219     }
220
221   {
222     int parse_res;
223     nis_result *result;
224     char buf[36 + tablename_len];
225
226     sprintf (buf, "[gid=%d],%s", gid, tablename_val);
227
228     result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
229
230     if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
231       {
232         enum nss_status status = niserr2nss (result->status);
233
234         nis_freeresult (result);
235         return status;
236       }
237
238     parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen,
239                                           errnop);
240
241     nis_freeresult (result);
242
243     if (parse_res == -1)
244       {
245         *errnop = ERANGE;
246         return NSS_STATUS_TRYAGAIN;
247       }
248
249     if (parse_res)
250       return NSS_STATUS_SUCCESS;
251
252     return NSS_STATUS_NOTFOUND;
253   }
254 }