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 5*d3186a0eSjeanm * Common Development and Distribution License (the "License"). 6*d3186a0eSjeanm * 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*d3186a0eSjeanm * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/stat.h> 337c478bd9Sstevel@tonic-gate #include <fcntl.h> 347c478bd9Sstevel@tonic-gate #include <sys/mman.h> 357c478bd9Sstevel@tonic-gate #include <limits.h> 367c478bd9Sstevel@tonic-gate #include <deflt.h> 377c478bd9Sstevel@tonic-gate #include <auth_attr.h> 387c478bd9Sstevel@tonic-gate #include <prof_attr.h> 397c478bd9Sstevel@tonic-gate #include <user_attr.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static int _is_authorized(const char *, char *); 437c478bd9Sstevel@tonic-gate static int _chk_policy_auth(const char *, char **, int *); 447c478bd9Sstevel@tonic-gate static int _chkprof_for_auth(const char *, const char *, char **, int *); 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate int 487c478bd9Sstevel@tonic-gate chkauthattr(const char *authname, const char *username) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate int auth_granted = 0; 517c478bd9Sstevel@tonic-gate char *auths; 527c478bd9Sstevel@tonic-gate char *profiles; 537c478bd9Sstevel@tonic-gate userattr_t *user; 547c478bd9Sstevel@tonic-gate char *chkedprof[MAXPROFS]; 557c478bd9Sstevel@tonic-gate int chkedprof_cnt = 0; 567c478bd9Sstevel@tonic-gate int i; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate if (authname == NULL || username == NULL) 597c478bd9Sstevel@tonic-gate return (0); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate auth_granted = _chk_policy_auth(authname, chkedprof, &chkedprof_cnt); 627c478bd9Sstevel@tonic-gate if (auth_granted) { 637c478bd9Sstevel@tonic-gate return (1); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate if ((user = getusernam(username)) == NULL) 667c478bd9Sstevel@tonic-gate return (0); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate if ((auths = kva_match(user->attr, USERATTR_AUTHS_KW)) != NULL) { 697c478bd9Sstevel@tonic-gate if (_is_authorized(authname, auths)) { 707c478bd9Sstevel@tonic-gate free_userattr(user); 717c478bd9Sstevel@tonic-gate return (1); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if ((profiles = kva_match(user->attr, USERATTR_PROFILES_KW)) == NULL) { 767c478bd9Sstevel@tonic-gate free_userattr(user); 777c478bd9Sstevel@tonic-gate return (0); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate auth_granted = _chkprof_for_auth(profiles, authname, 817c478bd9Sstevel@tonic-gate chkedprof, &chkedprof_cnt); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* free memory allocated for checked array */ 847c478bd9Sstevel@tonic-gate for (i = 0; i < chkedprof_cnt; i++) { 857c478bd9Sstevel@tonic-gate free(chkedprof[i]); 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate free_userattr(user); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate return (auth_granted); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static int 947c478bd9Sstevel@tonic-gate _chkprof_for_auth(const char *profs, const char *authname, 957c478bd9Sstevel@tonic-gate char **chkedprof, int *chkedprof_cnt) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate char *prof, *lasts, *auths, *profiles; 997c478bd9Sstevel@tonic-gate profattr_t *pa; 1007c478bd9Sstevel@tonic-gate int i; 1017c478bd9Sstevel@tonic-gate int checked = 0; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate for (prof = strtok_r((char *)profs, ",", &lasts); prof != NULL; 1047c478bd9Sstevel@tonic-gate prof = strtok_r(NULL, ",", &lasts)) { 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate checked = 0; 1077c478bd9Sstevel@tonic-gate /* check if this profile has been checked */ 1087c478bd9Sstevel@tonic-gate for (i = 0; i < *chkedprof_cnt; i++) { 1097c478bd9Sstevel@tonic-gate if (strcmp(chkedprof[i], prof) == 0) { 1107c478bd9Sstevel@tonic-gate checked = 1; 1117c478bd9Sstevel@tonic-gate break; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate if (!checked) { 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate chkedprof[*chkedprof_cnt] = strdup(prof); 1187c478bd9Sstevel@tonic-gate *chkedprof_cnt = *chkedprof_cnt + 1; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate if ((pa = getprofnam(prof)) == NULL) 1217c478bd9Sstevel@tonic-gate continue; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if ((auths = kva_match(pa->attr, 1247c478bd9Sstevel@tonic-gate PROFATTR_AUTHS_KW)) != NULL) { 1257c478bd9Sstevel@tonic-gate if (_is_authorized(authname, auths)) { 1267c478bd9Sstevel@tonic-gate free_profattr(pa); 1277c478bd9Sstevel@tonic-gate return (1); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate if ((profiles = 1317c478bd9Sstevel@tonic-gate kva_match(pa->attr, PROFATTR_PROFS_KW)) != NULL) { 1327c478bd9Sstevel@tonic-gate /* Check for authorization in subprofiles */ 1337c478bd9Sstevel@tonic-gate if (_chkprof_for_auth(profiles, authname, 1347c478bd9Sstevel@tonic-gate chkedprof, chkedprof_cnt)) { 1357c478bd9Sstevel@tonic-gate free_profattr(pa); 1367c478bd9Sstevel@tonic-gate return (1); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate free_profattr(pa); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate /* authorization not found in any profile */ 1437c478bd9Sstevel@tonic-gate return (0); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate int 1477c478bd9Sstevel@tonic-gate _auth_match(const char *pattern, const char *auth) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate size_t len; 1507c478bd9Sstevel@tonic-gate char wildcard = KV_WILDCHAR; 1517c478bd9Sstevel@tonic-gate char *grant; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate len = strlen(pattern); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate /* 1567c478bd9Sstevel@tonic-gate * If the wildcard is not in the last position in the string, don't 1577c478bd9Sstevel@tonic-gate * match against it. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate if (pattern[len-1] != wildcard) 1607c478bd9Sstevel@tonic-gate return (0); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * If the strings are identical up to the wildcard and auth does not 1647c478bd9Sstevel@tonic-gate * end in "grant", then we have a match. 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate if (strncmp(pattern, auth, len-1) == 0) { 1677c478bd9Sstevel@tonic-gate grant = strrchr(auth, '.'); 1687c478bd9Sstevel@tonic-gate if (grant != NULL) { 1697c478bd9Sstevel@tonic-gate if (strncmp(grant + 1, "grant", 5) != NULL) 1707c478bd9Sstevel@tonic-gate return (1); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate return (0); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate static int 1787c478bd9Sstevel@tonic-gate _is_authorized(const char *authname, char *auths) 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate int found = 0; /* have we got a match, yet */ 1817c478bd9Sstevel@tonic-gate char wildcard = '*'; 1827c478bd9Sstevel@tonic-gate char *auth; /* current authorization being compared */ 1837c478bd9Sstevel@tonic-gate char *buf; 1847c478bd9Sstevel@tonic-gate char *lasts; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate buf = strdup(auths); 1877c478bd9Sstevel@tonic-gate for (auth = strtok_r(auths, ",", &lasts); auth != NULL && !found; 1887c478bd9Sstevel@tonic-gate auth = strtok_r(NULL, ",", &lasts)) { 1897c478bd9Sstevel@tonic-gate if (strcmp((char *)authname, auth) == 0) { 1907c478bd9Sstevel@tonic-gate /* Exact match. We're done. */ 1917c478bd9Sstevel@tonic-gate found = 1; 1927c478bd9Sstevel@tonic-gate } else if (strchr(auth, wildcard) != NULL) { 1937c478bd9Sstevel@tonic-gate if (_auth_match(auth, authname)) { 1947c478bd9Sstevel@tonic-gate found = 1; 1957c478bd9Sstevel@tonic-gate break; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate free(buf); 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate return (found); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate int 2077c478bd9Sstevel@tonic-gate _get_auth_policy(char **def_auth, char **def_prof) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate char *cp; 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate if (defopen(AUTH_POLICY) != 0) 2127c478bd9Sstevel@tonic-gate return (-1); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate cp = defread(DEF_AUTH); 2157c478bd9Sstevel@tonic-gate if (cp != NULL) { 2167c478bd9Sstevel@tonic-gate *def_auth = strdup(cp); 2177c478bd9Sstevel@tonic-gate if (*def_auth == NULL) 2187c478bd9Sstevel@tonic-gate return (-1); 2197c478bd9Sstevel@tonic-gate } else { 2207c478bd9Sstevel@tonic-gate *def_auth = NULL; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate cp = defread(DEF_PROF); 2247c478bd9Sstevel@tonic-gate if (cp != NULL) { 2257c478bd9Sstevel@tonic-gate *def_prof = strdup(cp); 2267c478bd9Sstevel@tonic-gate if (*def_prof == NULL) { 2277c478bd9Sstevel@tonic-gate free(*def_auth); 2287c478bd9Sstevel@tonic-gate return (-1); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate } else { 2317c478bd9Sstevel@tonic-gate *def_prof = NULL; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate (void) defopen(NULL); 2357c478bd9Sstevel@tonic-gate return (0); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 238*d3186a0eSjeanm void 239*d3186a0eSjeanm _free_auth_policy(char *def_auth, char *def_prof) 240*d3186a0eSjeanm { 241*d3186a0eSjeanm free(def_auth); 242*d3186a0eSjeanm free(def_prof); 243*d3186a0eSjeanm } 244*d3186a0eSjeanm 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * read /etc/security/policy.conf for AUTHS_GRANTED. 2477c478bd9Sstevel@tonic-gate * return 1 if found matching authname. 2487c478bd9Sstevel@tonic-gate * Otherwise, read PROFS_GRANTED to see if authname exists in any 2497c478bd9Sstevel@tonic-gate * default profiles. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate static int 2527c478bd9Sstevel@tonic-gate _chk_policy_auth(const char *authname, char **chkedprof, int *chkedprof_cnt) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate char *auths, *profs; 2557c478bd9Sstevel@tonic-gate int ret = 1; 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (_get_auth_policy(&auths, &profs) != 0) 2587c478bd9Sstevel@tonic-gate return (0); 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate if (auths != NULL) { 2617c478bd9Sstevel@tonic-gate if (_is_authorized(authname, auths)) 2627c478bd9Sstevel@tonic-gate goto exit; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate if (profs != NULL) { 2667c478bd9Sstevel@tonic-gate if (_chkprof_for_auth(profs, authname, chkedprof, 2677c478bd9Sstevel@tonic-gate chkedprof_cnt)) 2687c478bd9Sstevel@tonic-gate goto exit; 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate ret = 0; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate exit: 273*d3186a0eSjeanm _free_auth_policy(auths, profs); 2747c478bd9Sstevel@tonic-gate return (ret); 2757c478bd9Sstevel@tonic-gate } 276