diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-11-08 23:26:46 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-11-08 23:26:46 +0000 |
commit | 9669849d5fb728673356cc799d86f59fc09749db (patch) | |
tree | 77b222c6949770d3aa45d6568e5249865e3e86ea /usr.sbin/user | |
parent | 39b665ba259922f1a18b0ec4b59a9ace3d6b1d6c (diff) |
Replace unreadable snprintf() and pointer arithmetic with simple strlcat().
If group file line would grow to be too long, leave it unmolested (previously
it would get removed).
Use fclose(fp) not close(fd) for a stream that was fdopen()ed. Otherwise
we could leak memory.
When calling fwrite() pass the buffer as a single element instead of
using n one-byte elements.
Diffstat (limited to 'usr.sbin/user')
-rw-r--r-- | usr.sbin/user/user.c | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/usr.sbin/user/user.c b/usr.sbin/user/user.c index b6d5b1302d4..48100f366f7 100644 --- a/usr.sbin/user/user.c +++ b/usr.sbin/user/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.25 2001/09/18 01:50:44 millert Exp $ */ +/* $OpenBSD: user.c,v 1.26 2001/11/08 23:26:45 millert Exp $ */ /* $NetBSD: user.c,v 1.40 2001/08/17 08:29:00 joda Exp $ */ /* @@ -344,9 +344,9 @@ creategid(char *group, int gid, char *name) return 0; } while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) { - if (fwrite(buf, sizeof(char), cc, to) != cc) { + if (fwrite(buf, cc, 1, to) <= 0) { (void) fclose(from); - (void) close(fd); + (void) fclose(to); (void) unlink(f); warn("can't create gid: short write to `%s'", f); return 0; @@ -423,9 +423,9 @@ modify_gid(char *group, char *newent) (void) strlcpy(buf, newent, sizeof(buf)); } } - if (fwrite(buf, sizeof(char), cc, to) != cc) { + if (fwrite(buf, cc, 1, to) <= 0) { (void) fclose(from); - (void) close(fd); + (void) fclose(to); (void) unlink(f); warn("can't create gid: short write to `%s'", f); return 0; @@ -452,10 +452,7 @@ append_group(char *user, int ngroups, char **groups) char buf[LINE_MAX]; char f[MaxFileNameLen]; char *colon; - int groupc; - int entc; int fd; - int nc; int cc; int i; int j; @@ -508,29 +505,27 @@ append_group(char *user, int ngroups, char **groups) warn("badly formed entry `%s'", buf); continue; } - entc = (int)(colon - buf); for (i = 0 ; i < ngroups ; i++) { - if ((groupc = strlen(groups[i])) == 0) { - continue; - } - if (cc >= sizeof(buf)) { - warn("line `%s' too long, skipping", buf); - continue; - } - if (entc == groupc && strncmp(groups[i], buf, entc) == 0) { - if ((nc = snprintf(&buf[cc - 1], - sizeof(buf) - cc + 1, - "%s%s\n", - (buf[cc - 2] == ':') ? "" : ",", - user)) >= sizeof(buf) - cc + 1) { - warnx("Warning: group `%s' entry too long", groups[i]); + if (strncmp(groups[i], buf, colon - buf) == 0) { + while (isspace(buf[cc - 1])) + cc--; + buf[(j = cc)] = '\0'; + if (*(colon + 1) != '\0') + strlcat(buf, ",", sizeof(buf)); + cc = strlcat(buf, user, sizeof(buf)) + 1; + if (cc >= sizeof(buf)) { + warnx("Warning: group `%s' would " + "become too long, not modifying", + groups[i]); + cc = j + 1; } - cc += nc - 1; + buf[cc - 1] = '\n'; + buf[cc] = '\0'; } } - if (fwrite(buf, sizeof(char), cc, to) != cc) { + if (fwrite(buf, cc, 1, to) <= 0) { (void) fclose(from); - (void) close(fd); + (void) fclose(to); (void) unlink(f); warn("can't create gid: short write to `%s'", f); return 0; |