1 /* pam_lastlog module */
6 * Written by Andrew Morgan <morgan@linux.kernel.org> 1996/3/11
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.
13 #include <security/_pam_aconf.h>
27 #include <sys/types.h>
32 #include <pwdb/pwdb_public.h> /* use POSIX front end */
35 #if defined(hpux) || defined(sunos) || defined(solaris)
36 # ifndef _PATH_LASTLOG
37 # define _PATH_LASTLOG "/usr/adm/lastlog"
38 # endif /* _PATH_LASTLOG */
40 # define UT_HOSTSIZE 16
41 # endif /* UT_HOSTSIZE */
43 # define UT_LINESIZE 12
44 # endif /* UT_LINESIZE */
49 char ll_line[UT_LINESIZE];
50 char ll_host[UT_HOSTSIZE]; /* same as in utmp */
54 /* XXX - time before ignoring lock. Is 1 sec enough? */
55 #define LASTLOG_IGNORE_LOCK_TIME 1
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))
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.
78 #define PAM_SM_SESSION
80 #include <security/pam_modules.h>
81 #include <security/_pam_macros.h>
85 static void _log_err(int err, const char *format, ...)
89 va_start(args, format);
90 openlog("PAM-lastlog", LOG_CONS|LOG_PID, LOG_AUTH);
91 vsyslog(err, format, args);
96 /* argument parsing */
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 */
105 static int _pam_parse(int flags, int argc, const char **argv)
107 int ctrl=(LASTLOG_DATE|LASTLOG_HOST|LASTLOG_LINE);
109 /* does the appliction require quiet? */
110 if (flags & PAM_SILENT) {
111 ctrl |= LASTLOG_QUIET;
114 /* step through arguments */
115 for (; argc-- > 0; ++argv) {
117 /* generic options */
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;
132 _log_err(LOG_ERR,"unknown option; %s",*argv);
136 D(("ctrl = %o", ctrl));
140 /* a front end for conversations */
142 static int converse(pam_handle_t *pamh, int ctrl, int nargs
143 , struct pam_message **message
144 , struct pam_response **response)
147 struct pam_conv *conv;
149 D(("begin to converse"));
151 retval = pam_get_item( pamh, PAM_CONV, (const void **) &conv ) ;
152 if ( retval == PAM_SUCCESS ) {
154 retval = conv->conv(nargs, ( const struct pam_message ** ) message
155 , response, conv->appdata_ptr);
157 D(("returned from application's conversation function"));
159 if (retval != PAM_SUCCESS && (ctrl & LASTLOG_DEBUG) ) {
160 _log_err(LOG_DEBUG, "conversation failure [%s]"
161 , pam_strerror(pamh, retval));
165 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
166 , pam_strerror(pamh, retval));
169 D(("ready to return from module conversation"));
171 return retval; /* propagate error status */
174 static int make_remark(pam_handle_t *pamh, int ctrl, const char *remark)
178 if (!(ctrl & LASTLOG_QUIET)) {
179 struct pam_message msg[1], *mesg[1];
180 struct pam_response *resp=NULL;
183 msg[0].msg_style = PAM_TEXT_INFO;
186 retval = converse(pamh, ctrl, 1, mesg, &resp);
190 _pam_drop_reply(resp, 1);
193 D(("keeping quiet"));
194 retval = PAM_SUCCESS;
197 D(("returning %s", pam_strerror(pamh, retval)));
202 * Values for the announce flags..
205 static int last_login_date(pam_handle_t *pamh, int announce, uid_t uid)
207 struct flock last_lock;
208 struct lastlog last_login;
209 int retval = PAM_SESSION_ERR;
212 /* obtain the last login date and all the relevant info */
213 last_fd = open(_PATH_LASTLOG, O_RDWR);
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);
219 retval = PAM_PERM_DENIED;
223 /* read the lastlogin file - for this uid */
224 (void) lseek(last_fd, sizeof(last_login) * (off_t) uid, SEEK_SET);
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);
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);
238 win = ( read(last_fd, &last_login, sizeof(last_login))
239 == sizeof(last_login) );
241 last_lock.l_type = F_UNLCK;
242 (void) fcntl(last_fd, F_SETLK, &last_lock); /* unlock */
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);
249 memset(&last_login, 0, sizeof(last_login));
253 (void) lseek(last_fd, sizeof(last_login) * (off_t) uid, SEEK_SET);
255 if (!(announce & LASTLOG_QUIET)) {
256 if (last_login.ll_time) {
260 the_time = ctime(&last_login.ll_time);
261 the_time[-1+strlen(the_time)] = '\0'; /* delete '\n' */
263 remark = malloc(LASTLOG_MAXSIZE);
264 if (remark == NULL) {
265 D(("no memory for last login remark"));
266 retval = PAM_BUF_ERR;
270 /* printing prefix */
271 at = sprintf(remark, "%s", LASTLOG_INTRO);
273 /* we want the date? */
274 if (announce & LASTLOG_DATE) {
275 at += sprintf(remark+at, LASTLOG_TIME, the_time);
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)];
283 (void) sprintf(format, _LASTLOG_HOST_FORMAT
285 D(("format: %s", format));
286 at += sprintf(remark+at, format, last_login.ll_host);
287 _pam_overwrite(format);
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)];
295 (void) sprintf(format, _LASTLOG_LINE_FORMAT
297 D(("format: %s", format));
298 at += sprintf(remark+at, format, last_login.ll_line);
299 _pam_overwrite(format);
302 /* display requested combo */
303 sprintf(remark+at, "%s", LASTLOG_TAIL);
305 retval = make_remark(pamh, announce, remark);
307 /* free all the stuff malloced */
308 _pam_overwrite(remark);
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);
316 D(("no text was requested"));
317 retval = PAM_SUCCESS;
320 /* write latest value */
322 const char *remote_host=NULL
323 , *terminal_line=DEFAULT_TERM;
325 /* set this login date */
326 D(("set the most recent login time"));
328 (void) time(&last_login.ll_time); /* set the time */
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;
336 /* copy to last_login */
337 strncpy(last_login.ll_host, remote_host
338 , sizeof(last_login.ll_host));
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.. */
350 D(("terminal = %s", terminal_line));
352 /* copy to last_login */
353 strncpy(last_login.ll_line, terminal_line
354 , sizeof(last_login.ll_line));
355 terminal_line = NULL;
357 D(("locking last_log file"));
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);
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);
372 D(("writing to the last_log file"));
373 (void) write(last_fd, &last_login, sizeof(last_login));
375 last_lock.l_type = F_UNLCK;
376 (void) fcntl(last_fd, F_SETLK, &last_lock); /* unlock */
379 close(last_fd); /* all done */
381 D(("all done with last login"));
384 /* reset the last login structure */
385 memset(&last_login, 0, sizeof(last_login));
390 /* --- authentication management functions (only) --- */
393 int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc
398 const struct passwd *pwd;
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.
406 ctrl = _pam_parse(flags, argc, argv);
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;
418 pwd = getpwnam(user);
420 D(("couldn't identify user %s", user));
421 return PAM_CRED_INSUFFICIENT;
424 pwd = NULL; /* tidy up */
426 /* process the current login attempt (indicate last) */
428 retval = last_login_date(pamh, ctrl, uid);
430 /* indicate success or failure */
432 uid = -1; /* forget this */
438 int pam_sm_close_session(pam_handle_t *pamh,int flags,int argc
446 /* static module data */
448 struct pam_module _pam_lastlog_modstruct = {
454 pam_sm_close_session,
460 /* end of module definition */