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*14839a76Sbubbva * Common Development and Distribution License (the "License"). 6*14839a76Sbubbva * 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*14839a76Sbubbva * 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 <unistd.h> 317c478bd9Sstevel@tonic-gate #include <pwd.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <deflt.h> 347c478bd9Sstevel@tonic-gate #include <libintl.h> 357c478bd9Sstevel@tonic-gate #include <locale.h> 367c478bd9Sstevel@tonic-gate #include <user_attr.h> 377c478bd9Sstevel@tonic-gate #include <prof_attr.h> 387c478bd9Sstevel@tonic-gate #include <auth_attr.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #define ALL_AUTHS "All" 427c478bd9Sstevel@tonic-gate #define ALL_SUN_AUTHS "solaris.*" 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #define EXIT_OK 0 457c478bd9Sstevel@tonic-gate #define EXIT_FATAL 1 467c478bd9Sstevel@tonic-gate #define EXIT_NON_FATAL 2 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN /* Should be defined by cc -D */ 497c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 507c478bd9Sstevel@tonic-gate #endif 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #define PROFLIST_SEP "," 537c478bd9Sstevel@tonic-gate #define AUTH_SEP "," 547c478bd9Sstevel@tonic-gate #define MAXAUTHS 4096 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static int show_auths(char *, char **, int, int); 587c478bd9Sstevel@tonic-gate static int list_auths(userattr_t *, char **, int *); 597c478bd9Sstevel@tonic-gate static char *get_default_auths(char **, int *); 607c478bd9Sstevel@tonic-gate static void getProfiles(char *, char **, int *, char **, int *); 617c478bd9Sstevel@tonic-gate static void add_auths(char *, char **, int *); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static char *progname = "auths"; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate int 687c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 697c478bd9Sstevel@tonic-gate { 707c478bd9Sstevel@tonic-gate int status = EXIT_OK; 717c478bd9Sstevel@tonic-gate char *defauths[MAXAUTHS]; 727c478bd9Sstevel@tonic-gate int defauth_cnt = 0; 737c478bd9Sstevel@tonic-gate int i; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 767c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate (void) get_default_auths(defauths, &defauth_cnt); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate switch (argc) { 817c478bd9Sstevel@tonic-gate case 1: 827c478bd9Sstevel@tonic-gate status = show_auths(NULL, defauths, defauth_cnt, 0); 837c478bd9Sstevel@tonic-gate break; 847c478bd9Sstevel@tonic-gate case 2: 857c478bd9Sstevel@tonic-gate status = show_auths(argv[argc-1], defauths, defauth_cnt, 0); 867c478bd9Sstevel@tonic-gate break; 877c478bd9Sstevel@tonic-gate default: 887c478bd9Sstevel@tonic-gate while (*++argv) { 897c478bd9Sstevel@tonic-gate status = show_auths(*argv, defauths, defauth_cnt, 1); 907c478bd9Sstevel@tonic-gate if (status == EXIT_FATAL) { 917c478bd9Sstevel@tonic-gate break; 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* free memory allocated for default authorizations */ 987c478bd9Sstevel@tonic-gate for (i = 0; i < defauth_cnt; i++) { 997c478bd9Sstevel@tonic-gate free(defauths[i]); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate status = (status == EXIT_OK) ? status : EXIT_FATAL; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate return (status); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static int 1097c478bd9Sstevel@tonic-gate show_auths(char *username, char **defauths, int defauth_cnt, int print_name) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate int status = EXIT_OK; 1127c478bd9Sstevel@tonic-gate struct passwd *pw; 1137c478bd9Sstevel@tonic-gate userattr_t *user; 1147c478bd9Sstevel@tonic-gate char *userauths[MAXAUTHS]; 1157c478bd9Sstevel@tonic-gate int userauth_cnt = 0, old_userauth_cnt; 1167c478bd9Sstevel@tonic-gate int i, j, have_allauths, duplicate; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if (username == NULL) { 1197c478bd9Sstevel@tonic-gate if ((pw = getpwuid(getuid())) == NULL) { 1207c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname); 1227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No passwd entry\n")); 1237c478bd9Sstevel@tonic-gate return (status); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate username = pw->pw_name; 126*14839a76Sbubbva } else if (getpwnam(username) == NULL) { 1277c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s : ", progname, username); 1297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No such user\n")); 1307c478bd9Sstevel@tonic-gate return (status); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate have_allauths = 0; 1347c478bd9Sstevel@tonic-gate if (username != NULL) { 1357c478bd9Sstevel@tonic-gate /* if ALL_AUTHS is default, don't need to look at other auths */ 1367c478bd9Sstevel@tonic-gate for (i = 0; i < defauth_cnt; i++) { 1377c478bd9Sstevel@tonic-gate if (strcmp(defauths[i], ALL_AUTHS) == 0) { 1387c478bd9Sstevel@tonic-gate have_allauths = 1; 1397c478bd9Sstevel@tonic-gate break; 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate if (have_allauths) { 1437c478bd9Sstevel@tonic-gate status = EXIT_OK; 1447c478bd9Sstevel@tonic-gate } else if ((user = getusernam(username)) != NULL) { 1457c478bd9Sstevel@tonic-gate status = list_auths(user, userauths, &userauth_cnt); 1467c478bd9Sstevel@tonic-gate /* check if any profiles have ALL_AUTHS */ 1477c478bd9Sstevel@tonic-gate for (i = 0; i < userauth_cnt; i++) { 1487c478bd9Sstevel@tonic-gate if (strcmp(userauths[i], ALL_AUTHS) == 0) { 1497c478bd9Sstevel@tonic-gate have_allauths = 1; 1507c478bd9Sstevel@tonic-gate break; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate if ((defauth_cnt + userauth_cnt) == 0) { 1557c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate if (status == EXIT_NON_FATAL) { 1597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s : ", progname, username); 1607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No authorizations\n")); 1617c478bd9Sstevel@tonic-gate } else { 1627c478bd9Sstevel@tonic-gate if (print_name) { 1637c478bd9Sstevel@tonic-gate (void) printf("%s : ", username); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (have_allauths) { 1677c478bd9Sstevel@tonic-gate (void) printf("%s\n", ALL_SUN_AUTHS); 1687c478bd9Sstevel@tonic-gate } else { 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * combine the user auths and default auths, 1717c478bd9Sstevel@tonic-gate * and eliminate duplicates from the two 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate old_userauth_cnt = userauth_cnt; 1747c478bd9Sstevel@tonic-gate for (i = 0; i < defauth_cnt; i++) { 1757c478bd9Sstevel@tonic-gate duplicate = 0; 1767c478bd9Sstevel@tonic-gate for (j = 0; j < old_userauth_cnt; j++) { 1777c478bd9Sstevel@tonic-gate if (strcmp(userauths[j], defauths[i]) == 1787c478bd9Sstevel@tonic-gate 0) { 1797c478bd9Sstevel@tonic-gate duplicate = 1; 1807c478bd9Sstevel@tonic-gate break; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate if (!duplicate) { 1847c478bd9Sstevel@tonic-gate userauths[userauth_cnt] = 1857c478bd9Sstevel@tonic-gate strdup(defauths[i]); 1867c478bd9Sstevel@tonic-gate userauth_cnt++; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* print out the auths */ 1917c478bd9Sstevel@tonic-gate for (i = 0; i < (userauth_cnt - 1); i++) { 1927c478bd9Sstevel@tonic-gate (void) printf("%s,", userauths[i]); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* print out the last entry, without the comma */ 1967c478bd9Sstevel@tonic-gate (void) printf("%s\n", userauths[userauth_cnt - 1]); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* free memory allocated for authorizations */ 2017c478bd9Sstevel@tonic-gate for (i = 0; i < userauth_cnt; i++) { 2027c478bd9Sstevel@tonic-gate free(userauths[i]); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate return (status); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate static int 2107c478bd9Sstevel@tonic-gate list_auths(userattr_t *user, char **authArray, int *authcnt) 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate int status = EXIT_OK; 2137c478bd9Sstevel@tonic-gate char *authlist = NULL; 2147c478bd9Sstevel@tonic-gate char *proflist = NULL; 2157c478bd9Sstevel@tonic-gate char *profArray[MAXPROFS]; 2167c478bd9Sstevel@tonic-gate int profcnt = 0; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate authlist = kva_match(user->attr, USERATTR_AUTHS_KW); 2197c478bd9Sstevel@tonic-gate if (authlist != NULL) { 2207c478bd9Sstevel@tonic-gate add_auths(authlist, authArray, authcnt); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate if ((proflist = kva_match(user->attr, USERATTR_PROFILES_KW)) == NULL) { 2237c478bd9Sstevel@tonic-gate if (authcnt == 0) { 2247c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate } else { 2277c478bd9Sstevel@tonic-gate getProfiles(proflist, profArray, &profcnt, 2287c478bd9Sstevel@tonic-gate authArray, authcnt); 2297c478bd9Sstevel@tonic-gate free_proflist(profArray, profcnt); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate if (authcnt == 0) { 2327c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate free_userattr(user); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate return (status); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate static char * 2417c478bd9Sstevel@tonic-gate get_default_auths(char **authArray, int *authcnt) 2427c478bd9Sstevel@tonic-gate { 2437c478bd9Sstevel@tonic-gate char *auths = NULL; 2447c478bd9Sstevel@tonic-gate char *profs = NULL; 2457c478bd9Sstevel@tonic-gate char *profArray[MAXPROFS]; 2467c478bd9Sstevel@tonic-gate int profcnt = 0; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate if (defopen(AUTH_POLICY) == NULL) { 2497c478bd9Sstevel@tonic-gate auths = defread(DEF_AUTH); 2507c478bd9Sstevel@tonic-gate if (auths != NULL) { 2517c478bd9Sstevel@tonic-gate add_auths(auths, authArray, authcnt); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate /* get authorizations from default profiles */ 2557c478bd9Sstevel@tonic-gate profs = defread(DEF_PROF); 2567c478bd9Sstevel@tonic-gate if (profs != NULL) { 2577c478bd9Sstevel@tonic-gate getProfiles(profs, profArray, &profcnt, 2587c478bd9Sstevel@tonic-gate authArray, authcnt); 2597c478bd9Sstevel@tonic-gate free_proflist(profArray, profcnt); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate return (auths); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate void 2677c478bd9Sstevel@tonic-gate add_auths(char *auths, char **authArray, int *authcnt) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate char *authname, *lasts, *real_authname; 2707c478bd9Sstevel@tonic-gate int i; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate for (authname = (char *)strtok_r(auths, AUTH_SEP, &lasts); 2737c478bd9Sstevel@tonic-gate authname != NULL; 2747c478bd9Sstevel@tonic-gate authname = (char *)strtok_r(NULL, AUTH_SEP, &lasts)) { 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate if ((strcmp(authname, KV_WILDCARD) == 0) || 2777c478bd9Sstevel@tonic-gate (strcmp(authname, ALL_SUN_AUTHS) == 0)) { 2787c478bd9Sstevel@tonic-gate real_authname = ALL_AUTHS; 2797c478bd9Sstevel@tonic-gate } else { 2807c478bd9Sstevel@tonic-gate real_authname = authname; 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate /* check to see if authorization is already in list */ 2847c478bd9Sstevel@tonic-gate for (i = 0; i < *authcnt; i++) { 2857c478bd9Sstevel@tonic-gate if (strcmp(real_authname, authArray[i]) == 0) { 2867c478bd9Sstevel@tonic-gate break; /* already in list */ 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate /* not in list, add it in */ 2917c478bd9Sstevel@tonic-gate if (i == *authcnt) { 2927c478bd9Sstevel@tonic-gate authArray[i] = strdup(real_authname); 2937c478bd9Sstevel@tonic-gate *authcnt = i + 1; 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate static void 3007c478bd9Sstevel@tonic-gate getProfiles(char *profiles, char **profArray, int *profcnt, 3017c478bd9Sstevel@tonic-gate char **authArray, int *authcnt) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate char *prof; 3057c478bd9Sstevel@tonic-gate char *lasts; 3067c478bd9Sstevel@tonic-gate profattr_t *pa; 3077c478bd9Sstevel@tonic-gate char *auths; 3087c478bd9Sstevel@tonic-gate int i; 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate for (prof = (char *)strtok_r(profiles, PROFLIST_SEP, &lasts); 3117c478bd9Sstevel@tonic-gate prof != NULL; 3127c478bd9Sstevel@tonic-gate prof = (char *)strtok_r(NULL, PROFLIST_SEP, &lasts)) { 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate getproflist(prof, profArray, profcnt); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate /* get authorizations from list of profiles */ 3187c478bd9Sstevel@tonic-gate for (i = 0; i < *profcnt; i++) { 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if ((pa = getprofnam(profArray[i])) == NULL) { 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * this should never happen. 3237c478bd9Sstevel@tonic-gate * unless the database has an undefined profile 3247c478bd9Sstevel@tonic-gate */ 3257c478bd9Sstevel@tonic-gate continue; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate /* get auths this profile */ 3297c478bd9Sstevel@tonic-gate auths = kva_match(pa->attr, PROFATTR_AUTHS_KW); 3307c478bd9Sstevel@tonic-gate if (auths != NULL) { 3317c478bd9Sstevel@tonic-gate add_auths(auths, authArray, authcnt); 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate free_profattr(pa); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate } 337