int pw_group(struct userconf * cnf, int mode, struct cargs * args) { int rc; struct carg *a_name = getarg(args, 'n'); struct carg *a_gid = getarg(args, 'g'); struct carg *arg; struct group *grp = NULL; int grmembers = 0; char **members = NULL; static struct group fakegroup = { "nogroup", "*", -1, NULL }; if (mode == M_LOCK || mode == M_UNLOCK) errx(EX_USAGE, "'lock' command is not available for groups"); /* * With M_NEXT, we only need to return the * next gid to stdout */ if (mode == M_NEXT) { gid_t next = gr_gidpolicy(cnf, args); if (getarg(args, 'q')) return next; printf("%ld\n", (long)next); return EXIT_SUCCESS; } if (mode == M_PRINT && getarg(args, 'a')) { int pretty = getarg(args, 'P') != NULL; SETGRENT(); while ((grp = GETGRENT()) != NULL) print_group(grp, pretty); ENDGRENT(); return EXIT_SUCCESS; } if (a_gid == NULL) { if (a_name == NULL) errx(EX_DATAERR, "group name or id required"); if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) { (a_gid = a_name)->ch = 'g'; a_name = NULL; } } grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val)); if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { if (a_name == NULL && grp == NULL) /* Try harder */ grp = GETGRGID(atoi(a_gid->val)); if (grp == NULL) { if (mode == M_PRINT && getarg(args, 'F')) { char *fmems[1]; fmems[0] = NULL; fakegroup.gr_name = a_name ? a_name->val : "nogroup"; fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1; fakegroup.gr_mem = fmems; return print_group(&fakegroup, getarg(args, 'P') != NULL); } errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val); } if (a_name == NULL) /* Needed later */ a_name = addarg(args, 'n', grp->gr_name); /* * Handle deletions now */ if (mode == M_DELETE) { gid_t gid = grp->gr_gid; rc = delgrent(grp); if (rc == -1) err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name); else if (rc != 0) { warn("group update"); return EX_IOERR; } pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid); return EXIT_SUCCESS; } else if (mode == M_PRINT) return print_group(grp, getarg(args, 'P') != NULL); if (a_gid) grp->gr_gid = (gid_t) atoi(a_gid->val); if ((arg = getarg(args, 'l')) != NULL) grp->gr_name = pw_checkname((u_char *)arg->val, 0); } else { if (a_name == NULL) /* Required */ errx(EX_DATAERR, "group name required"); else if (grp != NULL) /* Exists */ errx(EX_DATAERR, "group name `%s' already exists", a_name->val); extendarray(&members, &grmembers, 200); members[0] = NULL; grp = &fakegroup; grp->gr_name = pw_checkname((u_char *)a_name->val, 0); grp->gr_passwd = "*"; grp->gr_gid = gr_gidpolicy(cnf, args); grp->gr_mem = members; } /* * This allows us to set a group password Group passwords is an * antique idea, rarely used and insecure (no secure database) Should * be discouraged, but it is apparently still supported by some * software. */ if ((arg = getarg(args, 'h')) != NULL || (arg = getarg(args, 'H')) != NULL) { if (strcmp(arg->val, "-") == 0) grp->gr_passwd = "*"; /* No access */ else { int fd = atoi(arg->val); int precrypt = (arg->ch == 'H'); int b; int istty = isatty(fd); struct termios t; char *p, line[256]; if (istty) { if (tcgetattr(fd, &t) == -1) istty = 0; else { struct termios n = t; /* Disable echo */ n.c_lflag &= ~(ECHO); tcsetattr(fd, TCSANOW, &n); printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name); fflush(stdout); } } b = read(fd, line, sizeof(line) - 1); if (istty) { /* Restore state */ tcsetattr(fd, TCSANOW, &t); fputc('\n', stdout); fflush(stdout); } if (b < 0) { warn("-h file descriptor"); return EX_OSERR; } line[b] = '\0'; if ((p = strpbrk(line, " \t\r\n")) != NULL) *p = '\0'; if (!*line) errx(EX_DATAERR, "empty password read on file descriptor %d", fd); if (precrypt) { if (strchr(line, ':') != NULL) return EX_DATAERR; grp->gr_passwd = line; } else grp->gr_passwd = pw_pwcrypt(line); } } if (((arg = getarg(args, 'M')) != NULL || (arg = getarg(args, 'd')) != NULL || (arg = getarg(args, 'm')) != NULL) && arg->val) { int i = 0; char *p; struct passwd *pwd; /* Make sure this is not stay NULL with -M "" */ extendarray(&members, &grmembers, 200); if (arg->ch == 'd') delete_members(&members, &grmembers, &i, arg, grp); else if (arg->ch == 'm') { int k = 0; if (grp->gr_mem != NULL) { while (grp->gr_mem[k] != NULL) { if (extendarray(&members, &grmembers, i + 2) != -1) members[i++] = grp->gr_mem[k]; k++; } } } if (arg->ch != 'd') for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { int j; /* * Check for duplicates */ pwd = lookup_pwent(p); for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) ; if (j == i && extendarray(&members, &grmembers, i + 2) != -1) members[i++] = newstr(pwd->pw_name); } while (i < grmembers) members[i++] = NULL; grp->gr_mem = members; } if (getarg(args, 'N') != NULL) return print_group(grp, getarg(args, 'P') != NULL); if (mode == M_ADD && (rc = addgrent(grp)) != 0) { if (rc == -1) warnx("group '%s' already exists", grp->gr_name); else warn("group update"); return EX_IOERR; } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) { if (rc == -1) warnx("group '%s' not available (NIS?)", grp->gr_name); else warn("group update"); return EX_IOERR; } /* grp may have been invalidated */ if ((grp = GETGRNAM(a_name->val)) == NULL) errx(EX_SOFTWARE, "group disappeared during update"); pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); free(members); return EXIT_SUCCESS; }
int pw_group(int mode, char *name, long id, struct cargs * args) { int rc; struct carg *arg; struct group *grp = NULL; int grmembers = 0; char **members = NULL; struct userconf *cnf = conf.userconf; static struct group fakegroup = { "nogroup", "*", -1, NULL }; if (mode == M_LOCK || mode == M_UNLOCK) errx(EX_USAGE, "'lock' command is not available for groups"); /* * With M_NEXT, we only need to return the * next gid to stdout */ if (mode == M_NEXT) { gid_t next = gr_gidpolicy(cnf, id); if (getarg(args, 'q')) return next; printf("%u\n", next); return EXIT_SUCCESS; } if (mode == M_PRINT && getarg(args, 'a')) { SETGRENT(); while ((grp = GETGRENT()) != NULL) print_group(grp); ENDGRENT(); return EXIT_SUCCESS; } if (id < 0 && name == NULL) errx(EX_DATAERR, "group name or id required"); grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id); if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { if (name == NULL && grp == NULL) /* Try harder */ grp = GETGRGID(id); if (grp == NULL) { if (mode == M_PRINT && getarg(args, 'F')) { char *fmems[1]; fmems[0] = NULL; fakegroup.gr_name = name ? name : "nogroup"; fakegroup.gr_gid = (gid_t) id; fakegroup.gr_mem = fmems; return print_group(&fakegroup); } if (name == NULL) errx(EX_DATAERR, "unknown group `%s'", name); else errx(EX_DATAERR, "unknown group `%ld'", id); } if (name == NULL) /* Needed later */ name = grp->gr_name; /* * Handle deletions now */ if (mode == M_DELETE) { rc = delgrent(grp); if (rc == -1) err(EX_IOERR, "group '%s' not available (NIS?)", name); else if (rc != 0) { err(EX_IOERR, "group update"); } pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", name, id); return EXIT_SUCCESS; } else if (mode == M_PRINT) return print_group(grp); if (id > 0) grp->gr_gid = (gid_t) id; if (conf.newname != NULL) grp->gr_name = pw_checkname(conf.newname, 0); } else { if (name == NULL) /* Required */ errx(EX_DATAERR, "group name required"); else if (grp != NULL) /* Exists */ errx(EX_DATAERR, "group name `%s' already exists", name); extendarray(&members, &grmembers, 200); members[0] = NULL; grp = &fakegroup; grp->gr_name = pw_checkname(name, 0); grp->gr_passwd = "*"; grp->gr_gid = gr_gidpolicy(cnf, id); grp->gr_mem = members; } /* * This allows us to set a group password Group passwords is an * antique idea, rarely used and insecure (no secure database) Should * be discouraged, but it is apparently still supported by some * software. */ if (conf.fd != -1) set_passwd(grp, mode == M_UPDATE); if (((arg = getarg(args, 'M')) != NULL || (arg = getarg(args, 'd')) != NULL || (arg = getarg(args, 'm')) != NULL) && arg->val) { int i = 0; char *p; struct passwd *pwd; /* Make sure this is not stay NULL with -M "" */ extendarray(&members, &grmembers, 200); if (arg->ch == 'd') delete_members(&members, &grmembers, &i, arg, grp); else if (arg->ch == 'm') { int k = 0; if (grp->gr_mem != NULL) { while (grp->gr_mem[k] != NULL) { if (extendarray(&members, &grmembers, i + 2) != -1) members[i++] = grp->gr_mem[k]; k++; } } } if (arg->ch != 'd') for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { int j; /* * Check for duplicates */ pwd = lookup_pwent(p); for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) ; if (j == i && extendarray(&members, &grmembers, i + 2) != -1) members[i++] = newstr(pwd->pw_name); } while (i < grmembers) members[i++] = NULL; grp->gr_mem = members; } if (conf.dryrun) return print_group(grp); if (mode == M_ADD && (rc = addgrent(grp)) != 0) { if (rc == -1) errx(EX_IOERR, "group '%s' already exists", grp->gr_name); else err(EX_IOERR, "group update"); } else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) { if (rc == -1) errx(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name); else err(EX_IOERR, "group update"); } if (conf.newname != NULL) name = conf.newname; /* grp may have been invalidated */ if ((grp = GETGRNAM(name)) == NULL) errx(EX_SOFTWARE, "group disappeared during update"); pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid); free(members); return EXIT_SUCCESS; }