From owner-freebsd-ports@FreeBSD.ORG Sun Jan 9 22:20:31 2011 Return-Path: Delivered-To: ports@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9257B1065670 for ; Sun, 9 Jan 2011 22:20:31 +0000 (UTC) (envelope-from rea@codelabs.ru) Received: from 0.mx.codelabs.ru (0.mx.codelabs.ru [144.206.177.45]) by mx1.freebsd.org (Postfix) with ESMTP id 2A0078FC1A for ; Sun, 9 Jan 2011 22:20:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codelabs.ru; s=two; h=Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=zKPGQsTgeBiMin1TXJu+J3bs1u4XTyXdzMZVJdMj0hc=; b=tq+bV7fgtYz6GC5+E1kG5lb8ztn3oYlSt+xJP00WujIo7pcJlxqz1PmpFcl1t1bbVBcSxS4CVDcEeOvEiwqGrJgrda2hVT/tNrE+QUCBjGdKJndFj2Tyb5keyDGYK1Rd8exoxviv9SfYOyultTcw+eB1k8/OsBKv2hmTPyTvRorH7OjN9/AOIpesPyv2ABFYWiEMgiUy4WJ2Ntwqfot1eJfjm9HlaaFmGgXTQq2C0BDi6OSpy2ZgoQWNeT8ycgn0wKp6tIF6yrTv6rLUe3jH1YLfWd9TryDCPfs5aTEeQrWwKutnIfV04+iHnyWeilTFhJ/7avhafkDPyHUnJrhgwQ==; Received: from amnesiac.at.no.dns (ppp91-78-248-40.pppoe.mtu-net.ru [91.78.248.40]) by 0.mx.codelabs.ru with esmtpsa (TLSv1:AES256-SHA:256) id 1Pc3cj-000PGU-6m; Mon, 10 Jan 2011 01:20:29 +0300 Date: Mon, 10 Jan 2011 01:20:24 +0300 From: Eygene Ryabinkin To: Ben Kibbey Message-ID: References: <201101091555.p09Ft22H023380@rs49.luxsci.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="/e2eDi0V/xtL+Mc8" Content-Disposition: inline In-Reply-To: <201101091555.p09Ft22H023380@rs49.luxsci.com> Sender: rea@codelabs.ru Cc: ports@FreeBSD.org Subject: Re: FreeBSD Port: sysutils/userinfo X-BeenThere: freebsd-ports@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting software to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 09 Jan 2011 22:20:31 -0000 --/e2eDi0V/xtL+Mc8 Content-Type: multipart/mixed; boundary="neYutvxvOLaeuPCA" Content-Disposition: inline --neYutvxvOLaeuPCA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Ben, good day. Sun, Jan 09, 2011 at 10:54:28AM -0500, Ben Kibbey wrote: > I noticed a build failure for the userinfo utility not too long ago. > There is no longer a ports maintainer for this one so I thought I'd try > to get it fixed here. Could someone see if the following patch fixes it > as I don't have a FreeBSD box. I believe that the attached patch will fix the utility. The need of login.{h,c} hunks is obvious, the need for the configure one should be explained, I think. At my machine when I had compiled the code the number of the active sessions was 0; so getutxent() will return NULL. I think that the test should really be using UTXDB_LOG that has all entries for utmpx. And that check was implemented. Two more nits: - I wasn't able to invoke 'ui -O login.so -la -- `users`', it were giving me the main help until I had killed the chaining test just after option parsing; - at least login.c uses unsafe constructions like {{{ for (i =3D 0; i < cnt; i++) { ... strncat(line, buf, sizeof(line)); ... } }}} It won't protect you from overrunning 'line', because the last argument of that strncat specifies how many bytes from _buf_ we want to copy at most. You will likely want to read the "Security considerations" section of http://www.freebsd.org/cgi/man.cgi?query=3Dstrncat&apropos=3D0&sektion= =3D0&manpath=3DFreeBSD+8.1-RELEASE&format=3Dhtml or to use strlcpy/strlcat (but they aren't the standard ones on all platforms). --=20 Eygene Ryabinkin ,,,^..^,,, [ Life's unfair - but root password helps! | codelabs.ru ] [ 82FE 06BC D497 C0DE 49EC 4FF0 16AF 9EAE 8152 ECFB | freebsd.org ] --neYutvxvOLaeuPCA Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=patch-utmpx Content-Transfer-Encoding: quoted-printable --- src/modules/login.c.orig 2011-01-09 22:06:08.813688876 +0300 +++ src/modules/login.c 2011-01-10 00:27:50.225685582 +0300 @@ -24,7 +24,6 @@ #include #include #include -#include =20 #ifdef HAVE_CONFIG_H #include @@ -32,6 +31,11 @@ =20 #include "login.h" =20 +#ifndef FREEBSD_UTMPX +/* FreeBSD with utmpx support has utmp.h that will #error us. */ +#include +#endif + #ifndef HAVE_STRSEP #include "../strsep.c" #endif @@ -338,6 +342,40 @@ return; } =20 +#ifdef FREEBSD_UTMPX +/* + * Get the last login data for the given user + * via utmpx(3) interface. + *. + * Has a side effect of setting the current database + * to the LASTLOGIN one. + */ +static char *lastloginx(const char *name, char *tf) +{ + static char buf[LINE_MAX]; + struct utmpx *last =3D NULL; + + if (setutxdb(UTXDB_LASTLOGIN, NULL)) { + warn("setutxdb(UTXDB_LASTLOGIN)"); + return NULL; + } + + while ((last =3D getutxent()) !=3D NULL) { + if (last->ut_type =3D=3D USER_PROCESS && + strcmp(last->ut_user, name) =3D=3D 0) + break; + } + if (last =3D=3D NULL) + return NULL; + + snprintf(buf, sizeof(buf), "%s,%s,%s", + (last->ut_line[0] =3D=3D '\0' ? "!" : last->ut_line), + (last->ut_host[0] =3D=3D '\0' ? "-" : last->ut_host), + stamp(last->ut_tv.tv_sec, tf)); + + return buf; +} +#else /* defined(FREEBSD_UTMPX) */ /* Get the lastlog structure from the lastlog file. */ static char *lastlogin(uid_t uid, char *tf) { @@ -423,6 +461,7 @@ =20 return buf; } +#endif /* defined(FREEBSD_UTMPX) */ =20 /* This will return an array of utmp structures if a user is logged in, NU= LL * otherwise. We'll try to keep the utmp file descriptor open if possible = to @@ -452,10 +491,18 @@ login_count =3D 0; =20 #ifdef UTMPX_FORMAT +#ifdef FREEBSD_UTMPX + setutxent(); +#else setutent(); +#endif =20 while ((u =3D getutxent()) !=3D NULL) { +#ifdef FREEBSD_UTMPX + if (strcmp(u->ut_user, user) =3D=3D 0) { +#else if (strcmp(u->ut_name, user) =3D=3D 0) { +#endif #else lseek(fd, 0, SEEK_SET); =20 @@ -481,7 +528,11 @@ strncpy(logins[login_count]->ut_host, u->ut_host, UTX_HOSTSIZE); logins[login_count]->ut_pid =3D u->ut_pid; #else +#ifdef FREEBSD_UTMPX + strncpy(logins[login_count]->ut_user, u->ut_user, UT_NAMESIZE); +#else strncpy(logins[login_count]->ut_name, u->ut_name, UT_NAMESIZE); +#endif strncpy(logins[login_count]->ut_line, u->ut_line, UT_LINESIZE); strncpy(logins[login_count]->ut_host, u->ut_host, UT_HOSTSIZE); logins[login_count]->ut_tv.tv_sec =3D u->ut_tv.tv_sec; @@ -639,7 +690,13 @@ add_string(&strings, (u) ? idle(u, multi) : "!"); break; case 'l': - last_strings(lastlogin(pw->pw_uid, tf)); + last_strings( +#ifdef FREEBSD_UTMPX + lastloginx(pw->pw_name,tf) +#else + lastlogin(pw->pw_uid, tf) +#endif + ); break; case 'h': for (i =3D 0; i < login_count; i++) { --- src/modules/login.h.orig 2011-01-09 23:40:11.136688587 +0300 +++ src/modules/login.h 2011-01-09 23:41:18.568686438 +0300 @@ -51,6 +51,9 @@ #endif =20 #ifdef UTMPX_FORMAT +#ifdef __FreeBSD__ +#define FREEBSD_UTMPX +#endif #include #ifndef UT_HOSTSIZE #define UT_HOSTSIZE 256 --- configure.orig 2011-01-10 00:34:34.768686578 +0300 +++ configure 2011-01-10 00:35:46.413686990 +0300 @@ -20786,12 +20786,15 @@ cat >conftest.$ac_ext <<_ACEOF \ #include + #include #include =20 int main() { struct utmpx *u; =20 + if (setutxdb(UTXDB_LASTLOGIN, NULL)) + exit(1); if ((u =3D getutxent()) =3D=3D NULL) exit(1); =20 --neYutvxvOLaeuPCA-- --/e2eDi0V/xtL+Mc8 Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (FreeBSD) iF4EAREIAAYFAk0qNKgACgkQFq+eroFS7Ps04QD+KSLbM+t2IISH8FrDL2Mf+z5F fX7fAi/wwzXvF+KXuaYA/2WtwqzmKAO26eO1CIn8sNfz9OkyIdMJvHd4JIs3KTrv =XQs3 -----END PGP SIGNATURE----- --/e2eDi0V/xtL+Mc8--