summaryrefslogtreecommitdiff
path: root/usr.sbin/user/user.c
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2004-05-10 09:44:46 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2004-05-10 09:44:46 +0000
commite133c5d77537b062d5de59ccdffed86afa21f1ca (patch)
treed0950b6fd769d2e8ed830448fe9f4ac0b55519a2 /usr.sbin/user/user.c
parentb2de25f1ae98d6dec4e2cdb1a364c395ba22b03a (diff)
when adding a new group to /etc/groups, place it just before the first
+ entry. assistance from tdeval and otto. this is the first half of pr 3727, brendan@cs.uchicago.edu
Diffstat (limited to 'usr.sbin/user/user.c')
-rw-r--r--usr.sbin/user/user.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/usr.sbin/user/user.c b/usr.sbin/user/user.c
index af62c52b7e7..5159bb861e3 100644
--- a/usr.sbin/user/user.c
+++ b/usr.sbin/user/user.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: user.c,v 1.57 2004/04/19 17:48:31 millert Exp $ */
+/* $OpenBSD: user.c,v 1.58 2004/05/10 09:44:45 deraadt Exp $ */
/* $NetBSD: user.c,v 1.69 2003/04/14 17:40:07 agc Exp $ */
/*
@@ -320,10 +320,11 @@ creategid(char *group, gid_t gid, const char *name)
struct stat st;
FILE *from;
FILE *to;
- char buf[LINE_MAX];
+ char *buf;
char f[MaxFileNameLen];
- int fd;
- int cc;
+ int fd, ret;
+ int wroteit = 0;
+ size_t len;
if (getgrnam(group) != NULL) {
warnx("group `%s' already exists", group);
@@ -351,8 +352,14 @@ creategid(char *group, gid_t gid, const char *name)
warn("can't create gid: fdopen `%s' failed", f);
return 0;
}
- while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
- if (fwrite(buf, cc, 1, to) != 1) {
+ while ((buf = fgetln(from, &len)) != NULL && len > 0) {
+ ret = 0;
+ if (buf[0] == '+' && wroteit == 0) {
+ ret = fprintf(to, "%s:*:%u:%s\n", group, gid, name);
+ wroteit = 1;
+ }
+ if (ret == -1 ||
+ fprintf(to, "%*.*s", (int)len, (int)len, buf) != len) {
(void) fclose(from);
(void) fclose(to);
(void) unlink(f);
@@ -360,9 +367,15 @@ creategid(char *group, gid_t gid, const char *name)
return 0;
}
}
- (void) fprintf(to, "%s:*:%u:%s\n", group, gid, name);
+ ret = 0;
+ if (wroteit == 0)
+ ret = fprintf(to, "%s:*:%u:%s\n", group, gid, name);
(void) fclose(from);
- (void) fclose(to);
+ if (fclose(to) == EOF || ret == -1) {
+ (void) unlink(f);
+ warn("can't create gid: short write to `%s'", f);
+ return 0;
+ }
if (rename(f, _PATH_GROUP) < 0) {
(void) unlink(f);
warn("can't create gid: can't rename `%s' to `%s'", f,