1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <nss_dbdefs.h> 27 #include <pwd.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <syslog.h> 31 #include <unistd.h> 32 #include <auth_attr.h> 33 #include <deflt.h> 34 #include <priv.h> 35 #include <secdb.h> 36 #include <user_attr.h> 37 #include <sys/task.h> 38 #include <libintl.h> 39 #include <project.h> 40 #include <errno.h> 41 #include <alloca.h> 42 43 #include <bsm/adt.h> 44 #include <bsm/adt_event.h> /* adt_get_auid() */ 45 46 #include <security/pam_appl.h> 47 #include <security/pam_modules.h> 48 #include <security/pam_impl.h> 49 50 #define PROJECT "project=" 51 #define PROJSZ (sizeof (PROJECT) - 1) 52 53 /* 54 * unix_cred - PAM auth modules must contain both pam_sm_authenticate 55 * and pam_sm_setcred. Some other auth module is responsible 56 * for authentication (e.g., pam_unix_auth.so), this module 57 * only implements pam_sm_setcred so that the authentication 58 * can be separated without knowledge of the Solaris Unix style 59 * credential setting. 60 * Solaris Unix style credential setting includes initializing 61 * the audit characteristics if not already initialized and 62 * setting the user's default and limit privileges. 63 */ 64 65 /* 66 * unix_cred - pam_sm_authenticate 67 * 68 * Returns PAM_IGNORE. 69 */ 70 71 /*ARGSUSED*/ 72 int 73 pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) 74 { 75 return (PAM_IGNORE); 76 } 77 78 /* 79 * Obtain a privilege set "keyname" from userattr; if none is present, 80 * fall back to the default, "defname". 81 */ 82 static int 83 getset(char *keyname, char *defname, userattr_t *ua, priv_set_t **res, 84 void *defp) 85 { 86 char *str; 87 priv_set_t *tmp; 88 char *badp; 89 int len; 90 91 if ((ua == NULL || ua->attr == NULL || 92 (str = kva_match(ua->attr, keyname)) == NULL) && 93 (defp == NULL || (str = defread_r(defname, defp)) == NULL)) 94 return (0); 95 96 len = strlen(str) + 1; 97 badp = alloca(len); 98 (void) memset(badp, '\0', len); 99 do { 100 const char *q, *endp; 101 tmp = priv_str_to_set(str, ",", &endp); 102 if (tmp == NULL) { 103 if (endp == NULL) 104 break; 105 106 /* Now remove the bad privilege endp points to */ 107 q = strchr(endp, ','); 108 if (q == NULL) 109 q = endp + strlen(endp); 110 111 if (*badp != '\0') 112 (void) strlcat(badp, ",", len); 113 /* Memset above guarantees NUL termination */ 114 /* LINTED */ 115 (void) strncat(badp, endp, q - endp); 116 /* excise bad privilege; strtok ignores 2x sep */ 117 (void) memmove((void *)endp, q, strlen(q) + 1); 118 } 119 } while (tmp == NULL && *str != '\0'); 120 121 if (tmp == NULL) { 122 syslog(LOG_AUTH|LOG_ERR, 123 "pam_setcred: can't parse privilege specification: %m\n"); 124 return (-1); 125 } else if (*badp != '\0') { 126 syslog(LOG_AUTH|LOG_DEBUG, 127 "pam_setcred: unrecognized privilege(s): %s\n", badp); 128 } 129 *res = tmp; 130 return (0); 131 } 132 133 /* 134 * unix_cred - pam_sm_setcred 135 * 136 * Entry flags = PAM_ESTABLISH_CRED, set up Solaris Unix cred. 137 * PAM_DELETE_CRED, NOP, return PAM_SUCCESS. 138 * PAM_REINITIALIZE_CRED, set up Solaris Unix cred, 139 * or merge the current context with the new 140 * user. 141 * PAM_REFRESH_CRED, set up Solaris Unix cred. 142 * PAM_SILENT, print no messages to user. 143 * 144 * Returns PAM_SUCCESS, if all successful. 145 * PAM_CRED_ERR, if unable to set credentials. 146 * PAM_USER_UNKNOWN, if PAM_USER not set, or unable to find 147 * user in databases. 148 * PAM_SYSTEM_ERR, if no valid flag, or unable to get/set 149 * user's audit state. 150 */ 151 152 int 153 pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) 154 { 155 int i; 156 int debug = 0; 157 uint_t nowarn = flags & PAM_SILENT; 158 int ret = PAM_SUCCESS; 159 char *user; 160 char *auser; 161 char *rhost; 162 char *tty; 163 au_id_t auid; 164 adt_session_data_t *ah; 165 adt_termid_t *termid = NULL; 166 userattr_t *ua; 167 priv_set_t *lim, *def, *tset; 168 char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE]; 169 char buf[PROJECT_BUFSZ]; 170 struct project proj, *pproj; 171 int error; 172 char *projname; 173 char *kvs; 174 struct passwd pwd; 175 char pwbuf[NSS_BUFLEN_PASSWD]; 176 void *defp; 177 178 for (i = 0; i < argc; i++) { 179 if (strcmp(argv[i], "debug") == 0) 180 debug = 1; 181 else if (strcmp(argv[i], "nowarn") == 0) 182 nowarn |= 1; 183 } 184 185 if (debug) 186 syslog(LOG_AUTH | LOG_DEBUG, 187 "pam_unix_cred: pam_sm_setcred(flags = %x, argc= %d)", 188 flags, argc); 189 190 (void) pam_get_item(pamh, PAM_USER, (void **)&user); 191 192 if (user == NULL || *user == '\0') { 193 syslog(LOG_AUTH | LOG_ERR, 194 "pam_unix_cred: USER NULL or empty!\n"); 195 return (PAM_USER_UNKNOWN); 196 } 197 (void) pam_get_item(pamh, PAM_AUSER, (void **)&auser); 198 (void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost); 199 (void) pam_get_item(pamh, PAM_TTY, (void **)&tty); 200 if (debug) 201 syslog(LOG_AUTH | LOG_DEBUG, 202 "pam_unix_cred: user = %s, auser = %s, rhost = %s, " 203 "tty = %s", user, 204 (auser == NULL) ? "NULL" : (*auser == '\0') ? "ZERO" : 205 auser, 206 (rhost == NULL) ? "NULL" : (*rhost == '\0') ? "ZERO" : 207 rhost, 208 (tty == NULL) ? "NULL" : (*tty == '\0') ? "ZERO" : 209 tty); 210 211 /* validate flags */ 212 switch (flags & (PAM_ESTABLISH_CRED | PAM_DELETE_CRED | 213 PAM_REINITIALIZE_CRED | PAM_REFRESH_CRED)) { 214 case 0: 215 /* set default flag */ 216 flags |= PAM_ESTABLISH_CRED; 217 break; 218 case PAM_ESTABLISH_CRED: 219 case PAM_REINITIALIZE_CRED: 220 case PAM_REFRESH_CRED: 221 break; 222 case PAM_DELETE_CRED: 223 return (PAM_SUCCESS); 224 default: 225 syslog(LOG_AUTH | LOG_ERR, 226 "pam_unix_cred: invalid flags %x", flags); 227 return (PAM_SYSTEM_ERR); 228 } 229 230 /* 231 * if auditing on and process audit state not set, 232 * setup audit context for process. 233 */ 234 if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) { 235 syslog(LOG_AUTH | LOG_ERR, 236 "pam_unix_cred: cannot create start audit session %m"); 237 return (PAM_SYSTEM_ERR); 238 } 239 adt_get_auid(ah, &auid); 240 if (debug) { 241 int auditstate; 242 243 if (auditon(A_GETCOND, (caddr_t)&auditstate, 244 sizeof (auditstate)) != 0) { 245 auditstate = AUC_DISABLED; 246 } 247 syslog(LOG_AUTH | LOG_DEBUG, 248 "pam_unix_cred: state = %d, auid = %d", auditstate, 249 auid); 250 } 251 if (getpwnam_r(user, &pwd, pwbuf, sizeof (pwbuf)) == NULL) { 252 syslog(LOG_AUTH | LOG_ERR, 253 "pam_unix_cred: cannot get passwd entry for user = %s", 254 user); 255 ret = PAM_USER_UNKNOWN; 256 goto adt_done; 257 } 258 259 if ((auid == AU_NOAUDITID) && 260 (flags & PAM_ESTABLISH_CRED)) { 261 struct passwd apwd; 262 char apwbuf[NSS_BUFLEN_PASSWD]; 263 264 if ((rhost == NULL || *rhost == '\0')) { 265 if (adt_load_ttyname(tty, &termid) != 0) { 266 syslog(LOG_AUTH | LOG_ERR, 267 "pam_unix_cred: cannot load ttyname %m"); 268 ret = PAM_SYSTEM_ERR; 269 goto adt_done; 270 } 271 } else { 272 if (adt_load_hostname(rhost, &termid) != 0) { 273 syslog(LOG_AUTH | LOG_ERR, 274 "pam_unix_cred: cannot load hostname %m"); 275 ret = PAM_SYSTEM_ERR; 276 goto adt_done; 277 } 278 } 279 if ((auser != NULL) && (*auser != '\0') && 280 (getpwnam_r(auser, &apwd, apwbuf, 281 sizeof (apwbuf)) != NULL)) { 282 /* 283 * set up the initial audit for user coming 284 * from another user 285 */ 286 if (adt_set_user(ah, apwd.pw_uid, apwd.pw_gid, 287 apwd.pw_uid, apwd.pw_gid, termid, ADT_NEW) != 0) { 288 syslog(LOG_AUTH | LOG_ERR, 289 "pam_unix_cred: cannot set auser audit " 290 "%m"); 291 ret = PAM_SYSTEM_ERR; 292 goto adt_done; 293 } 294 if (adt_set_user(ah, pwd.pw_uid, pwd.pw_gid, 295 pwd.pw_uid, pwd.pw_gid, NULL, 296 ADT_UPDATE) != 0) { 297 syslog(LOG_AUTH | LOG_ERR, 298 "pam_unix_cred: cannot merge user audit " 299 "%m"); 300 ret = PAM_SYSTEM_ERR; 301 goto adt_done; 302 } 303 if (debug) { 304 syslog(LOG_AUTH | LOG_DEBUG, 305 "pam_unix_cred: new audit set for %d:%d", 306 apwd.pw_uid, pwd.pw_uid); 307 } 308 } else { 309 /* 310 * No authenticated user or authenticated user is 311 * not a local user, no remote attribution, set 312 * up the initial audit as for direct user login 313 */ 314 if (adt_set_user(ah, pwd.pw_uid, pwd.pw_gid, 315 pwd.pw_uid, pwd.pw_gid, termid, ADT_NEW) != 0) { 316 syslog(LOG_AUTH | LOG_ERR, 317 "pam_unix_cred: cannot set user audit %m"); 318 ret = PAM_SYSTEM_ERR; 319 goto adt_done; 320 } 321 } 322 if (adt_set_proc(ah) != 0) { 323 syslog(LOG_AUTH | LOG_ERR, 324 "pam_unix_cred: cannot set process audit %m"); 325 ret = PAM_CRED_ERR; 326 goto adt_done; 327 } 328 if (debug) { 329 syslog(LOG_AUTH | LOG_DEBUG, 330 "pam_unix_cred: new audit set for %d", 331 pwd.pw_uid); 332 } 333 } else if ((auid != AU_NOAUDITID) && 334 (flags & PAM_REINITIALIZE_CRED)) { 335 if (adt_set_user(ah, pwd.pw_uid, pwd.pw_gid, pwd.pw_uid, 336 pwd.pw_gid, NULL, ADT_UPDATE) != 0) { 337 syslog(LOG_AUTH | LOG_ERR, 338 "pam_unix_cred: cannot set user audit %m"); 339 ret = PAM_SYSTEM_ERR; 340 goto adt_done; 341 } 342 if (adt_set_proc(ah) != 0) { 343 syslog(LOG_AUTH | LOG_ERR, 344 "pam_unix_cred: cannot set process audit %m"); 345 ret = PAM_CRED_ERR; 346 goto adt_done; 347 } 348 if (debug) { 349 syslog(LOG_AUTH | LOG_DEBUG, 350 "pam_unix_cred: audit merged for %d:%d", 351 auid, pwd.pw_uid); 352 } 353 } else if (debug) { 354 syslog(LOG_AUTH | LOG_DEBUG, 355 "pam_unix_cred: audit already set for %d", auid); 356 } 357 adt_done: 358 if (termid != NULL) 359 free(termid); 360 if (adt_end_session(ah) != 0) { 361 syslog(LOG_AUTH | LOG_ERR, 362 "pam_unix_cred: unable to end audit session"); 363 } 364 365 if (ret != PAM_SUCCESS) 366 return (ret); 367 368 /* Initialize the user's project */ 369 (void) pam_get_item(pamh, PAM_RESOURCE, (void **)&kvs); 370 if (kvs != NULL) { 371 char *tmp, *lasts, *tok; 372 373 kvs = tmp = strdup(kvs); 374 if (kvs == NULL) 375 return (PAM_BUF_ERR); 376 377 while ((tok = strtok_r(tmp, ";", &lasts)) != NULL) { 378 if (strncmp(tok, PROJECT, PROJSZ) == 0) { 379 projname = tok + PROJSZ; 380 break; 381 } 382 tmp = NULL; 383 } 384 } else { 385 projname = NULL; 386 } 387 388 if (projname == NULL || *projname == '\0') { 389 pproj = getdefaultproj(user, &proj, (void *)&buf, 390 PROJECT_BUFSZ); 391 } else { 392 pproj = getprojbyname(projname, &proj, (void *)&buf, 393 PROJECT_BUFSZ); 394 } 395 /* projname points into kvs, so this is the first opportunity to free */ 396 if (kvs != NULL) 397 free(kvs); 398 if (pproj == NULL) { 399 syslog(LOG_AUTH | LOG_ERR, 400 "pam_unix_cred: no default project for user %s", user); 401 if (!nowarn) { 402 (void) snprintf(messages[0], sizeof (messages[0]), 403 dgettext(TEXT_DOMAIN, "No default project!")); 404 (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 405 1, messages, NULL); 406 } 407 return (PAM_SYSTEM_ERR); 408 } 409 if ((error = setproject(proj.pj_name, user, TASK_NORMAL)) != 0) { 410 kva_t *kv_array; 411 412 switch (error) { 413 case SETPROJ_ERR_TASK: 414 if (errno == EAGAIN) { 415 syslog(LOG_AUTH | LOG_ERR, 416 "pam_unix_cred: project \"%s\" resource " 417 "control limit has been reached", 418 proj.pj_name); 419 (void) snprintf(messages[0], 420 sizeof (messages[0]), dgettext( 421 TEXT_DOMAIN, 422 "Resource control limit has been " 423 "reached")); 424 } else { 425 syslog(LOG_AUTH | LOG_ERR, 426 "pam_unix_cred: user %s could not join " 427 "project \"%s\": %m", user, proj.pj_name); 428 (void) snprintf(messages[0], 429 sizeof (messages[0]), dgettext( 430 TEXT_DOMAIN, 431 "Could not join default project")); 432 } 433 if (!nowarn) 434 (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1, 435 messages, NULL); 436 break; 437 case SETPROJ_ERR_POOL: 438 (void) snprintf(messages[0], sizeof (messages[0]), 439 dgettext(TEXT_DOMAIN, 440 "Could not bind to resource pool")); 441 switch (errno) { 442 case EACCES: 443 syslog(LOG_AUTH | LOG_ERR, 444 "pam_unix_cred: project \"%s\" could not " 445 "bind to resource pool: No resource pool " 446 "accepting default bindings exists", 447 proj.pj_name); 448 (void) snprintf(messages[1], 449 sizeof (messages[1]), 450 dgettext(TEXT_DOMAIN, 451 "No resource pool accepting " 452 "default bindings exists")); 453 break; 454 case ESRCH: 455 syslog(LOG_AUTH | LOG_ERR, 456 "pam_unix_cred: project \"%s\" could not " 457 "bind to resource pool: The resource pool " 458 "is unknown", proj.pj_name); 459 (void) snprintf(messages[1], 460 sizeof (messages[1]), 461 dgettext(TEXT_DOMAIN, 462 "The specified resource pool " 463 "is unknown")); 464 break; 465 default: 466 (void) snprintf(messages[1], 467 sizeof (messages[1]), 468 dgettext(TEXT_DOMAIN, 469 "Failure during pool binding")); 470 syslog(LOG_AUTH | LOG_ERR, 471 "pam_unix_cred: project \"%s\" could not " 472 "bind to resource pool: %m", proj.pj_name); 473 } 474 if (!nowarn) 475 (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 476 2, messages, NULL); 477 break; 478 default: 479 /* 480 * Resource control assignment failed. Unlike 481 * newtask(1m), we treat this as an error. 482 */ 483 if (error < 0) { 484 /* 485 * This isn't supposed to happen, but in 486 * case it does, this error message 487 * doesn't use error as an index, like 488 * the others might. 489 */ 490 syslog(LOG_AUTH | LOG_ERR, 491 "pam_unix_cred: unkwown error joining " 492 "project \"%s\" (%d)", proj.pj_name, error); 493 (void) snprintf(messages[0], 494 sizeof (messages[0]), 495 dgettext(TEXT_DOMAIN, 496 "unkwown error joining project \"%s\"" 497 " (%d)"), proj.pj_name, error); 498 } else if ((kv_array = _str2kva(proj.pj_attr, KV_ASSIGN, 499 KV_DELIMITER)) != NULL) { 500 syslog(LOG_AUTH | LOG_ERR, 501 "pam_unix_cred: %s resource control " 502 "assignment failed for project \"%s\"", 503 kv_array->data[error - 1].key, 504 proj.pj_name); 505 (void) snprintf(messages[0], 506 sizeof (messages[0]), 507 dgettext(TEXT_DOMAIN, 508 "%s resource control assignment failed for " 509 "project \"%s\""), 510 kv_array->data[error - 1].key, 511 proj.pj_name); 512 _kva_free(kv_array); 513 } else { 514 syslog(LOG_AUTH | LOG_ERR, 515 "pam_unix_cred: resource control " 516 "assignment failed for project \"%s\"" 517 "attribute %d", proj.pj_name, error); 518 (void) snprintf(messages[0], 519 sizeof (messages[0]), 520 dgettext(TEXT_DOMAIN, 521 "resource control assignment failed for " 522 "project \"%s\" attribute %d"), 523 proj.pj_name, error); 524 } 525 if (!nowarn) 526 (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 527 1, messages, NULL); 528 } 529 return (PAM_SYSTEM_ERR); 530 } 531 532 ua = getusernam(user); 533 534 defp = defopen_r(AUTH_POLICY); 535 536 tset = def = lim = NULL; 537 538 if (getset(USERATTR_LIMPRIV_KW, DEF_LIMITPRIV, ua, &lim, defp) != 0 || 539 getset(USERATTR_DFLTPRIV_KW, DEF_DFLTPRIV, ua, &def, defp) != 0) { 540 ret = PAM_SYSTEM_ERR; 541 goto out; 542 } 543 544 if (def == NULL) { 545 def = priv_str_to_set("basic", ",", NULL); 546 errno = 0; 547 if ((pathconf("/", _PC_CHOWN_RESTRICTED) == -1) && (errno == 0)) 548 (void) priv_addset(def, PRIV_FILE_CHOWN_SELF); 549 } 550 /* 551 * Silently limit the privileges to those actually available 552 * in the current zone. 553 */ 554 tset = priv_allocset(); 555 if (tset == NULL) { 556 ret = PAM_SYSTEM_ERR; 557 goto out; 558 } 559 if (getppriv(PRIV_PERMITTED, tset) != 0) { 560 ret = PAM_SYSTEM_ERR; 561 goto out; 562 } 563 if (!priv_issubset(def, tset)) 564 priv_intersect(tset, def); 565 /* 566 * We set privilege awareness here so that I gets copied to 567 * P & E when the final setuid(uid) happens. 568 */ 569 (void) setpflags(PRIV_AWARE, 1); 570 if (setppriv(PRIV_SET, PRIV_INHERITABLE, def) != 0) { 571 syslog(LOG_AUTH | LOG_ERR, 572 "pam_setcred: setppriv(defaultpriv) failed: %m"); 573 ret = PAM_CRED_ERR; 574 } 575 576 if (lim != NULL) { 577 /* 578 * Silently limit the privileges to the limit set available. 579 */ 580 if (getppriv(PRIV_LIMIT, tset) != 0) { 581 ret = PAM_SYSTEM_ERR; 582 goto out; 583 } 584 if (!priv_issubset(lim, tset)) 585 priv_intersect(tset, lim); 586 /* 587 * In order not to suprise certain applications, we 588 * need to retain privilege awareness and thus we must 589 * also set P and E. 590 */ 591 if (setppriv(PRIV_SET, PRIV_LIMIT, lim) != 0 || 592 setppriv(PRIV_SET, PRIV_PERMITTED, lim) != 0) { 593 syslog(LOG_AUTH | LOG_ERR, 594 "pam_setcred: setppriv(limitpriv) failed: %m"); 595 ret = PAM_CRED_ERR; 596 } 597 } 598 (void) setpflags(PRIV_AWARE, 0); 599 600 out: 601 if (defp != NULL) 602 defclose_r(defp); 603 604 if (ua != NULL) 605 free_userattr(ua); 606 if (lim != NULL) 607 priv_freeset(lim); 608 if (def != NULL) 609 priv_freeset(def); 610 if (tset != NULL) 611 priv_freeset(tset); 612 613 return (ret); 614 } 615