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
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2014 Nexenta Systems, Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fcntl.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_impl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#define TIMESTAMP_DIR "/var/run/tty_timestamps"
#define TIMESTAMP_TIMEOUT 5 /* default timeout */
#define ROOT_UID 0 /* root uid */
#define ROOT_GID 0 /* root gid */
struct user_info {
dev_t dev; /* ID of device tty resides on */
dev_t rdev; /* tty device ID */
ino_t ino; /* tty inode number */
uid_t uid; /* user's uid */
pid_t ppid; /* parent pid */
pid_t sid; /* session ID associated with tty/ppid */
timestruc_t ts; /* time of tty last status change */
};
int debug = 0;
int
validate_basic(
pam_handle_t *pamh,
char *user_tty,
char *timestampfile)
{
char *user;
char *auser;
char *ttyn;
/* get user, auser and users's tty */
(void) pam_get_item(pamh, PAM_USER, (void **)&user);
(void) pam_get_item(pamh, PAM_AUSER, (void **)&auser);
(void) pam_get_item(pamh, PAM_TTY, (void **)&ttyn);
if (user == NULL || *user == '\0') {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"PAM_USER NULL or empty");
return (PAM_IGNORE);
}
if (auser == NULL || *auser == '\0') {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"PAM_AUSER NULL or empty");
return (PAM_IGNORE);
}
if (ttyn == NULL || *ttyn == '\0') {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"PAM_TTY NULL or empty");
return (PAM_IGNORE);
}
if (debug)
syslog(LOG_AUTH | LOG_DEBUG, "pam_timestamp: "
"user = %s, auser = %s, tty = %s", user, auser, ttyn);
(void) strlcpy(user_tty, ttyn, MAXPATHLEN);
if (strchr(ttyn, '/') == NULL || strncmp(ttyn, "/dev/", 5) == 0) {
ttyn = strrchr(ttyn, '/') + 1;
} else {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"invalid tty: %s", ttyn);
return (PAM_IGNORE);
}
/* format timestamp file name */
(void) snprintf(timestampfile, MAXPATHLEN, "%s/%s/%s:%s", TIMESTAMP_DIR,
auser, ttyn, user);
return (PAM_SUCCESS);
}
int
validate_dir(const char *dir)
{
struct stat sb;
/*
* check that the directory exist and has
* right owner and permissions.
*/
if (lstat(dir, &sb) < 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"directory %s does not exist", dir);
return (PAM_IGNORE);
}
if (!S_ISDIR(sb.st_mode)) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"%s is not a directory", dir);
return (PAM_IGNORE);
}
if (S_ISLNK(sb.st_mode)) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"%s is a symbolic link", dir);
return (PAM_IGNORE);
}
if (sb.st_uid != 0 || sb.st_gid != 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"%s is not owned by root", dir);
return (PAM_IGNORE);
}
if (sb.st_mode & (S_IWGRP | S_IWOTH | S_IROTH)) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"%s has wrong permissions", dir);
return (PAM_IGNORE);
}
return (PAM_SUCCESS);
}
int
create_dir(char *dir)
{
/*
* create directory if it doesn't exist and attempt to set
* the owner to root.
*/
if (mkdir(dir, S_IRWXU) < 0) {
if (errno != EEXIST) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't create directory %s", dir);
return (PAM_IGNORE);
}
} else if (lchown(dir, ROOT_UID, ROOT_GID) < 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't set permissions on directory %s", dir);
return (PAM_IGNORE);
}
return (PAM_SUCCESS);
}
/*
* pam_sm_authenticate
*
* Read authentication from user, using cached successful authentication
* attempts.
*
* returns PAM_SUCCESS on success, otherwise always returns PAM_IGNORE:
* while this module has "sufficient" control value, in case of any failure
* user will be authenticated with the pam_unix_auth module.
* options -
* debug
* timeout= timeout in min, default is 5
*/
/*ARGSUSED*/
int
pam_sm_authenticate(
pam_handle_t *pamh,
int flags,
int argc,
const char **argv)
{
struct user_info info;
struct stat sb, tty;
time_t timeout = 0;
long tmp = 0;
int result = PAM_IGNORE;
int i;
int fd = -1;
char *p;
char user_tty[MAXPATHLEN];
char timestampdir[MAXPATHLEN];
char timestampfile[MAXPATHLEN];
char *sudir;
timeout = TIMESTAMP_TIMEOUT;
/* check options passed to this module */
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "debug") == 0) {
debug = 1;
} else if (strncmp(argv[i], "timeout=", 8) == 0) {
tmp = strtol(argv[i] + 8, &p, 0);
if ((p != NULL) && (*p == '\0') && tmp > 0) {
timeout = tmp;
}
}
}
if (validate_basic(pamh, user_tty, timestampfile) != PAM_SUCCESS)
return (result);
sudir = TIMESTAMP_DIR;
if (validate_dir(sudir) != PAM_SUCCESS)
return (result);
(void) strlcpy(timestampdir, timestampfile, MAXPATHLEN);
if (validate_dir(dirname(timestampdir)) != PAM_SUCCESS)
return (result);
/*
* check that timestamp file is exist and has right owner
* and permissions.
*/
if (lstat(timestampfile, &sb) == 0 && sb.st_size != 0) {
if (!S_ISREG(sb.st_mode)) {
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"timestamp file %s is not a regular file",
timestampfile);
return (result);
}
if (sb.st_uid != 0 || sb.st_gid != 0) {
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"timestamp file %s is not owned by root",
timestampfile);
return (result);
}
if (sb.st_nlink != 1 || S_ISLNK(sb.st_mode)) {
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"timestamp file %s is a symbolic link",
timestampfile);
return (result);
}
if (sb.st_mode & (S_IRWXG | S_IRWXO)) {
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"timestamp file %s has wrong permissions",
timestampfile);
return (result);
}
} else {
if (debug)
syslog(LOG_AUTH | LOG_DEBUG, "pam_timestamp: "
"timestamp file %s does not exist: %m",
timestampfile);
return (result);
}
if (stat(user_tty, &tty) < 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't stat tty: %m");
return (result);
}
if ((fd = open(timestampfile, O_RDONLY)) < 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't open timestamp file %s for reading: %m",
timestampfile);
return (result);
}
if (read(fd, &info, sizeof (info)) != sizeof (info)) {
(void) close(fd);
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"timestamp file '%s' is corrupt: %m", timestampfile);
return (result);
}
if (info.dev != tty.st_dev || info.ino != tty.st_ino ||
info.rdev != tty.st_rdev || info.sid != getsid(getpid()) ||
info.uid != getuid() || info.ts.tv_sec != tty.st_ctim.tv_sec ||
info.ts.tv_nsec != tty.st_ctim.tv_nsec) {
(void) close(fd);
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"the content of the timestamp file '%s' is not valid",
timestampfile);
return (result);
}
if (time((time_t *)0) - sb.st_mtime > 60 * timeout) {
(void) unlink(timestampfile);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"timestamp file '%s' has expired, disallowing access",
timestampfile);
return (result);
} else {
if (debug)
syslog(LOG_AUTH | LOG_DEBUG, "pam_timestamp: "
"timestamp file %s is not expired, "
"allowing access ", timestampfile);
result = PAM_SUCCESS;
}
return (result);
}
/*
* pam_sm_setcred
*
* Creates timestamp directory and writes
* timestamp file if it doesn't exist.
*
* returns PAM_SUCCESS on success, otherwise PAM_IGNORE
*/
/*ARGSUSED*/
int
pam_sm_setcred(
pam_handle_t *pamh,
int flags,
int argc,
const char **argv)
{
struct stat sb;
struct stat tty;
struct user_info info;
int result = PAM_IGNORE;
int fd = -1;
char user_tty[MAXPATHLEN];
char timestampdir[MAXPATHLEN];
char timestampfile[MAXPATHLEN];
/* validate flags */
if (flags && !(flags & PAM_ESTABLISH_CRED) &&
!(flags & PAM_REINITIALIZE_CRED) &&
!(flags & PAM_REFRESH_CRED) &&
!(flags & PAM_DELETE_CRED) &&
!(flags & PAM_SILENT)) {
syslog(LOG_ERR, "pam_timestamp: illegal flag %d", flags);
return (result);
}
if (validate_basic(pamh, user_tty, timestampfile) != PAM_SUCCESS)
return (result);
/*
* user doesn't need to authenticate for PAM_DELETE_CRED
*/
if (flags & PAM_DELETE_CRED) {
(void) unlink(timestampfile);
return (result);
}
/* if the timestamp file exist, there is nothing to do */
if (lstat(timestampfile, &sb) == 0) {
if (debug)
syslog(LOG_AUTH | LOG_DEBUG, "pam_timestamp: "
"timestamp file %s is not expired", timestampfile);
return (result);
}
if (create_dir(TIMESTAMP_DIR) != PAM_SUCCESS)
return (result);
(void) strlcpy(timestampdir, timestampfile, MAXPATHLEN);
if (create_dir(dirname(timestampdir)) != PAM_SUCCESS)
return (result);
if (stat(user_tty, &tty) < 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't stat tty: %m");
return (result);
}
info.dev = tty.st_dev;
info.ino = tty.st_ino;
info.rdev = tty.st_rdev;
info.sid = getsid(getpid());
info.uid = getuid();
info.ts = tty.st_ctim;
if ((fd = open(timestampfile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't open timestamp file %s for writing: %m",
timestampfile);
return (result);
} else if (fchown(fd, ROOT_UID, ROOT_GID) != 0) {
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't set permissions on timestamp file %s: %m",
timestampfile);
(void) close(fd);
return (result);
}
if (write(fd, &info, sizeof (info)) != sizeof (info)) {
(void) close(fd);
syslog(LOG_AUTH | LOG_ERR, "pam_timestamp: "
"can't write timestamp file %s: %m", timestampfile);
return (result);
}
(void) close(fd);
return (PAM_SUCCESS);
}
|