xref: /titanic_51/usr/src/cmd/profiles/profiles.c (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
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 2008 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 <deflt.h>
36 #include <user_attr.h>
37 #include <prof_attr.h>
38 #include <exec_attr.h>
39 #include <auth_attr.h>
40 
41 
42 #define	EXIT_OK		0
43 #define	EXIT_FATAL	1
44 #define	EXIT_NON_FATAL	2
45 
46 #define	MAX_LINE_LEN	80		/* max 80 chars per line of output */
47 #define	TMP_BUF_LEN	2048		/* size of temp string buffer */
48 
49 #define	PRINT_DEFAULT	0x0000
50 #define	PRINT_NAME	0x0010
51 #define	PRINT_LONG	0x0020
52 
53 #ifndef TEXT_DOMAIN			/* Should be defined by cc -D */
54 #define	TEXT_DOMAIN	"SYS_TEST"
55 #endif
56 
57 #define	PROFLIST_SEP	","
58 
59 
60 static void usage();
61 static int show_profs(char *, int);
62 static int list_profs(userattr_t *, int);
63 static void print_profs_long(char *, void *, int);
64 static void print_profs(char *, char **, int, int);
65 static void format_attr(int *, int, char *);
66 static void getProfiles(char *, char **, int *);
67 static void getDefaultProfiles(char *, char **, int *);
68 
69 static char *progname = "profiles";
70 
71 int
72 main(int argc, char *argv[])
73 {
74 	extern int	optind;
75 	register int	c;
76 	register int	status = EXIT_OK;
77 	int		print_flag = PRINT_DEFAULT;
78 
79 	(void) setlocale(LC_ALL, "");
80 	(void) textdomain(TEXT_DOMAIN);
81 
82 	while ((c = getopt(argc, argv, "l")) != EOF) {
83 		switch (c) {
84 		case 'l':
85 			print_flag |= PRINT_LONG;
86 			break;
87 		default:
88 			usage();
89 			return (EXIT_FATAL);
90 		}
91 	}
92 	argc -= optind;
93 	argv += optind;
94 
95 	if (*argv == NULL) {
96 		status = show_profs(NULL, print_flag);
97 	} else {
98 		do {
99 			status = show_profs((char *)*argv, print_flag);
100 			if (status == EXIT_FATAL) {
101 				break;
102 			}
103 		} while (*++argv);
104 	}
105 	status = (status == EXIT_OK) ? status : EXIT_FATAL;
106 
107 	return (status);
108 }
109 
110 
111 static int
112 show_profs(char *username, int print_flag)
113 {
114 	register int		status = EXIT_OK;
115 	register struct passwd	*pw;
116 	register userattr_t	*user;
117 	char			*profArray[MAXPROFS];
118 	int			profcnt = 0;
119 	execattr_t		*exec;
120 
121 	if (username == NULL) {
122 		if ((pw = getpwuid(getuid())) == NULL) {
123 			status = EXIT_NON_FATAL;
124 			(void) fprintf(stderr, "%s: ", progname);
125 			(void) fprintf(stderr, gettext("No passwd entry\n"));
126 			return (status);
127 		}
128 		username = pw->pw_name;
129 	} else if (getpwnam(username) == NULL) {
130 		status = EXIT_NON_FATAL;
131 		(void) fprintf(stderr, "%s: %s : ", progname, username);
132 		(void) fprintf(stderr, gettext("No such user\n"));
133 		return (status);
134 	}
135 	if (username != NULL) {
136 		if ((user = getusernam(username)) != NULL) {
137 			status = list_profs(user, print_flag);
138 		} else {
139 			getDefaultProfiles(username, profArray, &profcnt);
140 			if (profcnt == 0) {
141 				status = EXIT_NON_FATAL;
142 			} else {
143 				if (print_flag & PRINT_LONG) {
144 					exec = getexecuser(username, KV_COMMAND,
145 					    NULL, GET_ALL);
146 					print_profs_long(username,
147 					    exec, print_flag);
148 					free_execattr(exec);
149 				} else {
150 					print_profs(username, profArray,
151 					    print_flag, profcnt);
152 				}
153 			}
154 		}
155 	}
156 
157 	if (status == EXIT_NON_FATAL) {
158 		(void) fprintf(stderr, "%s: %s : ", progname, username);
159 		(void) fprintf(stderr, gettext("No profiles\n"));
160 	}
161 
162 	return (status);
163 }
164 
165 
166 static int
167 list_profs(userattr_t *user, int print_flag)
168 {
169 	register int	status = EXIT_OK;
170 	char		*proflist = (char *)NULL;
171 	execattr_t	*exec = (execattr_t *)NULL;
172 	char		*profArray[MAXPROFS];
173 	int		profcnt = 0;
174 
175 	if (print_flag & PRINT_LONG) {
176 		exec = getexecuser(user->name, KV_COMMAND, NULL, GET_ALL);
177 		if (exec == NULL) {
178 			status = EXIT_NON_FATAL;
179 		}
180 	} else {
181 		proflist = kva_match(user->attr, USERATTR_PROFILES_KW);
182 		if (proflist != NULL) {
183 			getProfiles(proflist, profArray, &profcnt);
184 		}
185 		/* Also get any default profiles */
186 		getDefaultProfiles(user->name, profArray, &profcnt);
187 		if (profcnt == 0) {
188 			status = EXIT_NON_FATAL;
189 		}
190 	}
191 	if (status == EXIT_OK) {
192 		if (print_flag & PRINT_LONG) {
193 			print_profs_long(user->name, exec, print_flag);
194 			free_execattr(exec);
195 		} else {
196 			print_profs(user->name, profArray,
197 			    print_flag, profcnt);
198 		}
199 	}
200 	free_userattr(user);
201 
202 	return (status);
203 }
204 
205 
206 static void
207 print_profs_long(char *user, void *data, int print_flag)
208 {
209 
210 	register int		i;
211 	register int		len;
212 	int			outlen;
213 	char			tmpstr[TMP_BUF_LEN];
214 	register char		*empty = "";
215 	register char		*lastname = empty;
216 	register char		*key;
217 	register char		*val;
218 	register kv_t		*kv_pair;
219 	register execattr_t	*exec;
220 
221 	if (print_flag & PRINT_NAME) {
222 		(void) printf("%s : ", user);
223 	}
224 	(void) printf("\n");
225 	exec = (execattr_t *)data;
226 	while (exec != (execattr_t *)NULL) {
227 		if (strcmp(exec->name, lastname) != NULL) {
228 			(void) snprintf(tmpstr, sizeof (tmpstr),
229 			    "      %s:", exec->name);
230 			(void) printf("%s\n", tmpstr);
231 		}
232 		(void) snprintf(tmpstr, sizeof (tmpstr),
233 		    "          %s    ", exec->id);
234 		outlen = strlen(tmpstr);
235 		len = outlen;
236 		(void) printf("%s", tmpstr);
237 		if ((exec->attr == NULL) ||
238 		    (kv_pair = exec->attr->data) == NULL) {
239 			(void) printf("\n");
240 			lastname = exec->name;
241 			exec = exec->next;
242 			continue;
243 		}
244 		for (i = 0; i < exec->attr->length; i++) {
245 			key = kv_pair[i].key;
246 			val = kv_pair[i].value;
247 			if ((key == NULL) || (val == NULL)) {
248 				break;
249 			}
250 			if (i > 0) {
251 				(void) strlcpy(tmpstr, ", ", TMP_BUF_LEN);
252 				format_attr(&outlen, len, tmpstr);
253 			}
254 			(void) snprintf(tmpstr, sizeof (tmpstr), "%s=%s",
255 			    key, val);
256 			format_attr(&outlen, len, tmpstr);
257 		}
258 		(void) printf("\n");
259 		lastname = exec->name;
260 		exec = exec->next;
261 	}
262 }
263 
264 
265 static void
266 format_attr(int *outlen, int len, char *str)
267 {
268 	int newline = 0;
269 
270 	if ((MAX_LINE_LEN - *outlen) < strlen(str)) {
271 		newline = 1;
272 	}
273 	if (newline) {
274 		(void) printf("\n");
275 		len += strlen(str);
276 		(void) printf("%*s", len, str);
277 		*outlen = len;
278 	} else {
279 		*outlen += strlen(str);
280 		(void) printf("%s", str);
281 	}
282 }
283 
284 static void
285 usage()
286 {
287 	(void) fprintf(stderr,
288 	    gettext("  usage: profiles [-l] [user1 user2 ...]\n"));
289 }
290 
291 static void
292 getProfiles(char *profiles, char **profArray, int *profcnt) {
293 
294 	char		*prof;
295 	char		*lasts;
296 
297 	for (prof = (char *)strtok_r(profiles, PROFLIST_SEP, &lasts);
298 	    prof != NULL;
299 	    prof = (char *)strtok_r(NULL, PROFLIST_SEP, &lasts)) {
300 
301 		getproflist(prof, profArray, profcnt);
302 
303 	}
304 }
305 
306 static void
307 print_profs(char *user, char **profnames, int print_flag, int profcnt)
308 {
309 
310 	int i;
311 
312 	if (print_flag & PRINT_NAME) {
313 		(void) printf("%s : ", user);
314 	}
315 
316 	for (i = 0; i < profcnt; i++) {
317 		(void) printf("%s\n", profnames[i]);
318 	}
319 
320 	free_proflist(profnames, profcnt);
321 }
322 
323 /*
324  * Get the list of default profiles from /etc/security/policy.conf
325  */
326 static void
327 getDefaultProfiles(char *user, char **profArray, int *profcnt)
328 {
329 	char *profs = NULL;
330 
331 	if (_get_user_defs(user, NULL, &profs) == 0) {
332 		if (profs != NULL) {
333 			getProfiles(profs, profArray, profcnt);
334 			_free_user_defs(NULL, profs);
335 		}
336 	}
337 }
338