X-Git-Url: https://git.cameronkatri.com/pw-darwin.git/blobdiff_plain/1c918146f943052c1876cdc9983597efd1b4964f..1169eb1d26941a26f08566863b597c2d66f8c93e:/pw/pw_group.c diff --git a/pw/pw_group.c b/pw/pw_group.c index ce1b230..c9af998 100644 --- a/pw/pw_group.c +++ b/pw/pw_group.c @@ -26,7 +26,7 @@ #ifndef lint static const char rcsid[] = - "$Id$"; + "$FreeBSD$"; #endif /* not lint */ #include @@ -36,7 +36,6 @@ static const char rcsid[] = #include "pw.h" #include "bitmap.h" -#include "pwupd.h" static int print_group(struct group * grp, int pretty); @@ -45,12 +44,13 @@ 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 = { @@ -60,12 +60,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; @@ -76,26 +78,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) 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')) { @@ -117,8 +119,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) - err(EX_IOERR, "error updating group file"); + 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) @@ -212,8 +219,8 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args) } 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) + if ((pwd = GETPWNAM(p)) == NULL) { + if (!isdigit((unsigned char)*p) || (pwd = getpwuid((uid_t) atoi(p))) == NULL) errx(EX_NOUSER, "user `%s' does not exist", p); } /* @@ -232,12 +239,21 @@ 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))) { - warn("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) + 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); @@ -262,7 +278,7 @@ 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) + 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; @@ -281,11 +297,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