xref: /freebsd/libexec/ulog-helper/ulog-helper.c (revision 0ea9a5ddd46a7a77a4a5c74170d8a5969f721212)
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.
39*0ea9a5ddSEd Schouten  * Unprivileged processes are not capable of writing records to utmpx,
40*0ea9a5ddSEd Schouten  * but we do want to allow this for pseudo-terminals.  Because a file
41*0ea9a5ddSEd Schouten  * descriptor to a pseudo-terminal master device can only be obtained by
42*0ea9a5ddSEd Schouten  * processes using the pseudo-terminal, we expect such a descriptor on
43*0ea9a5ddSEd 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 
492208eadfSEd Schouten int
502208eadfSEd Schouten main(int argc, char *argv[])
512208eadfSEd Schouten {
52*0ea9a5ddSEd Schouten 	const char *line, *user, *host;
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 		/* Username. */
60*0ea9a5ddSEd Schouten 		user = user_from_uid(getuid(), 1);
61*0ea9a5ddSEd Schouten 		if (user == NULL)
622208eadfSEd Schouten 			return (EX_OSERR);
632208eadfSEd Schouten 
642208eadfSEd Schouten 		/* Hostname. */
65*0ea9a5ddSEd Schouten 		host = argc == 3 ? argv[2] : NULL;
662208eadfSEd Schouten 
67*0ea9a5ddSEd Schouten 		ulog_login(line, user, host);
682208eadfSEd Schouten 		return (EX_OK);
692208eadfSEd Schouten 	} else if (argc == 2 && strcmp(argv[1], "logout") == 0) {
709e9a895eSEd Schouten 		ulog_logout(line);
712208eadfSEd Schouten 		return (EX_OK);
722208eadfSEd Schouten 	}
732208eadfSEd Schouten 
742208eadfSEd Schouten 	return (EX_USAGE);
752208eadfSEd Schouten }
76