[12589] | 1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
[2842] | 2 | * |
---|
[12589] | 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. |
---|
[2842] | 14 | */ |
---|
| 15 | |
---|
[12589] | 16 | /* This is detach, which is used to detach lockers from workstations. */ |
---|
[2842] | 17 | |
---|
[13631] | 18 | static const char rcsid[] = "$Id: detach.c,v 1.24 1999-09-27 16:14:13 danw Exp $"; |
---|
[2842] | 19 | |
---|
[12589] | 20 | #include <netdb.h> |
---|
| 21 | #include <pwd.h> |
---|
| 22 | #include <stdlib.h> |
---|
[12678] | 23 | #include <string.h> |
---|
[12589] | 24 | #include <unistd.h> |
---|
[2842] | 25 | |
---|
[12748] | 26 | #include <locker.h> |
---|
| 27 | #include "attach.h" |
---|
[12589] | 28 | #include "agetopt.h" |
---|
| 29 | |
---|
[12748] | 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); |
---|
[12589] | 35 | |
---|
[12748] | 36 | static struct agetopt_option detach_options[] = { |
---|
[12589] | 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 | |
---|
[12748] | 58 | static int verbose = 1; |
---|
[12589] | 59 | |
---|
| 60 | enum { DETACH_FILESYSTEM, DETACH_EXPLICIT, DETACH_BY_HOST }; |
---|
| 61 | |
---|
[12748] | 62 | int detach_main(int argc, char **argv) |
---|
[2842] | 63 | { |
---|
[12589] | 64 | locker_context context; |
---|
| 65 | locker_attachent *at; |
---|
| 66 | int options = LOCKER_DETACH_DEFAULT_OPTIONS; |
---|
[12946] | 67 | char *type = "nfs"; |
---|
[12589] | 68 | int mode = DETACH_FILESYSTEM, opt, gotname = 0; |
---|
| 69 | int status, estatus = 0; |
---|
[2842] | 70 | |
---|
[12589] | 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 | |
---|
[12749] | 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 | |
---|
[12589] | 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(); |
---|
[2842] | 173 | } |
---|
[12589] | 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 | } |
---|
[12797] | 192 | if (status == LOCKER_SUCCESS) |
---|
[12589] | 193 | { |
---|
| 194 | if (verbose) |
---|
| 195 | printf("%s: %s detached\n", whoami, at->name); |
---|
| 196 | locker_free_attachent(context, at); |
---|
| 197 | } |
---|
[12797] | 198 | else if (!LOCKER_DETACH_SUCCESS(status)) |
---|
[12589] | 199 | estatus = 2; |
---|
| 200 | break; |
---|
| 201 | |
---|
| 202 | case DETACH_EXPLICIT: |
---|
| 203 | status = locker_detach_explicit(context, type, argv[optind], |
---|
| 204 | NULL, options, &at); |
---|
[12797] | 205 | if (status == LOCKER_SUCCESS) |
---|
[12589] | 206 | { |
---|
| 207 | if (verbose) |
---|
| 208 | printf("%s: %s detached\n", whoami, at->name); |
---|
| 209 | locker_free_attachent(context, at); |
---|
| 210 | } |
---|
[12797] | 211 | else if (!LOCKER_DETACH_SUCCESS(status)) |
---|
[12589] | 212 | estatus = 2; |
---|
| 213 | break; |
---|
| 214 | |
---|
| 215 | case DETACH_BY_HOST: |
---|
| 216 | detach_by_host(context, argv[optind], options); |
---|
| 217 | break; |
---|
[2842] | 218 | } |
---|
| 219 | |
---|
[12589] | 220 | optind++; |
---|
| 221 | } |
---|
[2842] | 222 | } |
---|
| 223 | |
---|
[12589] | 224 | if (!gotname) |
---|
| 225 | usage(); |
---|
[2842] | 226 | |
---|
[12785] | 227 | locker_do_zsubs(context, LOCKER_ZEPHYR_UNSUBSCRIBE); |
---|
[12589] | 228 | locker_end(context); |
---|
[13604] | 229 | exit(estatus); |
---|
[12589] | 230 | } |
---|
| 231 | |
---|
[12748] | 232 | static void detach_by_host(locker_context context, char *host, int options) |
---|
[2842] | 233 | { |
---|
[12589] | 234 | struct hostent *h; |
---|
[2842] | 235 | |
---|
[12589] | 236 | h = gethostbyname(host); |
---|
| 237 | if (!h) |
---|
| 238 | { |
---|
| 239 | fprintf(stderr, "%s: Could not resolve hostname \"%s\".\n", |
---|
| 240 | whoami, host); |
---|
| 241 | exit(1); |
---|
[2842] | 242 | } |
---|
[13631] | 243 | locker_iterate_attachtab(context, locker_check_host, h->h_addr, |
---|
[12589] | 244 | detach_attachent, &options); |
---|
[2842] | 245 | } |
---|
| 246 | |
---|
[12748] | 247 | static int detach_attachent(locker_context context, locker_attachent *at, |
---|
[12589] | 248 | void *optionsp) |
---|
[2842] | 249 | { |
---|
[12589] | 250 | int status, options = *(int *)optionsp; |
---|
[2842] | 251 | |
---|
[12589] | 252 | status = locker_detach_attachent(context, at, options); |
---|
| 253 | if (status == LOCKER_SUCCESS && verbose) |
---|
| 254 | printf("%s: %s detached\n", whoami, at->name); |
---|
[12678] | 255 | return 0; |
---|
[2842] | 256 | } |
---|
| 257 | |
---|
[12748] | 258 | static void detach_all(locker_context context, int options) |
---|
[2842] | 259 | { |
---|
[12589] | 260 | locker_iterate_attachtab(context, NULL, NULL, detach_attachent, &options); |
---|
| 261 | } |
---|
[2842] | 262 | |
---|
[5229] | 263 | |
---|
[12748] | 264 | static void usage(void) |
---|
[12589] | 265 | { |
---|
| 266 | fprintf(stderr, "Usage: detach [options] filesystem ... [options] filesystem ...\n"); |
---|
[13425] | 267 | fprintf(stderr, " detach [options] mountpoint ...\n"); |
---|
[12589] | 268 | fprintf(stderr, " detach [options] -H host ...\n"); |
---|
| 269 | fprintf(stderr, " detach [options] -a\n"); |
---|
| 270 | exit(1); |
---|
[2842] | 271 | } |
---|