* src/usermod.c: Do not assign static to NULL.

* src/usermod.c (date_to_str): buf needs to be unique (e.g.
	independent from negativ), and is an out buffer.
	* src/usermod.c: Ignore return value from snprintf, and force
	nul-termination of buffer.
	* src/usermod.c: Improve memory management.
	* src/usermod.c: An audit bloc was not reachable, moved above on
	success to move the home directory.
	* src/usermod.c: Ignore close() return value for the mailbox
	(opened read only).
This commit is contained in:
nekral-guest 2011-08-15 09:56:43 +00:00
parent 5eb9ed0aaf
commit 7c96d6cbcc
2 changed files with 41 additions and 19 deletions

View File

@ -1,3 +1,16 @@
2011-08-15 Nicolas François <nicolas.francois@centraliens.net>
* src/usermod.c: Do not assign static to NULL.
* src/usermod.c (date_to_str): buf needs to be unique (e.g.
independent from negativ), and is an out buffer.
* src/usermod.c: Ignore return value from snprintf, and force
nul-termination of buffer.
* src/usermod.c: Improve memory management.
* src/usermod.c: An audit bloc was not reachable, moved above on
success to move the home directory.
* src/usermod.c: Ignore close() return value for the mailbox
(opened read only).
2011-08-15 Nicolas François <nicolas.francois@centraliens.net> 2011-08-15 Nicolas François <nicolas.francois@centraliens.net>
* src/su.c: Added const modifiers. * src/su.c: Added const modifiers.

View File

@ -92,21 +92,21 @@
const char *Prog; const char *Prog;
static char *user_name; static char *user_name;
static char *user_newname = NULL; static char *user_newname;
static char *user_pass; static char *user_pass;
static uid_t user_id; static uid_t user_id;
static uid_t user_newid; static uid_t user_newid;
static gid_t user_gid; static gid_t user_gid;
static gid_t user_newgid; static gid_t user_newgid;
static char *user_comment; static char *user_comment;
static char *user_newcomment = NULL; static char *user_newcomment;
static char *user_home; static char *user_home;
static char *user_newhome = NULL; static char *user_newhome;
static char *user_shell; static char *user_shell;
#ifdef WITH_SELINUX #ifdef WITH_SELINUX
static const char *user_selinux = ""; static const char *user_selinux = "";
#endif #endif
static char *user_newshell = NULL; static char *user_newshell;
static long user_expire; static long user_expire;
static long user_newexpire; static long user_newexpire;
static long user_inactive; static long user_inactive;
@ -149,7 +149,7 @@ static bool sgr_locked = false;
/* local function prototypes */ /* local function prototypes */
static void date_to_str (char *buf, size_t maxsize, static void date_to_str (/*@unique@*//*@out@*/char *buf, size_t maxsize,
long int date, const char *negativ); long int date, const char *negativ);
static int get_groups (char *); static int get_groups (char *);
static /*@noreturn@*/void usage (int status); static /*@noreturn@*/void usage (int status);
@ -179,7 +179,7 @@ static void update_faillog (void);
static void move_mailbox (void); static void move_mailbox (void);
#endif #endif
static void date_to_str (char *buf, size_t maxsize, static void date_to_str (/*@unique@*//*@out@*/char *buf, size_t maxsize,
long int date, const char *negativ) long int date, const char *negativ)
{ {
struct tm *tp; struct tm *tp;
@ -192,8 +192,10 @@ static void date_to_str (char *buf, size_t maxsize,
#ifdef HAVE_STRFTIME #ifdef HAVE_STRFTIME
strftime (buf, maxsize, "%Y-%m-%d", tp); strftime (buf, maxsize, "%Y-%m-%d", tp);
#else #else
snprintf (buf, maxsize, "%04d-%02d-%02d", (void) snprintf (buf, maxsize, "%04d-%02d-%02d",
tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday); tp->tm_year + 1900,
tp->tm_mon + 1,
tp->tm_mday);
#endif /* HAVE_STRFTIME */ #endif /* HAVE_STRFTIME */
} }
buf[maxsize - 1] = '\0'; buf[maxsize - 1] = '\0';
@ -271,6 +273,7 @@ static int get_groups (char *list)
fprintf (stderr, fprintf (stderr,
_("%s: group '%s' is a NIS group.\n"), _("%s: group '%s' is a NIS group.\n"),
Prog, grp->gr_name); Prog, grp->gr_name);
gr_free (grp);
continue; continue;
} }
#endif #endif
@ -279,6 +282,7 @@ static int get_groups (char *list)
fprintf (stderr, fprintf (stderr,
_("%s: too many groups specified (max %d).\n"), _("%s: too many groups specified (max %d).\n"),
Prog, ngroups); Prog, ngroups);
gr_free (grp);
break; break;
} }
@ -286,6 +290,7 @@ static int get_groups (char *list)
* Add the group name to the user's list of groups. * Add the group name to the user's list of groups.
*/ */
user_groups[ngroups++] = xstrdup (grp->gr_name); user_groups[ngroups++] = xstrdup (grp->gr_name);
gr_free (grp);
} while (NULL != list); } while (NULL != list);
user_groups[ngroups] = (char *) 0; user_groups[ngroups] = (char *) 0;
@ -1521,6 +1526,12 @@ static void move_home (void)
Prog); Prog);
fail_exit (E_HOMEDIR); fail_exit (E_HOMEDIR);
} }
#ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
"moving home directory",
user_newname, (unsigned int) user_newid,
1);
#endif
return; return;
} else { } else {
if (EXDEV == errno) { if (EXDEV == errno) {
@ -1553,11 +1564,6 @@ static void move_home (void)
Prog, user_home, user_newhome); Prog, user_home, user_newhome);
fail_exit (E_HOMEDIR); fail_exit (E_HOMEDIR);
} }
#ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
"moving home directory",
user_newname, (unsigned int) user_newid, 1);
#endif
} }
} }
@ -1713,7 +1719,9 @@ static void move_mailbox (void)
* replacing /var/spool/mail/luser with a hard link to /etc/passwd * replacing /var/spool/mail/luser with a hard link to /etc/passwd
* between stat and chown). --marekm * between stat and chown). --marekm
*/ */
snprintf (mailfile, sizeof mailfile, "%s/%s", maildir, user_name); (void) snprintf (mailfile, sizeof mailfile, "%s/%s",
maildir, user_name);
mailfile[(sizeof mailfile) - 1] = '\0';
fd = open (mailfile, O_RDONLY | O_NONBLOCK, 0); fd = open (mailfile, O_RDONLY | O_NONBLOCK, 0);
if (fd < 0) { if (fd < 0) {
/* no need for warnings if the mailbox doesn't exist */ /* no need for warnings if the mailbox doesn't exist */
@ -1724,14 +1732,14 @@ static void move_mailbox (void)
} }
if (fstat (fd, &st) < 0) { if (fstat (fd, &st) < 0) {
perror ("fstat"); perror ("fstat");
close (fd); (void) close (fd);
return; return;
} }
if (st.st_uid != user_id) { if (st.st_uid != user_id) {
/* better leave it alone */ /* better leave it alone */
fprintf (stderr, _("%s: warning: %s not owned by %s\n"), fprintf (stderr, _("%s: warning: %s not owned by %s\n"),
Prog, mailfile, user_name); Prog, mailfile, user_name);
close (fd); (void) close (fd);
return; return;
} }
if (uflg) { if (uflg) {
@ -1747,11 +1755,12 @@ static void move_mailbox (void)
#endif #endif
} }
close (fd); (void) close (fd);
if (lflg) { if (lflg) {
snprintf (newmailfile, sizeof newmailfile, "%s/%s", (void) snprintf (newmailfile, sizeof newmailfile, "%s/%s",
maildir, user_newname); maildir, user_newname);
newmailfile[(sizeof newmailfile) - 1] = '\0';
if ( (link (mailfile, newmailfile) != 0) if ( (link (mailfile, newmailfile) != 0)
|| (unlink (mailfile) != 0)) { || (unlink (mailfile) != 0)) {
perror (_("failed to rename mailbox")); perror (_("failed to rename mailbox"));