Sync sudo_pwdup with OpenBSD changes: use macros for size computaton and

string copies.
This commit is contained in:
Todd C. Miller
2005-03-25 17:36:52 +00:00
parent ca7c435740
commit f78ce55170

View File

@@ -113,6 +113,23 @@ cmp_pwnam(v1, v2)
return(strcmp(pw1->pw_name, pw2->pw_name)); return(strcmp(pw1->pw_name, pw2->pw_name));
} }
#define PW_SIZE(name, size) \
do { \
if (pw->name) { \
size = strlen(pw->name) + 1; \
total += size; \
} \
} while (0)
#define PW_COPY(name, size) \
do { \
if (pw->name) { \
memcpy(cp, pw->name, size); \
newpw->name = cp; \
cp += size; \
} \
} while (0)
/* /*
* Dynamically allocate space for a struct password and the constituent parts * Dynamically allocate space for a struct password and the constituent parts
* that we care about. Fills in pw_passwd from shadow file. * that we care about. Fills in pw_passwd from shadow file.
@@ -133,74 +150,33 @@ sudo_pwdup(pw)
/* Allocate in one big chunk for easy freeing. */ /* Allocate in one big chunk for easy freeing. */
nsize = psize = csize = gsize = dsize = ssize = 0; nsize = psize = csize = gsize = dsize = ssize = 0;
total = sizeof(struct passwd); total = sizeof(struct passwd);
if (pw->pw_name) { PW_SIZE(pw_name, nsize);
nsize = strlen(pw->pw_name) + 1; PW_SIZE(pw_passwd, psize);
total += nsize;
}
if (pw->pw_passwd) {
psize = strlen(pw->pw_passwd) + 1;
total += psize;
}
#ifdef HAVE_LOGIN_CAP_H #ifdef HAVE_LOGIN_CAP_H
if (pw->pw_class) { PW_SIZE(pw_class, csize);
csize = strlen(pw->pw_class) + 1;
total += csize;
}
#endif #endif
if (pw->pw_gecos) { PW_SIZE(pw_gecos, gsize);
gsize = strlen(pw->pw_gecos) + 1; PW_SIZE(pw_dir, dsize);
total += gsize; PW_SIZE(pw_shell, ssize);
}
if (pw->pw_dir) {
dsize = strlen(pw->pw_dir) + 1;
total += dsize;
}
if (pw_shell) {
ssize = strlen(pw_shell) + 1;
total += ssize;
}
if ((cp = malloc(total)) == NULL) if ((cp = malloc(total)) == NULL)
return(NULL); return(NULL);
newpw = (struct passwd *)cp; newpw = (struct passwd *) cp;
/* /*
* Copy in passwd contents and make strings relative to space * Copy in passwd contents and make strings relative to space
* at the end of the buffer. * at the end of the buffer.
*/ */
(void)memcpy(newpw, pw, sizeof(struct passwd)); memcpy(newpw, pw, sizeof(struct passwd));
cp += sizeof(struct passwd); cp += sizeof(struct passwd);
if (nsize) { PW_COPY(pw_name, nsize);
(void)memcpy(cp, pw->pw_name, nsize); PW_COPY(pw_passwd, psize);
newpw->pw_name = cp;
cp += nsize;
}
if (psize) {
(void)memcpy(cp, pw->pw_passwd, psize);
newpw->pw_passwd = cp;
cp += psize;
}
#ifdef HAVE_LOGIN_CAP_H #ifdef HAVE_LOGIN_CAP_H
if (csize) { PW_COPY(pw_class, csize);
(void)memcpy(cp, pw->pw_class, csize);
newpw->pw_class = cp;
cp += csize;
}
#endif #endif
if (gsize) { PW_COPY(pw_gecos, gsize);
(void)memcpy(cp, pw->pw_gecos, gsize); PW_COPY(pw_dir, dsize);
newpw->pw_gecos = cp; PW_COPY(pw_shell, ssize);
cp += gsize;
}
if (dsize) {
(void)memcpy(cp, pw->pw_dir, dsize);
newpw->pw_dir = cp;
cp += dsize;
}
if (ssize) {
(void)memcpy(cp, pw_shell, ssize);
newpw->pw_shell = cp;
cp += ssize;
}
return(newpw); return(newpw);
} }