/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2009 Ed Schouten <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/cdefs.h>29#include <pwd.h>30#include <unistd.h>31#include <stdlib.h>32#include <string.h>33#include <sysexits.h>34#include <ulog.h>3536/*37* This setuid helper utility writes user login records to disk.38* Unprivileged processes are not capable of writing records to utmpx,39* but we do want to allow this for pseudo-terminals. Because a file40* descriptor to a pseudo-terminal master device can only be obtained by41* processes using the pseudo-terminal, we expect such a descriptor on42* stdin.43*44* It uses the real user ID of the calling process to determine the45* username. It does allow users to log arbitrary hostnames.46*/4748static const char *49get_username(void)50{51const struct passwd *pw;52const char *login;53uid_t uid;5455/*56* Attempt to determine the username corresponding to this login57* session. First, validate the results of getlogin() against58* the password database. If getlogin() returns invalid data,59* return an arbitrary username corresponding to this uid.60*/61uid = getuid();62if ((login = getlogin()) != NULL && (pw = getpwnam(login)) != NULL &&63pw->pw_uid == uid)64return (login);65if ((pw = getpwuid(uid)) != NULL)66return (pw->pw_name);67return (NULL);68}6970int71main(int argc, char *argv[])72{73const char *line, *user, *host;7475/* Device line name. */76if ((line = ptsname(STDIN_FILENO)) == NULL)77return (EX_USAGE);7879if ((argc == 2 || argc == 3) && strcmp(argv[1], "login") == 0) {80/* Username. */81user = get_username();82if (user == NULL)83return (EX_OSERR);8485/* Hostname. */86host = argc == 3 ? argv[2] : NULL;8788ulog_login(line, user, host);89return (EX_OK);90} else if (argc == 2 && strcmp(argv[1], "logout") == 0) {91ulog_logout(line);92return (EX_OK);93}9495return (EX_USAGE);96}979899