17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 59919ad43Sgww * Common Development and Distribution License (the "License"). 69919ad43Sgww * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*3fdcff3dSgww * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <syslog.h> 277c478bd9Sstevel@tonic-gate #include <pwd.h> 287c478bd9Sstevel@tonic-gate #include <unistd.h> 297c478bd9Sstevel@tonic-gate #include <strings.h> 307c478bd9Sstevel@tonic-gate #include <security/pam_appl.h> 317c478bd9Sstevel@tonic-gate #include <security/pam_modules.h> 327c478bd9Sstevel@tonic-gate #include <libintl.h> 337c478bd9Sstevel@tonic-gate #include <pwd.h> 347c478bd9Sstevel@tonic-gate #include <user_attr.h> 357c478bd9Sstevel@tonic-gate #include <secdb.h> 367c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 377c478bd9Sstevel@tonic-gate #include <security/pam_impl.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate static int roleinlist(); 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * pam_sm_acct_mgmt(): 437c478bd9Sstevel@tonic-gate * Account management module 447c478bd9Sstevel@tonic-gate * This module disallows roles for primary logins and adds special 457c478bd9Sstevel@tonic-gate * checks to allow roles for secondary logins. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 497c478bd9Sstevel@tonic-gate int 507c478bd9Sstevel@tonic-gate pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate uid_t uid; 537c478bd9Sstevel@tonic-gate userattr_t *user_entry; 547c478bd9Sstevel@tonic-gate char *kva_value; 557c478bd9Sstevel@tonic-gate char *username; 569919ad43Sgww char *auser; 577c478bd9Sstevel@tonic-gate char *rhost; 587c478bd9Sstevel@tonic-gate char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE]; 597c478bd9Sstevel@tonic-gate struct passwd *pw_entry, pwd; 607c478bd9Sstevel@tonic-gate char buf[NSS_BUFLEN_PASSWD]; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate int i; 637c478bd9Sstevel@tonic-gate int debug = 0; 647c478bd9Sstevel@tonic-gate int allow_remote = 0; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate (void) pam_get_item(pamh, PAM_USER, (void **)&username); 679919ad43Sgww (void) pam_get_item(pamh, PAM_AUSER, (void **)&auser); 687c478bd9Sstevel@tonic-gate (void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) { 717c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "allow_remote") == 0) { 727c478bd9Sstevel@tonic-gate allow_remote = 1; 737c478bd9Sstevel@tonic-gate } else if (strcmp(argv[i], "debug") == 0) { 747c478bd9Sstevel@tonic-gate debug = 1; 757c478bd9Sstevel@tonic-gate } else { 767c478bd9Sstevel@tonic-gate __pam_log(LOG_AUTH | LOG_ERR, 777c478bd9Sstevel@tonic-gate "pam_roles:pam_sm_acct_mgmt: illegal module " 787c478bd9Sstevel@tonic-gate "option %s", argv[i]); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (debug) { 83*3fdcff3dSgww char *ruser; 847c478bd9Sstevel@tonic-gate char *service; 857c478bd9Sstevel@tonic-gate 86*3fdcff3dSgww (void) pam_get_item(pamh, PAM_RUSER, (void **)&ruser); 877c478bd9Sstevel@tonic-gate (void) pam_get_item(pamh, PAM_SERVICE, (void **)&service); 887c478bd9Sstevel@tonic-gate __pam_log(LOG_AUTH | LOG_DEBUG, "pam_roles:pam_sm_acct_mgmt: " 899919ad43Sgww "service = %s, allow_remote = %d, user = %s auser = %s " 90*3fdcff3dSgww "ruser = %s rhost = %s\n", (service) ? service : "not set", 91*3fdcff3dSgww allow_remote, (username) ? username : "not set", 92*3fdcff3dSgww (auser) ? auser: "not set", (ruser) ? ruser: "not set", 937c478bd9Sstevel@tonic-gate (rhost) ? rhost: "not set"); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate if (username == NULL) 977c478bd9Sstevel@tonic-gate return (PAM_USER_UNKNOWN); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* stop masquerades by mapping username to uid to username */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if ((pw_entry = getpwnam_r(username, &pwd, buf, sizeof (buf))) == NULL) 1027c478bd9Sstevel@tonic-gate return (PAM_USER_UNKNOWN); 1037c478bd9Sstevel@tonic-gate if ((pw_entry = getpwuid_r(pw_entry->pw_uid, &pwd, buf, 1047c478bd9Sstevel@tonic-gate sizeof (buf))) == NULL) 1057c478bd9Sstevel@tonic-gate return (PAM_USER_UNKNOWN); 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * If there's no user_attr entry for the primary user or it's not a 1087c478bd9Sstevel@tonic-gate * role, no further checks are needed. 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate if (((user_entry = getusernam(pw_entry->pw_name)) == NULL) || 1127c478bd9Sstevel@tonic-gate ((kva_value = kva_match((kva_t *)user_entry->attr, 1137c478bd9Sstevel@tonic-gate USERATTR_TYPE_KW)) == NULL) || 1147c478bd9Sstevel@tonic-gate ((strcmp(kva_value, USERATTR_TYPE_NONADMIN_KW) != 0) && 1157c478bd9Sstevel@tonic-gate (strcmp(kva_value, USERATTR_TYPE_ADMIN_KW) != 0))) { 1167c478bd9Sstevel@tonic-gate free_userattr(user_entry); 1177c478bd9Sstevel@tonic-gate return (PAM_IGNORE); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate free_userattr(user_entry); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* username is a role */ 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if (strcmp(username, pw_entry->pw_name) != 0) { 1247c478bd9Sstevel@tonic-gate __pam_log(LOG_AUTH | LOG_ALERT, 1257c478bd9Sstevel@tonic-gate "pam_roles:pam_sm_acct_mgmt: user name %s " 1267c478bd9Sstevel@tonic-gate "maps to user id %d which is user name %s", 1277c478bd9Sstevel@tonic-gate username, pw_entry->pw_uid, pw_entry->pw_name); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* Who's the user requesting the role? */ 1327c478bd9Sstevel@tonic-gate 1339919ad43Sgww if (auser != NULL && *auser != '\0') { 1349919ad43Sgww /* authenticated requesting user */ 1359919ad43Sgww 1369919ad43Sgww user_entry = getusernam(auser); 1379919ad43Sgww } else { 1387c478bd9Sstevel@tonic-gate /* user is implied by real UID */ 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if ((uid = getuid()) == 0) { 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Root user_attr entry cannot have roles. 1437c478bd9Sstevel@tonic-gate * Force error and deny access. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate user_entry = NULL; 1467c478bd9Sstevel@tonic-gate } else { 1477c478bd9Sstevel@tonic-gate if ((pw_entry = getpwuid_r(uid, &pwd, buf, 1487c478bd9Sstevel@tonic-gate sizeof (buf))) == NULL) { 1497c478bd9Sstevel@tonic-gate return (PAM_USER_UNKNOWN); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate user_entry = getusernam(pw_entry->pw_name); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1559919ad43Sgww if ((rhost != NULL && *rhost != '\0') && 1569919ad43Sgww allow_remote == 0) { 1577c478bd9Sstevel@tonic-gate /* don't allow remote roles for this service */ 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate free_userattr(user_entry); 1607c478bd9Sstevel@tonic-gate return (PAM_PERM_DENIED); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* 1647c478bd9Sstevel@tonic-gate * If the original user does not have a user_attr entry or isn't 1657c478bd9Sstevel@tonic-gate * assigned the role being assumed, fail. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if ((user_entry == NULL) || 1697c478bd9Sstevel@tonic-gate ((kva_value = kva_match((kva_t *)user_entry->attr, 1707c478bd9Sstevel@tonic-gate USERATTR_ROLES_KW)) == NULL) || 1717c478bd9Sstevel@tonic-gate (roleinlist(kva_value, username) == 0)) { 1727c478bd9Sstevel@tonic-gate free_userattr(user_entry); 1737c478bd9Sstevel@tonic-gate (void) strlcpy(messages[0], dgettext(TEXT_DOMAIN, 1747c478bd9Sstevel@tonic-gate "Roles can only be assumed by authorized users"), 1757c478bd9Sstevel@tonic-gate sizeof (messages[0])); 1767c478bd9Sstevel@tonic-gate (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, 1777c478bd9Sstevel@tonic-gate NULL); 1787c478bd9Sstevel@tonic-gate return (PAM_PERM_DENIED); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate free_userattr(user_entry); 1827c478bd9Sstevel@tonic-gate return (PAM_IGNORE); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate int 1867c478bd9Sstevel@tonic-gate roleinlist(char *list, char *role) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate char *lasts = (char *)NULL; 1897c478bd9Sstevel@tonic-gate char *rolename = (char *)strtok_r(list, ",", &lasts); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate while (rolename) { 1927c478bd9Sstevel@tonic-gate if (strcmp(rolename, role) == 0) 1937c478bd9Sstevel@tonic-gate return (1); 1947c478bd9Sstevel@tonic-gate else 1957c478bd9Sstevel@tonic-gate rolename = (char *)strtok_r(NULL, ",", &lasts); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate return (0); 1987c478bd9Sstevel@tonic-gate } 199