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 /* 22499fd601Sgww * 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); 63*58d1a73cSgww static void print_profs_long(void *, int); 64*58d1a73cSgww static void print_profs(char **, int, int); 657c478bd9Sstevel@tonic-gate static void format_attr(int *, int, char *); 667c478bd9Sstevel@tonic-gate static void getProfiles(char *, char **, int *); 67499fd601Sgww 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; 75*58d1a73cSgww int c; 76*58d1a73cSgww 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) { 96499fd601Sgww status = show_profs(NULL, print_flag); 977c478bd9Sstevel@tonic-gate } else { 987c478bd9Sstevel@tonic-gate do { 99*58d1a73cSgww (void) printf("\n%s :\n", *argv); 100*58d1a73cSgww status = show_profs((char *)*argv, 101*58d1a73cSgww (print_flag | PRINT_NAME)); 1027c478bd9Sstevel@tonic-gate if (status == EXIT_FATAL) { 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate } while (*++argv); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate status = (status == EXIT_OK) ? status : EXIT_FATAL; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate return (status); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate static int 1147c478bd9Sstevel@tonic-gate show_profs(char *username, int print_flag) 1157c478bd9Sstevel@tonic-gate { 116*58d1a73cSgww int status = EXIT_OK; 117*58d1a73cSgww struct passwd *pw; 118*58d1a73cSgww userattr_t *user; 1197c478bd9Sstevel@tonic-gate char *profArray[MAXPROFS]; 1207c478bd9Sstevel@tonic-gate int profcnt = 0; 1217c478bd9Sstevel@tonic-gate execattr_t *exec; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if (username == NULL) { 1247c478bd9Sstevel@tonic-gate if ((pw = getpwuid(getuid())) == NULL) { 1257c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname); 1277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No passwd entry\n")); 1287c478bd9Sstevel@tonic-gate return (status); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate username = pw->pw_name; 13114839a76Sbubbva } else if (getpwnam(username) == NULL) { 1327c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s : ", progname, username); 1347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No such user\n")); 1357c478bd9Sstevel@tonic-gate return (status); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate if (username != NULL) { 1387c478bd9Sstevel@tonic-gate if ((user = getusernam(username)) != NULL) { 1397c478bd9Sstevel@tonic-gate status = list_profs(user, print_flag); 1407c478bd9Sstevel@tonic-gate } else { 141499fd601Sgww getDefaultProfiles(username, profArray, &profcnt); 1427c478bd9Sstevel@tonic-gate if (profcnt == 0) { 1437c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1447c478bd9Sstevel@tonic-gate } else { 1457c478bd9Sstevel@tonic-gate if (print_flag & PRINT_LONG) { 1467c478bd9Sstevel@tonic-gate exec = getexecuser(username, KV_COMMAND, 1477c478bd9Sstevel@tonic-gate NULL, GET_ALL); 148*58d1a73cSgww print_profs_long(exec, print_flag); 1497c478bd9Sstevel@tonic-gate free_execattr(exec); 1507c478bd9Sstevel@tonic-gate } else { 151*58d1a73cSgww print_profs(profArray, print_flag, 152*58d1a73cSgww profcnt); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate } 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 profiles\n")); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate return (status); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate static int 1687c478bd9Sstevel@tonic-gate list_profs(userattr_t *user, int print_flag) 1697c478bd9Sstevel@tonic-gate { 170*58d1a73cSgww int status = EXIT_OK; 1717c478bd9Sstevel@tonic-gate char *proflist = (char *)NULL; 1727c478bd9Sstevel@tonic-gate execattr_t *exec = (execattr_t *)NULL; 1737c478bd9Sstevel@tonic-gate char *profArray[MAXPROFS]; 1747c478bd9Sstevel@tonic-gate int profcnt = 0; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if (print_flag & PRINT_LONG) { 1777c478bd9Sstevel@tonic-gate exec = getexecuser(user->name, KV_COMMAND, NULL, GET_ALL); 1787c478bd9Sstevel@tonic-gate if (exec == NULL) { 1797c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate } else { 1827c478bd9Sstevel@tonic-gate proflist = kva_match(user->attr, USERATTR_PROFILES_KW); 1837c478bd9Sstevel@tonic-gate if (proflist != NULL) { 1847c478bd9Sstevel@tonic-gate getProfiles(proflist, profArray, &profcnt); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate /* Also get any default profiles */ 187499fd601Sgww getDefaultProfiles(user->name, profArray, &profcnt); 1887c478bd9Sstevel@tonic-gate if (profcnt == 0) { 1897c478bd9Sstevel@tonic-gate status = EXIT_NON_FATAL; 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate if (status == EXIT_OK) { 1937c478bd9Sstevel@tonic-gate if (print_flag & PRINT_LONG) { 194*58d1a73cSgww print_profs_long(exec, print_flag); 1957c478bd9Sstevel@tonic-gate free_execattr(exec); 1967c478bd9Sstevel@tonic-gate } else { 197*58d1a73cSgww print_profs(profArray, 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 207*58d1a73cSgww print_profs_long(void *data, int print_flag) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate 210*58d1a73cSgww int i; 211*58d1a73cSgww int len; 2127c478bd9Sstevel@tonic-gate int outlen; 2137c478bd9Sstevel@tonic-gate char tmpstr[TMP_BUF_LEN]; 214*58d1a73cSgww char *lastname = ""; 215*58d1a73cSgww char *key; 216*58d1a73cSgww char *val; 217*58d1a73cSgww kv_t *kv_pair; 218*58d1a73cSgww execattr_t *exec; 2197c478bd9Sstevel@tonic-gate 220*58d1a73cSgww if (!(print_flag & PRINT_NAME)) { 2217c478bd9Sstevel@tonic-gate (void) printf("\n"); 222*58d1a73cSgww } 2237c478bd9Sstevel@tonic-gate exec = (execattr_t *)data; 2247c478bd9Sstevel@tonic-gate while (exec != (execattr_t *)NULL) { 2257c478bd9Sstevel@tonic-gate if (strcmp(exec->name, lastname) != NULL) { 2267c478bd9Sstevel@tonic-gate (void) snprintf(tmpstr, sizeof (tmpstr), 2277c478bd9Sstevel@tonic-gate " %s:", exec->name); 2287c478bd9Sstevel@tonic-gate (void) printf("%s\n", tmpstr); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate (void) snprintf(tmpstr, sizeof (tmpstr), 2317c478bd9Sstevel@tonic-gate " %s ", exec->id); 2327c478bd9Sstevel@tonic-gate outlen = strlen(tmpstr); 2337c478bd9Sstevel@tonic-gate len = outlen; 2347c478bd9Sstevel@tonic-gate (void) printf("%s", tmpstr); 2357c478bd9Sstevel@tonic-gate if ((exec->attr == NULL) || 2367c478bd9Sstevel@tonic-gate (kv_pair = exec->attr->data) == NULL) { 2377c478bd9Sstevel@tonic-gate (void) printf("\n"); 2387c478bd9Sstevel@tonic-gate lastname = exec->name; 2397c478bd9Sstevel@tonic-gate exec = exec->next; 2407c478bd9Sstevel@tonic-gate continue; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate for (i = 0; i < exec->attr->length; i++) { 2437c478bd9Sstevel@tonic-gate key = kv_pair[i].key; 2447c478bd9Sstevel@tonic-gate val = kv_pair[i].value; 2457c478bd9Sstevel@tonic-gate if ((key == NULL) || (val == NULL)) { 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate if (i > 0) { 2497c478bd9Sstevel@tonic-gate (void) strlcpy(tmpstr, ", ", TMP_BUF_LEN); 2507c478bd9Sstevel@tonic-gate format_attr(&outlen, len, tmpstr); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate (void) snprintf(tmpstr, sizeof (tmpstr), "%s=%s", 2537c478bd9Sstevel@tonic-gate key, val); 2547c478bd9Sstevel@tonic-gate format_attr(&outlen, len, tmpstr); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate (void) printf("\n"); 2577c478bd9Sstevel@tonic-gate lastname = exec->name; 2587c478bd9Sstevel@tonic-gate exec = exec->next; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static void 2647c478bd9Sstevel@tonic-gate format_attr(int *outlen, int len, char *str) 2657c478bd9Sstevel@tonic-gate { 2667c478bd9Sstevel@tonic-gate int newline = 0; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate if ((MAX_LINE_LEN - *outlen) < strlen(str)) { 2697c478bd9Sstevel@tonic-gate newline = 1; 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate if (newline) { 2727c478bd9Sstevel@tonic-gate (void) printf("\n"); 2737c478bd9Sstevel@tonic-gate len += strlen(str); 2747c478bd9Sstevel@tonic-gate (void) printf("%*s", len, str); 2757c478bd9Sstevel@tonic-gate *outlen = len; 2767c478bd9Sstevel@tonic-gate } else { 2777c478bd9Sstevel@tonic-gate *outlen += strlen(str); 2787c478bd9Sstevel@tonic-gate (void) printf("%s", str); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate static void 2837c478bd9Sstevel@tonic-gate usage() 2847c478bd9Sstevel@tonic-gate { 2857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2867c478bd9Sstevel@tonic-gate gettext(" usage: profiles [-l] [user1 user2 ...]\n")); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate static void 2907c478bd9Sstevel@tonic-gate getProfiles(char *profiles, char **profArray, int *profcnt) { 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate char *prof; 2937c478bd9Sstevel@tonic-gate char *lasts; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate for (prof = (char *)strtok_r(profiles, PROFLIST_SEP, &lasts); 2967c478bd9Sstevel@tonic-gate prof != NULL; 2977c478bd9Sstevel@tonic-gate prof = (char *)strtok_r(NULL, PROFLIST_SEP, &lasts)) { 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate getproflist(prof, profArray, profcnt); 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate static void 305*58d1a73cSgww print_profs(char **profnames, int print_flag, int profcnt) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate int i; 309*58d1a73cSgww char *indent = ""; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if (print_flag & PRINT_NAME) { 312*58d1a73cSgww indent = " "; 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate for (i = 0; i < profcnt; i++) { 316*58d1a73cSgww (void) printf("%s%s\n", indent, profnames[i]); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate free_proflist(profnames, profcnt); 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate /* 3237c478bd9Sstevel@tonic-gate * Get the list of default profiles from /etc/security/policy.conf 3247c478bd9Sstevel@tonic-gate */ 3257c478bd9Sstevel@tonic-gate static void 326499fd601Sgww getDefaultProfiles(char *user, char **profArray, int *profcnt) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate char *profs = NULL; 3297c478bd9Sstevel@tonic-gate 330499fd601Sgww if (_get_user_defs(user, NULL, &profs) == 0) { 3317c478bd9Sstevel@tonic-gate if (profs != NULL) { 3327c478bd9Sstevel@tonic-gate getProfiles(profs, profArray, profcnt); 333499fd601Sgww _free_user_defs(NULL, profs); 3347c478bd9Sstevel@tonic-gate } 335499fd601Sgww } 3367c478bd9Sstevel@tonic-gate } 337