]>
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
[] =
41 static int print_group(struct group
* grp
, int pretty
);
42 static gid_t
gr_gidpolicy(struct userconf
* cnf
, struct cargs
* args
);
45 pw_group(struct userconf
* cnf
, int mode
, struct cargs
* args
)
48 struct carg
*a_name
= getarg(args
, 'n');
49 struct carg
*a_gid
= getarg(args
, 'g');
51 struct group
*grp
= NULL
;
53 char **members
= NULL
;
55 static struct group fakegroup
=
63 if (mode
== M_LOCK
|| mode
== M_UNLOCK
)
64 errx(EX_USAGE
, "'lock' command is not available for groups");
67 * With M_NEXT, we only need to return the
71 gid_t next
= gr_gidpolicy(cnf
, args
);
72 if (getarg(args
, 'q'))
74 printf("%ld\n", (long)next
);
78 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
79 int pretty
= getarg(args
, 'P') != NULL
;
82 while ((grp
= GETGRENT()) != NULL
)
83 print_group(grp
, pretty
);
89 errx(EX_DATAERR
, "group name or id required");
91 if (mode
!= M_ADD
&& grp
== NULL
&& isdigit((unsigned char)*a_name
->val
)) {
92 (a_gid
= a_name
)->ch
= 'g';
96 grp
= (a_name
!= NULL
) ? GETGRNAM(a_name
->val
) : GETGRGID((gid_t
) atoi(a_gid
->val
));
98 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
99 if (a_name
== NULL
&& grp
== NULL
) /* Try harder */
100 grp
= GETGRGID(atoi(a_gid
->val
));
103 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
106 fakegroup
.gr_name
= a_name
? a_name
->val
: "nogroup";
107 fakegroup
.gr_gid
= a_gid
? (gid_t
) atol(a_gid
->val
) : -1;
108 fakegroup
.gr_mem
= fmems
;
109 return print_group(&fakegroup
, getarg(args
, 'P') != NULL
);
111 errx(EX_DATAERR
, "unknown group `%s'", a_name
? a_name
->val
: a_gid
->val
);
113 if (a_name
== NULL
) /* Needed later */
114 a_name
= addarg(args
, 'n', grp
->gr_name
);
117 * Handle deletions now
119 if (mode
== M_DELETE
) {
120 gid_t gid
= grp
->gr_gid
;
124 err(EX_IOERR
, "group '%s' not available (NIS?)", grp
->gr_name
);
126 warn("group update");
129 pw_log(cnf
, mode
, W_GROUP
, "%s(%ld) removed", a_name
->val
, (long) gid
);
131 } else if (mode
== M_PRINT
)
132 return print_group(grp
, getarg(args
, 'P') != NULL
);
135 grp
->gr_gid
= (gid_t
) atoi(a_gid
->val
);
137 if ((arg
= getarg(args
, 'l')) != NULL
)
138 grp
->gr_name
= pw_checkname((u_char
*)arg
->val
, 0);
140 if (a_name
== NULL
) /* Required */
141 errx(EX_DATAERR
, "group name required");
142 else if (grp
!= NULL
) /* Exists */
143 errx(EX_DATAERR
, "group name `%s' already exists", a_name
->val
);
145 extendarray(&members
, &grmembers
, 200);
148 grp
->gr_name
= pw_checkname((u_char
*)a_name
->val
, 0);
149 grp
->gr_passwd
= "*";
150 grp
->gr_gid
= gr_gidpolicy(cnf
, args
);
151 grp
->gr_mem
= members
;
155 * This allows us to set a group password Group passwords is an
156 * antique idea, rarely used and insecure (no secure database) Should
157 * be discouraged, but it is apparently still supported by some
161 if ((arg
= getarg(args
, 'h')) != NULL
) {
162 if (strcmp(arg
->val
, "-") == 0)
163 grp
->gr_passwd
= "*"; /* No access */
165 int fd
= atoi(arg
->val
);
167 int istty
= isatty(fd
);
172 if (tcgetattr(fd
, &t
) == -1)
175 struct termios n
= t
;
178 n
.c_lflag
&= ~(ECHO
);
179 tcsetattr(fd
, TCSANOW
, &n
);
180 printf("%sassword for group %s:", (mode
== M_UPDATE
) ? "New p" : "P", grp
->gr_name
);
184 b
= read(fd
, line
, sizeof(line
) - 1);
185 if (istty
) { /* Restore state */
186 tcsetattr(fd
, TCSANOW
, &t
);
191 warn("-h file descriptor");
195 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
198 errx(EX_DATAERR
, "empty password read on file descriptor %d", fd
);
199 grp
->gr_passwd
= pw_pwcrypt(line
);
203 if (((arg
= getarg(args
, 'M')) != NULL
|| (arg
= getarg(args
, 'm')) != NULL
) && arg
->val
) {
208 /* Make sure this is not stay NULL with -M "" */
209 extendarray(&members
, &grmembers
, 200);
210 if (arg
->ch
== 'm') {
213 while (grp
->gr_mem
[k
] != NULL
) {
214 if (extendarray(&members
, &grmembers
, i
+ 2) != -1) {
215 members
[i
++] = grp
->gr_mem
[k
];
220 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
222 if ((pwd
= GETPWNAM(p
)) == NULL
) {
223 if (!isdigit((unsigned char)*p
) || (pwd
= getpwuid((uid_t
) atoi(p
))) == NULL
)
224 errx(EX_NOUSER
, "user `%s' does not exist", p
);
227 * Check for duplicates
229 for (j
= 0; j
< i
&& strcmp(members
[j
], pwd
->pw_name
)!=0; j
++)
231 if (j
== i
&& extendarray(&members
, &grmembers
, i
+ 2) != -1)
232 members
[i
++] = newstr(pwd
->pw_name
);
234 while (i
< grmembers
)
236 grp
->gr_mem
= members
;
239 if (getarg(args
, 'N') != NULL
)
240 return print_group(grp
, getarg(args
, 'P') != NULL
);
242 if (mode
== M_ADD
&& (rc
= addgrent(grp
)) != 0) {
244 warnx("group '%s' already exists", grp
->gr_name
);
246 warn("group update");
248 } else if (mode
== M_UPDATE
&& (rc
= chggrent(a_name
->val
, grp
)) != 0) {
250 warnx("group '%s' not available (NIS?)", grp
->gr_name
);
252 warn("group update");
255 /* grp may have been invalidated */
256 if ((grp
= GETGRNAM(a_name
->val
)) == NULL
)
257 errx(EX_SOFTWARE
, "group disappeared during update");
259 pw_log(cnf
, mode
, W_GROUP
, "%s(%ld)", grp
->gr_name
, (long) grp
->gr_gid
);
269 gr_gidpolicy(struct userconf
* cnf
, struct cargs
* args
)
272 gid_t gid
= (gid_t
) - 1;
273 struct carg
*a_gid
= getarg(args
, 'g');
276 * Check the given gid, if any
279 gid
= (gid_t
) atol(a_gid
->val
);
281 if ((grp
= GETGRGID(gid
)) != NULL
&& getarg(args
, 'o') == NULL
)
282 errx(EX_DATAERR
, "gid `%ld' has already been allocated", (long) grp
->gr_gid
);
287 * We need to allocate the next available gid under one of
288 * two policies a) Grab the first unused gid b) Grab the
289 * highest possible unused gid
291 if (cnf
->min_gid
>= cnf
->max_gid
) { /* Sanity claus^H^H^H^Hheck */
293 cnf
->max_gid
= 32000;
295 bm
= bm_alloc(cnf
->max_gid
- cnf
->min_gid
+ 1);
298 * Now, let's fill the bitmap from the password file
301 while ((grp
= GETGRENT()) != NULL
)
302 if ((gid_t
)grp
->gr_gid
>= (gid_t
)cnf
->min_gid
&&
303 (gid_t
)grp
->gr_gid
<= (gid_t
)cnf
->max_gid
)
304 bm_setbit(&bm
, grp
->gr_gid
- cnf
->min_gid
);
308 * Then apply the policy, with fallback to reuse if necessary
311 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
313 gid
= (gid_t
) (bm_lastset(&bm
) + 1);
314 if (!bm_isset(&bm
, gid
))
317 gid
= (gid_t
) (bm_firstunset(&bm
) + cnf
->min_gid
);
321 * Another sanity check
323 if (gid
< cnf
->min_gid
|| gid
> cnf
->max_gid
)
324 errx(EX_SOFTWARE
, "unable to allocate a new gid - range fully used");
332 print_group(struct group
* grp
, int pretty
)
338 fmtgrent(&buf
, &buflen
, grp
);
344 printf("Group Name: %-15s #%lu\n"
346 grp
->gr_name
, (long) grp
->gr_gid
);
347 for (i
= 0; grp
->gr_mem
[i
]; i
++)
348 printf("%s%s", i
? "," : "", grp
->gr_mem
[i
]);
349 fputs("\n\n", stdout
);