xref: /illumos-gate/usr/src/cmd/userattr/userattr.c (revision 6a634c9d)
1*49c13cf4Sgww /*
2*49c13cf4Sgww  * CDDL HEADER START
3*49c13cf4Sgww  *
4*49c13cf4Sgww  * The contents of this file are subject to the terms of the
5*49c13cf4Sgww  * Common Development and Distribution License (the "License").
6*49c13cf4Sgww  * You may not use this file except in compliance with the License.
7*49c13cf4Sgww  *
8*49c13cf4Sgww  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*49c13cf4Sgww  * or http://www.opensolaris.org/os/licensing.
10*49c13cf4Sgww  * See the License for the specific language governing permissions
11*49c13cf4Sgww  * and limitations under the License.
12*49c13cf4Sgww  *
13*49c13cf4Sgww  * When distributing Covered Code, include this CDDL HEADER in each
14*49c13cf4Sgww  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*49c13cf4Sgww  * If applicable, add the following below this CDDL HEADER, with the
16*49c13cf4Sgww  * fields enclosed by brackets "[]" replaced with your own identifying
17*49c13cf4Sgww  * information: Portions Copyright [yyyy] [name of copyright owner]
18*49c13cf4Sgww  *
19*49c13cf4Sgww  * CDDL HEADER END
20*49c13cf4Sgww  */
21*49c13cf4Sgww 
22*49c13cf4Sgww /*
23*49c13cf4Sgww  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*49c13cf4Sgww  */
25*49c13cf4Sgww 
26*49c13cf4Sgww #include <errno.h>
27*49c13cf4Sgww #include <locale.h>
28*49c13cf4Sgww #include <pwd.h>
29*49c13cf4Sgww #include <secdb.h>
30*49c13cf4Sgww #include <stdio.h>
31*49c13cf4Sgww #include <stdlib.h>
32*49c13cf4Sgww #include <string.h>
33*49c13cf4Sgww #include <unistd.h>
34*49c13cf4Sgww 
35*49c13cf4Sgww #include <sys/types.h>
36*49c13cf4Sgww 
37*49c13cf4Sgww boolean_t	verbose = B_FALSE;
38*49c13cf4Sgww char		*attr_name = NULL;
39*49c13cf4Sgww 
40*49c13cf4Sgww #if	!defined(TEXT_DOMAIN)
41*49c13cf4Sgww #define	TEXT_DOMAIN "SYS_TEST"
42*49c13cf4Sgww #endif	/* !TEXT_DOMAIN */
43*49c13cf4Sgww 
44*49c13cf4Sgww /*
45*49c13cf4Sgww  *	userattr [-v] attr_name [user]
46*49c13cf4Sgww  */
47*49c13cf4Sgww 
48*49c13cf4Sgww /* ARGSUSED */
49*49c13cf4Sgww static int
attr(const char * name,kva_t * kva,void * ctxt,void * pres)50*49c13cf4Sgww attr(const char *name, kva_t *kva, void *ctxt, void *pres)
51*49c13cf4Sgww {
52*49c13cf4Sgww 	char 	*val;
53*49c13cf4Sgww 
54*49c13cf4Sgww 	if ((val = kva_match(kva, attr_name)) != NULL) {
55*49c13cf4Sgww 		if (verbose) {
56*49c13cf4Sgww 			char *prof_name = "user_attr";
57*49c13cf4Sgww 
58*49c13cf4Sgww 			if (name != NULL) {
59*49c13cf4Sgww 				prof_name = (char *)name;
60*49c13cf4Sgww 			}
61*49c13cf4Sgww 			(void) printf("%s : %s\n", prof_name, val);
62*49c13cf4Sgww 		} else {
63*49c13cf4Sgww 			(void) printf("%s\n", val);
64*49c13cf4Sgww 		}
65*49c13cf4Sgww 		exit(0);
66*49c13cf4Sgww 	}
67*49c13cf4Sgww 
68*49c13cf4Sgww 	return (0);	/* no match */
69*49c13cf4Sgww }
70*49c13cf4Sgww 
71*49c13cf4Sgww int
main(int argc,char * argv[])72*49c13cf4Sgww main(int argc, char *argv[])
73*49c13cf4Sgww {
74*49c13cf4Sgww 	int	opt = 1;
75*49c13cf4Sgww 	char	*user = NULL;
76*49c13cf4Sgww 	struct passwd *pwd;
77*49c13cf4Sgww 
78*49c13cf4Sgww 	(void) setlocale(LC_ALL, "");
79*49c13cf4Sgww 	(void) textdomain(TEXT_DOMAIN);
80*49c13cf4Sgww 
81*49c13cf4Sgww 	if ((argc >= 2) &&
82*49c13cf4Sgww 	    (strncmp(argv[opt], "-v", sizeof ("-v")) == 0)) {
83*49c13cf4Sgww 		verbose = B_TRUE;
84*49c13cf4Sgww 		opt++;
85*49c13cf4Sgww 		argc--;
86*49c13cf4Sgww 	}
87*49c13cf4Sgww 	if (argc >= 2) {
88*49c13cf4Sgww 		attr_name = argv[opt++];
89*49c13cf4Sgww 	}
90*49c13cf4Sgww 	if (argc >= 3) {
91*49c13cf4Sgww 		user = argv[opt++];
92*49c13cf4Sgww 	}
93*49c13cf4Sgww 
94*49c13cf4Sgww 	if ((attr_name == NULL) || (opt < argc)) {
95*49c13cf4Sgww 		(void) fprintf(stderr,
96*49c13cf4Sgww 		    gettext("Usage: %s [-v] attribute_name [user]\n"), argv[0]);
97*49c13cf4Sgww 		exit(1);
98*49c13cf4Sgww 	}
99*49c13cf4Sgww 
100*49c13cf4Sgww 	if (user == NULL) {
101*49c13cf4Sgww 		uid_t	uid = getuid();
102*49c13cf4Sgww 
103*49c13cf4Sgww 		if ((pwd = getpwuid(uid)) == NULL) {
104*49c13cf4Sgww 			(void) fprintf(stderr,
105*49c13cf4Sgww 			    gettext("Cannot find user for uid %d\n"), uid);
106*49c13cf4Sgww 			exit(1);
107*49c13cf4Sgww 		}
108*49c13cf4Sgww 		user = pwd->pw_name;
109*49c13cf4Sgww 	} else {
110*49c13cf4Sgww 		if ((pwd = getpwnam(user)) == NULL) {
111*49c13cf4Sgww 			(void) fprintf(stderr,
112*49c13cf4Sgww 			    gettext("No such user %s\n"), user);
113*49c13cf4Sgww 			exit(1);
114*49c13cf4Sgww 		}
115*49c13cf4Sgww 	}
116*49c13cf4Sgww 
117*49c13cf4Sgww 	(void) _enum_attrs(user, attr, NULL, NULL);
118*49c13cf4Sgww 
119*49c13cf4Sgww 	if (verbose) {
120*49c13cf4Sgww 		(void) fprintf(stderr,
121*49c13cf4Sgww 		    gettext("attribute \"%s\" not found for %s\n"), attr_name,
122*49c13cf4Sgww 		    user);
123*49c13cf4Sgww 	}
124*49c13cf4Sgww 
125*49c13cf4Sgww 	return (1);
126*49c13cf4Sgww }
127