pam_lastlog.c revision 1.10 1 /* $NetBSD: pam_lastlog.c,v 1.10 2006/03/18 10:53:17 jnemeth 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.10 2006/03/18 10:53:17 jnemeth 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 #ifdef LOGIN_CAP
108 login_cap_t *lc;
109 #endif
110
111 pam_err = pam_get_user(pamh, &user, NULL);
112 if (pam_err != PAM_SUCCESS)
113 return pam_err;
114
115 if (user == NULL ||
116 getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
117 pwd == NULL)
118 return PAM_SERVICE_ERR;
119
120 PAM_LOG("Got user: %s", user);
121
122 pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost);
123 if (pam_err != PAM_SUCCESS)
124 goto err;
125 rhost = (const char *)vrhost;
126
127 pam_err = pam_get_item(pamh, PAM_SOCKADDR, &vss);
128 if (pam_err != PAM_SUCCESS)
129 goto err;
130 ss = (const struct sockaddr_storage *)vss;
131
132 pam_err = pam_get_item(pamh, PAM_TTY, &vtty);
133 if (pam_err != PAM_SUCCESS)
134 goto err;
135 tty = (const char *)vtty;
136
137 if (tty == NULL) {
138 pam_err = PAM_SERVICE_ERR;
139 goto err;
140 }
141
142 if (pam_get_item(pamh, PAM_NUSER, &vnuser) != PAM_SUCCESS)
143 nuser = NULL;
144 else
145 nuser = (const char *)vnuser;
146
147 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
148 tty = tty + strlen(_PATH_DEV);
149
150 if (*tty == '\0') {
151 pam_err = PAM_SERVICE_ERR;
152 goto err;
153 }
154
155 (void)gettimeofday(&now, NULL);
156
157 if (openpam_get_option(pamh, "no_nested") == NULL || nuser == NULL) {
158 int quiet;
159 if ((flags & PAM_SILENT) != 0)
160 quiet = 1;
161 else {
162 #ifdef LOGIN_CAP
163 lc = login_getpwclass(pwd);
164 quiet = login_getcapbool(lc, "hushlogin", 0);
165 login_close(lc);
166 #else
167 quiet = 0;
168 #endif
169 }
170 #ifdef SUPPORT_UTMPX
171 doutmpx(user, rhost, tty, ss, &now);
172 dolastlogx(pamh, quiet, pwd, rhost, tty, ss, &now);
173 quiet = 1;
174 #endif
175 #ifdef SUPPORT_UTMP
176 doutmp(user, rhost, tty, &now);
177 dolastlog(pamh, quiet, pwd, rhost, tty, &now);
178 #endif
179 }
180 err:
181 if (openpam_get_option(pamh, "no_fail"))
182 return PAM_SUCCESS;
183 return pam_err;
184 }
185
186 PAM_EXTERN int
187 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
188 int argc __unused, const char *argv[] __unused)
189 {
190 const void *vtty, *vnuser;
191 const char *tty, *nuser;
192
193 if (pam_get_item(pamh, PAM_NUSER, &vnuser) != PAM_SUCCESS)
194 nuser = NULL;
195 else
196 nuser = (const char *)vnuser;
197
198 pam_get_item(pamh, PAM_TTY, &vtty);
199 if (vtty == NULL)
200 return PAM_SERVICE_ERR;
201 tty = (const char *)vtty;
202
203 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
204 tty = tty + strlen(_PATH_DEV);
205
206 if (*tty == '\0')
207 return PAM_SERVICE_ERR;
208
209 if (openpam_get_option(pamh, "no_nested") == NULL || nuser == NULL) {
210
211 #ifdef SUPPORT_UTMPX
212 if (logoutx(tty, 0, DEAD_PROCESS))
213 logwtmpx(tty, "", "", 0, DEAD_PROCESS);
214 else
215 syslog(LOG_NOTICE, "%s(): no utmpx record for %s",
216 __func__, tty);
217 #endif
218
219 #ifdef SUPPORT_UTMP
220 if (logout(tty))
221 logwtmp(tty, "", "");
222 else
223 syslog(LOG_NOTICE, "%s(): no utmp record for %s",
224 __func__, tty);
225 #endif
226 }
227 return PAM_SUCCESS;
228 }
229
230 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
231 static void
232 domsg(pam_handle_t *pamh, time_t t, const char *host, size_t hsize,
233 const char *line, size_t lsize)
234 {
235 char buf[MAXHOSTNAMELEN + 32], *promptresp = NULL;
236 int pam_err;
237
238 if (*host) {
239 (void)snprintf(buf, sizeof(buf), "from %.*s ",
240 (int)hsize, host);
241 host = buf;
242 }
243
244 pam_err = pam_prompt(pamh, PAM_TEXT_INFO, &promptresp,
245 "Last login: %.24s %son %.*s\n", ctime(&t), host, (int)lsize, line);
246
247 if (pam_err == PAM_SUCCESS && promptresp)
248 free(promptresp);
249 }
250 #endif
251
252 #ifdef SUPPORT_UTMPX
253 static void
254 doutmpx(const char *username, const char *hostname, const char *tty,
255 const struct sockaddr_storage *ss, const struct timeval *now)
256 {
257 struct utmpx utmpx;
258 const char *t;
259
260 memset((void *)&utmpx, 0, sizeof(utmpx));
261 utmpx.ut_tv = *now;
262 (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
263 if (hostname) {
264 (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
265 if (ss)
266 utmpx.ut_ss = *ss;
267 }
268 (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
269 utmpx.ut_type = USER_PROCESS;
270 utmpx.ut_pid = getpid();
271 t = tty + strlen(tty);
272 if (t - tty >= sizeof(utmpx.ut_id)) {
273 (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
274 sizeof(utmpx.ut_id));
275 } else {
276 (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
277 }
278 if (pututxline(&utmpx) == NULL)
279 syslog(LOG_NOTICE, "Cannot update utmpx %m");
280 endutxent();
281 if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
282 syslog(LOG_NOTICE, "Cannot update wtmpx %m");
283 }
284
285 static void
286 dolastlogx(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
287 const char *hostname, const char *tty, const struct sockaddr_storage *ss,
288 const struct timeval *now)
289 {
290 struct lastlogx ll;
291 if (!quiet) {
292 if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL)
293 domsg(pamh, (time_t)ll.ll_tv.tv_sec, ll.ll_host,
294 sizeof(ll.ll_host), ll.ll_line,
295 sizeof(ll.ll_line));
296 }
297 ll.ll_tv = *now;
298 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
299
300 if (hostname)
301 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
302 else
303 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
304
305 if (ss)
306 ll.ll_ss = *ss;
307 else
308 (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
309
310 if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
311 syslog(LOG_NOTICE, "Cannot update lastlogx %m");
312 PAM_LOG("Login recorded in %s", _PATH_LASTLOGX);
313 }
314 #endif
315
316 #ifdef SUPPORT_UTMP
317 static void
318 doutmp(const char *username, const char *hostname, const char *tty,
319 const struct timeval *now)
320 {
321 struct utmp utmp;
322
323 (void)memset((void *)&utmp, 0, sizeof(utmp));
324 utmp.ut_time = now->tv_sec;
325 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
326 if (hostname)
327 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
328 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
329 login(&utmp);
330 }
331
332 static void
333 dolastlog(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
334 const char *hostname, const char *tty, const struct timeval *now)
335 {
336 struct lastlog ll;
337 int fd;
338
339 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) == -1) {
340 syslog(LOG_NOTICE, "Cannot open `%s' %m", _PATH_LASTLOG);
341 return;
342 }
343 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
344
345 if (!quiet) {
346 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
347 ll.ll_time != 0)
348 domsg(pamh, ll.ll_time, ll.ll_host,
349 sizeof(ll.ll_host), ll.ll_line,
350 sizeof(ll.ll_line));
351 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
352 }
353
354 ll.ll_time = now->tv_sec;
355 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
356
357 if (hostname)
358 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
359 else
360 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
361
362 (void)write(fd, &ll, sizeof(ll));
363 (void)close(fd);
364
365 PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
366 }
367 #endif
368
369 PAM_MODULE_ENTRY("pam_lastlog");
370