-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_session_timelimit.c
615 lines (505 loc) · 14.3 KB
/
pam_session_timelimit.c
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
*
* Copyright (c) 2023 Steve Langasek <vorlon@dodds.net>
*
* pam_session_timelimit is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* pam_session_timelimit is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <security/pam_modules.h>
#include <security/pam_ext.h>
#include "time-util.h"
#define UNUSED __attribute__((unused))
#define DEFAULT_CONFIG_PATH CONFIGDIR "/time_limits.conf"
#define DEFAULT_STATE_PATH LOCALSTATEDIR "/lib/session_times"
static void cleanup(pam_handle_t *handle UNUSED, void *data, int err UNUSED)
{
if (!data)
return;
free(data);
}
/* returns fd, or -1 on failure */
static int open_state_path (const pam_handle_t *handle, const char *statepath)
{
int fd, retval;
ssize_t bytes;
char buf[12];
if (geteuid() == 0) {
/* must set the real uid to 0 so the helper will not error
out if pam is called from setuid binary (su, sudo...) */
if (setuid(0) == -1) {
pam_syslog(handle, LOG_ERR,
"Could not gain root privilege: %s",
strerror(errno));
return -1;
}
}
fd = open(statepath, O_RDWR);
if (fd < 0 && errno == ENOENT) {
fd = open(statepath, O_RDWR|O_CREAT, 0600);
if (fd < 0) {
pam_syslog(handle, LOG_ERR,
"Could not create statefile: %s",
strerror(errno));
return -1;
}
retval = flock(fd, LOCK_EX);
if (retval < 0) {
pam_syslog(handle, LOG_ERR,
"Could not lock statefile: %s",
strerror(errno));
close(fd);
return -1;
}
strncpy(buf, "Format: ", 9);
/* This file format is not portable between systems of
different endianness */
*((uint32_t *)(buf+8)) = 1;
bytes = write(fd, buf, 12);
if (bytes != 12) {
pam_syslog(handle, LOG_ERR,
"Could not initialize statefile: %s",
strerror(errno));
close(fd);
return -1;
}
return fd;
}
if (fd < 0) {
pam_syslog(handle, LOG_ERR, "Could not open statefile: %s",
strerror(errno));
return -1;
}
retval = flock(fd, LOCK_EX);
if (retval < 0) {
pam_syslog(handle, LOG_ERR,
"Could not lock statefile: %s",
strerror(errno));
close(fd);
return -1;
}
bytes = read(fd, buf, 12);
if (bytes != 12) {
pam_syslog(handle, LOG_ERR, "Could not read from statefile: %s",
strerror(errno));
close(fd);
return -1;
}
if (strncmp(buf, "Format: ", 8) != 0
|| *((uint32_t *)(buf+8)) != 1)
{
pam_syslog(handle, LOG_ERR, "Unknown statefile format");
close(fd);
return -1;
}
return fd;
}
static time_t time_today(void) {
struct tm current_tm;
time_t current_time = time(NULL);
if (localtime_r(¤t_time, ¤t_tm) == NULL) {
return -1;
}
// get the time at 00:00:00 today
current_tm.tm_sec = current_tm.tm_min = current_tm.tm_hour = 0;
// we query the local time, but we write in GMT so that the session
// limits don't get reset if the system timezone changes
return timegm(¤t_tm);
}
static int get_used_time_for_user(const pam_handle_t *handle,
const char *statepath,
const char *username,
usec_t *used_time)
{
char buf[NAME_MAX+1 + sizeof(time_t) + sizeof(usec_t)];
ssize_t read_bytes, buf_bytes = 0;
int retval = PAM_SUCCESS;
int state_file = open_state_path(handle, statepath);
*used_time = 0;
if (state_file < 0)
return PAM_SYSTEM_ERR;
do {
if (buf_bytes == sizeof(buf)) {
// found the record for this user
if (!strncmp(username, buf, NAME_MAX+1)) {
time_t last_seen;
memcpy(&last_seen, buf + NAME_MAX+1,
sizeof(time_t));
/* record is for a different day, so doesn't
count against us */
if (last_seen < time_today())
break;
memcpy(used_time,
buf + NAME_MAX+1 + sizeof(time_t),
sizeof(usec_t));
break;
}
buf_bytes = 0;
}
read_bytes = read(state_file, buf, sizeof(buf) - buf_bytes);
if (read_bytes < 0) {
if (errno == EINTR)
continue;
retval = PAM_SYSTEM_ERR;
}
buf_bytes += read_bytes;
} while (read_bytes != 0);
close(state_file);
return retval;
}
static int set_used_time_for_user(const pam_handle_t *handle,
const char *statepath,
const char *username,
usec_t used_time)
{
char buf[NAME_MAX+1 + sizeof(time_t) + sizeof(usec_t)];
ssize_t read_bytes, buf_bytes = 0;
int state_file = open_state_path(handle, statepath);
if (state_file < 0)
return PAM_SYSTEM_ERR;
do {
if (buf_bytes == sizeof(buf)) {
// found the record for this user
if (!strncmp(username, buf, NAME_MAX+1)) {
// found our record, so rewind to the start
lseek(state_file, -sizeof(buf), SEEK_CUR);
break;
}
buf_bytes = 0;
}
read_bytes = read(state_file, buf, sizeof(buf) - buf_bytes);
if (read_bytes < 0) {
if (errno == EINTR)
continue;
close(state_file);
return PAM_SYSTEM_ERR;
}
buf_bytes += read_bytes;
} while (read_bytes != 0);
memset(buf, '\0', sizeof(buf));
strncpy(buf, username, NAME_MAX+1);
*((time_t *)(buf + NAME_MAX + 1)) = time_today();
*((usec_t *)(buf + NAME_MAX + 1 + sizeof(time_t))) = used_time;
buf_bytes = write(state_file, buf, sizeof(buf));
close(state_file);
if (buf_bytes != sizeof(buf)) {
pam_syslog(handle, LOG_ERR,
"Could not update statefile: %s",
strerror(errno));
return PAM_SYSTEM_ERR;
}
return PAM_SUCCESS;
}
static void free_config_file(char **user_table)
{
int i;
for (i = 0; user_table[i]; i += 2)
{
free(user_table[i]);
free(user_table[i+1]);
}
free(user_table);
}
static int parse_config_line(char *line, char **user, char **limit)
{
size_t length;
int i;
char *comment;
*user = NULL;
*limit = NULL;
length = strlen(line);
/* line >= 1024 chars, go away */
if (line[length-1] != '\n')
return PAM_BUF_ERR;
/* remove trailing newline */
line[--length] = '\0';
/* strip comments */
comment = strchr(line, '#');
if (comment) {
*comment = '\0';
length = comment - line;
}
/* eat trailing whitespace */
while (isspace(line[length-1]))
line[--length] = '\0';
/* comment-only or empty line */
if (!length)
return PAM_SUCCESS;
/* find the end of the username */
for (i = 0; i <= length; i++) {
if (isspace(line[i]))
break;
}
/* no leading whitespace allowed */
if (!i)
return PAM_SYSTEM_ERR;
*user = malloc(i+1);
if (!*user)
return PAM_BUF_ERR;
if (!strncpy(*user, line, i)) {
free(*user);
*user = NULL;
return PAM_BUF_ERR;
}
(*user)[i] = '\0';
/* skip whitespace to find the start of the limit */
line += i;
while (isspace(*line))
line++;
/* no limit specified */
if (*line == '\0') {
free(*user);
*user = NULL;
return PAM_SYSTEM_ERR;
}
*limit = strdup(line);
return PAM_SUCCESS;
}
static int parse_config_file(pam_handle_t *handle, const char *path,
char ***user_table)
{
FILE *config_file;
struct stat statbuf;
int usercount = 0;
char line[1024];
char **results;
*user_table = NULL;
if (stat(path, &statbuf)) {
pam_syslog(handle, LOG_INFO,
"No config file for module, ignoring.");
return PAM_IGNORE;
}
config_file = fopen(path, "r");
if (config_file == NULL) {
pam_syslog(handle, LOG_ERR,
"Failed to open config file '%s': %s",
path, strerror(errno));
return PAM_PERM_DENIED;
}
results = malloc(sizeof(char *));
results[0] = NULL;
while (fgets(line, sizeof(line), config_file)) {
int ret;
char *user = NULL;
char *limit = NULL;
char **newresults;
ret = parse_config_line(line, &user, &limit);
if (ret != PAM_SUCCESS) {
free_config_file(results);
pam_syslog(handle, LOG_ERR, "invalid config file '%s'",
path);
return PAM_PERM_DENIED;
}
if (!user || !limit)
{
free(user);
free(limit);
continue;
}
newresults = reallocarray(results, sizeof(char *),
++usercount * 2 + 1);
if (!newresults) {
free(user);
free(limit);
free_config_file(results);
return PAM_BUF_ERR;
}
results = newresults;
results[(usercount - 1) * 2] = user;
results[(usercount - 1) * 2 + 1] = limit;
results[usercount * 2] = NULL;
}
if (!usercount) {
free(results);
return PAM_IGNORE;
}
*user_table = results;
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_open_session(pam_handle_t *handle,
int flags,
int argc, const char **argv)
{
int retval;
time_t *current_time = malloc(sizeof(time_t));
if (!current_time)
return PAM_BUF_ERR;
*current_time = time(NULL);
retval = pam_set_data(handle, "timelimit.session_start",
(void *)current_time, cleanup);
if (retval != PAM_SUCCESS) {
free(current_time);
return PAM_SYSTEM_ERR;
}
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_close_session(pam_handle_t *handle,
int flags,
int argc, const char **argv)
{
int retval;
const char *statepath = NULL, *username = NULL;
usec_t elapsed_time, used_time = 0;
time_t *start_time, end_time = time(NULL);
char *runtime_max_sec = NULL;
// if no time limit is set for us, then short-circuit to avoid
// creating an unnecessarily large state file
retval = pam_get_data(handle, "systemd.runtime_max_sec",
(const void **)&runtime_max_sec);
if (retval != PAM_SUCCESS || runtime_max_sec == NULL)
return PAM_SUCCESS;
retval = pam_get_data(handle, "timelimit.session_start",
(const void **)&start_time);
for (; argc-- > 0; ++argv) {
if (strncmp(*argv, "statepath=", strlen("statepath="))
== 0)
statepath = *argv + strlen("statepath=");
else {
pam_syslog(handle, LOG_ERR,
"Unknown module argument: %s", *argv);
return PAM_SYSTEM_ERR;
}
}
if (!statepath)
statepath = DEFAULT_STATE_PATH;
retval = pam_get_data(handle, "timelimit.session_start",
(const void **)&start_time);
if (retval != PAM_SUCCESS) {
pam_syslog(handle, LOG_ERR, "start time missing from session");
return PAM_SESSION_ERR;
}
if (end_time < *start_time) {
pam_syslog(handle, LOG_ERR, "session start time in the future");
return PAM_SESSION_ERR;
}
elapsed_time = (end_time - *start_time) * USEC_PER_SEC;
retval = pam_get_item(handle, PAM_USER, (const void **)&username);
if (retval != PAM_SUCCESS)
return retval;
if (!username)
return PAM_SESSION_ERR;
retval = get_used_time_for_user(handle, statepath, username,
&used_time);
if (retval != PAM_SUCCESS) {
return PAM_SESSION_ERR;
}
if (USEC_INFINITY - used_time < elapsed_time)
elapsed_time = USEC_INFINITY;
else
elapsed_time += used_time;
retval = set_used_time_for_user(handle, statepath, username,
elapsed_time);
if (retval != PAM_SUCCESS)
return PAM_SESSION_ERR;
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *handle,
int flags,
int argc, const char **argv)
{
const char *path = NULL, *statepath = NULL, *username = NULL;
char *current_limit = NULL, *runtime_max_sec = NULL;
char **user_table;
unsigned int i;
int retval;
usec_t timeval = 0, old_timeval = 0, used_time = 0;
for (; argc-- > 0; ++argv) {
if (strncmp(*argv, "path=", strlen("path=")) == 0)
path = *argv + strlen("path=");
else if (strncmp(*argv, "statepath=", strlen("statepath="))
== 0)
statepath = *argv + strlen("statepath=");
else {
pam_syslog(handle, LOG_ERR,
"Unknown module argument: %s", *argv);
return PAM_PERM_DENIED;
}
}
if (!path)
path = DEFAULT_CONFIG_PATH;
if (!statepath)
statepath = DEFAULT_STATE_PATH;
retval = pam_get_item(handle, PAM_USER, (const void **)&username);
/* Uh we don't know the user we're acting for? Yeah, bail. */
if (retval != PAM_SUCCESS)
return retval;
if (!username)
return PAM_PERM_DENIED;
retval = parse_config_file(handle, path, &user_table);
if (retval != PAM_SUCCESS)
return retval;
for (i = 0; user_table[i]; i += 2)
{
if (!strcmp(user_table[i], username))
{
runtime_max_sec = user_table[i+1];
pam_syslog(handle, LOG_INFO,
"Limiting user login time for '%s' to '%s'",
username, runtime_max_sec);
}
}
if (!runtime_max_sec) {
free_config_file(user_table);
return PAM_IGNORE;
}
retval = parse_time(runtime_max_sec, &timeval, USEC_PER_SEC);
free_config_file(user_table);
if (retval) {
pam_syslog(handle, LOG_ERR,
"Invalid time limit '%s'", runtime_max_sec);
return PAM_PERM_DENIED;
}
retval = get_used_time_for_user(handle, statepath, username,
&used_time);
if (retval != PAM_SUCCESS) {
return PAM_PERM_DENIED;
}
if (timeval <= used_time)
return PAM_PERM_DENIED;
timeval -= used_time;
pam_get_data(handle, "systemd.runtime_max_sec",
(const void **)¤t_limit);
if (current_limit) {
retval = parse_time(current_limit, &old_timeval, USEC_PER_SEC);
timeval = MIN(old_timeval, timeval);
}
if (timeval != old_timeval) {
runtime_max_sec = malloc(FORMAT_TIMESPAN_MAX);
if (!format_timespan(runtime_max_sec, FORMAT_TIMESPAN_MAX,
timeval, USEC_PER_SEC))
{
free((void *)runtime_max_sec);
return PAM_PERM_DENIED;
}
retval = pam_set_data(handle, "systemd.runtime_max_sec",
(void *)runtime_max_sec, cleanup);
if (retval != PAM_SUCCESS) {
free((void *)runtime_max_sec);
retval = PAM_PERM_DENIED;
}
}
return retval;
}