login: cleanup code to copy to log strings

man utmp:
  String fields are terminated by a null byte ('\0') if they are shorter
  than the size of the field.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2018-10-03 17:08:11 +02:00
parent 561472a40d
commit ac5c12fdc2
1 changed files with 12 additions and 12 deletions

View File

@ -437,14 +437,14 @@ static void log_btmp(struct login_context *cxt)
memset(&ut, 0, sizeof(ut));
strncpy(ut.ut_user,
str2memcpy(ut.ut_user,
cxt->username ? cxt->username : "(unknown)",
sizeof(ut.ut_user));
if (cxt->tty_number)
strncpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
str2memcpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
if (cxt->tty_name)
xstrncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
str2memcpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
gettimeofday(&tv, NULL);
ut.ut_tv.tv_sec = tv.tv_sec;
@ -454,7 +454,7 @@ static void log_btmp(struct login_context *cxt)
ut.ut_pid = cxt->pid;
if (cxt->hostname) {
xstrncpy(ut.ut_host, cxt->hostname, sizeof(ut.ut_host));
str2memcpy(ut.ut_host, cxt->hostname, sizeof(ut.ut_host));
if (*cxt->hostaddress)
memcpy(&ut.ut_addr_v6, cxt->hostaddress,
sizeof(ut.ut_addr_v6));
@ -541,9 +541,9 @@ static void log_lastlog(struct login_context *cxt)
ll.ll_time = t; /* ll_time is always 32bit */
if (cxt->tty_name)
xstrncpy(ll.ll_line, cxt->tty_name, sizeof(ll.ll_line));
str2memcpy(ll.ll_line, cxt->tty_name, sizeof(ll.ll_line));
if (cxt->hostname)
xstrncpy(ll.ll_host, cxt->hostname, sizeof(ll.ll_host));
str2memcpy(ll.ll_host, cxt->hostname, sizeof(ll.ll_host));
if (write_all(fd, (char *)&ll, sizeof(ll)))
warn(_("write lastlog failed"));
@ -585,7 +585,7 @@ static void log_utmp(struct login_context *cxt)
if (utp == NULL && cxt->tty_name) {
setutxent();
ut.ut_type = LOGIN_PROCESS;
strncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
str2memcpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
utp = getutxline(&ut);
}
@ -594,7 +594,7 @@ static void log_utmp(struct login_context *cxt)
if (utp == NULL && cxt->tty_number) {
setutxent();
ut.ut_type = DEAD_PROCESS;
strncpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
str2memcpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
utp = getutxid(&ut);
}
@ -605,11 +605,11 @@ static void log_utmp(struct login_context *cxt)
memset(&ut, 0, sizeof(ut));
if (cxt->tty_number && ut.ut_id[0] == 0)
strncpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
str2memcpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
if (cxt->username)
strncpy(ut.ut_user, cxt->username, sizeof(ut.ut_user));
str2memcpy(ut.ut_user, cxt->username, sizeof(ut.ut_user));
if (cxt->tty_name)
xstrncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
str2memcpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
gettimeofday(&tv, NULL);
ut.ut_tv.tv_sec = tv.tv_sec;
@ -617,7 +617,7 @@ static void log_utmp(struct login_context *cxt)
ut.ut_type = USER_PROCESS;
ut.ut_pid = cxt->pid;
if (cxt->hostname) {
xstrncpy(ut.ut_host, cxt->hostname, sizeof(ut.ut_host));
str2memcpy(ut.ut_host, cxt->hostname, sizeof(ut.ut_host));
if (*cxt->hostaddress)
memcpy(&ut.ut_addr_v6, cxt->hostaddress,
sizeof(ut.ut_addr_v6));