]> granicus.if.org Git - linux-pam/blob - modules/pam_lastlog/pam_lastlog.c
Relevant BUGIDs: 131549
[linux-pam] / modules / pam_lastlog / pam_lastlog.c
1 /* pam_lastlog module */
2
3 /*
4  * $Id$
5  *
6  * Written by Andrew Morgan <morgan@linux.kernel.org> 1996/3/11
7  *
8  * This module does the necessary work to display the last login
9  * time+date for this user, it then updates this entry for the
10  * present (login) service.
11  */
12
13 #include <security/_pam_aconf.h>
14
15 #include <fcntl.h>
16 #include <time.h>
17 #ifdef HAVE_UTMP_H
18 # include <utmp.h>
19 #else
20 # include <lastlog.h>
21 #endif
22 #include <pwd.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <syslog.h>
29 #include <unistd.h>
30
31 #ifdef WANT_PWDB
32 #include <pwdb/pwdb_public.h>                /* use POSIX front end */
33 #endif
34
35 #if defined(hpux) || defined(sunos) || defined(solaris)
36 # ifndef _PATH_LASTLOG
37 #  define _PATH_LASTLOG "/usr/adm/lastlog"
38 # endif /* _PATH_LASTLOG */
39 # ifndef UT_HOSTSIZE
40 #  define UT_HOSTSIZE 16
41 # endif /* UT_HOSTSIZE */
42 # ifndef UT_LINESIZE
43 #  define UT_LINESIZE 12
44 # endif /* UT_LINESIZE */
45 #endif
46 #if defined(hpux)
47 struct lastlog {
48     time_t  ll_time;
49     char    ll_line[UT_LINESIZE];
50     char    ll_host[UT_HOSTSIZE];            /* same as in utmp */
51 };
52 #endif /* hpux */
53
54 /* XXX - time before ignoring lock. Is 1 sec enough? */
55 #define LASTLOG_IGNORE_LOCK_TIME     1
56
57 #define DEFAULT_HOST     ""  /* "[no.where]" */
58 #define DEFAULT_TERM     ""  /* "tt???" */
59 #define LASTLOG_NEVER_WELCOME       "Welcome to your new account!"
60 #define LASTLOG_INTRO    "Last login:"
61 #define LASTLOG_TIME     " %s"
62 #define _LASTLOG_HOST_FORMAT   " from %%.%ds"
63 #define _LASTLOG_LINE_FORMAT   " on %%.%ds"
64 #define LASTLOG_TAIL     ""
65 #define LASTLOG_MAXSIZE  (sizeof(LASTLOG_INTRO)+0 \
66                           +sizeof(LASTLOG_TIME)+strlen(the_time) \
67                           +sizeof(_LASTLOG_HOST_FORMAT)+UT_HOSTSIZE \
68                           +sizeof(_LASTLOG_LINE_FORMAT)+UT_LINESIZE \
69                           +sizeof(LASTLOG_TAIL))
70
71 /*
72  * here, we make a definition for the externally accessible function
73  * in this file (this definition is required for static a module
74  * but strongly encouraged generally) it is used to instruct the
75  * modules include file to define the function prototypes.
76  */
77
78 #define PAM_SM_SESSION
79
80 #include <security/pam_modules.h>
81 #include <security/_pam_macros.h>
82
83 /* some syslogging */
84
85 static void _log_err(int err, const char *format, ...)
86 {
87     va_list args;
88
89     va_start(args, format);
90     openlog("PAM-lastlog", LOG_CONS|LOG_PID, LOG_AUTH);
91     vsyslog(err, format, args);
92     va_end(args);
93     closelog();
94 }
95
96 /* argument parsing */
97
98 #define LASTLOG_DATE        01  /* display the date of the last login */
99 #define LASTLOG_HOST        02  /* display the last host used (if set) */
100 #define LASTLOG_LINE        04  /* display the last terminal used */
101 #define LASTLOG_NEVER      010  /* display a welcome message for first login */
102 #define LASTLOG_DEBUG      020  /* send info to syslog(3) */
103 #define LASTLOG_QUIET      040  /* keep quiet about things */
104
105 static int _pam_parse(int flags, int argc, const char **argv)
106 {
107     int ctrl=(LASTLOG_DATE|LASTLOG_HOST|LASTLOG_LINE);
108
109     /* does the appliction require quiet? */
110     if (flags & PAM_SILENT) {
111         ctrl |= LASTLOG_QUIET;
112     }
113
114     /* step through arguments */
115     for (; argc-- > 0; ++argv) {
116
117         /* generic options */
118
119         if (!strcmp(*argv,"debug")) {
120             ctrl |= LASTLOG_DEBUG;
121         } else if (!strcmp(*argv,"nodate")) {
122             ctrl |= ~LASTLOG_DATE;
123         } else if (!strcmp(*argv,"noterm")) {
124             ctrl |= ~LASTLOG_LINE;
125         } else if (!strcmp(*argv,"nohost")) {
126             ctrl |= ~LASTLOG_HOST;
127         } else if (!strcmp(*argv,"silent")) {
128             ctrl |= LASTLOG_QUIET;
129         } else if (!strcmp(*argv,"never")) {
130             ctrl |= LASTLOG_NEVER;
131         } else {
132             _log_err(LOG_ERR,"unknown option; %s",*argv);
133         }
134     }
135
136     D(("ctrl = %o", ctrl));
137     return ctrl;
138 }
139
140 /* a front end for conversations */
141
142 static int converse(pam_handle_t *pamh, int ctrl, int nargs
143                     , struct pam_message **message
144                     , struct pam_response **response)
145 {
146     int retval;
147     struct pam_conv *conv;
148
149     D(("begin to converse"));
150
151     retval = pam_get_item( pamh, PAM_CONV, (const void **) &conv ) ; 
152     if ( retval == PAM_SUCCESS ) {
153
154         retval = conv->conv(nargs, ( const struct pam_message ** ) message
155                             , response, conv->appdata_ptr);
156
157         D(("returned from application's conversation function"));
158
159         if (retval != PAM_SUCCESS && (ctrl & LASTLOG_DEBUG) ) {
160             _log_err(LOG_DEBUG, "conversation failure [%s]"
161                      , pam_strerror(pamh, retval));
162         }
163
164     } else {
165         _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
166                  , pam_strerror(pamh, retval));
167     }
168
169     D(("ready to return from module conversation"));
170
171     return retval;                  /* propagate error status */
172 }
173
174 static int make_remark(pam_handle_t *pamh, int ctrl, const char *remark)
175 {
176     int retval;
177
178     if (!(ctrl & LASTLOG_QUIET)) {
179         struct pam_message msg[1], *mesg[1];
180         struct pam_response *resp=NULL;
181
182         mesg[0] = &msg[0];
183         msg[0].msg_style = PAM_TEXT_INFO;
184         msg[0].msg = remark;
185
186         retval = converse(pamh, ctrl, 1, mesg, &resp);
187
188         msg[0].msg = NULL;
189         if (resp) {
190             _pam_drop_reply(resp, 1);
191         }
192     } else {
193         D(("keeping quiet"));
194         retval = PAM_SUCCESS;
195     }
196
197     D(("returning %s", pam_strerror(pamh, retval)));
198     return retval;
199 }
200
201 /*
202  * Values for the announce flags..
203  */
204
205 static int last_login_date(pam_handle_t *pamh, int announce, uid_t uid)
206 {
207     struct flock last_lock;
208     struct lastlog last_login;
209     int retval = PAM_SESSION_ERR;
210     int last_fd;
211
212     /* obtain the last login date and all the relevant info */
213     last_fd = open(_PATH_LASTLOG, O_RDWR);
214     if (last_fd < 0) {
215         D(("unable to open the %s file", _PATH_LASTLOG));
216         if (announce & LASTLOG_DEBUG) {
217             _log_err(LOG_DEBUG, "unable to open %s file", _PATH_LASTLOG);
218         }
219         retval = PAM_PERM_DENIED;
220     } else {
221         int win;
222
223         /* read the lastlogin file - for this uid */
224         (void) lseek(last_fd, sizeof(last_login) * (off_t) uid, SEEK_SET);
225
226         memset(&last_lock, 0, sizeof(last_lock));
227         last_lock.l_type = F_RDLCK;
228         last_lock.l_whence = SEEK_SET;
229         last_lock.l_start = sizeof(last_login) * (off_t) uid;
230         last_lock.l_len = sizeof(last_login);
231
232         if ( fcntl(last_fd, F_SETLK, &last_lock) < 0 ) {
233             D(("locking %s failed..(waiting a little)", _PATH_LASTLOG));
234             _log_err(LOG_ALERT, "%s file is locked/read", _PATH_LASTLOG);
235             sleep(LASTLOG_IGNORE_LOCK_TIME);
236         }
237
238         win = ( read(last_fd, &last_login, sizeof(last_login))
239                 == sizeof(last_login) );
240
241         last_lock.l_type = F_UNLCK;
242         (void) fcntl(last_fd, F_SETLK, &last_lock);        /* unlock */
243
244         if (!win) {
245             D(("First login for user uid=%d", _PATH_LASTLOG, uid));
246             if (announce & LASTLOG_DEBUG) {
247                 _log_err(LOG_DEBUG, "creating lastlog for uid %d", uid);
248             }
249             memset(&last_login, 0, sizeof(last_login));
250         }
251
252         /* rewind */
253         (void) lseek(last_fd, sizeof(last_login) * (off_t) uid, SEEK_SET);
254
255         if (!(announce & LASTLOG_QUIET)) {
256             if (last_login.ll_time) {
257                 char *the_time;
258                 char *remark;
259
260                 the_time = ctime(&last_login.ll_time);
261                 the_time[-1+strlen(the_time)] = '\0';    /* delete '\n' */
262
263                 remark = malloc(LASTLOG_MAXSIZE);
264                 if (remark == NULL) {
265                     D(("no memory for last login remark"));
266                     retval = PAM_BUF_ERR;
267                 } else {
268                     int at;
269
270                     /* printing prefix */
271                     at = sprintf(remark, "%s", LASTLOG_INTRO);
272
273                     /* we want the date? */
274                     if (announce & LASTLOG_DATE) {
275                         at += sprintf(remark+at, LASTLOG_TIME, the_time);
276                     }
277
278                     /* we want & have the host? */
279                     if ((announce & LASTLOG_HOST)
280                         && (last_login.ll_host[0] != '\0')) {
281                         char format[2*sizeof(_LASTLOG_HOST_FORMAT)];
282
283                         (void) sprintf(format, _LASTLOG_HOST_FORMAT
284                                        , UT_HOSTSIZE);
285                         D(("format: %s", format));
286                         at += sprintf(remark+at, format, last_login.ll_host);
287                         _pam_overwrite(format);
288                     }
289
290                     /* we want and have the terminal? */
291                     if ((announce & LASTLOG_LINE)
292                         && (last_login.ll_line[0] != '\0')) {
293                         char format[2*sizeof(_LASTLOG_LINE_FORMAT)];
294
295                         (void) sprintf(format, _LASTLOG_LINE_FORMAT
296                                        , UT_LINESIZE);
297                         D(("format: %s", format));
298                         at += sprintf(remark+at, format, last_login.ll_line);
299                         _pam_overwrite(format);
300                     }
301
302                     /* display requested combo */
303                     sprintf(remark+at, "%s", LASTLOG_TAIL);
304
305                     retval = make_remark(pamh, announce, remark);
306
307                     /* free all the stuff malloced */
308                     _pam_overwrite(remark);
309                     _pam_drop(remark);
310                 }
311             } else if ((!last_login.ll_time) && (announce & LASTLOG_NEVER)) {
312                 D(("this is the first time this user has logged in"));
313                 retval = make_remark(pamh, announce, LASTLOG_NEVER_WELCOME);
314             }
315         } else {
316             D(("no text was requested"));
317             retval = PAM_SUCCESS;
318         }
319
320         /* write latest value */
321         {
322             const char *remote_host=NULL
323                 , *terminal_line=DEFAULT_TERM;
324
325             /* set this login date */
326             D(("set the most recent login time"));
327
328             (void) time(&last_login.ll_time);    /* set the time */
329
330             /* set the remote host */
331             (void) pam_get_item(pamh, PAM_RHOST, (const void **)&remote_host);
332             if (remote_host == NULL) {
333                 remote_host = DEFAULT_HOST;
334             }
335
336             /* copy to last_login */
337             strncpy(last_login.ll_host, remote_host
338                     , sizeof(last_login.ll_host));
339             remote_host = NULL;
340
341             /* set the terminal line */
342             (void) pam_get_item(pamh, PAM_TTY, (const void **)&terminal_line);
343             D(("terminal = %s", terminal_line));
344             if (terminal_line == NULL) {
345                 terminal_line = DEFAULT_TERM;
346             } else if ( !strncmp("/dev/", terminal_line, 5) ) {
347                 /* strip leading "/dev/" from tty.. */
348                 terminal_line += 5;
349             }
350             D(("terminal = %s", terminal_line));
351
352             /* copy to last_login */
353             strncpy(last_login.ll_line, terminal_line
354                     , sizeof(last_login.ll_line));
355             terminal_line = NULL;
356
357             D(("locking last_log file"));
358
359             /* now we try to lock this file-record exclusively; non-blocking */
360             memset(&last_lock, 0, sizeof(last_lock));
361             last_lock.l_type = F_WRLCK;
362             last_lock.l_whence = SEEK_SET;
363             last_lock.l_start = sizeof(last_login) * (off_t) uid;
364             last_lock.l_len = sizeof(last_login);
365
366             if ( fcntl(last_fd, F_SETLK, &last_lock) < 0 ) {
367                 D(("locking %s failed..(waiting a little)", _PATH_LASTLOG));
368                 _log_err(LOG_ALERT, "%s file is locked/write", _PATH_LASTLOG);
369                 sleep(LASTLOG_IGNORE_LOCK_TIME);
370             }
371
372             D(("writing to the last_log file"));
373             (void) write(last_fd, &last_login, sizeof(last_login));
374
375             last_lock.l_type = F_UNLCK;
376             (void) fcntl(last_fd, F_SETLK, &last_lock);        /* unlock */
377             D(("unlocked"));
378
379             close(last_fd);                                  /* all done */
380         }
381         D(("all done with last login"));
382     }
383
384     /* reset the last login structure */
385     memset(&last_login, 0, sizeof(last_login));
386
387     return retval;
388 }
389
390 /* --- authentication management functions (only) --- */
391
392 PAM_EXTERN
393 int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc
394                         , const char **argv)
395 {
396     int retval, ctrl;
397     const char *user;
398     const struct passwd *pwd;
399     uid_t uid;
400
401     /*
402      * this module gets the uid of the PAM_USER. Uses it to display
403      * last login info and then updates the lastlog for that user.
404      */
405
406     ctrl = _pam_parse(flags, argc, argv);
407
408     /* which user? */
409
410     retval = pam_get_item(pamh, PAM_USER, (const void **)&user);
411     if (retval != PAM_SUCCESS || user == NULL || *user == '\0') {
412         _log_err(LOG_NOTICE, "user unknown");
413         return PAM_USER_UNKNOWN;
414     }
415
416     /* what uid? */
417
418     pwd = getpwnam(user);
419     if (pwd == NULL) {
420         D(("couldn't identify user %s", user));
421         return PAM_CRED_INSUFFICIENT;
422     }
423     uid = pwd->pw_uid;
424     pwd = NULL;                                         /* tidy up */
425
426     /* process the current login attempt (indicate last) */
427
428     retval = last_login_date(pamh, ctrl, uid);
429
430     /* indicate success or failure */
431
432     uid = -1;                                           /* forget this */
433
434     return retval;
435 }
436
437 PAM_EXTERN
438 int pam_sm_close_session(pam_handle_t *pamh,int flags,int argc
439                          ,const char **argv)
440 {
441     return PAM_SUCCESS;
442 }
443
444 #ifdef PAM_STATIC
445
446 /* static module data */
447
448 struct pam_module _pam_lastlog_modstruct = {
449      "pam_lastlog",
450      NULL,
451      NULL,
452      NULL,
453      pam_sm_open_session,
454      pam_sm_close_session,
455      NULL,
456 };
457
458 #endif
459
460 /* end of module definition */