]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: cleanup code to copy to log strings
authorKarel Zak <kzak@redhat.com>
Wed, 3 Oct 2018 15:08:11 +0000 (17:08 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 3 Oct 2018 15:08:11 +0000 (17:08 +0200)
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>
login-utils/login.c

index 9aef3d804a3176bea031e55b1a217e5d6e7ba226..23b62b8d0417cc1d1e6e5c8a8e7f3211194ea443 100644 (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));