]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_group.c
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char rcsid
[] =
47 static struct passwd
*lookup_pwent(const char *user
);
48 static void delete_members(struct group
*grp
, char *list
);
49 static int print_group(struct group
* grp
, bool pretty
);
50 static gid_t
gr_gidpolicy(struct userconf
* cnf
, intmax_t id
);
53 grp_set_passwd(struct group
*grp
, bool update
, int fd
, bool precrypted
)
64 grp
->gr_passwd
= "*"; /* No access */
68 if ((istty
= isatty(fd
))) {
72 tcsetattr(fd
, TCSANOW
, &n
);
73 printf("%sassword for group %s:", update
? "New p" : "P",
77 b
= read(fd
, line
, sizeof(line
) - 1);
78 if (istty
) { /* Restore state */
79 tcsetattr(fd
, TCSANOW
, &t
);
84 err(EX_OSERR
, "-h file descriptor");
86 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
89 errx(EX_DATAERR
, "empty password read on file descriptor %d",
92 if (strchr(line
, ':') != 0)
93 errx(EX_DATAERR
, "wrong encrypted passwrd");
94 grp
->gr_passwd
= line
;
96 grp
->gr_passwd
= pw_pwcrypt(line
);
100 pw_groupnext(struct userconf
*cnf
, bool quiet
)
102 gid_t next
= gr_gidpolicy(cnf
, -1);
106 printf("%ju\n", (uintmax_t)next
);
108 return (EXIT_SUCCESS
);
111 static struct group
*
112 getgroup(char *name
, intmax_t id
, bool fatal
)
116 if (id
< 0 && name
== NULL
)
117 errx(EX_DATAERR
, "groupname or id required");
118 grp
= (name
!= NULL
) ? GETGRNAM(name
) : GETGRGID(id
);
123 errx(EX_DATAERR
, "unknown gid `%ju'", id
);
124 errx(EX_DATAERR
, "unknown group `%s'", name
);
130 * Lookup a passwd entry using a name or UID.
132 static struct passwd
*
133 lookup_pwent(const char *user
)
137 if ((pwd
= GETPWNAM(user
)) == NULL
&&
138 (!isdigit((unsigned char)*user
) ||
139 (pwd
= getpwuid((uid_t
) atoi(user
))) == NULL
))
140 errx(EX_NOUSER
, "user `%s' does not exist", user
);
147 * Delete requested members from a group.
150 delete_members(struct group
*grp
, char *list
)
155 if (grp
->gr_mem
== NULL
)
158 for (p
= strtok(list
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
159 for (k
= 0; grp
->gr_mem
[k
] != NULL
; k
++) {
160 if (strcmp(grp
->gr_mem
[k
], p
) == 0)
163 if (grp
->gr_mem
[k
] == NULL
) /* No match */
166 for (; grp
->gr_mem
[k
] != NULL
; k
++)
167 grp
->gr_mem
[k
] = grp
->gr_mem
[k
+1];
172 gr_gidpolicy(struct userconf
* cnf
, intmax_t id
)
176 gid_t gid
= (gid_t
) - 1;
179 * Check the given gid, if any
184 if ((grp
= GETGRGID(gid
)) != NULL
&& conf
.checkduplicate
)
185 errx(EX_DATAERR
, "gid `%ju' has already been allocated",
186 (uintmax_t)grp
->gr_gid
);
191 * We need to allocate the next available gid under one of
192 * two policies a) Grab the first unused gid b) Grab the
193 * highest possible unused gid
195 if (cnf
->min_gid
>= cnf
->max_gid
) { /* Sanity claus^H^H^H^Hheck */
197 cnf
->max_gid
= 32000;
199 bm
= bm_alloc(cnf
->max_gid
- cnf
->min_gid
+ 1);
202 * Now, let's fill the bitmap from the password file
205 while ((grp
= GETGRENT()) != NULL
)
206 if ((gid_t
)grp
->gr_gid
>= (gid_t
)cnf
->min_gid
&&
207 (gid_t
)grp
->gr_gid
<= (gid_t
)cnf
->max_gid
)
208 bm_setbit(&bm
, grp
->gr_gid
- cnf
->min_gid
);
212 * Then apply the policy, with fallback to reuse if necessary
215 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
217 gid
= (gid_t
) (bm_lastset(&bm
) + 1);
218 if (!bm_isset(&bm
, gid
))
221 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
225 * Another sanity check
227 if (gid
< cnf
->min_gid
|| gid
> cnf
->max_gid
)
228 errx(EX_SOFTWARE
, "unable to allocate a new gid - range fully "
235 print_group(struct group
* grp
, bool pretty
)
241 printf("Group Name: %-15s #%lu\n"
243 grp
->gr_name
, (long) grp
->gr_gid
);
244 if (grp
->gr_mem
!= NULL
) {
245 for (i
= 0; grp
->gr_mem
[i
]; i
++)
246 printf("%s%s", i
? "," : "", grp
->gr_mem
[i
]);
248 fputs("\n\n", stdout
);
249 return (EXIT_SUCCESS
);
255 return (EXIT_SUCCESS
);
259 pw_group_next(int argc
, char **argv
, char *arg1 __unused
)
261 struct userconf
*cnf
;
262 const char *cfg
= NULL
;
266 while ((ch
= getopt(argc
, argv
, "C:q")) != -1) {
278 freopen(_PATH_DEVNULL
, "w", stderr
);
279 cnf
= get_userconfig(cfg
);
280 return (pw_groupnext(cnf
, quiet
));
284 pw_group_show(int argc
, char **argv
, char *arg1
)
286 struct group
*grp
= NULL
;
290 bool all
, force
, quiet
, pretty
;
292 all
= force
= quiet
= pretty
= false;
294 struct group fakegroup
= {
302 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
303 id
= pw_checkid(arg1
, GID_MAX
);
308 while ((ch
= getopt(argc
, argv
, "C:qn:g:FPa")) != -1) {
311 /* ignore compatibility */
320 id
= pw_checkid(optarg
, GID_MAX
);
335 freopen(_PATH_DEVNULL
, "w", stderr
);
339 while ((grp
= GETGRENT()) != NULL
)
340 print_group(grp
, pretty
);
342 return (EXIT_SUCCESS
);
345 grp
= getgroup(name
, id
, !force
);
349 return (print_group(grp
, pretty
));
353 pw_group_del(int argc
, char **argv
, char *arg1
)
355 struct userconf
*cnf
= NULL
;
356 struct group
*grp
= NULL
;
358 const char *cfg
= NULL
;
365 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
366 id
= pw_checkid(arg1
, GID_MAX
);
371 while ((ch
= getopt(argc
, argv
, "C:qn:g:Y")) != -1) {
383 id
= pw_checkid(optarg
, GID_MAX
);
392 freopen(_PATH_DEVNULL
, "w", stderr
);
393 grp
= getgroup(name
, id
, true);
394 cnf
= get_userconfig(cfg
);
397 err(EX_IOERR
, "group '%s' not available (NIS?)", name
);
399 err(EX_IOERR
, "group update");
400 pw_log(cnf
, M_DELETE
, W_GROUP
, "%s(%ju) removed", name
,
403 if (nis
&& nis_update() == 0)
404 pw_log(cnf
, M_DELETE
, W_GROUP
, "NIS maps updated");
406 return (EXIT_SUCCESS
);
410 grp_has_member(struct group
*grp
, const char *name
)
414 for (j
= 0; grp
->gr_mem
!= NULL
&& grp
->gr_mem
[j
] != NULL
; j
++)
415 if (strcmp(grp
->gr_mem
[j
], name
) == 0)
421 grp_add_members(struct group
**grp
, char *members
)
429 for (p
= strtok(members
, tok
); p
!= NULL
; p
= strtok(NULL
, tok
)) {
430 pwd
= lookup_pwent(p
);
431 if (grp_has_member(*grp
, pwd
->pw_name
))
433 *grp
= gr_add(*grp
, pwd
->pw_name
);
438 groupadd(struct userconf
*cnf
, char *name
, gid_t id
, char *members
, int fd
,
439 bool dryrun
, bool pretty
, bool precrypted
)
444 struct group fakegroup
= {
452 grp
->gr_name
= pw_checkname(name
, 0);
453 grp
->gr_passwd
= "*";
454 grp
->gr_gid
= gr_gidpolicy(cnf
, id
);
458 * This allows us to set a group password Group passwords is an
459 * antique idea, rarely used and insecure (no secure database) Should
460 * be discouraged, but it is apparently still supported by some
463 grp_set_passwd(grp
, false, fd
, precrypted
);
464 grp_add_members(&grp
, members
);
466 return (print_group(grp
, pretty
));
468 if ((rc
= addgrent(grp
)) != 0) {
470 errx(EX_IOERR
, "group '%s' already exists",
473 err(EX_IOERR
, "group update");
476 pw_log(cnf
, M_ADD
, W_GROUP
, "%s(%ju)", grp
->gr_name
,
477 (uintmax_t)grp
->gr_gid
);
479 return (EXIT_SUCCESS
);
483 pw_group_add(int argc
, char **argv
, char *arg1
)
485 struct userconf
*cnf
= NULL
;
487 char *members
= NULL
;
488 const char *cfg
= NULL
;
491 bool quiet
, precrypted
, dryrun
, pretty
, nis
;
493 quiet
= precrypted
= dryrun
= pretty
= nis
= false;
496 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
497 id
= pw_checkid(arg1
, GID_MAX
);
502 while ((ch
= getopt(argc
, argv
, "C:qn:g:h:H:M:oNPY")) != -1) {
514 id
= pw_checkid(optarg
, GID_MAX
);
518 errx(EX_USAGE
, "'-h' and '-H' are mutually "
519 "exclusive options");
520 fd
= pw_checkfd(optarg
);
523 errx(EX_USAGE
, "-H expects a file descriptor");
527 errx(EX_USAGE
, "'-h' and '-H' are mutually "
528 "exclusive options");
529 fd
= pw_checkfd(optarg
);
535 conf
.checkduplicate
= false;
550 freopen(_PATH_DEVNULL
, "w", stderr
);
552 errx(EX_DATAERR
, "group name required");
553 if (GETGRNAM(name
) != NULL
)
554 errx(EX_DATAERR
, "group name `%s' already exists", name
);
555 cnf
= get_userconfig(cfg
);
556 rc
= groupadd(cnf
, name
, gr_gidpolicy(cnf
, id
), members
, fd
, dryrun
,
558 if (nis
&& rc
== EXIT_SUCCESS
&& nis_update() == 0)
559 pw_log(cnf
, M_ADD
, W_GROUP
, "NIS maps updated");
565 pw_group_mod(int argc
, char **argv
, char *arg1
)
567 struct userconf
*cnf
;
568 struct group
*grp
= NULL
;
569 const char *cfg
= NULL
;
570 char *oldmembers
= NULL
;
571 char *members
= NULL
;
572 char *newmembers
= NULL
;
573 char *newname
= NULL
;
577 bool quiet
, pretty
, dryrun
, nis
, precrypted
;
579 quiet
= pretty
= dryrun
= nis
= precrypted
= false;
582 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
583 id
= pw_checkid(arg1
, GID_MAX
);
588 while ((ch
= getopt(argc
, argv
, "C:qn:d:g:l:h:H:M:m:NPY")) != -1) {
600 id
= pw_checkid(optarg
, GID_MAX
);
610 errx(EX_USAGE
, "'-h' and '-H' are mutually "
611 "exclusive options");
612 fd
= pw_checkfd(optarg
);
615 errx(EX_USAGE
, "-H expects a file descriptor");
619 errx(EX_USAGE
, "'-h' and '-H' are mutually "
620 "exclusive options");
621 fd
= pw_checkfd(optarg
);
641 freopen(_PATH_DEVNULL
, "w", stderr
);
642 cnf
= get_userconfig(cfg
);
643 grp
= getgroup(name
, id
, true);
650 grp
->gr_name
= pw_checkname(newname
, 0);
652 grp_set_passwd(grp
, true, fd
, precrypted
);
654 * Keep the same logic as old code for now:
655 * if -M is passed, -d and -m are ignored
656 * then id -d, -m is ignored
662 grp_add_members(&grp
, members
);
663 } else if (oldmembers
) {
664 delete_members(grp
, oldmembers
);
665 } else if (newmembers
) {
666 grp_add_members(&grp
, newmembers
);
670 print_group(grp
, pretty
);
671 return (EXIT_SUCCESS
);
674 if ((rc
= chggrent(name
, grp
)) != 0) {
676 errx(EX_IOERR
, "group '%s' not available (NIS?)",
679 err(EX_IOERR
, "group update");
685 /* grp may have been invalidated */
686 if ((grp
= GETGRNAM(name
)) == NULL
)
687 errx(EX_SOFTWARE
, "group disappeared during update");
689 pw_log(cnf
, M_UPDATE
, W_GROUP
, "%s(%ju)", grp
->gr_name
,
690 (uintmax_t)grp
->gr_gid
);
692 if (nis
&& nis_update() == 0)
693 pw_log(cnf
, M_UPDATE
, W_GROUP
, "NIS maps updated");
695 return (EXIT_SUCCESS
);