52d0e6abe116c7670206ae3a99f6ca06147155a2
[games.git] / lib / pam_module / pam_lastlog / pam_lastlog.c
1 /*-
2  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2001 Mark R V Murray
5  * All rights reserved.
6  * Copyright (c) 2001 Networks Associates Technology, Inc.
7  * All rights reserved.
8  * Copyright (c) 2004 Joe R. Doupnik
9  * All rights reserved.
10  *
11  * Portions of this software were developed for the FreeBSD Project by
12  * ThinkSec AS and NAI Labs, the Security Research Division of Network
13  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
14  * ("CBOSS"), as part of the DARPA CHATS research program.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. The name of the author may not be used to endorse or promote
25  *    products derived from this software without specific prior written
26  *    permission.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  * $FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.23 2007/07/22 15:17:29 des Exp $
44  */
45
46 #define _BSD_SOURCE
47
48 #include <sys/param.h>
49
50 #include <fcntl.h>
51 #include <libutil.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <syslog.h>
58 #include <time.h>
59 #include <unistd.h>
60
61 #define PAM_SM_SESSION
62
63 #include <security/pam_appl.h>
64 #include <security/pam_modules.h>
65 #include <security/pam_mod_misc.h>
66
67 #define SUPPORT_UTMP 1
68 #define SUPPORT_UTMPX 1
69
70 #ifdef SUPPORT_UTMP
71 #include <utmp.h>
72 static void doutmp(const char *, const char *, const char *,
73     const struct timeval *);
74 static void dolastlog(pam_handle_t *, int, const struct passwd *, const char *,
75     const char *, const struct timeval *);
76 #endif
77
78 #ifdef SUPPORT_UTMPX
79 #include <utmpx.h>
80 static void doutmpx(const char *, const char *, const char *,
81     const struct sockaddr_storage *ss, const struct timeval *);
82 static void dolastlogx(pam_handle_t *, int, const struct passwd *, const char *,
83     const char *, const struct sockaddr_storage *ss, const struct timeval *);
84 #endif
85
86 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
87 static void domsg(pam_handle_t *, time_t, const char *, size_t, const char *,
88     size_t);
89 #endif
90
91 static void
92 logit(int level, const char *fmt, ...)
93 {
94         va_list ap;
95
96         openlog("pam_lastlog", LOG_PID, LOG_AUTHPRIV);
97         va_start(ap, fmt);
98         vsyslog(level, fmt, ap);
99         va_end(ap);
100         closelog();
101 }
102
103
104 PAM_EXTERN int
105 pam_sm_open_session(pam_handle_t *pamh, int flags,
106     int argc __unused, const char *argv[] __unused)
107 {
108         struct passwd *pwd, pwres;
109         struct timeval now;
110         const char *user, *rhost, *tty;
111         const void *vrhost, *vtty;
112         int pam_err;
113         int quiet;
114         char pwbuf[1024];
115 #ifdef LOGIN_CAP
116         login_cap_t *lc;
117 #endif
118
119         pam_err = pam_get_user(pamh, &user, NULL);
120         if (pam_err != PAM_SUCCESS)
121                 return pam_err;
122
123         if (user == NULL ||
124             getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
125             pwd == NULL)
126                 return PAM_SERVICE_ERR;
127
128         PAM_LOG("Got user: %s", user);
129
130         pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost);
131         if (pam_err != PAM_SUCCESS)
132                 goto err;
133         rhost = (const char *)vrhost;
134
135         pam_err = pam_get_item(pamh, PAM_TTY, &vtty);
136         if (pam_err != PAM_SUCCESS)
137                 goto err;
138         tty = (const char *)vtty;
139
140         if (tty == NULL) {
141                 pam_err = PAM_SERVICE_ERR;
142                 goto err;
143         }
144
145         if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
146                 tty = tty + strlen(_PATH_DEV);
147
148         if (*tty == '\0') {
149                 pam_err = PAM_SERVICE_ERR;
150                 goto err;
151         }
152
153         (void)gettimeofday(&now, NULL);
154
155         if ((flags & PAM_SILENT) != 0)
156                 quiet = 1;
157         else {
158 #ifdef LOGIN_CAP
159                 lc = login_getpwclass(pwd);
160                 quiet = login_getcapbool(lc, "hushlogin", 0);
161                 login_close(lc);
162 #else
163                 quiet = 0;
164 #endif
165         }
166 #ifdef SUPPORT_UTMPX
167         dolastlogx(pamh, quiet, pwd, rhost, tty, NULL, &now);
168         doutmpx(user, rhost, tty, NULL, &now);
169         quiet = 1;
170 #endif
171 #ifdef SUPPORT_UTMP
172         doutmp(user, rhost, tty, &now);
173         dolastlog(pamh, quiet, pwd, rhost, tty, &now);
174 #endif
175 err:
176         if (openpam_get_option(pamh, "no_fail"))
177                 return PAM_SUCCESS;
178         return pam_err;
179 }
180
181 PAM_EXTERN int
182 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
183     int argc __unused, const char *argv[] __unused)
184 {
185         const void *vtty;
186         const char *tty;
187
188         pam_get_item(pamh, PAM_TTY, &vtty);
189         if (vtty == NULL)
190                 return PAM_SERVICE_ERR;
191         tty = (const char *)vtty;
192
193         if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
194                 tty = tty + strlen(_PATH_DEV);
195
196         if (*tty == '\0')
197                 return PAM_SERVICE_ERR;
198
199 #ifdef SUPPORT_UTMPX
200         if (logoutx(tty, 0, DEAD_PROCESS))
201                 logwtmpx(tty, "", "", 0, DEAD_PROCESS);
202         else
203                 logit(LOG_NOTICE, "%s(): no utmpx record for %s",
204                     __func__, tty);
205 #endif
206
207 #ifdef SUPPORT_UTMP
208         if (logout(tty))
209                 logwtmp(tty, "", "");
210         else
211                 logit(LOG_NOTICE, "%s(): no utmp record for %s",
212                     __func__, tty);
213 #endif
214         return PAM_SUCCESS;
215 }
216
217 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
218 static void
219 domsg(pam_handle_t *pamh, time_t t, const char *host, size_t hsize,
220     const char *line, size_t lsize)
221 {
222         char buf[MAXHOSTNAMELEN + 32], *promptresp = NULL;
223         int pam_err;
224
225         if (*host) {
226                 (void)snprintf(buf, sizeof(buf), "from %.*s ",
227                     (int)hsize, host);
228                 host = buf;
229         }
230
231         pam_err = pam_prompt(pamh, PAM_TEXT_INFO, &promptresp,
232             "Last login: %.24s %son %.*s\n", ctime(&t), host, (int)lsize, line);
233
234         if (pam_err == PAM_SUCCESS && promptresp)
235                 free(promptresp);
236 }
237 #endif
238
239 #ifdef SUPPORT_UTMPX
240 static void
241 doutmpx(const char *username, const char *hostname, const char *tty,
242     const struct sockaddr_storage *ss, const struct timeval *now)
243 {
244         struct utmpx utmpx;
245         const char *t;
246
247         memset((void *)&utmpx, 0, sizeof(utmpx));
248         utmpx.ut_tv = *now;
249         (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
250         if (hostname) {
251                 (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
252                 if (ss)
253                         utmpx.ut_ss = *ss;
254         }
255         (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
256         utmpx.ut_type = USER_PROCESS;
257         utmpx.ut_pid = getpid();
258         t = tty + strlen(tty);
259         if ((size_t)(t - tty) >= sizeof(utmpx.ut_id)) {
260                 (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
261                     sizeof(utmpx.ut_id));
262         } else {
263                 (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
264         }
265         if (pututxline(&utmpx) == NULL)
266                 logit(LOG_NOTICE, "Cannot update utmpx %m");
267         endutxent();
268         if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
269                 logit(LOG_NOTICE, "Cannot update wtmpx %m");
270 }
271
272 static void
273 dolastlogx(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
274     const char *hostname __unused, const char *tty __unused,
275     const struct sockaddr_storage *ss __unused,
276     const struct timeval *now __unused)
277 {
278         struct lastlogx ll;
279         if (!quiet) {
280             if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL)
281                     domsg(pamh, (time_t)ll.ll_tv.tv_sec, ll.ll_host,
282                         sizeof(ll.ll_host), ll.ll_line,
283                         sizeof(ll.ll_line));
284         }
285 #if 0
286         ll.ll_tv = *now;
287         (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
288
289         if (hostname)
290                 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
291         else
292                 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
293
294         if (ss)
295                 ll.ll_ss = *ss;
296         else
297                 (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
298
299         if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
300                 logit(LOG_NOTICE, "Cannot update lastlogx %m");
301         PAM_LOG("Login recorded in %s", _PATH_LASTLOGX);
302 #endif
303 }
304 #endif
305
306 #ifdef SUPPORT_UTMP
307 static void
308 doutmp(const char *username, const char *hostname, const char *tty,
309     const struct timeval *now)
310 {
311         struct utmp utmp;
312
313         (void)memset((void *)&utmp, 0, sizeof(utmp));
314         utmp.ut_time = now->tv_sec;
315         (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
316         if (hostname)
317                 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
318         (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
319         login(&utmp);
320 }
321
322 static void
323 dolastlog(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
324     const char *hostname, const char *tty, const struct timeval *now)
325 {
326         struct lastlog ll;
327         int fd;
328
329         if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) == -1) {
330                 logit(LOG_NOTICE, "Cannot open `%s' %m", _PATH_LASTLOG);
331                 return;
332         }
333         (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
334
335         if (!quiet) {
336                 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
337                     ll.ll_time != 0)
338                         domsg(pamh, ll.ll_time, ll.ll_host,
339                             sizeof(ll.ll_host), ll.ll_line,
340                             sizeof(ll.ll_line));
341                 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
342         }
343
344         ll.ll_time = now->tv_sec;
345         (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
346
347         if (hostname)
348                 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
349         else
350                 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
351
352         (void)write(fd, &ll, sizeof(ll));
353         (void)close(fd);
354
355         PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
356 }
357 #endif
358
359 PAM_MODULE_ENTRY("pam_lastlog");
360