[12589] | 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 | |
---|
[14216] | 20 | static const char rcsid[] = "$Id: fsid.c,v 1.8 2000-01-31 15:58:02 danw Exp $"; |
---|
[12589] | 21 | |
---|
| 22 | #include <netdb.h> |
---|
| 23 | #include <stdlib.h> |
---|
[12678] | 24 | #include <string.h> |
---|
[12589] | 25 | #include <unistd.h> |
---|
| 26 | |
---|
| 27 | #include <locker.h> |
---|
[12748] | 28 | #include "attach.h" |
---|
[12589] | 29 | #include "agetopt.h" |
---|
| 30 | |
---|
[12748] | 31 | static void usage(void); |
---|
| 32 | static int fsid_attachent(locker_context context, locker_attachent *at, |
---|
| 33 | void *opp); |
---|
[14216] | 34 | static void fsid_auth_to_cells(locker_context context, char *cells, int op); |
---|
[12748] | 35 | static char *opped(int op); |
---|
[12589] | 36 | |
---|
[12748] | 37 | static struct agetopt_option fsid_options[] = { |
---|
[12589] | 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 | |
---|
[12748] | 53 | static int verbose = 1; |
---|
[12589] | 54 | |
---|
| 55 | enum { FSID_WHATEVER, FSID_FILESYSTEM, FSID_HOST, FSID_CELL }; |
---|
| 56 | |
---|
[12748] | 57 | int fsid_main(int argc, char **argv) |
---|
[12589] | 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; |
---|
[13031] | 63 | uid_t uid = getuid(); |
---|
[12589] | 64 | |
---|
[13031] | 65 | if (locker_init(&context, uid, NULL, NULL)) |
---|
[12589] | 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': |
---|
[13031] | 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 | { |
---|
[14216] | 83 | char *cells; |
---|
| 84 | |
---|
[13031] | 85 | locker_iterate_attachtab(context, locker_check_owner, &uid, |
---|
| 86 | fsid_attachent, &op); |
---|
[14216] | 87 | cells = getenv("FSID_EXTRA_CELLS"); |
---|
| 88 | if (cells) |
---|
| 89 | fsid_auth_to_cells(context, cells, op); |
---|
[13031] | 90 | } |
---|
[12589] | 91 | gotname++; |
---|
| 92 | break; |
---|
| 93 | |
---|
| 94 | case 'c': |
---|
| 95 | mode = FSID_CELL; |
---|
| 96 | break; |
---|
| 97 | |
---|
[13424] | 98 | case 'f': |
---|
| 99 | mode = FSID_FILESYSTEM; |
---|
| 100 | break; |
---|
| 101 | |
---|
[12589] | 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 | |
---|
[13600] | 126 | case 'v': |
---|
| 127 | verbose = 1; |
---|
| 128 | break; |
---|
| 129 | |
---|
[12589] | 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(); |
---|
[12785] | 187 | locker_end(context); |
---|
[12589] | 188 | exit(estatus); |
---|
| 189 | } |
---|
| 190 | |
---|
[12748] | 191 | static int fsid_attachent(locker_context context, locker_attachent *at, |
---|
| 192 | void *opp) |
---|
[12589] | 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)); |
---|
[12678] | 199 | return 0; |
---|
[12589] | 200 | } |
---|
| 201 | |
---|
[14216] | 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 | |
---|
[12748] | 215 | static char *opped(int op) |
---|
[12589] | 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"; |
---|
[12678] | 227 | default: |
---|
| 228 | return "(unknown)"; |
---|
[12589] | 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
[12748] | 232 | static void usage(void) |
---|
[12589] | 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 | } |
---|