xref: /titanic_50/usr/src/cmd/roles/roles.c (revision 8a3c961b6b8e22607c570d092514b791eb1519e9)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <pwd.h>
32 #include <string.h>
33 #include <libintl.h>
34 #include <locale.h>
35 #include <user_attr.h>
36 
37 
38 #define	EXIT_OK		0
39 #define	EXIT_FATAL	1
40 
41 #ifndef	TEXT_DOMAIN			/* Should be defined by cc -D */
42 #define	TEXT_DOMAIN	"SYS_TEST"
43 #endif
44 
45 static int show_roles(char *, int);
46 
47 static char *progname = "roles";
48 
49 int
50 main(int argc, char *argv[])
51 {
52 	int	print_name = 0;
53 	int	errs = 0;
54 
55 	(void) setlocale(LC_ALL, "");
56 	(void) textdomain(TEXT_DOMAIN);
57 
58 	if (argc > 2)
59 		print_name = 1;
60 
61 	if (argc == 1) {
62 		errs = show_roles(NULL, 0);
63 	} else {
64 		while (*++argv)
65 			errs += show_roles(*argv, print_name);
66 	}
67 
68 	return ((errs == 0) ? EXIT_OK : EXIT_FATAL);
69 }
70 
71 
72 static int
73 show_roles(char *username, int print_name)
74 {
75 	register char		*rolelist = NULL;
76 	register struct passwd	*pw;
77 	register userattr_t	*user;
78 
79 	if (username == NULL) {
80 		if ((pw = getpwuid(getuid())) == NULL) {
81 			(void) fprintf(stderr, "%s: ", progname);
82 			(void) fprintf(stderr, gettext("No passwd entry\n"));
83 			return (1);
84 		}
85 		username = pw->pw_name;
86 	} else if (getpwnam(username) == NULL) {
87 		(void) fprintf(stderr, "%s: %s : ", progname, username);
88 		(void) fprintf(stderr, gettext("No such user\n"));
89 		return (1);
90 	}
91 
92 	if ((user = getusernam(username)) != NULL) {
93 		rolelist = kva_match(user->attr, USERATTR_ROLES_KW);
94 		if (rolelist == NULL)
95 			rolelist = gettext("No roles");
96 		if (print_name && username != NULL)
97 			(void) printf("%s : ", username);
98 		(void) printf("%s\n", rolelist);
99 		free_userattr(user);
100 	} else {
101 		if (print_name && username != NULL)
102 			(void) printf("%s : ", username);
103 		(void) printf("%s\n", gettext("No roles"));
104 	}
105 
106 	return (0);
107 }
108