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 detach, which is used to detach lockers from workstations. */ |
---|
17 | |
---|
18 | static const char rcsid[] = "$Id: detach.c,v 1.24 1999-09-27 16:14:13 danw Exp $"; |
---|
19 | |
---|
20 | #include <netdb.h> |
---|
21 | #include <pwd.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | #include <unistd.h> |
---|
25 | |
---|
26 | #include <locker.h> |
---|
27 | #include "attach.h" |
---|
28 | #include "agetopt.h" |
---|
29 | |
---|
30 | static void usage(void); |
---|
31 | static void detach_all(locker_context context, int options); |
---|
32 | static void detach_by_host(locker_context context, char *host, int options); |
---|
33 | static int detach_attachent(locker_context context, locker_attachent *at, |
---|
34 | void *optionsp); |
---|
35 | |
---|
36 | static struct agetopt_option detach_options[] = { |
---|
37 | { "all", 'a', 0 }, |
---|
38 | { "clean", 'C', 0 }, |
---|
39 | { "debug", 'd', 0 }, |
---|
40 | { "explicit", 'e', 0 }, |
---|
41 | { "force", 'f', 0 }, |
---|
42 | { "nozephyr", 'h', 0 }, |
---|
43 | { "host", 'H', 0 }, |
---|
44 | { "lint", 'L', 0 }, |
---|
45 | { "nomap", 'n', 0 }, |
---|
46 | { "override", 'O', 0 }, |
---|
47 | { "quiet", 'q', 0 }, |
---|
48 | { "spoofhost", 's', 1 }, |
---|
49 | { "type", 't', 1 }, |
---|
50 | { "user", 'U', 1 }, |
---|
51 | { "verbose", 'v', 0 }, |
---|
52 | { "noexplicit", 'x', 0 }, |
---|
53 | { "unmap", 'y', 0 }, |
---|
54 | { "zephyr", 'z', 0 }, |
---|
55 | { 0, 0, 0 } |
---|
56 | }; |
---|
57 | |
---|
58 | static int verbose = 1; |
---|
59 | |
---|
60 | enum { DETACH_FILESYSTEM, DETACH_EXPLICIT, DETACH_BY_HOST }; |
---|
61 | |
---|
62 | int detach_main(int argc, char **argv) |
---|
63 | { |
---|
64 | locker_context context; |
---|
65 | locker_attachent *at; |
---|
66 | int options = LOCKER_DETACH_DEFAULT_OPTIONS; |
---|
67 | char *type = "nfs"; |
---|
68 | int mode = DETACH_FILESYSTEM, opt, gotname = 0; |
---|
69 | int status, estatus = 0; |
---|
70 | |
---|
71 | if (locker_init(&context, getuid(), NULL, NULL)) |
---|
72 | exit(1); |
---|
73 | |
---|
74 | /* Wrap another while around the getopt so we can go through |
---|
75 | * multiple cycles of "[options] lockers...". |
---|
76 | */ |
---|
77 | while (optind < argc) |
---|
78 | { |
---|
79 | while ((opt = attach_getopt(argc, argv, detach_options)) != -1) |
---|
80 | { |
---|
81 | switch (opt) |
---|
82 | { |
---|
83 | case 'a': |
---|
84 | /* backward compatibility: "detach -O -a" implies |
---|
85 | * override, but not unlock. |
---|
86 | */ |
---|
87 | detach_all(context, options & ~LOCKER_DETACH_OPT_UNLOCK); |
---|
88 | gotname++; |
---|
89 | break; |
---|
90 | |
---|
91 | case 'C': |
---|
92 | options |= LOCKER_DETACH_OPT_CLEAN; |
---|
93 | break; |
---|
94 | |
---|
95 | case 'e': |
---|
96 | mode = DETACH_EXPLICIT; |
---|
97 | break; |
---|
98 | |
---|
99 | case 'h': |
---|
100 | options &= ~LOCKER_DETACH_OPT_UNZEPHYR; |
---|
101 | break; |
---|
102 | |
---|
103 | case 'H': |
---|
104 | mode = DETACH_BY_HOST; |
---|
105 | break; |
---|
106 | |
---|
107 | case 'n': |
---|
108 | options &= ~LOCKER_DETACH_OPT_UNAUTH; |
---|
109 | break; |
---|
110 | |
---|
111 | case 'O': |
---|
112 | options |= LOCKER_DETACH_OPT_OVERRIDE | LOCKER_DETACH_OPT_UNLOCK; |
---|
113 | break; |
---|
114 | |
---|
115 | case 'q': |
---|
116 | verbose = 0; |
---|
117 | break; |
---|
118 | |
---|
119 | case 't': |
---|
120 | type = optarg; |
---|
121 | break; |
---|
122 | |
---|
123 | case 'U': |
---|
124 | if (getuid() != 0) |
---|
125 | { |
---|
126 | fprintf(stderr, "%s: You are not allowed to use the " |
---|
127 | "--user option.\n", whoami); |
---|
128 | exit(1); |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | struct passwd *pw; |
---|
133 | |
---|
134 | pw = getpwnam(optarg); |
---|
135 | if (!pw) |
---|
136 | { |
---|
137 | fprintf(stderr, "%s: No such user %s.\n", |
---|
138 | whoami, optarg); |
---|
139 | exit(1); |
---|
140 | } |
---|
141 | locker_end(context); |
---|
142 | if (locker_init(&context, pw->pw_uid, NULL, NULL)) |
---|
143 | exit(1); |
---|
144 | } |
---|
145 | break; |
---|
146 | |
---|
147 | case 'v': |
---|
148 | verbose = 1; |
---|
149 | break; |
---|
150 | |
---|
151 | case 'x': |
---|
152 | mode = DETACH_FILESYSTEM; |
---|
153 | break; |
---|
154 | |
---|
155 | case 'y': |
---|
156 | options |= LOCKER_DETACH_OPT_UNAUTH; |
---|
157 | break; |
---|
158 | |
---|
159 | case 'z': |
---|
160 | options |= LOCKER_DETACH_OPT_UNZEPHYR; |
---|
161 | break; |
---|
162 | |
---|
163 | case 'd': |
---|
164 | case 'f': |
---|
165 | case 'L': |
---|
166 | case 's': |
---|
167 | fprintf(stderr, "%s: The '%c' flag is no longer supported.\n", |
---|
168 | whoami, opt); |
---|
169 | break; |
---|
170 | |
---|
171 | default: |
---|
172 | usage(); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | while (optind < argc && argv[optind][0] != '-') |
---|
177 | { |
---|
178 | gotname++; |
---|
179 | switch (mode) |
---|
180 | { |
---|
181 | case DETACH_FILESYSTEM: |
---|
182 | if (*argv[optind] == '/') |
---|
183 | { |
---|
184 | status = locker_detach(context, NULL, argv[optind], |
---|
185 | options, &at); |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | status = locker_detach(context, argv[optind], NULL, |
---|
190 | options, &at); |
---|
191 | } |
---|
192 | if (status == LOCKER_SUCCESS) |
---|
193 | { |
---|
194 | if (verbose) |
---|
195 | printf("%s: %s detached\n", whoami, at->name); |
---|
196 | locker_free_attachent(context, at); |
---|
197 | } |
---|
198 | else if (!LOCKER_DETACH_SUCCESS(status)) |
---|
199 | estatus = 2; |
---|
200 | break; |
---|
201 | |
---|
202 | case DETACH_EXPLICIT: |
---|
203 | status = locker_detach_explicit(context, type, argv[optind], |
---|
204 | NULL, options, &at); |
---|
205 | if (status == LOCKER_SUCCESS) |
---|
206 | { |
---|
207 | if (verbose) |
---|
208 | printf("%s: %s detached\n", whoami, at->name); |
---|
209 | locker_free_attachent(context, at); |
---|
210 | } |
---|
211 | else if (!LOCKER_DETACH_SUCCESS(status)) |
---|
212 | estatus = 2; |
---|
213 | break; |
---|
214 | |
---|
215 | case DETACH_BY_HOST: |
---|
216 | detach_by_host(context, argv[optind], options); |
---|
217 | break; |
---|
218 | } |
---|
219 | |
---|
220 | optind++; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | if (!gotname) |
---|
225 | usage(); |
---|
226 | |
---|
227 | locker_do_zsubs(context, LOCKER_ZEPHYR_UNSUBSCRIBE); |
---|
228 | locker_end(context); |
---|
229 | exit(estatus); |
---|
230 | } |
---|
231 | |
---|
232 | static void detach_by_host(locker_context context, char *host, int options) |
---|
233 | { |
---|
234 | struct hostent *h; |
---|
235 | |
---|
236 | h = gethostbyname(host); |
---|
237 | if (!h) |
---|
238 | { |
---|
239 | fprintf(stderr, "%s: Could not resolve hostname \"%s\".\n", |
---|
240 | whoami, host); |
---|
241 | exit(1); |
---|
242 | } |
---|
243 | locker_iterate_attachtab(context, locker_check_host, h->h_addr, |
---|
244 | detach_attachent, &options); |
---|
245 | } |
---|
246 | |
---|
247 | static int detach_attachent(locker_context context, locker_attachent *at, |
---|
248 | void *optionsp) |
---|
249 | { |
---|
250 | int status, options = *(int *)optionsp; |
---|
251 | |
---|
252 | status = locker_detach_attachent(context, at, options); |
---|
253 | if (status == LOCKER_SUCCESS && verbose) |
---|
254 | printf("%s: %s detached\n", whoami, at->name); |
---|
255 | return 0; |
---|
256 | } |
---|
257 | |
---|
258 | static void detach_all(locker_context context, int options) |
---|
259 | { |
---|
260 | locker_iterate_attachtab(context, NULL, NULL, detach_attachent, &options); |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | static void usage(void) |
---|
265 | { |
---|
266 | fprintf(stderr, "Usage: detach [options] filesystem ... [options] filesystem ...\n"); |
---|
267 | fprintf(stderr, " detach [options] mountpoint ...\n"); |
---|
268 | fprintf(stderr, " detach [options] -H host ...\n"); |
---|
269 | fprintf(stderr, " detach [options] -a\n"); |
---|
270 | exit(1); |
---|
271 | } |
---|