[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    A simple patch to support /var/log/btmp file
From:       Emilio Riva <aviroilime () gmail ! com>
Date:       2020-05-11 6:57:34
Message-ID: CAO2iarHNdVJQN9KdkD0sj74uAUv=Bf9Sk_sSzQ9pcjK2qhRQbQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi all,

if it's of any interest for someone else than myself (!), here in attach is
a simple patch to support /var/log/btmp handling on login applet.
As usual, /var/log/btmp must be created at system init time to update it,
with 0600 permission and owned by root.

BR,
Milo

[Attachment #5 (text/html)]

<div dir="ltr"><div>Hi all,</div><div><br></div><div>if it&#39;s of any interest for \
someone else than myself (!), here in attach is a simple patch to support \
/var/log/btmp handling on login applet.</div><div></div><div>As usual, /var/log/btmp \
must be created at system init time to update it, with 0600 permission and owned by \
root.</div><div><br></div><div>BR,</div><div>Milo<br></div></div>


["busybox-1.31.1-btmpsimple.patch" (application/octet-stream)]

diff -NabBur busybox-1.31.1/include/libbb.h busybox-1.31.1_btmp/include/libbb.h
--- busybox-1.31.1/include/libbb.h	2019-06-10 12:50:53.000000000 +0200
+++ busybox-1.31.1_btmp/include/libbb.h	2020-02-14 10:33:02.000000000 +0100
@@ -112,6 +112,12 @@
 #   define _PATH_UTMPX _PATH_UTMP
 #  endif
 # endif
+# ifndef _PATH_BTMP		/* glibc should define this in paths.h */
+#  define _PATH_BTMP		"/var/log/btmp"
+# endif
+# if defined _PATH_BTMP && !defined _PATH_BTMPX
+#  define _PATH_BTMPX _PATH_BTMP
+# endif
 #endif
 #if ENABLE_LOCALE_SUPPORT
 # include <locale.h>
@@ -1102,10 +1108,12 @@
 void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const \
char *username, const char *hostname);  void FAST_FUNC update_utmp(pid_t pid, int \
new_type, const char *tty_name, const char *username, const char *hostname);  void \
FAST_FUNC update_utmp_DEAD_PROCESS(pid_t pid); +void FAST_FUNC write_new_btmp(pid_t \
pid, const char *tty_name, const char *username, const char *hostname);  #else
 # define write_new_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
 # define update_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
 # define update_utmp_DEAD_PROCESS(pid) ((void)0)
+# define write_new_btmp(pid, tty_name, username, hostname) ((void)0)
 #endif
 
 
diff -NabBur busybox-1.31.1/libbb/utmp.c busybox-1.31.1_btmp/libbb/utmp.c
--- busybox-1.31.1/libbb/utmp.c	2019-06-10 12:50:53.000000000 +0200
+++ busybox-1.31.1_btmp/libbb/utmp.c	2020-02-17 11:19:21.193245013 +0100
@@ -7,16 +7,19 @@
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 #include "libbb.h"
+#include <syslog.h>
 
-static void touch(const char *filename)
+static void touch(const char *filename, mode_t fmode)
 {
 	if (access(filename, R_OK | W_OK) == -1)
-		close(open(filename, O_WRONLY | O_CREAT, 0664));
+		close(open(filename, O_WRONLY | O_CREAT, fmode));
 }
 
 void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const \
char *username, const char *hostname)  {
 	struct utmpx utent;
+	struct timeval tv;
+
 	char *id;
 	unsigned width;
 
@@ -29,7 +32,10 @@
 		safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user));
 	if (hostname)
 		safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host));
-	utent.ut_tv.tv_sec = time(NULL);
+
+	gettimeofday(&tv, NULL);
+	utent.ut_tv.tv_sec = tv.tv_sec;
+	utent.ut_tv.tv_usec = tv.tv_usec;
 
 	/* Invent our own ut_id. ut_id is only 4 chars wide.
 	 * Try to fit something remotely meaningful... */
@@ -45,7 +51,10 @@
 		tty_name += 3;
 	strncpy(id, tty_name, width);
 
-	touch(_PATH_UTMPX);
+	if (new_type != LOGIN_PROCESS)
+		syslog(LOG_INFO, "write_new_utmp: session opened for user %s", utent.ut_user);
+
+	touch(_PATH_UTMPX, 0664);
 	//utmpxname(_PATH_UTMPX);
 	setutxent();
 	/* Append new one (hopefully, unless we collide on ut_id) */
@@ -54,20 +63,63 @@
 
 #if ENABLE_FEATURE_WTMP
 	/* "man utmp" says wtmp file should *not* be created automagically */
-	/*touch(bb_path_wtmp_file);*/
+	/*touch(bb_path_wtmp_file, 0644);*/
 	updwtmpx(bb_path_wtmp_file, &utent);
 #endif
 }
 
+void FAST_FUNC write_new_btmp(pid_t pid, const char *tty_name, const char *username, \
const char *hostname) +{
+	struct utmpx utent;
+	struct timeval tv;
+
+	char *id;
+	unsigned width;
+
+	memset(&utent, 0, sizeof(utent));
+
+	utent.ut_pid = pid;
+	utent.ut_type = LOGIN_PROCESS;	/* XXX doesn't matter */
+	tty_name = skip_dev_pfx(tty_name);
+	safe_strncpy(utent.ut_line, tty_name, sizeof(utent.ut_line));
+	if (username)
+		safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user));
+	if (hostname)
+		safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host));
+
+	gettimeofday(&tv, NULL);
+	utent.ut_tv.tv_sec = tv.tv_sec;
+	utent.ut_tv.tv_usec = tv.tv_usec;
+
+	/* Invent our own ut_id. ut_id is only 4 chars wide.
+	 * Try to fit something remotely meaningful... */
+	id = utent.ut_id;
+	width = sizeof(utent.ut_id);
+	if (tty_name[0] == 'p') {
+		/* if "ptyXXX", map to "pXXX" */
+		/* if "pts/XX", map to "p/XX" */
+		*id++ = 'p';
+		width--;
+	} /* else: usually it's "ttyXXXX", map to "XXXX" */
+	if (strlen(tty_name) > 3)
+		tty_name += 3;
+	strncpy(id, tty_name, width);
+
+	touch(_PATH_BTMPX, 0600);
+	updwtmpx(_PATH_BTMP, &utent);
+}
+
 /*
  * Read "man utmp" to make sense out of it.
  */
 void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char \
*username, const char *hostname)  {
 	struct utmpx utent;
+	struct timeval tv;
+
 	struct utmpx *utp;
 
-	touch(_PATH_UTMPX);
+	touch(_PATH_UTMPX, 0664);
 	//utmpxname(_PATH_UTMPX);
 	setutxent();
 
@@ -117,7 +169,15 @@
 		safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user));
 	if (hostname)
 		safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host));
-	utent.ut_tv.tv_sec = time(NULL);
+
+	gettimeofday(&tv, NULL);
+	utent.ut_tv.tv_sec = tv.tv_sec;
+	utent.ut_tv.tv_usec = tv.tv_usec;
+
+	if (new_type == DEAD_PROCESS)
+		syslog(LOG_INFO, "update_utmp: session closed for user %s", utent.ut_user);
+        else if (new_type != LOGIN_PROCESS)
+		syslog(LOG_INFO, "update_utmp: session opened for user %s", utent.ut_user);
 
 	/* Update, or append new one */
 	//setutxent();
@@ -126,7 +186,7 @@
 
 #if ENABLE_FEATURE_WTMP
 	/* "man utmp" says wtmp file should *not* be created automagically */
-	/*touch(bb_path_wtmp_file);*/
+	/*touch(bb_path_wtmp_file, 0644);*/
 	updwtmpx(bb_path_wtmp_file, &utent);
 #endif
 }
diff -NabBur busybox-1.31.1/loginutils/login.c busybox-1.31.1_btmp/loginutils/login.c
--- busybox-1.31.1/loginutils/login.c	2019-06-10 12:50:53.000000000 +0200
+++ busybox-1.31.1_btmp/loginutils/login.c	2020-02-14 10:56:49.000000000 +0100
@@ -510,6 +510,8 @@
 		if (++count == 3) {
 			syslog(LOG_WARNING, "invalid password for '%s'%s",
 						username, fromhost);
+			if (ENABLE_FEATURE_UTMP)
+				write_new_btmp(getpid(), short_tty, username, run_by_root ? opt_host : NULL);
 
 			if (ENABLE_FEATURE_CLEAN_UP)
 				free(fromhost);



_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic