xref: /titanic_52/usr/src/cmd/roles/roles.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include <string.h>
34 #include <libintl.h>
35 #include <locale.h>
36 #include <user_attr.h>
37 
38 
39 #define	EXIT_OK		0
40 #define	EXIT_FATAL	1
41 
42 #ifndef	TEXT_DOMAIN			/* Should be defined by cc -D */
43 #define	TEXT_DOMAIN	"SYS_TEST"
44 #endif
45 
46 static int show_roles(char *, int);
47 
48 static char *progname = "roles";
49 
50 int
51 main(int argc, char *argv[])
52 {
53 	int	print_name = 0;
54 	int	errs = 0;
55 
56 	(void) setlocale(LC_ALL, "");
57 	(void) textdomain(TEXT_DOMAIN);
58 
59 	if (argc > 2)
60 		print_name = 1;
61 
62 	if (argc == 1) {
63 		errs = show_roles(NULL, 0);
64 	} else {
65 		while (*++argv)
66 			errs += show_roles(*argv, print_name);
67 	}
68 
69 	return ((errs == 0) ? EXIT_OK : EXIT_FATAL);
70 }
71 
72 
73 static int
74 show_roles(char *username, int print_name)
75 {
76 	register char		*rolelist = NULL;
77 	register struct passwd	*pw;
78 	register userattr_t	*user;
79 
80 	if (username == NULL) {
81 		if ((pw = getpwuid(getuid())) == NULL) {
82 			(void) fprintf(stderr, "%s: ", progname);
83 			(void) fprintf(stderr, gettext("No passwd entry\n"));
84 			return (1);
85 		}
86 		username = pw->pw_name;
87 	} else if ((pw = getpwnam(username)) == NULL) {
88 		(void) fprintf(stderr, "%s: %s : ", progname, username);
89 		(void) fprintf(stderr, gettext("No such user\n"));
90 		return (1);
91 	}
92 
93 	if ((user = getusernam(username)) != NULL) {
94 		rolelist = kva_match(user->attr, USERATTR_ROLES_KW);
95 		if (rolelist == NULL)
96 			rolelist = gettext("No roles");
97 		if (print_name && username != NULL)
98 			(void) printf("%s : ", username);
99 		(void) printf("%s\n", rolelist);
100 		free_userattr(user);
101 	} else {
102 		if (print_name && username != NULL)
103 			(void) printf("%s : ", username);
104 		(void) printf("%s\n", gettext("No roles"));
105 	}
106 
107 	return (0);
108 }
109