svn commit: r202141 - user/ed/utmpx/lib/libulog

Ed Schouten ed at FreeBSD.org
Tue Jan 12 07:34:24 UTC 2010


Author: ed
Date: Tue Jan 12 07:34:23 2010
New Revision: 202141
URL: http://svn.freebsd.org/changeset/base/202141

Log:
  Since ut_id is binary, just use a hash function to generate ut_id.
  
  I also wanted to include ut_pid into the hash as well, effectively only
  allowing ulog_logout() to be called by the same pid, but in practice
  this doesn't work, because ulog_log{in,out}_pseudo() spawn a process. I
  could use the parent process ID, though...

Modified:
  user/ed/utmpx/lib/libulog/Makefile
  user/ed/utmpx/lib/libulog/ulog_login.c

Modified: user/ed/utmpx/lib/libulog/Makefile
==============================================================================
--- user/ed/utmpx/lib/libulog/Makefile	Tue Jan 12 07:33:33 2010	(r202140)
+++ user/ed/utmpx/lib/libulog/Makefile	Tue Jan 12 07:34:23 2010	(r202141)
@@ -19,6 +19,9 @@ MLINKS+=ulog_login.3 ulog_login_pseudo.3
 	utempter_remove_added_record.3 removeFromUtmp.3 \
 	utempter_remove_record.3 removeLineFromUtmp.3
 
+DPADD=	${LIBMD}
+LDADD=	-lmd
+
 VERSION_DEF= ${.CURDIR}/../libc/Versions.def
 SYMBOL_MAPS= ${.CURDIR}/Symbol.map
 

Modified: user/ed/utmpx/lib/libulog/ulog_login.c
==============================================================================
--- user/ed/utmpx/lib/libulog/ulog_login.c	Tue Jan 12 07:33:33 2010	(r202140)
+++ user/ed/utmpx/lib/libulog/ulog_login.c	Tue Jan 12 07:34:23 2010	(r202141)
@@ -27,28 +27,37 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <sys/param.h>
 #include <sys/time.h>
 #include <paths.h>
+#include <sha.h>
 #include <string.h>
 #include <unistd.h>
 #include <utmpx.h>
 #include "ulog.h"
 
 static void
-ulog_genid(struct utmpx *utx, const char *line)
+ulog_fill(struct utmpx *utx, const char *line)
 {
-	size_t s, d;
+	SHA_CTX c;
+	char id[SHA_DIGEST_LENGTH];
 
-	/*
-	 * Generate an ut_id based on the TTY name.  Use a reverse order
-	 * to prevent the limited space we have to be wasted on prefixes
-	 * like "tty" and "pts/".  It also makes sure aliasing because
-	 * of cropping of "pts/1000" and "pts/1001" is less likely.
-	 * Prepend an 'u' to indicate it was done by libulog.
-	 */
-	utx->ut_id[0] = 'u';
-	for (s = strlen(line), d = 1; s > 0 && d < sizeof utx->ut_id; s--, d++)
-		utx->ut_id[d] = line[s - 1];
+	/* Remove /dev/ component. */
+	if (strncmp(line, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
+		line += sizeof _PATH_DEV - 1;
+
+	memset(utx, 0, sizeof *utx);
+
+	utx->ut_pid = getpid();
+	gettimeofday(&utx->ut_tv, NULL);
+	strncpy(utx->ut_line, line, sizeof utx->ut_line);
+
+	SHA1_Init(&c);
+	SHA1_Update(&c, "libulog", 7);
+	SHA1_Update(&c, utx->ut_line, sizeof utx->ut_line);
+	SHA_Final(id, &c);
+
+	memcpy(utx->ut_id, id, MIN(sizeof utx->ut_id, sizeof id));
 }
 
 void
@@ -56,21 +65,11 @@ ulog_login(const char *line, const char 
 {
 	struct utmpx utx;
 
-	/* Remove /dev/ component. */
-	if (strncmp(line, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
-		line += sizeof _PATH_DEV - 1;
-
-	memset(&utx, 0, sizeof utx);
-
+	ulog_fill(&utx, line);
 	utx.ut_type = USER_PROCESS;
-	utx.ut_pid = getpid();
-	ulog_genid(&utx, line);
-	strncpy(utx.ut_line, line, sizeof utx.ut_line);
 	strncpy(utx.ut_user, user, sizeof utx.ut_user);
 	if (host != NULL)
 		strncpy(utx.ut_host, host, sizeof utx.ut_host);
-	gettimeofday(&utx.ut_tv, NULL);
-
 	pututxline(&utx);
 }
 
@@ -79,16 +78,7 @@ ulog_logout(const char *line)
 {
 	struct utmpx utx;
 
-	/* Remove /dev/ component. */
-	if (strncmp(line, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
-		line += sizeof _PATH_DEV - 1;
-
-	memset(&utx, 0, sizeof utx);
-
+	ulog_fill(&utx, line);
 	utx.ut_type = DEAD_PROCESS;
-	utx.ut_pid = getpid();
-	ulog_genid(&utx, line);
-	gettimeofday(&utx.ut_tv, NULL);
-
 	pututxline(&utx);
 }


More information about the svn-src-user mailing list