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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
/*
* 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 (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <nss_dbdefs.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <auth_attr.h>
#include <deflt.h>
#include <priv.h>
#include <secdb.h>
#include <user_attr.h>
#include <sys/task.h>
#include <libintl.h>
#include <project.h>
#include <errno.h>
#include <alloca.h>
#include <bsm/adt.h>
#include <bsm/adt_event.h> /* adt_get_auid() */
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_impl.h>
#define PROJECT "project="
#define PROJSZ (sizeof (PROJECT) - 1)
/*
* unix_cred - PAM auth modules must contain both pam_sm_authenticate
* and pam_sm_setcred. Some other auth module is responsible
* for authentication (e.g., pam_unix_auth.so), this module
* only implements pam_sm_setcred so that the authentication
* can be separated without knowledge of the Solaris Unix style
* credential setting.
* Solaris Unix style credential setting includes initializing
* the audit characteristics if not already initialized and
* setting the user's default and limit privileges.
*/
/*
* unix_cred - pam_sm_authenticate
*
* Returns PAM_IGNORE.
*/
/*ARGSUSED*/
int
pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return (PAM_IGNORE);
}
/*
* Set the privilege set. The attributes are enumerated by _enum_attrs,
* including the attribues user_attr, prof_attr and policy.conf
*/
static int
getset(char *str, priv_set_t **res)
{
priv_set_t *tmp;
char *badp;
int len;
if (str == NULL)
return (0);
len = strlen(str) + 1;
badp = alloca(len);
(void) memset(badp, '\0', len);
do {
const char *q, *endp;
tmp = priv_str_to_set(str, ",", &endp);
if (tmp == NULL) {
if (endp == NULL)
break;
/* Now remove the bad privilege endp points to */
q = strchr(endp, ',');
if (q == NULL)
q = endp + strlen(endp);
if (*badp != '\0')
(void) strlcat(badp, ",", len);
/* Memset above guarantees NUL termination */
/* LINTED */
(void) strncat(badp, endp, q - endp);
/* excise bad privilege; strtok ignores 2x sep */
(void) memmove((void *)endp, q, strlen(q) + 1);
}
} while (tmp == NULL && *str != '\0');
if (tmp == NULL) {
syslog(LOG_AUTH|LOG_ERR,
"pam_setcred: can't parse privilege specification: %m\n");
return (-1);
} else if (*badp != '\0') {
syslog(LOG_AUTH|LOG_DEBUG,
"pam_setcred: unrecognized privilege(s): %s\n", badp);
}
*res = tmp;
return (0);
}
typedef struct deflim {
char *def;
char *lim;
} deflim_t;
/*ARGSUSED*/
static int
finddeflim(const char *name, kva_t *kva, void *ctxt, void *pres)
{
deflim_t *pdef = pres;
char *val;
if (pdef->def == NULL) {
val = kva_match(kva, USERATTR_DFLTPRIV_KW);
if (val != NULL)
pdef->def = strdup(val);
}
if (pdef->lim == NULL) {
val = kva_match(kva, USERATTR_LIMPRIV_KW);
if (val != NULL)
pdef->lim = strdup(val);
}
return (pdef->lim != NULL && pdef->def != NULL);
}
/*
* unix_cred - pam_sm_setcred
*
* Entry flags = PAM_ESTABLISH_CRED, set up Solaris Unix cred.
* PAM_DELETE_CRED, NOP, return PAM_SUCCESS.
* PAM_REINITIALIZE_CRED, set up Solaris Unix cred,
* or merge the current context with the new
* user.
* PAM_REFRESH_CRED, set up Solaris Unix cred.
* PAM_SILENT, print no messages to user.
*
* Returns PAM_SUCCESS, if all successful.
* PAM_CRED_ERR, if unable to set credentials.
* PAM_USER_UNKNOWN, if PAM_USER not set, or unable to find
* user in databases.
* PAM_SYSTEM_ERR, if no valid flag, or unable to get/set
* user's audit state.
*/
int
pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int i;
int debug = 0;
uint_t nowarn = flags & PAM_SILENT;
int ret = PAM_SUCCESS;
char *user;
char *auser;
char *rhost;
char *tty;
au_id_t auid;
adt_session_data_t *ah;
adt_termid_t *termid = NULL;
priv_set_t *lim, *def, *tset;
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
char buf[PROJECT_BUFSZ];
struct project proj, *pproj;
int error;
char *projname;
char *kvs;
struct passwd pwd;
char pwbuf[NSS_BUFLEN_PASSWD];
deflim_t deflim;
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "debug") == 0)
debug = 1;
else if (strcmp(argv[i], "nowarn") == 0)
nowarn |= 1;
}
if (debug)
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: pam_sm_setcred(flags = %x, argc= %d)",
flags, argc);
(void) pam_get_item(pamh, PAM_USER, (void **)&user);
if (user == NULL || *user == '\0') {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: USER NULL or empty!\n");
return (PAM_USER_UNKNOWN);
}
(void) pam_get_item(pamh, PAM_AUSER, (void **)&auser);
(void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
(void) pam_get_item(pamh, PAM_TTY, (void **)&tty);
if (debug)
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: user = %s, auser = %s, rhost = %s, "
"tty = %s", user,
(auser == NULL) ? "NULL" : (*auser == '\0') ? "ZERO" :
auser,
(rhost == NULL) ? "NULL" : (*rhost == '\0') ? "ZERO" :
rhost,
(tty == NULL) ? "NULL" : (*tty == '\0') ? "ZERO" :
tty);
/* validate flags */
switch (flags & (PAM_ESTABLISH_CRED | PAM_DELETE_CRED |
PAM_REINITIALIZE_CRED | PAM_REFRESH_CRED)) {
case 0:
/* set default flag */
flags |= PAM_ESTABLISH_CRED;
break;
case PAM_ESTABLISH_CRED:
case PAM_REINITIALIZE_CRED:
case PAM_REFRESH_CRED:
break;
case PAM_DELETE_CRED:
return (PAM_SUCCESS);
default:
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: invalid flags %x", flags);
return (PAM_SYSTEM_ERR);
}
/*
* if auditing on and process audit state not set,
* setup audit context for process.
*/
if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot create start audit session %m");
return (PAM_SYSTEM_ERR);
}
adt_get_auid(ah, &auid);
if (debug) {
int auditstate;
if (auditon(A_GETCOND, (caddr_t)&auditstate,
sizeof (auditstate)) != 0) {
auditstate = AUC_DISABLED;
}
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: state = %d, auid = %d", auditstate,
auid);
}
if (getpwnam_r(user, &pwd, pwbuf, sizeof (pwbuf)) == NULL) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot get passwd entry for user = %s",
user);
ret = PAM_USER_UNKNOWN;
goto adt_done;
}
if ((auid == AU_NOAUDITID) &&
(flags & PAM_ESTABLISH_CRED)) {
struct passwd apwd;
char apwbuf[NSS_BUFLEN_PASSWD];
errno = 0;
if ((rhost == NULL || *rhost == '\0')) {
if (adt_load_ttyname(tty, &termid) != 0) {
if (errno == ENETDOWN) {
/*
* tolerate not being able to
* translate local hostname
* to a termid -- it will be
* "loopback".
*/
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot load "
"ttyname: %m, continuing.");
goto adt_setuser;
} else if (errno != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot load "
"ttyname: %m.");
} else {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot load "
"ttyname.");
}
ret = PAM_SYSTEM_ERR;
goto adt_done;
}
} else {
if (adt_load_hostname(rhost, &termid) != 0) {
if (errno != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot load "
"hostname: %m.");
} else {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot load "
"hostname.");
}
ret = PAM_SYSTEM_ERR;
goto adt_done;
}
}
adt_setuser:
if ((auser != NULL) && (*auser != '\0') &&
(getpwnam_r(auser, &apwd, apwbuf,
sizeof (apwbuf)) != NULL)) {
/*
* set up the initial audit for user coming
* from another user
*/
if (adt_set_user(ah, apwd.pw_uid, apwd.pw_gid,
apwd.pw_uid, apwd.pw_gid, termid, ADT_NEW) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot set auser audit "
"%m");
ret = PAM_SYSTEM_ERR;
goto adt_done;
}
if (adt_set_user(ah, pwd.pw_uid, pwd.pw_gid,
pwd.pw_uid, pwd.pw_gid, NULL,
ADT_UPDATE) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot merge user audit "
"%m");
ret = PAM_SYSTEM_ERR;
goto adt_done;
}
if (debug) {
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: new audit set for %d:%d",
apwd.pw_uid, pwd.pw_uid);
}
} else {
/*
* No authenticated user or authenticated user is
* not a local user, no remote attribution, set
* up the initial audit as for direct user login
*/
if (adt_set_user(ah, pwd.pw_uid, pwd.pw_gid,
pwd.pw_uid, pwd.pw_gid, termid, ADT_NEW) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot set user audit %m");
ret = PAM_SYSTEM_ERR;
goto adt_done;
}
}
if (adt_set_proc(ah) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot set process audit %m");
ret = PAM_CRED_ERR;
goto adt_done;
}
if (debug) {
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: new audit set for %d",
pwd.pw_uid);
}
} else if ((auid != AU_NOAUDITID) &&
(flags & PAM_REINITIALIZE_CRED)) {
if (adt_set_user(ah, pwd.pw_uid, pwd.pw_gid, pwd.pw_uid,
pwd.pw_gid, NULL, ADT_UPDATE) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot set user audit %m");
ret = PAM_SYSTEM_ERR;
goto adt_done;
}
if (adt_set_proc(ah) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: cannot set process audit %m");
ret = PAM_CRED_ERR;
goto adt_done;
}
if (debug) {
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: audit merged for %d:%d",
auid, pwd.pw_uid);
}
} else if (debug) {
syslog(LOG_AUTH | LOG_DEBUG,
"pam_unix_cred: audit already set for %d", auid);
}
adt_done:
if (termid != NULL)
free(termid);
if (adt_end_session(ah) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: unable to end audit session");
}
if (ret != PAM_SUCCESS)
return (ret);
/* Initialize the user's project */
(void) pam_get_item(pamh, PAM_RESOURCE, (void **)&kvs);
if (kvs != NULL) {
char *tmp, *lasts, *tok;
kvs = tmp = strdup(kvs);
if (kvs == NULL)
return (PAM_BUF_ERR);
while ((tok = strtok_r(tmp, ";", &lasts)) != NULL) {
if (strncmp(tok, PROJECT, PROJSZ) == 0) {
projname = tok + PROJSZ;
break;
}
tmp = NULL;
}
} else {
projname = NULL;
}
if (projname == NULL || *projname == '\0') {
pproj = getdefaultproj(user, &proj, (void *)&buf,
PROJECT_BUFSZ);
} else {
pproj = getprojbyname(projname, &proj, (void *)&buf,
PROJECT_BUFSZ);
}
/* projname points into kvs, so this is the first opportunity to free */
if (kvs != NULL)
free(kvs);
if (pproj == NULL) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: no default project for user %s", user);
if (!nowarn) {
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN, "No default project!"));
(void) __pam_display_msg(pamh, PAM_ERROR_MSG,
1, messages, NULL);
}
return (PAM_SYSTEM_ERR);
}
if ((error = setproject(proj.pj_name, user, TASK_NORMAL)) != 0) {
kva_t *kv_array;
switch (error) {
case SETPROJ_ERR_TASK:
if (errno == EAGAIN) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: project \"%s\" resource "
"control limit has been reached",
proj.pj_name);
(void) snprintf(messages[0],
sizeof (messages[0]), dgettext(
TEXT_DOMAIN,
"Resource control limit has been "
"reached"));
} else {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: user %s could not join "
"project \"%s\": %m", user, proj.pj_name);
(void) snprintf(messages[0],
sizeof (messages[0]), dgettext(
TEXT_DOMAIN,
"Could not join default project"));
}
if (!nowarn)
(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
messages, NULL);
break;
case SETPROJ_ERR_POOL:
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Could not bind to resource pool"));
switch (errno) {
case EACCES:
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: project \"%s\" could not "
"bind to resource pool: No resource pool "
"accepting default bindings exists",
proj.pj_name);
(void) snprintf(messages[1],
sizeof (messages[1]),
dgettext(TEXT_DOMAIN,
"No resource pool accepting "
"default bindings exists"));
break;
case ESRCH:
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: project \"%s\" could not "
"bind to resource pool: The resource pool "
"is unknown", proj.pj_name);
(void) snprintf(messages[1],
sizeof (messages[1]),
dgettext(TEXT_DOMAIN,
"The specified resource pool "
"is unknown"));
break;
default:
(void) snprintf(messages[1],
sizeof (messages[1]),
dgettext(TEXT_DOMAIN,
"Failure during pool binding"));
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: project \"%s\" could not "
"bind to resource pool: %m", proj.pj_name);
}
if (!nowarn)
(void) __pam_display_msg(pamh, PAM_ERROR_MSG,
2, messages, NULL);
break;
default:
/*
* Resource control assignment failed. Unlike
* newtask(1), we treat this as an error.
*/
if (error < 0) {
/*
* This isn't supposed to happen, but in
* case it does, this error message
* doesn't use error as an index, like
* the others might.
*/
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: unkwown error joining "
"project \"%s\" (%d)", proj.pj_name, error);
(void) snprintf(messages[0],
sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"unkwown error joining project \"%s\""
" (%d)"), proj.pj_name, error);
} else if ((kv_array = _str2kva(proj.pj_attr, KV_ASSIGN,
KV_DELIMITER)) != NULL) {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: %s resource control "
"assignment failed for project \"%s\"",
kv_array->data[error - 1].key,
proj.pj_name);
(void) snprintf(messages[0],
sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"%s resource control assignment failed for "
"project \"%s\""),
kv_array->data[error - 1].key,
proj.pj_name);
_kva_free(kv_array);
} else {
syslog(LOG_AUTH | LOG_ERR,
"pam_unix_cred: resource control "
"assignment failed for project \"%s\""
"attribute %d", proj.pj_name, error);
(void) snprintf(messages[0],
sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"resource control assignment failed for "
"project \"%s\" attribute %d"),
proj.pj_name, error);
}
if (!nowarn)
(void) __pam_display_msg(pamh, PAM_ERROR_MSG,
1, messages, NULL);
}
return (PAM_SYSTEM_ERR);
}
tset = def = lim = NULL;
deflim.def = deflim.lim = NULL;
(void) _enum_attrs(user, finddeflim, NULL, &deflim);
if (getset(deflim.lim, &lim) != 0 || getset(deflim.def, &def) != 0) {
ret = PAM_SYSTEM_ERR;
goto out;
}
if (def == NULL) {
def = priv_allocset();
if (def == NULL) {
ret = PAM_SYSTEM_ERR;
goto out;
}
priv_basicset(def);
errno = 0;
if ((pathconf("/", _PC_CHOWN_RESTRICTED) == -1) && (errno == 0))
(void) priv_addset(def, PRIV_FILE_CHOWN_SELF);
}
/*
* Silently limit the privileges to those actually available
* in the current zone.
*/
tset = priv_allocset();
if (tset == NULL) {
ret = PAM_SYSTEM_ERR;
goto out;
}
if (getppriv(PRIV_PERMITTED, tset) != 0) {
ret = PAM_SYSTEM_ERR;
goto out;
}
if (!priv_issubset(def, tset))
priv_intersect(tset, def);
/*
* We set privilege awareness here so that I gets copied to
* P & E when the final setuid(uid) happens.
*/
(void) setpflags(PRIV_AWARE, 1);
if (setppriv(PRIV_SET, PRIV_INHERITABLE, def) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_setcred: setppriv(defaultpriv) failed: %m");
ret = PAM_CRED_ERR;
}
if (lim != NULL) {
/*
* Silently limit the privileges to the limit set available.
*/
if (getppriv(PRIV_LIMIT, tset) != 0) {
ret = PAM_SYSTEM_ERR;
goto out;
}
if (!priv_issubset(lim, tset))
priv_intersect(tset, lim);
if (setppriv(PRIV_SET, PRIV_LIMIT, lim) != 0) {
syslog(LOG_AUTH | LOG_ERR,
"pam_setcred: setppriv(limitpriv) failed: %m");
ret = PAM_CRED_ERR;
goto out;
}
/*
* In order not to surprise certain applications, we
* need to get rid of privilege awareness and thus we must
* set this flag which will cause a reset on set*uid().
*/
(void) setpflags(PRIV_AWARE_RESET, 1);
}
/*
* This may fail but we do not care as this will be reset later
* when the uids are set to their final values.
*/
(void) setpflags(PRIV_AWARE, 0);
/*
* Remove PRIV_PFEXEC; stop running as if we are under a profile
* shell. A user with a profile shell will set PRIV_PFEXEC.
*/
(void) setpflags(PRIV_PFEXEC, 0);
out:
free(deflim.lim);
free(deflim.def);
if (lim != NULL)
priv_freeset(lim);
if (def != NULL)
priv_freeset(def);
if (tset != NULL)
priv_freeset(tset);
return (ret);
}
|