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]
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
33 #include <user_attr.h>
34 #include <prof_attr.h>
35 #include <exec_attr.h>
36 #include <auth_attr.h>
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"
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";
61 main(int argc
, char *argv
[])
66 int print_flag
= PRINT_DEFAULT
;
68 (void) setlocale(LC_ALL
, "");
69 (void) textdomain(TEXT_DOMAIN
);
71 while ((c
= getopt(argc
, argv
, "l")) != EOF
) {
74 print_flag
|= PRINT_LONG
;
85 status
= show_profs(NULL
, print_flag
);
88 (void) printf("%s:\n", *argv
);
89 status
= show_profs((char *)*argv
,
90 (print_flag
| PRINT_NAME
));
91 if (status
== EXIT_FATAL
) {
94 if (argv
[1] != NULL
) {
95 /* seperate users with empty line */
100 status
= (status
== EXIT_OK
) ? status
: EXIT_FATAL
;
106 show_profs_callback(const char *prof
, kva_t
*pa
, void *pflag
, void *vcnt
)
109 const int *print_flag
= pflag
;
114 if ((*print_flag
) & PRINT_NAME
) {
118 (void) printf("%s%s", indent
, prof
);
119 print_profile_privs(pa
);
126 show_profs(char *username
, int print_flag
)
128 int status
= EXIT_OK
;
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"));
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"));
147 if (print_flag
& PRINT_LONG
) {
148 exec
= getexecuser(username
, KV_COMMAND
, NULL
,
149 GET_ALL
|__SEARCH_ALL_POLS
);
151 print_profs_long(exec
);
154 status
= EXIT_NON_FATAL
;
158 (void) _enum_profs(username
, show_profs_callback
, &print_flag
,
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"));
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]
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
198 print_profs_long(execattr_t
*exec
)
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) {
211 curprofile
= exec
->name
;
213 (void) printf(" %s", curprofile
);
215 pa
= getprofnam(curprofile
);
217 print_profile_privs(pa
->attr
);
222 len
= printf(" %s ", exec
->id
);
224 if ((exec
->attr
== NULL
|| exec
->attr
->data
== NULL
)) {
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.
236 (void) printf("\n%*s", ATTR_COL
, " ");
238 (void) printf("%*s", ATTR_COL
-len
, " ");
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
)
248 /* align subsequent attributes on the same column */
250 (void) printf("%*s", len
, " ");
251 (void) printf("%s=%s\n", key
, val
);
259 (void) fprintf(stderr
,
260 gettext(" usage: profiles [-l] [user1 user2 ...]\n"));
264 print_profile_privs(kva_t
*attr
)
269 privs
= kva_match(attr
, PROFATTR_PRIVS_KW
);
271 (void) printf(" privs=%s", privs
);