]>
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",
184 (uintmax_t)grp
->gr_gid
);
189 * We need to allocate the next available gid under one of
190 * two policies a) Grab the first unused gid b) Grab the
191 * highest possible unused gid
193 if (cnf
->min_gid
>= cnf
->max_gid
) { /* Sanity claus^H^H^H^Hheck */
195 cnf
->max_gid
= 32000;
197 bm
= bm_alloc(cnf
->max_gid
- cnf
->min_gid
+ 1);
200 * Now, let's fill the bitmap from the password file
203 while ((grp
= GETGRENT()) != NULL
)
204 if ((gid_t
)grp
->gr_gid
>= (gid_t
)cnf
->min_gid
&&
205 (gid_t
)grp
->gr_gid
<= (gid_t
)cnf
->max_gid
)
206 bm_setbit(&bm
, grp
->gr_gid
- cnf
->min_gid
);
210 * Then apply the policy, with fallback to reuse if necessary
213 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
215 gid
= (gid_t
) (bm_lastset(&bm
) + 1);
216 if (!bm_isset(&bm
, gid
))
219 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
223 * Another sanity check
225 if (gid
< cnf
->min_gid
|| gid
> cnf
->max_gid
)
226 errx(EX_SOFTWARE
, "unable to allocate a new gid - range fully "
233 print_group(struct group
* grp
, bool pretty
)
239 printf("Group Name: %-15s #%lu\n"
241 grp
->gr_name
, (long) grp
->gr_gid
);
242 if (grp
->gr_mem
!= NULL
) {
243 for (i
= 0; grp
->gr_mem
[i
]; i
++)
244 printf("%s%s", i
? "," : "", grp
->gr_mem
[i
]);
246 fputs("\n\n", stdout
);
247 return (EXIT_SUCCESS
);
253 return (EXIT_SUCCESS
);
257 pw_group_next(int argc
, char **argv
, char *arg1 __unused
)
259 struct userconf
*cnf
;
260 const char *cfg
= NULL
;
264 while ((ch
= getopt(argc
, argv
, "C:q")) != -1) {
276 freopen(_PATH_DEVNULL
, "w", stderr
);
277 cnf
= get_userconfig(cfg
);
278 return (pw_groupnext(cnf
, quiet
));
282 pw_group_show(int argc
, char **argv
, char *arg1
)
284 struct group
*grp
= NULL
;
288 bool all
, force
, quiet
, pretty
;
290 all
= force
= quiet
= pretty
= false;
292 struct group fakegroup
= {
300 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
301 id
= pw_checkid(arg1
, GID_MAX
);
306 while ((ch
= getopt(argc
, argv
, "C:qn:g:FPa")) != -1) {
309 /* ignore compatibility */
318 id
= pw_checkid(optarg
, GID_MAX
);
333 freopen(_PATH_DEVNULL
, "w", stderr
);
337 while ((grp
= GETGRENT()) != NULL
)
338 print_group(grp
, pretty
);
340 return (EXIT_SUCCESS
);
343 grp
= getgroup(name
, id
, !force
);
347 return (print_group(grp
, pretty
));
351 pw_group_del(int argc
, char **argv
, char *arg1
)
353 struct userconf
*cnf
= NULL
;
354 struct group
*grp
= NULL
;
356 const char *cfg
= NULL
;
363 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
364 id
= pw_checkid(arg1
, GID_MAX
);
369 while ((ch
= getopt(argc
, argv
, "C:qn:g:Y")) != -1) {
381 id
= pw_checkid(optarg
, GID_MAX
);
390 freopen(_PATH_DEVNULL
, "w", stderr
);
391 grp
= getgroup(name
, id
, true);
392 cnf
= get_userconfig(cfg
);
395 err(EX_IOERR
, "group '%s' not available (NIS?)", name
);
397 err(EX_IOERR
, "group update");
398 pw_log(cnf
, M_DELETE
, W_GROUP
, "%s(%ju) removed", name
,
401 if (nis
&& nis_update() == 0)
402 pw_log(cnf
, M_DELETE
, W_GROUP
, "NIS maps updated");
404 return (EXIT_SUCCESS
);
408 grp_has_member(struct group
*grp
, const char *name
)
412 for (j
= 0; grp
->gr_mem
!= NULL
&& grp
->gr_mem
[j
] != NULL
; j
++)
413 if (strcmp(grp
->gr_mem
[j
], name
) == 0)
419 grp_add_members(struct group
**grp
, char *members
)
427 for (p
= strtok(members
, tok
); p
!= NULL
; p
= strtok(NULL
, tok
)) {
428 pwd
= lookup_pwent(p
);
429 if (grp_has_member(*grp
, pwd
->pw_name
))
431 *grp
= gr_add(*grp
, pwd
->pw_name
);
436 groupadd(struct userconf
*cnf
, char *name
, gid_t id
, char *members
, int fd
,
437 bool dryrun
, bool pretty
, bool precrypted
)
442 struct group fakegroup
= {
450 grp
->gr_name
= pw_checkname(name
, 0);
451 grp
->gr_passwd
= "*";
452 grp
->gr_gid
= gr_gidpolicy(cnf
, id
);
456 * This allows us to set a group password Group passwords is an
457 * antique idea, rarely used and insecure (no secure database) Should
458 * be discouraged, but it is apparently still supported by some
461 grp_set_passwd(grp
, false, fd
, precrypted
);
462 grp_add_members(&grp
, members
);
464 return (print_group(grp
, pretty
));
466 if ((rc
= addgrent(grp
)) != 0) {
468 errx(EX_IOERR
, "group '%s' already exists",
471 err(EX_IOERR
, "group update");
474 pw_log(cnf
, M_ADD
, W_GROUP
, "%s(%ju)", grp
->gr_name
,
475 (uintmax_t)grp
->gr_gid
);
477 return (EXIT_SUCCESS
);
481 pw_group_add(int argc
, char **argv
, char *arg1
)
483 struct userconf
*cnf
= NULL
;
485 char *members
= NULL
;
486 const char *cfg
= NULL
;
489 bool quiet
, precrypted
, dryrun
, pretty
, nis
;
491 quiet
= precrypted
= dryrun
= pretty
= nis
= false;
494 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
495 id
= pw_checkid(arg1
, GID_MAX
);
500 while ((ch
= getopt(argc
, argv
, "C:qn:g:h:H:M:oNPY")) != -1) {
512 id
= pw_checkid(optarg
, GID_MAX
);
516 errx(EX_USAGE
, "'-h' and '-H' are mutually "
517 "exclusive options");
518 fd
= pw_checkfd(optarg
);
521 errx(EX_USAGE
, "-H expects a file descriptor");
525 errx(EX_USAGE
, "'-h' and '-H' are mutually "
526 "exclusive options");
527 fd
= pw_checkfd(optarg
);
533 conf
.checkduplicate
= false;
548 freopen(_PATH_DEVNULL
, "w", stderr
);
550 errx(EX_DATAERR
, "group name required");
551 if (GETGRNAM(name
) != NULL
)
552 errx(EX_DATAERR
, "group name `%s' already exists", name
);
553 cnf
= get_userconfig(cfg
);
554 rc
= groupadd(cnf
, name
, gr_gidpolicy(cnf
, id
), members
, fd
, dryrun
,
556 if (nis
&& rc
== EXIT_SUCCESS
&& nis_update() == 0)
557 pw_log(cnf
, M_ADD
, W_GROUP
, "NIS maps updated");
563 pw_group_mod(int argc
, char **argv
, char *arg1
)
565 struct userconf
*cnf
;
566 struct group
*grp
= NULL
;
567 const char *cfg
= NULL
;
568 char *oldmembers
= NULL
;
569 char *members
= NULL
;
570 char *newmembers
= NULL
;
571 char *newname
= NULL
;
575 bool quiet
, pretty
, dryrun
, nis
, precrypted
;
577 quiet
= pretty
= dryrun
= nis
= precrypted
= false;
580 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
581 id
= pw_checkid(arg1
, GID_MAX
);
586 while ((ch
= getopt(argc
, argv
, "C:qn:d:g:l:h:H:M:m:NPY")) != -1) {
598 id
= pw_checkid(optarg
, GID_MAX
);
608 errx(EX_USAGE
, "'-h' and '-H' are mutually "
609 "exclusive options");
610 fd
= pw_checkfd(optarg
);
613 errx(EX_USAGE
, "-H expects a file descriptor");
617 errx(EX_USAGE
, "'-h' and '-H' are mutually "
618 "exclusive options");
619 fd
= pw_checkfd(optarg
);
639 freopen(_PATH_DEVNULL
, "w", stderr
);
640 cnf
= get_userconfig(cfg
);
641 grp
= getgroup(name
, id
, true);
648 grp
->gr_name
= pw_checkname(newname
, 0);
650 grp_set_passwd(grp
, true, fd
, precrypted
);
652 * Keep the same logic as old code for now:
653 * if -M is passed, -d and -m are ignored
654 * then id -d, -m is ignored
660 grp_add_members(&grp
, members
);
661 } else if (oldmembers
) {
662 delete_members(grp
, oldmembers
);
663 } else if (newmembers
) {
664 grp_add_members(&grp
, newmembers
);
668 print_group(grp
, pretty
);
669 return (EXIT_SUCCESS
);
672 if ((rc
= chggrent(name
, grp
)) != 0) {
674 errx(EX_IOERR
, "group '%s' not available (NIS?)",
677 err(EX_IOERR
, "group update");
683 /* grp may have been invalidated */
684 if ((grp
= GETGRNAM(name
)) == NULL
)
685 errx(EX_SOFTWARE
, "group disappeared during update");
687 pw_log(cnf
, M_UPDATE
, W_GROUP
, "%s(%ju)", grp
->gr_name
,
688 (uintmax_t)grp
->gr_gid
);
690 if (nis
&& nis_update() == 0)
691 pw_log(cnf
, M_UPDATE
, W_GROUP
, "NIS maps updated");
693 return (EXIT_SUCCESS
);