]>
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
[] =
44 static struct passwd
*lookup_pwent(const char *user
);
45 static void delete_members(char ***members
, int *grmembers
, int *i
,
46 struct carg
*arg
, struct group
*grp
);
47 static int print_group(struct group
* grp
, int pretty
);
48 static gid_t
gr_gidpolicy(struct userconf
* cnf
, struct cargs
* args
);
51 pw_group(struct userconf
* cnf
, int mode
, struct cargs
* args
)
54 struct carg
*a_newname
= getarg(args
, 'l');
55 struct carg
*a_name
= getarg(args
, 'n');
56 struct carg
*a_gid
= getarg(args
, 'g');
58 struct group
*grp
= NULL
;
60 char **members
= NULL
;
62 static struct group fakegroup
=
71 if (strspn(a_gid
->val
, "0123456789") != strlen(a_gid
->val
))
72 errx(EX_USAGE
, "-g expects a number");
75 if (mode
== M_LOCK
|| mode
== M_UNLOCK
)
76 errx(EX_USAGE
, "'lock' command is not available for groups");
79 * With M_NEXT, we only need to return the
83 gid_t next
= gr_gidpolicy(cnf
, args
);
84 if (getarg(args
, 'q'))
90 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
91 int pretty
= getarg(args
, 'P') != NULL
;
94 while ((grp
= GETGRENT()) != NULL
)
95 print_group(grp
, pretty
);
101 errx(EX_DATAERR
, "group name or id required");
103 if (mode
!= M_ADD
&& grp
== NULL
&& isdigit((unsigned char)*a_name
->val
)) {
104 (a_gid
= a_name
)->ch
= 'g';
108 grp
= (a_name
!= NULL
) ? GETGRNAM(a_name
->val
) : GETGRGID((gid_t
) atoi(a_gid
->val
));
110 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
111 if (a_name
== NULL
&& grp
== NULL
) /* Try harder */
112 grp
= GETGRGID(atoi(a_gid
->val
));
115 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
118 fakegroup
.gr_name
= a_name
? a_name
->val
: "nogroup";
119 fakegroup
.gr_gid
= a_gid
? (gid_t
) atol(a_gid
->val
) : (gid_t
)-1;
120 fakegroup
.gr_mem
= fmems
;
121 return print_group(&fakegroup
, getarg(args
, 'P') != NULL
);
123 errx(EX_DATAERR
, "unknown group `%s'", a_name
? a_name
->val
: a_gid
->val
);
125 if (a_name
== NULL
) /* Needed later */
126 a_name
= addarg(args
, 'n', grp
->gr_name
);
129 * Handle deletions now
131 if (mode
== M_DELETE
) {
132 gid_t gid
= grp
->gr_gid
;
136 err(EX_IOERR
, "group '%s' not available (NIS?)", grp
->gr_name
);
138 err(EX_IOERR
, "group update");
140 pw_log(cnf
, mode
, W_GROUP
, "%s(%u) removed", a_name
->val
, gid
);
142 } else if (mode
== M_PRINT
)
143 return print_group(grp
, getarg(args
, 'P') != NULL
);
146 grp
->gr_gid
= (gid_t
) atoi(a_gid
->val
);
148 if (a_newname
!= NULL
)
149 grp
->gr_name
= pw_checkname((u_char
*)a_newname
->val
, 0);
151 if (a_name
== NULL
) /* Required */
152 errx(EX_DATAERR
, "group name required");
153 else if (grp
!= NULL
) /* Exists */
154 errx(EX_DATAERR
, "group name `%s' already exists", a_name
->val
);
156 extendarray(&members
, &grmembers
, 200);
159 grp
->gr_name
= pw_checkname((u_char
*)a_name
->val
, 0);
160 grp
->gr_passwd
= "*";
161 grp
->gr_gid
= gr_gidpolicy(cnf
, args
);
162 grp
->gr_mem
= members
;
166 * This allows us to set a group password Group passwords is an
167 * antique idea, rarely used and insecure (no secure database) Should
168 * be discouraged, but it is apparently still supported by some
172 if ((arg
= getarg(args
, 'h')) != NULL
||
173 (arg
= getarg(args
, 'H')) != NULL
) {
174 if (strcmp(arg
->val
, "-") == 0)
175 grp
->gr_passwd
= "*"; /* No access */
177 int fd
= atoi(arg
->val
);
178 int precrypt
= (arg
->ch
== 'H');
180 int istty
= isatty(fd
);
185 if (tcgetattr(fd
, &t
) == -1)
188 struct termios n
= t
;
191 n
.c_lflag
&= ~(ECHO
);
192 tcsetattr(fd
, TCSANOW
, &n
);
193 printf("%sassword for group %s:", (mode
== M_UPDATE
) ? "New p" : "P", grp
->gr_name
);
197 b
= read(fd
, line
, sizeof(line
) - 1);
198 if (istty
) { /* Restore state */
199 tcsetattr(fd
, TCSANOW
, &t
);
204 err(EX_OSERR
, "-h file descriptor");
206 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
209 errx(EX_DATAERR
, "empty password read on file descriptor %d", fd
);
211 if (strchr(line
, ':') != NULL
)
213 grp
->gr_passwd
= line
;
215 grp
->gr_passwd
= pw_pwcrypt(line
);
219 if (((arg
= getarg(args
, 'M')) != NULL
||
220 (arg
= getarg(args
, 'd')) != NULL
||
221 (arg
= getarg(args
, 'm')) != NULL
) && arg
->val
) {
226 /* Make sure this is not stay NULL with -M "" */
227 extendarray(&members
, &grmembers
, 200);
229 delete_members(&members
, &grmembers
, &i
, arg
, grp
);
230 else if (arg
->ch
== 'm') {
233 if (grp
->gr_mem
!= NULL
) {
234 while (grp
->gr_mem
[k
] != NULL
) {
235 if (extendarray(&members
, &grmembers
, i
+ 2) != -1)
236 members
[i
++] = grp
->gr_mem
[k
];
243 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
247 * Check for duplicates
249 pwd
= lookup_pwent(p
);
250 for (j
= 0; j
< i
&& strcmp(members
[j
], pwd
->pw_name
) != 0; j
++)
252 if (j
== i
&& extendarray(&members
, &grmembers
, i
+ 2) != -1)
253 members
[i
++] = newstr(pwd
->pw_name
);
255 while (i
< grmembers
)
257 grp
->gr_mem
= members
;
260 if (getarg(args
, 'N') != NULL
)
261 return print_group(grp
, getarg(args
, 'P') != NULL
);
263 if (mode
== M_ADD
&& (rc
= addgrent(grp
)) != 0) {
265 errx(EX_IOERR
, "group '%s' already exists",
268 err(EX_IOERR
, "group update");
269 } else if (mode
== M_UPDATE
&& (rc
= chggrent(a_name
->val
, grp
)) != 0) {
271 errx(EX_IOERR
, "group '%s' not available (NIS?)",
274 err(EX_IOERR
, "group update");
277 arg
= a_newname
!= NULL
? a_newname
: a_name
;
278 /* grp may have been invalidated */
279 if ((grp
= GETGRNAM(arg
->val
)) == NULL
)
280 errx(EX_SOFTWARE
, "group disappeared during update");
282 pw_log(cnf
, mode
, W_GROUP
, "%s(%u)", grp
->gr_name
, grp
->gr_gid
);
291 * Lookup a passwd entry using a name or UID.
293 static struct passwd
*
294 lookup_pwent(const char *user
)
298 if ((pwd
= GETPWNAM(user
)) == NULL
&&
299 (!isdigit((unsigned char)*user
) ||
300 (pwd
= getpwuid((uid_t
) atoi(user
))) == NULL
))
301 errx(EX_NOUSER
, "user `%s' does not exist", user
);
308 * Delete requested members from a group.
311 delete_members(char ***members
, int *grmembers
, int *i
, struct carg
*arg
,
321 if (grp
->gr_mem
== NULL
)
325 while (grp
->gr_mem
[k
] != NULL
) {
327 if ((valueCopy
= strdup(arg
->val
)) == NULL
)
328 errx(EX_UNAVAILABLE
, "out of memory");
329 valuePtr
= valueCopy
;
330 while ((user
= strsep(&valuePtr
, ", \t")) != NULL
) {
331 pwd
= lookup_pwent(user
);
332 if (strcmp(grp
->gr_mem
[k
], pwd
->pw_name
) == 0) {
340 extendarray(members
, grmembers
, *i
+ 2) != -1)
341 (*members
)[(*i
)++] = grp
->gr_mem
[k
];
351 gr_gidpolicy(struct userconf
* cnf
, struct cargs
* args
)
354 gid_t gid
= (gid_t
) - 1;
355 struct carg
*a_gid
= getarg(args
, 'g');
358 * Check the given gid, if any
361 gid
= (gid_t
) atol(a_gid
->val
);
363 if ((grp
= GETGRGID(gid
)) != NULL
&& getarg(args
, 'o') == NULL
)
364 errx(EX_DATAERR
, "gid `%u' has already been allocated", grp
->gr_gid
);
369 * We need to allocate the next available gid under one of
370 * two policies a) Grab the first unused gid b) Grab the
371 * highest possible unused gid
373 if (cnf
->min_gid
>= cnf
->max_gid
) { /* Sanity claus^H^H^H^Hheck */
375 cnf
->max_gid
= 32000;
377 bm
= bm_alloc(cnf
->max_gid
- cnf
->min_gid
+ 1);
380 * Now, let's fill the bitmap from the password file
383 while ((grp
= GETGRENT()) != NULL
)
384 if ((gid_t
)grp
->gr_gid
>= (gid_t
)cnf
->min_gid
&&
385 (gid_t
)grp
->gr_gid
<= (gid_t
)cnf
->max_gid
)
386 bm_setbit(&bm
, grp
->gr_gid
- cnf
->min_gid
);
390 * Then apply the policy, with fallback to reuse if necessary
393 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
395 gid
= (gid_t
) (bm_lastset(&bm
) + 1);
396 if (!bm_isset(&bm
, gid
))
399 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
403 * Another sanity check
405 if (gid
< cnf
->min_gid
|| gid
> cnf
->max_gid
)
406 errx(EX_SOFTWARE
, "unable to allocate a new gid - range fully used");
414 print_group(struct group
* grp
, int pretty
)
425 printf("Group Name: %-15s #%lu\n"
427 grp
->gr_name
, (long) grp
->gr_gid
);
428 if (grp
->gr_mem
!= NULL
) {
429 for (i
= 0; grp
->gr_mem
[i
]; i
++)
430 printf("%s%s", i
? "," : "", grp
->gr_mem
[i
]);
432 fputs("\n\n", stdout
);