xref: /freebsd/libexec/ulog-helper/ulog-helper.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
12208eadfSEd Schouten /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3e6209940SPedro F. Giffuni  *
42208eadfSEd Schouten  * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
52208eadfSEd Schouten  * All rights reserved.
62208eadfSEd Schouten  *
72208eadfSEd Schouten  * Redistribution and use in source and binary forms, with or without
82208eadfSEd Schouten  * modification, are permitted provided that the following conditions
92208eadfSEd Schouten  * are met:
102208eadfSEd Schouten  * 1. Redistributions of source code must retain the above copyright
112208eadfSEd Schouten  *    notice, this list of conditions and the following disclaimer.
122208eadfSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
132208eadfSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
142208eadfSEd Schouten  *    documentation and/or other materials provided with the distribution.
152208eadfSEd Schouten  *
162208eadfSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172208eadfSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182208eadfSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192208eadfSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202208eadfSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212208eadfSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222208eadfSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232208eadfSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242208eadfSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252208eadfSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262208eadfSEd Schouten  * SUCH DAMAGE.
272208eadfSEd Schouten  */
282208eadfSEd Schouten 
292208eadfSEd Schouten #include <sys/cdefs.h>
302208eadfSEd Schouten #include <pwd.h>
312208eadfSEd Schouten #include <unistd.h>
322208eadfSEd Schouten #include <stdlib.h>
332208eadfSEd Schouten #include <string.h>
342208eadfSEd Schouten #include <sysexits.h>
352208eadfSEd Schouten #include <ulog.h>
362208eadfSEd Schouten 
372208eadfSEd Schouten /*
382208eadfSEd Schouten  * This setuid helper utility writes user login records to disk.
390ea9a5ddSEd Schouten  * Unprivileged processes are not capable of writing records to utmpx,
400ea9a5ddSEd Schouten  * but we do want to allow this for pseudo-terminals.  Because a file
410ea9a5ddSEd Schouten  * descriptor to a pseudo-terminal master device can only be obtained by
420ea9a5ddSEd Schouten  * processes using the pseudo-terminal, we expect such a descriptor on
430ea9a5ddSEd Schouten  * stdin.
442208eadfSEd Schouten  *
452208eadfSEd Schouten  * It uses the real user ID of the calling process to determine the
462208eadfSEd Schouten  * username.  It does allow users to log arbitrary hostnames.
472208eadfSEd Schouten  */
482208eadfSEd Schouten 
491d2276c8SEd Schouten static const char *
get_username(void)501d2276c8SEd Schouten get_username(void)
511d2276c8SEd Schouten {
521d2276c8SEd Schouten 	const struct passwd *pw;
531d2276c8SEd Schouten 	const char *login;
541d2276c8SEd Schouten 	uid_t uid;
551d2276c8SEd Schouten 
561d2276c8SEd Schouten 	/*
571d2276c8SEd Schouten 	 * Attempt to determine the username corresponding to this login
581d2276c8SEd Schouten 	 * session.  First, validate the results of getlogin() against
591d2276c8SEd Schouten 	 * the password database.  If getlogin() returns invalid data,
601d2276c8SEd Schouten 	 * return an arbitrary username corresponding to this uid.
611d2276c8SEd Schouten 	 */
621d2276c8SEd Schouten 	uid = getuid();
631d2276c8SEd Schouten 	if ((login = getlogin()) != NULL && (pw = getpwnam(login)) != NULL &&
641d2276c8SEd Schouten 	    pw->pw_uid == uid)
651d2276c8SEd Schouten 		return (login);
661d2276c8SEd Schouten 	if ((pw = getpwuid(uid)) != NULL)
671d2276c8SEd Schouten 		return (pw->pw_name);
681d2276c8SEd Schouten 	return (NULL);
691d2276c8SEd Schouten }
701d2276c8SEd Schouten 
712208eadfSEd Schouten int
main(int argc,char * argv[])722208eadfSEd Schouten main(int argc, char *argv[])
732208eadfSEd Schouten {
740ea9a5ddSEd Schouten 	const char *line, *user, *host;
752208eadfSEd Schouten 
762208eadfSEd Schouten 	/* Device line name. */
772208eadfSEd Schouten 	if ((line = ptsname(STDIN_FILENO)) == NULL)
782208eadfSEd Schouten 		return (EX_USAGE);
792208eadfSEd Schouten 
802208eadfSEd Schouten 	if ((argc == 2 || argc == 3) && strcmp(argv[1], "login") == 0) {
812208eadfSEd Schouten 		/* Username. */
821d2276c8SEd Schouten 		user = get_username();
830ea9a5ddSEd Schouten 		if (user == NULL)
842208eadfSEd Schouten 			return (EX_OSERR);
852208eadfSEd Schouten 
862208eadfSEd Schouten 		/* Hostname. */
870ea9a5ddSEd Schouten 		host = argc == 3 ? argv[2] : NULL;
882208eadfSEd Schouten 
890ea9a5ddSEd Schouten 		ulog_login(line, user, host);
902208eadfSEd Schouten 		return (EX_OK);
912208eadfSEd Schouten 	} else if (argc == 2 && strcmp(argv[1], "logout") == 0) {
929e9a895eSEd Schouten 		ulog_logout(line);
932208eadfSEd Schouten 		return (EX_OK);
942208eadfSEd Schouten 	}
952208eadfSEd Schouten 
962208eadfSEd Schouten 	return (EX_USAGE);
972208eadfSEd Schouten }
98