xref: /freebsd/libexec/ulog-helper/ulog-helper.c (revision 2208eadf43351a36e70377f3e796daf76a06b8d9)
12208eadfSEd Schouten /*-
22208eadfSEd Schouten  * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
32208eadfSEd Schouten  * All rights reserved.
42208eadfSEd Schouten  *
52208eadfSEd Schouten  * Redistribution and use in source and binary forms, with or without
62208eadfSEd Schouten  * modification, are permitted provided that the following conditions
72208eadfSEd Schouten  * are met:
82208eadfSEd Schouten  * 1. Redistributions of source code must retain the above copyright
92208eadfSEd Schouten  *    notice, this list of conditions and the following disclaimer.
102208eadfSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
112208eadfSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
122208eadfSEd Schouten  *    documentation and/or other materials provided with the distribution.
132208eadfSEd Schouten  *
142208eadfSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152208eadfSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162208eadfSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172208eadfSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182208eadfSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192208eadfSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202208eadfSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212208eadfSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222208eadfSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232208eadfSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242208eadfSEd Schouten  * SUCH DAMAGE.
252208eadfSEd Schouten  */
262208eadfSEd Schouten 
272208eadfSEd Schouten #include <sys/cdefs.h>
282208eadfSEd Schouten __FBSDID("$FreeBSD$");
292208eadfSEd Schouten 
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.
392208eadfSEd Schouten  * Unprivileged processes are not capable of writing records to utmp,
402208eadfSEd Schouten  * wtmp and lastlog, but we do want to allow this for pseudo-terminals.
412208eadfSEd Schouten  * Because a file descriptor to a pseudo-terminal master device can only
422208eadfSEd Schouten  * be obtained by processes using the pseudo-terminal, we expect such a
432208eadfSEd Schouten  * descriptor on 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 
492208eadfSEd Schouten int
502208eadfSEd Schouten main(int argc, char *argv[])
512208eadfSEd Schouten {
522208eadfSEd Schouten 	const char *line;
532208eadfSEd Schouten 
542208eadfSEd Schouten 	/* Device line name. */
552208eadfSEd Schouten 	if ((line = ptsname(STDIN_FILENO)) == NULL)
562208eadfSEd Schouten 		return (EX_USAGE);
572208eadfSEd Schouten 
582208eadfSEd Schouten 	if ((argc == 2 || argc == 3) && strcmp(argv[1], "login") == 0) {
592208eadfSEd Schouten 		struct passwd *pwd;
602208eadfSEd Schouten 		const char *host = NULL;
612208eadfSEd Schouten 
622208eadfSEd Schouten 		/* Username. */
632208eadfSEd Schouten 		pwd = getpwuid(getuid());
642208eadfSEd Schouten 		if (pwd == NULL)
652208eadfSEd Schouten 			return (EX_OSERR);
662208eadfSEd Schouten 
672208eadfSEd Schouten 		/* Hostname. */
682208eadfSEd Schouten 		if (argc == 3)
692208eadfSEd Schouten 			host = argv[2];
702208eadfSEd Schouten 
712208eadfSEd Schouten 		if (ulog_login(line, pwd->pw_name, host) != 0)
722208eadfSEd Schouten 			return (EX_OSFILE);
732208eadfSEd Schouten 		return (EX_OK);
742208eadfSEd Schouten 	} else if (argc == 2 && strcmp(argv[1], "logout") == 0) {
752208eadfSEd Schouten 		if (ulog_logout(line) != 0)
762208eadfSEd Schouten 			return (EX_OSFILE);
772208eadfSEd Schouten 		return (EX_OK);
782208eadfSEd Schouten 	}
792208eadfSEd Schouten 
802208eadfSEd Schouten 	return (EX_USAGE);
812208eadfSEd Schouten }
82