| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <string.h> |
| 27 | #include <hurd.h> |
| 28 | #include <hurd/process.h> |
| 29 | |
| 30 | #include "frobauth.h" |
| 31 | #include "ugids.h" |
| 32 | #include "pids.h" |
| 33 | |
| 34 | static struct argp common_argp; |
| 35 | static struct argp fr_ugids_argp; |
| 36 | |
| 37 | static const struct argp_option common_options[] = |
| 38 | { |
| 39 | {"verbose", 'v', 0, 0, "Print informational messages"}, |
| 40 | {"dry-run", 'n', 0, 0, "Don't do anything, just print what would be done"}, |
| 41 | { 0 } |
| 42 | }; |
| 43 | static struct argp_child common_child_argps[] = |
| 44 | { |
| 45 | { &pids_argp, 0, "Process selection:" }, |
| 46 | { 0 } |
| 47 | }; |
| 48 | |
| 49 | static const char common_args_doc[] = "USER..."; |
| 50 | static const char common_doc[] = |
| 51 | "\vBy default, all processes in the current login collection are selected"; |
| 52 | |
| 53 | static struct argp_child ugids_child_argps[] = |
| 54 | { |
| 55 | { &ugids_argp, 0, "User/group ids:" }, |
| 56 | { 0 } |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | static const struct argp_option ea_options[] = |
| 64 | { |
| 65 | {"available", 'a', 0, 0, "USER... specifies available ids"}, |
| 66 | {"effective", 'e', 0, 0, "USER... specifies effective ids"}, |
| 67 | { 0 } |
| 68 | }; |
| 69 | |
| 70 | static struct argp_child ea_posix_child_argps[] = |
| 71 | { |
| 72 | { &common_argp }, |
| 73 | { &fr_ugids_argp }, |
| 74 | { 0 } |
| 75 | }; |
| 76 | |
| 77 | static struct argp_child no_ugids_child_argps[] = |
| 78 | { |
| 79 | { &common_argp }, |
| 80 | { 0 } |
| 81 | }; |
| 82 |
|
| 83 | |
| 84 | struct frobauth_argp_state |
| 85 | { |
| 86 | struct frobauth *frobauth; |
| 87 | struct pids_argp_params pids_argp_params; |
| 88 | struct ugids_argp_params ugids_argp_params; |
| 89 | }; |
| 90 | |
| 91 | static error_t |
| 92 | common_parse_opt (int key, char *arg, struct argp_state *state) |
| 93 | { |
| 94 | struct frobauth_argp_state *fs = state->input; |
| 95 | struct frobauth *frobauth = fs->frobauth; |
| 96 | |
| 97 | switch (key) |
| 98 | { |
| 99 | case 'v': |
| 100 | frobauth->verbose = 1; break; |
| 101 | case 'n': |
| 102 | frobauth->dry_run = 1; break; |
| 103 | |
| 104 | case ARGP_KEY_END0x1000001: |
| 105 | if (frobauth->num_pids == 0) |
| 106 | |
| 107 | { |
| 108 | pid_t lid; |
| 109 | error_t err = proc_getloginid (getproc (), getpid (), &lid); |
| 110 | |
| 111 | if (err) |
| 112 | argp_failure (state, 2, err, |
| 113 | "Couldn't get current login collection"); |
| 114 | |
| 115 | err = add_fn_pids (&frobauth->pids, &frobauth->num_pids, |
| 116 | lid, proc_getloginpids); |
| 117 | if (err) |
| 118 | argp_failure (state, 3, err, |
| 119 | "%d: Couldn't get login collection pids", lid); |
| 120 | |
| 121 | return err; |
| 122 | } |
| 123 | break; |
| 124 | |
| 125 | case ARGP_KEY_INIT0x1000003: |
| 126 | bzero (fs, sizeof *fs); |
| 127 | fs->frobauth = frobauth; |
| 128 | fs->pids_argp_params.pids = &frobauth->pids; |
| 129 | fs->pids_argp_params.num_pids = &frobauth->num_pids; |
| 130 | state->child_inputs[0] = &fs->pids_argp_params; |
| 131 | break; |
| 132 | |
| 133 | case ARGP_KEY_SUCCESS0x1000004: |
| 134 | case ARGP_KEY_ERROR0x1000005: |
| 135 | free (fs); |
| 136 | |
| 137 | default: |
| 138 | return ARGP_ERR_UNKNOWN((0x10 << 26) | ((7) & 0x3fff)); |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static error_t |
| 144 | ugids_parse_opt (int key, char *arg, struct argp_state *state) |
| 145 | { |
| 146 | struct frobauth_argp_state *fs = state->input; |
| 147 | struct frobauth *frobauth = fs->frobauth; |
| 148 | |
| 149 | switch (key) |
| 150 | { |
| 151 | case ARGP_KEY_INIT0x1000003: |
| 152 | fs->ugids_argp_params.ugids = &frobauth->ugids; |
| 153 | fs->ugids_argp_params.parse_user_args = 1; |
| 154 | fs->ugids_argp_params.default_user = frobauth->default_user; |
| 155 | fs->ugids_argp_params.require_ids = frobauth->require_ids; |
| 156 | fs->pids_argp_params.pids = &frobauth->pids; |
| 157 | fs->pids_argp_params.num_pids = &frobauth->num_pids; |
| 158 | |
| 159 | state->child_inputs[0] = &fs->ugids_argp_params; |
| 160 | |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | return ARGP_ERR_UNKNOWN((0x10 << 26) | ((7) & 0x3fff)); |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static error_t |
| 170 | ea_parse_opt (int key, char *arg, struct argp_state *state) |
| 171 | { |
| 172 | struct frobauth_argp_state *fs = state->hook; |
| 173 | |
| 174 | switch (key) |
| 175 | { |
| 176 | case 'a': |
| 177 | fs->ugids_argp_params.user_args_are_available = 1; |
| 178 | break; |
| 179 | case 'e': |
| 180 | fs->ugids_argp_params.user_args_are_effective = 1; |
| 181 | break; |
| 182 | |
| 183 | case ARGP_KEY_ARG0: |
| 184 | if (!fs->ugids_argp_params.user_args_are_effective |
| 185 | && !fs->ugids_argp_params.user_args_are_available) |
| 186 | |
| 187 | fs->ugids_argp_params.user_args_are_effective = 1; |
| 188 | |
| 189 | |
| 190 | return ARGP_ERR_UNKNOWN((0x10 << 26) | ((7) & 0x3fff)); |
| 191 | |
| 192 | case ARGP_KEY_INIT0x1000003: |
| 193 | |
| 194 | fs = state->hook = malloc (sizeof (struct frobauth_argp_state)); |
| 195 | if (! fs) |
| 196 | return ENOMEM((0x10 << 26) | ((12) & 0x3fff)); |
| 197 | |
| 198 | fs->frobauth = state->input; |
| 199 | state->child_inputs[0] = fs; |
| 200 | state->child_inputs[1] = fs; |
| 201 | break; |
| 202 | |
| 203 | default: |
| 204 | return ARGP_ERR_UNKNOWN((0x10 << 26) | ((7) & 0x3fff)); |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static error_t |
| 210 | posix_parse_opt (int key, char *arg, struct argp_state *state) |
| 211 | { |
| 212 | struct frobauth_argp_state *fs = state->hook; |
| 213 | |
| 214 | switch (key) |
| 215 | { |
| 216 | case ARGP_KEY_INIT0x1000003: |
| 217 | |
| 218 | fs = state->hook = malloc (sizeof (struct frobauth_argp_state)); |
| 219 | if (! fs) |
| 220 | return ENOMEM((0x10 << 26) | ((12) & 0x3fff)); |
| 221 | |
| 222 | fs->frobauth = state->input; |
| 223 | state->child_inputs[0] = fs; |
| 224 | state->child_inputs[1] = fs; |
| 225 | break; |
| 226 | |
| 227 | default: |
| 228 | return ARGP_ERR_UNKNOWN((0x10 << 26) | ((7) & 0x3fff)); |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static error_t |
| 234 | no_ugids_parse_opt (int key, char *arg, struct argp_state *state) |
| 235 | { |
| 236 | struct frobauth_argp_state *fs = state->hook; |
| Value stored to 'fs' during its initialization is never read |
| 237 | |
| 238 | switch (key) |
| 239 | { |
| 240 | case ARGP_KEY_INIT0x1000003: |
| 241 | |
| 242 | fs = state->hook = malloc (sizeof (struct frobauth_argp_state)); |
| 243 | if (! fs) |
| 244 | return ENOMEM((0x10 << 26) | ((12) & 0x3fff)); |
| 245 | |
| 246 | fs->frobauth = state->input; |
| 247 | state->child_inputs[0] = fs; |
| 248 | break; |
| 249 | |
| 250 | default: |
| 251 | return ARGP_ERR_UNKNOWN((0x10 << 26) | ((7) & 0x3fff)); |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static struct argp common_argp = |
| 257 | { |
| 258 | common_options, common_parse_opt, 0, common_doc, common_child_argps |
| 259 | }; |
| 260 | static struct argp fr_ugids_argp = |
| 261 | { |
| 262 | 0, ugids_parse_opt, 0, 0, ugids_child_argps |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | |
| 267 | struct argp frobauth_ea_argp = |
| 268 | { |
| 269 | ea_options, ea_parse_opt, 0, 0, ea_posix_child_argps |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | struct argp frobauth_posix_argp = |
| 274 | { |
| 275 | 0, posix_parse_opt, 0, 0, ea_posix_child_argps |
| 276 | }; |
| 277 | |
| 278 | |
| 279 | struct argp frobauth_no_ugids_argp = |
| 280 | { |
| 281 | 0, no_ugids_parse_opt, 0, 0, no_ugids_child_argps |
| 282 | }; |