1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <pwd.h>
30 #include <string.h>
31 #include <libintl.h>
32 #include <locale.h>
33 #include <user_attr.h>
34
35
36 #define EXIT_OK 0
37 #define EXIT_FATAL 1
38
39 #ifndef TEXT_DOMAIN /* Should be defined by cc -D */
40 #define TEXT_DOMAIN "SYS_TEST"
41 #endif
42
43 static int show_roles(char *, int);
44
45 static char *progname = "roles";
46
47 int
main(int argc,char * argv[])48 main(int argc, char *argv[])
49 {
50 int print_name = 0;
51 int errs = 0;
52
53 (void) setlocale(LC_ALL, "");
54 (void) textdomain(TEXT_DOMAIN);
55
56 if (argc > 2)
57 print_name = 1;
58
59 if (argc == 1) {
60 errs = show_roles(NULL, 0);
61 } else {
62 while (*++argv)
63 errs += show_roles(*argv, print_name);
64 }
65
66 return ((errs == 0) ? EXIT_OK : EXIT_FATAL);
67 }
68
69
70 static int
show_roles(char * username,int print_name)71 show_roles(char *username, int print_name)
72 {
73 register char *rolelist = NULL;
74 register struct passwd *pw;
75 register userattr_t *user;
76
77 if (username == NULL) {
78 if ((pw = getpwuid(getuid())) == NULL) {
79 (void) fprintf(stderr, "%s: ", progname);
80 (void) fprintf(stderr, gettext("No passwd entry\n"));
81 return (1);
82 }
83 username = pw->pw_name;
84 } else if (getpwnam(username) == NULL) {
85 (void) fprintf(stderr, "%s: %s : ", progname, username);
86 (void) fprintf(stderr, gettext("No such user\n"));
87 return (1);
88 }
89
90 if ((user = getusernam(username)) != NULL) {
91 rolelist = kva_match(user->attr, USERATTR_ROLES_KW);
92 if (rolelist == NULL)
93 rolelist = gettext("No roles");
94 if (print_name && username != NULL)
95 (void) printf("%s : ", username);
96 (void) printf("%s\n", rolelist);
97 free_userattr(user);
98 } else {
99 if (print_name && username != NULL)
100 (void) printf("%s : ", username);
101 (void) printf("%s\n", gettext("No roles"));
102 }
103
104 return (0);
105 }
106