From owner-svn-src-all@FreeBSD.ORG Thu Apr 19 21:12:09 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 267BC106566B; Thu, 19 Apr 2012 21:12:09 +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 111208FC0C; Thu, 19 Apr 2012 21:12:09 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q3JLC8QI007496; Thu, 19 Apr 2012 21:12:08 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q3JLC8RU007494; Thu, 19 Apr 2012 21:12:08 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201204192112.q3JLC8RU007494@svn.freebsd.org> From: Ed Schouten Date: Thu, 19 Apr 2012 21:12:08 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r234469 - head/libexec/ulog-helper X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Apr 2012 21:12:09 -0000 Author: ed Date: Thu Apr 19 21:12:08 2012 New Revision: 234469 URL: http://svn.freebsd.org/changeset/base/234469 Log: Do a better job at determining the username of the login session. When multiple users share the same UID, the old code will simply pick an arbitrary username to attach to the utmpx entries. Make the code a bit more accurate by first checking whether getlogin() returns a username which corresponds to the uid of the calling process. If this fails, simply fall back to picking an arbitrary username. Reported by: saurik on GitHub MFC after: 2 weeks Modified: head/libexec/ulog-helper/ulog-helper.c Modified: head/libexec/ulog-helper/ulog-helper.c ============================================================================== --- head/libexec/ulog-helper/ulog-helper.c Thu Apr 19 20:44:40 2012 (r234468) +++ head/libexec/ulog-helper/ulog-helper.c Thu Apr 19 21:12:08 2012 (r234469) @@ -46,6 +46,28 @@ __FBSDID("$FreeBSD$"); * username. It does allow users to log arbitrary hostnames. */ +static const char * +get_username(void) +{ + const struct passwd *pw; + const char *login; + uid_t uid; + + /* + * Attempt to determine the username corresponding to this login + * session. First, validate the results of getlogin() against + * the password database. If getlogin() returns invalid data, + * return an arbitrary username corresponding to this uid. + */ + uid = getuid(); + if ((login = getlogin()) != NULL && (pw = getpwnam(login)) != NULL && + pw->pw_uid == uid) + return (login); + if ((pw = getpwuid(uid)) != NULL) + return (pw->pw_name); + return (NULL); +} + int main(int argc, char *argv[]) { @@ -57,7 +79,7 @@ main(int argc, char *argv[]) if ((argc == 2 || argc == 3) && strcmp(argv[1], "login") == 0) { /* Username. */ - user = user_from_uid(getuid(), 1); + user = get_username(); if (user == NULL) return (EX_OSERR);