summaryrefslogtreecommitdiff
path: root/modules/pam_lastlog/pam_lastlog.c
blob: d3690edd63132a742e04901837b902ff75c5b930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* pam_lastlog module */

/*
 * $Id$
 *
 * Written by Andrew Morgan <morgan@linux.kernel.org> 1996/3/11
 *
 * This module does the necessary work to display the last login
 * time+date for this user, it then updates this entry for the
 * present (login) service.
 */

#include "config.h"

#include <fcntl.h>
#include <time.h>
#ifdef HAVE_UTMP_H
# include <utmp.h>
#else
# include <lastlog.h>
#endif
#include <pwd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>

#ifdef WANT_PWDB
#include <pwdb/pwdb_public.h>                /* use POSIX front end */
#endif

#if defined(hpux) || defined(sunos) || defined(solaris)
# ifndef _PATH_LASTLOG
#  define _PATH_LASTLOG "/usr/adm/lastlog"
# endif /* _PATH_LASTLOG */
# ifndef UT_HOSTSIZE
#  define UT_HOSTSIZE 16
# endif /* UT_HOSTSIZE */
# ifndef UT_LINESIZE
#  define UT_LINESIZE 12
# endif /* UT_LINESIZE */
#endif
#if defined(hpux)
struct lastlog {
    time_t  ll_time;
    char    ll_line[UT_LINESIZE];
    char    ll_host[UT_HOSTSIZE];            /* same as in utmp */
};
#endif /* hpux */

/* XXX - time before ignoring lock. Is 1 sec enough? */
#define LASTLOG_IGNORE_LOCK_TIME     1

#define DEFAULT_HOST     ""  /* "[no.where]" */
#define DEFAULT_TERM     ""  /* "tt???" */
#define LASTLOG_NEVER_WELCOME       "Welcome to your new account!"
#define LASTLOG_INTRO    "Last login:"
#define LASTLOG_TIME     " %s"
#define _LASTLOG_HOST_FORMAT   " from %%.%ds"
#define _LASTLOG_LINE_FORMAT   " on %%.%ds"
#define LASTLOG_TAIL     ""
#define LASTLOG_MAXSIZE  (sizeof(LASTLOG_INTRO)+0 \
			  +sizeof(LASTLOG_TIME)+strlen(the_time) \
			  +sizeof(_LASTLOG_HOST_FORMAT)+UT_HOSTSIZE \
			  +sizeof(_LASTLOG_LINE_FORMAT)+UT_LINESIZE \
			  +sizeof(LASTLOG_TAIL))

/*
 * here, we make a definition for the externally accessible function
 * in this file (this definition is required for static a module
 * but strongly encouraged generally) it is used to instruct the
 * modules include file to define the function prototypes.
 */

#define PAM_SM_SESSION

#include <security/pam_modules.h>
#include <security/_pam_macros.h>
#include <security/_pam_modutil.h>

/* some syslogging */

static void _log_err(int err, const char *format, ...)
{
    va_list args;

    va_start(args, format);
    openlog("PAM-lastlog", LOG_CONS|LOG_PID, LOG_AUTH);
    vsyslog(err, format, args);
    va_end(args);
    closelog();
}

/* argument parsing */

#define LASTLOG_DATE        01  /* display the date of the last login */
#define LASTLOG_HOST        02  /* display the last host used (if set) */
#define LASTLOG_LINE        04  /* display the last terminal used */
#define LASTLOG_NEVER      010  /* display a welcome message for first login */
#define LASTLOG_DEBUG      020  /* send info to syslog(3) */
#define LASTLOG_QUIET      040  /* keep quiet about things */

static int _pam_parse(int flags, int argc, const char **argv)
{
    int ctrl=(LASTLOG_DATE|LASTLOG_HOST|LASTLOG_LINE);

    /* does the appliction require quiet? */
    if (flags & PAM_SILENT) {
	ctrl |= LASTLOG_QUIET;
    }

    /* step through arguments */
    for (; argc-- > 0; ++argv) {

	/* generic options */

	if (!strcmp(*argv,"debug")) {
	    ctrl |= LASTLOG_DEBUG;
	} else if (!strcmp(*argv,"nodate")) {
	    ctrl |= ~LASTLOG_DATE;
	} else if (!strcmp(*argv,"noterm")) {
	    ctrl |= ~LASTLOG_LINE;
	} else if (!strcmp(*argv,"nohost")) {
	    ctrl |= ~LASTLOG_HOST;
	} else if (!strcmp(*argv,"silent")) {
	    ctrl |= LASTLOG_QUIET;
	} else if (!strcmp(*argv,"never")) {
	    ctrl |= LASTLOG_NEVER;
	} else {
	    _log_err(LOG_ERR,"unknown option; %s",*argv);
	}
    }

    D(("ctrl = %o", ctrl));
    return ctrl;
}

/* a front end for conversations */

static int converse(pam_handle_t *pamh, int ctrl, int nargs
		    , struct pam_message **message
		    , struct pam_response **response)
{
    int retval;
    const void *void_conv;
    const struct pam_conv *conv;

    D(("begin to converse"));

    retval = pam_get_item( pamh, PAM_CONV, &void_conv ) ;
    conv = (const struct pam_conv *)void_conv;
    if ( retval == PAM_SUCCESS && conv) {

	retval = conv->conv(nargs, ( const struct pam_message ** ) message
			    , response, conv->appdata_ptr);

	D(("returned from application's conversation function"));

	if (retval != PAM_SUCCESS && (ctrl & LASTLOG_DEBUG) ) {
	    _log_err(LOG_DEBUG, "conversation failure [%s]"
		     , pam_strerror(pamh, retval));
	}

    } else {
	_log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
		 , pam_strerror(pamh, retval));
	if (retval == PAM_SUCCESS)
		retval = PAM_BAD_ITEM; /* conv was NULL */
    }

    D(("ready to return from module conversation"));

    return retval;                  /* propagate error status */
}

static int make_remark(pam_handle_t *pamh, int ctrl, const char *remark)
{
    int retval;

    if (!(ctrl & LASTLOG_QUIET)) {
	struct pam_message msg[1], *mesg[1];
	struct pam_response *resp=NULL;

	mesg[0] = &msg[0];
	msg[0].msg_style = PAM_TEXT_INFO;
	msg[0].msg = remark;

	retval = converse(pamh, ctrl, 1, mesg, &resp);

	msg[0].msg = NULL;
	if (resp) {
	    _pam_drop_reply(resp, 1);
	}
    } else {
	D(("keeping quiet"));
	retval = PAM_SUCCESS;
    }

    D(("returning %s", pam_strerror(pamh, retval)));
    return retval;
}

/*
 * Values for the announce flags..
 */

static int last_login_date(pam_handle_t *pamh, int announce, uid_t uid)
{
    struct flock last_lock;
    struct lastlog last_login;
    int retval = PAM_SESSION_ERR;
    int last_fd;

    /* obtain the last login date and all the relevant info */
    last_fd = open(_PATH_LASTLOG, O_RDWR);
    if (last_fd < 0) {
	D(("unable to open the %s file", _PATH_LASTLOG));
	if (announce & LASTLOG_DEBUG) {
	    _log_err(LOG_DEBUG, "unable to open %s file", _PATH_LASTLOG);
	}
	retval = PAM_PERM_DENIED;
    } else {
	int win;

	/* read the lastlogin file - for this uid */
	(void) lseek(last_fd, sizeof(last_login) * (off_t) uid, SEEK_SET);

	memset(&last_lock, 0, sizeof(last_lock));
	last_lock.l_type = F_RDLCK;
	last_lock.l_whence = SEEK_SET;
	last_lock.l_start = sizeof(last_login) * (off_t) uid;
	last_lock.l_len = sizeof(last_login);

	if ( fcntl(last_fd, F_SETLK, &last_lock) < 0 ) {
	    D(("locking %s failed..(waiting a little)", _PATH_LASTLOG));
	    _log_err(LOG_ALERT, "%s file is locked/read", _PATH_LASTLOG);
	    sleep(LASTLOG_IGNORE_LOCK_TIME);
	}

	win = (_pammodutil_read (last_fd, (char *) &last_login,
				 sizeof(last_login)) == sizeof(last_login));

	last_lock.l_type = F_UNLCK;
	(void) fcntl(last_fd, F_SETLK, &last_lock);        /* unlock */

	if (!win) {
	    D(("First login for user uid=%d", _PATH_LASTLOG, uid));
	    if (announce & LASTLOG_DEBUG) {
		_log_err(LOG_DEBUG, "creating lastlog for uid %d", uid);
	    }
	    memset(&last_login, 0, sizeof(last_login));
	}

	/* rewind */
	(void) lseek(last_fd, sizeof(last_login) * (off_t) uid, SEEK_SET);

	if (!(announce & LASTLOG_QUIET)) {
	    if (last_login.ll_time) {
		time_t ll_time;
		char *the_time;
		char *remark;

		ll_time = last_login.ll_time;
		the_time = ctime(&ll_time);
		the_time[-1+strlen(the_time)] = '\0';    /* delete '\n' */

		remark = malloc(LASTLOG_MAXSIZE);
		if (remark == NULL) {
		    D(("no memory for last login remark"));
		    retval = PAM_BUF_ERR;
		} else {
		    int at;

		    /* printing prefix */
		    at = sprintf(remark, "%s", LASTLOG_INTRO);

		    /* we want the date? */
		    if (announce & LASTLOG_DATE) {
			at += sprintf(remark+at, LASTLOG_TIME, the_time);
		    }

		    /* we want & have the host? */
		    if ((announce & LASTLOG_HOST)
			&& (last_login.ll_host[0] != '\0')) {
			char format[2*sizeof(_LASTLOG_HOST_FORMAT)];

			(void) sprintf(format, _LASTLOG_HOST_FORMAT
				       , UT_HOSTSIZE);
			D(("format: %s", format));
			at += sprintf(remark+at, format, last_login.ll_host);
			_pam_overwrite(format);
		    }

		    /* we want and have the terminal? */
		    if ((announce & LASTLOG_LINE)
			&& (last_login.ll_line[0] != '\0')) {
			char format[2*sizeof(_LASTLOG_LINE_FORMAT)];

			(void) sprintf(format, _LASTLOG_LINE_FORMAT
				       , UT_LINESIZE);
			D(("format: %s", format));
			at += sprintf(remark+at, format, last_login.ll_line);
			_pam_overwrite(format);
		    }

		    /* display requested combo */
		    sprintf(remark+at, "%s", LASTLOG_TAIL);

		    retval = make_remark(pamh, announce, remark);

		    /* free all the stuff malloced */
		    _pam_overwrite(remark);
		    _pam_drop(remark);
		}
	    } else if ((!last_login.ll_time) && (announce & LASTLOG_NEVER)) {
		D(("this is the first time this user has logged in"));
		retval = make_remark(pamh, announce, LASTLOG_NEVER_WELCOME);
	    }
	} else {
	    D(("no text was requested"));
	    retval = PAM_SUCCESS;
	}

	/* write latest value */
	{
	    time_t ll_time;
	    const void *remote_host=NULL
		, *void_terminal_line=DEFAULT_TERM;
	    const char *terminal_line;

	    /* set this login date */
	    D(("set the most recent login time"));

	    (void) time(&ll_time);    /* set the time */
            last_login.ll_time = ll_time;

	    /* set the remote host */
	    (void) pam_get_item(pamh, PAM_RHOST, &remote_host);
	    if (remote_host == NULL) {
		remote_host = DEFAULT_HOST;
	    }

	    /* copy to last_login */
	    strncpy(last_login.ll_host, remote_host,
		    sizeof(last_login.ll_host));
	    last_login.ll_host[sizeof(last_login.ll_host) - 1] = '\0';
	    remote_host = NULL;

	    /* set the terminal line */
	    (void) pam_get_item(pamh, PAM_TTY, &void_terminal_line);
	    terminal_line = void_terminal_line;
	    D(("terminal = %s", terminal_line));
	    if (terminal_line == NULL) {
		terminal_line = DEFAULT_TERM;
	    } else if ( !strncmp("/dev/", terminal_line, 5) ) {
		/* strip leading "/dev/" from tty.. */
		terminal_line += 5;
	    }
	    D(("terminal = %s", terminal_line));

	    /* copy to last_login */
	    strncpy(last_login.ll_line, terminal_line,
		    sizeof(last_login.ll_line));
	    last_login.ll_host[sizeof(last_login.ll_host) - 1] = '\0';
	    terminal_line = NULL;

	    D(("locking last_log file"));

	    /* now we try to lock this file-record exclusively; non-blocking */
	    memset(&last_lock, 0, sizeof(last_lock));
	    last_lock.l_type = F_WRLCK;
	    last_lock.l_whence = SEEK_SET;
	    last_lock.l_start = sizeof(last_login) * (off_t) uid;
	    last_lock.l_len = sizeof(last_login);

	    if ( fcntl(last_fd, F_SETLK, &last_lock) < 0 ) {
		D(("locking %s failed..(waiting a little)", _PATH_LASTLOG));
		_log_err(LOG_ALERT, "%s file is locked/write", _PATH_LASTLOG);
		sleep(LASTLOG_IGNORE_LOCK_TIME);
	    }

	    D(("writing to the last_log file"));
	    _pammodutil_write (last_fd, (char *) &last_login,
			        sizeof (last_login));

	    last_lock.l_type = F_UNLCK;
	    (void) fcntl(last_fd, F_SETLK, &last_lock);        /* unlock */
	    D(("unlocked"));

	    close(last_fd);                                  /* all done */
	}
	D(("all done with last login"));
    }

    /* reset the last login structure */
    memset(&last_login, 0, sizeof(last_login));

    return retval;
}

/* --- authentication management functions (only) --- */

PAM_EXTERN
int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc
			, const char **argv)
{
    int retval, ctrl;
    const void *user;
    const struct passwd *pwd;
    uid_t uid;

    /*
     * this module gets the uid of the PAM_USER. Uses it to display
     * last login info and then updates the lastlog for that user.
     */

    ctrl = _pam_parse(flags, argc, argv);

    /* which user? */

    retval = pam_get_item(pamh, PAM_USER, &user);
    if (retval != PAM_SUCCESS || user == NULL || *(const char *)user == '\0') {
	_log_err(LOG_NOTICE, "user unknown");
	return PAM_USER_UNKNOWN;
    }

    /* what uid? */

    pwd = _pammodutil_getpwnam (pamh, user);
    if (pwd == NULL) {
	D(("couldn't identify user %s", user));
	return PAM_CRED_INSUFFICIENT;
    }
    uid = pwd->pw_uid;
    pwd = NULL;                                         /* tidy up */

    /* process the current login attempt (indicate last) */

    retval = last_login_date(pamh, ctrl, uid);

    /* indicate success or failure */

    uid = -1;                                           /* forget this */

    return retval;
}

PAM_EXTERN int
pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
		      int argc UNUSED, const char **argv UNUSED)
{
    return PAM_SUCCESS;
}

#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_lastlog_modstruct = {
     "pam_lastlog",
     NULL,
     NULL,
     NULL,
     pam_sm_open_session,
     pam_sm_close_session,
     NULL,
};

#endif

/* end of module definition */