Add __printflike's where possible and fix all related bugs & issues.
[dragonfly.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 logit(int, const char *, ...) __printflike(2, 3);
92
93 static void
94 logit(int level, const char *fmt, ...)
95 {
96         va_list ap;
97
98         openlog("pam_lastlog", LOG_PID, LOG_AUTHPRIV);
99         va_start(ap, fmt);
100         vsyslog(level, fmt, ap);
101         va_end(ap);
102         closelog();
103 }
104
105
106 PAM_EXTERN int
107 pam_sm_open_session(pam_handle_t *pamh, int flags,
108     int argc __unused, const char *argv[] __unused)
109 {
110         struct passwd *pwd, pwres;
111         struct timeval now;
112         const char *user, *rhost, *tty;
113         const void *vrhost, *vtty;
114         int pam_err;
115         int quiet;
116         char pwbuf[1024];
117 #ifdef LOGIN_CAP
118         login_cap_t *lc;
119 #endif
120
121         pam_err = pam_get_user(pamh, &user, NULL);
122         if (pam_err != PAM_SUCCESS)
123                 return pam_err;
124
125         if (user == NULL ||
126             getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
127             pwd == NULL)
128                 return PAM_SERVICE_ERR;
129
130         PAM_LOG("Got user: %s", user);
131
132         pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost);
133         if (pam_err != PAM_SUCCESS)
134                 goto err;
135         rhost = (const char *)vrhost;
136
137         pam_err = pam_get_item(pamh, PAM_TTY, &vtty);
138         if (pam_err != PAM_SUCCESS)
139                 goto err;
140         tty = (const char *)vtty;
141
142         if (tty == NULL) {
143                 pam_err = PAM_SERVICE_ERR;
144                 goto err;
145         }
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 ((flags & PAM_SILENT) != 0)
158                 quiet = 1;
159         else {
160 #ifdef LOGIN_CAP
161                 lc = login_getpwclass(pwd);
162                 quiet = login_getcapbool(lc, "hushlogin", 0);
163                 login_close(lc);
164 #else
165                 quiet = 0;
166 #endif
167         }
168 #ifdef SUPPORT_UTMPX
169         dolastlogx(pamh, quiet, pwd, rhost, tty, NULL, &now);
170         doutmpx(user, rhost, tty, NULL, &now);
171         quiet = 1;
172 #endif
173 #ifdef SUPPORT_UTMP
174         doutmp(user, rhost, tty, &now);
175         dolastlog(pamh, quiet, pwd, rhost, tty, &now);
176 #endif
177 err:
178         if (openpam_get_option(pamh, "no_fail"))
179                 return PAM_SUCCESS;
180         return pam_err;
181 }
182
183 PAM_EXTERN int
184 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
185     int argc __unused, const char *argv[] __unused)
186 {
187         const void *vtty;
188         const char *tty;
189
190         pam_get_item(pamh, PAM_TTY, &vtty);
191         if (vtty == NULL)
192                 return PAM_SERVICE_ERR;
193         tty = (const char *)vtty;
194
195         if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
196                 tty = tty + strlen(_PATH_DEV);
197
198         if (*tty == '\0')
199                 return PAM_SERVICE_ERR;
200
201 #ifdef SUPPORT_UTMPX
202         if (logoutx(tty, 0, DEAD_PROCESS))
203                 logwtmpx(tty, "", "", 0, DEAD_PROCESS);
204         else
205                 logit(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                 logit(LOG_NOTICE, "%s(): no utmp record for %s",
214                     __func__, tty);
215 #endif
216         return PAM_SUCCESS;
217 }
218
219 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
220 static void
221 domsg(pam_handle_t *pamh, time_t t, const char *host, size_t hsize,
222     const char *line, size_t lsize)
223 {
224         char buf[MAXHOSTNAMELEN + 32], *promptresp = NULL;
225         int pam_err;
226
227         if (*host) {
228                 (void)snprintf(buf, sizeof(buf), "from %.*s ",
229                     (int)hsize, host);
230                 host = buf;
231         }
232
233         pam_err = pam_prompt(pamh, PAM_TEXT_INFO, &promptresp,
234             "Last login: %.24s %son %.*s\n", ctime(&t), host, (int)lsize, line);
235
236         if (pam_err == PAM_SUCCESS && promptresp)
237                 free(promptresp);
238 }
239 #endif
240
241 #ifdef SUPPORT_UTMPX
242 static void
243 doutmpx(const char *username, const char *hostname, const char *tty,
244     const struct sockaddr_storage *ss, const struct timeval *now)
245 {
246         struct utmpx utmpx;
247         const char *t;
248
249         memset((void *)&utmpx, 0, sizeof(utmpx));
250         utmpx.ut_tv = *now;
251         (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
252         if (hostname) {
253                 (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
254                 if (ss)
255                         utmpx.ut_ss = *ss;
256         }
257         (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
258         utmpx.ut_type = USER_PROCESS;
259         utmpx.ut_pid = getpid();
260         t = tty + strlen(tty);
261         if ((size_t)(t - tty) >= sizeof(utmpx.ut_id)) {
262                 (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
263                     sizeof(utmpx.ut_id));
264         } else {
265                 (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
266         }
267         if (pututxline(&utmpx) == NULL)
268                 logit(LOG_NOTICE, "Cannot update utmpx %m");
269         endutxent();
270         if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
271                 logit(LOG_NOTICE, "Cannot update wtmpx %m");
272 }
273
274 static void
275 dolastlogx(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
276     const char *hostname __unused, const char *tty __unused,
277     const struct sockaddr_storage *ss __unused,
278     const struct timeval *now __unused)
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 #if 0
288         ll.ll_tv = *now;
289         (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
290
291         if (hostname)
292                 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
293         else
294                 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
295
296         if (ss)
297                 ll.ll_ss = *ss;
298         else
299                 (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
300
301         if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
302                 logit(LOG_NOTICE, "Cannot update lastlogx %m");
303         PAM_LOG("Login recorded in %s", _PATH_LASTLOGX);
304 #endif
305 }
306 #endif
307
308 #ifdef SUPPORT_UTMP
309 static void
310 doutmp(const char *username, const char *hostname, const char *tty,
311     const struct timeval *now)
312 {
313         struct utmp utmp;
314
315         (void)memset((void *)&utmp, 0, sizeof(utmp));
316         utmp.ut_time = now->tv_sec;
317         (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
318         if (hostname)
319                 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
320         (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
321         login(&utmp);
322 }
323
324 static void
325 dolastlog(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
326     const char *hostname, const char *tty, const struct timeval *now)
327 {
328         struct lastlog ll;
329         int fd;
330
331         if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) == -1) {
332                 logit(LOG_NOTICE, "Cannot open `%s' %m", _PATH_LASTLOG);
333                 return;
334         }
335         (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
336
337         if (!quiet) {
338                 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
339                     ll.ll_time != 0)
340                         domsg(pamh, ll.ll_time, ll.ll_host,
341                             sizeof(ll.ll_host), ll.ll_line,
342                             sizeof(ll.ll_line));
343                 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
344         }
345
346         ll.ll_time = now->tv_sec;
347         (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
348
349         if (hostname)
350                 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
351         else
352                 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
353
354         (void)write(fd, &ll, sizeof(ll));
355         (void)close(fd);
356
357         PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
358 }
359 #endif
360
361 PAM_MODULE_ENTRY("pam_lastlog");
362