summaryrefslogtreecommitdiffstats
path: root/pam_lastlog2/src/pam_lastlog2.c
blob: e800700bf5879595c41b7691318409cd13d85422 (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
/* SPDX-License-Identifier: BSD-2-Clause

  Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <security/pam_modules.h>
#include <security/pam_ext.h>
#include <security/pam_modutil.h>
#include <security/_pam_macros.h>

#include "lastlog2.h"
#include "strutils.h"

#define LASTLOG2_DEBUG        01  /* send info to syslog(3) */
#define LASTLOG2_QUIET        02  /* keep quiet about things */

static const char *lastlog2_path = LL2_DEFAULT_DATABASE;

/* check for list match. */
static int
check_in_list (const char *service, const char *arg)
{
	const char *item;
	const char *remaining;

	if (!service)
		return 0;

	remaining = arg;

	for (;;) {
		item = strstr (remaining, service);
		if (item == NULL)
			break;

		/* is it really the start of an item in the list? */
		if (item == arg || *(item - 1) == ',') {
			item += strlen (service);
			/* is item really the service? */
			if (*item == '\0' || *item == ',')
				return 1;
		}

		remaining = strchr (item, ',');
		if (remaining == NULL)
			break;

		/* skip ',' */
		++remaining;
	}

	return 0;
}


static int
_pam_parse_args (pam_handle_t *pamh,
		 int flags, int argc,
		 const char **argv)
{
	int ctrl = 0;
	const char *str;

	/* does the application require quiet? */
	if (flags & PAM_SILENT)
		ctrl |= LASTLOG2_QUIET;

	/* step through arguments */
	for (; argc-- > 0; ++argv) {
		if (strcmp (*argv, "debug") == 0)
			ctrl |= LASTLOG2_DEBUG;
		else if (strcmp (*argv, "silent") == 0)
			ctrl |= LASTLOG2_QUIET;
		else if ((str = startswith (*argv, "database=")) != NULL)
			lastlog2_path = str;
		else if ((str = startswith (*argv, "silent_if=")) != NULL) {
			const void *void_str = NULL;
			const char *service;
			if ((pam_get_item (pamh, PAM_SERVICE, &void_str) != PAM_SUCCESS) ||
			    void_str == NULL)
				service = "";
			else
				service = void_str;

			if (check_in_list (service, str)) {
				if (ctrl & LASTLOG2_DEBUG)
					pam_syslog (pamh, LOG_DEBUG, "silent_if='%s' contains '%s'", str, service);
				ctrl |= LASTLOG2_QUIET;
			}
		} else
			pam_syslog (pamh, LOG_ERR, "Unknown option: %s", *argv);
	}

	return ctrl;
}

static int
write_login_data (pam_handle_t *pamh, int ctrl, const char *user)
{
	const void *void_str;
	const char *tty;
	const char *rhost;
	const char *pam_service;
	const char *xdg_vtnr;
	int xdg_vtnr_nr;
	char tty_buf[8];
	time_t ll_time;
	char *error = NULL;
	int retval;

	void_str = NULL;
	retval = pam_get_item (pamh, PAM_TTY, &void_str);
	if (retval != PAM_SUCCESS || void_str == NULL)
		tty = "";
	else
		tty = void_str;

	/* strip leading "/dev/" from tty. */
	const char *str = startswith(tty, "/dev/");
	if (str != NULL)
		tty = str;

	if (ctrl & LASTLOG2_DEBUG)
		pam_syslog (pamh, LOG_DEBUG, "tty=%s", tty);

	/* if PAM_TTY is not set or an X11 $DISPLAY, try XDG_VTNR */
	if ((tty[0] == '\0' || strchr(tty, ':') != NULL) && (xdg_vtnr = pam_getenv (pamh, "XDG_VTNR")) != NULL) {
		xdg_vtnr_nr = atoi (xdg_vtnr);
		if (xdg_vtnr_nr > 0 && snprintf (tty_buf, sizeof(tty_buf), "tty%d", xdg_vtnr_nr) < (int) sizeof(tty_buf)) {
			tty = tty_buf;
			if (ctrl & LASTLOG2_DEBUG)
				pam_syslog (pamh, LOG_DEBUG, "tty(XDG_VTNR)=%s", tty);
		}
	}

	void_str = NULL;
	retval = pam_get_item (pamh, PAM_RHOST, &void_str);
	if (retval != PAM_SUCCESS || void_str == NULL) {
		void_str = NULL;
		retval = pam_get_item (pamh, PAM_XDISPLAY, &void_str);
		if (retval != PAM_SUCCESS || void_str == NULL) {
			rhost = "";
		} else {
			rhost = void_str;
			if (ctrl & LASTLOG2_DEBUG)
				pam_syslog (pamh, LOG_DEBUG, "rhost(PAM_XDISPLAY)=%s", rhost);
		}
	} else {
		rhost = void_str;
		if (ctrl & LASTLOG2_DEBUG)
			pam_syslog (pamh, LOG_DEBUG, "rhost(PAM_RHOST)=%s", rhost);
	}

	void_str = NULL;
	if ((pam_get_item (pamh, PAM_SERVICE, &void_str) != PAM_SUCCESS) ||
	    void_str == NULL)
		pam_service = "";
	else
		pam_service = void_str;

	if (time (&ll_time) < 0)
		return PAM_SYSTEM_ERR;

	struct ll2_context *context = ll2_new_context(lastlog2_path);
	if (ll2_write_entry (context, user, ll_time, tty, rhost,
			     pam_service, &error) != 0) {
		if (error) {
			pam_syslog (pamh, LOG_ERR, "%s", error);
			free (error);
		} else
			pam_syslog (pamh, LOG_ERR, "Unknown error writing to database %s", lastlog2_path);
		ll2_unref_context(context);
		return PAM_SYSTEM_ERR;
	}
	ll2_unref_context(context);

	return PAM_SUCCESS;
}

static int
show_lastlogin (pam_handle_t *pamh, int ctrl, const char *user)
{
	int64_t ll_time = 0;
	char *tty = NULL;
	char *rhost = NULL;
	char *service = NULL;
	char *date = NULL;
	char the_time[256];
	char *error = NULL;
	int retval = PAM_SUCCESS;

	if (ctrl & LASTLOG2_QUIET)
		return retval;

	struct ll2_context *context = ll2_new_context(lastlog2_path);
	if (ll2_read_entry (context, user, &ll_time, &tty, &rhost,
			    &service, &error) != 0) {
		if (errno == ENOENT)
		{
			/* DB file not found --> it is OK */
			ll2_unref_context(context);
			free(error);
			return PAM_SUCCESS;
		}
		if (error) {
			pam_syslog (pamh, LOG_ERR, "%s", error);
			free (error);
		} else
			pam_syslog (pamh, LOG_ERR, "Unknown error reading database %s", lastlog2_path);
		ll2_unref_context(context);
		return PAM_SYSTEM_ERR;
	}
	ll2_unref_context(context);

	if (ll_time) {
		struct tm *tm, tm_buf;
		/* this is necessary if you compile this on architectures with
		   a 32bit time_t type. */
		time_t t_time = ll_time;		

		if ((tm = localtime_r (&t_time, &tm_buf)) != NULL) {
			strftime (the_time, sizeof (the_time),
				  " %a %b %e %H:%M:%S %Z %Y", tm);
			date = the_time;
		}
	}

	if (date != NULL || rhost != NULL || tty != NULL)
		retval = pam_info(pamh, "Last login:%s%s%s%s%s",
				  date ? date : "",
				  rhost ? " from " : "",
				  rhost ? rhost : "",
				  tty ? " on " : "",
				  tty ? tty : "");

	_pam_drop(service);
	_pam_drop(rhost);
	_pam_drop(tty);

	return retval;
}

int
pam_sm_authenticate (pam_handle_t *pamh __attribute__((__unused__)),
		     int flags __attribute__((__unused__)),
		     int argc __attribute__((__unused__)),
		     const char **argv __attribute__((__unused__)))
{
	return PAM_IGNORE;
}

int
pam_sm_setcred (pam_handle_t *pamh __attribute__((__unused__)),
		int flags __attribute__((__unused__)),
		int argc __attribute__((__unused__)),
		const char **argv __attribute__((__unused__)))
{
	return PAM_IGNORE;
}

int
pam_sm_acct_mgmt (pam_handle_t *pamh __attribute__((__unused__)),
		  int flags __attribute__((__unused__)),
		  int argc __attribute__((__unused__)),
		  const char **argv __attribute__((__unused__)))
{
	return PAM_IGNORE;
}

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

	ctrl = _pam_parse_args (pamh, flags, argc, argv);

	void_str = NULL;
	int retval = pam_get_item (pamh, PAM_USER, &void_str);
	if (retval != PAM_SUCCESS || void_str == NULL || strlen (void_str) == 0) {
		if (!(ctrl & LASTLOG2_QUIET))
			pam_syslog (pamh, LOG_NOTICE, "User unknown");
		return PAM_USER_UNKNOWN;
	}
	user = void_str;

	/* verify the user exists */
	pwd = pam_modutil_getpwnam (pamh, user);
	if (pwd == NULL) {
		if (ctrl & LASTLOG2_DEBUG)
			pam_syslog (pamh, LOG_DEBUG, "Couldn't find user %s",
				    (const char *)user);
		return PAM_USER_UNKNOWN;
	}

	if (ctrl & LASTLOG2_DEBUG)
		pam_syslog (pamh, LOG_DEBUG, "user=%s", user);

	show_lastlogin (pamh, ctrl, user);

	return write_login_data (pamh, ctrl, user);
}

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