From owner-svn-src-user@FreeBSD.ORG Tue Jan 12 07:34:24 2010 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 040EE106566C; Tue, 12 Jan 2010 07:34:24 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id E79558FC15; Tue, 12 Jan 2010 07:34:23 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o0C7YNb0016219; Tue, 12 Jan 2010 07:34:23 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o0C7YN5Q016216; Tue, 12 Jan 2010 07:34:23 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201001120734.o0C7YN5Q016216@svn.freebsd.org> From: Ed Schouten Date: Tue, 12 Jan 2010 07:34:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r202141 - user/ed/utmpx/lib/libulog X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2010 07:34:24 -0000 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 __FBSDID("$FreeBSD$"); +#include #include #include +#include #include #include #include #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); }