pam_lastlog.c revision 1.2 1 1.2 christos /* $NetBSD: pam_lastlog.c,v 1.2 2004/12/12 08:18:46 christos Exp $ */
2 1.2 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos * Copyright (c) 2001 Mark R V Murray
7 1.1 christos * All rights reserved.
8 1.1 christos * Copyright (c) 2001 Networks Associates Technology, Inc.
9 1.1 christos * All rights reserved.
10 1.1 christos * Copyright (c) 2004 Joe R. Doupnik
11 1.1 christos * All rights reserved.
12 1.1 christos *
13 1.1 christos * Portions of this software were developed for the FreeBSD Project by
14 1.1 christos * ThinkSec AS and NAI Labs, the Security Research Division of Network
15 1.1 christos * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
16 1.1 christos * ("CBOSS"), as part of the DARPA CHATS research program.
17 1.1 christos *
18 1.1 christos * Redistribution and use in source and binary forms, with or without
19 1.1 christos * modification, are permitted provided that the following conditions
20 1.1 christos * are met:
21 1.1 christos * 1. Redistributions of source code must retain the above copyright
22 1.1 christos * notice, this list of conditions and the following disclaimer.
23 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
24 1.1 christos * notice, this list of conditions and the following disclaimer in the
25 1.1 christos * documentation and/or other materials provided with the distribution.
26 1.1 christos * 3. The name of the author may not be used to endorse or promote
27 1.1 christos * products derived from this software without specific prior written
28 1.1 christos * permission.
29 1.1 christos * 4. Neither the name of the University nor the names of its contributors
30 1.1 christos * may be used to endorse or promote products derived from this software
31 1.1 christos * without specific prior written permission.
32 1.1 christos *
33 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 1.1 christos * SUCH DAMAGE.
44 1.1 christos */
45 1.1 christos
46 1.1 christos #include <sys/cdefs.h>
47 1.2 christos #ifdef __FreeBSD__
48 1.1 christos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.20 2004/01/26 19:28:37 des Exp $");
49 1.2 christos #else
50 1.2 christos __RCSID("$NetBSD: pam_lastlog.c,v 1.2 2004/12/12 08:18:46 christos Exp $");
51 1.2 christos #endif
52 1.1 christos
53 1.1 christos #define _BSD_SOURCE
54 1.1 christos
55 1.1 christos #include <sys/param.h>
56 1.1 christos
57 1.1 christos #include <fcntl.h>
58 1.2 christos #include <util.h>
59 1.1 christos #include <paths.h>
60 1.1 christos #include <pwd.h>
61 1.1 christos #include <stdio.h>
62 1.1 christos #include <stdlib.h>
63 1.1 christos #include <string.h>
64 1.1 christos #include <syslog.h>
65 1.1 christos #include <time.h>
66 1.1 christos #include <unistd.h>
67 1.1 christos #include <utmp.h>
68 1.1 christos
69 1.1 christos #define PAM_SM_SESSION
70 1.1 christos
71 1.1 christos #include <security/pam_appl.h>
72 1.1 christos #include <security/pam_modules.h>
73 1.1 christos #include <security/pam_mod_misc.h>
74 1.1 christos
75 1.1 christos PAM_EXTERN int
76 1.1 christos pam_sm_open_session(pam_handle_t *pamh, int flags,
77 1.1 christos int argc __unused, const char *argv[] __unused)
78 1.1 christos {
79 1.1 christos struct passwd *pwd;
80 1.1 christos struct utmp utmp;
81 1.1 christos struct lastlog ll;
82 1.1 christos time_t t;
83 1.1 christos const char *user;
84 1.1 christos const void *rhost, *tty;
85 1.1 christos off_t llpos;
86 1.1 christos int fd, pam_err;
87 1.1 christos
88 1.1 christos pam_err = pam_get_user(pamh, &user, NULL);
89 1.1 christos if (pam_err != PAM_SUCCESS)
90 1.1 christos return (pam_err);
91 1.1 christos if (user == NULL || (pwd = getpwnam(user)) == NULL)
92 1.1 christos return (PAM_SERVICE_ERR);
93 1.1 christos PAM_LOG("Got user: %s", user);
94 1.1 christos
95 1.1 christos pam_err = pam_get_item(pamh, PAM_RHOST, &rhost);
96 1.1 christos if (pam_err != PAM_SUCCESS)
97 1.1 christos goto err;
98 1.1 christos pam_err = pam_get_item(pamh, PAM_TTY, &tty);
99 1.1 christos if (pam_err != PAM_SUCCESS)
100 1.1 christos goto err;
101 1.1 christos if (tty == NULL) {
102 1.1 christos pam_err = PAM_SERVICE_ERR;
103 1.1 christos goto err;
104 1.1 christos }
105 1.1 christos if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
106 1.1 christos tty = (const char *)tty + strlen(_PATH_DEV);
107 1.1 christos if (*(const char *)tty == '\0')
108 1.1 christos return (PAM_SERVICE_ERR);
109 1.1 christos
110 1.1 christos fd = open(_PATH_LASTLOG, O_RDWR|O_CREAT, 0644);
111 1.1 christos if (fd == -1)
112 1.1 christos goto file_err;
113 1.1 christos
114 1.1 christos /*
115 1.1 christos * Record session in lastlog(5).
116 1.1 christos */
117 1.1 christos llpos = (off_t)(pwd->pw_uid * sizeof(ll));
118 1.1 christos if (lseek(fd, llpos, L_SET) != llpos)
119 1.1 christos goto file_err;
120 1.1 christos if ((flags & PAM_SILENT) == 0) {
121 1.1 christos if (read(fd, &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) {
122 1.1 christos t = ll.ll_time;
123 1.1 christos if (*ll.ll_host != '\0')
124 1.1 christos pam_info(pamh, "Last login: %.*s from %.*s",
125 1.1 christos 24 - 5, ctime(&t),
126 1.1 christos (int)sizeof(ll.ll_host), ll.ll_host);
127 1.1 christos else
128 1.1 christos pam_info(pamh, "Last login: %.*s on %.*s",
129 1.1 christos 24 - 5, ctime(&t),
130 1.1 christos (int)sizeof(ll.ll_line), ll.ll_line);
131 1.1 christos }
132 1.1 christos if (lseek(fd, llpos, L_SET) != llpos)
133 1.1 christos goto file_err;
134 1.1 christos }
135 1.1 christos
136 1.1 christos bzero(&ll, sizeof(ll));
137 1.1 christos ll.ll_time = time(NULL);
138 1.1 christos
139 1.1 christos /* note: does not need to be NUL-terminated */
140 1.1 christos strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
141 1.1 christos if (rhost != NULL && *(const char *)rhost != '\0')
142 1.1 christos /* note: does not need to be NUL-terminated */
143 1.1 christos strncpy(ll.ll_host, rhost, sizeof(ll.ll_host));
144 1.1 christos
145 1.1 christos if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0)
146 1.1 christos goto file_err;
147 1.1 christos
148 1.1 christos PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
149 1.1 christos
150 1.1 christos /*
151 1.1 christos * Record session in utmp(5) and wtmp(5).
152 1.1 christos */
153 1.1 christos bzero(&utmp, sizeof(utmp));
154 1.1 christos utmp.ut_time = time(NULL);
155 1.1 christos /* note: does not need to be NUL-terminated */
156 1.1 christos strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
157 1.1 christos if (rhost != NULL && *(const char *)rhost != '\0')
158 1.1 christos strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host));
159 1.1 christos (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
160 1.1 christos login(&utmp);
161 1.1 christos
162 1.1 christos return (PAM_SUCCESS);
163 1.1 christos
164 1.1 christos file_err:
165 1.1 christos syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG);
166 1.1 christos if (fd != -1)
167 1.1 christos close(fd);
168 1.1 christos pam_err = PAM_SYSTEM_ERR;
169 1.1 christos err:
170 1.1 christos if (openpam_get_option(pamh, "no_fail"))
171 1.1 christos return (PAM_SUCCESS);
172 1.1 christos return (pam_err);
173 1.1 christos }
174 1.1 christos
175 1.1 christos PAM_EXTERN int
176 1.1 christos pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
177 1.1 christos int argc __unused, const char *argv[] __unused)
178 1.1 christos {
179 1.1 christos const void *tty;
180 1.1 christos
181 1.1 christos pam_get_item(pamh, PAM_TTY, (const void **)&tty);
182 1.1 christos if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
183 1.1 christos tty = (const char *)tty + strlen(_PATH_DEV);
184 1.1 christos if (*(const char *)tty == '\0')
185 1.1 christos return (PAM_SERVICE_ERR);
186 1.1 christos if (logout(tty) != 1)
187 1.1 christos syslog(LOG_ERR, "%s(): no utmp record for %s",
188 1.1 christos __func__, (const char *)tty);
189 1.1 christos logwtmp(tty, "", "");
190 1.1 christos return (PAM_SUCCESS);
191 1.1 christos }
192 1.1 christos
193 1.1 christos PAM_MODULE_ENTRY("pam_lastlog");
194