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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_impl.h>
#include <syslog.h>
#include <pwd.h>
#include <shadow.h>
#include <lastlog.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <libintl.h>
#include <signal.h>
#include <thread.h>
#include <synch.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <crypt.h>
#include <assert.h>
#include <deflt.h>
#include <libintl.h>
#include <passwdutil.h>
#define LASTLOG "/var/adm/lastlog"
#define LOGINADMIN "/etc/default/login"
#define UNIX_AUTH_DATA "SUNW-UNIX-AUTH-DATA"
#define UNIX_AUTHTOK_DATA "SUNW-UNIX-AUTHTOK-DATA"
/*
* Function Declarations
*/
extern void setusershell();
extern int _nfssys(int, void *);
typedef struct _unix_authtok_data_ {
int age_status;
}unix_authtok_data;
/*ARGSUSED*/
static void
unix_cleanup(
pam_handle_t *pamh,
void *data,
int pam_status)
{
free((unix_authtok_data *)data);
}
/*
* check_for_login_inactivity - Check for login inactivity
*
*/
static int
check_for_login_inactivity(
uid_t pw_uid,
struct spwd *shpwd)
{
int fdl;
struct lastlog ll;
int retval;
offset_t offset;
offset = (offset_t)pw_uid * (offset_t)sizeof (struct lastlog);
if ((fdl = open(LASTLOG, O_RDWR|O_CREAT, 0444)) >= 0) {
/*
* Read the last login (ll) time
*/
if (llseek(fdl, offset, SEEK_SET) != offset) {
__pam_log(LOG_AUTH | LOG_ERR,
"pam_unix_acct: pam_sm_acct_mgmt: "
"can't obtain last login info on uid %d "
"(uid too large)", pw_uid);
(void) close(fdl);
return (0);
}
retval = read(fdl, (char *)&ll, sizeof (ll));
/* Check for login inactivity */
if ((shpwd->sp_inact > 0) && (retval == sizeof (ll)) &&
ll.ll_time) {
/*
* account inactive too long.
* and no update password set
* and no last pwd change date in shadow file
* and last pwd change more than inactive time
* then account inactive too long and no access.
*/
if (((time_t)((ll.ll_time / DAY) + shpwd->sp_inact)
< DAY_NOW) &&
(shpwd->sp_lstchg != 0) &&
(shpwd->sp_lstchg != -1) &&
((shpwd->sp_lstchg + shpwd->sp_inact) < DAY_NOW)) {
/*
* Account inactive for too long
*/
(void) close(fdl);
return (1);
}
}
(void) close(fdl);
}
return (0);
}
/*
* new_password_check()
*
* check to see if the user needs to change their password
*/
static int
new_password_check(shpwd, flags)
struct spwd *shpwd;
int flags;
{
time_t now = DAY_NOW;
/*
* We want to make sure that we change the password only if
* passwords are required for the system, the user does not
* have a password, AND the user's NULL password can be changed
* according to its password aging information
*/
if ((flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
if (shpwd->sp_pwdp[0] == '\0') {
if (((shpwd->sp_max == -1) ||
((time_t)shpwd->sp_lstchg > now) ||
((now >= (time_t)(shpwd->sp_lstchg +
shpwd->sp_min)) &&
(shpwd->sp_max >= shpwd->sp_min)))) {
return (PAM_NEW_AUTHTOK_REQD);
}
}
}
return (PAM_SUCCESS);
}
/*
* perform_passwd_aging_check
* - Check for password exipration.
*/
static int
perform_passwd_aging_check(
pam_handle_t *pamh,
struct spwd *shpwd,
int flags)
{
time_t now = DAY_NOW;
int idledays = -1;
char *ptr;
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
void *defp;
if ((defp = defopen_r(LOGINADMIN)) != NULL) {
if ((ptr = defread_r("IDLEWEEKS=", defp)) != NULL)
idledays = 7 * atoi(ptr);
defclose_r(defp);
}
/*
* if (sp_lstchg == 0), the administrator has forced the
* user to change their passwd
*/
if (shpwd->sp_lstchg == 0)
return (PAM_NEW_AUTHTOK_REQD);
/* If password aging is disabled (or min>max), all is well */
if (shpwd->sp_max < 0 || shpwd->sp_max < shpwd->sp_min)
return (PAM_SUCCESS);
/* Password aging is enabled. See if the password has aged */
if (now < (time_t)(shpwd->sp_lstchg + shpwd->sp_max))
return (PAM_SUCCESS);
/* Password has aged. Has it aged more than idledays ? */
if (idledays < 0) /* IDLEWEEKS not configured */
return (PAM_NEW_AUTHTOK_REQD);
/* idledays is configured */
if (idledays > 0 && (now < (time_t)(shpwd->sp_lstchg + idledays)))
return (PAM_NEW_AUTHTOK_REQD);
/* password has aged more that allowed for by IDLEWEEKS */
if (!(flags & PAM_SILENT)) {
(void) strlcpy(messages[0], dgettext(TEXT_DOMAIN,
"Your password has been expired for too long."),
sizeof (messages[0]));
(void) strlcpy(messages[1], dgettext(TEXT_DOMAIN,
"Please contact the system administrator."),
sizeof (messages[0]));
(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 2, messages,
NULL);
}
return (PAM_AUTHTOK_EXPIRED);
}
/*
* warn_user_passwd_will_expire - warn the user when the password will
* expire.
*/
static void
warn_user_passwd_will_expire(
pam_handle_t *pamh,
struct spwd shpwd)
{
time_t now = DAY_NOW;
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
time_t days;
if ((shpwd.sp_warn > 0) && (shpwd.sp_max > 0) &&
(now + shpwd.sp_warn) >= (time_t)(shpwd.sp_lstchg + shpwd.sp_max)) {
days = (time_t)(shpwd.sp_lstchg + shpwd.sp_max) - now;
if (days <= 0)
(void) snprintf(messages[0],
sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password will expire within 24 hours."));
else if (days == 1)
(void) snprintf(messages[0],
sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password will expire in 1 day."));
else
(void) snprintf(messages[0],
sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password will expire in %d days."),
(int)days);
(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages,
NULL);
}
}
/*
* pam_sm_acct_mgmt - main account managment routine.
* Returns: module error or specific error on failure
*/
int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
uid_t pw_uid;
char *repository_name = NULL;
char *user;
attrlist attr_pw[3];
attrlist attr_spw[7];
pwu_repository_t *pwu_rep = PWU_DEFAULT_REP;
pwu_repository_t *auth_rep = NULL;
int error = PAM_ACCT_EXPIRED;
int result;
int i;
int debug = 0;
int server_policy = 0;
unix_authtok_data *status;
struct spwd shpwd = {NULL, NULL,
-1, -1, -1, -1, -1, -1, 0};
for (i = 0; i < argc; i++) {
if (strcasecmp(argv[i], "debug") == 0)
debug = 1;
else if (strcasecmp(argv[i], "server_policy") == 0)
server_policy = 1;
else if (strcasecmp(argv[i], "nowarn") == 0) {
flags = flags | PAM_SILENT;
} else {
__pam_log(LOG_AUTH | LOG_ERR,
"ACCOUNT:pam_sm_acct_mgmt: illegal option %s",
argv[i]);
}
}
if (debug)
__pam_log(LOG_AUTH | LOG_DEBUG,
"pam_unix_account: entering pam_sm_acct_mgmt()");
if ((error = pam_get_item(pamh, PAM_USER, (void **)&user))
!= PAM_SUCCESS)
goto out;
if (user == NULL) {
error = PAM_USER_UNKNOWN;
goto out;
} else
shpwd.sp_namp = user;
if ((error = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep))
!= PAM_SUCCESS)
goto out;
if (auth_rep == NULL) {
pwu_rep = PWU_DEFAULT_REP;
} else {
if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL) {
error = PAM_BUF_ERR;
goto out;
}
pwu_rep->type = auth_rep->type;
pwu_rep->scope = auth_rep->scope;
pwu_rep->scope_len = auth_rep->scope_len;
}
/*
* First get the password information
*/
attr_pw[0].type = ATTR_REP_NAME; attr_pw[0].next = &attr_pw[1];
attr_pw[1].type = ATTR_UID; attr_pw[1].next = &attr_pw[2];
attr_pw[2].type = ATTR_PASSWD; attr_pw[2].next = NULL;
result = __get_authtoken_attr(user, pwu_rep, attr_pw);
if (result == PWU_NOT_FOUND) {
error = PAM_USER_UNKNOWN;
goto out;
} else if (result == PWU_DENIED) {
error = PAM_PERM_DENIED;
goto out;
} else if (result == PWU_NOMEM) {
error = PAM_BUF_ERR;
goto out;
} else if (result != PWU_SUCCESS) {
error = PAM_SERVICE_ERR;
goto out;
} else {
repository_name = attr_pw[0].data.val_s;
pw_uid = attr_pw[1].data.val_i;
shpwd.sp_pwdp = attr_pw[2].data.val_s;
}
/*
* if repository is not files|nis, and user wants server_policy,
* we don't care about aging and hence return PAM_IGNORE
*/
if (server_policy &&
strcmp(repository_name, "files") != 0 &&
strcmp(repository_name, "nis") != 0) {
error = PAM_IGNORE;
goto out;
}
/*
* Now get the aging information
*/
attr_spw[0].type = ATTR_LSTCHG; attr_spw[0].next = &attr_spw[1];
attr_spw[1].type = ATTR_MIN; attr_spw[1].next = &attr_spw[2];
attr_spw[2].type = ATTR_MAX; attr_spw[2].next = &attr_spw[3];
attr_spw[3].type = ATTR_WARN; attr_spw[3].next = &attr_spw[4];
attr_spw[4].type = ATTR_INACT; attr_spw[4].next = &attr_spw[5];
attr_spw[5].type = ATTR_EXPIRE; attr_spw[5].next = &attr_spw[6];
attr_spw[6].type = ATTR_FLAG; attr_spw[6].next = NULL;
result = __get_authtoken_attr(user, pwu_rep, attr_spw);
if (result == PWU_SUCCESS) {
shpwd.sp_lstchg = attr_spw[0].data.val_i;
shpwd.sp_min = attr_spw[1].data.val_i;
shpwd.sp_max = attr_spw[2].data.val_i;
shpwd.sp_warn = attr_spw[3].data.val_i;
shpwd.sp_inact = attr_spw[4].data.val_i;
shpwd.sp_expire = attr_spw[5].data.val_i;
shpwd.sp_flag = attr_spw[6].data.val_i;
}
if (debug) {
char *pw = "Unix PW";
if (shpwd.sp_pwdp == NULL)
pw = "NULL";
else if (strncmp(shpwd.sp_pwdp, LOCKSTRING,
sizeof (LOCKSTRING) - 1) == 0)
pw = LOCKSTRING;
else if (strcmp(shpwd.sp_pwdp, NOPWDRTR) == 0)
pw = NOPWDRTR;
if (result == PWU_DENIED) {
__pam_log(LOG_AUTH | LOG_DEBUG,
"pam_unix_account: %s: permission denied "
"to access password aging information. "
"Using defaults.", user);
}
__pam_log(LOG_AUTH | LOG_DEBUG,
"%s Policy:Unix, pw=%s, lstchg=%d, min=%d, max=%d, "
"warn=%d, inact=%d, expire=%d",
user, pw, shpwd.sp_lstchg, shpwd.sp_min, shpwd.sp_max,
shpwd.sp_warn, shpwd.sp_inact, shpwd.sp_expire);
}
if (pwu_rep != PWU_DEFAULT_REP) {
free(pwu_rep);
pwu_rep = PWU_DEFAULT_REP;
}
if (result == PWU_NOT_FOUND) {
error = PAM_USER_UNKNOWN;
goto out;
} else if (result == PWU_NOMEM) {
error = PAM_BUF_ERR;
goto out;
} else if (result != PWU_SUCCESS && result != PWU_DENIED) {
error = PAM_SERVICE_ERR;
goto out;
}
/*
* Check for locked account
*/
if (shpwd.sp_pwdp != NULL &&
strncmp(shpwd.sp_pwdp, LOCKSTRING, sizeof (LOCKSTRING) - 1) == 0) {
char *service;
char *rhost = NULL;
(void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
(void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
__pam_log(LOG_AUTH | LOG_NOTICE,
"pam_unix_account: %s attempting to validate locked "
"account %s from %s",
service, user,
(rhost != NULL && *rhost != '\0') ? rhost : "local host");
error = PAM_PERM_DENIED;
goto out;
}
/*
* Check for NULL password and, if so, see if such is allowed
*/
if (shpwd.sp_pwdp[0] == '\0' &&
(flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
char *service;
char *rhost = NULL;
(void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
(void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
__pam_log(LOG_AUTH | LOG_NOTICE,
"pam_unix_account: %s: empty password not allowed for "
"account %s from %s", service, user,
(rhost != NULL && *rhost != '\0') ? rhost : "local host");
error = PAM_PERM_DENIED;
goto out;
}
/*
* Check for account expiration
*/
if (shpwd.sp_expire > 0 &&
(time_t)shpwd.sp_expire < DAY_NOW) {
error = PAM_ACCT_EXPIRED;
goto out;
}
/*
* Check for excessive login account inactivity
*/
if (check_for_login_inactivity(pw_uid, &shpwd)) {
error = PAM_PERM_DENIED;
goto out;
}
/*
* Check to see if the user needs to change their password
*/
if (error = new_password_check(&shpwd, flags)) {
goto out;
}
/*
* Check to make sure password aging information is okay
*/
if ((error = perform_passwd_aging_check(pamh, &shpwd, flags))
!= PAM_SUCCESS) {
goto out;
}
/*
* Finally, warn the user if their password is about to expire.
*/
if (!(flags & PAM_SILENT)) {
warn_user_passwd_will_expire(pamh, shpwd);
}
/*
* All done, return Success
*/
error = PAM_SUCCESS;
out:
{
int pam_res;
unix_authtok_data *authtok_data;
if (debug) {
__pam_log(LOG_AUTH | LOG_DEBUG,
"pam_unix_account: %s: %s",
(user == NULL)?"NULL":user,
pam_strerror(pamh, error));
}
if (repository_name)
free(repository_name);
if (pwu_rep != PWU_DEFAULT_REP)
free(pwu_rep);
if (shpwd.sp_pwdp) {
(void) memset(shpwd.sp_pwdp, 0, strlen(shpwd.sp_pwdp));
free(shpwd.sp_pwdp);
}
/* store the password aging status in the pam handle */
pam_res = pam_get_data(pamh, UNIX_AUTHTOK_DATA,
(const void **)&authtok_data);
if ((status = (unix_authtok_data *)calloc(1,
sizeof (unix_authtok_data))) == NULL) {
return (PAM_BUF_ERR);
}
if (pam_res == PAM_SUCCESS)
(void) memcpy(status, authtok_data,
sizeof (unix_authtok_data));
status->age_status = error;
if (pam_set_data(pamh, UNIX_AUTHTOK_DATA, status, unix_cleanup)
!= PAM_SUCCESS) {
free(status);
return (PAM_SERVICE_ERR);
}
}
return (error);
}
|