]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/pw/pw_group.c
MFC revisions 262864-262865, 263114, 267970:
[FreeBSD/stable/10.git] / usr.sbin / pw / pw_group.c
1 /*-
2  * Copyright (C) 1996
3  *      David L. Nugent.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD$";
30 #endif /* not lint */
31
32 #include <ctype.h>
33 #include <err.h>
34 #include <termios.h>
35 #include <stdbool.h>
36 #include <unistd.h>
37 #include <grp.h>
38 #include <libutil.h>
39
40 #include "pw.h"
41 #include "bitmap.h"
42
43
44 static struct passwd *lookup_pwent(const char *user);
45 static void     delete_members(char ***members, int *grmembers, int *i,
46     struct carg *arg, struct group *grp);
47 static int      print_group(struct group * grp, int pretty);
48 static gid_t    gr_gidpolicy(struct userconf * cnf, struct cargs * args);
49
50 int
51 pw_group(struct userconf * cnf, int mode, struct cargs * args)
52 {
53         int             rc;
54         struct carg    *a_name = getarg(args, 'n');
55         struct carg    *a_gid = getarg(args, 'g');
56         struct carg    *arg;
57         struct group   *grp = NULL;
58         int             grmembers = 0;
59         char           **members = NULL;
60
61         static struct group fakegroup =
62         {
63                 "nogroup",
64                 "*",
65                 -1,
66                 NULL
67         };
68
69         if (mode == M_LOCK || mode == M_UNLOCK)
70                 errx(EX_USAGE, "'lock' command is not available for groups");
71
72         /*
73          * With M_NEXT, we only need to return the
74          * next gid to stdout
75          */
76         if (mode == M_NEXT) {
77                 gid_t next = gr_gidpolicy(cnf, args);
78                 if (getarg(args, 'q'))
79                         return next;
80                 printf("%ld\n", (long)next);
81                 return EXIT_SUCCESS;
82         }
83
84         if (mode == M_PRINT && getarg(args, 'a')) {
85                 int             pretty = getarg(args, 'P') != NULL;
86
87                 SETGRENT();
88                 while ((grp = GETGRENT()) != NULL)
89                         print_group(grp, pretty);
90                 ENDGRENT();
91                 return EXIT_SUCCESS;
92         }
93         if (a_gid == NULL) {
94                 if (a_name == NULL)
95                         errx(EX_DATAERR, "group name or id required");
96
97                 if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) {
98                         (a_gid = a_name)->ch = 'g';
99                         a_name = NULL;
100                 }
101         }
102         grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));
103
104         if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
105                 if (a_name == NULL && grp == NULL)      /* Try harder */
106                         grp = GETGRGID(atoi(a_gid->val));
107
108                 if (grp == NULL) {
109                         if (mode == M_PRINT && getarg(args, 'F')) {
110                                 char    *fmems[1];
111                                 fmems[0] = NULL;
112                                 fakegroup.gr_name = a_name ? a_name->val : "nogroup";
113                                 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1;
114                                 fakegroup.gr_mem = fmems;
115                                 return print_group(&fakegroup, getarg(args, 'P') != NULL);
116                         }
117                         errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
118                 }
119                 if (a_name == NULL)     /* Needed later */
120                         a_name = addarg(args, 'n', grp->gr_name);
121
122                 /*
123                  * Handle deletions now
124                  */
125                 if (mode == M_DELETE) {
126                         gid_t           gid = grp->gr_gid;
127
128                         rc = delgrent(grp);
129                         if (rc == -1)
130                                 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
131                         else if (rc != 0) {
132                                 warn("group update");
133                                 return EX_IOERR;
134                         }
135                         pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
136                         return EXIT_SUCCESS;
137                 } else if (mode == M_PRINT)
138                         return print_group(grp, getarg(args, 'P') != NULL);
139
140                 if (a_gid)
141                         grp->gr_gid = (gid_t) atoi(a_gid->val);
142
143                 if ((arg = getarg(args, 'l')) != NULL)
144                         grp->gr_name = pw_checkname((u_char *)arg->val, 0);
145         } else {
146                 if (a_name == NULL)     /* Required */
147                         errx(EX_DATAERR, "group name required");
148                 else if (grp != NULL)   /* Exists */
149                         errx(EX_DATAERR, "group name `%s' already exists", a_name->val);
150
151                 extendarray(&members, &grmembers, 200);
152                 members[0] = NULL;
153                 grp = &fakegroup;
154                 grp->gr_name = pw_checkname((u_char *)a_name->val, 0);
155                 grp->gr_passwd = "*";
156                 grp->gr_gid = gr_gidpolicy(cnf, args);
157                 grp->gr_mem = members;
158         }
159
160         /*
161          * This allows us to set a group password Group passwords is an
162          * antique idea, rarely used and insecure (no secure database) Should
163          * be discouraged, but it is apparently still supported by some
164          * software.
165          */
166
167         if ((arg = getarg(args, 'h')) != NULL ||
168             (arg = getarg(args, 'H')) != NULL) {
169                 if (strcmp(arg->val, "-") == 0)
170                         grp->gr_passwd = "*";   /* No access */
171                 else {
172                         int             fd = atoi(arg->val);
173                         int             precrypt = (arg->ch == 'H');
174                         int             b;
175                         int             istty = isatty(fd);
176                         struct termios  t;
177                         char           *p, line[256];
178
179                         if (istty) {
180                                 if (tcgetattr(fd, &t) == -1)
181                                         istty = 0;
182                                 else {
183                                         struct termios  n = t;
184
185                                         /* Disable echo */
186                                         n.c_lflag &= ~(ECHO);
187                                         tcsetattr(fd, TCSANOW, &n);
188                                         printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name);
189                                         fflush(stdout);
190                                 }
191                         }
192                         b = read(fd, line, sizeof(line) - 1);
193                         if (istty) {    /* Restore state */
194                                 tcsetattr(fd, TCSANOW, &t);
195                                 fputc('\n', stdout);
196                                 fflush(stdout);
197                         }
198                         if (b < 0) {
199                                 warn("-h file descriptor");
200                                 return EX_OSERR;
201                         }
202                         line[b] = '\0';
203                         if ((p = strpbrk(line, " \t\r\n")) != NULL)
204                                 *p = '\0';
205                         if (!*line)
206                                 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
207                         if (precrypt) {
208                                 if (strchr(line, ':') != NULL)
209                                         return EX_DATAERR;
210                                 grp->gr_passwd = line;
211                         } else
212                                 grp->gr_passwd = pw_pwcrypt(line);
213                 }
214         }
215
216         if (((arg = getarg(args, 'M')) != NULL ||
217             (arg = getarg(args, 'd')) != NULL ||
218             (arg = getarg(args, 'm')) != NULL) && arg->val) {
219                 int     i = 0;
220                 char   *p;
221                 struct passwd   *pwd;
222
223                 /* Make sure this is not stay NULL with -M "" */
224                 extendarray(&members, &grmembers, 200);
225                 if (arg->ch == 'd')
226                         delete_members(&members, &grmembers, &i, arg, grp);
227                 else if (arg->ch == 'm') {
228                         int     k = 0;
229
230                         if (grp->gr_mem != NULL) {
231                                 while (grp->gr_mem[k] != NULL) {
232                                         if (extendarray(&members, &grmembers, i + 2) != -1)
233                                                 members[i++] = grp->gr_mem[k];
234                                         k++;
235                                 }
236                         }
237                 }
238
239                 if (arg->ch != 'd')
240                         for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
241                                 int     j;
242
243                                 /*
244                                  * Check for duplicates
245                                  */
246                                 pwd = lookup_pwent(p);
247                                 for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
248                                         ;
249                                 if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
250                                         members[i++] = newstr(pwd->pw_name);
251                         }
252                 while (i < grmembers)
253                         members[i++] = NULL;
254                 grp->gr_mem = members;
255         }
256
257         if (getarg(args, 'N') != NULL)
258                 return print_group(grp, getarg(args, 'P') != NULL);
259
260         if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
261                 if (rc == -1)
262                         warnx("group '%s' already exists", grp->gr_name);
263                 else
264                         warn("group update");
265                 return EX_IOERR;
266         } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
267                 if (rc == -1)
268                         warnx("group '%s' not available (NIS?)", grp->gr_name);
269                 else
270                         warn("group update");
271                 return EX_IOERR;
272         }
273         /* grp may have been invalidated */
274         if ((grp = GETGRNAM(a_name->val)) == NULL)
275                 errx(EX_SOFTWARE, "group disappeared during update");
276
277         pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
278
279         free(members);
280
281         return EXIT_SUCCESS;
282 }
283
284
285 /*
286  * Lookup a passwd entry using a name or UID.
287  */
288 static struct passwd *
289 lookup_pwent(const char *user)
290 {
291         struct passwd *pwd;
292
293         if ((pwd = GETPWNAM(user)) == NULL &&
294             (!isdigit((unsigned char)*user) ||
295             (pwd = getpwuid((uid_t) atoi(user))) == NULL))
296                 errx(EX_NOUSER, "user `%s' does not exist", user);
297
298         return (pwd);
299 }
300
301
302 /*
303  * Delete requested members from a group.
304  */
305 static void
306 delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
307     struct group *grp)
308 {
309         bool matchFound;
310         char *user;
311         char *valueCopy;
312         char *valuePtr;
313         int k;
314         struct passwd *pwd;
315
316         if (grp->gr_mem == NULL)
317                 return;
318
319         k = 0;
320         while (grp->gr_mem[k] != NULL) {
321                 matchFound = false;
322                 if ((valueCopy = strdup(arg->val)) == NULL)
323                         errx(EX_UNAVAILABLE, "out of memory");
324                 valuePtr = valueCopy;
325                 while ((user = strsep(&valuePtr, ", \t")) != NULL) {
326                         pwd = lookup_pwent(user);
327                         if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
328                                 matchFound = true;
329                                 break;
330                         }
331                 }
332                 free(valueCopy);
333
334                 if (!matchFound &&
335                     extendarray(members, grmembers, *i + 2) != -1)
336                         (*members)[(*i)++] = grp->gr_mem[k];
337
338                 k++;
339         }
340
341         return;
342 }
343
344
345 static          gid_t
346 gr_gidpolicy(struct userconf * cnf, struct cargs * args)
347 {
348         struct group   *grp;
349         gid_t           gid = (gid_t) - 1;
350         struct carg    *a_gid = getarg(args, 'g');
351
352         /*
353          * Check the given gid, if any
354          */
355         if (a_gid != NULL) {
356                 gid = (gid_t) atol(a_gid->val);
357
358                 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL)
359                         errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid);
360         } else {
361                 struct bitmap   bm;
362
363                 /*
364                  * We need to allocate the next available gid under one of
365                  * two policies a) Grab the first unused gid b) Grab the
366                  * highest possible unused gid
367                  */
368                 if (cnf->min_gid >= cnf->max_gid) {     /* Sanity claus^H^H^H^Hheck */
369                         cnf->min_gid = 1000;
370                         cnf->max_gid = 32000;
371                 }
372                 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
373
374                 /*
375                  * Now, let's fill the bitmap from the password file
376                  */
377                 SETGRENT();
378                 while ((grp = GETGRENT()) != NULL)
379                         if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
380                             (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
381                                 bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
382                 ENDGRENT();
383
384                 /*
385                  * Then apply the policy, with fallback to reuse if necessary
386                  */
387                 if (cnf->reuse_gids)
388                         gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
389                 else {
390                         gid = (gid_t) (bm_lastset(&bm) + 1);
391                         if (!bm_isset(&bm, gid))
392                                 gid += cnf->min_gid;
393                         else
394                                 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
395                 }
396
397                 /*
398                  * Another sanity check
399                  */
400                 if (gid < cnf->min_gid || gid > cnf->max_gid)
401                         errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
402                 bm_dealloc(&bm);
403         }
404         return gid;
405 }
406
407
408 static int
409 print_group(struct group * grp, int pretty)
410 {
411         if (!pretty) {
412                 char           *buf = NULL;
413
414                 buf = gr_make(grp);
415                 printf("%s\n", buf);
416                 free(buf);
417         } else {
418                 int             i;
419
420                 printf("Group Name: %-15s   #%lu\n"
421                        "   Members: ",
422                        grp->gr_name, (long) grp->gr_gid);
423                 if (grp->gr_mem != NULL) {
424                         for (i = 0; grp->gr_mem[i]; i++)
425                                 printf("%s%s", i ? "," : "", grp->gr_mem[i]);
426                 }
427                 fputs("\n\n", stdout);
428         }
429         return EXIT_SUCCESS;
430 }