X-Git-Url: https://git.cameronkatri.com/pw-darwin.git/blobdiff_plain/d66de55848d58c9fd3cf2ac4401182f5c3fbb06a..32215e1fded83eb454a0ec2522dbd53877e5a384:/pw/pw_group.c diff --git a/pw/pw_group.c b/pw/pw_group.c index 3d31c7a..f4f2116 100644 --- a/pw/pw_group.c +++ b/pw/pw_group.c @@ -22,31 +22,41 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * - * $FreeBSD$ */ -#include +#ifndef lint +static const char rcsid[] = + "$FreeBSD$"; +#endif /* not lint */ + #include +#include #include +#include +#include +#include +#include #include "pw.h" #include "bitmap.h" -#include "pwupd.h" +static struct passwd *lookup_pwent(const char *user); +static void delete_members(char ***members, int *grmembers, int *i, + struct carg *arg, struct group *grp); static int print_group(struct group * grp, int pretty); static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 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; + char **members = NULL; static struct group fakegroup = { @@ -56,12 +66,14 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) 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) - { + if (mode == M_NEXT) { gid_t next = gr_gidpolicy(cnf, args); if (getarg(args, 'q')) return next; @@ -72,26 +84,26 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) if (mode == M_PRINT && getarg(args, 'a')) { int pretty = getarg(args, 'P') != NULL; - setgrent(); - while ((grp = getgrent()) != NULL) + SETGRENT(); + while ((grp = GETGRENT()) != NULL) print_group(grp, pretty); - endgrent(); + ENDGRENT(); return EXIT_SUCCESS; } if (a_gid == NULL) { if (a_name == NULL) - cmderr(EX_DATAERR, "group name or id required\n"); + errx(EX_DATAERR, "group name or id required"); - if (mode != M_ADD && grp == NULL && isdigit(*a_name->val)) { + 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)); + 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)); + grp = GETGRGID(atoi(a_gid->val)); if (grp == NULL) { if (mode == M_PRINT && getarg(args, 'F')) { @@ -102,7 +114,7 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) fakegroup.gr_mem = fmems; return print_group(&fakegroup, getarg(args, 'P') != NULL); } - cmderr(EX_DATAERR, "unknown group `%s'\n", a_name ? a_name->val : a_gid->val); + 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); @@ -113,8 +125,13 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) if (mode == M_DELETE) { gid_t gid = grp->gr_gid; - if (delgrent(grp) == -1) - cmderr(EX_IOERR, "Error updating group file: %s\n", strerror(errno)); + 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) @@ -127,9 +144,9 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) grp->gr_name = pw_checkname((u_char *)arg->val, 0); } else { if (a_name == NULL) /* Required */ - cmderr(EX_DATAERR, "group name required\n"); + errx(EX_DATAERR, "group name required"); else if (grp != NULL) /* Exists */ - cmderr(EX_DATAERR, "group name `%s' already exists\n", a_name->val); + errx(EX_DATAERR, "group name `%s' already exists", a_name->val); extendarray(&members, &grmembers, 200); members[0] = NULL; @@ -147,11 +164,13 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) * software. */ - if ((arg = getarg(args, 'h')) != NULL) { + 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; @@ -177,49 +196,57 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) fflush(stdout); } if (b < 0) { - perror("-h file descriptor"); + warn("-h file descriptor"); return EX_OSERR; } line[b] = '\0'; if ((p = strpbrk(line, " \t\r\n")) != NULL) *p = '\0'; if (!*line) - cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd); - grp->gr_passwd = pw_pwcrypt(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, 'm')) != NULL) && arg->val) { + 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 == 'm') { + if (arg->ch == 'd') + delete_members(&members, &grmembers, &i, arg, grp); + else if (arg->ch == 'm') { int k = 0; while (grp->gr_mem[k] != NULL) { - if (extendarray(&members, &grmembers, i + 2) != -1) { + if (extendarray(&members, &grmembers, i + 2) != -1) members[i++] = grp->gr_mem[k]; - } k++; } } - for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { - int j; - if ((pwd = getpwnam(p)) == NULL) { - if (!isdigit(*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL) - cmderr(EX_NOUSER, "user `%s' does not exist\n", p); + + 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); } - /* - * Check for duplicates - */ - 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; @@ -228,13 +255,22 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) if (getarg(args, 'N') != NULL) return print_group(grp, getarg(args, 'P') != NULL); - if ((mode == M_ADD && !addgrent(grp)) || (mode == M_UPDATE && !chggrent(a_name->val, grp))) { - perror("group update"); + 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) - cmderr(EX_SOFTWARE, "group disappeared during update\n"); + 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); @@ -245,6 +281,63 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) } +/* + * Lookup a passwd entry using a name or UID. + */ +static struct passwd * +lookup_pwent(const char *user) +{ + struct passwd *pwd; + + if ((pwd = GETPWNAM(user)) == NULL && + (!isdigit((unsigned char)*user) || + (pwd = getpwuid((uid_t) atoi(user))) == NULL)) + errx(EX_NOUSER, "user `%s' does not exist", user); + + return (pwd); +} + + +/* + * Delete requested members from a group. + */ +static void +delete_members(char ***members, int *grmembers, int *i, struct carg *arg, + struct group *grp) +{ + bool matchFound; + char *user; + char *valueCopy; + char *valuePtr; + int k; + struct passwd *pwd; + + k = 0; + while (grp->gr_mem[k] != NULL) { + matchFound = false; + if ((valueCopy = strdup(arg->val)) == NULL) + errx(EX_UNAVAILABLE, "out of memory"); + valuePtr = valueCopy; + while ((user = strsep(&valuePtr, ", \t")) != NULL) { + pwd = lookup_pwent(user); + if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) { + matchFound = true; + break; + } + } + free(valueCopy); + + if (!matchFound && + extendarray(members, grmembers, *i + 2) != -1) + (*members)[(*i)++] = grp->gr_mem[k]; + + k++; + } + + return; +} + + static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args) { @@ -258,8 +351,8 @@ gr_gidpolicy(struct userconf * cnf, struct cargs * args) if (a_gid != NULL) { gid = (gid_t) atol(a_gid->val); - if ((grp = getgrgid(gid)) != NULL && getarg(args, 'o') == NULL) - cmderr(EX_DATAERR, "gid `%ld' has already been allocated\n", (long) grp->gr_gid); + if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) + errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); } else { struct bitmap bm; @@ -277,11 +370,12 @@ gr_gidpolicy(struct userconf * cnf, struct cargs * args) /* * Now, let's fill the bitmap from the password file */ - setgrent(); - while ((grp = getgrent()) != NULL) - if (grp->gr_gid >= (int) cnf->min_gid && grp->gr_gid <= (int) cnf->max_gid) + SETGRENT(); + while ((grp = GETGRENT()) != NULL) + if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && + (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) bm_setbit(&bm, grp->gr_gid - cnf->min_gid); - endgrent(); + ENDGRENT(); /* * Then apply the policy, with fallback to reuse if necessary @@ -300,7 +394,7 @@ gr_gidpolicy(struct userconf * cnf, struct cargs * args) * Another sanity check */ if (gid < cnf->min_gid || gid > cnf->max_gid) - cmderr(EX_SOFTWARE, "unable to allocate a new gid - range fully used\n"); + errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); bm_dealloc(&bm); } return gid; @@ -311,10 +405,9 @@ static int print_group(struct group * grp, int pretty) { if (!pretty) { - int buflen = 0; char *buf = NULL; - fmtgrent(&buf, &buflen, grp); + buf = gr_make(grp); fputs(buf, stdout); free(buf); } else {