loginrec.c revision 285830
1275970Scy/* 2275970Scy * Copyright (c) 2000 Andre Lucas. All rights reserved. 3275970Scy * Portions copyright (c) 1998 Todd C. Miller 4275970Scy * Portions copyright (c) 1996 Jason Downs 5275970Scy * Portions copyright (c) 1996 Theo de Raadt 6275970Scy * 7275970Scy * Redistribution and use in source and binary forms, with or without 8275970Scy * modification, are permitted provided that the following conditions 9275970Scy * are met: 10275970Scy * 1. Redistributions of source code must retain the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer. 12275970Scy * 2. Redistributions in binary form must reproduce the above copyright 13275970Scy * notice, this list of conditions and the following disclaimer in the 14275970Scy * documentation and/or other materials provided with the distribution. 15275970Scy * 16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26275970Scy */ 27275970Scy 28275970Scy/* 29275970Scy * The btmp logging code is derived from login.c from util-linux and is under 30275970Scy * the the following license: 31275970Scy * 32275970Scy * Copyright (c) 1980, 1987, 1988 The Regents of the University of California. 33275970Scy * All rights reserved. 34275970Scy * 35275970Scy * Redistribution and use in source and binary forms are permitted 36275970Scy * provided that the above copyright notice and this paragraph are 37275970Scy * duplicated in all such forms and that any documentation, 38275970Scy * advertising materials, and other materials related to such 39275970Scy * distribution and use acknowledge that the software was developed 40275970Scy * by the University of California, Berkeley. The name of the 41275970Scy * University may not be used to endorse or promote products derived 42275970Scy * from this software without specific prior written permission. 43275970Scy * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 44275970Scy * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 45275970Scy * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 46275970Scy */ 47275970Scy 48275970Scy 49275970Scy/** 50275970Scy ** loginrec.c: platform-independent login recording and lastlog retrieval 51275970Scy **/ 52275970Scy 53275970Scy/* 54275970Scy * The new login code explained 55275970Scy * ============================ 56275970Scy * 57275970Scy * This code attempts to provide a common interface to login recording 58275970Scy * (utmp and friends) and last login time retrieval. 59275970Scy * 60275970Scy * Its primary means of achieving this is to use 'struct logininfo', a 61275970Scy * union of all the useful fields in the various different types of 62275970Scy * system login record structures one finds on UNIX variants. 63275970Scy * 64275970Scy * We depend on autoconf to define which recording methods are to be 65275970Scy * used, and which fields are contained in the relevant data structures 66275970Scy * on the local system. Many C preprocessor symbols affect which code 67275970Scy * gets compiled here. 68275970Scy * 69275970Scy * The code is designed to make it easy to modify a particular 70275970Scy * recording method, without affecting other methods nor requiring so 71275970Scy * many nested conditional compilation blocks as were commonplace in 72275970Scy * the old code. 73275970Scy * 74275970Scy * For login recording, we try to use the local system's libraries as 75275970Scy * these are clearly most likely to work correctly. For utmp systems 76275970Scy * this usually means login() and logout() or setutent() etc., probably 77275970Scy * in libutil, along with logwtmp() etc. On these systems, we fall back 78275970Scy * to writing the files directly if we have to, though this method 79275970Scy * requires very thorough testing so we do not corrupt local auditing 80275970Scy * information. These files and their access methods are very system 81275970Scy * specific indeed. 82275970Scy * 83275970Scy * For utmpx systems, the corresponding library functions are 84275970Scy * setutxent() etc. To the author's knowledge, all utmpx systems have 85275970Scy * these library functions and so no direct write is attempted. If such 86275970Scy * a system exists and needs support, direct analogues of the [uw]tmp 87275970Scy * code should suffice. 88275970Scy * 89275970Scy * Retrieving the time of last login ('lastlog') is in some ways even 90275970Scy * more problemmatic than login recording. Some systems provide a 91275970Scy * simple table of all users which we seek based on uid and retrieve a 92275970Scy * relatively standard structure. Others record the same information in 93275970Scy * a directory with a separate file, and others don't record the 94275970Scy * information separately at all. For systems in the latter category, 95275970Scy * we look backwards in the wtmp or wtmpx file for the last login entry 96275970Scy * for our user. Naturally this is slower and on busy systems could 97275970Scy * incur a significant performance penalty. 98275970Scy * 99275970Scy * Calling the new code 100275970Scy * -------------------- 101275970Scy * 102275970Scy * In OpenSSH all login recording and retrieval is performed in 103275970Scy * login.c. Here you'll find working examples. Also, in the logintest.c 104275970Scy * program there are more examples. 105275970Scy * 106275970Scy * Internal handler calling method 107275970Scy * ------------------------------- 108275970Scy * 109275970Scy * When a call is made to login_login() or login_logout(), both 110275970Scy * routines set a struct logininfo flag defining which action (log in, 111275970Scy * or log out) is to be taken. They both then call login_write(), which 112275970Scy * calls whichever of the many structure-specific handlers autoconf 113275970Scy * selects for the local system. 114275970Scy * 115275970Scy * The handlers themselves handle system data structure specifics. Both 116275970Scy * struct utmp and struct utmpx have utility functions (see 117275970Scy * construct_utmp*()) to try to make it simpler to add extra systems 118275970Scy * that introduce new features to either structure. 119275970Scy * 120275970Scy * While it may seem terribly wasteful to replicate so much similar 121275970Scy * code for each method, experience has shown that maintaining code to 122275970Scy * write both struct utmp and utmpx in one function, whilst maintaining 123275970Scy * support for all systems whether they have library support or not, is 124275970Scy * a difficult and time-consuming task. 125275970Scy * 126275970Scy * Lastlog support proceeds similarly. Functions login_get_lastlog() 127275970Scy * (and its OpenSSH-tuned friend login_get_lastlog_time()) call 128275970Scy * getlast_entry(), which tries one of three methods to find the last 129275970Scy * login time. It uses local system lastlog support if it can, 130275970Scy * otherwise it tries wtmp or wtmpx before giving up and returning 0, 131275970Scy * meaning "tilt". 132275970Scy * 133275970Scy * Maintenance 134275970Scy * ----------- 135275970Scy * 136275970Scy * In many cases it's possible to tweak autoconf to select the correct 137275970Scy * methods for a particular platform, either by improving the detection 138275970Scy * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE 139275970Scy * symbols for the platform. 140275970Scy * 141275970Scy * Use logintest to check which symbols are defined before modifying 142275970Scy * configure.ac and loginrec.c. (You have to build logintest yourself 143275970Scy * with 'make logintest' as it's not built by default.) 144275970Scy * 145275970Scy * Otherwise, patches to the specific method(s) are very helpful! 146275970Scy */ 147275970Scy 148275970Scy#include "includes.h" 149275970Scy 150275970Scy#include <sys/types.h> 151275970Scy#include <sys/stat.h> 152275970Scy#include <sys/socket.h> 153275970Scy 154275970Scy#include <netinet/in.h> 155275970Scy 156275970Scy#include <errno.h> 157275970Scy#include <fcntl.h> 158275970Scy#ifdef HAVE_PATHS_H 159275970Scy# include <paths.h> 160275970Scy#endif 161275970Scy#include <pwd.h> 162275970Scy#include <stdarg.h> 163275970Scy#include <string.h> 164275970Scy#include <time.h> 165275970Scy#include <unistd.h> 166275970Scy 167275970Scy#include "xmalloc.h" 168275970Scy#include "key.h" 169275970Scy#include "hostfile.h" 170275970Scy#include "ssh.h" 171275970Scy#include "loginrec.h" 172275970Scy#include "log.h" 173275970Scy#include "atomicio.h" 174275970Scy#include "packet.h" 175275970Scy#include "canohost.h" 176275970Scy#include "auth.h" 177275970Scy#include "buffer.h" 178275970Scy 179275970Scy#ifdef HAVE_UTIL_H 180275970Scy# include <util.h> 181275970Scy#endif 182275970Scy 183275970Scy/** 184275970Scy ** prototypes for helper functions in this file 185275970Scy **/ 186275970Scy 187275970Scy#if HAVE_UTMP_H 188275970Scyvoid set_utmp_time(struct logininfo *li, struct utmp *ut); 189275970Scyvoid construct_utmp(struct logininfo *li, struct utmp *ut); 190275970Scy#endif 191275970Scy 192275970Scy#ifdef HAVE_UTMPX_H 193275970Scyvoid set_utmpx_time(struct logininfo *li, struct utmpx *ut); 194275970Scyvoid construct_utmpx(struct logininfo *li, struct utmpx *ut); 195275970Scy#endif 196275970Scy 197275970Scyint utmp_write_entry(struct logininfo *li); 198275970Scyint utmpx_write_entry(struct logininfo *li); 199275970Scyint wtmp_write_entry(struct logininfo *li); 200275970Scyint wtmpx_write_entry(struct logininfo *li); 201275970Scyint lastlog_write_entry(struct logininfo *li); 202275970Scyint syslogin_write_entry(struct logininfo *li); 203275970Scy 204275970Scyint getlast_entry(struct logininfo *li); 205275970Scyint lastlog_get_entry(struct logininfo *li); 206275970Scyint utmpx_get_entry(struct logininfo *li); 207275970Scyint wtmp_get_entry(struct logininfo *li); 208275970Scyint wtmpx_get_entry(struct logininfo *li); 209275970Scy 210275970Scyextern Buffer loginmsg; 211275970Scy 212275970Scy/* pick the shortest string */ 213275970Scy#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2)) 214275970Scy 215275970Scy/** 216275970Scy ** platform-independent login functions 217275970Scy **/ 218275970Scy 219275970Scy/* 220275970Scy * login_login(struct logininfo *) - Record a login 221275970Scy * 222275970Scy * Call with a pointer to a struct logininfo initialised with 223275970Scy * login_init_entry() or login_alloc_entry() 224275970Scy * 225275970Scy * Returns: 226275970Scy * >0 if successful 227275970Scy * 0 on failure (will use OpenSSH's logging facilities for diagnostics) 228275970Scy */ 229275970Scyint 230275970Scylogin_login(struct logininfo *li) 231275970Scy{ 232275970Scy li->type = LTYPE_LOGIN; 233275970Scy return (login_write(li)); 234275970Scy} 235275970Scy 236275970Scy 237275970Scy/* 238275970Scy * login_logout(struct logininfo *) - Record a logout 239275970Scy * 240275970Scy * Call as with login_login() 241275970Scy * 242275970Scy * Returns: 243275970Scy * >0 if successful 244275970Scy * 0 on failure (will use OpenSSH's logging facilities for diagnostics) 245275970Scy */ 246275970Scyint 247275970Scylogin_logout(struct logininfo *li) 248275970Scy{ 249275970Scy li->type = LTYPE_LOGOUT; 250275970Scy return (login_write(li)); 251275970Scy} 252275970Scy 253275970Scy/* 254275970Scy * login_get_lastlog_time(int) - Retrieve the last login time 255275970Scy * 256275970Scy * Retrieve the last login time for the given uid. Will try to use the 257275970Scy * system lastlog facilities if they are available, but will fall back 258275970Scy * to looking in wtmp/wtmpx if necessary 259275970Scy * 260275970Scy * Returns: 261275970Scy * 0 on failure, or if user has never logged in 262275970Scy * Time in seconds from the epoch if successful 263275970Scy * 264275970Scy * Useful preprocessor symbols: 265275970Scy * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog 266275970Scy * info 267275970Scy * USE_LASTLOG: If set, indicates the presence of system lastlog 268275970Scy * facilities. If this and DISABLE_LASTLOG are not set, 269275970Scy * try to retrieve lastlog information from wtmp/wtmpx. 270275970Scy */ 271275970Scyunsigned int 272275970Scylogin_get_lastlog_time(const uid_t uid) 273275970Scy{ 274275970Scy struct logininfo li; 275275970Scy 276275970Scy if (login_get_lastlog(&li, uid)) 277275970Scy return (li.tv_sec); 278275970Scy else 279275970Scy return (0); 280275970Scy} 281275970Scy 282275970Scy/* 283275970Scy * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry 284275970Scy * 285275970Scy * Retrieve a logininfo structure populated (only partially) with 286275970Scy * information from the system lastlog data, or from wtmp/wtmpx if no 287275970Scy * system lastlog information exists. 288275970Scy * 289275970Scy * Note this routine must be given a pre-allocated logininfo. 290275970Scy * 291275970Scy * Returns: 292275970Scy * >0: A pointer to your struct logininfo if successful 293275970Scy * 0 on failure (will use OpenSSH's logging facilities for diagnostics) 294275970Scy */ 295275970Scystruct logininfo * 296275970Scylogin_get_lastlog(struct logininfo *li, const uid_t uid) 297275970Scy{ 298275970Scy struct passwd *pw; 299275970Scy 300275970Scy memset(li, '\0', sizeof(*li)); 301275970Scy li->uid = uid; 302275970Scy 303275970Scy /* 304275970Scy * If we don't have a 'real' lastlog, we need the username to 305275970Scy * reliably search wtmp(x) for the last login (see 306275970Scy * wtmp_get_entry().) 307275970Scy */ 308275970Scy pw = getpwuid(uid); 309275970Scy if (pw == NULL) 310275970Scy fatal("%s: Cannot find account for uid %ld", __func__, 311275970Scy (long)uid); 312275970Scy 313275970Scy if (strlcpy(li->username, pw->pw_name, sizeof(li->username)) >= 314275970Scy sizeof(li->username)) { 315275970Scy error("%s: username too long (%lu > max %lu)", __func__, 316275970Scy (unsigned long)strlen(pw->pw_name), 317275970Scy (unsigned long)sizeof(li->username) - 1); 318275970Scy return NULL; 319275970Scy } 320275970Scy 321275970Scy if (getlast_entry(li)) 322275970Scy return (li); 323275970Scy else 324275970Scy return (NULL); 325275970Scy} 326275970Scy 327275970Scy/* 328275970Scy * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise 329275970Scy * a logininfo structure 330275970Scy * 331275970Scy * This function creates a new struct logininfo, a data structure 332275970Scy * meant to carry the information required to portably record login info. 333275970Scy * 334275970Scy * Returns a pointer to a newly created struct logininfo. If memory 335275970Scy * allocation fails, the program halts. 336275970Scy */ 337275970Scystruct 338275970Scylogininfo *login_alloc_entry(pid_t pid, const char *username, 339275970Scy const char *hostname, const char *line) 340275970Scy{ 341275970Scy struct logininfo *newli; 342275970Scy 343275970Scy newli = xmalloc(sizeof(*newli)); 344275970Scy login_init_entry(newli, pid, username, hostname, line); 345275970Scy return (newli); 346275970Scy} 347275970Scy 348275970Scy 349275970Scy/* login_free_entry(struct logininfo *) - free struct memory */ 350275970Scyvoid 351275970Scylogin_free_entry(struct logininfo *li) 352275970Scy{ 353275970Scy free(li); 354275970Scy} 355275970Scy 356275970Scy 357275970Scy/* login_init_entry(struct logininfo *, int, char*, char*, char*) 358275970Scy * - initialise a struct logininfo 359275970Scy * 360275970Scy * Populates a new struct logininfo, a data structure meant to carry 361275970Scy * the information required to portably record login info. 362275970Scy * 363275970Scy * Returns: 1 364275970Scy */ 365275970Scyint 366275970Scylogin_init_entry(struct logininfo *li, pid_t pid, const char *username, 367275970Scy const char *hostname, const char *line) 368275970Scy{ 369275970Scy struct passwd *pw; 370275970Scy 371275970Scy memset(li, 0, sizeof(*li)); 372275970Scy 373275970Scy li->pid = pid; 374275970Scy 375275970Scy /* set the line information */ 376275970Scy if (line) 377275970Scy line_fullname(li->line, line, sizeof(li->line)); 378275970Scy 379275970Scy if (username) { 380275970Scy strlcpy(li->username, username, sizeof(li->username)); 381275970Scy pw = getpwnam(li->username); 382275970Scy if (pw == NULL) { 383275970Scy fatal("%s: Cannot find user \"%s\"", __func__, 384275970Scy li->username); 385275970Scy } 386275970Scy li->uid = pw->pw_uid; 387275970Scy } 388275970Scy 389275970Scy if (hostname) 390275970Scy strlcpy(li->hostname, hostname, sizeof(li->hostname)); 391275970Scy 392275970Scy return (1); 393275970Scy} 394275970Scy 395275970Scy/* 396275970Scy * login_set_current_time(struct logininfo *) - set the current time 397275970Scy * 398275970Scy * Set the current time in a logininfo structure. This function is 399275970Scy * meant to eliminate the need to deal with system dependencies for 400275970Scy * time handling. 401275970Scy */ 402275970Scyvoid 403275970Scylogin_set_current_time(struct logininfo *li) 404275970Scy{ 405275970Scy struct timeval tv; 406275970Scy 407275970Scy gettimeofday(&tv, NULL); 408275970Scy 409275970Scy li->tv_sec = tv.tv_sec; 410275970Scy li->tv_usec = tv.tv_usec; 411275970Scy} 412275970Scy 413275970Scy/* copy a sockaddr_* into our logininfo */ 414275970Scyvoid 415275970Scylogin_set_addr(struct logininfo *li, const struct sockaddr *sa, 416275970Scy const unsigned int sa_size) 417275970Scy{ 418275970Scy unsigned int bufsize = sa_size; 419275970Scy 420275970Scy /* make sure we don't overrun our union */ 421275970Scy if (sizeof(li->hostaddr) < sa_size) 422275970Scy bufsize = sizeof(li->hostaddr); 423275970Scy 424275970Scy memcpy(&li->hostaddr.sa, sa, bufsize); 425275970Scy} 426275970Scy 427275970Scy 428275970Scy/** 429275970Scy ** login_write: Call low-level recording functions based on autoconf 430275970Scy ** results 431275970Scy **/ 432275970Scyint 433275970Scylogin_write(struct logininfo *li) 434275970Scy{ 435275970Scy#ifndef HAVE_CYGWIN 436275970Scy if (geteuid() != 0) { 437275970Scy logit("Attempt to write login records by non-root user (aborting)"); 438275970Scy return (1); 439275970Scy } 440275970Scy#endif 441275970Scy 442275970Scy /* set the timestamp */ 443275970Scy login_set_current_time(li); 444275970Scy#ifdef USE_LOGIN 445275970Scy syslogin_write_entry(li); 446275970Scy#endif 447275970Scy#ifdef USE_LASTLOG 448275970Scy if (li->type == LTYPE_LOGIN) 449275970Scy lastlog_write_entry(li); 450275970Scy#endif 451275970Scy#ifdef USE_UTMP 452275970Scy utmp_write_entry(li); 453275970Scy#endif 454275970Scy#ifdef USE_WTMP 455275970Scy wtmp_write_entry(li); 456275970Scy#endif 457275970Scy#ifdef USE_UTMPX 458275970Scy utmpx_write_entry(li); 459275970Scy#endif 460275970Scy#ifdef USE_WTMPX 461275970Scy wtmpx_write_entry(li); 462275970Scy#endif 463275970Scy#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN 464275970Scy if (li->type == LTYPE_LOGIN && 465275970Scy !sys_auth_record_login(li->username,li->hostname,li->line, 466275970Scy &loginmsg)) 467275970Scy logit("Writing login record failed for %s", li->username); 468275970Scy#endif 469275970Scy#ifdef SSH_AUDIT_EVENTS 470275970Scy if (li->type == LTYPE_LOGIN) 471275970Scy audit_session_open(li); 472275970Scy else if (li->type == LTYPE_LOGOUT) 473275970Scy audit_session_close(li); 474275970Scy#endif 475275970Scy return (0); 476275970Scy} 477275970Scy 478275970Scy#ifdef LOGIN_NEEDS_UTMPX 479275970Scyint 480275970Scylogin_utmp_only(struct logininfo *li) 481275970Scy{ 482275970Scy li->type = LTYPE_LOGIN; 483275970Scy login_set_current_time(li); 484275970Scy# ifdef USE_UTMP 485275970Scy utmp_write_entry(li); 486275970Scy# endif 487275970Scy# ifdef USE_WTMP 488275970Scy wtmp_write_entry(li); 489275970Scy# endif 490275970Scy# ifdef USE_UTMPX 491275970Scy utmpx_write_entry(li); 492275970Scy# endif 493275970Scy# ifdef USE_WTMPX 494275970Scy wtmpx_write_entry(li); 495275970Scy# endif 496275970Scy return (0); 497275970Scy} 498275970Scy#endif 499275970Scy 500275970Scy/** 501275970Scy ** getlast_entry: Call low-level functions to retrieve the last login 502275970Scy ** time. 503275970Scy **/ 504275970Scy 505275970Scy/* take the uid in li and return the last login time */ 506275970Scyint 507275970Scygetlast_entry(struct logininfo *li) 508275970Scy{ 509275970Scy#ifdef USE_LASTLOG 510275970Scy return(lastlog_get_entry(li)); 511275970Scy#else /* !USE_LASTLOG */ 512275970Scy#if defined(USE_UTMPX) && defined(HAVE_SETUTXDB) && \ 513275970Scy defined(UTXDB_LASTLOGIN) && defined(HAVE_GETUTXUSER) 514275970Scy return (utmpx_get_entry(li)); 515275970Scy#endif 516275970Scy 517275970Scy#if defined(DISABLE_LASTLOG) 518275970Scy /* On some systems we shouldn't even try to obtain last login 519275970Scy * time, e.g. AIX */ 520275970Scy return (0); 521275970Scy# elif defined(USE_WTMP) && \ 522275970Scy (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP)) 523275970Scy /* retrieve last login time from utmp */ 524275970Scy return (wtmp_get_entry(li)); 525275970Scy# elif defined(USE_WTMPX) && \ 526275970Scy (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX)) 527275970Scy /* If wtmp isn't available, try wtmpx */ 528275970Scy return (wtmpx_get_entry(li)); 529275970Scy# else 530275970Scy /* Give up: No means of retrieving last login time */ 531275970Scy return (0); 532275970Scy# endif /* DISABLE_LASTLOG */ 533275970Scy#endif /* USE_LASTLOG */ 534275970Scy} 535275970Scy 536275970Scy 537275970Scy 538275970Scy/* 539275970Scy * 'line' string utility functions 540275970Scy * 541275970Scy * These functions process the 'line' string into one of three forms: 542275970Scy * 543275970Scy * 1. The full filename (including '/dev') 544275970Scy * 2. The stripped name (excluding '/dev') 545275970Scy * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00 546275970Scy * /dev/pts/1 -> ts/1 ) 547275970Scy * 548275970Scy * Form 3 is used on some systems to identify a .tmp.? entry when 549275970Scy * attempting to remove it. Typically both addition and removal is 550275970Scy * performed by one application - say, sshd - so as long as the choice 551275970Scy * uniquely identifies a terminal it's ok. 552275970Scy */ 553275970Scy 554275970Scy 555275970Scy/* 556275970Scy * line_fullname(): add the leading '/dev/' if it doesn't exist make 557275970Scy * sure dst has enough space, if not just copy src (ugh) 558275970Scy */ 559275970Scychar * 560275970Scyline_fullname(char *dst, const char *src, u_int dstsize) 561275970Scy{ 562275970Scy memset(dst, '\0', dstsize); 563275970Scy if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5))) 564275970Scy strlcpy(dst, src, dstsize); 565275970Scy else { 566275970Scy strlcpy(dst, "/dev/", dstsize); 567275970Scy strlcat(dst, src, dstsize); 568275970Scy } 569275970Scy return (dst); 570275970Scy} 571275970Scy 572275970Scy/* line_stripname(): strip the leading '/dev' if it exists, return dst */ 573275970Scychar * 574275970Scyline_stripname(char *dst, const char *src, int dstsize) 575275970Scy{ 576275970Scy memset(dst, '\0', dstsize); 577275970Scy if (strncmp(src, "/dev/", 5) == 0) 578275970Scy strlcpy(dst, src + 5, dstsize); 579275970Scy else 580275970Scy strlcpy(dst, src, dstsize); 581275970Scy return (dst); 582275970Scy} 583275970Scy 584275970Scy/* 585275970Scy * line_abbrevname(): Return the abbreviated (usually four-character) 586275970Scy * form of the line (Just use the last <dstsize> characters of the 587275970Scy * full name.) 588275970Scy * 589275970Scy * NOTE: use strncpy because we do NOT necessarily want zero 590275970Scy * termination 591275970Scy */ 592275970Scychar * 593275970Scyline_abbrevname(char *dst, const char *src, int dstsize) 594275970Scy{ 595275970Scy size_t len; 596275970Scy 597275970Scy memset(dst, '\0', dstsize); 598275970Scy 599275970Scy /* Always skip prefix if present */ 600275970Scy if (strncmp(src, "/dev/", 5) == 0) 601275970Scy src += 5; 602275970Scy 603275970Scy#ifdef WITH_ABBREV_NO_TTY 604275970Scy if (strncmp(src, "tty", 3) == 0) 605275970Scy src += 3; 606275970Scy#endif 607275970Scy 608275970Scy len = strlen(src); 609275970Scy 610275970Scy if (len > 0) { 611275970Scy if (((int)len - dstsize) > 0) 612275970Scy src += ((int)len - dstsize); 613275970Scy 614275970Scy /* note: _don't_ change this to strlcpy */ 615275970Scy strncpy(dst, src, (size_t)dstsize); 616275970Scy } 617275970Scy 618275970Scy return (dst); 619275970Scy} 620275970Scy 621275970Scy/** 622275970Scy ** utmp utility functions 623275970Scy ** 624275970Scy ** These functions manipulate struct utmp, taking system differences 625275970Scy ** into account. 626275970Scy **/ 627275970Scy 628275970Scy#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN) 629275970Scy 630275970Scy/* build the utmp structure */ 631275970Scyvoid 632275970Scyset_utmp_time(struct logininfo *li, struct utmp *ut) 633275970Scy{ 634275970Scy# if defined(HAVE_TV_IN_UTMP) 635275970Scy ut->ut_tv.tv_sec = li->tv_sec; 636275970Scy ut->ut_tv.tv_usec = li->tv_usec; 637275970Scy# elif defined(HAVE_TIME_IN_UTMP) 638275970Scy ut->ut_time = li->tv_sec; 639275970Scy# endif 640275970Scy} 641275970Scy 642275970Scyvoid 643275970Scyconstruct_utmp(struct logininfo *li, 644275970Scy struct utmp *ut) 645275970Scy{ 646275970Scy# ifdef HAVE_ADDR_V6_IN_UTMP 647275970Scy struct sockaddr_in6 *sa6; 648275970Scy# endif 649275970Scy 650275970Scy memset(ut, '\0', sizeof(*ut)); 651275970Scy 652275970Scy /* First fill out fields used for both logins and logouts */ 653275970Scy 654275970Scy# ifdef HAVE_ID_IN_UTMP 655275970Scy line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id)); 656275970Scy# endif 657275970Scy 658275970Scy# ifdef HAVE_TYPE_IN_UTMP 659275970Scy /* This is done here to keep utmp constants out of struct logininfo */ 660275970Scy switch (li->type) { 661275970Scy case LTYPE_LOGIN: 662275970Scy ut->ut_type = USER_PROCESS; 663275970Scy#ifdef _UNICOS 664275970Scy cray_set_tmpdir(ut); 665275970Scy#endif 666275970Scy break; 667275970Scy case LTYPE_LOGOUT: 668275970Scy ut->ut_type = DEAD_PROCESS; 669275970Scy#ifdef _UNICOS 670275970Scy cray_retain_utmp(ut, li->pid); 671275970Scy#endif 672275970Scy break; 673275970Scy } 674275970Scy# endif 675275970Scy set_utmp_time(li, ut); 676275970Scy 677275970Scy line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line)); 678275970Scy 679275970Scy# ifdef HAVE_PID_IN_UTMP 680275970Scy ut->ut_pid = li->pid; 681275970Scy# endif 682275970Scy 683275970Scy /* If we're logging out, leave all other fields blank */ 684275970Scy if (li->type == LTYPE_LOGOUT) 685275970Scy return; 686275970Scy 687275970Scy /* 688275970Scy * These fields are only used when logging in, and are blank 689275970Scy * for logouts. 690275970Scy */ 691275970Scy 692275970Scy /* Use strncpy because we don't necessarily want null termination */ 693275970Scy strncpy(ut->ut_name, li->username, 694275970Scy MIN_SIZEOF(ut->ut_name, li->username)); 695275970Scy# ifdef HAVE_HOST_IN_UTMP 696275970Scy strncpy(ut->ut_host, li->hostname, 697275970Scy MIN_SIZEOF(ut->ut_host, li->hostname)); 698275970Scy# endif 699275970Scy# ifdef HAVE_ADDR_IN_UTMP 700275970Scy /* this is just a 32-bit IP address */ 701275970Scy if (li->hostaddr.sa.sa_family == AF_INET) 702275970Scy ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; 703275970Scy# endif 704275970Scy# ifdef HAVE_ADDR_V6_IN_UTMP 705275970Scy /* this is just a 128-bit IPv6 address */ 706275970Scy if (li->hostaddr.sa.sa_family == AF_INET6) { 707275970Scy sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa); 708275970Scy memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16); 709275970Scy if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { 710275970Scy ut->ut_addr_v6[0] = ut->ut_addr_v6[3]; 711275970Scy ut->ut_addr_v6[1] = 0; 712275970Scy ut->ut_addr_v6[2] = 0; 713275970Scy ut->ut_addr_v6[3] = 0; 714275970Scy } 715275970Scy } 716275970Scy# endif 717275970Scy} 718275970Scy#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */ 719275970Scy 720275970Scy/** 721275970Scy ** utmpx utility functions 722275970Scy ** 723275970Scy ** These functions manipulate struct utmpx, accounting for system 724275970Scy ** variations. 725275970Scy **/ 726275970Scy 727275970Scy#if defined(USE_UTMPX) || defined (USE_WTMPX) 728275970Scy/* build the utmpx structure */ 729275970Scyvoid 730275970Scyset_utmpx_time(struct logininfo *li, struct utmpx *utx) 731275970Scy{ 732275970Scy# if defined(HAVE_TV_IN_UTMPX) 733275970Scy utx->ut_tv.tv_sec = li->tv_sec; 734275970Scy utx->ut_tv.tv_usec = li->tv_usec; 735275970Scy# elif defined(HAVE_TIME_IN_UTMPX) 736275970Scy utx->ut_time = li->tv_sec; 737275970Scy# endif 738275970Scy} 739275970Scy 740275970Scyvoid 741275970Scyconstruct_utmpx(struct logininfo *li, struct utmpx *utx) 742275970Scy{ 743275970Scy# ifdef HAVE_ADDR_V6_IN_UTMP 744275970Scy struct sockaddr_in6 *sa6; 745275970Scy# endif 746275970Scy memset(utx, '\0', sizeof(*utx)); 747275970Scy 748275970Scy# ifdef HAVE_ID_IN_UTMPX 749275970Scy line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id)); 750275970Scy# endif 751275970Scy 752275970Scy /* this is done here to keep utmp constants out of loginrec.h */ 753275970Scy switch (li->type) { 754275970Scy case LTYPE_LOGIN: 755275970Scy utx->ut_type = USER_PROCESS; 756275970Scy break; 757275970Scy case LTYPE_LOGOUT: 758275970Scy utx->ut_type = DEAD_PROCESS; 759275970Scy break; 760275970Scy } 761275970Scy line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line)); 762275970Scy set_utmpx_time(li, utx); 763275970Scy utx->ut_pid = li->pid; 764275970Scy 765275970Scy /* strncpy(): Don't necessarily want null termination */ 766275970Scy strncpy(utx->ut_user, li->username, 767275970Scy MIN_SIZEOF(utx->ut_user, li->username)); 768275970Scy 769275970Scy if (li->type == LTYPE_LOGOUT) 770275970Scy return; 771275970Scy 772275970Scy /* 773275970Scy * These fields are only used when logging in, and are blank 774275970Scy * for logouts. 775275970Scy */ 776275970Scy 777275970Scy# ifdef HAVE_HOST_IN_UTMPX 778275970Scy strncpy(utx->ut_host, li->hostname, 779275970Scy MIN_SIZEOF(utx->ut_host, li->hostname)); 780275970Scy# endif 781275970Scy# ifdef HAVE_ADDR_IN_UTMPX 782275970Scy /* this is just a 32-bit IP address */ 783275970Scy if (li->hostaddr.sa.sa_family == AF_INET) 784275970Scy utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; 785275970Scy# endif 786275970Scy# ifdef HAVE_ADDR_V6_IN_UTMP 787275970Scy /* this is just a 128-bit IPv6 address */ 788275970Scy if (li->hostaddr.sa.sa_family == AF_INET6) { 789275970Scy sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa); 790275970Scy memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16); 791275970Scy if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { 792275970Scy ut->ut_addr_v6[0] = ut->ut_addr_v6[3]; 793275970Scy ut->ut_addr_v6[1] = 0; 794275970Scy ut->ut_addr_v6[2] = 0; 795275970Scy ut->ut_addr_v6[3] = 0; 796275970Scy } 797275970Scy } 798275970Scy# endif 799275970Scy# ifdef HAVE_SYSLEN_IN_UTMPX 800275970Scy /* ut_syslen is the length of the utx_host string */ 801275970Scy utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host)); 802275970Scy# endif 803275970Scy} 804275970Scy#endif /* USE_UTMPX || USE_WTMPX */ 805275970Scy 806275970Scy/** 807275970Scy ** Low-level utmp functions 808275970Scy **/ 809275970Scy 810275970Scy/* FIXME: (ATL) utmp_write_direct needs testing */ 811275970Scy#ifdef USE_UTMP 812275970Scy 813275970Scy/* if we can, use pututline() etc. */ 814275970Scy# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \ 815275970Scy defined(HAVE_PUTUTLINE) 816275970Scy# define UTMP_USE_LIBRARY 817275970Scy# endif 818275970Scy 819275970Scy 820275970Scy/* write a utmp entry with the system's help (pututline() and pals) */ 821275970Scy# ifdef UTMP_USE_LIBRARY 822275970Scystatic int 823275970Scyutmp_write_library(struct logininfo *li, struct utmp *ut) 824275970Scy{ 825275970Scy setutent(); 826275970Scy pututline(ut); 827275970Scy# ifdef HAVE_ENDUTENT 828275970Scy endutent(); 829275970Scy# endif 830275970Scy return (1); 831275970Scy} 832275970Scy# else /* UTMP_USE_LIBRARY */ 833275970Scy 834275970Scy/* 835275970Scy * Write a utmp entry direct to the file 836275970Scy * This is a slightly modification of code in OpenBSD's login.c 837275970Scy */ 838275970Scystatic int 839275970Scyutmp_write_direct(struct logininfo *li, struct utmp *ut) 840275970Scy{ 841275970Scy struct utmp old_ut; 842275970Scy register int fd; 843275970Scy int tty; 844275970Scy 845275970Scy /* FIXME: (ATL) ttyslot() needs local implementation */ 846275970Scy 847275970Scy#if defined(HAVE_GETTTYENT) 848275970Scy struct ttyent *ty; 849275970Scy 850275970Scy tty=0; 851275970Scy setttyent(); 852275970Scy while (NULL != (ty = getttyent())) { 853275970Scy tty++; 854275970Scy if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line))) 855275970Scy break; 856275970Scy } 857275970Scy endttyent(); 858275970Scy 859275970Scy if (NULL == ty) { 860275970Scy logit("%s: tty not found", __func__); 861275970Scy return (0); 862275970Scy } 863275970Scy#else /* FIXME */ 864275970Scy 865275970Scy tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */ 866275970Scy 867275970Scy#endif /* HAVE_GETTTYENT */ 868275970Scy 869275970Scy if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) { 870275970Scy off_t pos, ret; 871275970Scy 872275970Scy pos = (off_t)tty * sizeof(struct utmp); 873275970Scy if ((ret = lseek(fd, pos, SEEK_SET)) == -1) { 874275970Scy logit("%s: lseek: %s", __func__, strerror(errno)); 875275970Scy close(fd); 876275970Scy return (0); 877275970Scy } 878275970Scy if (ret != pos) { 879275970Scy logit("%s: Couldn't seek to tty %d slot in %s", 880275970Scy __func__, tty, UTMP_FILE); 881275970Scy close(fd); 882275970Scy return (0); 883275970Scy } 884275970Scy /* 885275970Scy * Prevent luser from zero'ing out ut_host. 886275970Scy * If the new ut_line is empty but the old one is not 887275970Scy * and ut_line and ut_name match, preserve the old ut_line. 888275970Scy */ 889275970Scy if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) && 890275970Scy (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') && 891275970Scy (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) && 892 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) 893 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host)); 894 895 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) { 896 logit("%s: lseek: %s", __func__, strerror(errno)); 897 close(fd); 898 return (0); 899 } 900 if (ret != pos) { 901 logit("%s: Couldn't seek to tty %d slot in %s", 902 __func__, tty, UTMP_FILE); 903 close(fd); 904 return (0); 905 } 906 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) { 907 logit("%s: error writing %s: %s", __func__, 908 UTMP_FILE, strerror(errno)); 909 close(fd); 910 return (0); 911 } 912 913 close(fd); 914 return (1); 915 } else { 916 return (0); 917 } 918} 919# endif /* UTMP_USE_LIBRARY */ 920 921static int 922utmp_perform_login(struct logininfo *li) 923{ 924 struct utmp ut; 925 926 construct_utmp(li, &ut); 927# ifdef UTMP_USE_LIBRARY 928 if (!utmp_write_library(li, &ut)) { 929 logit("%s: utmp_write_library() failed", __func__); 930 return (0); 931 } 932# else 933 if (!utmp_write_direct(li, &ut)) { 934 logit("%s: utmp_write_direct() failed", __func__); 935 return (0); 936 } 937# endif 938 return (1); 939} 940 941 942static int 943utmp_perform_logout(struct logininfo *li) 944{ 945 struct utmp ut; 946 947 construct_utmp(li, &ut); 948# ifdef UTMP_USE_LIBRARY 949 if (!utmp_write_library(li, &ut)) { 950 logit("%s: utmp_write_library() failed", __func__); 951 return (0); 952 } 953# else 954 if (!utmp_write_direct(li, &ut)) { 955 logit("%s: utmp_write_direct() failed", __func__); 956 return (0); 957 } 958# endif 959 return (1); 960} 961 962 963int 964utmp_write_entry(struct logininfo *li) 965{ 966 switch(li->type) { 967 case LTYPE_LOGIN: 968 return (utmp_perform_login(li)); 969 970 case LTYPE_LOGOUT: 971 return (utmp_perform_logout(li)); 972 973 default: 974 logit("%s: invalid type field", __func__); 975 return (0); 976 } 977} 978#endif /* USE_UTMP */ 979 980 981/** 982 ** Low-level utmpx functions 983 **/ 984 985/* not much point if we don't want utmpx entries */ 986#ifdef USE_UTMPX 987 988/* if we have the wherewithall, use pututxline etc. */ 989# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \ 990 defined(HAVE_PUTUTXLINE) 991# define UTMPX_USE_LIBRARY 992# endif 993 994 995/* write a utmpx entry with the system's help (pututxline() and pals) */ 996# ifdef UTMPX_USE_LIBRARY 997static int 998utmpx_write_library(struct logininfo *li, struct utmpx *utx) 999{ 1000 setutxent(); 1001 pututxline(utx); 1002 1003# ifdef HAVE_ENDUTXENT 1004 endutxent(); 1005# endif 1006 return (1); 1007} 1008 1009# else /* UTMPX_USE_LIBRARY */ 1010 1011/* write a utmp entry direct to the file */ 1012static int 1013utmpx_write_direct(struct logininfo *li, struct utmpx *utx) 1014{ 1015 logit("%s: not implemented!", __func__); 1016 return (0); 1017} 1018# endif /* UTMPX_USE_LIBRARY */ 1019 1020static int 1021utmpx_perform_login(struct logininfo *li) 1022{ 1023 struct utmpx utx; 1024 1025 construct_utmpx(li, &utx); 1026# ifdef UTMPX_USE_LIBRARY 1027 if (!utmpx_write_library(li, &utx)) { 1028 logit("%s: utmp_write_library() failed", __func__); 1029 return (0); 1030 } 1031# else 1032 if (!utmpx_write_direct(li, &ut)) { 1033 logit("%s: utmp_write_direct() failed", __func__); 1034 return (0); 1035 } 1036# endif 1037 return (1); 1038} 1039 1040 1041static int 1042utmpx_perform_logout(struct logininfo *li) 1043{ 1044 struct utmpx utx; 1045 1046 construct_utmpx(li, &utx); 1047# ifdef HAVE_ID_IN_UTMPX 1048 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id)); 1049# endif 1050# ifdef HAVE_TYPE_IN_UTMPX 1051 utx.ut_type = DEAD_PROCESS; 1052# endif 1053 1054# ifdef UTMPX_USE_LIBRARY 1055 utmpx_write_library(li, &utx); 1056# else 1057 utmpx_write_direct(li, &utx); 1058# endif 1059 return (1); 1060} 1061 1062int 1063utmpx_write_entry(struct logininfo *li) 1064{ 1065 switch(li->type) { 1066 case LTYPE_LOGIN: 1067 return (utmpx_perform_login(li)); 1068 case LTYPE_LOGOUT: 1069 return (utmpx_perform_logout(li)); 1070 default: 1071 logit("%s: invalid type field", __func__); 1072 return (0); 1073 } 1074} 1075#endif /* USE_UTMPX */ 1076 1077 1078/** 1079 ** Low-level wtmp functions 1080 **/ 1081 1082#ifdef USE_WTMP 1083 1084/* 1085 * Write a wtmp entry direct to the end of the file 1086 * This is a slight modification of code in OpenBSD's logwtmp.c 1087 */ 1088static int 1089wtmp_write(struct logininfo *li, struct utmp *ut) 1090{ 1091 struct stat buf; 1092 int fd, ret = 1; 1093 1094 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) { 1095 logit("%s: problem writing %s: %s", __func__, 1096 WTMP_FILE, strerror(errno)); 1097 return (0); 1098 } 1099 if (fstat(fd, &buf) == 0) 1100 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) { 1101 ftruncate(fd, buf.st_size); 1102 logit("%s: problem writing %s: %s", __func__, 1103 WTMP_FILE, strerror(errno)); 1104 ret = 0; 1105 } 1106 close(fd); 1107 return (ret); 1108} 1109 1110static int 1111wtmp_perform_login(struct logininfo *li) 1112{ 1113 struct utmp ut; 1114 1115 construct_utmp(li, &ut); 1116 return (wtmp_write(li, &ut)); 1117} 1118 1119 1120static int 1121wtmp_perform_logout(struct logininfo *li) 1122{ 1123 struct utmp ut; 1124 1125 construct_utmp(li, &ut); 1126 return (wtmp_write(li, &ut)); 1127} 1128 1129 1130int 1131wtmp_write_entry(struct logininfo *li) 1132{ 1133 switch(li->type) { 1134 case LTYPE_LOGIN: 1135 return (wtmp_perform_login(li)); 1136 case LTYPE_LOGOUT: 1137 return (wtmp_perform_logout(li)); 1138 default: 1139 logit("%s: invalid type field", __func__); 1140 return (0); 1141 } 1142} 1143 1144 1145/* 1146 * Notes on fetching login data from wtmp/wtmpx 1147 * 1148 * Logouts are usually recorded with (amongst other things) a blank 1149 * username on a given tty line. However, some systems (HP-UX is one) 1150 * leave all fields set, but change the ut_type field to DEAD_PROCESS. 1151 * 1152 * Since we're only looking for logins here, we know that the username 1153 * must be set correctly. On systems that leave it in, we check for 1154 * ut_type==USER_PROCESS (indicating a login.) 1155 * 1156 * Portability: Some systems may set something other than USER_PROCESS 1157 * to indicate a login process. I don't know of any as I write. Also, 1158 * it's possible that some systems may both leave the username in 1159 * place and not have ut_type. 1160 */ 1161 1162/* return true if this wtmp entry indicates a login */ 1163static int 1164wtmp_islogin(struct logininfo *li, struct utmp *ut) 1165{ 1166 if (strncmp(li->username, ut->ut_name, 1167 MIN_SIZEOF(li->username, ut->ut_name)) == 0) { 1168# ifdef HAVE_TYPE_IN_UTMP 1169 if (ut->ut_type & USER_PROCESS) 1170 return (1); 1171# else 1172 return (1); 1173# endif 1174 } 1175 return (0); 1176} 1177 1178int 1179wtmp_get_entry(struct logininfo *li) 1180{ 1181 struct stat st; 1182 struct utmp ut; 1183 int fd, found = 0; 1184 1185 /* Clear the time entries in our logininfo */ 1186 li->tv_sec = li->tv_usec = 0; 1187 1188 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) { 1189 logit("%s: problem opening %s: %s", __func__, 1190 WTMP_FILE, strerror(errno)); 1191 return (0); 1192 } 1193 if (fstat(fd, &st) != 0) { 1194 logit("%s: couldn't stat %s: %s", __func__, 1195 WTMP_FILE, strerror(errno)); 1196 close(fd); 1197 return (0); 1198 } 1199 1200 /* Seek to the start of the last struct utmp */ 1201 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) { 1202 /* Looks like we've got a fresh wtmp file */ 1203 close(fd); 1204 return (0); 1205 } 1206 1207 while (!found) { 1208 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) { 1209 logit("%s: read of %s failed: %s", __func__, 1210 WTMP_FILE, strerror(errno)); 1211 close (fd); 1212 return (0); 1213 } 1214 if (wtmp_islogin(li, &ut) ) { 1215 found = 1; 1216 /* 1217 * We've already checked for a time in struct 1218 * utmp, in login_getlast() 1219 */ 1220# ifdef HAVE_TIME_IN_UTMP 1221 li->tv_sec = ut.ut_time; 1222# else 1223# if HAVE_TV_IN_UTMP 1224 li->tv_sec = ut.ut_tv.tv_sec; 1225# endif 1226# endif 1227 line_fullname(li->line, ut.ut_line, 1228 MIN_SIZEOF(li->line, ut.ut_line)); 1229# ifdef HAVE_HOST_IN_UTMP 1230 strlcpy(li->hostname, ut.ut_host, 1231 MIN_SIZEOF(li->hostname, ut.ut_host)); 1232# endif 1233 continue; 1234 } 1235 /* Seek back 2 x struct utmp */ 1236 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) { 1237 /* We've found the start of the file, so quit */ 1238 close(fd); 1239 return (0); 1240 } 1241 } 1242 1243 /* We found an entry. Tidy up and return */ 1244 close(fd); 1245 return (1); 1246} 1247# endif /* USE_WTMP */ 1248 1249 1250/** 1251 ** Low-level wtmpx functions 1252 **/ 1253 1254#ifdef USE_WTMPX 1255/* 1256 * Write a wtmpx entry direct to the end of the file 1257 * This is a slight modification of code in OpenBSD's logwtmp.c 1258 */ 1259static int 1260wtmpx_write(struct logininfo *li, struct utmpx *utx) 1261{ 1262#ifndef HAVE_UPDWTMPX 1263 struct stat buf; 1264 int fd, ret = 1; 1265 1266 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) { 1267 logit("%s: problem opening %s: %s", __func__, 1268 WTMPX_FILE, strerror(errno)); 1269 return (0); 1270 } 1271 1272 if (fstat(fd, &buf) == 0) 1273 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) { 1274 ftruncate(fd, buf.st_size); 1275 logit("%s: problem writing %s: %s", __func__, 1276 WTMPX_FILE, strerror(errno)); 1277 ret = 0; 1278 } 1279 close(fd); 1280 1281 return (ret); 1282#else 1283 updwtmpx(WTMPX_FILE, utx); 1284 return (1); 1285#endif 1286} 1287 1288 1289static int 1290wtmpx_perform_login(struct logininfo *li) 1291{ 1292 struct utmpx utx; 1293 1294 construct_utmpx(li, &utx); 1295 return (wtmpx_write(li, &utx)); 1296} 1297 1298 1299static int 1300wtmpx_perform_logout(struct logininfo *li) 1301{ 1302 struct utmpx utx; 1303 1304 construct_utmpx(li, &utx); 1305 return (wtmpx_write(li, &utx)); 1306} 1307 1308 1309int 1310wtmpx_write_entry(struct logininfo *li) 1311{ 1312 switch(li->type) { 1313 case LTYPE_LOGIN: 1314 return (wtmpx_perform_login(li)); 1315 case LTYPE_LOGOUT: 1316 return (wtmpx_perform_logout(li)); 1317 default: 1318 logit("%s: invalid type field", __func__); 1319 return (0); 1320 } 1321} 1322 1323/* Please see the notes above wtmp_islogin() for information about the 1324 next two functions */ 1325 1326/* Return true if this wtmpx entry indicates a login */ 1327static int 1328wtmpx_islogin(struct logininfo *li, struct utmpx *utx) 1329{ 1330 if (strncmp(li->username, utx->ut_user, 1331 MIN_SIZEOF(li->username, utx->ut_user)) == 0 ) { 1332# ifdef HAVE_TYPE_IN_UTMPX 1333 if (utx->ut_type == USER_PROCESS) 1334 return (1); 1335# else 1336 return (1); 1337# endif 1338 } 1339 return (0); 1340} 1341 1342 1343int 1344wtmpx_get_entry(struct logininfo *li) 1345{ 1346 struct stat st; 1347 struct utmpx utx; 1348 int fd, found=0; 1349 1350 /* Clear the time entries */ 1351 li->tv_sec = li->tv_usec = 0; 1352 1353 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) { 1354 logit("%s: problem opening %s: %s", __func__, 1355 WTMPX_FILE, strerror(errno)); 1356 return (0); 1357 } 1358 if (fstat(fd, &st) != 0) { 1359 logit("%s: couldn't stat %s: %s", __func__, 1360 WTMPX_FILE, strerror(errno)); 1361 close(fd); 1362 return (0); 1363 } 1364 1365 /* Seek to the start of the last struct utmpx */ 1366 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) { 1367 /* probably a newly rotated wtmpx file */ 1368 close(fd); 1369 return (0); 1370 } 1371 1372 while (!found) { 1373 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) { 1374 logit("%s: read of %s failed: %s", __func__, 1375 WTMPX_FILE, strerror(errno)); 1376 close (fd); 1377 return (0); 1378 } 1379 /* 1380 * Logouts are recorded as a blank username on a particular 1381 * line. So, we just need to find the username in struct utmpx 1382 */ 1383 if (wtmpx_islogin(li, &utx)) { 1384 found = 1; 1385# if defined(HAVE_TV_IN_UTMPX) 1386 li->tv_sec = utx.ut_tv.tv_sec; 1387# elif defined(HAVE_TIME_IN_UTMPX) 1388 li->tv_sec = utx.ut_time; 1389# endif 1390 line_fullname(li->line, utx.ut_line, sizeof(li->line)); 1391# if defined(HAVE_HOST_IN_UTMPX) 1392 strlcpy(li->hostname, utx.ut_host, 1393 MIN_SIZEOF(li->hostname, utx.ut_host)); 1394# endif 1395 continue; 1396 } 1397 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) { 1398 close(fd); 1399 return (0); 1400 } 1401 } 1402 1403 close(fd); 1404 return (1); 1405} 1406#endif /* USE_WTMPX */ 1407 1408/** 1409 ** Low-level libutil login() functions 1410 **/ 1411 1412#ifdef USE_LOGIN 1413static int 1414syslogin_perform_login(struct logininfo *li) 1415{ 1416 struct utmp *ut; 1417 1418 ut = xmalloc(sizeof(*ut)); 1419 construct_utmp(li, ut); 1420 login(ut); 1421 free(ut); 1422 1423 return (1); 1424} 1425 1426static int 1427syslogin_perform_logout(struct logininfo *li) 1428{ 1429# ifdef HAVE_LOGOUT 1430 char line[UT_LINESIZE]; 1431 1432 (void)line_stripname(line, li->line, sizeof(line)); 1433 1434 if (!logout(line)) 1435 logit("%s: logout() returned an error", __func__); 1436# ifdef HAVE_LOGWTMP 1437 else 1438 logwtmp(line, "", ""); 1439# endif 1440 /* FIXME: (ATL - if the need arises) What to do if we have 1441 * login, but no logout? what if logout but no logwtmp? All 1442 * routines are in libutil so they should all be there, 1443 * but... */ 1444# endif 1445 return (1); 1446} 1447 1448int 1449syslogin_write_entry(struct logininfo *li) 1450{ 1451 switch (li->type) { 1452 case LTYPE_LOGIN: 1453 return (syslogin_perform_login(li)); 1454 case LTYPE_LOGOUT: 1455 return (syslogin_perform_logout(li)); 1456 default: 1457 logit("%s: Invalid type field", __func__); 1458 return (0); 1459 } 1460} 1461#endif /* USE_LOGIN */ 1462 1463/* end of file log-syslogin.c */ 1464 1465/** 1466 ** Low-level lastlog functions 1467 **/ 1468 1469#ifdef USE_LASTLOG 1470 1471#if !defined(LASTLOG_WRITE_PUTUTXLINE) || !defined(HAVE_GETLASTLOGXBYNAME) 1472/* open the file (using filemode) and seek to the login entry */ 1473static int 1474lastlog_openseek(struct logininfo *li, int *fd, int filemode) 1475{ 1476 off_t offset; 1477 char lastlog_file[1024]; 1478 struct stat st; 1479 1480 if (stat(LASTLOG_FILE, &st) != 0) { 1481 logit("%s: Couldn't stat %s: %s", __func__, 1482 LASTLOG_FILE, strerror(errno)); 1483 return (0); 1484 } 1485 if (S_ISDIR(st.st_mode)) { 1486 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s", 1487 LASTLOG_FILE, li->username); 1488 } else if (S_ISREG(st.st_mode)) { 1489 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file)); 1490 } else { 1491 logit("%s: %.100s is not a file or directory!", __func__, 1492 LASTLOG_FILE); 1493 return (0); 1494 } 1495 1496 *fd = open(lastlog_file, filemode, 0600); 1497 if (*fd < 0) { 1498 debug("%s: Couldn't open %s: %s", __func__, 1499 lastlog_file, strerror(errno)); 1500 return (0); 1501 } 1502 1503 if (S_ISREG(st.st_mode)) { 1504 /* find this uid's offset in the lastlog file */ 1505 offset = (off_t) ((u_long)li->uid * sizeof(struct lastlog)); 1506 1507 if (lseek(*fd, offset, SEEK_SET) != offset) { 1508 logit("%s: %s->lseek(): %s", __func__, 1509 lastlog_file, strerror(errno)); 1510 close(*fd); 1511 return (0); 1512 } 1513 } 1514 1515 return (1); 1516} 1517#endif /* !LASTLOG_WRITE_PUTUTXLINE || !HAVE_GETLASTLOGXBYNAME */ 1518 1519#ifdef LASTLOG_WRITE_PUTUTXLINE 1520int 1521lastlog_write_entry(struct logininfo *li) 1522{ 1523 switch(li->type) { 1524 case LTYPE_LOGIN: 1525 return 1; /* lastlog written by pututxline */ 1526 default: 1527 logit("lastlog_write_entry: Invalid type field"); 1528 return 0; 1529 } 1530} 1531#else /* LASTLOG_WRITE_PUTUTXLINE */ 1532int 1533lastlog_write_entry(struct logininfo *li) 1534{ 1535 struct lastlog last; 1536 int fd; 1537 1538 switch(li->type) { 1539 case LTYPE_LOGIN: 1540 /* create our struct lastlog */ 1541 memset(&last, '\0', sizeof(last)); 1542 line_stripname(last.ll_line, li->line, sizeof(last.ll_line)); 1543 strlcpy(last.ll_host, li->hostname, 1544 MIN_SIZEOF(last.ll_host, li->hostname)); 1545 last.ll_time = li->tv_sec; 1546 1547 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT)) 1548 return (0); 1549 1550 /* write the entry */ 1551 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) { 1552 close(fd); 1553 logit("%s: Error writing to %s: %s", __func__, 1554 LASTLOG_FILE, strerror(errno)); 1555 return (0); 1556 } 1557 1558 close(fd); 1559 return (1); 1560 default: 1561 logit("%s: Invalid type field", __func__); 1562 return (0); 1563 } 1564} 1565#endif /* LASTLOG_WRITE_PUTUTXLINE */ 1566 1567#ifdef HAVE_GETLASTLOGXBYNAME 1568int 1569lastlog_get_entry(struct logininfo *li) 1570{ 1571 struct lastlogx l, *ll; 1572 1573 if ((ll = getlastlogxbyname(li->username, &l)) == NULL) { 1574 memset(&l, '\0', sizeof(l)); 1575 ll = &l; 1576 } 1577 line_fullname(li->line, ll->ll_line, sizeof(li->line)); 1578 strlcpy(li->hostname, ll->ll_host, 1579 MIN_SIZEOF(li->hostname, ll->ll_host)); 1580 li->tv_sec = ll->ll_tv.tv_sec; 1581 li->tv_usec = ll->ll_tv.tv_usec; 1582 return (1); 1583} 1584#else /* HAVE_GETLASTLOGXBYNAME */ 1585int 1586lastlog_get_entry(struct logininfo *li) 1587{ 1588 struct lastlog last; 1589 int fd, ret; 1590 1591 if (!lastlog_openseek(li, &fd, O_RDONLY)) 1592 return (0); 1593 1594 ret = atomicio(read, fd, &last, sizeof(last)); 1595 close(fd); 1596 1597 switch (ret) { 1598 case 0: 1599 memset(&last, '\0', sizeof(last)); 1600 /* FALLTHRU */ 1601 case sizeof(last): 1602 line_fullname(li->line, last.ll_line, sizeof(li->line)); 1603 strlcpy(li->hostname, last.ll_host, 1604 MIN_SIZEOF(li->hostname, last.ll_host)); 1605 li->tv_sec = last.ll_time; 1606 return (1); 1607 case -1: 1608 error("%s: Error reading from %s: %s", __func__, 1609 LASTLOG_FILE, strerror(errno)); 1610 return (0); 1611 default: 1612 error("%s: Error reading from %s: Expecting %d, got %d", 1613 __func__, LASTLOG_FILE, (int)sizeof(last), ret); 1614 return (0); 1615 } 1616 1617 /* NOTREACHED */ 1618 return (0); 1619} 1620#endif /* HAVE_GETLASTLOGXBYNAME */ 1621#endif /* USE_LASTLOG */ 1622 1623#if defined(USE_UTMPX) && defined(HAVE_SETUTXDB) && \ 1624 defined(UTXDB_LASTLOGIN) && defined(HAVE_GETUTXUSER) 1625int 1626utmpx_get_entry(struct logininfo *li) 1627{ 1628 struct utmpx *utx; 1629 1630 if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) 1631 return (0); 1632 utx = getutxuser(li->username); 1633 if (utx == NULL) { 1634 endutxent(); 1635 return (0); 1636 } 1637 1638 line_fullname(li->line, utx->ut_line, 1639 MIN_SIZEOF(li->line, utx->ut_line)); 1640 strlcpy(li->hostname, utx->ut_host, 1641 MIN_SIZEOF(li->hostname, utx->ut_host)); 1642 li->tv_sec = utx->ut_tv.tv_sec; 1643 li->tv_usec = utx->ut_tv.tv_usec; 1644 endutxent(); 1645 return (1); 1646} 1647#endif /* USE_UTMPX && HAVE_SETUTXDB && UTXDB_LASTLOGIN && HAVE_GETUTXUSER */ 1648 1649#ifdef USE_BTMP 1650 /* 1651 * Logs failed login attempts in _PATH_BTMP if that exists. 1652 * The most common login failure is to give password instead of username. 1653 * So the _PATH_BTMP file checked for the correct permission, so that 1654 * only root can read it. 1655 */ 1656 1657void 1658record_failed_login(const char *username, const char *hostname, 1659 const char *ttyn) 1660{ 1661 int fd; 1662 struct utmp ut; 1663 struct sockaddr_storage from; 1664 socklen_t fromlen = sizeof(from); 1665 struct sockaddr_in *a4; 1666 struct sockaddr_in6 *a6; 1667 time_t t; 1668 struct stat fst; 1669 1670 if (geteuid() != 0) 1671 return; 1672 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) { 1673 debug("Unable to open the btmp file %s: %s", _PATH_BTMP, 1674 strerror(errno)); 1675 return; 1676 } 1677 if (fstat(fd, &fst) < 0) { 1678 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP, 1679 strerror(errno)); 1680 goto out; 1681 } 1682 if((fst.st_mode & (S_IXGRP | S_IRWXO)) || (fst.st_uid != 0)){ 1683 logit("Excess permission or bad ownership on file %s", 1684 _PATH_BTMP); 1685 goto out; 1686 } 1687 1688 memset(&ut, 0, sizeof(ut)); 1689 /* strncpy because we don't necessarily want nul termination */ 1690 strncpy(ut.ut_user, username, sizeof(ut.ut_user)); 1691 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line)); 1692 1693 time(&t); 1694 ut.ut_time = t; /* ut_time is not always a time_t */ 1695 ut.ut_type = LOGIN_PROCESS; 1696 ut.ut_pid = getpid(); 1697 1698 /* strncpy because we don't necessarily want nul termination */ 1699 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host)); 1700 1701 if (packet_connection_is_on_socket() && 1702 getpeername(packet_get_connection_in(), 1703 (struct sockaddr *)&from, &fromlen) == 0) { 1704 ipv64_normalise_mapped(&from, &fromlen); 1705 if (from.ss_family == AF_INET) { 1706 a4 = (struct sockaddr_in *)&from; 1707 memcpy(&ut.ut_addr, &(a4->sin_addr), 1708 MIN_SIZEOF(ut.ut_addr, a4->sin_addr)); 1709 } 1710#ifdef HAVE_ADDR_V6_IN_UTMP 1711 if (from.ss_family == AF_INET6) { 1712 a6 = (struct sockaddr_in6 *)&from; 1713 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr), 1714 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr)); 1715 } 1716#endif 1717 } 1718 1719 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut)) 1720 error("Failed to write to %s: %s", _PATH_BTMP, 1721 strerror(errno)); 1722 1723out: 1724 close(fd); 1725} 1726#endif /* USE_BTMP */ 1727