1b528cefcSMark Murray /* Author: Wietse Venema <wietse@wzv.win.tue.nl> */ 2b528cefcSMark Murray 3b528cefcSMark Murray #include "login_locl.h" 4b528cefcSMark Murray 5adb0ddaeSAssar Westerlund RCSID("$Id: utmpx_login.c,v 1.25 2001/02/08 16:08:47 assar Exp $"); 6b528cefcSMark Murray 7b528cefcSMark Murray /* utmpx_login - update utmp and wtmp after login */ 8b528cefcSMark Murray 9b528cefcSMark Murray #ifndef HAVE_UTMPX_H 10b528cefcSMark Murray int utmpx_login(char *line, const char *user, const char *host) { return 0; } 11b528cefcSMark Murray #else 12b528cefcSMark Murray 13b528cefcSMark Murray static void 14b528cefcSMark Murray utmpx_update(struct utmpx *ut, char *line, const char *user, const char *host) 15b528cefcSMark Murray { 16b528cefcSMark Murray struct timeval tmp; 17b528cefcSMark Murray char *clean_tty = clean_ttyname(line); 18b528cefcSMark Murray 19b528cefcSMark Murray strncpy(ut->ut_line, clean_tty, sizeof(ut->ut_line)); 20b528cefcSMark Murray #ifdef HAVE_STRUCT_UTMPX_UT_ID 21b528cefcSMark Murray strncpy(ut->ut_id, make_id(clean_tty), sizeof(ut->ut_id)); 22b528cefcSMark Murray #endif 23b528cefcSMark Murray strncpy(ut->ut_user, user, sizeof(ut->ut_user)); 24adb0ddaeSAssar Westerlund shrink_hostname (host, ut->ut_host, sizeof(ut->ut_host)); 25b528cefcSMark Murray #ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN 26b528cefcSMark Murray ut->ut_syslen = strlen(host) + 1; 27b528cefcSMark Murray if (ut->ut_syslen > sizeof(ut->ut_host)) 28b528cefcSMark Murray ut->ut_syslen = sizeof(ut->ut_host); 29b528cefcSMark Murray #endif 30b528cefcSMark Murray ut->ut_type = USER_PROCESS; 31b528cefcSMark Murray gettimeofday (&tmp, 0); 32b528cefcSMark Murray ut->ut_tv.tv_sec = tmp.tv_sec; 33b528cefcSMark Murray ut->ut_tv.tv_usec = tmp.tv_usec; 34b528cefcSMark Murray pututxline(ut); 35b528cefcSMark Murray #ifdef WTMPX_FILE 36b528cefcSMark Murray updwtmpx(WTMPX_FILE, ut); 37b528cefcSMark Murray #elif defined(WTMP_FILE) 38b528cefcSMark Murray { 39b528cefcSMark Murray struct utmp utmp; 40b528cefcSMark Murray int fd; 41b528cefcSMark Murray 42b528cefcSMark Murray prepare_utmp (&utmp, line, user, host); 43b528cefcSMark Murray if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { 44b528cefcSMark Murray write(fd, &utmp, sizeof(struct utmp)); 45b528cefcSMark Murray close(fd); 46b528cefcSMark Murray } 47b528cefcSMark Murray } 48b528cefcSMark Murray #endif 49b528cefcSMark Murray } 50b528cefcSMark Murray 51b528cefcSMark Murray int 52b528cefcSMark Murray utmpx_login(char *line, const char *user, const char *host) 53b528cefcSMark Murray { 54b528cefcSMark Murray struct utmpx *ut, save_ut; 55b528cefcSMark Murray pid_t mypid = getpid(); 56b528cefcSMark Murray int ret = (-1); 57b528cefcSMark Murray 58b528cefcSMark Murray /* 59b528cefcSMark Murray * SYSV4 ttymon and login use tty port names with the "/dev/" prefix 60b528cefcSMark Murray * stripped off. Rlogind and telnetd, on the other hand, make utmpx 61b528cefcSMark Murray * entries with device names like /dev/pts/nnn. We therefore cannot use 62b528cefcSMark Murray * getutxline(). Return nonzero if no utmp entry was found with our own 63b528cefcSMark Murray * process ID for a login or user process. 64b528cefcSMark Murray */ 65b528cefcSMark Murray 66b528cefcSMark Murray while ((ut = getutxent())) { 67b528cefcSMark Murray /* Try to find a reusable entry */ 68b528cefcSMark Murray if (ut->ut_pid == mypid 69b528cefcSMark Murray && ( ut->ut_type == INIT_PROCESS 70b528cefcSMark Murray || ut->ut_type == LOGIN_PROCESS 71b528cefcSMark Murray || ut->ut_type == USER_PROCESS)) { 72b528cefcSMark Murray save_ut = *ut; 73b528cefcSMark Murray utmpx_update(&save_ut, line, user, host); 74b528cefcSMark Murray ret = 0; 75b528cefcSMark Murray break; 76b528cefcSMark Murray } 77b528cefcSMark Murray } 78b528cefcSMark Murray if (ret == -1) { 79b528cefcSMark Murray /* Grow utmpx file by one record. */ 80b528cefcSMark Murray struct utmpx newut; 81b528cefcSMark Murray memset(&newut, 0, sizeof(newut)); 82b528cefcSMark Murray newut.ut_pid = mypid; 83b528cefcSMark Murray utmpx_update(&newut, line, user, host); 84b528cefcSMark Murray ret = 0; 85b528cefcSMark Murray } 86b528cefcSMark Murray endutxent(); 87b528cefcSMark Murray return (ret); 88b528cefcSMark Murray } 89b528cefcSMark Murray #endif /* HAVE_UTMPX_H */ 90