xref: /illumos-gate/usr/src/cmd/userattr/userattr.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
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 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <errno.h>
27 #include <locale.h>
28 #include <pwd.h>
29 #include <secdb.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <sys/types.h>
36 
37 boolean_t	verbose = B_FALSE;
38 char		*attr_name = NULL;
39 
40 #if	!defined(TEXT_DOMAIN)
41 #define	TEXT_DOMAIN "SYS_TEST"
42 #endif	/* !TEXT_DOMAIN */
43 
44 /*
45  *	userattr [-v] attr_name [user]
46  */
47 
48 /* ARGSUSED */
49 static int
attr(const char * name,kva_t * kva,void * ctxt,void * pres)50 attr(const char *name, kva_t *kva, void *ctxt, void *pres)
51 {
52 	char 	*val;
53 
54 	if ((val = kva_match(kva, attr_name)) != NULL) {
55 		if (verbose) {
56 			char *prof_name = "user_attr";
57 
58 			if (name != NULL) {
59 				prof_name = (char *)name;
60 			}
61 			(void) printf("%s : %s\n", prof_name, val);
62 		} else {
63 			(void) printf("%s\n", val);
64 		}
65 		exit(0);
66 	}
67 
68 	return (0);	/* no match */
69 }
70 
71 int
main(int argc,char * argv[])72 main(int argc, char *argv[])
73 {
74 	int	opt = 1;
75 	char	*user = NULL;
76 	struct passwd *pwd;
77 
78 	(void) setlocale(LC_ALL, "");
79 	(void) textdomain(TEXT_DOMAIN);
80 
81 	if ((argc >= 2) &&
82 	    (strncmp(argv[opt], "-v", sizeof ("-v")) == 0)) {
83 		verbose = B_TRUE;
84 		opt++;
85 		argc--;
86 	}
87 	if (argc >= 2) {
88 		attr_name = argv[opt++];
89 	}
90 	if (argc >= 3) {
91 		user = argv[opt++];
92 	}
93 
94 	if ((attr_name == NULL) || (opt < argc)) {
95 		(void) fprintf(stderr,
96 		    gettext("Usage: %s [-v] attribute_name [user]\n"), argv[0]);
97 		exit(1);
98 	}
99 
100 	if (user == NULL) {
101 		uid_t	uid = getuid();
102 
103 		if ((pwd = getpwuid(uid)) == NULL) {
104 			(void) fprintf(stderr,
105 			    gettext("Cannot find user for uid %d\n"), uid);
106 			exit(1);
107 		}
108 		user = pwd->pw_name;
109 	} else {
110 		if ((pwd = getpwnam(user)) == NULL) {
111 			(void) fprintf(stderr,
112 			    gettext("No such user %s\n"), user);
113 			exit(1);
114 		}
115 	}
116 
117 	(void) _enum_attrs(user, attr, NULL, NULL);
118 
119 	if (verbose) {
120 		(void) fprintf(stderr,
121 		    gettext("attribute \"%s\" not found for %s\n"), attr_name,
122 		    user);
123 	}
124 
125 	return (1);
126 }
127