summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid E. O'Brien <obrien@FreeBSD.org>2013-02-08 16:10:16 +0000
committerDavid E. O'Brien <obrien@FreeBSD.org>2013-02-08 16:10:16 +0000
commit08324d276f48c53cc1af9cfe116a4c930bb77679 (patch)
tree3c2aec6142a66931af5f1da71eae54920375189e
parent08870ffbc9e2cee84244c234ad5b56ea9aa26fc1 (diff)
parentcbfb9c649bd7eecce96516a347e4bab7c5b2cb52 (diff)
downloadpw-darwin-08324d276f48c53cc1af9cfe116a4c930bb77679.tar.gz
pw-darwin-08324d276f48c53cc1af9cfe116a4c930bb77679.tar.zst
pw-darwin-08324d276f48c53cc1af9cfe116a4c930bb77679.zip
Sync with HEAD.
-rw-r--r--pw/bitmap.c3
-rw-r--r--pw/grupd.c11
-rw-r--r--pw/pw_group.c5
-rw-r--r--pw/pw_log.c2
-rw-r--r--pw/pw_nis.c2
-rw-r--r--pw/pw_user.c29
-rw-r--r--pw/pw_vpw.c236
-rw-r--r--pw/pwupd.c16
-rw-r--r--pw/rm_r.c2
9 files changed, 99 insertions, 207 deletions
diff --git a/pw/bitmap.c b/pw/bitmap.c
index bcfea7e..8e96bff 100644
--- a/pw/bitmap.c
+++ b/pw/bitmap.c
@@ -50,8 +50,7 @@ bm_alloc(int size)
void
bm_dealloc(struct bitmap * bm)
{
- if (bm->map)
- free(bm->map);
+ free(bm->map);
}
static void
diff --git a/pw/grupd.c b/pw/grupd.c
index e9f6b5e..3f78e95 100644
--- a/pw/grupd.c
+++ b/pw/grupd.c
@@ -50,12 +50,11 @@ setgrdir(const char * dir)
{
if (dir == NULL)
return -1;
- else {
- char * d = malloc(strlen(dir)+1);
- if (d == NULL)
- return -1;
- grpath = strcpy(d, dir);
- }
+ else
+ grpath = strdup(dir);
+ if (grpath == NULL)
+ return -1;
+
return 0;
}
diff --git a/pw/pw_group.c b/pw/pw_group.c
index f4f2116..3259412 100644
--- a/pw/pw_group.c
+++ b/pw/pw_group.c
@@ -274,8 +274,7 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args)
pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
- if (members)
- free(members);
+ free(members);
return EXIT_SUCCESS;
}
@@ -408,7 +407,7 @@ print_group(struct group * grp, int pretty)
char *buf = NULL;
buf = gr_make(grp);
- fputs(buf, stdout);
+ printf("%s\n", buf);
free(buf);
} else {
int i;
diff --git a/pw/pw_log.c b/pw/pw_log.c
index f16274f..b774423 100644
--- a/pw/pw_log.c
+++ b/pw/pw_log.c
@@ -47,7 +47,6 @@ pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
}
if (logfile != NULL) {
va_list argp;
- int l;
time_t now = time(NULL);
struct tm *t = localtime(&now);
char nfmt[256];
@@ -57,7 +56,6 @@ pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
name = "unknown";
/* ISO 8601 International Standard Date format */
strftime(nfmt, sizeof nfmt, "%Y-%m-%d %T ", t);
- l = strlen(nfmt);
sprintf(nfmt + strlen(nfmt), "[%s:%s%s] %s\n", name, Which[which], Modes[mode], fmt);
va_start(argp, fmt);
vfprintf(logfile, nfmt, argp);
diff --git a/pw/pw_nis.c b/pw/pw_nis.c
index af5901a..918fc30 100644
--- a/pw/pw_nis.c
+++ b/pw/pw_nis.c
@@ -66,6 +66,8 @@ pw_nisupdate(const char * path, struct passwd * pwd, char const * user)
pw_fini();
err(1, "pw_copy()");
}
+ if (chmod(pw_tempname(), 0644) == -1)
+ err(1, "chmod()");
if (rename(pw_tempname(), path) == -1)
err(1, "rename()");
diff --git a/pw/pw_user.c b/pw/pw_user.c
index abf1c35..5f4d7a9 100644
--- a/pw/pw_user.c
+++ b/pw/pw_user.c
@@ -394,7 +394,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args)
/*
* Remove crontabs
*/
- sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
+ snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
if (access(file, F_OK) == 0) {
sprintf(file, "crontab -u %s -r", pwd->pw_name);
system(file);
@@ -425,7 +425,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args)
}
grp = GETGRNAM(a_name->val);
- if (*grp->gr_mem == NULL)
+ if (grp != NULL && *grp->gr_mem == NULL)
delgrent(GETGRNAM(a_name->val));
SETGRENT();
while ((grp = GETGRENT()) != NULL) {
@@ -745,25 +745,20 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args)
*/
if (mode == M_ADD || getarg(args, 'G') != NULL) {
- int i, j;
+ int i;
for (i = 0; cnf->groups[i] != NULL; i++) {
grp = GETGRNAM(cnf->groups[i]);
- for (j = 0; grp->gr_mem[j] != NULL; j++) {
- if (!strcmp(grp->gr_mem[j], pwd->pw_name))
- break;
- }
- if (grp->gr_mem[j] != NULL) /* user already member of group */
+ grp = gr_add(grp, pwd->pw_name);
+ /*
+ * grp can only be NULL in 2 cases:
+ * - the new member is already a member
+ * - a problem with memory occurs
+ * in both cases we want to skip now.
+ */
+ if (grp == NULL)
continue;
-
- if (j == 0)
- grp->gr_mem = NULL;
-
- grp->gr_mem = reallocf(grp->gr_mem, sizeof(*grp->gr_mem) *
- (j + 2));
-
- grp->gr_mem[j] = pwd->pw_name;
- grp->gr_mem[j+1] = NULL;
chggrent(cnf->groups[i], grp);
+ free(grp);
}
}
diff --git a/pw/pw_vpw.c b/pw/pw_vpw.c
index 674b64f..99663be 100644
--- a/pw/pw_vpw.c
+++ b/pw/pw_vpw.c
@@ -30,6 +30,10 @@ static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
+#include <pwd.h>
+#include <grp.h>
+#include <libutil.h>
+#define _WITH_GETLINE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -55,101 +59,44 @@ vsetpwent(void)
}
static struct passwd *
-vnextpwent(char const * nam, uid_t uid, int doclose)
+vnextpwent(char const *nam, uid_t uid, int doclose)
{
- struct passwd * pw = NULL;
- static char pwtmp[1024];
-
- strlcpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof(pwtmp));
-
- if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) {
- int done = 0;
-
- static struct passwd pwd;
-
- while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
- {
- int i, quickout = 0;
- char * q;
- char * p = strchr(pwtmp, '\n');
-
- if (p == NULL) {
- while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
- ; /* Skip long lines */
- continue;
- }
-
- /* skip comments & empty lines */
- if (*pwtmp =='\n' || *pwtmp == '#')
+ struct passwd *pw;
+ char *line;
+ size_t linecap;
+ ssize_t linelen;
+
+ pw = NULL;
+ line = NULL;
+ linecap = 0;
+ linelen = 0;
+
+ if (pwd_fp != NULL || (pwd_fp = fopen(getpwpath(_MASTERPASSWD), "r")) != NULL) {
+ while ((linelen = getline(&line, &linecap, pwd_fp)) > 0) {
+ /* Skip comments and empty lines */
+ if (*line == '\n' || *line == '#')
continue;
-
- i = 0;
- q = p = pwtmp;
- bzero(&pwd, sizeof pwd);
- while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
- switch (i++)
- {
- case 0: /* username */
- pwd.pw_name = p;
- if (nam) {
- if (strcmp(nam, p) == 0)
- done = 1;
- else
- quickout = 1;
- }
- break;
- case 1: /* password */
- pwd.pw_passwd = p;
- break;
- case 2: /* uid */
- pwd.pw_uid = atoi(p);
- if (uid != (uid_t)-1) {
- if (uid == pwd.pw_uid)
- done = 1;
- else
- quickout = 1;
- }
- break;
- case 3: /* gid */
- pwd.pw_gid = atoi(p);
- break;
- case 4: /* class */
- if (nam == NULL && uid == (uid_t)-1)
- done = 1;
- pwd.pw_class = p;
- break;
- case 5: /* change */
- pwd.pw_change = (time_t)atol(p);
- break;
- case 6: /* expire */
- pwd.pw_expire = (time_t)atol(p);
- break;
- case 7: /* gecos */
- pwd.pw_gecos = p;
- break;
- case 8: /* directory */
- pwd.pw_dir = p;
- break;
- case 9: /* shell */
- pwd.pw_shell = p;
- break;
- }
- }
- }
+ /* trim latest \n */
+ if (line[linelen - 1 ] == '\n')
+ line[linelen - 1] = '\0';
+ pw = pw_scan(line, PWSCAN_MASTER);
+ if (uid != (uid_t)-1) {
+ if (uid == pw->pw_uid)
+ break;
+ } else if (nam != NULL) {
+ if (strcmp(nam, pw->pw_name) == 0)
+ break;
+ } else
+ break;
+ free(pw);
+ pw = NULL;
+ }
if (doclose)
vendpwent();
- if (done && pwd.pw_name) {
- pw = &pwd;
+ }
+ free(line);
- #define CKNULL(s) s = s ? s : ""
- CKNULL(pwd.pw_passwd);
- CKNULL(pwd.pw_class);
- CKNULL(pwd.pw_gecos);
- CKNULL(pwd.pw_dir);
- CKNULL(pwd.pw_shell);
- }
- }
- return pw;
+ return (pw);
}
struct passwd *
@@ -192,93 +139,44 @@ vsetgrent(void)
}
static struct group *
-vnextgrent(char const * nam, gid_t gid, int doclose)
+vnextgrent(char const *nam, gid_t gid, int doclose)
{
- struct group * gr = NULL;
-
- static char * grtmp = NULL;
- static int grlen = 0;
- static char ** mems = NULL;
- static int memlen = 0;
-
- extendline(&grtmp, &grlen, MAXPATHLEN);
- strlcpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
-
- if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
- int done = 0;
-
- static struct group grp;
-
- while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
- {
- int i, quickout = 0;
- int mno = 0;
- char * q, * p;
- const char * sep = ":\n";
-
- if ((p = strchr(grtmp, '\n')) == NULL) {
- int l;
- extendline(&grtmp, &grlen, grlen + PWBUFSZ);
- l = strlen(grtmp);
- if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
- break; /* No newline terminator on last line */
- }
+ struct group *gr;
+ char *line;
+ size_t linecap;
+ ssize_t linelen;
+
+ gr = NULL;
+ line = NULL;
+ linecap = 0;
+ linelen = 0;
+
+ if (grp_fp != NULL || (grp_fp = fopen(getgrpath(_GROUP), "r")) != NULL) {
+ while ((linelen = getline(&line, &linecap, grp_fp)) > 0) {
/* Skip comments and empty lines */
- if (*grtmp == '\n' || *grtmp == '#')
+ if (*line == '\n' || *line == '#')
continue;
- i = 0;
- q = p = grtmp;
- bzero(&grp, sizeof grp);
- extendarray(&mems, &memlen, 200);
- while (!quickout && (p = strsep(&q, sep)) != NULL) {
- switch (i++)
- {
- case 0: /* groupname */
- grp.gr_name = p;
- if (nam) {
- if (strcmp(nam, p) == 0)
- done = 1;
- else
- quickout = 1;
- }
- break;
- case 1: /* password */
- grp.gr_passwd = p;
+ /* trim latest \n */
+ if (line[linelen - 1 ] == '\n')
+ line[linelen - 1] = '\0';
+ gr = gr_scan(line);
+ if (gid != (gid_t)-1) {
+ if (gid == gr->gr_gid)
break;
- case 2: /* gid */
- grp.gr_gid = atoi(p);
- if (gid != (gid_t)-1) {
- if (gid == (gid_t)grp.gr_gid)
- done = 1;
- else
- quickout = 1;
- } else if (nam == NULL)
- done = 1;
+ } else if (nam != NULL) {
+ if (strcmp(nam, gr->gr_name) == 0)
break;
- case 3:
- q = p;
- sep = ",\n";
- break;
- default:
- if (*p) {
- extendarray(&mems, &memlen, mno + 2);
- mems[mno++] = p;
- }
- break;
- }
- }
- grp.gr_mem = mems;
- mems[mno] = NULL;
- }
+ } else
+ break;
+ free(gr);
+ gr = NULL;
+ }
if (doclose)
vendgrent();
- if (done && grp.gr_name) {
- gr = &grp;
-
- CKNULL(grp.gr_passwd);
- }
}
- return gr;
+ free(line);
+
+ return (gr);
}
struct group *
diff --git a/pw/pwupd.c b/pw/pwupd.c
index 4ab0f01..22662db 100644
--- a/pw/pwupd.c
+++ b/pw/pwupd.c
@@ -56,12 +56,10 @@ setpwdir(const char * dir)
{
if (dir == NULL)
return -1;
- else {
- char * d = malloc(strlen(dir)+1);
- if (d == NULL)
- return -1;
- pwpath = strcpy(d, dir);
- }
+ else
+ pwpath = strdup(dir);
+ if (pwpath == NULL)
+ return -1;
return 0;
}
@@ -148,7 +146,11 @@ pw_update(struct passwd * pwd, char const * user)
pw_fini();
err(1, "pw_copy()");
}
- if (pw_mkdb(user) == -1) {
+ /*
+ * in case of deletion of a user, the whole database
+ * needs to be regenerated
+ */
+ if (pw_mkdb(pw != NULL ? user : NULL) == -1) {
pw_fini();
err(1, "pw_mkdb()");
}
diff --git a/pw/rm_r.c b/pw/rm_r.c
index 4ad590b..797ca9d 100644
--- a/pw/rm_r.c
+++ b/pw/rm_r.c
@@ -52,7 +52,7 @@ rm_r(char const * dir, uid_t uid)
while ((e = readdir(d)) != NULL) {
if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) {
- sprintf(file, "%s/%s", dir, e->d_name);
+ snprintf(file, sizeof(file), "%s/%s", dir, e->d_name);
if (lstat(file, &st) == 0) { /* Need symlinks, not
* linked file */
if (S_ISDIR(st.st_mode)) /* Directory - recurse */