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