]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_group.c
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
, bool pretty
);
48 static gid_t
gr_gidpolicy(struct userconf
* cnf
, intmax_t id
);
51 grp_set_passwd(struct group
*grp
, bool update
, int fd
, bool precrypted
)
62 grp
->gr_passwd
= "*"; /* No access */
66 if ((istty
= isatty(fd
))) {
70 tcsetattr(fd
, TCSANOW
, &n
);
71 printf("%sassword for group %s:", update
? "New p" : "P",
75 b
= read(fd
, line
, sizeof(line
) - 1);
76 if (istty
) { /* Restore state */
77 tcsetattr(fd
, TCSANOW
, &t
);
82 err(EX_OSERR
, "-h file descriptor");
84 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
87 errx(EX_DATAERR
, "empty password read on file descriptor %d",
90 if (strchr(line
, ':') != 0)
91 errx(EX_DATAERR
, "wrong encrypted passwrd");
92 grp
->gr_passwd
= line
;
94 grp
->gr_passwd
= pw_pwcrypt(line
);
98 pw_groupnext(struct userconf
*cnf
, bool quiet
)
100 gid_t next
= gr_gidpolicy(cnf
, -1);
104 printf("%ju\n", (uintmax_t)next
);
106 return (EXIT_SUCCESS
);
109 static struct group
*
110 getgroup(char *name
, intmax_t id
, bool fatal
)
114 if (id
< 0 && name
== NULL
)
115 errx(EX_DATAERR
, "groupname or id required");
116 grp
= (name
!= NULL
) ? GETGRNAM(name
) : GETGRGID(id
);
121 errx(EX_DATAERR
, "unknown gid `%ju'", id
);
122 errx(EX_DATAERR
, "unknown group `%s'", name
);
128 * Lookup a passwd entry using a name or UID.
130 static struct passwd
*
131 lookup_pwent(const char *user
)
135 if ((pwd
= GETPWNAM(user
)) == NULL
&&
136 (!isdigit((unsigned char)*user
) ||
137 (pwd
= getpwuid((uid_t
) atoi(user
))) == NULL
))
138 errx(EX_NOUSER
, "user `%s' does not exist", user
);
145 * Delete requested members from a group.
148 delete_members(struct group
*grp
, char *list
)
153 if (grp
->gr_mem
== NULL
)
156 for (p
= strtok(list
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
157 for (k
= 0; grp
->gr_mem
[k
] != NULL
; k
++) {
158 if (strcmp(grp
->gr_mem
[k
], p
) == 0)
161 if (grp
->gr_mem
[k
] == NULL
) /* No match */
164 for (; grp
->gr_mem
[k
] != NULL
; k
++)
165 grp
->gr_mem
[k
] = grp
->gr_mem
[k
+1];
170 gr_gidpolicy(struct userconf
* cnf
, intmax_t id
)
174 gid_t gid
= (gid_t
) - 1;
177 * Check the given gid, if any
182 if ((grp
= GETGRGID(gid
)) != NULL
&& conf
.checkduplicate
)
183 errx(EX_DATAERR
, "gid `%ju' has already been allocated", (uintmax_t)grp
->gr_gid
);
188 * We need to allocate the next available gid under one of
189 * two policies a) Grab the first unused gid b) Grab the
190 * highest possible unused gid
192 if (cnf
->min_gid
>= cnf
->max_gid
) { /* Sanity claus^H^H^H^Hheck */
194 cnf
->max_gid
= 32000;
196 bm
= bm_alloc(cnf
->max_gid
- cnf
->min_gid
+ 1);
199 * Now, let's fill the bitmap from the password file
202 while ((grp
= GETGRENT()) != NULL
)
203 if ((gid_t
)grp
->gr_gid
>= (gid_t
)cnf
->min_gid
&&
204 (gid_t
)grp
->gr_gid
<= (gid_t
)cnf
->max_gid
)
205 bm_setbit(&bm
, grp
->gr_gid
- cnf
->min_gid
);
209 * Then apply the policy, with fallback to reuse if necessary
212 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
214 gid
= (gid_t
) (bm_lastset(&bm
) + 1);
215 if (!bm_isset(&bm
, gid
))
218 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
222 * Another sanity check
224 if (gid
< cnf
->min_gid
|| gid
> cnf
->max_gid
)
225 errx(EX_SOFTWARE
, "unable to allocate a new gid - range fully used");
231 print_group(struct group
* grp
, bool pretty
)
237 printf("Group Name: %-15s #%lu\n"
239 grp
->gr_name
, (long) grp
->gr_gid
);
240 if (grp
->gr_mem
!= NULL
) {
241 for (i
= 0; grp
->gr_mem
[i
]; i
++)
242 printf("%s%s", i
? "," : "", grp
->gr_mem
[i
]);
244 fputs("\n\n", stdout
);
245 return (EXIT_SUCCESS
);
251 return (EXIT_SUCCESS
);
255 pw_group_next(int argc
, char **argv
, char *arg1 __unused
)
257 struct userconf
*cnf
;
258 const char *cfg
= NULL
;
262 while ((ch
= getopt(argc
, argv
, "Cq")) != -1) {
274 freopen(_PATH_DEVNULL
, "w", stderr
);
275 cnf
= get_userconfig(cfg
);
276 return (pw_groupnext(cnf
, quiet
));
280 pw_group_show(int argc
, char **argv
, char *arg1
)
282 struct group
*grp
= NULL
;
286 bool all
, force
, quiet
, pretty
;
288 all
= force
= quiet
= pretty
= false;
290 struct group fakegroup
= {
298 if (strspn(arg1
, "0123456789") == strlen(arg1
))
299 id
= pw_checkid(arg1
, GID_MAX
);
304 while ((ch
= getopt(argc
, argv
, "C:qn:g:FPa")) != -1) {
307 /* ignore compatibility */
316 id
= pw_checkid(optarg
, GID_MAX
);
331 freopen(_PATH_DEVNULL
, "w", stderr
);
335 while ((grp
= GETGRENT()) != NULL
)
336 print_group(grp
, pretty
);
338 return (EXIT_SUCCESS
);
341 grp
= getgroup(name
, id
, !force
);
345 return (print_group(grp
, pretty
));
349 pw_group_del(int argc
, char **argv
, char *arg1
)
351 struct userconf
*cnf
= NULL
;
352 struct group
*grp
= NULL
;
354 const char *cfg
= NULL
;
361 if (strspn(arg1
, "0123456789") == strlen(arg1
))
362 id
= pw_checkid(arg1
, GID_MAX
);
367 while ((ch
= getopt(argc
, argv
, "C:qn:g:Y")) != -1) {
379 id
= pw_checkid(optarg
, GID_MAX
);
388 freopen(_PATH_DEVNULL
, "w", stderr
);
389 grp
= getgroup(name
, id
, true);
390 cnf
= get_userconfig(cfg
);
393 err(EX_IOERR
, "group '%s' not available (NIS?)", name
);
395 err(EX_IOERR
, "group update");
396 pw_log(cnf
, M_DELETE
, W_GROUP
, "%s(%ju) removed", name
,
399 if (nis
&& nis_update() == 0)
400 pw_log(cnf
, M_DELETE
, W_GROUP
, "NIS maps updated");
402 return (EXIT_SUCCESS
);
406 grp_has_member(struct group
*grp
, const char *name
)
410 for (j
= 0; grp
->gr_mem
!= NULL
&& grp
->gr_mem
[j
] != NULL
; j
++)
411 if (strcmp(grp
->gr_mem
[j
], name
) == 0)
417 grp_add_members(struct group
**grp
, char *members
)
425 for (p
= strtok(members
, tok
); p
!= NULL
; p
= strtok(NULL
, tok
)) {
426 pwd
= lookup_pwent(p
);
427 if (grp_has_member(*grp
, pwd
->pw_name
))
429 *grp
= gr_add(*grp
, pwd
->pw_name
);
434 groupadd(struct userconf
*cnf
, char *name
, gid_t id
, char *members
, int fd
,
435 bool dryrun
, bool pretty
, bool precrypted
)
440 struct group fakegroup
= {
448 grp
->gr_name
= pw_checkname(name
, 0);
449 grp
->gr_passwd
= "*";
450 grp
->gr_gid
= gr_gidpolicy(cnf
, id
);
454 * This allows us to set a group password Group passwords is an
455 * antique idea, rarely used and insecure (no secure database) Should
456 * be discouraged, but it is apparently still supported by some
459 grp_set_passwd(grp
, false, fd
, precrypted
);
460 grp_add_members(&grp
, members
);
462 return (print_group(grp
, pretty
));
464 if ((rc
= addgrent(grp
)) != 0) {
466 errx(EX_IOERR
, "group '%s' already exists",
469 err(EX_IOERR
, "group update");
472 pw_log(cnf
, M_ADD
, W_GROUP
, "%s(%ju)", grp
->gr_name
,
473 (uintmax_t)grp
->gr_gid
);
475 return (EXIT_SUCCESS
);
479 pw_group_add(int argc
, char **argv
, char *arg1
)
481 struct userconf
*cnf
= NULL
;
483 char *members
= NULL
;
484 const char *cfg
= NULL
;
487 bool quiet
, precrypted
, dryrun
, pretty
, nis
;
489 quiet
= precrypted
= dryrun
= pretty
= nis
= false;
492 if (strspn(arg1
, "0123456789") == strlen(arg1
))
493 id
= pw_checkid(arg1
, GID_MAX
);
498 while ((ch
= getopt(argc
, argv
, "C:qn:g:h:H:M:oNPY")) != -1) {
510 id
= pw_checkid(optarg
, GID_MAX
);
514 errx(EX_USAGE
, "'-h' and '-H' are mutually "
515 "exclusive options");
516 fd
= pw_checkfd(optarg
);
519 errx(EX_USAGE
, "-H expects a file descriptor");
523 errx(EX_USAGE
, "'-h' and '-H' are mutually "
524 "exclusive options");
525 fd
= pw_checkfd(optarg
);
531 conf
.checkduplicate
= false;
546 freopen(_PATH_DEVNULL
, "w", stderr
);
548 errx(EX_DATAERR
, "group name required");
549 if (GETGRNAM(name
) != NULL
)
550 errx(EX_DATAERR
, "group name `%s' already exists", name
);
551 cnf
= get_userconfig(cfg
);
552 rc
= groupadd(cnf
, name
, gr_gidpolicy(cnf
, id
), members
, fd
, dryrun
,
554 if (nis
&& rc
== EXIT_SUCCESS
&& nis_update() == 0)
555 pw_log(cnf
, M_ADD
, W_GROUP
, "NIS maps updated");
561 pw_group_mod(int argc
, char **argv
, char *arg1
)
563 struct userconf
*cnf
;
564 struct group
*grp
= NULL
;
565 const char *cfg
= NULL
;
566 char *oldmembers
= NULL
;
567 char *members
= NULL
;
568 char *newmembers
= NULL
;
569 char *newname
= NULL
;
573 bool quiet
, pretty
, dryrun
, nis
, precrypted
;
575 quiet
= pretty
= dryrun
= nis
= precrypted
= false;
578 if (strspn(arg1
, "0123456789") == strlen(arg1
))
579 id
= pw_checkid(arg1
, GID_MAX
);
584 while ((ch
= getopt(argc
, argv
, "C:qn:d:g:l:h:H:M:m:NPY")) != -1) {
596 id
= pw_checkid(optarg
, GID_MAX
);
606 errx(EX_USAGE
, "'-h' and '-H' are mutually "
607 "exclusive options");
608 fd
= pw_checkfd(optarg
);
611 errx(EX_USAGE
, "-H expects a file descriptor");
615 errx(EX_USAGE
, "'-h' and '-H' are mutually "
616 "exclusive options");
617 fd
= pw_checkfd(optarg
);
637 freopen(_PATH_DEVNULL
, "w", stderr
);
638 cnf
= get_userconfig(cfg
);
639 grp
= getgroup(name
, id
, true);
646 grp
->gr_name
= pw_checkname(newname
, 0);
648 grp_set_passwd(grp
, true, fd
, precrypted
);
650 * Keep the same logic as old code for now:
651 * if -M is passed, -d and -m are ignored
652 * then id -d, -m is ignored
658 grp_add_members(&grp
, members
);
659 } else if (oldmembers
) {
660 delete_members(grp
, oldmembers
);
661 } else if (newmembers
) {
662 grp_add_members(&grp
, newmembers
);
665 if ((rc
= chggrent(name
, grp
)) != 0) {
667 errx(EX_IOERR
, "group '%s' not available (NIS?)",
670 err(EX_IOERR
, "group update");
676 /* grp may have been invalidated */
677 if ((grp
= GETGRNAM(name
)) == NULL
)
678 errx(EX_SOFTWARE
, "group disappeared during update");
680 pw_log(cnf
, M_UPDATE
, W_GROUP
, "%s(%ju)", grp
->gr_name
,
681 (uintmax_t)grp
->gr_gid
);
683 if (nis
&& nis_update() == 0)
684 pw_log(cnf
, M_UPDATE
, W_GROUP
, "NIS maps updated");
686 return (EXIT_SUCCESS
);