* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
-#include <libgen.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
errno = ENAMETOOLONG;
return (-1);
}
- if ((tfd = mkstemp(tempname)) == -1)
+ if ((tfd = mkostemp(tempname, 0)) == -1)
return (-1);
if (mfd != -1) {
while ((nr = read(mfd, buf, sizeof(buf))) > 0)
(void)setuid(getuid());
}
errno = 0;
- execlp(editor, basename(editor), tempname, (char *)NULL);
+ execlp(editor, editor, tempname, (char *)NULL);
_exit(errno);
default:
/* parent */
int
pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
{
- char buf[8192], *end, *line, *p, *q, *r, t;
+ char *buf, *end, *line, *p, *q, *r, *tmp;
struct passwd *fpw;
const struct passwd *spw;
- size_t len;
+ size_t len, size;
int eof, readlen;
+ char t;
if (old_pw == NULL && pw == NULL)
return (-1);
if (spw == NULL)
spw = pw;
+ /* initialize the buffer */
+ if ((buf = malloc(size = 1024)) == NULL)
+ goto err;
+
eof = 0;
len = 0;
p = q = end = buf;
if (q >= end) {
if (eof)
break;
- if ((size_t)(q - p) >= sizeof(buf)) {
- warnx("passwd line too long");
- errno = EINVAL; /* hack */
- goto err;
+ while ((size_t)(q - p) >= size) {
+ if ((tmp = realloc(buf, size * 2)) == NULL) {
+ warnx("passwd line too long");
+ goto err;
+ }
+ p = tmp + (p - buf);
+ q = tmp + (q - buf);
+ end = tmp + (end - buf);
+ buf = tmp;
+ size = size * 2;
}
if (p < end) {
q = memmove(buf, p, end - p);
} else {
p = q = end = buf;
}
- readlen = read(ffd, end, sizeof(buf) - (end - buf));
+ readlen = read(ffd, end, size - (end - buf));
if (readlen == -1)
goto err;
else
break;
end += len;
len = end - buf;
- if (len < (ssize_t)sizeof(buf)) {
+ if (len < size) {
eof = 1;
if (len > 0 && buf[len - 1] != '\n')
++len, *end++ = '\n';
if (write(tfd, q, end - q) != end - q)
goto err;
q = buf;
- readlen = read(ffd, buf, sizeof(buf));
+ readlen = read(ffd, buf, size);
if (readlen == 0)
break;
else
write(tfd, "\n", 1) != 1)
goto err;
done:
- if (line != NULL)
- free(line);
+ free(line);
+ free(buf);
return (0);
err:
- if (line != NULL)
- free(line);
+ free(line);
+ free(buf);
return (-1);
}