]> andersk Git - openssh.git/blob - login.c
- Prevent typedefs from being compiled more than once. Report from
[openssh.git] / login.c
1 /*
2  * 
3  * login.c
4  * 
5  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6  * 
7  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8  *                    All rights reserved
9  * 
10  * Created: Fri Mar 24 14:51:08 1995 ylo
11  * 
12  * This file performs some of the things login(1) normally does.  We cannot
13  * easily use something like login -p -h host -f user, because there are
14  * several different logins around, and it is hard to determined what kind of
15  * login the current system has.  Also, we want to be able to execute commands
16  * on a tty.
17  * 
18  */
19
20 #include "includes.h"
21 RCSID("$Id$");
22
23 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
24 # include <utmpx.h>
25 #endif
26 #ifdef HAVE_UTMP_H
27 # include <utmp.h>
28 #endif
29 #include "ssh.h"
30
31 #ifdef HAVE_UTIL_H
32 # include <util.h>
33 #endif
34 #ifdef HAVE_LASTLOG_H
35 # include <lastlog.h>
36 #endif
37 #ifdef HAVE_LOGIN_H
38 # include <login.h>
39 #endif
40
41 /*
42  * Returns the time when the user last logged in.  Returns 0 if the
43  * information is not available.  This must be called before record_login.
44  * The host the user logged in from will be returned in buf.
45  */
46
47 /*
48  * Returns the time when the user last logged in (or 0 if no previous login
49  * is found).  The name of the host used last time is returned in buf.
50  */
51
52 unsigned long 
53 get_last_login_time(uid_t uid, const char *logname,
54                     char *buf, unsigned int bufsize)
55 {
56 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
57         struct lastlog ll;
58         char *lastlog;
59         int fd;
60
61         lastlog = _PATH_LASTLOG;
62         buf[0] = '\0';
63
64         fd = open(lastlog, O_RDONLY);
65         if (fd < 0)
66                 return 0;
67         lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
68         if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
69                 close(fd);
70                 return 0;
71         }
72         close(fd);
73         if (bufsize > sizeof(ll.ll_host) + 1)
74                 bufsize = sizeof(ll.ll_host) + 1;
75         strncpy(buf, ll.ll_host, bufsize - 1);
76         buf[bufsize - 1] = 0;
77         return ll.ll_time;
78
79 #else /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
80         /* Look in wtmp for the last login */
81         struct utmp  wt;
82         char        *wt_file = _PATH_WTMP;
83         int         fd1;
84         unsigned long t = 0;
85
86         if ( (fd1 = open(wt_file, O_RDONLY)) < 0 ) {
87                 error("Couldn't open %.100s to find last login time.", wt_file);
88                 return 0;
89         }
90
91         /* seek to last record of file */
92         lseek(fd1, (off_t)(0-sizeof(struct utmp)), SEEK_END);
93
94         /* loop through wtmp for our last user login record */
95         do {
96                 if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) {
97                         close(fd1);
98                         return 0;
99                 }
100
101                 if ( wt.ut_type == USER_PROCESS) {
102                         if ( !strncmp(logname, wt.ut_user, 8) ) {
103                                 t = (unsigned long) wt.ut_time;
104 #ifdef HAVE_HOST_IN_UTMP
105                                 if (bufsize > sizeof(wt.ut_host) + 1)
106                                 bufsize = sizeof(wt.ut_host) + 1;
107                                 strncpy(buf, wt.ut_host, bufsize - 1);
108                                 buf[bufsize - 1] = 0;
109 #else /* HAVE_HOST_IN_UTMP */
110                                 buf[0] = 0;
111 #endif /* HAVE_HOST_IN_UTMP */
112                         }
113                 }
114
115                 if (lseek(fd1, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1)
116                         break;
117         } while (t == 0);
118
119         return t;
120 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
121 }
122
123 /*
124  * Records that the user has logged in.  I these parts of operating systems
125  * were more standardized.
126  */
127
128 void 
129 record_login(int pid, const char *ttyname, const char *user, uid_t uid,
130              const char *host, struct sockaddr_in * addr)
131 {
132 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
133         struct lastlog ll;
134         char *lastlog;
135 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
136         struct utmp u;
137 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
138         struct utmpx utx;
139 #endif
140
141         /* Construct an utmp/wtmp entry. */
142         memset(&u, 0, sizeof(u));
143         strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
144 #if defined(HAVE_ID_IN_UTMP)
145         strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id));
146 #endif /* defined(HAVE_ID_IN_UTMP) */ 
147         strncpy(u.ut_name, user, sizeof(u.ut_name));
148 #if defined(HAVE_TV_IN_UTMP)
149         (void)gettimeofday(&u.ut_tv, NULL);
150 #else /* defined(HAVE_TV_IN_UTMP) */
151         u.ut_time = time(NULL);
152 #endif /* defined(HAVE_TV_IN_UTMP) */
153 #if defined(HAVE_PID_IN_UTMP)
154         u.ut_pid = (pid_t)pid;
155 #endif /* HAVE_PID_IN_UTMP */
156 #if defined(HAVE_TYPE_IN_UTMP)
157         u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
158 #endif /* HAVE_TYPE_IN_UTMP */
159 #if defined(HAVE_HOST_IN_UTMP)
160         strncpy(u.ut_host, host, sizeof(u.ut_host));
161 #endif
162
163 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
164         memset(&utx, 0, sizeof(utx));
165         strncpy(utx.ut_user, user, sizeof(utx.ut_name));
166         strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line));
167         strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id));
168         utx.ut_pid = (pid_t)pid;
169         (void)gettimeofday(&utx.ut_tv, NULL);
170         utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
171 # ifdef HAVE_HOST_IN_UTMPX
172 #  ifdef HAVE_SYSLEN_IN_UTMPX
173         utx.ut_syslen = strlen(host);
174         strncpy(utx.ut_host, host, utx.ut_syslen);
175 #  else
176         strncpy(utx.ut_host, host, sizeof(utx.ut_host));
177 #  endif /* HAVE_SYSLEN_IN_UTMPX */
178 # endif
179 #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
180
181 /*#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) && !defined(HAVE_LOGIN)*/
182 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
183         login(&u, &utx);
184 #else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
185         login(&u);
186 #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
187
188 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
189         lastlog = _PATH_LASTLOG;
190
191         /* Update lastlog unless actually recording a logout. */
192         if (strcmp(user, "") != 0) {
193                 int fd;
194                 /*
195                  * It is safer to bzero the lastlog structure first because
196                  * some systems might have some extra fields in it (e.g. SGI)
197                  */
198                 memset(&ll, 0, sizeof(ll));
199
200                 /* Update lastlog. */
201                 ll.ll_time = time(NULL);
202                 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
203                 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
204                 fd = open(lastlog, O_RDWR);
205                 if (fd >= 0) {
206                         lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
207                         if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
208                                 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
209                         close(fd);
210                 }
211         }
212 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
213 }
214
215 /* Records that the user has logged out. */
216
217 void 
218 record_logout(int pid, const char *ttyname)
219 {
220 #ifdef HAVE_LIBUTIL_LOGIN
221         const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
222         if (logout(line))
223                 logwtmp(line, "", "");
224 #else /* HAVE_LIBUTIL_LOGIN */
225         record_login(pid, ttyname, "", -1, "", NULL);
226 #endif /* HAVE_LIBUTIL_LOGIN */
227 }
This page took 0.055611 seconds and 5 git commands to generate.