From f8169403d8544ad1b4afe9e690f9d0b5e7aa9cac Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 19:00:16 +0000 Subject: Use sbuf(9) instead of homebrewed buffered string --- pw/Makefile | 2 +- pw/fileupd.c | 13 --------- pw/pw_conf.c | 94 +++++++++++++++++++++++++++++------------------------------- 3 files changed, 47 insertions(+), 62 deletions(-) diff --git a/pw/Makefile b/pw/Makefile index 269b145..69953da 100644 --- a/pw/Makefile +++ b/pw/Makefile @@ -8,7 +8,7 @@ SRCS= pw.c pw_conf.c pw_user.c pw_group.c pw_log.c pw_nis.c pw_vpw.c \ WARNS?= 2 -LIBADD= crypt util +LIBADD= crypt util sbuf .include diff --git a/pw/fileupd.c b/pw/fileupd.c index 7df4bb1..d2987ec 100644 --- a/pw/fileupd.c +++ b/pw/fileupd.c @@ -41,19 +41,6 @@ static const char rcsid[] = #include "pwupd.h" -int -extendline(char **buf, int * buflen, int needed) -{ - if (needed > *buflen) { - char *tmp = realloc(*buf, needed); - if (tmp == NULL) - return -1; - *buf = tmp; - *buflen = needed; - } - return *buflen; -} - int extendarray(char ***buf, int * buflen, int needed) { diff --git a/pw/pw_conf.c b/pw/pw_conf.c index 1289b3e..9dce918 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -29,6 +29,8 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ +#include +#include #include #include #include @@ -366,6 +368,7 @@ int write_userconfig(char const * file) { int fd; + struct sbuf *buf; if (file == NULL) file = _PATH_PW_CONF; @@ -376,126 +379,121 @@ write_userconfig(char const * file) if ((fp = fdopen(fd, "w")) == NULL) close(fd); else { - int i, j, k; - int len = LNBUFSZ; - char *buf = malloc(len); - + int i, j; + + buf = sbuf_new_auto(); for (i = _UC_NONE; i < _UC_FIELDS; i++) { int quote = 1; - char const *val = buf; - *buf = '\0'; + sbuf_clear(buf); switch (i) { case _UC_DEFAULTPWD: - val = boolean_str(config.default_password); + sbuf_cat(buf, boolean_str(config.default_password)); break; case _UC_REUSEUID: - val = boolean_str(config.reuse_uids); + sbuf_cat(buf, boolean_str(config.reuse_uids)); break; case _UC_REUSEGID: - val = boolean_str(config.reuse_gids); + sbuf_cat(buf, boolean_str(config.reuse_gids)); break; case _UC_NISPASSWD: - val = config.nispasswd ? config.nispasswd : ""; + sbuf_cat(buf, config.nispasswd ? + config.nispasswd : ""); quote = 0; break; case _UC_DOTDIR: - val = config.dotdir ? config.dotdir : boolean_str(0); + sbuf_cat(buf, config.dotdir ? + config.dotdir : boolean_str(0)); break; case _UC_NEWMAIL: - val = config.newmail ? config.newmail : boolean_str(0); + sbuf_cat(buf, config.newmail ? + config.newmail : boolean_str(0)); break; case _UC_LOGFILE: - val = config.logfile ? config.logfile : boolean_str(0); + sbuf_cat(buf, config.logfile ? + config.logfile : boolean_str(0)); break; case _UC_HOMEROOT: - val = config.home; + sbuf_cat(buf, config.home); break; case _UC_HOMEMODE: - sprintf(buf, "%04o", config.homemode); + sbuf_printf(buf, "%04o", config.homemode); quote = 0; break; case _UC_SHELLPATH: - val = config.shelldir; + sbuf_cat(buf, config.shelldir); break; case _UC_SHELLS: - for (j = k = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) { - char lbuf[64]; - int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", system_shells[j]); - if (l < 0) - l = 0; - if (l + k + 1 < len || extendline(&buf, &len, len + LNBUFSZ) != -1) { - strcpy(buf + k, lbuf); - k += l; - } + for (j = 0; j < _UC_MAXSHELLS && + system_shells[j] != NULL; j++) { + sbuf_printf(buf, "%s\"%s\"", j ? + "," : "", system_shells[j]); } quote = 0; break; case _UC_DEFAULTSHELL: - val = config.shell_default ? config.shell_default : bourne_shell; + sbuf_cat(buf, config.shell_default ? + config.shell_default : bourne_shell); break; case _UC_DEFAULTGROUP: - val = config.default_group ? config.default_group : ""; + sbuf_cat(buf, config.default_group ? + config.default_group : ""); break; case _UC_EXTRAGROUPS: extendarray(&config.groups, &config.numgroups, 200); - for (j = k = 0; j < config.numgroups && config.groups[j] != NULL; j++) { - char lbuf[64]; - int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", config.groups[j]); - if (l < 0) - l = 0; - if (l + k + 1 < len || extendline(&buf, &len, len + 1024) != -1) { - strcpy(buf + k, lbuf); - k += l; - } - } + for (j = 0; j < config.numgroups && + config.groups[j] != NULL; j++) + sbuf_printf(buf, "%s\"%s\"", j ? + "," : "", config.groups[j]); quote = 0; break; case _UC_DEFAULTCLASS: - val = config.default_class ? config.default_class : ""; + sbuf_cat(buf, config.default_class ? + config.default_class : ""); break; case _UC_MINUID: - sprintf(buf, "%lu", (unsigned long) config.min_uid); + sbuf_printf(buf, "%lu", (unsigned long) config.min_uid); quote = 0; break; case _UC_MAXUID: - sprintf(buf, "%lu", (unsigned long) config.max_uid); + sbuf_printf(buf, "%lu", (unsigned long) config.max_uid); quote = 0; break; case _UC_MINGID: - sprintf(buf, "%lu", (unsigned long) config.min_gid); + sbuf_printf(buf, "%lu", (unsigned long) config.min_gid); quote = 0; break; case _UC_MAXGID: - sprintf(buf, "%lu", (unsigned long) config.max_gid); + sbuf_printf(buf, "%lu", (unsigned long) config.max_gid); quote = 0; break; case _UC_EXPIRE: - sprintf(buf, "%d", config.expire_days); + sbuf_printf(buf, "%d", config.expire_days); quote = 0; break; case _UC_PASSWORD: - sprintf(buf, "%d", config.password_days); + sbuf_printf(buf, "%d", config.password_days); quote = 0; break; case _UC_NONE: break; } + sbuf_finish(buf); if (comments[i]) fputs(comments[i], fp); if (*kwds[i]) { if (quote) - fprintf(fp, "%s = \"%s\"\n", kwds[i], val); + fprintf(fp, "%s = \"%s\"\n", kwds[i], sbuf_data(buf)); else - fprintf(fp, "%s = %s\n", kwds[i], val); + fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf)); #if debugging - printf("WROTE: %s = %s\n", kwds[i], val); + printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf)); #endif } } - free(buf); + sbuf_delete(buf); return fclose(fp) != EOF; } } -- cgit v1.2.3 From c40fda9eafe03e89a520e779c3640d53528ff0d5 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 19:09:34 +0000 Subject: Use snprintf(3) instead of strcpy(3) + strncat(3) --- pw/pw_user.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pw/pw_user.c b/pw/pw_user.c index 483148a..0f7dd26 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -185,8 +185,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) * But we create a symlink from cnf->home -> "/usr" -> cnf->home */ if (strchr(cnf->home+1, '/') == NULL) { - strcpy(dbuf, "/usr"); - strncat(dbuf, cnf->home, MAXPATHLEN-5); + snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { chown(dbuf, 0, 0); /* -- cgit v1.2.3 From 8ed7622e3f439163a5b57edaaaedfc5a44cc0373 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 19:12:16 +0000 Subject: Replace malloc(3) + strcpy(3) + strcat(3) by asprintf(3) --- pw/pw_user.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pw/pw_user.c b/pw/pw_user.c index 0f7dd26..dc9a2d8 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -363,11 +363,9 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) if (mode == M_LOCK) { if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); - passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); + asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); if (passtmp == NULL) /* disaster */ errx(EX_UNAVAILABLE, "out of memory"); - strcpy(passtmp, locked_str); - strcat(passtmp, pwd->pw_passwd); pwd->pw_passwd = passtmp; edited = 1; } else if (mode == M_UNLOCK) { -- cgit v1.2.3 From 629e41f9d4d2cda0f7b1ed3b1fb929cfbf0873f1 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 19:22:33 +0000 Subject: Remove unneeded headers --- pw/fileupd.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pw/fileupd.c b/pw/fileupd.c index d2987ec..dc32712 100644 --- a/pw/fileupd.c +++ b/pw/fileupd.c @@ -29,15 +29,7 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include -#include #include -#include -#include -#include -#include -#include -#include #include "pwupd.h" -- cgit v1.2.3 From b55b6a4630fcb1f1ae2fee2d830b0e7e6fa9fca2 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 19:29:55 +0000 Subject: Remove some uneeded headers --- pw/grupd.c | 4 ---- pw/pw_nis.c | 3 --- pw/pw_user.c | 1 - 3 files changed, 8 deletions(-) diff --git a/pw/grupd.c b/pw/grupd.c index 3f78e95..74cc390 100644 --- a/pw/grupd.c +++ b/pw/grupd.c @@ -35,10 +35,6 @@ static const char rcsid[] = #include #include #include -#include -#include -#include -#include #include #include "pwupd.h" diff --git a/pw/pw_nis.c b/pw/pw_nis.c index 918fc30..c786cc7 100644 --- a/pw/pw_nis.c +++ b/pw/pw_nis.c @@ -29,9 +29,6 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include -#include -#include #include #include #include diff --git a/pw/pw_user.c b/pw/pw_user.c index dc9a2d8..bdb3cd6 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -40,7 +40,6 @@ static const char rcsid[] = #include #include #include -#include #include #include #include -- cgit v1.2.3 From 9cea317d482d6e554ba9b98df718e69f37709fed Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 21:53:33 +0000 Subject: Return from the function as early as possible This reduces the depth of the if statements and improves clarity of the code --- pw/pw_conf.c | 233 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 116 insertions(+), 117 deletions(-) diff --git a/pw/pw_conf.c b/pw/pw_conf.c index 9dce918..4e97fd9 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -368,134 +368,133 @@ int write_userconfig(char const * file) { int fd; + int i, j; struct sbuf *buf; + FILE *fp; if (file == NULL) file = _PATH_PW_CONF; - if ((fd = open(file, O_CREAT | O_RDWR | O_TRUNC | O_EXLOCK, 0644)) != -1) { - FILE *fp; + if ((fd = open(file, O_CREAT|O_RDWR|O_TRUNC|O_EXLOCK, 0644)) == -1) + return (0); - if ((fp = fdopen(fd, "w")) == NULL) - close(fd); - else { - int i, j; + if ((fp = fdopen(fd, "w")) == NULL) { + close(fd); + return (0); + } - buf = sbuf_new_auto(); - for (i = _UC_NONE; i < _UC_FIELDS; i++) { - int quote = 1; - - sbuf_clear(buf); - switch (i) { - case _UC_DEFAULTPWD: - sbuf_cat(buf, boolean_str(config.default_password)); - break; - case _UC_REUSEUID: - sbuf_cat(buf, boolean_str(config.reuse_uids)); - break; - case _UC_REUSEGID: - sbuf_cat(buf, boolean_str(config.reuse_gids)); - break; - case _UC_NISPASSWD: - sbuf_cat(buf, config.nispasswd ? - config.nispasswd : ""); - quote = 0; - break; - case _UC_DOTDIR: - sbuf_cat(buf, config.dotdir ? - config.dotdir : boolean_str(0)); - break; - case _UC_NEWMAIL: - sbuf_cat(buf, config.newmail ? - config.newmail : boolean_str(0)); - break; - case _UC_LOGFILE: - sbuf_cat(buf, config.logfile ? - config.logfile : boolean_str(0)); - break; - case _UC_HOMEROOT: - sbuf_cat(buf, config.home); - break; - case _UC_HOMEMODE: - sbuf_printf(buf, "%04o", config.homemode); - quote = 0; - break; - case _UC_SHELLPATH: - sbuf_cat(buf, config.shelldir); - break; - case _UC_SHELLS: - for (j = 0; j < _UC_MAXSHELLS && - system_shells[j] != NULL; j++) { - sbuf_printf(buf, "%s\"%s\"", j ? - "," : "", system_shells[j]); - } - quote = 0; - break; - case _UC_DEFAULTSHELL: - sbuf_cat(buf, config.shell_default ? - config.shell_default : bourne_shell); - break; - case _UC_DEFAULTGROUP: - sbuf_cat(buf, config.default_group ? - config.default_group : ""); - break; - case _UC_EXTRAGROUPS: - extendarray(&config.groups, &config.numgroups, 200); - for (j = 0; j < config.numgroups && - config.groups[j] != NULL; j++) - sbuf_printf(buf, "%s\"%s\"", j ? - "," : "", config.groups[j]); - quote = 0; - break; - case _UC_DEFAULTCLASS: - sbuf_cat(buf, config.default_class ? - config.default_class : ""); - break; - case _UC_MINUID: - sbuf_printf(buf, "%lu", (unsigned long) config.min_uid); - quote = 0; - break; - case _UC_MAXUID: - sbuf_printf(buf, "%lu", (unsigned long) config.max_uid); - quote = 0; - break; - case _UC_MINGID: - sbuf_printf(buf, "%lu", (unsigned long) config.min_gid); - quote = 0; - break; - case _UC_MAXGID: - sbuf_printf(buf, "%lu", (unsigned long) config.max_gid); - quote = 0; - break; - case _UC_EXPIRE: - sbuf_printf(buf, "%d", config.expire_days); - quote = 0; - break; - case _UC_PASSWORD: - sbuf_printf(buf, "%d", config.password_days); - quote = 0; - break; - case _UC_NONE: - break; - } - sbuf_finish(buf); + buf = sbuf_new_auto(); + for (i = _UC_NONE; i < _UC_FIELDS; i++) { + int quote = 1; + + sbuf_clear(buf); + switch (i) { + case _UC_DEFAULTPWD: + sbuf_cat(buf, boolean_str(config.default_password)); + break; + case _UC_REUSEUID: + sbuf_cat(buf, boolean_str(config.reuse_uids)); + break; + case _UC_REUSEGID: + sbuf_cat(buf, boolean_str(config.reuse_gids)); + break; + case _UC_NISPASSWD: + sbuf_cat(buf, config.nispasswd ? config.nispasswd : + ""); + quote = 0; + break; + case _UC_DOTDIR: + sbuf_cat(buf, config.dotdir ? config.dotdir : + boolean_str(0)); + break; + case _UC_NEWMAIL: + sbuf_cat(buf, config.newmail ? config.newmail : + boolean_str(0)); + break; + case _UC_LOGFILE: + sbuf_cat(buf, config.logfile ? config.logfile : + boolean_str(0)); + break; + case _UC_HOMEROOT: + sbuf_cat(buf, config.home); + break; + case _UC_HOMEMODE: + sbuf_printf(buf, "%04o", config.homemode); + quote = 0; + break; + case _UC_SHELLPATH: + sbuf_cat(buf, config.shelldir); + break; + case _UC_SHELLS: + for (j = 0; j < _UC_MAXSHELLS && + system_shells[j] != NULL; j++) + sbuf_printf(buf, "%s\"%s\"", j ? + "," : "", system_shells[j]); + quote = 0; + break; + case _UC_DEFAULTSHELL: + sbuf_cat(buf, config.shell_default ? + config.shell_default : bourne_shell); + break; + case _UC_DEFAULTGROUP: + sbuf_cat(buf, config.default_group ? + config.default_group : ""); + break; + case _UC_EXTRAGROUPS: + extendarray(&config.groups, &config.numgroups, 200); + for (j = 0; j < config.numgroups && + config.groups[j] != NULL; j++) + sbuf_printf(buf, "%s\"%s\"", j ? + "," : "", config.groups[j]); + quote = 0; + break; + case _UC_DEFAULTCLASS: + sbuf_cat(buf, config.default_class ? + config.default_class : ""); + break; + case _UC_MINUID: + sbuf_printf(buf, "%lu", (unsigned long) config.min_uid); + quote = 0; + break; + case _UC_MAXUID: + sbuf_printf(buf, "%lu", (unsigned long) config.max_uid); + quote = 0; + break; + case _UC_MINGID: + sbuf_printf(buf, "%lu", (unsigned long) config.min_gid); + quote = 0; + break; + case _UC_MAXGID: + sbuf_printf(buf, "%lu", (unsigned long) config.max_gid); + quote = 0; + break; + case _UC_EXPIRE: + sbuf_printf(buf, "%d", config.expire_days); + quote = 0; + break; + case _UC_PASSWORD: + sbuf_printf(buf, "%d", config.password_days); + quote = 0; + break; + case _UC_NONE: + break; + } + sbuf_finish(buf); - if (comments[i]) - fputs(comments[i], fp); + if (comments[i]) + fputs(comments[i], fp); - if (*kwds[i]) { - if (quote) - fprintf(fp, "%s = \"%s\"\n", kwds[i], sbuf_data(buf)); - else - fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf)); + if (*kwds[i]) { + if (quote) + fprintf(fp, "%s = \"%s\"\n", kwds[i], + sbuf_data(buf)); + else + fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf)); #if debugging - printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf)); + printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf)); #endif - } - } - sbuf_delete(buf); - return fclose(fp) != EOF; } } - return 0; + sbuf_delete(buf); + return (fclose(fp) != EOF); } -- cgit v1.2.3 From 2f3cb3756d908fc83cd3e754db1d6652a4cb0615 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 22:08:30 +0000 Subject: Remove now unused LNBUFSZ buffer size --- pw/pw_conf.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/pw/pw_conf.c b/pw/pw_conf.c index 4e97fd9..f17857b 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -222,9 +222,6 @@ newstr(char const * p) return q; } -#define LNBUFSZ 1024 - - struct userconf * read_userconfig(char const * file) { -- cgit v1.2.3 From 12334184dee67245367ba6ff6b4914392538bc4d Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 22:43:44 +0000 Subject: Use snprintf(3) instead of sprintf(3) Remove useless "else" --- pw/pw_user.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pw/pw_user.c b/pw/pw_user.c index bdb3cd6..d7aed71 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -1018,17 +1018,16 @@ static char * pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) { struct carg *arg = getarg(args, 'd'); + static char home[128]; if (arg) - return arg->val; - else { - static char home[128]; + return (arg->val); - if (cnf->home == NULL || *cnf->home == '\0') - errx(EX_CONFIG, "no base home directory set"); - sprintf(home, "%s/%s", cnf->home, user); - return home; - } + if (cnf->home == NULL || *cnf->home == '\0') + errx(EX_CONFIG, "no base home directory set"); + snprintf(home, sizeof(home), "%s/%s", cnf->home, user); + + return (home); } static char * -- cgit v1.2.3 From 4db6c45d7e880b5d8f46254434f002dd425cef60 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sat, 9 May 2015 22:48:48 +0000 Subject: Replace sprintf(3) with snprintf(3) --- pw/pw_user.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pw/pw_user.c b/pw/pw_user.c index d7aed71..b058aab 100644 --- a/pw/pw_user.c +++ b/pw/pw_user.c @@ -397,7 +397,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) */ 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); + snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); system(file); } } @@ -405,7 +405,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) * Save these for later, since contents of pwd may be * invalidated by deletion */ - sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); + snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); strlcpy(home, pwd->pw_dir, sizeof(home)); gr = GETGRGID(pwd->pw_gid); if (gr != NULL) @@ -811,7 +811,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) */ if (mode == M_ADD) { if (!PWALTDIR()) { - sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); + snprintf(line, sizeof(line), "%s/%s", _PATH_MAILDIR, pwd->pw_name); close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & * mtime */ chown(line, pwd->pw_uid, pwd->pw_gid); @@ -955,7 +955,7 @@ pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer * function will happily handle that case for us and exit. */ if (GETGRGID(prefer) == NULL) { - sprintf(tmp, "%lu", (unsigned long) prefer); + snprintf(tmp, sizeof(tmp), "%u", prefer); addarg(&grpargs, 'g', tmp); } if (getarg(args, 'N')) @@ -1048,12 +1048,12 @@ shell_path(char const * path, char *shells[], char *sh) static char shellpath[256]; if (sh != NULL) { - sprintf(shellpath, "%s/%s", p, sh); + snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); if (access(shellpath, X_OK) == 0) return shellpath; } else for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { - sprintf(shellpath, "%s/%s", p, shells[i]); + snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); if (access(shellpath, X_OK) == 0) return shellpath; } @@ -1303,7 +1303,7 @@ rmat(uid_t uid) st.st_uid == uid) { char tmp[MAXPATHLEN]; - sprintf(tmp, "/usr/bin/atrm %s", e->d_name); + snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); system(tmp); } } -- cgit v1.2.3 From 4d66b7efe222802d3efab85f54488efbc7f97602 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 09:02:15 +0000 Subject: Some style(9) fixes --- pw/pwupd.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pw/pwupd.c b/pw/pwupd.c index c2a9a53..e8defa3 100644 --- a/pw/pwupd.c +++ b/pw/pwupd.c @@ -52,12 +52,13 @@ int setpwdir(const char * dir) { if (dir == NULL) - return -1; + return (-1); else pwpath = strdup(dir); if (pwpath == NULL) - return -1; - return 0; + return (-1); + + return (0); } char * @@ -66,7 +67,8 @@ getpwpath(char const * file) static char pathbuf[MAXPATHLEN]; snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file); - return pathbuf; + + return (pathbuf); } static int @@ -101,7 +103,8 @@ pwdb(char *arg,...) i = EIO; } va_end(ap); - return i; + + return (i); } static int @@ -146,19 +149,22 @@ pw_update(struct passwd * pwd, char const * user) free(pw); pw_fini(); } - return 0; + + return (0); } int addpwent(struct passwd * pwd) { - return pw_update(pwd, NULL); + + return (pw_update(pwd, NULL)); } int chgpwent(char const * login, struct passwd * pwd) { - return pw_update(pwd, login); + + return (pw_update(pwd, login)); } int @@ -167,5 +173,6 @@ delpwent(struct passwd * pwd) char login[MAXLOGNAME]; strlcpy(login, pwd->pw_name, MAXLOGNAME); - return pw_update(NULL, login); + + return (pw_update(NULL, login)); } -- cgit v1.2.3 From 5691ab931306852c6c08a9d01a37abbaa5ddc7c0 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 09:11:12 +0000 Subject: if the check of the pw db fails return the failed value --- pw/pwupd.c | 72 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/pw/pwupd.c b/pw/pwupd.c index e8defa3..51d732e 100644 --- a/pw/pwupd.c +++ b/pw/pwupd.c @@ -110,45 +110,43 @@ pwdb(char *arg,...) static int pw_update(struct passwd * pwd, char const * user) { - int rc = 0; - - rc = pwdb("-C", (char *)NULL); /* Check only */ - if (rc == 0) { - int pfd, tfd; - struct passwd *pw = NULL; - struct passwd *old_pw = NULL; - - if (pwd != NULL) - pw = pw_dup(pwd); - - if (user != NULL) - old_pw = GETPWNAM(user); - - if (pw_init(pwpath, NULL)) - err(1, "pw_init()"); - if ((pfd = pw_lock()) == -1) { - pw_fini(); - err(1, "pw_lock()"); - } - if ((tfd = pw_tmp(-1)) == -1) { - pw_fini(); - err(1, "pw_tmp()"); - } - if (pw_copy(pfd, tfd, pw, old_pw) == -1) { - pw_fini(); - err(1, "pw_copy()"); - } - /* - * in case of deletion of a user, the whole database - * needs to be regenerated - */ - if (pw_mkdb(pw != NULL ? pw->pw_name : NULL) == -1) { - pw_fini(); - err(1, "pw_mkdb()"); - } - free(pw); + struct passwd *pw = NULL; + struct passwd *old_pw = NULL; + int rc, pfd, tfd; + + if ((rc = pwdb("-C", NULL)) != 0) + return (rc); + + if (pwd != NULL) + pw = pw_dup(pwd); + + if (user != NULL) + old_pw = GETPWNAM(user); + + if (pw_init(pwpath, NULL)) + err(1, "pw_init()"); + if ((pfd = pw_lock()) == -1) { + pw_fini(); + err(1, "pw_lock()"); + } + if ((tfd = pw_tmp(-1)) == -1) { + pw_fini(); + err(1, "pw_tmp()"); + } + if (pw_copy(pfd, tfd, pw, old_pw) == -1) { + pw_fini(); + err(1, "pw_copy()"); + } + /* + * in case of deletion of a user, the whole database + * needs to be regenerated + */ + if (pw_mkdb(pw != NULL ? pw->pw_name : NULL) == -1) { pw_fini(); + err(1, "pw_mkdb()"); } + free(pw); + pw_fini(); return (0); } -- cgit v1.2.3 From f87aeed8e4c049e6510accf0e0dfab0d29b7f27f Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 09:23:03 +0000 Subject: The pwdb function is only used once to check the database rename it pwdb_check and simplify it accordingly --- pw/pwupd.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/pw/pwupd.c b/pw/pwupd.c index 51d732e..710e901 100644 --- a/pw/pwupd.c +++ b/pw/pwupd.c @@ -33,7 +33,6 @@ static const char rcsid[] = #include #include #include -#include #include #include #include @@ -72,19 +71,15 @@ getpwpath(char const * file) } static int -pwdb(char *arg,...) +pwdb_check(void) { int i = 0; pid_t pid; - va_list ap; char *args[10]; args[i++] = _PATH_PWD_MKDB; - va_start(ap, arg); - while (i < 6 && arg != NULL) { - args[i++] = arg; - arg = va_arg(ap, char *); - } + args[i++] = "-C"; + if (pwpath != pathpwd) { args[i++] = "-d"; args[i++] = pwpath; @@ -102,7 +97,6 @@ pwdb(char *arg,...) if (WEXITSTATUS(i)) i = EIO; } - va_end(ap); return (i); } @@ -114,7 +108,7 @@ pw_update(struct passwd * pwd, char const * user) struct passwd *old_pw = NULL; int rc, pfd, tfd; - if ((rc = pwdb("-C", NULL)) != 0) + if ((rc = pwdb_check()) != 0) return (rc); if (pwd != NULL) -- cgit v1.2.3 From c8d02e06b734db2f87942aa323b6004b52b8022f Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 09:33:15 +0000 Subject: Remove useless call to extendarray --- pw/pw_conf.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pw/pw_conf.c b/pw/pw_conf.c index f17857b..209982c 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -438,7 +438,6 @@ write_userconfig(char const * file) config.default_group : ""); break; case _UC_EXTRAGROUPS: - extendarray(&config.groups, &config.numgroups, 200); for (j = 0; j < config.numgroups && config.groups[j] != NULL; j++) sbuf_printf(buf, "%s\"%s\"", j ? -- cgit v1.2.3 From 23c65c2198c1b58184f7fafd8a70e8274fe1e91a Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 10:02:09 +0000 Subject: Use strndup(3) instead of malloc(3) + memcpy(3) Check the return of strndup --- pw/pw_conf.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pw/pw_conf.c b/pw/pw_conf.c index 209982c..46474a1 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -34,6 +34,7 @@ static const char rcsid[] = #include #include #include +#include #include "pw.h" @@ -211,15 +212,18 @@ boolean_str(int val) char * newstr(char const * p) { - char *q = NULL; + char *q; + size_t l; - if ((p = unquote(p)) != NULL) { - int l = strlen(p) + 1; + if ((p = unquote(p)) == NULL) + return (NULL); - if ((q = malloc(l)) != NULL) - memcpy(q, p, l); - } - return q; + l = strlen(p) + 1; + + if ((q = strndup(p, l)) == NULL) + err(1, "strndup()"); + + return (q); } struct userconf * -- cgit v1.2.3 From 97f18faf798474452fd15880e7e8c81d786465e7 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 10:15:36 +0000 Subject: The initial logic for allocating the new string was wrong, the conversion to strndup(3) duplicated the same mistake, actually strdup(3) is good enough to allocate the new string. --- pw/pw_conf.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pw/pw_conf.c b/pw/pw_conf.c index 46474a1..e988f4b 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -213,15 +213,12 @@ char * newstr(char const * p) { char *q; - size_t l; if ((p = unquote(p)) == NULL) return (NULL); - l = strlen(p) + 1; - - if ((q = strndup(p, l)) == NULL) - err(1, "strndup()"); + if ((q = strdup(p)) == NULL) + err(1, "strdup()"); return (q); } -- cgit v1.2.3 From c9b35173704bf9aaebcf62d0915543b81e9ee8d3 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 11:18:01 +0000 Subject: Use calloc(3) instead of malloc(3) + memset(3) While here check the return of calloc(3) --- pw/pw_conf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pw/pw_conf.c b/pw/pw_conf.c index e988f4b..99d3e8f 100644 --- a/pw/pw_conf.c +++ b/pw/pw_conf.c @@ -234,8 +234,10 @@ read_userconfig(char const * file) buf = NULL; linecap = 0; - extendarray(&config.groups, &config.numgroups, 200); - memset(config.groups, 0, config.numgroups * sizeof(char *)); + config.numgroups = 200; + config.groups = calloc(config.numgroups, sizeof(char *)); + if (config.groups == NULL) + err(1, "calloc()"); if (file == NULL) file = _PATH_PW_CONF; -- cgit v1.2.3 From 09c3fc64aeceba152705f8feeb0d4df0413ff5af Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 10 May 2015 11:24:16 +0000 Subject: Remove extendline definition Remove now unused PWBUFSZ define --- pw/pwupd.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/pw/pwupd.h b/pw/pwupd.h index 200ffee..d6e39ce 100644 --- a/pw/pwupd.h +++ b/pw/pwupd.h @@ -112,10 +112,7 @@ void vendgrent(void); void copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid); void rm_r(char const * dir, uid_t uid); -int extendline(char **buf, int *buflen, int needed); int extendarray(char ***buf, int *buflen, int needed); __END_DECLS -#define PWBUFSZ 1024 - #endif /* !_PWUPD_H */ -- cgit v1.2.3 From 9450d093eacc6a58d124997fb60de631275447b1 Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Wed, 27 May 2015 18:11:05 +0000 Subject: Export a list of VM objects in the system via a sysctl. The list can be examined via 'vmstat -o'. It can be used to determine which files are using physical pages of memory and how much each is using. Differential Revision: https://reviews.freebsd.org/D2277 Reviewed by: alc, kib MFC after: 2 weeks Sponsored by: Norse Corp, Inc. (forward porting to HEAD/10) --- libutil/libutil.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libutil/libutil.h b/libutil/libutil.h index b8b9836..b20ffa2 100644 --- a/libutil/libutil.h +++ b/libutil/libutil.h @@ -102,6 +102,8 @@ struct kinfo_file * kinfo_getfile(pid_t _pid, int *_cntp); struct kinfo_vmentry * kinfo_getvmmap(pid_t _pid, int *_cntp); +struct kinfo_vmobject * + kinfo_getvmobject(int *_cntp); struct kinfo_proc * kinfo_getallproc(int *_cntp); struct kinfo_proc * -- cgit v1.2.3