Merge from vendor branch OPENSSL:
[dragonfly.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  * $FreeBSD: src/usr.sbin/pw/pw_group.c,v 1.12.2.1 2000/06/28 19:19:04 ache Exp $
27  * $DragonFly: src/usr.sbin/pw/pw_group.c,v 1.2 2003/06/17 04:30:02 dillon Exp $
28  */
29
30 #include <ctype.h>
31 #include <err.h>
32 #include <termios.h>
33 #include <unistd.h>
34
35 #include "pw.h"
36 #include "bitmap.h"
37
38
39 static int      print_group(struct group * grp, int pretty);
40 static gid_t    gr_gidpolicy(struct userconf * cnf, struct cargs * args);
41
42 int
43 pw_group(struct userconf * cnf, int mode, struct cargs * args)
44 {
45         int             rc;
46         struct carg    *a_name = getarg(args, 'n');
47         struct carg    *a_gid = getarg(args, 'g');
48         struct carg    *arg;
49         struct group   *grp = NULL;
50         int             grmembers = 0;
51         char           **members = NULL;
52
53         static struct group fakegroup =
54         {
55                 "nogroup",
56                 "*",
57                 -1,
58                 NULL
59         };
60
61         if (mode == M_LOCK || mode == M_UNLOCK)
62                 errx(EX_USAGE, "'lock' command is not available for groups");
63
64         /*
65          * With M_NEXT, we only need to return the
66          * next gid to stdout
67          */
68         if (mode == M_NEXT) {
69                 gid_t next = gr_gidpolicy(cnf, args);
70                 if (getarg(args, 'q'))
71                         return next;
72                 printf("%ld\n", (long)next);
73                 return EXIT_SUCCESS;
74         }
75
76         if (mode == M_PRINT && getarg(args, 'a')) {
77                 int             pretty = getarg(args, 'P') != NULL;
78
79                 SETGRENT();
80                 while ((grp = GETGRENT()) != NULL)
81                         print_group(grp, pretty);
82                 ENDGRENT();
83                 return EXIT_SUCCESS;
84         }
85         if (a_gid == NULL) {
86                 if (a_name == NULL)
87                         errx(EX_DATAERR, "group name or id required");
88
89                 if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) {
90                         (a_gid = a_name)->ch = 'g';
91                         a_name = NULL;
92                 }
93         }
94         grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));
95
96         if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
97                 if (a_name == NULL && grp == NULL)      /* Try harder */
98                         grp = GETGRGID(atoi(a_gid->val));
99
100                 if (grp == NULL) {
101                         if (mode == M_PRINT && getarg(args, 'F')) {
102                                 char    *fmems[1];
103                                 fmems[0] = NULL;
104                                 fakegroup.gr_name = a_name ? a_name->val : "nogroup";
105                                 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1;
106                                 fakegroup.gr_mem = fmems;
107                                 return print_group(&fakegroup, getarg(args, 'P') != NULL);
108                         }
109                         errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
110                 }
111                 if (a_name == NULL)     /* Needed later */
112                         a_name = addarg(args, 'n', grp->gr_name);
113
114                 /*
115                  * Handle deletions now
116                  */
117                 if (mode == M_DELETE) {
118                         gid_t           gid = grp->gr_gid;
119
120                         rc = delgrent(grp);
121                         if (rc == -1)
122                                 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
123                         else if (rc != 0) {
124                                 warn("group update");
125                                 return EX_IOERR;
126                         }
127                         pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
128                         return EXIT_SUCCESS;
129                 } else if (mode == M_PRINT)
130                         return print_group(grp, getarg(args, 'P') != NULL);
131
132                 if (a_gid)
133                         grp->gr_gid = (gid_t) atoi(a_gid->val);
134
135                 if ((arg = getarg(args, 'l')) != NULL)
136                         grp->gr_name = pw_checkname((u_char *)arg->val, 0);
137         } else {
138                 if (a_name == NULL)     /* Required */
139                         errx(EX_DATAERR, "group name required");
140                 else if (grp != NULL)   /* Exists */
141                         errx(EX_DATAERR, "group name `%s' already exists", a_name->val);
142
143                 extendarray(&members, &grmembers, 200);
144                 members[0] = NULL;
145                 grp = &fakegroup;
146                 grp->gr_name = pw_checkname((u_char *)a_name->val, 0);
147                 grp->gr_passwd = "*";
148                 grp->gr_gid = gr_gidpolicy(cnf, args);
149                 grp->gr_mem = members;
150         }
151
152         /*
153          * This allows us to set a group password Group passwords is an
154          * antique idea, rarely used and insecure (no secure database) Should
155          * be discouraged, but it is apparently still supported by some
156          * software.
157          */
158
159         if ((arg = getarg(args, 'h')) != NULL) {
160                 if (strcmp(arg->val, "-") == 0)
161                         grp->gr_passwd = "*";   /* No access */
162                 else {
163                         int             fd = atoi(arg->val);
164                         int             b;
165                         int             istty = isatty(fd);
166                         struct termios  t;
167                         char           *p, line[256];
168
169                         if (istty) {
170                                 if (tcgetattr(fd, &t) == -1)
171                                         istty = 0;
172                                 else {
173                                         struct termios  n = t;
174
175                                         /* Disable echo */
176                                         n.c_lflag &= ~(ECHO);
177                                         tcsetattr(fd, TCSANOW, &n);
178                                         printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name);
179                                         fflush(stdout);
180                                 }
181                         }
182                         b = read(fd, line, sizeof(line) - 1);
183                         if (istty) {    /* Restore state */
184                                 tcsetattr(fd, TCSANOW, &t);
185                                 fputc('\n', stdout);
186                                 fflush(stdout);
187                         }
188                         if (b < 0) {
189                                 warn("-h file descriptor");
190                                 return EX_OSERR;
191                         }
192                         line[b] = '\0';
193                         if ((p = strpbrk(line, " \t\r\n")) != NULL)
194                                 *p = '\0';
195                         if (!*line)
196                                 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
197                         grp->gr_passwd = pw_pwcrypt(line);
198                 }
199         }
200
201         if (((arg = getarg(args, 'M')) != NULL || (arg = getarg(args, 'm')) != NULL) && arg->val) {
202                 int     i = 0;
203                 char   *p;
204                 struct passwd   *pwd;
205
206                 /* Make sure this is not stay NULL with -M "" */
207                 extendarray(&members, &grmembers, 200);
208                 if (arg->ch == 'm') {
209                         int     k = 0;
210
211                         while (grp->gr_mem[k] != NULL) {
212                                 if (extendarray(&members, &grmembers, i + 2) != -1) {
213                                         members[i++] = grp->gr_mem[k];
214                                 }
215                                 k++;
216                         }
217                 }
218                 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
219                         int     j;
220                         if ((pwd = GETPWNAM(p)) == NULL) {
221                                 if (!isdigit((unsigned char)*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL)
222                                         errx(EX_NOUSER, "user `%s' does not exist", p);
223                         }
224                         /*
225                          * Check for duplicates
226                          */
227                         for (j = 0; j < i && strcmp(members[j], pwd->pw_name)!=0; j++)
228                                 ;
229                         if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
230                                 members[i++] = newstr(pwd->pw_name);
231                 }
232                 while (i < grmembers)
233                         members[i++] = NULL;
234                 grp->gr_mem = members;
235         }
236
237         if (getarg(args, 'N') != NULL)
238                 return print_group(grp, getarg(args, 'P') != NULL);
239
240         if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
241                 if (rc == -1)
242                         warnx("group '%s' already exists", grp->gr_name);
243                 else
244                         warn("group update");
245                 return EX_IOERR;
246         } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
247                 if (rc == -1)
248                         warnx("group '%s' not available (NIS?)", grp->gr_name);
249                 else
250                         warn("group update");
251                 return EX_IOERR;
252         }
253         /* grp may have been invalidated */
254         if ((grp = GETGRNAM(a_name->val)) == NULL)
255                 errx(EX_SOFTWARE, "group disappeared during update");
256
257         pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
258
259         if (members)
260                 free(members);
261
262         return EXIT_SUCCESS;
263 }
264
265
266 static          gid_t
267 gr_gidpolicy(struct userconf * cnf, struct cargs * args)
268 {
269         struct group   *grp;
270         gid_t           gid = (gid_t) - 1;
271         struct carg    *a_gid = getarg(args, 'g');
272
273         /*
274          * Check the given gid, if any
275          */
276         if (a_gid != NULL) {
277                 gid = (gid_t) atol(a_gid->val);
278
279                 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL)
280                         errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid);
281         } else {
282                 struct bitmap   bm;
283
284                 /*
285                  * We need to allocate the next available gid under one of
286                  * two policies a) Grab the first unused gid b) Grab the
287                  * highest possible unused gid
288                  */
289                 if (cnf->min_gid >= cnf->max_gid) {     /* Sanity claus^H^H^H^Hheck */
290                         cnf->min_gid = 1000;
291                         cnf->max_gid = 32000;
292                 }
293                 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
294
295                 /*
296                  * Now, let's fill the bitmap from the password file
297                  */
298                 SETGRENT();
299                 while ((grp = GETGRENT()) != NULL)
300                         if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
301                             (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
302                                 bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
303                 ENDGRENT();
304
305                 /*
306                  * Then apply the policy, with fallback to reuse if necessary
307                  */
308                 if (cnf->reuse_gids)
309                         gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
310                 else {
311                         gid = (gid_t) (bm_lastset(&bm) + 1);
312                         if (!bm_isset(&bm, gid))
313                                 gid += cnf->min_gid;
314                         else
315                                 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
316                 }
317
318                 /*
319                  * Another sanity check
320                  */
321                 if (gid < cnf->min_gid || gid > cnf->max_gid)
322                         errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
323                 bm_dealloc(&bm);
324         }
325         return gid;
326 }
327
328
329 static int
330 print_group(struct group * grp, int pretty)
331 {
332         if (!pretty) {
333                 int             buflen = 0;
334                 char           *buf = NULL;
335
336                 fmtgrent(&buf, &buflen, grp);
337                 fputs(buf, stdout);
338                 free(buf);
339         } else {
340                 int             i;
341
342                 printf("Group Name: %-15s   #%lu\n"
343                        "   Members: ",
344                        grp->gr_name, (long) grp->gr_gid);
345                 for (i = 0; grp->gr_mem[i]; i++)
346                         printf("%s%s", i ? "," : "", grp->gr_mem[i]);
347                 fputs("\n\n", stdout);
348         }
349         return EXIT_SUCCESS;
350 }