3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 static const char rcsid[] =
45 static struct passwd *lookup_pwent(const char *user);
46 static void delete_members(struct group *grp, char *list);
47 static int print_group(struct group * grp);
48 static gid_t gr_gidpolicy(struct userconf * cnf, long id);
51 set_passwd(struct group *grp, bool update)
59 grp->gr_passwd = "*"; /* No access */
63 if ((istty = isatty(conf.fd))) {
67 tcsetattr(conf.fd, TCSANOW, &n);
68 printf("%sassword for group %s:", update ? "New p" : "P",
72 b = read(conf.fd, line, sizeof(line) - 1);
73 if (istty) { /* Restore state */
74 tcsetattr(conf.fd, TCSANOW, &t);
79 err(EX_OSERR, "-h file descriptor");
81 if ((p = strpbrk(line, " \t\r\n")) != NULL)
84 errx(EX_DATAERR, "empty password read on file descriptor %d",
86 if (conf.precrypted) {
87 if (strchr(line, ':') != 0)
88 errx(EX_DATAERR, "wrong encrypted passwrd");
89 grp->gr_passwd = line;
91 grp->gr_passwd = pw_pwcrypt(line);
95 pw_groupnext(struct userconf *cnf, bool quiet)
97 gid_t next = gr_gidpolicy(cnf, -1);
101 printf("%ju\n", (uintmax_t)next);
103 return (EXIT_SUCCESS);
107 pw_groupshow(const char *name, long id, struct group *fakegroup)
109 struct group *grp = NULL;
111 if (id < 0 && name == NULL && !conf.all)
112 errx(EX_DATAERR, "groupname or id or '-a' required");
116 while ((grp = GETGRENT()) != NULL)
120 return (EXIT_SUCCESS);
123 grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
129 errx(EX_DATAERR, "unknown gid `%ld'", id);
130 errx(EX_DATAERR, "unknown group `%s'", name);
134 return (print_group(grp));
138 pw_groupdel(const char *name, long id)
140 struct group *grp = NULL;
143 grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
146 errx(EX_DATAERR, "unknown gid `%ld'", id);
147 errx(EX_DATAERR, "unknown group `%s'", name);
152 err(EX_IOERR, "group '%s' not available (NIS?)", name);
154 err(EX_IOERR, "group update");
155 pw_log(conf.userconf, M_DELETE, W_GROUP, "%s(%ld) removed", name, id);
157 return (EXIT_SUCCESS);
161 pw_group(int mode, char *name, long id, struct cargs * args)
165 struct group *grp = NULL;
166 struct userconf *cnf = conf.userconf;
168 static struct group fakegroup =
177 return (pw_groupnext(cnf, conf.quiet));
180 return (pw_groupshow(name, id, &fakegroup));
182 if (mode == M_DELETE)
183 return (pw_groupdel(name, id));
185 if (mode == M_LOCK || mode == M_UNLOCK)
186 errx(EX_USAGE, "'lock' command is not available for groups");
188 if (id < 0 && name == NULL)
189 errx(EX_DATAERR, "group name or id required");
191 grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
193 if (mode == M_UPDATE) {
194 if (name == NULL && grp == NULL) /* Try harder */
199 errx(EX_DATAERR, "unknown group `%s'", name);
201 errx(EX_DATAERR, "unknown group `%ld'", id);
203 if (name == NULL) /* Needed later */
207 grp->gr_gid = (gid_t) id;
209 if (conf.newname != NULL)
210 grp->gr_name = pw_checkname(conf.newname, 0);
212 if (name == NULL) /* Required */
213 errx(EX_DATAERR, "group name required");
214 else if (grp != NULL) /* Exists */
215 errx(EX_DATAERR, "group name `%s' already exists", name);
218 grp->gr_name = pw_checkname(name, 0);
219 grp->gr_passwd = "*";
220 grp->gr_gid = gr_gidpolicy(cnf, id);
225 * This allows us to set a group password Group passwords is an
226 * antique idea, rarely used and insecure (no secure database) Should
227 * be discouraged, but it is apparently still supported by some
231 if (conf.which == W_GROUP && conf.fd != -1)
232 set_passwd(grp, mode == M_UPDATE);
234 if (((arg = getarg(args, 'M')) != NULL ||
235 (arg = getarg(args, 'd')) != NULL ||
236 (arg = getarg(args, 'm')) != NULL) && arg->val) {
240 /* Make sure this is not stay NULL with -M "" */
242 delete_members(grp, arg->val);
243 else if (arg->ch == 'M')
246 for (p = strtok(arg->val, ", \t"); arg->ch != 'd' && p != NULL;
247 p = strtok(NULL, ", \t")) {
251 * Check for duplicates
253 pwd = lookup_pwent(p);
254 for (j = 0; grp->gr_mem != NULL && grp->gr_mem[j] != NULL; j++) {
255 if (strcmp(grp->gr_mem[j], pwd->pw_name) == 0)
258 if (grp->gr_mem != NULL && grp->gr_mem[j] != NULL)
260 grp = gr_add(grp, pwd->pw_name);
265 return print_group(grp);
267 if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
269 errx(EX_IOERR, "group '%s' already exists",
272 err(EX_IOERR, "group update");
273 } else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) {
275 errx(EX_IOERR, "group '%s' not available (NIS?)",
278 err(EX_IOERR, "group update");
281 if (conf.newname != NULL)
283 /* grp may have been invalidated */
284 if ((grp = GETGRNAM(name)) == NULL)
285 errx(EX_SOFTWARE, "group disappeared during update");
287 pw_log(cnf, mode, W_GROUP, "%s(%ju)", grp->gr_name, (uintmax_t)grp->gr_gid);
294 * Lookup a passwd entry using a name or UID.
296 static struct passwd *
297 lookup_pwent(const char *user)
301 if ((pwd = GETPWNAM(user)) == NULL &&
302 (!isdigit((unsigned char)*user) ||
303 (pwd = getpwuid((uid_t) atoi(user))) == NULL))
304 errx(EX_NOUSER, "user `%s' does not exist", user);
311 * Delete requested members from a group.
314 delete_members(struct group *grp, char *list)
319 if (grp->gr_mem == NULL)
322 for (p = strtok(list, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
323 for (k = 0; grp->gr_mem[k] != NULL; k++) {
324 if (strcmp(grp->gr_mem[k], p) == 0)
327 if (grp->gr_mem[k] == NULL) /* No match */
330 for (; grp->gr_mem[k] != NULL; k++)
331 grp->gr_mem[k] = grp->gr_mem[k+1];
337 gr_gidpolicy(struct userconf * cnf, long id)
340 gid_t gid = (gid_t) - 1;
343 * Check the given gid, if any
348 if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate)
349 errx(EX_DATAERR, "gid `%ju' has already been allocated", (uintmax_t)grp->gr_gid);
354 * We need to allocate the next available gid under one of
355 * two policies a) Grab the first unused gid b) Grab the
356 * highest possible unused gid
358 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
360 cnf->max_gid = 32000;
362 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
365 * Now, let's fill the bitmap from the password file
368 while ((grp = GETGRENT()) != NULL)
369 if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
370 (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
371 bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
375 * Then apply the policy, with fallback to reuse if necessary
378 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
380 gid = (gid_t) (bm_lastset(&bm) + 1);
381 if (!bm_isset(&bm, gid))
384 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
388 * Another sanity check
390 if (gid < cnf->min_gid || gid > cnf->max_gid)
391 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
399 print_group(struct group * grp)
410 printf("Group Name: %-15s #%lu\n"
412 grp->gr_name, (long) grp->gr_gid);
413 if (grp->gr_mem != NULL) {
414 for (i = 0; grp->gr_mem[i]; i++)
415 printf("%s%s", i ? "," : "", grp->gr_mem[i]);
417 fputs("\n\n", stdout);