Update.
[platform/upstream/glibc.git] / nis / nis_print_group_entry.c
1 /* Copyright (c) 1997, 1998 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 <string.h>
21 #include <rpcsvc/nis.h>
22
23 void
24 nis_print_group_entry (const_nis_name group)
25 {
26   if (group != NULL && group[0] != '\0')
27     {
28       size_t grouplen = strlen (group);
29       char buf[grouplen + 50];
30       char leafbuf[grouplen + 3];
31       char domainbuf[grouplen + 3];
32       unsigned long mem_exp_cnt = 0, mem_imp_cnt = 0, mem_rec_cnt = 0;
33       unsigned long nomem_exp_cnt = 0, nomem_imp_cnt = 0, nomem_rec_cnt = 0;
34       char **mem_exp, **mem_imp, **mem_rec;
35       char **nomem_exp, **nomem_imp, **nomem_rec;
36       nis_result *res;
37       char *cp, *cp2;
38       u_int i;
39
40       cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1));
41       cp = stpcpy (cp, ".groups_dir");
42       cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
43       if (cp2 != NULL && cp2[0] != '\0')
44         {
45           *cp++ = '.';
46           stpcpy (cp, cp2);
47         }
48       res = nis_lookup (buf, FOLLOW_LINKS | EXPAND_NAME);
49
50       if (NIS_RES_STATUS(res) != NIS_SUCCESS)
51         return;
52
53       if ((NIS_RES_NUMOBJ (res) != 1) ||
54           (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ))
55         return;
56
57       mem_exp = malloc (sizeof (char *) * NIS_RES_NUMOBJ (res));
58       mem_imp = malloc (sizeof (char *) * NIS_RES_NUMOBJ (res));
59       mem_rec = malloc (sizeof (char *) * NIS_RES_NUMOBJ (res));
60       nomem_exp = malloc (sizeof (char *) * NIS_RES_NUMOBJ (res));
61       nomem_imp = malloc (sizeof (char *) * NIS_RES_NUMOBJ (res));
62       nomem_rec = malloc (sizeof (char *) * NIS_RES_NUMOBJ (res));
63
64       for (i = 0;
65          i < NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len; ++i)
66         {
67           char *grmem =
68           NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
69           int neg = grmem[0] == '-';
70
71           switch (grmem[neg])
72             {
73             case '*':
74               if (neg)
75                 {
76                   nomem_imp[nomem_imp_cnt] = grmem;
77                   ++nomem_imp_cnt;
78                 }
79               else
80                 {
81                   mem_imp[mem_imp_cnt] = grmem;
82                   ++mem_imp_cnt;
83                 }
84               break;
85             case '@':
86               if (neg)
87                 {
88                   nomem_rec[nomem_rec_cnt] = grmem;
89                   ++nomem_rec_cnt;
90                 }
91               else
92                 {
93                   mem_rec[mem_rec_cnt] = grmem;
94                   ++mem_rec_cnt;
95                 }
96               break;
97             default:
98               if (neg)
99                 {
100                   nomem_exp[nomem_exp_cnt] = grmem;
101                   ++nomem_exp_cnt;
102                 }
103               else
104                 {
105                   mem_exp[mem_exp_cnt] = grmem;
106                   ++mem_exp_cnt;
107                 }
108               break;
109             }
110         }
111       {
112         char buf[strlen (NIS_RES_OBJECT (res)->zo_domain) + 10];
113         printf (_("Group entry for \"%s.%s\" group:\n"),
114                 NIS_RES_OBJECT (res)->zo_name,
115                 nis_domain_of_r (NIS_RES_OBJECT (res)->zo_domain,
116                                  buf, strlen (NIS_RES_OBJECT (res)->zo_domain)
117                                  + 10));
118       }
119       if (mem_exp_cnt)
120         {
121           fputs (_("    Explicit members:\n"), stdout);
122           for (i = 0; i < mem_exp_cnt; ++i)
123             printf ("\t%s\n", mem_exp[i]);
124         }
125       else
126         fputs (_("    No explicit members\n"), stdout);
127       if (mem_imp_cnt)
128         {
129           fputs (_("    Implicit members:\n"), stdout);
130           for (i = 0; i < mem_imp_cnt; ++i)
131             printf ("\t%s\n", &mem_imp[i][2]);
132         }
133       else
134         fputs (_("    No implicit members\n"), stdout);
135       if (mem_rec_cnt)
136         {
137           fputs (_("    Recursive members:\n"), stdout);
138           for (i = 0; i < mem_rec_cnt; ++i)
139             printf ("\t%s\n", &mem_rec[i][1]);
140         }
141       else
142         fputs (_("    No recursive members\n"), stdout);
143       if (nomem_exp_cnt)
144         {
145           fputs (_("    Explicit nonmembers:\n"), stdout);
146           for (i = 0; i < nomem_exp_cnt; ++i)
147             printf ("\t%s\n", &nomem_exp[i][1]);
148         }
149       else
150         fputs (_("    No explicit nonmembers\n"), stdout);
151       if (nomem_imp_cnt)
152         {
153           fputs (_("    Implicit nonmembers:\n"), stdout);
154           for (i = 0; i < nomem_imp_cnt; ++i)
155             printf ("\t%s\n", &mem_imp[i][3]);
156         }
157       else
158         fputs (_("    No implicit nonmembers\n"), stdout);
159       if (nomem_rec_cnt)
160         {
161           fputs (_("    Explicit nonmembers:\n"), stdout);
162           for (i = 0; i < nomem_rec_cnt; ++i)
163             printf ("\t%s=n", &nomem_rec[i][2]);
164         }
165       else
166         fputs (_("    No recursive nonmembers\n"), stdout);
167
168       free (mem_exp);
169       free (mem_imp);
170       free (mem_rec);
171       free (nomem_exp);
172       free (nomem_imp);
173       free (nomem_rec);
174       nis_freeresult (res);
175     }
176 }