comm: add -Wall
[unleashed.git] / usr / src / cmd / profiles / profiles.c
blob265fd0c22ebea4c9ea670ce5efa2391a8fcf0d49
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <pwd.h>
29 #include <string.h>
30 #include <libintl.h>
31 #include <locale.h>
32 #include <deflt.h>
33 #include <user_attr.h>
34 #include <prof_attr.h>
35 #include <exec_attr.h>
36 #include <auth_attr.h>
39 #define EXIT_OK 0
40 #define EXIT_FATAL 1
41 #define EXIT_NON_FATAL 2
43 #define TMP_BUF_LEN 2048 /* size of temp string buffer */
45 #define PRINT_DEFAULT 0x0000
46 #define PRINT_NAME 0x0010
47 #define PRINT_LONG 0x0020
49 #ifndef TEXT_DOMAIN /* Should be defined by cc -D */
50 #define TEXT_DOMAIN "SYS_TEST"
51 #endif
53 static void usage();
54 static int show_profs(char *, int);
55 static void print_profs_long(execattr_t *);
56 static void print_profile_privs(kva_t *);
58 static char *progname = "profiles";
60 int
61 main(int argc, char *argv[])
63 extern int optind;
64 int c;
65 int status = EXIT_OK;
66 int print_flag = PRINT_DEFAULT;
68 (void) setlocale(LC_ALL, "");
69 (void) textdomain(TEXT_DOMAIN);
71 while ((c = getopt(argc, argv, "l")) != EOF) {
72 switch (c) {
73 case 'l':
74 print_flag |= PRINT_LONG;
75 break;
76 default:
77 usage();
78 return (EXIT_FATAL);
81 argc -= optind;
82 argv += optind;
84 if (*argv == NULL) {
85 status = show_profs(NULL, print_flag);
86 } else {
87 do {
88 (void) printf("%s:\n", *argv);
89 status = show_profs((char *)*argv,
90 (print_flag | PRINT_NAME));
91 if (status == EXIT_FATAL) {
92 break;
94 if (argv[1] != NULL) {
95 /* seperate users with empty line */
96 (void) printf("\n");
98 } while (*++argv);
100 status = (status == EXIT_OK) ? status : EXIT_FATAL;
102 return (status);
105 static int
106 show_profs_callback(const char *prof, kva_t *pa, void *pflag, void *vcnt)
108 char *indent = "";
109 const int *print_flag = pflag;
110 int *pcnt = vcnt;
112 (*pcnt)++;
114 if ((*print_flag) & PRINT_NAME) {
115 indent = " ";
118 (void) printf("%s%s", indent, prof);
119 print_profile_privs(pa);
120 (void) printf("\n");
122 return (0);
125 static int
126 show_profs(char *username, int print_flag)
128 int status = EXIT_OK;
129 struct passwd *pw;
130 execattr_t *exec;
132 if (username == NULL) {
133 if ((pw = getpwuid(getuid())) == NULL) {
134 status = EXIT_NON_FATAL;
135 (void) fprintf(stderr, "%s: ", progname);
136 (void) fprintf(stderr, gettext("No passwd entry\n"));
137 return (status);
139 username = pw->pw_name;
140 } else if (getpwnam(username) == NULL) {
141 status = EXIT_NON_FATAL;
142 (void) fprintf(stderr, "%s: %s: ", progname, username);
143 (void) fprintf(stderr, gettext("No such user\n"));
144 return (status);
147 if (print_flag & PRINT_LONG) {
148 exec = getexecuser(username, KV_COMMAND, NULL,
149 GET_ALL|__SEARCH_ALL_POLS);
150 if (exec != NULL) {
151 print_profs_long(exec);
152 free_execattr(exec);
153 } else {
154 status = EXIT_NON_FATAL;
156 } else {
157 int cnt = 0;
158 (void) _enum_profs(username, show_profs_callback, &print_flag,
159 &cnt);
161 if (cnt == 0)
162 status = EXIT_NON_FATAL;
165 if (status == EXIT_NON_FATAL) {
166 (void) fprintf(stderr, "%s: %s: ", progname, username);
167 (void) fprintf(stderr, gettext("No profiles\n"));
170 return (status);
174 * print extended profile information.
176 * output is "pretty printed" like
177 * [6spaces]Profile Name1[ possible profile privileges]
178 * [10spaces ]execname1 [skip to ATTR_COL]exec1 attributes1
179 * [ spaces to ATTR_COL ]exec1 attributes2
180 * [10spaces ]execname2 [skip to ATTR_COL]exec2 attributes1
181 * [ spaces to ATTR_COL ]exec2 attributes2
182 * [6spaces]Profile Name2[ possible profile privileges]
183 * etc
186 * ATTR_COL is based on
187 * 10 leading spaces +
188 * 25 positions for the executable +
189 * 1 space seperating the execname from the attributes
190 * so attribute printing starts at column 37 (36 whitespaces)
192 * 25 spaces for the execname seems reasonable since currently
193 * less than 3% of the shipped exec_attr would overflow this
195 #define ATTR_COL 37
197 static void
198 print_profs_long(execattr_t *exec)
200 char *curprofile;
201 int len;
202 kv_t *kv_pair;
203 char *key;
204 char *val;
205 int i;
207 for (curprofile = ""; exec != NULL; exec = exec->next) {
208 /* print profile name if it is a new one */
209 if (strcmp(curprofile, exec->name) != 0) {
210 profattr_t *pa;
211 curprofile = exec->name;
213 (void) printf(" %s", curprofile);
215 pa = getprofnam(curprofile);
216 if (pa != NULL) {
217 print_profile_privs(pa->attr);
218 free_profattr(pa);
220 (void) printf("\n");
222 len = printf(" %s ", exec->id);
224 if ((exec->attr == NULL || exec->attr->data == NULL)) {
225 (void) printf("\n");
226 continue;
230 * if printing the name of the executable got us past the
231 * ATTR_COLth column, skip to ATTR_COL on a new line to
232 * print the attribues.
233 * else, just skip to ATTR_COL column.
235 if (len >= ATTR_COL)
236 (void) printf("\n%*s", ATTR_COL, " ");
237 else
238 (void) printf("%*s", ATTR_COL-len, " ");
239 len = ATTR_COL;
241 /* print all attributes of this profile */
242 kv_pair = exec->attr->data;
243 for (i = 0; i < exec->attr->length; i++) {
244 key = kv_pair[i].key;
245 val = kv_pair[i].value;
246 if (key == NULL || val == NULL)
247 break;
248 /* align subsequent attributes on the same column */
249 if (i > 0)
250 (void) printf("%*s", len, " ");
251 (void) printf("%s=%s\n", key, val);
256 static void
257 usage()
259 (void) fprintf(stderr,
260 gettext(" usage: profiles [-l] [user1 user2 ...]\n"));
263 static void
264 print_profile_privs(kva_t *attr)
266 char *privs;
268 if (attr) {
269 privs = kva_match(attr, PROFATTR_PRIVS_KW);
270 if (privs)
271 (void) printf(" privs=%s", privs);