Update.
[platform/upstream/glibc.git] / nis / nss_compat / compat-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 <errno.h>
21 #include <fcntl.h>
22 #include <nss.h>
23 #include <grp.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <rpcsvc/nis.h>
30 #include <nsswitch.h>
31
32 #include "nss-nis.h"
33 #include "nss-nisplus.h"
34 #include "nisplus-parser.h"
35
36 static service_user *ni = NULL;
37 static bool_t use_nisplus = FALSE; /* default: group_compat: nis */
38 static nis_name grptable = NULL; /* Name of the group table */
39 static size_t grptablelen = 0;
40
41 /* Get the declaration of the parser function.  */
42 #define ENTNAME grent
43 #define STRUCTURE group
44 #define EXTERN_PARSER
45 #include <nss/nss_files/files-parse.c>
46
47 /* Structure for remembering -group members ... */
48 #define BLACKLIST_INITIAL_SIZE 512
49 #define BLACKLIST_INCREMENT 256
50 struct blacklist_t
51   {
52     char *data;
53     int current;
54     int size;
55   };
56
57 struct response_t
58 {
59   char *val;
60   struct response_t *next;
61 };
62
63 struct ent_t
64   {
65     bool_t nis;
66     bool_t nis_first;
67     char *oldkey;
68     int oldkeylen;
69     nis_result *result;
70     FILE *stream;
71     struct blacklist_t blacklist;
72     struct response_t *start;
73     struct response_t *next;
74 };
75 typedef struct ent_t ent_t;
76
77
78 /* Prototypes for local functions.  */
79 static void blacklist_store_name (const char *, ent_t *);
80 static int in_blacklist (const char *, int, ent_t *);
81
82 static int
83 saveit (int instatus, char *inkey, int inkeylen, char *inval,
84         int invallen, char *indata)
85 {
86   ent_t *intern = (ent_t *) indata;
87
88   if (instatus != YP_TRUE)
89     return instatus;
90
91   if (inkey && inkeylen > 0 && inval && invallen > 0)
92     {
93       if (intern->start == NULL)
94         {
95           intern->start = malloc (sizeof (struct response_t));
96           if (intern->start == NULL)
97             return YP_FALSE;
98           intern->next = intern->start;
99         }
100       else
101         {
102           intern->next->next = malloc (sizeof (struct response_t));
103           if (intern->next->next == NULL)
104             return YP_FALSE;
105           intern->next = intern->next->next;
106         }
107       intern->next->next = NULL;
108       intern->next->val = malloc (invallen + 1);
109       if (intern->next->val == NULL)
110         return YP_FALSE;
111       strncpy (intern->next->val, inval, invallen);
112       intern->next->val[invallen] = '\0';
113     }
114
115   return 0;
116 }
117
118 static enum nss_status
119 _nss_first_init (void)
120 {
121   if (ni == NULL)
122     {
123       __nss_database_lookup ("group_compat", NULL, "nis", &ni);
124       use_nisplus = (strcmp (ni->name, "nisplus") == 0);
125     }
126
127   if (grptable == NULL)
128     {
129       static const char key[] = "group.org_dir.";
130       const char *local_dir = nis_local_directory ();
131       size_t len_local_dir = strlen (local_dir);
132
133       grptable = malloc (sizeof (key) + len_local_dir);
134       if (grptable == NULL)
135         return NSS_STATUS_TRYAGAIN;
136
137       grptablelen = ((char *) mempcpy (mempcpy (grptable,
138                                                 key, sizeof (key) - 1),
139                                        local_dir, len_local_dir + 1)
140                      - grptable) - 1;
141     }
142
143   return NSS_STATUS_SUCCESS;
144 }
145
146 static enum nss_status
147 internal_setgrent (ent_t *ent)
148 {
149   enum nss_status status = NSS_STATUS_SUCCESS;
150
151   ent->nis = ent->nis_first = 0;
152
153   ent->start = NULL;
154   ent->next = NULL;
155
156   if (_nss_first_init () != NSS_STATUS_SUCCESS)
157     return NSS_STATUS_UNAVAIL;
158
159   if (ent->oldkey != NULL)
160     {
161       free (ent->oldkey);
162       ent->oldkey = NULL;
163       ent->oldkeylen = 0;
164     }
165
166   if (ent->result != NULL)
167     {
168       nis_freeresult (ent->result);
169       ent->result = NULL;
170     }
171
172   if (ent->blacklist.data != NULL)
173     {
174       ent->blacklist.current = 1;
175       ent->blacklist.data[0] = '|';
176       ent->blacklist.data[1] = '\0';
177     }
178   else
179     ent->blacklist.current = 0;
180
181   if (ent->stream == NULL)
182     {
183       ent->stream = fopen ("/etc/group", "r");
184
185       if (ent->stream == NULL)
186         status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
187       else
188         {
189           /* We have to make sure the file is  `closed on exec'.  */
190           int result, flags;
191
192           result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
193           if (result >= 0)
194             {
195               flags |= FD_CLOEXEC;
196               result = fcntl (fileno (ent->stream), F_SETFD, flags);
197             }
198           if (result < 0)
199             {
200               /* Something went wrong.  Close the stream and return a
201                  failure.  */
202               fclose (ent->stream);
203               ent->stream = NULL;
204               status = NSS_STATUS_UNAVAIL;
205             }
206         }
207     }
208   else
209     rewind (ent->stream);
210
211   return status;
212 }
213
214
215 static enum nss_status
216 internal_endgrent (ent_t *ent)
217 {
218   if (ent->stream != NULL)
219     {
220       fclose (ent->stream);
221       ent->stream = NULL;
222     }
223
224   ent->nis = ent->nis_first = 0;
225
226   if (ent->oldkey != NULL)
227     {
228       free (ent->oldkey);
229       ent->oldkey = NULL;
230       ent->oldkeylen = 0;
231     }
232
233   if (ent->result != NULL)
234     {
235       nis_freeresult (ent->result);
236       ent->result = NULL;
237     }
238
239   if (ent->blacklist.data != NULL)
240     {
241       ent->blacklist.current = 1;
242       ent->blacklist.data[0] = '|';
243       ent->blacklist.data[1] = '\0';
244     }
245   else
246     ent->blacklist.current = 0;
247
248   while (ent->start != NULL)
249     {
250       if (ent->start->val != NULL)
251         free (ent->start->val);
252       ent->next = ent->start;
253       ent->start = ent->start->next;
254       free (ent->next);
255     }
256
257
258   return NSS_STATUS_SUCCESS;
259 }
260
261 static enum nss_status
262 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
263                    size_t buflen, int *errnop)
264 {
265   struct parser_data *data = (void *) buffer;
266   char *domain, *p;
267   int parse_res;
268
269   if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
270     {
271       ent->nis = 0;
272       *errnop = ENOENT;
273       return NSS_STATUS_NOTFOUND;
274     }
275
276   if (ent->start == NULL)
277     {
278       struct ypall_callback ypcb;
279       enum nss_status status;
280
281       ypcb.foreach = saveit;
282       ypcb.data = (char *) ent;
283       status = yperr2nss (yp_all (domain, "group.byname", &ypcb));
284       ent->next = ent->start;
285
286       if (ent->start == NULL || status != NSS_STATUS_SUCCESS)
287         {
288           ent->nis = 0;
289           *errnop = ENOENT;
290           return NSS_STATUS_UNAVAIL;
291         }
292     }
293
294
295   do
296     {
297       if (ent->next == NULL)
298         {
299           *errnop = ENOENT;
300           ent->nis = 0;
301           return NSS_STATUS_NOTFOUND;
302         }
303
304       /* Copy the found data to our buffer...  */
305       p = strncpy (buffer, ent->next->val, buflen);
306       while (isspace (*p))
307         ++p;
308
309       parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
310       if (parse_res == -1)
311         {
312           *errnop = ERANGE;
313           return NSS_STATUS_TRYAGAIN;
314         }
315
316       ent->next = ent->next->next;
317
318       if (parse_res &&
319           in_blacklist (result->gr_name, strlen (result->gr_name), ent))
320         parse_res = 0; /* if result->gr_name in blacklist,search next entry */
321     }
322   while (!parse_res);
323
324   return NSS_STATUS_SUCCESS;
325 }
326
327 static enum nss_status
328 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
329                        size_t buflen, int *errnop)
330 {
331   int parse_res;
332
333   do
334     {
335       nis_result *save_oldres;
336       bool_t save_nis_first;
337
338       if (ent->nis_first)
339         {
340           save_oldres = ent->result;
341           save_nis_first = TRUE;
342           ent->result = nis_first_entry(grptable);
343           if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
344             {
345               ent->nis = 0;
346               return niserr2nss (ent->result->status);
347             }
348           ent->nis_first = FALSE;
349         }
350       else
351         {
352           nis_result *res;
353
354           save_oldres = ent->result;
355           save_nis_first = FALSE;
356           res = nis_next_entry(grptable, &ent->result->cookie);
357           ent->result = res;
358           if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
359             {
360               ent->nis = 0;
361               return niserr2nss (ent->result->status);
362             }
363         }
364       parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
365                                             buffer, buflen, errnop);
366       if (parse_res == -1)
367         {
368           nis_freeresult (ent->result);
369           ent->result = save_oldres;
370           ent->nis_first = save_nis_first;
371           *errnop = ERANGE;
372           return NSS_STATUS_TRYAGAIN;
373         }
374       else
375         {
376           if (!save_nis_first)
377             nis_freeresult (save_oldres);
378         }
379
380       if (parse_res &&
381           in_blacklist (result->gr_name, strlen (result->gr_name), ent))
382         parse_res = 0; /* if result->gr_name in blacklist,search next entry */
383     }
384   while (!parse_res);
385
386   return NSS_STATUS_SUCCESS;
387 }
388
389 /* This function handle the +group entrys in /etc/group */
390 static enum nss_status
391 getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
392                     size_t buflen, int *errnop)
393 {
394   struct parser_data *data = (void *) buffer;
395   int parse_res;
396
397   if (use_nisplus) /* Do the NIS+ query here */
398     {
399       nis_result *res;
400       char buf[strlen (name) + 24 + grptablelen];
401
402       sprintf(buf, "[name=%s],%s", name, grptable);
403       res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
404       if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
405         {
406           enum nss_status status =  niserr2nss (res->status);
407
408           nis_freeresult (res);
409           return status;
410         }
411       parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
412                                             errnop);
413       if (parse_res == -1)
414         {
415           nis_freeresult (res);
416           *errnop = ERANGE;
417           return NSS_STATUS_TRYAGAIN;
418         }
419       nis_freeresult (res);
420     }
421   else /* Use NIS */
422     {
423       char *domain, *outval, *p;
424       int outvallen;
425
426       if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
427         {
428           *errnop = ENOENT;
429           return NSS_STATUS_NOTFOUND;
430         }
431
432       if (yp_match (domain, "group.byname", name, strlen (name),
433                     &outval, &outvallen) != YPERR_SUCCESS)
434         {
435           *errnop = ENOENT;
436           return NSS_STATUS_NOTFOUND;
437         }
438
439       if (buflen < ((size_t) outvallen + 1))
440         {
441           free (outval);
442           *errnop = ERANGE;
443           return NSS_STATUS_TRYAGAIN;
444         }
445
446       /* Copy the found data to our buffer...  */
447       p = strncpy (buffer, outval, buflen);
448
449       /* ... and free the data.  */
450       free (outval);
451       while (isspace (*p))
452         ++p;
453       parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
454       if (parse_res == -1)
455         {
456           *errnop = ERANGE;
457           return NSS_STATUS_TRYAGAIN;
458         }
459     }
460
461   if (parse_res)
462     /* We found the entry.  */
463     return NSS_STATUS_SUCCESS;
464   else
465     return NSS_STATUS_RETURN;
466 }
467
468 static enum nss_status
469 getgrent_next_file (struct group *result, ent_t *ent,
470                     char *buffer, size_t buflen, int *errnop)
471 {
472   struct parser_data *data = (void *) buffer;
473   while (1)
474     {
475       fpos_t pos;
476       int parse_res = 0;
477       char *p;
478
479       do
480         {
481           fgetpos (ent->stream, &pos);
482           buffer[buflen - 1] = '\xff';
483           p = fgets (buffer, buflen, ent->stream);
484           if (p == NULL && feof (ent->stream))
485             {
486               *errnop = ENOENT;
487               return NSS_STATUS_NOTFOUND;
488             }
489           if (p == NULL || buffer[buflen - 1] != '\xff')
490             {
491               fsetpos (ent->stream, &pos);
492               *errnop = ERANGE;
493               return NSS_STATUS_TRYAGAIN;
494             }
495
496           /* Terminate the line for any case.  */
497           buffer[buflen - 1] = '\0';
498
499           /* Skip leading blanks.  */
500           while (isspace (*p))
501             ++p;
502         }
503       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
504       /* Parse the line.  If it is invalid, loop to
505          get the next line of the file to parse.  */
506              !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
507                                                    errnop)));
508
509       if (parse_res == -1)
510         {
511           /* The parser ran out of space.  */
512           fsetpos (ent->stream, &pos);
513           *errnop = ERANGE;
514           return NSS_STATUS_TRYAGAIN;
515         }
516
517       if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
518         /* This is a real entry.  */
519         break;
520
521       /* -group */
522       if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
523           && result->gr_name[1] != '@')
524         {
525           blacklist_store_name (&result->gr_name[1], ent);
526           continue;
527         }
528
529       /* +group */
530       if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
531           && result->gr_name[1] != '@')
532         {
533           enum nss_status status;
534
535           /* Store the group in the blacklist for the "+" at the end of
536              /etc/group */
537           blacklist_store_name (&result->gr_name[1], ent);
538           status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
539                                        buflen, errnop);
540           if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
541             break;
542           else
543             if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
544                 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
545               continue;
546             else
547               {
548                 if (status == NSS_STATUS_TRYAGAIN)
549                   {
550                     /* The parser ran out of space.  */
551                     fsetpos (ent->stream, &pos);
552                     *errnop = ERANGE;
553                   }
554                 return status;
555               }
556         }
557
558       /* +:... */
559       if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
560         {
561           ent->nis = TRUE;
562           ent->nis_first = TRUE;
563
564           if (use_nisplus)
565             return getgrent_next_nisplus (result, ent, buffer, buflen, errnop);
566           else
567             return getgrent_next_nis (result, ent, buffer, buflen, errnop);
568         }
569     }
570
571   return NSS_STATUS_SUCCESS;
572 }
573
574
575 static enum nss_status
576 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
577                      size_t buflen, int *errnop)
578 {
579   if (ent->nis)
580     {
581       if (use_nisplus)
582         return getgrent_next_nisplus (gr, ent, buffer, buflen, errnop);
583       else
584         return getgrent_next_nis (gr, ent, buffer, buflen, errnop);
585     }
586   else
587     return getgrent_next_file (gr, ent, buffer, buflen, errnop);
588 }
589
590 enum nss_status
591 _nss_compat_initgroups_dyn (const char *user, gid_t group, long int *start,
592                             long int *size, gid_t **groupsp, int *errnop)
593 {
594   struct group grpbuf, *g;
595   size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
596   char *tmpbuf;
597   enum nss_status status;
598   ent_t intern = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
599   gid_t *groups = *groupsp;
600
601   status = internal_setgrent (&intern);
602   if (status != NSS_STATUS_SUCCESS)
603     return status;
604
605   tmpbuf = __alloca (buflen);
606
607   do
608     {
609       while ((status =
610               internal_getgrent_r (&grpbuf, &intern, tmpbuf, buflen,
611                                    errnop)) == NSS_STATUS_TRYAGAIN
612              && *errnop == ERANGE)
613         {
614           buflen *= 2;
615           tmpbuf = __alloca (buflen);
616         }
617
618       if (status != NSS_STATUS_SUCCESS)
619         goto done;
620
621       g = &grpbuf;
622       if (g->gr_gid != group)
623         {
624           char **m;
625
626           for (m = g->gr_mem; *m != NULL; ++m)
627             if (strcmp (*m, user) == 0)
628               {
629                 /* Matches user.  Insert this group.  */
630                 if (*start == *size)
631                   {
632                     /* Need a bigger buffer.  */
633                     gid_t *newgroups;
634                     newgroups = realloc (groups, 2 * *size * sizeof (*groups));
635                     if (newgroups == NULL)
636                       goto done;
637                     *groupsp = groups = newgroups;
638                     *size *= 2;
639                   }
640
641                 groups[*start] = g->gr_gid;
642                 *start += 1;
643
644                 break;
645               }
646         }
647     }
648   while (status == NSS_STATUS_SUCCESS);
649
650 done:
651   internal_endgrent (&intern);
652
653   return NSS_STATUS_SUCCESS;
654 }
655
656
657 /* Support routines for remembering -@netgroup and -user entries.
658    The names are stored in a single string with `|' as separator. */
659 static void
660 blacklist_store_name (const char *name, ent_t *ent)
661 {
662   int namelen = strlen (name);
663   char *tmp;
664
665   /* first call, setup cache */
666   if (ent->blacklist.size == 0)
667     {
668       ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
669       ent->blacklist.data = malloc (ent->blacklist.size);
670       if (ent->blacklist.data == NULL)
671         return;
672       ent->blacklist.data[0] = '|';
673       ent->blacklist.data[1] = '\0';
674       ent->blacklist.current = 1;
675     }
676   else
677     {
678       if (in_blacklist (name, namelen, ent))
679         return;                 /* no duplicates */
680
681       if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
682         {
683           ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
684           tmp = realloc (ent->blacklist.data, ent->blacklist.size);
685           if (tmp == NULL)
686             {
687               free (ent->blacklist.data);
688               ent->blacklist.size = 0;
689               return;
690             }
691           ent->blacklist.data = tmp;
692         }
693     }
694
695   tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
696   *tmp++ = '|';
697   *tmp = '\0';
698   ent->blacklist.current += namelen + 1;
699
700   return;
701 }
702
703 /* returns TRUE if ent->blacklist contains name, else FALSE */
704 static bool_t
705 in_blacklist (const char *name, int namelen, ent_t *ent)
706 {
707   char buf[namelen + 3];
708   char *cp;
709
710   if (ent->blacklist.data == NULL)
711     return FALSE;
712
713   buf[0] = '|';
714   cp = stpcpy (&buf[1], name);
715   *cp++= '|';
716   *cp = '\0';
717   return strstr (ent->blacklist.data, buf) != NULL;
718 }