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. 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 49*1d2276c8SEd Schouten static const char * 50*1d2276c8SEd Schouten get_username(void) 51*1d2276c8SEd Schouten { 52*1d2276c8SEd Schouten const struct passwd *pw; 53*1d2276c8SEd Schouten const char *login; 54*1d2276c8SEd Schouten uid_t uid; 55*1d2276c8SEd Schouten 56*1d2276c8SEd Schouten /* 57*1d2276c8SEd Schouten * Attempt to determine the username corresponding to this login 58*1d2276c8SEd Schouten * session. First, validate the results of getlogin() against 59*1d2276c8SEd Schouten * the password database. If getlogin() returns invalid data, 60*1d2276c8SEd Schouten * return an arbitrary username corresponding to this uid. 61*1d2276c8SEd Schouten */ 62*1d2276c8SEd Schouten uid = getuid(); 63*1d2276c8SEd Schouten if ((login = getlogin()) != NULL && (pw = getpwnam(login)) != NULL && 64*1d2276c8SEd Schouten pw->pw_uid == uid) 65*1d2276c8SEd Schouten return (login); 66*1d2276c8SEd Schouten if ((pw = getpwuid(uid)) != NULL) 67*1d2276c8SEd Schouten return (pw->pw_name); 68*1d2276c8SEd Schouten return (NULL); 69*1d2276c8SEd Schouten } 70*1d2276c8SEd Schouten 712208eadfSEd Schouten int 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. */ 82*1d2276c8SEd 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