]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/heimdal/appl/login/utmpx_login.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / crypto / heimdal / appl / login / utmpx_login.c
1 /* Author: Wietse Venema <wietse@wzv.win.tue.nl> */
2
3 #include "login_locl.h"
4
5 RCSID("$Id: utmpx_login.c,v 1.24 1999/08/04 17:03:15 assar Exp $");
6
7 /* utmpx_login - update utmp and wtmp after login */
8
9 #ifndef HAVE_UTMPX_H
10 int utmpx_login(char *line, const char *user, const char *host) { return 0; }
11 #else
12
13 static void
14 utmpx_update(struct utmpx *ut, char *line, const char *user, const 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, const char *user, const char *host)
53 {
54     struct utmpx *ut, save_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             save_ut = *ut;
73             utmpx_update(&save_ut, line, user, host);
74             ret = 0;
75             break;
76         }
77     }
78     if (ret == -1) {
79         /* Grow utmpx file by one record. */
80         struct utmpx newut;
81         memset(&newut, 0, sizeof(newut));
82         newut.ut_pid = mypid;
83         utmpx_update(&newut, line, user, host);
84         ret = 0;
85     }
86     endutxent();
87     return (ret);
88 }
89 #endif /* HAVE_UTMPX_H */