1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | /* This is fsid, which is used to authenticate/unauthenticate to |
---|
17 | * lockers. |
---|
18 | */ |
---|
19 | |
---|
20 | static const char rcsid[] = "$Id: fsid.c,v 1.8 2000-01-31 15:58:02 danw Exp $"; |
---|
21 | |
---|
22 | #include <netdb.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | #include <unistd.h> |
---|
26 | |
---|
27 | #include <locker.h> |
---|
28 | #include "attach.h" |
---|
29 | #include "agetopt.h" |
---|
30 | |
---|
31 | static void usage(void); |
---|
32 | static int fsid_attachent(locker_context context, locker_attachent *at, |
---|
33 | void *opp); |
---|
34 | static void fsid_auth_to_cells(locker_context context, char *cells, int op); |
---|
35 | static char *opped(int op); |
---|
36 | |
---|
37 | static struct agetopt_option fsid_options[] = { |
---|
38 | { "all", 'a', 0 }, |
---|
39 | { "cell", 'c', 0 }, |
---|
40 | { "debug", 'd', 0 }, |
---|
41 | { "filsys", 'f', 0 }, |
---|
42 | { "host", 'h', 0 }, |
---|
43 | { "map", 'm', 0 }, |
---|
44 | { "purge", 'p', 0 }, |
---|
45 | { "quiet", 'q', 0 }, |
---|
46 | { "purgeuser", 'r', 0 }, |
---|
47 | { "user", 'U', 1 }, |
---|
48 | { "verbose", 'v', 0 }, |
---|
49 | { "unmap", 'u', 0 }, |
---|
50 | { 0, 0, 0 } |
---|
51 | }; |
---|
52 | |
---|
53 | static int verbose = 1; |
---|
54 | |
---|
55 | enum { FSID_WHATEVER, FSID_FILESYSTEM, FSID_HOST, FSID_CELL }; |
---|
56 | |
---|
57 | int fsid_main(int argc, char **argv) |
---|
58 | { |
---|
59 | locker_context context; |
---|
60 | int mode = FSID_WHATEVER, op = LOCKER_AUTH_AUTHENTICATE; |
---|
61 | struct hostent *h; |
---|
62 | int status, estatus = 0, opt, gotname = 0; |
---|
63 | uid_t uid = getuid(); |
---|
64 | |
---|
65 | if (locker_init(&context, uid, NULL, NULL)) |
---|
66 | exit(1); |
---|
67 | |
---|
68 | while (optind < argc) |
---|
69 | { |
---|
70 | while ((opt = attach_getopt(argc, argv, fsid_options)) != -1) |
---|
71 | { |
---|
72 | switch (opt) |
---|
73 | { |
---|
74 | case 'a': |
---|
75 | if (op == LOCKER_AUTH_PURGE || |
---|
76 | op == LOCKER_AUTH_PURGEUSER) |
---|
77 | { |
---|
78 | locker_iterate_attachtab(context, NULL, NULL, |
---|
79 | fsid_attachent, &op); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | char *cells; |
---|
84 | |
---|
85 | locker_iterate_attachtab(context, locker_check_owner, &uid, |
---|
86 | fsid_attachent, &op); |
---|
87 | cells = getenv("FSID_EXTRA_CELLS"); |
---|
88 | if (cells) |
---|
89 | fsid_auth_to_cells(context, cells, op); |
---|
90 | } |
---|
91 | gotname++; |
---|
92 | break; |
---|
93 | |
---|
94 | case 'c': |
---|
95 | mode = FSID_CELL; |
---|
96 | break; |
---|
97 | |
---|
98 | case 'f': |
---|
99 | mode = FSID_FILESYSTEM; |
---|
100 | break; |
---|
101 | |
---|
102 | case 'h': |
---|
103 | mode = FSID_HOST; |
---|
104 | break; |
---|
105 | |
---|
106 | case 'm': |
---|
107 | op = LOCKER_AUTH_AUTHENTICATE; |
---|
108 | break; |
---|
109 | |
---|
110 | case 'p': |
---|
111 | op = LOCKER_AUTH_PURGE; |
---|
112 | break; |
---|
113 | |
---|
114 | case 'q': |
---|
115 | verbose = 0; |
---|
116 | break; |
---|
117 | |
---|
118 | case 'r': |
---|
119 | op = LOCKER_AUTH_PURGEUSER; |
---|
120 | break; |
---|
121 | |
---|
122 | case 'u': |
---|
123 | op = LOCKER_AUTH_UNAUTHENTICATE; |
---|
124 | break; |
---|
125 | |
---|
126 | case 'v': |
---|
127 | verbose = 1; |
---|
128 | break; |
---|
129 | |
---|
130 | case 'd': |
---|
131 | case 'U': |
---|
132 | fprintf(stderr, "%s: The '%c' flag is no longer supported.\n", |
---|
133 | whoami, opt); |
---|
134 | break; |
---|
135 | |
---|
136 | default: |
---|
137 | usage(); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | while (optind < argc && argv[optind][0] != '-') |
---|
142 | { |
---|
143 | gotname++; |
---|
144 | switch (mode) |
---|
145 | { |
---|
146 | case FSID_WHATEVER: |
---|
147 | case FSID_HOST: |
---|
148 | h = gethostbyname(argv[optind]); |
---|
149 | if (h) |
---|
150 | { |
---|
151 | status = locker_auth_to_host(context, whoami, |
---|
152 | argv[optind], op); |
---|
153 | if (status != LOCKER_SUCCESS) |
---|
154 | estatus = 2; |
---|
155 | break; |
---|
156 | } |
---|
157 | else if (mode == FSID_HOST) |
---|
158 | { |
---|
159 | fprintf(stderr, "%s: Could not resolve hostname \"%s\".\n", |
---|
160 | whoami, argv[optind]); |
---|
161 | estatus = 2; |
---|
162 | } |
---|
163 | /* else if (mode == FSID_WHATEVER), fall through */ |
---|
164 | |
---|
165 | case FSID_FILESYSTEM: |
---|
166 | status = locker_auth(context, argv[optind], op); |
---|
167 | if (status != LOCKER_SUCCESS) |
---|
168 | estatus = 2; |
---|
169 | break; |
---|
170 | |
---|
171 | case FSID_CELL: |
---|
172 | status = locker_auth_to_cell(context, whoami, argv[optind], op); |
---|
173 | if (status != LOCKER_SUCCESS) |
---|
174 | estatus = 2; |
---|
175 | break; |
---|
176 | } |
---|
177 | |
---|
178 | if (verbose && status == LOCKER_SUCCESS) |
---|
179 | printf("%s: %s %s\n", whoami, argv[optind], opped(op)); |
---|
180 | |
---|
181 | optind++; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | if (!gotname) |
---|
186 | usage(); |
---|
187 | locker_end(context); |
---|
188 | exit(estatus); |
---|
189 | } |
---|
190 | |
---|
191 | static int fsid_attachent(locker_context context, locker_attachent *at, |
---|
192 | void *opp) |
---|
193 | { |
---|
194 | int status; |
---|
195 | |
---|
196 | status = at->fs->auth(context, at, LOCKER_AUTH_DEFAULT, *(int *)opp); |
---|
197 | if (verbose && status == LOCKER_SUCCESS) |
---|
198 | printf("%s: %s %s\n", whoami, at->name, opped(*(int *)opp)); |
---|
199 | return 0; |
---|
200 | } |
---|
201 | |
---|
202 | static void fsid_auth_to_cells(locker_context context, char *cells, int op) |
---|
203 | { |
---|
204 | char *cell; |
---|
205 | int status; |
---|
206 | |
---|
207 | for (cell = strtok(cells, " "); cell; cell = strtok(NULL, " ")) |
---|
208 | { |
---|
209 | status = locker_auth_to_cell(context, whoami, cell, op); |
---|
210 | if (verbose && status == LOCKER_SUCCESS) |
---|
211 | printf("%s: %s %s\n", whoami, cell, opped(op)); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | static char *opped(int op) |
---|
216 | { |
---|
217 | switch (op) |
---|
218 | { |
---|
219 | case LOCKER_AUTH_AUTHENTICATE: |
---|
220 | return "mapped"; |
---|
221 | case LOCKER_AUTH_UNAUTHENTICATE: |
---|
222 | return "unmapped"; |
---|
223 | case LOCKER_AUTH_PURGE: |
---|
224 | return "purged"; |
---|
225 | case LOCKER_AUTH_PURGEUSER: |
---|
226 | return "user-purged"; |
---|
227 | default: |
---|
228 | return "(unknown)"; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | static void usage(void) |
---|
233 | { |
---|
234 | fprintf(stderr, "Usage: fsid [-q | -v] [-m | -p | -r | -u] [ filesystem | host ] ...\n"); |
---|
235 | fprintf(stderr, " fsid [-q | -v] [-m | -p | -r | -u] -f filesystem ...\n"); |
---|
236 | fprintf(stderr, " fsid [-q | -v] [-m | -p | -r | -u] -h host ...\n"); |
---|
237 | fprintf(stderr, " fsid [-q | -v] [-m | -u] -c cell ...\n"); |
---|
238 | fprintf(stderr, " fsid [-q | -v] [-m | -p | -r | -u] -a\n"); |
---|
239 | exit(1); |
---|
240 | } |
---|