1/************************************************************************ 2* Copyright 1995 by Wietse Venema. All rights reserved. Some individual 3* files may be covered by other copyrights. 4* 5* This material was originally written and compiled by Wietse Venema at 6* Eindhoven University of Technology, The Netherlands, in 1990, 1991, 7* 1992, 1993, 1994 and 1995. 8* 9* Redistribution and use in source and binary forms, with or without 10* modification, are permitted provided that this entire copyright notice 11* is duplicated in all such copies. 12* 13* This software is provided "as is" and without any expressed or implied 14* warranties, including, without limitation, the implied warranties of 15* merchantibility and fitness for any particular purpose. 16************************************************************************/ 17/* Author: Wietse Venema <wietse@wzv.win.tue.nl> */ 18 19#include "login_locl.h" 20 21RCSID("$Id$"); 22 23/* utmpx_login - update utmp and wtmp after login */ 24 25#ifndef HAVE_UTMPX_H 26int utmpx_login(char *line, const char *user, const char *host) { return 0; } 27#else 28 29static void 30utmpx_update(struct utmpx *ut, char *line, const char *user, const char *host) 31{ 32 struct timeval tmp; 33 char *clean_tty = clean_ttyname(line); 34 35 strncpy(ut->ut_line, clean_tty, sizeof(ut->ut_line)); 36#ifdef HAVE_STRUCT_UTMPX_UT_ID 37 strncpy(ut->ut_id, make_id(clean_tty), sizeof(ut->ut_id)); 38#endif 39 strncpy(ut->ut_user, user, sizeof(ut->ut_user)); 40 shrink_hostname (host, ut->ut_host, sizeof(ut->ut_host)); 41#ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN 42 ut->ut_syslen = strlen(host) + 1; 43 if (ut->ut_syslen > sizeof(ut->ut_host)) 44 ut->ut_syslen = sizeof(ut->ut_host); 45#endif 46 ut->ut_type = USER_PROCESS; 47 gettimeofday (&tmp, 0); 48 ut->ut_tv.tv_sec = tmp.tv_sec; 49 ut->ut_tv.tv_usec = tmp.tv_usec; 50 pututxline(ut); 51#ifdef WTMPX_FILE 52 updwtmpx(WTMPX_FILE, ut); 53#elif defined(WTMP_FILE) 54 { /* XXX should be removed, just drop wtmp support */ 55 struct utmp utmp; 56 int fd; 57 58 prepare_utmp (&utmp, line, user, host); 59 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { 60 write(fd, &utmp, sizeof(struct utmp)); 61 close(fd); 62 } 63 } 64#endif 65} 66 67int 68utmpx_login(char *line, const char *user, const char *host) 69{ 70 struct utmpx *ut, save_ut; 71 pid_t mypid = getpid(); 72 int ret = (-1); 73 74 /* 75 * SYSV4 ttymon and login use tty port names with the "/dev/" prefix 76 * stripped off. Rlogind and telnetd, on the other hand, make utmpx 77 * entries with device names like /dev/pts/nnn. We therefore cannot use 78 * getutxline(). Return nonzero if no utmp entry was found with our own 79 * process ID for a login or user process. 80 */ 81 82 while ((ut = getutxent())) { 83 /* Try to find a reusable entry */ 84 if (ut->ut_pid == mypid 85 && ( ut->ut_type == INIT_PROCESS 86 || ut->ut_type == LOGIN_PROCESS 87 || ut->ut_type == USER_PROCESS)) { 88 save_ut = *ut; 89 utmpx_update(&save_ut, line, user, host); 90 ret = 0; 91 break; 92 } 93 } 94 if (ret == -1) { 95 /* Grow utmpx file by one record. */ 96 struct utmpx newut; 97 memset(&newut, 0, sizeof(newut)); 98 newut.ut_pid = mypid; 99 utmpx_update(&newut, line, user, host); 100 ret = 0; 101 } 102 endutxent(); 103 return (ret); 104} 105#endif /* HAVE_UTMPX_H */ 106