Update.
[platform/upstream/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 (void)
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         return NSS_STATUS_TRYAGAIN;
51       tablename_len = strlen (tablename_val);
52     }
53   return NSS_STATUS_SUCCESS;
54 }
55
56 static enum nss_status
57 internal_setgrent (void)
58 {
59   enum nss_status status;
60
61   if (result)
62     nis_freeresult (result);
63   result = NULL;
64   next_entry = 0;
65
66   if (tablename_val == NULL)
67     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
68       return NSS_STATUS_UNAVAIL;
69
70   result = nis_list (tablename_val, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
71   status = niserr2nss (result->status);
72   if (status != NSS_STATUS_SUCCESS)
73     {
74       nis_freeresult (result);
75       result = NULL;
76     }
77   return status;
78 }
79
80 enum nss_status
81 _nss_nisplus_setgrent (void)
82 {
83   enum nss_status status;
84
85   __libc_lock_lock (lock);
86
87   status = internal_setgrent ();
88
89   __libc_lock_unlock (lock);
90
91   return status;
92 }
93
94 enum nss_status
95 _nss_nisplus_endgrent (void)
96 {
97   __libc_lock_lock (lock);
98
99   if (result)
100     nis_freeresult (result);
101   result = NULL;
102
103   __libc_lock_unlock (lock);
104
105   return NSS_STATUS_SUCCESS;
106 }
107
108 static enum nss_status
109 internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen)
110 {
111   int parse_res;
112
113   if (result == NULL)
114     {
115       enum nss_status status;
116
117       status = internal_setgrent ();
118       if (result == NULL || status != NSS_STATUS_SUCCESS)
119         return status;
120     }
121
122   /* Get the next entry until we found a correct one. */
123   do
124     {
125       if (next_entry >= result->objects.objects_len)
126         return NSS_STATUS_NOTFOUND;
127
128       if ((parse_res = _nss_nisplus_parse_grent (result, next_entry, gr,
129                                                  buffer, buflen)) == -1)
130         return NSS_STATUS_TRYAGAIN;
131
132       ++next_entry;
133     }
134   while (!parse_res);
135
136   return NSS_STATUS_SUCCESS;
137 }
138
139 enum nss_status
140 _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen)
141 {
142   int status;
143
144   __libc_lock_lock (lock);
145
146   status = internal_nisplus_getgrent_r (result, buffer, buflen);
147
148   __libc_lock_unlock (lock);
149
150   return status;
151 }
152
153 enum nss_status
154 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
155                          char *buffer, size_t buflen)
156 {
157   int parse_res;
158
159   if (tablename_val == NULL)
160     if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
161       return NSS_STATUS_UNAVAIL;
162
163   if (name == NULL || strlen (name) > 8)
164     return NSS_STATUS_NOTFOUND;
165   else
166     {
167       nis_result *result;
168       char buf[strlen (name) + 24 + tablename_len];
169
170       sprintf (buf, "[name=%s],%s", name, tablename_val);
171
172       result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
173
174       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
175         {
176           enum nss_status status = niserr2nss (result->status);
177
178           nis_freeresult (result);
179           return status;
180         }
181
182       parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen);
183       nis_freeresult (result);
184
185       if (parse_res == -1)
186         return NSS_STATUS_TRYAGAIN;
187       if (parse_res)
188         return NSS_STATUS_SUCCESS;
189
190       return NSS_STATUS_NOTFOUND;
191     }
192 }
193
194 enum nss_status
195 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
196                          char *buffer, size_t buflen)
197 {
198   if (tablename_val == NULL)
199     if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
200       return NSS_STATUS_UNAVAIL;
201
202   {
203     int parse_res;
204     nis_result *result;
205     char buf[36 + tablename_len];
206
207     sprintf (buf, "[gid=%d],%s", gid, tablename_val);
208
209     result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
210
211     if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
212       {
213         enum nss_status status = niserr2nss (result->status);
214
215         nis_freeresult (result);
216         return status;
217       }
218
219     parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen);
220
221     nis_freeresult (result);
222
223     if (parse_res == -1)
224       return NSS_STATUS_TRYAGAIN;
225
226     if (parse_res)
227       return NSS_STATUS_SUCCESS;
228
229     return NSS_STATUS_NOTFOUND;
230   }
231 }