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