1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#include <deflt.h>
#include <libintl.h>
#include <locale.h>
#include <user_attr.h>
#include <prof_attr.h>
#include <auth_attr.h>
#define ALL_AUTHS "All"
#define ALL_SUN_AUTHS "solaris.*"
#define EXIT_OK 0
#define EXIT_FATAL 1
#define EXIT_NON_FATAL 2
#ifndef TEXT_DOMAIN /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST"
#endif
#define PROFLIST_SEP ","
#define AUTH_SEP ","
#define MAXAUTHS 4096
static int show_auths(char *, char **, int, int);
static int list_auths(userattr_t *, char **, int *);
static char *get_default_auths(char **, int *);
static void getProfiles(char *, char **, int *, char **, int *);
static void add_auths(char *, char **, int *);
static char *progname = "auths";
int
main(int argc, char *argv[])
{
int status = EXIT_OK;
char *defauths[MAXAUTHS];
int defauth_cnt = 0;
int i;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
(void) get_default_auths(defauths, &defauth_cnt);
switch (argc) {
case 1:
status = show_auths(NULL, defauths, defauth_cnt, 0);
break;
case 2:
status = show_auths(argv[argc-1], defauths, defauth_cnt, 0);
break;
default:
while (*++argv) {
status = show_auths(*argv, defauths, defauth_cnt, 1);
if (status == EXIT_FATAL) {
break;
}
}
break;
}
/* free memory allocated for default authorizations */
for (i = 0; i < defauth_cnt; i++) {
free(defauths[i]);
}
status = (status == EXIT_OK) ? status : EXIT_FATAL;
return (status);
}
static int
show_auths(char *username, char **defauths, int defauth_cnt, int print_name)
{
int status = EXIT_OK;
struct passwd *pw;
userattr_t *user;
char *userauths[MAXAUTHS];
int userauth_cnt = 0, old_userauth_cnt;
int i, j, have_allauths, duplicate;
if (username == NULL) {
if ((pw = getpwuid(getuid())) == NULL) {
status = EXIT_NON_FATAL;
(void) fprintf(stderr, "%s: ", progname);
(void) fprintf(stderr, gettext("No passwd entry\n"));
return (status);
}
username = pw->pw_name;
} else if (getpwnam(username) == NULL) {
status = EXIT_NON_FATAL;
(void) fprintf(stderr, "%s: %s : ", progname, username);
(void) fprintf(stderr, gettext("No such user\n"));
return (status);
}
have_allauths = 0;
if (username != NULL) {
/* if ALL_AUTHS is default, don't need to look at other auths */
for (i = 0; i < defauth_cnt; i++) {
if (strcmp(defauths[i], ALL_AUTHS) == 0) {
have_allauths = 1;
break;
}
}
if (have_allauths) {
status = EXIT_OK;
} else if ((user = getusernam(username)) != NULL) {
status = list_auths(user, userauths, &userauth_cnt);
/* check if any profiles have ALL_AUTHS */
for (i = 0; i < userauth_cnt; i++) {
if (strcmp(userauths[i], ALL_AUTHS) == 0) {
have_allauths = 1;
break;
}
}
}
if ((defauth_cnt + userauth_cnt) == 0) {
status = EXIT_NON_FATAL;
}
}
if (status == EXIT_NON_FATAL) {
(void) fprintf(stderr, "%s: %s : ", progname, username);
(void) fprintf(stderr, gettext("No authorizations\n"));
} else {
if (print_name) {
(void) printf("%s : ", username);
}
if (have_allauths) {
(void) printf("%s\n", ALL_SUN_AUTHS);
} else {
/*
* combine the user auths and default auths,
* and eliminate duplicates from the two
*/
old_userauth_cnt = userauth_cnt;
for (i = 0; i < defauth_cnt; i++) {
duplicate = 0;
for (j = 0; j < old_userauth_cnt; j++) {
if (strcmp(userauths[j], defauths[i]) ==
0) {
duplicate = 1;
break;
}
}
if (!duplicate) {
userauths[userauth_cnt] =
strdup(defauths[i]);
userauth_cnt++;
}
}
/* print out the auths */
for (i = 0; i < (userauth_cnt - 1); i++) {
(void) printf("%s,", userauths[i]);
}
/* print out the last entry, without the comma */
(void) printf("%s\n", userauths[userauth_cnt - 1]);
}
}
/* free memory allocated for authorizations */
for (i = 0; i < userauth_cnt; i++) {
free(userauths[i]);
}
return (status);
}
static int
list_auths(userattr_t *user, char **authArray, int *authcnt)
{
int status = EXIT_OK;
char *authlist = NULL;
char *proflist = NULL;
char *profArray[MAXPROFS];
int profcnt = 0;
authlist = kva_match(user->attr, USERATTR_AUTHS_KW);
if (authlist != NULL) {
add_auths(authlist, authArray, authcnt);
}
if ((proflist = kva_match(user->attr, USERATTR_PROFILES_KW)) == NULL) {
if (authcnt == 0) {
status = EXIT_NON_FATAL;
}
} else {
getProfiles(proflist, profArray, &profcnt,
authArray, authcnt);
free_proflist(profArray, profcnt);
}
if (authcnt == 0) {
status = EXIT_NON_FATAL;
}
free_userattr(user);
return (status);
}
static char *
get_default_auths(char **authArray, int *authcnt)
{
char *auths = NULL;
char *profs = NULL;
char *profArray[MAXPROFS];
int profcnt = 0;
if (defopen(AUTH_POLICY) == NULL) {
auths = defread(DEF_AUTH);
if (auths != NULL) {
add_auths(auths, authArray, authcnt);
}
/* get authorizations from default profiles */
profs = defread(DEF_PROF);
if (profs != NULL) {
getProfiles(profs, profArray, &profcnt,
authArray, authcnt);
free_proflist(profArray, profcnt);
}
}
return (auths);
}
void
add_auths(char *auths, char **authArray, int *authcnt)
{
char *authname, *lasts, *real_authname;
int i;
for (authname = (char *)strtok_r(auths, AUTH_SEP, &lasts);
authname != NULL;
authname = (char *)strtok_r(NULL, AUTH_SEP, &lasts)) {
if ((strcmp(authname, KV_WILDCARD) == 0) ||
(strcmp(authname, ALL_SUN_AUTHS) == 0)) {
real_authname = ALL_AUTHS;
} else {
real_authname = authname;
}
/* check to see if authorization is already in list */
for (i = 0; i < *authcnt; i++) {
if (strcmp(real_authname, authArray[i]) == 0) {
break; /* already in list */
}
}
/* not in list, add it in */
if (i == *authcnt) {
authArray[i] = strdup(real_authname);
*authcnt = i + 1;
}
}
}
static void
getProfiles(char *profiles, char **profArray, int *profcnt,
char **authArray, int *authcnt)
{
char *prof;
char *lasts;
profattr_t *pa;
char *auths;
int i;
for (prof = (char *)strtok_r(profiles, PROFLIST_SEP, &lasts);
prof != NULL;
prof = (char *)strtok_r(NULL, PROFLIST_SEP, &lasts)) {
getproflist(prof, profArray, profcnt);
}
/* get authorizations from list of profiles */
for (i = 0; i < *profcnt; i++) {
if ((pa = getprofnam(profArray[i])) == NULL) {
/*
* this should never happen.
* unless the database has an undefined profile
*/
continue;
}
/* get auths this profile */
auths = kva_match(pa->attr, PROFATTR_AUTHS_KW);
if (auths != NULL) {
add_auths(auths, authArray, authcnt);
}
free_profattr(pa);
}
}
|