Path: blob/main/crypto/heimdal/appl/login/utmpx_login.c
96309 views
/************************************************************************1* Copyright 1995 by Wietse Venema. All rights reserved. Some individual2* files may be covered by other copyrights.3*4* This material was originally written and compiled by Wietse Venema at5* Eindhoven University of Technology, The Netherlands, in 1990, 1991,6* 1992, 1993, 1994 and 1995.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that this entire copyright notice10* is duplicated in all such copies.11*12* This software is provided "as is" and without any expressed or implied13* warranties, including, without limitation, the implied warranties of14* merchantibility and fitness for any particular purpose.15************************************************************************/16/* Author: Wietse Venema <[email protected]> */1718#include "login_locl.h"1920RCSID("$Id$");2122/* utmpx_login - update utmp and wtmp after login */2324#ifndef HAVE_UTMPX_H25int utmpx_login(char *line, const char *user, const char *host) { return 0; }26#else2728static void29utmpx_update(struct utmpx *ut, char *line, const char *user, const char *host)30{31struct timeval tmp;32char *clean_tty = clean_ttyname(line);3334strncpy(ut->ut_line, clean_tty, sizeof(ut->ut_line));35#ifdef HAVE_STRUCT_UTMPX_UT_ID36strncpy(ut->ut_id, make_id(clean_tty), sizeof(ut->ut_id));37#endif38strncpy(ut->ut_user, user, sizeof(ut->ut_user));39shrink_hostname (host, ut->ut_host, sizeof(ut->ut_host));40#ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN41ut->ut_syslen = strlen(host) + 1;42if (ut->ut_syslen > sizeof(ut->ut_host))43ut->ut_syslen = sizeof(ut->ut_host);44#endif45ut->ut_type = USER_PROCESS;46gettimeofday (&tmp, 0);47ut->ut_tv.tv_sec = tmp.tv_sec;48ut->ut_tv.tv_usec = tmp.tv_usec;49pututxline(ut);50#ifdef WTMPX_FILE51updwtmpx(WTMPX_FILE, ut);52#elif defined(WTMP_FILE)53{ /* XXX should be removed, just drop wtmp support */54struct utmp utmp;55int fd;5657prepare_utmp (&utmp, line, user, host);58if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {59write(fd, &utmp, sizeof(struct utmp));60close(fd);61}62}63#endif64}6566int67utmpx_login(char *line, const char *user, const char *host)68{69struct utmpx *ut, save_ut;70pid_t mypid = getpid();71int ret = (-1);7273/*74* SYSV4 ttymon and login use tty port names with the "/dev/" prefix75* stripped off. Rlogind and telnetd, on the other hand, make utmpx76* entries with device names like /dev/pts/nnn. We therefore cannot use77* getutxline(). Return nonzero if no utmp entry was found with our own78* process ID for a login or user process.79*/8081while ((ut = getutxent())) {82/* Try to find a reusable entry */83if (ut->ut_pid == mypid84&& ( ut->ut_type == INIT_PROCESS85|| ut->ut_type == LOGIN_PROCESS86|| ut->ut_type == USER_PROCESS)) {87save_ut = *ut;88utmpx_update(&save_ut, line, user, host);89ret = 0;90break;91}92}93if (ret == -1) {94/* Grow utmpx file by one record. */95struct utmpx newut;96memset(&newut, 0, sizeof(newut));97newut.ut_pid = mypid;98utmpx_update(&newut, line, user, host);99ret = 0;100}101endutxent();102return (ret);103}104#endif /* HAVE_UTMPX_H */105106107