lib/, src/: Use SNPRINTF() instead of its pattern

The variable declarations for the buffers have been aligned in this
commit, so that they appear in the diff, making it easier to review.

Some important but somewhat tangent changes included in this commit:

-  lib/nss.c: The size was being defined as 65, but then used as 64.
   That was a bug, although not an important one; we were just wasting
   one byte.  Fix that while we replace snprintf() by SNPRINTF(), which
   will get the size from sizeof(), and thus will use the real size.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-08-26 12:32:32 +02:00
committed by Iker Pedrosa
parent 8c6634d9bc
commit cf9cc6963c
18 changed files with 159 additions and 165 deletions
+7 -5
View File
@@ -33,6 +33,7 @@
#include "pwio.h"
#include "shadowio.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/strtcpy.h"
#ifdef WITH_TCB
#include "tcbfuncs.h"
@@ -40,6 +41,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
/*
* Global variables
*/
@@ -158,19 +160,19 @@ usage (int status)
*/
static int new_fields (void)
{
char buf[200];
char buf[200];
(void) puts (_("Enter the new value, or press ENTER for the default"));
(void) puts ("");
(void) snprintf (buf, sizeof buf, "%ld", mindays);
SNPRINTF(buf, "%ld", mindays);
change_field (buf, sizeof buf, _("Minimum Password Age"));
if ( (getlong (buf, &mindays) == 0)
|| (mindays < -1)) {
return 0;
}
(void) snprintf (buf, sizeof buf, "%ld", maxdays);
SNPRINTF(buf, "%ld", maxdays);
change_field (buf, sizeof buf, _("Maximum Password Age"));
if ( (getlong (buf, &maxdays) == 0)
|| (maxdays < -1)) {
@@ -194,14 +196,14 @@ static int new_fields (void)
}
}
(void) snprintf (buf, sizeof buf, "%ld", warndays);
SNPRINTF(buf, "%ld", warndays);
change_field (buf, sizeof buf, _("Password Expiration Warning"));
if ( (getlong (buf, &warndays) == 0)
|| (warndays < -1)) {
return 0;
}
(void) snprintf (buf, sizeof buf, "%ld", inactdays);
SNPRINTF(buf, "%ld", inactdays);
change_field (buf, sizeof buf, _("Password Inactive"));
if ( (getlong (buf, &inactdays) == 0)
|| (inactdays < -1)) {
+8 -6
View File
@@ -32,8 +32,10 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/strtcpy.h"
/*
* Global variables.
*/
@@ -614,9 +616,9 @@ static void check_fields (void)
*/
int main (int argc, char **argv)
{
const struct passwd *pw; /* password file entry */
char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
char *user;
char new_gecos[BUFSIZ];
char *user;
const struct passwd *pw;
/*
* Get the program name. The program name is used as a
@@ -723,9 +725,9 @@ int main (int argc, char **argv)
fprintf (stderr, _("%s: fields too long\n"), Prog);
fail_exit (E_NOPERM);
}
snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
fullnm, roomno, workph, homeph,
('\0' != slop[0]) ? "," : "", slop);
SNPRINTF(new_gecos, "%s,%s,%s,%s%s%s",
fullnm, roomno, workph, homeph,
('\0' != slop[0]) ? "," : "", slop);
/* Rewrite the user's gecos in the passwd file */
update_gecos (user, new_gecos);
+21 -31
View File
@@ -35,8 +35,10 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/strtcpy.h"
/*
* Global variables
*/
@@ -385,13 +387,13 @@ static void log_gpasswd_failure (const char *suffix)
#ifdef WITH_AUDIT
char buf[1024];
#endif
if (aflg) {
SYSLOG ((LOG_ERR,
"%s failed to add user %s to group %s%s",
myname, user, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to add user %s to group %s%s",
SNPRINTF(buf, "%s failed to add user %s to group %s%s",
myname, user, group, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -403,8 +405,7 @@ static void log_gpasswd_failure (const char *suffix)
"%s failed to remove user %s from group %s%s",
myname, user, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to remove user %s from group %s%s",
SNPRINTF(buf, "%s failed to remove user %s from group %s%s",
myname, user, group, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -416,8 +417,7 @@ static void log_gpasswd_failure (const char *suffix)
"%s failed to remove password of group %s%s",
myname, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to remove password of group %s%s",
SNPRINTF(buf, "%s failed to remove password of group %s%s",
myname, group, suffix);
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
buf,
@@ -429,8 +429,7 @@ static void log_gpasswd_failure (const char *suffix)
"%s failed to restrict access to group %s%s",
myname, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to restrict access to group %s%s",
SNPRINTF(buf, "%s failed to restrict access to group %s%s",
myname, group, suffix);
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
buf,
@@ -444,8 +443,7 @@ static void log_gpasswd_failure (const char *suffix)
"%s failed to set the administrators of group %s to %s%s",
myname, group, admins, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to set the administrators of group %s to %s%s",
SNPRINTF(buf, "%s failed to set the administrators of group %s to %s%s",
myname, group, admins, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -459,8 +457,7 @@ static void log_gpasswd_failure (const char *suffix)
"%s failed to set the members of group %s to %s%s",
myname, group, members, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to set the members of group %s to %s%s",
SNPRINTF(buf, "%s failed to set the members of group %s to %s%s",
myname, group, members, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -473,8 +470,7 @@ static void log_gpasswd_failure (const char *suffix)
"%s failed to change password of group %s%s",
myname, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"%s failed to change password of group %s%s",
SNPRINTF(buf, "%s failed to change password of group %s%s",
myname, group, suffix);
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
buf,
@@ -493,7 +489,7 @@ static void log_gpasswd_failure_group (unused void *arg)
{
char buf[1024];
snprintf(buf, sizeof(buf), " in %s", gr_dbname());
SNPRINTF(buf, " in %s", gr_dbname());
log_gpasswd_failure (buf);
}
@@ -502,7 +498,7 @@ static void log_gpasswd_failure_gshadow (unused void *arg)
{
char buf[1024];
snprintf(buf, sizeof(buf), " in %s", sgr_dbname());
SNPRINTF(buf, " in %s", sgr_dbname());
log_gpasswd_failure (buf);
}
#endif /* SHADOWGRP */
@@ -512,13 +508,13 @@ static void log_gpasswd_success (const char *suffix)
#ifdef WITH_AUDIT
char buf[1024];
#endif
if (aflg) {
SYSLOG ((LOG_INFO,
"user %s added by %s to group %s%s",
user, myname, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"user %s added by %s to group %s%s",
SNPRINTF(buf, "user %s added by %s to group %s%s",
user, myname, group, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -530,8 +526,7 @@ static void log_gpasswd_success (const char *suffix)
"user %s removed by %s from group %s%s",
user, myname, group, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"user %s removed by %s from group %s%s",
SNPRINTF(buf, "user %s removed by %s from group %s%s",
user, myname, group, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -543,8 +538,7 @@ static void log_gpasswd_success (const char *suffix)
"password of group %s removed by %s%s",
group, myname, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"password of group %s removed by %s%s",
SNPRINTF(buf, "password of group %s removed by %s%s",
group, myname, suffix);
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
buf,
@@ -556,8 +550,7 @@ static void log_gpasswd_success (const char *suffix)
"access to group %s restricted by %s%s",
group, myname, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"access to group %s restricted by %s%s",
SNPRINTF(buf, "access to group %s restricted by %s%s",
group, myname, suffix);
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
buf,
@@ -571,8 +564,7 @@ static void log_gpasswd_success (const char *suffix)
"administrators of group %s set by %s to %s%s",
group, myname, admins, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"administrators of group %s set by %s to %s%s",
SNPRINTF(buf, "administrators of group %s set by %s to %s%s",
group, myname, admins, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -586,8 +578,7 @@ static void log_gpasswd_success (const char *suffix)
"members of group %s set by %s to %s%s",
group, myname, members, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"members of group %s set by %s to %s%s",
SNPRINTF(buf, "members of group %s set by %s to %s%s",
group, myname, members, suffix);
audit_logger (AUDIT_USER_ACCT, Prog,
buf,
@@ -600,8 +591,7 @@ static void log_gpasswd_success (const char *suffix)
"password of group %s changed by %s%s",
group, myname, suffix));
#ifdef WITH_AUDIT
snprintf(buf, sizeof(buf),
"password of group %s changed by %s%s",
SNPRINTF(buf, "password of group %s changed by %s%s",
group, myname, suffix);
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
buf,
@@ -620,7 +610,7 @@ static void log_gpasswd_success_group (unused void *arg)
{
char buf[1024];
snprintf(buf, sizeof(buf), " in %s", gr_dbname());
SNPRINTF(buf, " in %s", gr_dbname());
log_gpasswd_success (buf);
}
+27 -31
View File
@@ -37,6 +37,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/strtcpy.h"
#ifdef USE_PAM
@@ -476,34 +477,34 @@ static /*@observer@*/const char *get_failent_user (/*@returned@*/const char *use
*/
int main (int argc, char **argv)
{
const char *tmptty;
char tty[BUFSIZ];
unsigned int delay;
unsigned int retries;
bool subroot = false;
int err;
unsigned int timeout;
const char *cp;
const char *tmp;
char fromhost[512];
struct passwd *pwd = NULL;
char **envp = environ;
const char *failent_user;
char *host = NULL;
int err;
bool subroot = false;
char **envp = environ;
char *host = NULL;
char tty[BUFSIZ];
char fromhost[512];
const char *failent_user;
const char *tmptty;
const char *cp;
const char *tmp;
unsigned int delay;
unsigned int retries;
unsigned int timeout;
struct passwd *pwd = NULL;
#if defined(USE_PAM)
int retcode;
pid_t child;
char *pam_user = NULL;
int retcode;
char *pam_user = NULL;
pid_t child;
#else
bool is_console;
struct spwd *spwd = NULL;
# if defined(ENABLE_LASTLOG)
char ptime[80];
char ptime[80];
# endif
#endif
#if defined(RLOGIN)
char term[128] = "";
char term[128] = "";
#endif
/*
@@ -647,19 +648,16 @@ int main (int argc, char **argv)
}
if ('\0' != *cp) {
snprintf (fromhost, sizeof fromhost,
" on '%.100s' from '%.200s'", tty, cp);
SNPRINTF(fromhost, " on '%.100s' from '%.200s'", tty, cp);
} else {
snprintf (fromhost, sizeof fromhost,
" on '%.100s'", tty);
SNPRINTF(fromhost, " on '%.100s'", tty);
}
free(host);
top:
/* only allow ALARM sec. for login */
timeout = getdef_unum ("LOGIN_TIMEOUT", ALARM);
snprintf (tmsg, sizeof tmsg,
_("\nLogin timed out after %u seconds.\n"), timeout);
SNPRINTF(tmsg, _("\nLogin timed out after %u seconds.\n"), timeout);
(void) signal (SIGALRM, alarm_handler);
if (timeout > 0) {
(void) alarm (timeout);
@@ -698,15 +696,13 @@ int main (int argc, char **argv)
#endif
/* if fflg, then the user has already been authenticated */
if (!fflg) {
unsigned int failcount = 0;
char hostn[256];
char loginprompt[256]; /* That's one hell of a prompt :) */
char hostn[256];
char loginprompt[256]; //That's one hell of a prompt :)
unsigned int failcount = 0;
/* Make the login prompt look like we want it */
if (gethostname (hostn, sizeof (hostn)) == 0) {
snprintf (loginprompt,
sizeof (loginprompt),
_("%s login: "), hostn);
SNPRINTF(loginprompt, _("%s login: "), hostn);
} else {
STRTCPY(loginprompt, _("login: "));
}
+15 -22
View File
@@ -25,6 +25,8 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
/*
* Global variables
@@ -186,9 +188,8 @@ static void check_perms (const struct group *grp,
if (grp->gr_passwd[0] == '\0' ||
strcmp (cpasswd, grp->gr_passwd) != 0) {
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf),
"authentication new-gid=%lu",
(unsigned long) grp->gr_gid);
SNPRINTF(audit_buf, "authentication new-gid=%lu",
(unsigned long) grp->gr_gid);
audit_logger (AUDIT_GRP_AUTH, Prog,
audit_buf, NULL, getuid (), 0);
#endif
@@ -200,9 +201,8 @@ static void check_perms (const struct group *grp,
goto failure;
}
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf),
"authentication new-gid=%lu",
(unsigned long) grp->gr_gid);
SNPRINTF(audit_buf, "authentication new-gid=%lu",
(unsigned long) grp->gr_gid);
audit_logger (AUDIT_GRP_AUTH, Prog,
audit_buf, NULL, getuid (), 1);
#endif
@@ -217,8 +217,7 @@ failure:
closelog ();
#ifdef WITH_AUDIT
if (groupname) {
snprintf (audit_buf, sizeof(audit_buf),
"changing new-group=%s", groupname);
SNPRINTF(audit_buf, "changing new-group=%s", groupname);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
} else {
@@ -299,8 +298,8 @@ static void syslog_sg (const char *name, const char *group)
is_newgrp ? "newgrp" : "sg", strerror (errno));
#ifdef WITH_AUDIT
if (group) {
snprintf (audit_buf, sizeof(audit_buf),
"changing new-group=%s", group);
SNPRINTF(audit_buf,
"changing new-group=%s", group);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
} else {
@@ -557,8 +556,7 @@ int main (int argc, char **argv)
perror ("getgroups");
#ifdef WITH_AUDIT
if (group) {
snprintf (audit_buf, sizeof(audit_buf),
"changing new-group=%s", group);
SNPRINTF(audit_buf, "changing new-group=%s", group);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
} else {
@@ -717,8 +715,7 @@ int main (int argc, char **argv)
if (setgid (gid) != 0) {
perror ("setgid");
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf),
"changing new-gid=%lu", (unsigned long) gid);
SNPRINTF(audit_buf, "changing new-gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
#endif
@@ -728,8 +725,7 @@ int main (int argc, char **argv)
if (setuid (getuid ()) != 0) {
perror ("setuid");
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf),
"changing new-gid=%lu", (unsigned long) gid);
SNPRINTF(audit_buf, "changing new-gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
#endif
@@ -744,8 +740,7 @@ int main (int argc, char **argv)
closelog ();
execl (SHELL, "sh", "-c", command, (char *) NULL);
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf),
"changing new-gid=%lu", (unsigned long) gid);
SNPRINTF(audit_buf, "changing new-gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
#endif
@@ -811,8 +806,7 @@ int main (int argc, char **argv)
}
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf), "changing new-gid=%lu",
(unsigned long) gid);
SNPRINTF(audit_buf, "changing new-gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 1);
#endif
@@ -838,8 +832,7 @@ int main (int argc, char **argv)
closelog ();
#ifdef WITH_AUDIT
if (NULL != group) {
snprintf (audit_buf, sizeof(audit_buf),
"changing new-group=%s", group);
SNPRINTF(audit_buf, "changing new-group=%s", group);
audit_logger (AUDIT_CHGRP_ID, Prog,
audit_buf, NULL, getuid (), 0);
} else {
+5 -2
View File
@@ -50,6 +50,8 @@
#endif /* ENABLE_SUBIDS */
#include "chkname.h"
#include "shadowlog.h"
#include "string/sprintf.h"
/*
* Global variables
@@ -709,8 +711,9 @@ static void process_flags (int argc, char **argv)
if (argv[optind] != NULL) {
if (freopen (argv[optind], "r", stdin) == NULL) {
char buf[BUFSIZ];
snprintf (buf, sizeof buf, "%s: %s", Prog, argv[1]);
char buf[BUFSIZ];
SNPRINTF(buf, "%s: %s", Prog, argv[1]);
perror (buf);
fail_exit (EXIT_FAILURE);
}
+4 -2
View File
@@ -58,8 +58,10 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/strtcpy.h"
/*
* Global variables
*/
@@ -387,8 +389,8 @@ static void prepare_pam_close_session (void)
stderr);
(void) kill (-pid_child, caught);
snprintf (kill_msg, sizeof kill_msg, _(" ...killed.\n"));
snprintf (wait_msg, sizeof wait_msg, _(" ...waiting for child to terminate.\n"));
SNPRINTF(kill_msg, _(" ...killed.\n"));
SNPRINTF(wait_msg, _(" ...waiting for child to terminate.\n"));
/* Any signals other than SIGCHLD and SIGALRM will no longer have any effect,
* so it's time to block all of them. */
+1 -3
View File
@@ -525,7 +525,6 @@ static void show_defaults (void)
static int set_defaults (void)
{
int ofd;
int wlen;
int ret = -1;
bool out_group = false;
bool out_groups = false;
@@ -711,8 +710,7 @@ static int set_defaults (void)
/*
* Rename the current default file to its backup name.
*/
wlen = snprintf (buf, sizeof buf, "%s-", default_file);
assert (wlen < (int) sizeof buf);
assert(SNPRINTF(buf, "%s-", default_file) != -1);
unlink (buf);
if ((link (default_file, buf) != 0) && (ENOENT != errno)) {
int err = errno;
+5 -5
View File
@@ -205,7 +205,7 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
/* FIXME: the following should have variable sizes */
char filebackup[1024], fileedit[1024];
snprintf (filebackup, sizeof filebackup, "%s-", file);
SNPRINTF(filebackup, "%s-", file);
#ifdef WITH_TCB
if (tcb_mode) {
if ( (mkdir (TCB_DIR "/" SHADOWTCB_SCRATCHDIR, 0700) != 0)
@@ -215,12 +215,12 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) {
vipwexit (_("failed to drop privileges"), errno, 1);
}
snprintf (fileedit, sizeof fileedit,
TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s",
user);
SNPRINTF(fileedit,
TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s",
user);
} else {
#endif /* WITH_TCB */
snprintf (fileedit, sizeof fileedit, "%s.edit", file);
SNPRINTF(fileedit, "%s.edit", file);
#ifdef WITH_TCB
}
#endif /* WITH_TCB */