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