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 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #ifndef lint 28 static const char rcsid[] = 29 "$Id$"; 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 42 static int print_group(struct group * grp, int pretty); 43 static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 44 45 int 46 pw_group(struct userconf * cnf, int mode, struct cargs * args) 47 { 48 struct carg *a_name = getarg(args, 'n'); 49 struct carg *a_gid = getarg(args, 'g'); 50 struct carg *arg; 51 struct group *grp = NULL; 52 int grmembers = 0; 53 char **members = NULL; 54 55 static struct group fakegroup = 56 { 57 "nogroup", 58 "*", 59 -1, 60 NULL 61 }; 62 63 /* 64 * With M_NEXT, we only need to return the 65 * next gid to stdout 66 */ 67 if (mode == M_NEXT) 68 { 69 gid_t next = gr_gidpolicy(cnf, args); 70 if (getarg(args, 'q')) 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 79 setgrent(); 80 while ((grp = getgrent()) != NULL) 81 print_group(grp, pretty); 82 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 } 94 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 */ 98 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; 107 return print_group(&fakegroup, getarg(args, 'P') != NULL); 108 } 109 errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val); 110 } 111 if (a_name == NULL) /* Needed later */ 112 a_name = addarg(args, 'n', grp->gr_name); 113 114 /* 115 * Handle deletions now 116 */ 117 if (mode == M_DELETE) { 118 gid_t gid = grp->gr_gid; 119 120 if (delgrent(grp) == -1) 121 err(EX_IOERR, "error updating group file"); 122 pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid); 123 return EXIT_SUCCESS; 124 } else if (mode == M_PRINT) 125 return print_group(grp, getarg(args, 'P') != NULL); 126 127 if (a_gid) 128 grp->gr_gid = (gid_t) atoi(a_gid->val); 129 130 if ((arg = getarg(args, 'l')) != NULL) 131 grp->gr_name = pw_checkname((u_char *)arg->val, 0); 132 } else { 133 if (a_name == NULL) /* Required */ 134 errx(EX_DATAERR, "group name required"); 135 else if (grp != NULL) /* Exists */ 136 errx(EX_DATAERR, "group name `%s' already exists", a_name->val); 137 138 extendarray(&members, &grmembers, 200); 139 members[0] = NULL; 140 grp = &fakegroup; 141 grp->gr_name = pw_checkname((u_char *)a_name->val, 0); 142 grp->gr_passwd = "*"; 143 grp->gr_gid = gr_gidpolicy(cnf, args); 144 grp->gr_mem = members; 145 } 146 147 /* 148 * This allows us to set a group password Group passwords is an 149 * antique idea, rarely used and insecure (no secure database) Should 150 * be discouraged, but it is apparently still supported by some 151 * software. 152 */ 153 154 if ((arg = getarg(args, 'h')) != NULL) { 155 if (strcmp(arg->val, "-") == 0) 156 grp->gr_passwd = "*"; /* No access */ 157 else { 158 int fd = atoi(arg->val); 159 int b; 160 int istty = isatty(fd); 161 struct termios t; 162 char *p, line[256]; 163 164 if (istty) { 165 if (tcgetattr(fd, &t) == -1) 166 istty = 0; 167 else { 168 struct termios n = t; 169 170 /* Disable echo */ 171 n.c_lflag &= ~(ECHO); 172 tcsetattr(fd, TCSANOW, &n); 173 printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name); 174 fflush(stdout); 175 } 176 } 177 b = read(fd, line, sizeof(line) - 1); 178 if (istty) { /* Restore state */ 179 tcsetattr(fd, TCSANOW, &t); 180 fputc('\n', stdout); 181 fflush(stdout); 182 } 183 if (b < 0) { 184 warn("-h file descriptor"); 185 return EX_OSERR; 186 } 187 line[b] = '\0'; 188 if ((p = strpbrk(line, " \t\r\n")) != NULL) 189 *p = '\0'; 190 if (!*line) 191 errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 192 grp->gr_passwd = pw_pwcrypt(line); 193 } 194 } 195 196 if (((arg = getarg(args, 'M')) != NULL || (arg = getarg(args, 'm')) != NULL) && arg->val) { 197 int i = 0; 198 char *p; 199 struct passwd *pwd; 200 201 /* Make sure this is not stay NULL with -M "" */ 202 extendarray(&members, &grmembers, 200); 203 if (arg->ch == 'm') { 204 int k = 0; 205 206 while (grp->gr_mem[k] != NULL) { 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; 215 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 ; 224 if (j == i && extendarray(&members, &grmembers, i + 2) != -1) 225 members[i++] = newstr(pwd->pw_name); 226 } 227 while (i < grmembers) 228 members[i++] = NULL; 229 grp->gr_mem = members; 230 } 231 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 */ 240 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; 249 } 250 251 252 static gid_t 253 gr_gidpolicy(struct userconf * cnf, struct cargs * args) 254 { 255 struct group *grp; 256 gid_t gid = (gid_t) - 1; 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 265 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 */ 284 setgrent(); 285 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); 288 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); 297 if (!bm_isset(&bm, gid)) 298 gid += cnf->min_gid; 299 else 300 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 301 } 302 303 /* 304 * Another sanity check 305 */ 306 if (gid < cnf->min_gid || gid > cnf->max_gid) 307 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 308 bm_dealloc(&bm); 309 } 310 return gid; 311 } 312 313 314 static int 315 print_group(struct group * grp, int pretty) 316 { 317 if (!pretty) { 318 int buflen = 0; 319 char *buf = NULL; 320 321 fmtgrent(&buf, &buflen, grp); 322 fputs(buf, stdout); 323 free(buf); 324 } else { 325 int i; 326 327 printf("Group Name: %-15s #%lu\n" 328 " Members: ", 329 grp->gr_name, (long) grp->gr_gid); 330 for (i = 0; grp->gr_mem[i]; i++) 331 printf("%s%s", i ? "," : "", grp->gr_mem[i]); 332 fputs("\n\n", stdout); 333 } 334 return EXIT_SUCCESS; 335 } 336