1 /* $NetBSD: pam_lastlog.c,v 1.12 2006/11/03 18:55:40 christos Exp $ */
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
8 * Copyright (c) 2001 Networks Associates Technology, Inc.
10 * Copyright (c) 2004 Joe R. Doupnik
11 * All rights reserved.
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.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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
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.
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
46 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.20 2004/01/26 19:28:37 des Exp $");
50 __RCSID("$NetBSD: pam_lastlog.c,v 1.12 2006/11/03 18:55:40 christos Exp $");
53 #include <sys/param.h>
67 #include <login_cap.h>
70 #define PAM_SM_SESSION
72 #include <security/pam_appl.h>
73 #include <security/pam_modules.h>
74 #include <security/pam_mod_misc.h>
78 static void doutmp(const char *, const char *, const char *,
79 const struct timeval
*);
80 static void dolastlog(pam_handle_t
*, int, const struct passwd
*, const char *,
81 const char *, const struct timeval
*);
86 static void doutmpx(const char *, const char *, const char *,
87 const struct sockaddr_storage
*ss
, const struct timeval
*);
88 static void dolastlogx(pam_handle_t
*, int, const struct passwd
*, const char *,
89 const char *, const struct sockaddr_storage
*ss
, const struct timeval
*);
92 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
93 static void domsg(pam_handle_t
*, time_t, const char *, size_t, const char *,
98 logit(int level
, const char *fmt
, ...)
101 struct syslog_data data
= SYSLOG_DATA_INIT
;
103 openlog_r("pam_lastlog", LOG_PID
, LOG_AUTHPRIV
, &data
);
105 vsyslog_r(level
, &data
, fmt
, ap
);
112 pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
113 int argc __unused
, const char *argv
[] __unused
)
115 struct passwd
*pwd
, pwres
;
117 const char *user
, *rhost
, *tty
, *nuser
;
118 const void *vrhost
, *vtty
, *vss
, *vnuser
;
119 const struct sockaddr_storage
*ss
;
126 pam_err
= pam_get_user(pamh
, &user
, NULL
);
127 if (pam_err
!= PAM_SUCCESS
)
131 getpwnam_r(user
, &pwres
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0 ||
133 return PAM_SERVICE_ERR
;
135 PAM_LOG("Got user: %s", user
);
137 pam_err
= pam_get_item(pamh
, PAM_RHOST
, &vrhost
);
138 if (pam_err
!= PAM_SUCCESS
)
140 rhost
= (const char *)vrhost
;
142 pam_err
= pam_get_item(pamh
, PAM_SOCKADDR
, &vss
);
143 if (pam_err
!= PAM_SUCCESS
)
145 ss
= (const struct sockaddr_storage
*)vss
;
147 pam_err
= pam_get_item(pamh
, PAM_TTY
, &vtty
);
148 if (pam_err
!= PAM_SUCCESS
)
150 tty
= (const char *)vtty
;
153 pam_err
= PAM_SERVICE_ERR
;
157 if (pam_get_item(pamh
, PAM_NUSER
, &vnuser
) != PAM_SUCCESS
)
160 nuser
= (const char *)vnuser
;
162 if (strncmp(tty
, _PATH_DEV
, strlen(_PATH_DEV
)) == 0)
163 tty
= tty
+ strlen(_PATH_DEV
);
166 pam_err
= PAM_SERVICE_ERR
;
170 (void)gettimeofday(&now
, NULL
);
172 if (openpam_get_option(pamh
, "no_nested") == NULL
|| nuser
== NULL
) {
174 if ((flags
& PAM_SILENT
) != 0)
178 lc
= login_getpwclass(pwd
);
179 quiet
= login_getcapbool(lc
, "hushlogin", 0);
186 doutmpx(user
, rhost
, tty
, ss
, &now
);
187 dolastlogx(pamh
, quiet
, pwd
, rhost
, tty
, ss
, &now
);
191 doutmp(user
, rhost
, tty
, &now
);
192 dolastlog(pamh
, quiet
, pwd
, rhost
, tty
, &now
);
196 if (openpam_get_option(pamh
, "no_fail"))
202 pam_sm_close_session(pam_handle_t
*pamh __unused
, int flags __unused
,
203 int argc __unused
, const char *argv
[] __unused
)
205 const void *vtty
, *vnuser
;
206 const char *tty
, *nuser
;
208 if (pam_get_item(pamh
, PAM_NUSER
, &vnuser
) != PAM_SUCCESS
)
211 nuser
= (const char *)vnuser
;
213 pam_get_item(pamh
, PAM_TTY
, &vtty
);
215 return PAM_SERVICE_ERR
;
216 tty
= (const char *)vtty
;
218 if (strncmp(tty
, _PATH_DEV
, strlen(_PATH_DEV
)) == 0)
219 tty
= tty
+ strlen(_PATH_DEV
);
222 return PAM_SERVICE_ERR
;
224 if (openpam_get_option(pamh
, "no_nested") == NULL
|| nuser
== NULL
) {
227 if (logoutx(tty
, 0, DEAD_PROCESS
))
228 logwtmpx(tty
, "", "", 0, DEAD_PROCESS
);
230 logit(LOG_NOTICE
, "%s(): no utmpx record for %s",
236 logwtmp(tty
, "", "");
238 logit(LOG_NOTICE
, "%s(): no utmp record for %s",
245 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
247 domsg(pam_handle_t
*pamh
, time_t t
, const char *host
, size_t hsize
,
248 const char *line
, size_t lsize
)
250 char buf
[MAXHOSTNAMELEN
+ 32], *promptresp
= NULL
;
254 (void)snprintf(buf
, sizeof(buf
), "from %.*s ",
259 pam_err
= pam_prompt(pamh
, PAM_TEXT_INFO
, &promptresp
,
260 "Last login: %.24s %son %.*s\n", ctime(&t
), host
, (int)lsize
, line
);
262 if (pam_err
== PAM_SUCCESS
&& promptresp
)
269 doutmpx(const char *username
, const char *hostname
, const char *tty
,
270 const struct sockaddr_storage
*ss
, const struct timeval
*now
)
275 memset((void *)&utmpx
, 0, sizeof(utmpx
));
277 (void)strncpy(utmpx
.ut_name
, username
, sizeof(utmpx
.ut_name
));
279 (void)strncpy(utmpx
.ut_host
, hostname
, sizeof(utmpx
.ut_host
));
283 (void)strncpy(utmpx
.ut_line
, tty
, sizeof(utmpx
.ut_line
));
284 utmpx
.ut_type
= USER_PROCESS
;
285 utmpx
.ut_pid
= getpid();
286 t
= tty
+ strlen(tty
);
287 if ((size_t)(t
- tty
) >= sizeof(utmpx
.ut_id
)) {
288 (void)strncpy(utmpx
.ut_id
, t
- sizeof(utmpx
.ut_id
),
289 sizeof(utmpx
.ut_id
));
291 (void)strncpy(utmpx
.ut_id
, tty
, sizeof(utmpx
.ut_id
));
293 if (pututxline(&utmpx
) == NULL
)
294 logit(LOG_NOTICE
, "Cannot update utmpx %m");
296 if (updwtmpx(_PATH_WTMPX
, &utmpx
) != 0)
297 logit(LOG_NOTICE
, "Cannot update wtmpx %m");
301 dolastlogx(pam_handle_t
*pamh
, int quiet
, const struct passwd
*pwd
,
302 const char *hostname
, const char *tty
, const struct sockaddr_storage
*ss
,
303 const struct timeval
*now
)
307 if (getlastlogx(_PATH_LASTLOGX
, pwd
->pw_uid
, &ll
) != NULL
)
308 domsg(pamh
, (time_t)ll
.ll_tv
.tv_sec
, ll
.ll_host
,
309 sizeof(ll
.ll_host
), ll
.ll_line
,
313 (void)strncpy(ll
.ll_line
, tty
, sizeof(ll
.ll_line
));
316 (void)strncpy(ll
.ll_host
, hostname
, sizeof(ll
.ll_host
));
318 (void)memset(ll
.ll_host
, 0, sizeof(ll
.ll_host
));
323 (void)memset(&ll
.ll_ss
, 0, sizeof(ll
.ll_ss
));
325 if (updlastlogx(_PATH_LASTLOGX
, pwd
->pw_uid
, &ll
) != 0)
326 logit(LOG_NOTICE
, "Cannot update lastlogx %m");
327 PAM_LOG("Login recorded in %s", _PATH_LASTLOGX
);
333 doutmp(const char *username
, const char *hostname
, const char *tty
,
334 const struct timeval
*now
)
338 (void)memset((void *)&utmp
, 0, sizeof(utmp
));
339 utmp
.ut_time
= now
->tv_sec
;
340 (void)strncpy(utmp
.ut_name
, username
, sizeof(utmp
.ut_name
));
342 (void)strncpy(utmp
.ut_host
, hostname
, sizeof(utmp
.ut_host
));
343 (void)strncpy(utmp
.ut_line
, tty
, sizeof(utmp
.ut_line
));
348 dolastlog(pam_handle_t
*pamh
, int quiet
, const struct passwd
*pwd
,
349 const char *hostname
, const char *tty
, const struct timeval
*now
)
354 if ((fd
= open(_PATH_LASTLOG
, O_RDWR
, 0)) == -1) {
355 logit(LOG_NOTICE
, "Cannot open `%s' %m", _PATH_LASTLOG
);
358 (void)lseek(fd
, (off_t
)(pwd
->pw_uid
* sizeof(ll
)), SEEK_SET
);
361 if (read(fd
, (char *)&ll
, sizeof(ll
)) == sizeof(ll
) &&
363 domsg(pamh
, ll
.ll_time
, ll
.ll_host
,
364 sizeof(ll
.ll_host
), ll
.ll_line
,
366 (void)lseek(fd
, (off_t
)(pwd
->pw_uid
* sizeof(ll
)), SEEK_SET
);
369 ll
.ll_time
= now
->tv_sec
;
370 (void)strncpy(ll
.ll_line
, tty
, sizeof(ll
.ll_line
));
373 (void)strncpy(ll
.ll_host
, hostname
, sizeof(ll
.ll_host
));
375 (void)memset(ll
.ll_host
, 0, sizeof(ll
.ll_host
));
377 (void)write(fd
, &ll
, sizeof(ll
));
380 PAM_LOG("Login recorded in %s", _PATH_LASTLOG
);
384 PAM_MODULE_ENTRY("pam_lastlog");