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 514839a76Sbubbva * Common Development and Distribution License (the "License"). 614839a76Sbubbva * 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*499fd601Sgww * Copyright 2008 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 <libintl.h> 347c478bd9Sstevel@tonic-gate #include <locale.h> 357c478bd9Sstevel@tonic-gate #include <deflt.h> 367c478bd9Sstevel@tonic-gate #include <user_attr.h> 377c478bd9Sstevel@tonic-gate #include <prof_attr.h> 387c478bd9Sstevel@tonic-gate #include <exec_attr.h> 397c478bd9Sstevel@tonic-gate #include <auth_attr.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #define EXIT_OK 0 437c478bd9Sstevel@tonic-gate #define EXIT_FATAL 1 447c478bd9Sstevel@tonic-gate #define EXIT_NON_FATAL 2 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #define MAX_LINE_LEN 80 /* max 80 chars per line of output */ 477c478bd9Sstevel@tonic-gate #define TMP_BUF_LEN 2048 /* size of temp string buffer */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #define PRINT_DEFAULT 0x0000 507c478bd9Sstevel@tonic-gate #define PRINT_NAME 0x0010 517c478bd9Sstevel@tonic-gate #define PRINT_LONG 0x0020 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN /* Should be defined by cc -D */ 547c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 557c478bd9Sstevel@tonic-gate #endif 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define PROFLIST_SEP "," 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate static void usage(); 617c478bd9Sstevel@tonic-gate static int show_profs(char *, int); 627c478bd9Sstevel@tonic-gate static int list_profs(userattr_t *, int); 637c478bd9Sstevel@tonic-gate static void print_profs_long(char *, void *, int); 647c478bd9Sstevel@tonic-gate static void print_profs(char *, char **, int, int); 657c478bd9Sstevel@tonic-gate static void format_attr(int *, int, char *); 667c478bd9Sstevel@tonic-gate static void getProfiles(char *, char **, int *); 67*499fd601Sgww static void getDefaultProfiles(char *, char **, int *); 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate static char *progname = "profiles"; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate int 727c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 737c478bd9Sstevel@tonic-gate { 747c478bd9Sstevel@tonic-gate extern int optind; 757c478bd9Sstevel@tonic-gate register int c; 767c478bd9Sstevel@tonic-gate register int status = EXIT_OK; 777c478bd9Sstevel@tonic-gate int print_flag = PRINT_DEFAULT; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 807c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "l")) != EOF) { 837c478bd9Sstevel@tonic-gate switch (c) { 847c478bd9Sstevel@tonic-gate case 'l': 857c478bd9Sstevel@tonic-gate print_flag |= PRINT_LONG; 867c478bd9Sstevel@tonic-gate break; 877c478bd9Sstevel@tonic-gate default: 887c478bd9Sstevel@tonic-gate usage(); 897c478bd9Sstevel@tonic-gate return (EXIT_FATAL); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate argc -= optind; 937c478bd9Sstevel@tonic-gate argv += optind; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (*argv == NULL) { 96*499fd601Sgww status = show_profs(NULL, print_flag); 977c478bd9Sstevel@tonic-gate } else { 987c478bd9Sstevel@tonic-gate do { 99*499fd601Sgww status = show_profs((char *)*argv, print_flag); 1007c478bd9Sstevel@tonic-gate if (status == EXIT_FATAL) { 1017c478bd9Sstevel@tonic-gate break; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate } while (*++argv); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate status = (status == EXIT_OK) ? status : EXIT_FATAL; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate return (status); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static int 1127c478bd9Sstevel@tonic-gate show_profs(char *username, int print_flag) 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate register int status = EXIT_OK; 1157c478bd9Sstevel@tonic-gate register struct passwd *pw; 1167c478bd9Sstevel@tonic-gate register userattr_t *user; 1177c478bd9Sstevel@tonic-gate char *profArray[MAXPROFS]; 1187c478bd9Sstevel@tonic-gate int profcnt = 0; 1197c478bd9Sstevel@tonic-gate execattr_t *exec; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate if (username == NULL) { 1227c478bd9Sstevel@tonic-gate if ((pw = getpwuid(getuid())) == NULL) { 1237c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname); 1257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No passwd entry\n")); 1267c478bd9Sstevel@tonic-gate return (status); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate username = pw->pw_name; 12914839a76Sbubbva } else if (getpwnam(username) == NULL) { 1307c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s : ", progname, username); 1327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No such user\n")); 1337c478bd9Sstevel@tonic-gate return (status); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate if (username != NULL) { 1367c478bd9Sstevel@tonic-gate if ((user = getusernam(username)) != NULL) { 1377c478bd9Sstevel@tonic-gate status = list_profs(user, print_flag); 1387c478bd9Sstevel@tonic-gate } else { 139*499fd601Sgww getDefaultProfiles(username, profArray, &profcnt); 1407c478bd9Sstevel@tonic-gate if (profcnt == 0) { 1417c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1427c478bd9Sstevel@tonic-gate } else { 1437c478bd9Sstevel@tonic-gate if (print_flag & PRINT_LONG) { 1447c478bd9Sstevel@tonic-gate exec = getexecuser(username, KV_COMMAND, 1457c478bd9Sstevel@tonic-gate NULL, GET_ALL); 1467c478bd9Sstevel@tonic-gate print_profs_long(username, 1477c478bd9Sstevel@tonic-gate exec, print_flag); 1487c478bd9Sstevel@tonic-gate free_execattr(exec); 1497c478bd9Sstevel@tonic-gate } else { 1507c478bd9Sstevel@tonic-gate print_profs(username, profArray, 1517c478bd9Sstevel@tonic-gate print_flag, profcnt); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (status == EXIT_NON_FATAL) { 1587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s : ", progname, username); 1597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No profiles\n")); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate return (status); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate static int 1677c478bd9Sstevel@tonic-gate list_profs(userattr_t *user, int print_flag) 1687c478bd9Sstevel@tonic-gate { 1697c478bd9Sstevel@tonic-gate register int status = EXIT_OK; 1707c478bd9Sstevel@tonic-gate char *proflist = (char *)NULL; 1717c478bd9Sstevel@tonic-gate execattr_t *exec = (execattr_t *)NULL; 1727c478bd9Sstevel@tonic-gate char *profArray[MAXPROFS]; 1737c478bd9Sstevel@tonic-gate int profcnt = 0; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (print_flag & PRINT_LONG) { 1767c478bd9Sstevel@tonic-gate exec = getexecuser(user->name, KV_COMMAND, NULL, GET_ALL); 1777c478bd9Sstevel@tonic-gate if (exec == NULL) { 1787c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate } else { 1817c478bd9Sstevel@tonic-gate proflist = kva_match(user->attr, USERATTR_PROFILES_KW); 1827c478bd9Sstevel@tonic-gate if (proflist != NULL) { 1837c478bd9Sstevel@tonic-gate getProfiles(proflist, profArray, &profcnt); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate /* Also get any default profiles */ 186*499fd601Sgww getDefaultProfiles(user->name, profArray, &profcnt); 1877c478bd9Sstevel@tonic-gate if (profcnt == 0) { 1887c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate if (status == EXIT_OK) { 1927c478bd9Sstevel@tonic-gate if (print_flag & PRINT_LONG) { 1937c478bd9Sstevel@tonic-gate print_profs_long(user->name, exec, print_flag); 1947c478bd9Sstevel@tonic-gate free_execattr(exec); 1957c478bd9Sstevel@tonic-gate } else { 1967c478bd9Sstevel@tonic-gate print_profs(user->name, profArray, 1977c478bd9Sstevel@tonic-gate print_flag, profcnt); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate free_userattr(user); 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate return (status); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate static void 2077c478bd9Sstevel@tonic-gate print_profs_long(char *user, void *data, int print_flag) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate register int i; 2117c478bd9Sstevel@tonic-gate register int len; 2127c478bd9Sstevel@tonic-gate int outlen; 2137c478bd9Sstevel@tonic-gate char tmpstr[TMP_BUF_LEN]; 2147c478bd9Sstevel@tonic-gate register char *empty = ""; 2157c478bd9Sstevel@tonic-gate register char *lastname = empty; 2167c478bd9Sstevel@tonic-gate register char *key; 2177c478bd9Sstevel@tonic-gate register char *val; 2187c478bd9Sstevel@tonic-gate register kv_t *kv_pair; 2197c478bd9Sstevel@tonic-gate register execattr_t *exec; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate if (print_flag & PRINT_NAME) { 2227c478bd9Sstevel@tonic-gate (void) printf("%s : ", user); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate (void) printf("\n"); 2257c478bd9Sstevel@tonic-gate exec = (execattr_t *)data; 2267c478bd9Sstevel@tonic-gate while (exec != (execattr_t *)NULL) { 2277c478bd9Sstevel@tonic-gate if (strcmp(exec->name, lastname) != NULL) { 2287c478bd9Sstevel@tonic-gate (void) snprintf(tmpstr, sizeof (tmpstr), 2297c478bd9Sstevel@tonic-gate " %s:", exec->name); 2307c478bd9Sstevel@tonic-gate (void) printf("%s\n", tmpstr); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate (void) snprintf(tmpstr, sizeof (tmpstr), 2337c478bd9Sstevel@tonic-gate " %s ", exec->id); 2347c478bd9Sstevel@tonic-gate outlen = strlen(tmpstr); 2357c478bd9Sstevel@tonic-gate len = outlen; 2367c478bd9Sstevel@tonic-gate (void) printf("%s", tmpstr); 2377c478bd9Sstevel@tonic-gate if ((exec->attr == NULL) || 2387c478bd9Sstevel@tonic-gate (kv_pair = exec->attr->data) == NULL) { 2397c478bd9Sstevel@tonic-gate (void) printf("\n"); 2407c478bd9Sstevel@tonic-gate lastname = exec->name; 2417c478bd9Sstevel@tonic-gate exec = exec->next; 2427c478bd9Sstevel@tonic-gate continue; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate for (i = 0; i < exec->attr->length; i++) { 2457c478bd9Sstevel@tonic-gate key = kv_pair[i].key; 2467c478bd9Sstevel@tonic-gate val = kv_pair[i].value; 2477c478bd9Sstevel@tonic-gate if ((key == NULL) || (val == NULL)) { 2487c478bd9Sstevel@tonic-gate break; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate if (i > 0) { 2517c478bd9Sstevel@tonic-gate (void) strlcpy(tmpstr, ", ", TMP_BUF_LEN); 2527c478bd9Sstevel@tonic-gate format_attr(&outlen, len, tmpstr); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate (void) snprintf(tmpstr, sizeof (tmpstr), "%s=%s", 2557c478bd9Sstevel@tonic-gate key, val); 2567c478bd9Sstevel@tonic-gate format_attr(&outlen, len, tmpstr); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate (void) printf("\n"); 2597c478bd9Sstevel@tonic-gate lastname = exec->name; 2607c478bd9Sstevel@tonic-gate exec = exec->next; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate static void 2667c478bd9Sstevel@tonic-gate format_attr(int *outlen, int len, char *str) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate int newline = 0; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if ((MAX_LINE_LEN - *outlen) < strlen(str)) { 2717c478bd9Sstevel@tonic-gate newline = 1; 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate if (newline) { 2747c478bd9Sstevel@tonic-gate (void) printf("\n"); 2757c478bd9Sstevel@tonic-gate len += strlen(str); 2767c478bd9Sstevel@tonic-gate (void) printf("%*s", len, str); 2777c478bd9Sstevel@tonic-gate *outlen = len; 2787c478bd9Sstevel@tonic-gate } else { 2797c478bd9Sstevel@tonic-gate *outlen += strlen(str); 2807c478bd9Sstevel@tonic-gate (void) printf("%s", str); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate static void 2857c478bd9Sstevel@tonic-gate usage() 2867c478bd9Sstevel@tonic-gate { 2877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2887c478bd9Sstevel@tonic-gate gettext(" usage: profiles [-l] [user1 user2 ...]\n")); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate static void 2927c478bd9Sstevel@tonic-gate getProfiles(char *profiles, char **profArray, int *profcnt) { 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate char *prof; 2957c478bd9Sstevel@tonic-gate char *lasts; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate for (prof = (char *)strtok_r(profiles, PROFLIST_SEP, &lasts); 2987c478bd9Sstevel@tonic-gate prof != NULL; 2997c478bd9Sstevel@tonic-gate prof = (char *)strtok_r(NULL, PROFLIST_SEP, &lasts)) { 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate getproflist(prof, profArray, profcnt); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate static void 3077c478bd9Sstevel@tonic-gate print_profs(char *user, char **profnames, int print_flag, int profcnt) 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate int i; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (print_flag & PRINT_NAME) { 3137c478bd9Sstevel@tonic-gate (void) printf("%s : ", user); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate for (i = 0; i < profcnt; i++) { 3177c478bd9Sstevel@tonic-gate (void) printf("%s\n", profnames[i]); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate free_proflist(profnames, profcnt); 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate /* 3247c478bd9Sstevel@tonic-gate * Get the list of default profiles from /etc/security/policy.conf 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate static void 327*499fd601Sgww getDefaultProfiles(char *user, char **profArray, int *profcnt) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate char *profs = NULL; 3307c478bd9Sstevel@tonic-gate 331*499fd601Sgww if (_get_user_defs(user, NULL, &profs) == 0) { 3327c478bd9Sstevel@tonic-gate if (profs != NULL) { 3337c478bd9Sstevel@tonic-gate getProfiles(profs, profArray, profcnt); 334*499fd601Sgww _free_user_defs(NULL, profs); 3357c478bd9Sstevel@tonic-gate } 336*499fd601Sgww } 3377c478bd9Sstevel@tonic-gate } 338