pw_group.c (30259) | pw_group.c (44229) |
---|---|
1/*- 2 * Copyright (C) 1996 3 * David L. Nugent. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 12 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = | 1/*- 2 * Copyright (C) 1996 3 * David L. Nugent. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 12 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = |
29 "$Id$"; | 29 "$Id: pw_group.c,v 1.7 1997/10/10 06:23:36 charnier Exp $"; |
30#endif /* not lint */ 31 32#include <ctype.h> 33#include <err.h> 34#include <termios.h> 35#include <unistd.h> 36 37#include "pw.h" 38#include "bitmap.h" | 30#endif /* not lint */ 31 32#include <ctype.h> 33#include <err.h> 34#include <termios.h> 35#include <unistd.h> 36 37#include "pw.h" 38#include "bitmap.h" |
39#include "pwupd.h" | |
40 41 42static int print_group(struct group * grp, int pretty); 43static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 44 45int 46pw_group(struct userconf * cnf, int mode, struct cargs * args) 47{ --- 23 unchanged lines hidden (view full) --- 71 return next; 72 printf("%ld\n", (long)next); 73 return EXIT_SUCCESS; 74 } 75 76 if (mode == M_PRINT && getarg(args, 'a')) { 77 int pretty = getarg(args, 'P') != NULL; 78 | 39 40 41static int print_group(struct group * grp, int pretty); 42static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 43 44int 45pw_group(struct userconf * cnf, int mode, struct cargs * args) 46{ --- 23 unchanged lines hidden (view full) --- 70 return next; 71 printf("%ld\n", (long)next); 72 return EXIT_SUCCESS; 73 } 74 75 if (mode == M_PRINT && getarg(args, 'a')) { 76 int pretty = getarg(args, 'P') != NULL; 77 |
79 setgrent(); 80 while ((grp = getgrent()) != NULL) | 78 SETGRENT(); 79 while ((grp = GETGRENT()) != NULL) |
81 print_group(grp, pretty); | 80 print_group(grp, pretty); |
82 endgrent(); | 81 ENDGRENT(); |
83 return EXIT_SUCCESS; 84 } 85 if (a_gid == NULL) { 86 if (a_name == NULL) 87 errx(EX_DATAERR, "group name or id required"); 88 89 if (mode != M_ADD && grp == NULL && isdigit(*a_name->val)) { 90 (a_gid = a_name)->ch = 'g'; 91 a_name = NULL; 92 } 93 } | 82 return EXIT_SUCCESS; 83 } 84 if (a_gid == NULL) { 85 if (a_name == NULL) 86 errx(EX_DATAERR, "group name or id required"); 87 88 if (mode != M_ADD && grp == NULL && isdigit(*a_name->val)) { 89 (a_gid = a_name)->ch = 'g'; 90 a_name = NULL; 91 } 92 } |
94 grp = (a_name != NULL) ? getgrnam(a_name->val) : getgrgid((gid_t) atoi(a_gid->val)); | 93 grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val)); |
95 96 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 97 if (a_name == NULL && grp == NULL) /* Try harder */ | 94 95 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 96 if (a_name == NULL && grp == NULL) /* Try harder */ |
98 grp = getgrgid(atoi(a_gid->val)); | 97 grp = GETGRGID(atoi(a_gid->val)); |
99 100 if (grp == NULL) { 101 if (mode == M_PRINT && getarg(args, 'F')) { 102 char *fmems[1]; 103 fmems[0] = NULL; 104 fakegroup.gr_name = a_name ? a_name->val : "nogroup"; 105 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1; 106 fakegroup.gr_mem = fmems; --- 100 unchanged lines hidden (view full) --- 207 if (extendarray(&members, &grmembers, i + 2) != -1) { 208 members[i++] = grp->gr_mem[k]; 209 } 210 k++; 211 } 212 } 213 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 214 int j; | 98 99 if (grp == NULL) { 100 if (mode == M_PRINT && getarg(args, 'F')) { 101 char *fmems[1]; 102 fmems[0] = NULL; 103 fakegroup.gr_name = a_name ? a_name->val : "nogroup"; 104 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1; 105 fakegroup.gr_mem = fmems; --- 100 unchanged lines hidden (view full) --- 206 if (extendarray(&members, &grmembers, i + 2) != -1) { 207 members[i++] = grp->gr_mem[k]; 208 } 209 k++; 210 } 211 } 212 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 213 int j; |
215 if ((pwd = getpwnam(p)) == NULL) { | 214 if ((pwd = GETPWNAM(p)) == NULL) { |
216 if (!isdigit(*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL) 217 errx(EX_NOUSER, "user `%s' does not exist", p); 218 } 219 /* 220 * Check for duplicates 221 */ 222 for (j = 0; j < i && strcmp(members[j], pwd->pw_name)!=0; j++) 223 ; --- 8 unchanged lines hidden (view full) --- 232 if (getarg(args, 'N') != NULL) 233 return print_group(grp, getarg(args, 'P') != NULL); 234 235 if ((mode == M_ADD && !addgrent(grp)) || (mode == M_UPDATE && !chggrent(a_name->val, grp))) { 236 warn("group update"); 237 return EX_IOERR; 238 } 239 /* grp may have been invalidated */ | 215 if (!isdigit(*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL) 216 errx(EX_NOUSER, "user `%s' does not exist", p); 217 } 218 /* 219 * Check for duplicates 220 */ 221 for (j = 0; j < i && strcmp(members[j], pwd->pw_name)!=0; j++) 222 ; --- 8 unchanged lines hidden (view full) --- 231 if (getarg(args, 'N') != NULL) 232 return print_group(grp, getarg(args, 'P') != NULL); 233 234 if ((mode == M_ADD && !addgrent(grp)) || (mode == M_UPDATE && !chggrent(a_name->val, grp))) { 235 warn("group update"); 236 return EX_IOERR; 237 } 238 /* grp may have been invalidated */ |
240 if ((grp = getgrnam(a_name->val)) == NULL) | 239 if ((grp = GETGRNAM(a_name->val)) == NULL) |
241 errx(EX_SOFTWARE, "group disappeared during update"); 242 243 pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); 244 245 if (members) 246 free(members); 247 248 return EXIT_SUCCESS; --- 8 unchanged lines hidden (view full) --- 257 struct carg *a_gid = getarg(args, 'g'); 258 259 /* 260 * Check the given gid, if any 261 */ 262 if (a_gid != NULL) { 263 gid = (gid_t) atol(a_gid->val); 264 | 240 errx(EX_SOFTWARE, "group disappeared during update"); 241 242 pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); 243 244 if (members) 245 free(members); 246 247 return EXIT_SUCCESS; --- 8 unchanged lines hidden (view full) --- 256 struct carg *a_gid = getarg(args, 'g'); 257 258 /* 259 * Check the given gid, if any 260 */ 261 if (a_gid != NULL) { 262 gid = (gid_t) atol(a_gid->val); 263 |
265 if ((grp = getgrgid(gid)) != NULL && getarg(args, 'o') == NULL) | 264 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) |
266 errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); 267 } else { 268 struct bitmap bm; 269 270 /* 271 * We need to allocate the next available gid under one of 272 * two policies a) Grab the first unused gid b) Grab the 273 * highest possible unused gid 274 */ 275 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 276 cnf->min_gid = 1000; 277 cnf->max_gid = 32000; 278 } 279 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 280 281 /* 282 * Now, let's fill the bitmap from the password file 283 */ | 265 errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); 266 } else { 267 struct bitmap bm; 268 269 /* 270 * We need to allocate the next available gid under one of 271 * two policies a) Grab the first unused gid b) Grab the 272 * highest possible unused gid 273 */ 274 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 275 cnf->min_gid = 1000; 276 cnf->max_gid = 32000; 277 } 278 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 279 280 /* 281 * Now, let's fill the bitmap from the password file 282 */ |
284 setgrent(); 285 while ((grp = getgrent()) != NULL) | 283 SETGRENT(); 284 while ((grp = GETGRENT()) != NULL) |
286 if (grp->gr_gid >= (int) cnf->min_gid && grp->gr_gid <= (int) cnf->max_gid) 287 bm_setbit(&bm, grp->gr_gid - cnf->min_gid); | 285 if (grp->gr_gid >= (int) cnf->min_gid && grp->gr_gid <= (int) cnf->max_gid) 286 bm_setbit(&bm, grp->gr_gid - cnf->min_gid); |
288 endgrent(); | 287 ENDGRENT(); |
289 290 /* 291 * Then apply the policy, with fallback to reuse if necessary 292 */ 293 if (cnf->reuse_gids) 294 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 295 else { 296 gid = (gid_t) (bm_lastset(&bm) + 1); --- 39 unchanged lines hidden --- | 288 289 /* 290 * Then apply the policy, with fallback to reuse if necessary 291 */ 292 if (cnf->reuse_gids) 293 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 294 else { 295 gid = (gid_t) (bm_lastset(&bm) + 1); --- 39 unchanged lines hidden --- |