Update.
[platform/upstream/glibc.git] / nis / nss_nis / nis-initgroups.c
1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
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 <unistd.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28
29 #include "nss-nis.h"
30
31 /* Get the declaration of the parser function.  */
32 #define ENTNAME grent
33 #define STRUCTURE group
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
36
37 struct response_t
38 {
39   char *val;
40   struct response_t *next;
41 };
42
43 struct intern_t
44 {
45   struct response_t *start;
46   struct response_t *next;
47 };
48 typedef struct intern_t intern_t;
49
50 static int
51 saveit (int instatus, char *inkey, int inkeylen, char *inval,
52         int invallen, char *indata)
53 {
54   intern_t *intern = (intern_t *) indata;
55
56   if (instatus != YP_TRUE)
57     return instatus;
58
59   if (inkey && inkeylen > 0 && inval && invallen > 0)
60     {
61       if (intern->start == NULL)
62         {
63           intern->start = malloc (sizeof (struct response_t));
64           if (intern->start == NULL)
65             return YP_FALSE;
66           intern->next = intern->start;
67         }
68       else
69         {
70           intern->next->next = malloc (sizeof (struct response_t));
71           if (intern->next->next == NULL)
72             return YP_FALSE;
73           intern->next = intern->next->next;
74         }
75       intern->next->next = NULL;
76       intern->next->val = malloc (invallen + 1);
77       if (intern->next->val == NULL)
78         return YP_FALSE;
79       strncpy (intern->next->val, inval, invallen);
80       intern->next->val[invallen] = '\0';
81     }
82
83   return 0;
84 }
85
86 static enum nss_status
87 internal_setgrent (intern_t *intern)
88 {
89   char *domainname;
90   struct ypall_callback ypcb;
91   enum nss_status status;
92
93   if (yp_get_default_domain (&domainname))
94     return NSS_STATUS_UNAVAIL;
95
96   intern->start = NULL;
97
98   ypcb.foreach = saveit;
99   ypcb.data = (char *) intern;
100   status = yperr2nss (yp_all (domainname, "group.byname", &ypcb));
101   intern->next = intern->start;
102
103   return status;
104 }
105
106 static enum nss_status
107 internal_getgrent_r (struct group *grp, char *buffer, size_t buflen,
108                      int *errnop, intern_t *intern)
109 {
110   struct parser_data *data = (void *) buffer;
111   int parse_res;
112   char *p;
113
114   if (intern->start == NULL)
115     internal_setgrent (intern);
116
117   /* Get the next entry until we found a correct one. */
118   do
119     {
120       if (intern->next == NULL)
121         {
122           *errnop = ENOENT;
123           return NSS_STATUS_NOTFOUND;
124         }
125       p = strncpy (buffer, intern->next->val, buflen);
126       while (isspace (*p))
127         ++p;
128
129       parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
130       if (parse_res == -1)
131         return NSS_STATUS_TRYAGAIN;
132       intern->next = intern->next->next;
133     }
134   while (!parse_res);
135
136   return NSS_STATUS_SUCCESS;
137 }
138
139 enum nss_status
140 _nss_nis_initgroups_dyn (const char *user, gid_t group, long int *start,
141                          long int *size, gid_t **groupsp, int *errnop)
142 {
143   struct group grpbuf, *g;
144   size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
145   char *tmpbuf;
146   enum nss_status status;
147   intern_t intern = { NULL, NULL };
148   gid_t *groups = *groupsp;
149
150   status = internal_setgrent (&intern);
151   if (status != NSS_STATUS_SUCCESS)
152     return status;
153
154   tmpbuf = __alloca (buflen);
155
156   do
157     {
158       while ((status =
159               internal_getgrent_r (&grpbuf, tmpbuf, buflen, errnop,
160                                    &intern)) == NSS_STATUS_TRYAGAIN
161              && *errnop == ERANGE)
162         {
163           buflen *= 2;
164           tmpbuf = __alloca (buflen);
165         }
166
167       if (status != NSS_STATUS_SUCCESS)
168         goto done;
169
170
171       g = &grpbuf;
172       if (g->gr_gid != group)
173         {
174           char **m;
175
176           for (m = g->gr_mem; *m != NULL; ++m)
177             if (strcmp (*m, user) == 0)
178               {
179                 /* Matches user.  Insert this group.  */
180                 if (*start == *size)
181                   {
182                     /* Need a bigger buffer.  */
183                     gid_t *newgroups;
184                     newgroups = realloc (groups, 2 * *size * sizeof (*groups));
185                     if (newgroups == NULL)
186                       goto done;
187                     *groupsp = groups = newgroups;
188                     *size *= 2;
189                   }
190
191                 groups[*start] = g->gr_gid;
192                 *start += 1;
193
194                 break;
195               }
196         }
197     }
198   while (status == NSS_STATUS_SUCCESS);
199
200 done:
201   while (intern.start != NULL)
202     {
203       if (intern.start->val != NULL)
204         free (intern.start->val);
205       intern.next = intern.start;
206       intern.start = intern.start->next;
207       free (intern.next);
208     }
209
210   return NSS_STATUS_SUCCESS;
211 }