]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
2d7360ca | 2 | * |
d44cee72 | 3 | * Command line oriented Moira List tool. |
2d7360ca | 4 | * |
5 | * by Mark Rosenstein, September 1988. | |
6 | * | |
7ac48069 | 7 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
1b6b0a57 | 8 | * For copying and distribution information, please see the file |
9 | * <mit-copyright.h>. | |
2d7360ca | 10 | */ |
11 | ||
12 | #include <mit-copyright.h> | |
7ac48069 | 13 | #include <moira.h> |
14 | #include <moira_site.h> | |
2a12a5ec | 15 | #include <mrclient.h> |
7ac48069 | 16 | |
17 | #include <ctype.h> | |
9eb9cfb9 | 18 | #include <errno.h> |
2d7360ca | 19 | #include <stdio.h> |
7b58c1b9 | 20 | #include <stdlib.h> |
f071d8a7 | 21 | #include <string.h> |
2d7360ca | 22 | |
7ac48069 | 23 | RCSID("$Header$"); |
2d7360ca | 24 | |
25 | struct member { | |
5eaef520 | 26 | int type; |
2a942305 | 27 | char *name, *tag; |
2d7360ca | 28 | }; |
29 | ||
b5b167d6 | 30 | /* It is important to membercmp that M_USER < M_LIST < M_STRING */ |
2d7360ca | 31 | #define M_ANY 0 |
32 | #define M_USER 1 | |
33 | #define M_LIST 2 | |
34 | #define M_STRING 3 | |
8c3b414d | 35 | #define M_KERBEROS 4 |
5f7b0741 | 36 | #define M_MACHINE 5 |
37 | #define M_NONE 6 | |
2d7360ca | 38 | |
5f7b0741 | 39 | char *typename[] = { "ANY", "USER", "LIST", "STRING", "KERBEROS", "MACHINE", |
40 | "NONE" }; | |
c9bbc3e4 | 41 | |
1b6b0a57 | 42 | /* argument parsing macro */ |
5eaef520 | 43 | #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b)) |
1b6b0a57 | 44 | |
2d7360ca | 45 | /* flags from command line */ |
763465b8 | 46 | int infoflg, verbose, syncflg, memberflg, recursflg, noauth; |
5f7b0741 | 47 | int showusers, showstrings, showkerberos, showlists, showtags, showmachines; |
763465b8 | 48 | int createflag, setinfo, active, public, hidden, maillist, grouplist; |
c55f98d2 | 49 | int nfsgroup; |
c9bbc3e4 | 50 | struct member *owner, *memacl; |
763465b8 | 51 | char *desc, *newname; |
2d7360ca | 52 | |
53 | /* various member lists */ | |
2a942305 | 54 | struct save_queue *addlist, *dellist, *memberlist, *synclist, *taglist; |
2d7360ca | 55 | |
56 | char *listname, *whoami; | |
57 | ||
7ac48069 | 58 | void usage(char **argv); |
59 | void show_list_member(struct member *memberstruct); | |
60 | int show_list_info(int argc, char **argv, void *hint); | |
61 | int save_list_info(int argc, char **argv, void *hint); | |
62 | int show_list_count(int argc, char **argv, void *hint); | |
63 | void recursive_display_list_members(void); | |
64 | void unique_add_member(struct save_queue *q, struct member *m); | |
65 | int get_list_members(int argc, char **argv, void *sq); | |
66 | void get_members_from_file(char *filename, struct save_queue *queue); | |
67 | int collect(int argc, char **argv, void *l); | |
68 | struct member *parse_member(char *s); | |
69 | int membercmp(const void *mem1, const void *mem2); | |
70 | int sq_count_elts(struct save_queue *q); | |
e8714e3d | 71 | char *get_username(void); |
2d7360ca | 72 | |
5eaef520 | 73 | int main(int argc, char **argv) |
2d7360ca | 74 | { |
5eaef520 | 75 | int status, success; |
76 | char **arg = argv; | |
2a942305 | 77 | char *membervec[4]; |
5eaef520 | 78 | struct member *memberstruct; |
79 | char *server = NULL, *p; | |
80 | ||
81 | /* clear all flags & lists */ | |
82 | infoflg = verbose = syncflg = memberflg = recursflg = 0; | |
83 | noauth = showusers = showstrings = showkerberos = showlists = 0; | |
5f7b0741 | 84 | showtags = showmachines = createflag = setinfo = 0; |
c55f98d2 | 85 | active = public = hidden = maillist = grouplist = nfsgroup = -1; |
5eaef520 | 86 | listname = newname = desc = NULL; |
87 | owner = NULL; | |
c9bbc3e4 | 88 | memacl = NULL; |
5eaef520 | 89 | addlist = sq_create(); |
90 | dellist = sq_create(); | |
91 | memberlist = sq_create(); | |
92 | synclist = sq_create(); | |
2a942305 | 93 | taglist = sq_create(); |
5eaef520 | 94 | whoami = argv[0]; |
95 | ||
96 | success = 1; | |
97 | ||
98 | /* parse args, building addlist, dellist, & synclist */ | |
99 | while (++arg - argv < argc) | |
100 | { | |
101 | if (**arg == '-') | |
1b6b0a57 | 102 | { |
5eaef520 | 103 | if (argis("m", "members")) |
104 | memberflg++; | |
105 | else if (argis("u", "users")) | |
106 | showusers++; | |
107 | else if (argis("s", "strings")) | |
108 | showstrings++; | |
109 | else if (argis("l", "lists")) | |
110 | showlists++; | |
111 | else if (argis("k", "kerberos")) | |
112 | showkerberos++; | |
2a942305 | 113 | else if (argis("t", "tags")) |
114 | showtags++; | |
5eaef520 | 115 | else if (argis("i", "info")) |
116 | infoflg++; | |
117 | else if (argis("n", "noauth")) | |
118 | noauth++; | |
119 | else if (argis("v", "verbose")) | |
120 | verbose++; | |
121 | else if (argis("r", "recursive")) | |
122 | recursflg++; | |
123 | else if (argis("S", "server") || argis("db", "database")) | |
124 | { | |
125 | if (arg - argv < argc - 1) | |
126 | { | |
127 | ++arg; | |
128 | server = *arg; | |
129 | } | |
130 | else | |
131 | usage(argv); | |
132 | } | |
133 | else if (argis("a", "add")) | |
134 | { | |
135 | if (arg - argv < argc - 1) | |
136 | { | |
137 | ++arg; | |
138 | if ((memberstruct = parse_member(*arg))) | |
139 | sq_save_data(addlist, memberstruct); | |
140 | } | |
141 | else | |
142 | usage(argv); | |
143 | } | |
2a942305 | 144 | else if (argis("at", "addtagged")) |
145 | { | |
146 | if (arg - argv < argc - 2) | |
147 | { | |
148 | ++arg; | |
149 | if ((memberstruct = parse_member(*arg))) | |
150 | sq_save_data(addlist, memberstruct); | |
151 | memberstruct->tag = *++arg; | |
152 | } | |
153 | else | |
154 | usage(argv); | |
155 | } | |
5eaef520 | 156 | else if (argis("al", "addlist")) |
157 | { | |
158 | if (arg - argv < argc - 1) | |
159 | { | |
160 | ++arg; | |
161 | get_members_from_file(*arg, addlist); | |
162 | } | |
163 | else | |
164 | usage(argv); | |
165 | } | |
166 | else if (argis("d", "delete")) | |
167 | { | |
168 | if (arg - argv < argc - 1) | |
169 | { | |
170 | ++arg; | |
171 | if ((memberstruct = parse_member(*arg))) | |
172 | sq_save_data(dellist, memberstruct); | |
173 | } | |
174 | else | |
175 | usage(argv); | |
176 | } | |
177 | else if (argis("dl", "deletelist")) | |
178 | { | |
179 | if (arg - argv < argc - 1) | |
180 | { | |
181 | ++arg; | |
182 | get_members_from_file(*arg, dellist); | |
183 | } | |
184 | else | |
185 | usage(argv); | |
186 | } | |
187 | else if (argis("f", "file")) | |
188 | { | |
189 | if (arg - argv < argc - 1) | |
190 | { | |
191 | syncflg++; | |
192 | ++arg; | |
193 | get_members_from_file(*arg, synclist); | |
194 | } | |
195 | else | |
196 | usage(argv); | |
197 | } | |
2a942305 | 198 | else if (argis("ct", "changetag")) |
199 | { | |
200 | if (arg - argv < argc - 2) | |
201 | { | |
202 | ++arg; | |
203 | if ((memberstruct = parse_member(*arg))) | |
204 | sq_save_data(taglist, memberstruct); | |
205 | memberstruct->tag = *++arg; | |
206 | } | |
207 | else | |
208 | usage(argv); | |
209 | } | |
5eaef520 | 210 | else if (argis("C", "create")) |
211 | createflag++; | |
212 | else if (argis("P", "public")) | |
213 | { | |
214 | setinfo++; | |
215 | public = 1; | |
216 | } | |
217 | else if (argis("NP", "private")) | |
218 | { | |
219 | setinfo++; | |
220 | public = 0; | |
221 | } | |
222 | else if (argis("A", "active")) | |
223 | { | |
224 | setinfo++; | |
225 | active = 1; | |
226 | } | |
227 | else if (argis("I", "inactive")) | |
228 | { | |
229 | setinfo++; | |
230 | active = 0; | |
231 | } | |
232 | else if (argis("V", "visible")) | |
233 | { | |
234 | setinfo++; | |
235 | hidden = 0; | |
236 | } | |
237 | else if (argis("H", "hidden")) | |
238 | { | |
239 | setinfo++; | |
240 | hidden = 1; | |
241 | } | |
242 | else if (argis("M", "mail")) | |
243 | { | |
244 | setinfo++; | |
245 | maillist = 1; | |
246 | } | |
247 | else if (argis("NM", "notmail")) | |
248 | { | |
249 | setinfo++; | |
250 | maillist = 0; | |
251 | } | |
252 | else if (argis("G", "group")) | |
253 | { | |
254 | setinfo++; | |
255 | grouplist = 1; | |
256 | } | |
257 | else if (argis("NG", "notgroup")) | |
258 | { | |
259 | setinfo++; | |
260 | grouplist = 0; | |
261 | } | |
c55f98d2 | 262 | else if (argis("N", "nfs")) |
263 | { | |
264 | setinfo++; | |
265 | nfsgroup = 1; | |
266 | } | |
267 | else if (argis("NN", "notnfs")) | |
268 | { | |
269 | setinfo++; | |
270 | nfsgroup = 0; | |
271 | } | |
5eaef520 | 272 | else if (argis("D", "desc")) |
273 | { | |
274 | if (arg - argv < argc - 1) | |
275 | { | |
276 | setinfo++; | |
277 | ++arg; | |
278 | desc = *arg; | |
279 | } | |
280 | else | |
281 | usage(argv); | |
282 | } | |
283 | else if (argis("O", "owner")) | |
284 | { | |
285 | if (arg - argv < argc - 1) | |
286 | { | |
287 | setinfo++; | |
288 | ++arg; | |
289 | owner = parse_member(*arg); | |
290 | } | |
291 | else | |
292 | usage(argv); | |
293 | } | |
c9bbc3e4 | 294 | else if (argis("MA", "memacl")) |
295 | { | |
296 | if (arg - argv < argc -1) | |
297 | { | |
298 | setinfo++; | |
299 | ++arg; | |
300 | memacl = parse_member(*arg); | |
301 | } | |
302 | else | |
303 | usage(argv); | |
304 | } | |
5eaef520 | 305 | else if (argis("R", "rename")) |
306 | { | |
307 | if (arg - argv < argc - 1) | |
308 | { | |
309 | setinfo++; | |
310 | ++arg; | |
311 | newname = *arg; | |
312 | } | |
313 | else | |
2d7360ca | 314 | usage(argv); |
5eaef520 | 315 | } |
316 | else | |
317 | usage(argv); | |
1b6b0a57 | 318 | } |
5eaef520 | 319 | else if (listname == NULL) |
320 | listname = *arg; | |
321 | else | |
322 | usage(argv); | |
2d7360ca | 323 | } |
5eaef520 | 324 | if (listname == NULL) |
325 | usage(argv); | |
326 | ||
327 | /* if no other options specified, turn on list members flag */ | |
328 | if (!(infoflg || syncflg || createflag || setinfo || | |
2a942305 | 329 | addlist->q_next != addlist || dellist->q_next != dellist || |
330 | taglist->q_next != taglist)) | |
5eaef520 | 331 | memberflg++; |
332 | ||
5f7b0741 | 333 | /* If none of {users,strings,lists,kerberos,machines} specified, |
334 | turn them all on */ | |
5eaef520 | 335 | if (!(showusers || showstrings || showlists || showkerberos)) |
5f7b0741 | 336 | showusers = showstrings = showlists = showkerberos = showmachines = 1; |
5eaef520 | 337 | |
338 | /* fire up Moira */ | |
c9bbc3e4 | 339 | status = mrcl_connect(server, "blanche", 4, !noauth); |
2a12a5ec | 340 | if (status == MRCL_AUTH_ERROR) |
5eaef520 | 341 | { |
9cd29ee4 | 342 | com_err(whoami, 0, "Authentication error while working on list %s", |
343 | listname); | |
2a12a5ec | 344 | com_err(whoami, 0, "Try the -noauth flag if you don't " |
345 | "need authentication."); | |
2d7360ca | 346 | } |
2a12a5ec | 347 | if (status) |
348 | exit(2); | |
2d7360ca | 349 | |
84826a5d | 350 | /* check for username/listname clash */ |
351 | if (createflag || (setinfo && newname && strcmp(newname, listname))) | |
352 | { | |
353 | status = mr_query("get_user_account_by_login", 1, | |
354 | createflag ? &listname : &newname, | |
355 | NULL, NULL); | |
356 | if (status != MR_NO_MATCH) | |
357 | fprintf(stderr, "WARNING: A user by that name already exists.\n"); | |
358 | } | |
359 | ||
5eaef520 | 360 | /* create if needed */ |
361 | if (createflag) | |
362 | { | |
c9bbc3e4 | 363 | char *argv[13]; |
5eaef520 | 364 | |
c55f98d2 | 365 | argv[L_NAME] = listname; |
366 | argv[L_ACTIVE] = (active == 0) ? "0" : "1"; | |
367 | argv[L_PUBLIC] = (public == 1) ? "1" : "0"; | |
368 | argv[L_HIDDEN] = (hidden == 1) ? "1" : "0"; | |
369 | argv[L_MAILLIST] = (maillist == 0) ? "0" : "1"; | |
370 | argv[L_GROUP] = (grouplist == 1) ? "1" : "0"; | |
371 | argv[L_GID] = UNIQUE_GID; | |
372 | argv[L_NFSGROUP] = (nfsgroup == 1) ? "1" : "0"; | |
373 | argv[L_DESC] = desc ? desc : "none"; | |
5eaef520 | 374 | |
c9bbc3e4 | 375 | if (memacl) |
376 | { | |
377 | if (memacl->type == M_ANY) | |
378 | { | |
379 | status = mr_query("get_user_account_by_login", 1, | |
380 | &memacl->name, NULL, NULL); | |
381 | if (status == MR_NO_MATCH) | |
382 | memacl->type = M_LIST; | |
383 | else | |
384 | memacl->type = M_USER; | |
385 | } | |
386 | argv[L_MEMACE_TYPE] = typename[memacl->type]; | |
387 | argv[L_MEMACE_NAME] = memacl->name; | |
889d836f | 388 | if (memacl->type == M_KERBEROS) |
389 | { | |
390 | status = mrcl_validate_kerberos_member(argv[L_MEMACE_NAME], | |
391 | &argv[L_MEMACE_NAME]); | |
392 | if (mrcl_get_message()) | |
393 | mrcl_com_err(whoami); | |
138b81d7 | 394 | if (status == MRCL_REJECT) |
395 | exit(1); | |
889d836f | 396 | } |
c9bbc3e4 | 397 | } |
398 | else | |
399 | argv[L_MEMACE_TYPE] = argv[L_MEMACE_NAME] = "NONE"; | |
400 | ||
5eaef520 | 401 | if (owner) |
402 | { | |
c55f98d2 | 403 | argv[L_ACE_NAME] = owner->name; |
5eaef520 | 404 | switch (owner->type) |
405 | { | |
406 | case M_ANY: | |
407 | case M_USER: | |
c55f98d2 | 408 | argv[L_ACE_TYPE] = "USER"; |
c9bbc3e4 | 409 | status = mr_query("add_list", 13, argv, NULL, NULL); |
5eaef520 | 410 | if (owner->type != M_ANY || status != MR_USER) |
411 | break; | |
412 | ||
413 | case M_LIST: | |
c55f98d2 | 414 | argv[L_ACE_TYPE] = "LIST"; |
c9bbc3e4 | 415 | status = mr_query("add_list", 13, argv, NULL, NULL); |
5eaef520 | 416 | break; |
417 | ||
418 | case M_KERBEROS: | |
c55f98d2 | 419 | argv[L_ACE_TYPE] = "KERBEROS"; |
889d836f | 420 | status = mrcl_validate_kerberos_member(argv[L_ACE_NAME], |
421 | &argv[L_ACE_NAME]); | |
422 | if (mrcl_get_message()) | |
423 | mrcl_com_err(whoami); | |
138b81d7 | 424 | if (status == MRCL_REJECT) |
425 | exit(1); | |
c9bbc3e4 | 426 | status = mr_query("add_list", 13, argv, NULL, NULL); |
5eaef520 | 427 | break; |
e8015787 | 428 | case M_NONE: |
429 | argv[L_ACE_TYPE] = argv[L_ACE_NAME] = "NONE"; | |
430 | status = mr_query("add_list", 13, argv, NULL, NULL); | |
431 | break; | |
5eaef520 | 432 | } |
763465b8 | 433 | } |
5eaef520 | 434 | else |
435 | { | |
c55f98d2 | 436 | argv[L_ACE_TYPE] = "USER"; |
e8714e3d | 437 | argv[L_ACE_NAME] = get_username(); |
5eaef520 | 438 | |
c9bbc3e4 | 439 | status = mr_query("add_list", 13, argv, NULL, NULL); |
763465b8 | 440 | } |
441 | ||
5eaef520 | 442 | if (status) |
443 | { | |
444 | com_err(whoami, status, "while creating list."); | |
445 | exit(1); | |
446 | } | |
763465b8 | 447 | } |
5eaef520 | 448 | else if (setinfo) |
449 | { | |
c9bbc3e4 | 450 | char *argv[14]; |
763465b8 | 451 | |
5eaef520 | 452 | status = mr_query("get_list_info", 1, &listname, |
7ac48069 | 453 | save_list_info, argv); |
5eaef520 | 454 | if (status) |
455 | { | |
d44cee72 | 456 | com_err(whoami, status, "while getting list information"); |
5eaef520 | 457 | exit(1); |
2d7360ca | 458 | } |
5eaef520 | 459 | |
460 | argv[0] = listname; | |
461 | if (newname) | |
c55f98d2 | 462 | argv[L_NAME + 1] = newname; |
5eaef520 | 463 | if (active != -1) |
c55f98d2 | 464 | argv[L_ACTIVE + 1] = active ? "1" : "0"; |
5eaef520 | 465 | if (public != -1) |
c55f98d2 | 466 | argv[L_PUBLIC + 1] = public ? "1" : "0"; |
5eaef520 | 467 | if (hidden != -1) |
c55f98d2 | 468 | argv[L_HIDDEN + 1] = hidden ? "1" : "0"; |
5eaef520 | 469 | if (maillist != -1) |
c55f98d2 | 470 | argv[L_MAILLIST + 1] = maillist ? "1" : "0"; |
5eaef520 | 471 | if (grouplist != -1) |
c55f98d2 | 472 | argv[L_GROUP + 1] = grouplist ? "1" : "0"; |
473 | if (nfsgroup != -1) | |
474 | argv[L_NFSGROUP + 1] = nfsgroup ? "1" : "0"; | |
5eaef520 | 475 | if (desc) |
c55f98d2 | 476 | argv[L_DESC + 1] = desc; |
5eaef520 | 477 | |
c9bbc3e4 | 478 | if (memacl) |
479 | { | |
480 | if (memacl->type == M_ANY) | |
481 | { | |
482 | status = mr_query("get_user_account_by_login", 1, | |
483 | &memacl->name, NULL, NULL); | |
484 | if (status == MR_NO_MATCH) | |
485 | memacl->type = M_LIST; | |
486 | else | |
487 | memacl->type = M_USER; | |
488 | } | |
489 | argv[L_MEMACE_TYPE + 1] = typename[memacl->type]; | |
490 | argv[L_MEMACE_NAME + 1] = memacl->name; | |
889d836f | 491 | if (memacl->type == M_KERBEROS) |
492 | { | |
493 | status = mrcl_validate_kerberos_member(argv[L_MEMACE_NAME + 1], | |
494 | &argv[L_MEMACE_NAME + 1]); | |
495 | if (mrcl_get_message()) | |
496 | mrcl_com_err(whoami); | |
138b81d7 | 497 | if (status == MRCL_REJECT) |
498 | exit(1); | |
889d836f | 499 | } |
c9bbc3e4 | 500 | } |
c9bbc3e4 | 501 | |
5eaef520 | 502 | if (owner) |
503 | { | |
c55f98d2 | 504 | argv[L_ACE_NAME + 1] = owner->name; |
5eaef520 | 505 | switch (owner->type) |
506 | { | |
507 | case M_ANY: | |
508 | case M_USER: | |
c55f98d2 | 509 | argv[L_ACE_TYPE + 1] = "USER"; |
c9bbc3e4 | 510 | status = mr_query("update_list", 14, argv, NULL, NULL); |
5eaef520 | 511 | if (owner->type != M_ANY || status != MR_USER) |
512 | break; | |
513 | ||
514 | case M_LIST: | |
c55f98d2 | 515 | argv[L_ACE_TYPE + 1] = "LIST"; |
c9bbc3e4 | 516 | status = mr_query("update_list", 14, argv, NULL, NULL); |
5eaef520 | 517 | break; |
518 | ||
519 | case M_KERBEROS: | |
c55f98d2 | 520 | argv[L_ACE_TYPE + 1] = "KERBEROS"; |
889d836f | 521 | status = mrcl_validate_kerberos_member(argv[L_ACE_NAME + 1], |
522 | &argv[L_ACE_NAME + 1]); | |
523 | if (mrcl_get_message()) | |
524 | mrcl_com_err(whoami); | |
138b81d7 | 525 | if (status == MRCL_REJECT) |
526 | exit(1); | |
c9bbc3e4 | 527 | status = mr_query("update_list", 14, argv, NULL, NULL); |
5eaef520 | 528 | break; |
e8015787 | 529 | case M_NONE: |
530 | argv[L_ACE_TYPE + 1] = argv[L_ACE_NAME + 1] = "NONE"; | |
531 | status = mr_query("update_list", 14, argv, NULL, NULL); | |
532 | break; | |
5eaef520 | 533 | } |
534 | } | |
535 | else | |
c9bbc3e4 | 536 | status = mr_query("update_list", 14, argv, NULL, NULL); |
5eaef520 | 537 | |
538 | if (status) | |
e4d25787 | 539 | { |
540 | com_err(whoami, status, "while updating list."); | |
541 | success = 0; | |
542 | } | |
5eaef520 | 543 | else if (newname) |
544 | listname = newname; | |
2d7360ca | 545 | } |
546 | ||
5eaef520 | 547 | /* display list info if requested to */ |
548 | if (infoflg) | |
549 | { | |
550 | status = mr_query("get_list_info", 1, &listname, show_list_info, NULL); | |
551 | if (status) | |
e4d25787 | 552 | { |
553 | com_err(whoami, status, "while getting list information"); | |
554 | success = 0; | |
555 | } | |
5eaef520 | 556 | if (verbose && !memberflg) |
557 | { | |
558 | status = mr_query("count_members_of_list", 1, &listname, | |
559 | show_list_count, NULL); | |
560 | if (status) | |
e4d25787 | 561 | { |
562 | com_err(whoami, status, "while getting list count"); | |
563 | success = 0; | |
564 | } | |
26624cbe | 565 | } |
5eaef520 | 566 | } |
567 | ||
568 | /* if we're synchronizing to a file, we need to: | |
569 | * get the current members of the list | |
570 | * for each member of the sync file | |
571 | * if they are on the list, remove them from the in-memory copy | |
572 | * if they're not on the list, add them to add-list | |
573 | * if anyone is left on the in-memory copy, put them on the delete-list | |
574 | * lastly, reset memberlist so we can use it again later | |
575 | */ | |
576 | if (syncflg) | |
577 | { | |
578 | status = mr_query("get_members_of_list", 1, &listname, | |
7ac48069 | 579 | get_list_members, memberlist); |
5eaef520 | 580 | if (status) |
581 | { | |
582 | com_err(whoami, status, "getting members of list %s", listname); | |
583 | exit(2); | |
584 | } | |
585 | while (sq_get_data(synclist, &memberstruct)) | |
586 | { | |
587 | struct save_queue *q; | |
588 | int removed = 0; | |
589 | ||
590 | for (q = memberlist->q_next; q != memberlist; q = q->q_next) | |
591 | { | |
592 | if (membercmp(q->q_data, memberstruct) == 0) | |
593 | { | |
594 | q->q_prev->q_next = q->q_next; | |
595 | q->q_next->q_prev = q->q_prev; | |
596 | removed++; | |
597 | break; | |
2d7360ca | 598 | } |
599 | } | |
5eaef520 | 600 | if (!removed) |
601 | sq_save_data(addlist, memberstruct); | |
2d7360ca | 602 | } |
5eaef520 | 603 | while (sq_get_data(memberlist, &memberstruct)) |
604 | sq_save_data(dellist, memberstruct); | |
605 | sq_destroy(memberlist); | |
606 | memberlist = sq_create(); | |
2d7360ca | 607 | } |
608 | ||
5eaef520 | 609 | /* Process the add list */ |
610 | while (sq_get_data(addlist, &memberstruct)) | |
611 | { | |
612 | /* canonicalize string if necessary */ | |
2b3e2afe | 613 | if (memberstruct->type != M_KERBEROS && |
5eaef520 | 614 | (p = strchr(memberstruct->name, '@'))) |
615 | { | |
7ac48069 | 616 | char *host = canonicalize_hostname(strdup(++p)); |
5eaef520 | 617 | static char **mailhubs = NULL; |
618 | char *argv[4]; | |
7ac48069 | 619 | int i; |
5eaef520 | 620 | |
621 | if (!mailhubs) | |
622 | { | |
623 | argv[0] = "mailhub"; | |
624 | argv[1] = "TYPE"; | |
625 | argv[2] = "*"; | |
626 | mailhubs = malloc(sizeof(char *)); | |
627 | mailhubs[0] = NULL; | |
628 | status = mr_query("get_alias", 3, argv, collect, | |
7ac48069 | 629 | &mailhubs); |
5eaef520 | 630 | if (status != MR_SUCCESS && status != MR_NO_MATCH) |
631 | { | |
632 | com_err(whoami, status, | |
633 | " while reading list of MAILHUB servers"); | |
634 | mailhubs[0] = NULL; | |
99bdafb8 | 635 | } |
636 | } | |
5eaef520 | 637 | for (i = 0; (p = mailhubs[i]); i++) |
638 | { | |
639 | if (!strcasecmp(p, host)) | |
640 | { | |
7ac48069 | 641 | host = strdup(memberstruct->name); |
5eaef520 | 642 | *(strchr(memberstruct->name, '@')) = 0; |
2b3e2afe | 643 | if (memberstruct->type == M_STRING) |
644 | memberstruct->type = M_ANY; | |
645 | fprintf(stderr, "Warning: \"%s\" converted to " | |
5eaef520 | 646 | "\"%s\" because it is a local name.\n", |
647 | host, memberstruct->name); | |
648 | break; | |
99bdafb8 | 649 | } |
650 | } | |
5eaef520 | 651 | free(host); |
99bdafb8 | 652 | } |
5eaef520 | 653 | /* now continue adding member */ |
654 | membervec[0] = listname; | |
655 | membervec[2] = memberstruct->name; | |
2a942305 | 656 | membervec[3] = memberstruct->tag; |
5eaef520 | 657 | if (verbose) |
658 | { | |
659 | printf("Adding member "); | |
660 | show_list_member(memberstruct); | |
b5b167d6 | 661 | } |
5eaef520 | 662 | switch (memberstruct->type) |
663 | { | |
2d7360ca | 664 | case M_ANY: |
665 | case M_USER: | |
5eaef520 | 666 | membervec[1] = "USER"; |
2a942305 | 667 | status = mr_query("add_tagged_member_to_list", 4, membervec, |
668 | NULL, NULL); | |
5eaef520 | 669 | if (status == MR_SUCCESS) |
670 | break; | |
671 | else if (status != MR_USER || memberstruct->type != M_ANY) | |
672 | { | |
673 | com_err(whoami, status, "while adding member %s to %s", | |
674 | memberstruct->name, listname); | |
675 | success = 0; | |
2d7360ca | 676 | break; |
2d7360ca | 677 | } |
678 | case M_LIST: | |
5eaef520 | 679 | membervec[1] = "LIST"; |
2a942305 | 680 | status = mr_query("add_tagged_member_to_list", 4, membervec, |
7ac48069 | 681 | NULL, NULL); |
5eaef520 | 682 | if (status == MR_SUCCESS) |
683 | { | |
e8714e3d | 684 | if (!strcmp(membervec[0], get_username())) |
5eaef520 | 685 | { |
686 | fprintf(stderr, "\nWARNING: \"LIST:%s\" was just added " | |
687 | "to list \"%s\".\n", membervec[2], membervec[0]); | |
688 | fprintf(stderr, "If you meant to add yourself to the list " | |
689 | "\"%s\", type:\n", membervec[2]); | |
690 | fprintf(stderr, "\tblanche %s -d %s\t(to undo this)\n", | |
691 | membervec[0], membervec[2]); | |
692 | fprintf(stderr, "\tblanche %s -a %s\t(to add yourself to " | |
693 | "that list)\n", membervec[2], membervec[0]); | |
7b58c1b9 | 694 | } |
5eaef520 | 695 | break; |
696 | } | |
697 | else if (status != MR_LIST || memberstruct->type != M_ANY) | |
698 | { | |
699 | com_err(whoami, status, "while adding member %s to %s", | |
700 | memberstruct->name, listname); | |
701 | success = 0; | |
702 | break; | |
2d7360ca | 703 | } |
704 | case M_STRING: | |
375e818a | 705 | status = mrcl_validate_string_member(memberstruct->name); |
b15a6217 | 706 | if (memberstruct->type == M_ANY && status == MRCL_WARN) |
5eaef520 | 707 | { |
708 | /* if user is trying to add something which isn't a | |
709 | remote string, or a list, or a user, and didn't | |
710 | explicitly specify `STRING:', it's probably a typo */ | |
711 | com_err(whoami, MR_NO_MATCH, "while adding member %s to %s", | |
712 | memberstruct->name, listname); | |
713 | success = 0; | |
714 | break; | |
25978f25 | 715 | } |
b15a6217 | 716 | else |
717 | mrcl_com_err(whoami); | |
718 | ||
719 | if (status == MRCL_REJECT) | |
720 | { | |
721 | success = 0; | |
722 | break; | |
723 | } | |
5eaef520 | 724 | |
725 | membervec[1] = "STRING"; | |
2a942305 | 726 | status = mr_query("add_tagged_member_to_list", 4, membervec, |
7ac48069 | 727 | NULL, NULL); |
5eaef520 | 728 | if (status != MR_SUCCESS) |
729 | { | |
d44cee72 | 730 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 731 | memberstruct->name, listname); |
e681e918 | 732 | success = 0; |
733 | } | |
5eaef520 | 734 | break; |
8c3b414d | 735 | case M_KERBEROS: |
5eaef520 | 736 | membervec[1] = "KERBEROS"; |
375e818a | 737 | status = mrcl_validate_kerberos_member(membervec[2], &membervec[2]); |
738 | if (mrcl_get_message()) | |
739 | mrcl_com_err(whoami); | |
138b81d7 | 740 | if (status == MRCL_REJECT) |
741 | { | |
742 | success = 0; | |
743 | break; | |
744 | } | |
2a942305 | 745 | status = mr_query("add_tagged_member_to_list", 4, membervec, |
7ac48069 | 746 | NULL, NULL); |
5eaef520 | 747 | if (status != MR_SUCCESS) |
748 | { | |
d44cee72 | 749 | com_err(whoami, status, "while adding member %s to %s", |
8c3b414d | 750 | memberstruct->name, listname); |
e681e918 | 751 | success = 0; |
752 | } | |
375e818a | 753 | free(membervec[2]); |
2c0dfed5 | 754 | break; |
5f7b0741 | 755 | case M_MACHINE: |
756 | membervec[1] = "MACHINE"; | |
757 | membervec[2] = canonicalize_hostname(strdup(memberstruct->name)); | |
758 | status = mr_query("add_tagged_member_to_list", 4, membervec, | |
759 | NULL, NULL); | |
760 | if (status != MR_SUCCESS) | |
761 | { | |
762 | com_err(whoami, status, "while adding member %s to %s", | |
763 | memberstruct->name, listname); | |
764 | success = 0; | |
765 | } | |
766 | free(membervec[2]); | |
2d7360ca | 767 | } |
768 | } | |
769 | ||
5eaef520 | 770 | /* Process the delete list */ |
771 | while (sq_get_data(dellist, &memberstruct)) | |
772 | { | |
773 | membervec[0] = listname; | |
774 | membervec[2] = memberstruct->name; | |
775 | if (verbose) | |
776 | { | |
777 | printf("Deleting member "); | |
778 | show_list_member(memberstruct); | |
b5b167d6 | 779 | } |
5eaef520 | 780 | switch (memberstruct->type) |
781 | { | |
2d7360ca | 782 | case M_ANY: |
783 | case M_USER: | |
5eaef520 | 784 | membervec[1] = "USER"; |
785 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 786 | NULL, NULL); |
5eaef520 | 787 | if (status == MR_SUCCESS) |
788 | break; | |
789 | else if ((status != MR_USER && status != MR_NO_MATCH) || | |
790 | memberstruct->type != M_ANY) | |
791 | { | |
792 | com_err(whoami, status, "while deleting member %s from %s", | |
793 | memberstruct->name, listname); | |
794 | success = 0; | |
2d7360ca | 795 | break; |
2d7360ca | 796 | } |
797 | case M_LIST: | |
5eaef520 | 798 | membervec[1] = "LIST"; |
799 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 800 | NULL, NULL); |
5eaef520 | 801 | if (status == MR_SUCCESS) |
802 | break; | |
803 | else if ((status != MR_LIST && status != MR_NO_MATCH) || | |
804 | memberstruct->type != M_ANY) | |
805 | { | |
806 | if (status == MR_PERM && memberstruct->type == M_ANY && | |
e8714e3d | 807 | !strcmp(membervec[2], get_username())) |
5eaef520 | 808 | { |
809 | /* M_ANY means we've fallen through from the user | |
810 | * case. The user is trying to remove himself from | |
811 | * a list, but we got MR_USER or MR_NO_MATCH above, | |
812 | * meaning he's not really on it, and we got MR_PERM | |
813 | * when trying to remove LIST:$USER because he's not | |
814 | * on the acl. That error is useless, so return | |
815 | * MR_NO_MATCH instead. However, this will generate the | |
816 | * wrong error if the user was trying to remove the list | |
817 | * with his username from a list he doesn't administrate | |
818 | * without explicitly specifying "list:". | |
819 | */ | |
40637dba | 820 | status = MR_NO_MATCH; |
821 | } | |
5eaef520 | 822 | com_err(whoami, status, "while deleting member %s from %s", |
823 | memberstruct->name, listname); | |
824 | success = 0; | |
825 | break; | |
2d7360ca | 826 | } |
827 | case M_STRING: | |
5eaef520 | 828 | membervec[1] = "STRING"; |
829 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 830 | NULL, NULL); |
5eaef520 | 831 | if (status == MR_STRING && memberstruct->type == M_ANY) |
832 | { | |
d44cee72 | 833 | com_err(whoami, 0, " Unable to find member %s to delete from %s", |
2d7360ca | 834 | memberstruct->name, listname); |
e681e918 | 835 | success = 0; |
e8714e3d | 836 | if (!strcmp(membervec[0], get_username())) |
5eaef520 | 837 | { |
838 | fprintf(stderr, "(If you were trying to remove yourself " | |
839 | "from the list \"%s\",\n", membervec[2]); | |
840 | fprintf(stderr, "the correct command is \"blanche %s -d " | |
841 | "%s\".)\n", membervec[2], membervec[0]); | |
842 | } | |
843 | } | |
844 | else if (status != MR_SUCCESS) | |
845 | { | |
d44cee72 | 846 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 847 | memberstruct->name, listname); |
e681e918 | 848 | success = 0; |
849 | } | |
5eaef520 | 850 | break; |
8c3b414d | 851 | case M_KERBEROS: |
5eaef520 | 852 | membervec[1] = "KERBEROS"; |
853 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 854 | NULL, NULL); |
375e818a | 855 | if (status == MR_STRING || status == MR_NO_MATCH) |
856 | { | |
857 | /* Try canonicalizing the Kerberos principal and trying | |
858 | * again. If we succeed, print the message from mrcl. | |
859 | * Otherwise, just pretend we never did this and print | |
860 | * the original error message. | |
861 | */ | |
862 | mrcl_validate_kerberos_member(membervec[2], &membervec[2]); | |
863 | if (mrcl_get_message()) | |
864 | { | |
865 | if (mr_query("delete_member_from_list", 3, membervec, | |
866 | NULL, NULL) == MR_SUCCESS) | |
867 | mrcl_com_err(whoami); | |
868 | status = MR_SUCCESS; | |
869 | } | |
870 | } | |
5eaef520 | 871 | if (status != MR_SUCCESS) |
872 | { | |
d44cee72 | 873 | com_err(whoami, status, "while deleting member %s from %s", |
8c3b414d | 874 | memberstruct->name, listname); |
e681e918 | 875 | success = 0; |
876 | } | |
2c0dfed5 | 877 | break; |
5f7b0741 | 878 | case M_MACHINE: |
879 | membervec[1] = "MACHINE"; | |
880 | membervec[2] = canonicalize_hostname(memberstruct->name); | |
881 | status = mr_query("delete_member_from_list", 3, membervec, | |
882 | NULL, NULL); | |
883 | if (status != MR_SUCCESS) | |
884 | { | |
885 | com_err(whoami, status, "while deleting member %s from %s", | |
886 | memberstruct->name, listname); | |
887 | success = 0; | |
888 | } | |
889 | free(membervec[2]); | |
2d7360ca | 890 | } |
891 | } | |
892 | ||
2a942305 | 893 | /* Process the tag list */ |
894 | while (sq_get_data(taglist, &memberstruct)) | |
895 | { | |
896 | membervec[0] = listname; | |
897 | membervec[2] = memberstruct->name; | |
898 | membervec[3] = memberstruct->tag; | |
899 | if (verbose) | |
900 | { | |
901 | printf("Tagging member "); | |
902 | show_list_member(memberstruct); | |
903 | } | |
904 | switch (memberstruct->type) | |
905 | { | |
906 | case M_ANY: | |
907 | case M_USER: | |
908 | membervec[1] = "USER"; | |
909 | status = mr_query("tag_member_of_list", 4, membervec, | |
910 | NULL, NULL); | |
911 | if (status == MR_SUCCESS) | |
912 | break; | |
913 | else if ((status != MR_USER && status != MR_NO_MATCH) || | |
914 | memberstruct->type != M_ANY) | |
915 | { | |
916 | com_err(whoami, status, "while changing tag on member %s of %s", | |
917 | memberstruct->name, listname); | |
918 | success = 0; | |
919 | break; | |
920 | } | |
921 | case M_LIST: | |
922 | membervec[1] = "LIST"; | |
923 | status = mr_query("tag_member_of_list", 4, membervec, | |
924 | NULL, NULL); | |
925 | if (status == MR_SUCCESS) | |
926 | break; | |
927 | else if ((status != MR_LIST && status != MR_NO_MATCH) || | |
928 | memberstruct->type != M_ANY) | |
929 | { | |
930 | com_err(whoami, status, "while changing tag on member %s of %s", | |
931 | memberstruct->name, listname); | |
932 | success = 0; | |
933 | break; | |
934 | } | |
935 | case M_STRING: | |
936 | membervec[1] = "STRING"; | |
937 | status = mr_query("tag_member_of_list", 4, membervec, | |
938 | NULL, NULL); | |
939 | if (status == MR_STRING && memberstruct->type == M_ANY) | |
940 | { | |
941 | com_err(whoami, 0, " Unable to find member %s on list %s", | |
942 | memberstruct->name, listname); | |
943 | success = 0; | |
944 | } | |
945 | else if (status != MR_SUCCESS) | |
946 | { | |
947 | com_err(whoami, status, "while retagging member %s on %s", | |
948 | memberstruct->name, listname); | |
949 | success = 0; | |
950 | } | |
951 | break; | |
952 | case M_KERBEROS: | |
953 | membervec[1] = "KERBEROS"; | |
954 | status = mr_query("tag_member_of_list", 4, membervec, | |
955 | NULL, NULL); | |
375e818a | 956 | if (status == MR_STRING || status == MR_NO_MATCH) |
957 | { | |
958 | /* Try canonicalizing the Kerberos principal and trying | |
959 | * again. If we succeed, print the message from mrcl. | |
960 | * Otherwise, just pretend we never did this and print | |
961 | * the original error message. | |
962 | */ | |
963 | mrcl_validate_kerberos_member(membervec[2], &membervec[2]); | |
964 | if (mrcl_get_message()) | |
965 | { | |
966 | if (mr_query("tag_member_of_list", 4, membervec, | |
967 | NULL, NULL) == MR_SUCCESS) | |
968 | mrcl_com_err(whoami); | |
969 | status = MR_SUCCESS; | |
970 | } | |
971 | } | |
2a942305 | 972 | if (status != MR_SUCCESS) |
973 | { | |
974 | com_err(whoami, status, "while changing tag on member %s of %s", | |
975 | memberstruct->name, listname); | |
976 | success = 0; | |
977 | } | |
978 | } | |
979 | } | |
980 | ||
5eaef520 | 981 | /* Display the members of the list now, if requested */ |
982 | if (memberflg) | |
983 | { | |
984 | if (recursflg) | |
985 | recursive_display_list_members(); | |
986 | else | |
987 | { | |
2a942305 | 988 | status = mr_query(showtags ? "get_tagged_members_of_list" : |
989 | "get_members_of_list", 1, &listname, | |
7ac48069 | 990 | get_list_members, memberlist); |
5eaef520 | 991 | if (status) |
992 | com_err(whoami, status, "while getting members of list %s", | |
993 | listname); | |
994 | while (sq_get_data(memberlist, &memberstruct)) | |
995 | show_list_member(memberstruct); | |
b5b167d6 | 996 | } |
997 | } | |
2d7360ca | 998 | |
5eaef520 | 999 | /* We're done! */ |
1000 | mr_disconnect(); | |
1001 | exit(success ? 0 : 1); | |
2d7360ca | 1002 | } |
1003 | ||
7ac48069 | 1004 | void usage(char **argv) |
2d7360ca | 1005 | { |
533bacb3 | 1006 | #define USAGE_OPTIONS_FORMAT " %-39s%s\n" |
5eaef520 | 1007 | fprintf(stderr, "Usage: %s listname [options]\n", argv[0]); |
1008 | fprintf(stderr, "Options are\n"); | |
533bacb3 | 1009 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose", |
5eaef520 | 1010 | "-C | -create"); |
533bacb3 | 1011 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-m | -members", |
5eaef520 | 1012 | "-R | -rename newname"); |
533bacb3 | 1013 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-u | -users", |
5eaef520 | 1014 | "-P | -public"); |
533bacb3 | 1015 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-l | -lists", |
5eaef520 | 1016 | "-NP | -private"); |
533bacb3 | 1017 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-s | -strings", |
5eaef520 | 1018 | "-A | -active"); |
533bacb3 | 1019 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-k | -kerberos", |
5eaef520 | 1020 | "-I | -inactive"); |
533bacb3 | 1021 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info", |
5eaef520 | 1022 | "-V | -visible"); |
533bacb3 | 1023 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-r | -recursive", |
5eaef520 | 1024 | "-H | -hidden"); |
533bacb3 | 1025 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -add member", |
5eaef520 | 1026 | "-M | -mail"); |
533bacb3 | 1027 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -delete member", |
5eaef520 | 1028 | "-NM | -notmail"); |
533bacb3 | 1029 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-al | -addlist filename", |
5eaef520 | 1030 | "-G | -group"); |
533bacb3 | 1031 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dl | -deletelist filename", |
5eaef520 | 1032 | "-NG | -notgroup"); |
533bacb3 | 1033 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-f | -file filename", |
c55f98d2 | 1034 | "-N | -nfs"); |
533bacb3 | 1035 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-at | -addtagged member tag", |
c55f98d2 | 1036 | "-NN | -notnfs"); |
533bacb3 | 1037 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ct | -changetag member tag", |
c55f98d2 | 1038 | "-D | -desc description"); |
1039 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-t | -tags", | |
1040 | "-O | -owner owner"); | |
533bacb3 | 1041 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-n | -noauth", |
c9bbc3e4 | 1042 | "-MA | -memacl membership_acl"); |
1043 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-db | -database host[:port]", | |
1044 | ""); | |
5eaef520 | 1045 | exit(1); |
2d7360ca | 1046 | } |
1047 | ||
1048 | ||
b5b167d6 | 1049 | /* Display the members stored in the queue */ |
1b6b0a57 | 1050 | |
7ac48069 | 1051 | void show_list_member(struct member *memberstruct) |
b5b167d6 | 1052 | { |
5eaef520 | 1053 | char *s = ""; |
8b494222 | 1054 | |
5eaef520 | 1055 | switch (memberstruct->type) |
1056 | { | |
8b494222 | 1057 | case M_USER: |
5eaef520 | 1058 | if (!showusers) |
1059 | return; | |
1060 | s = "USER"; | |
1061 | break; | |
8b494222 | 1062 | case M_LIST: |
5eaef520 | 1063 | if (!showlists) |
1064 | return; | |
1065 | s = "LIST"; | |
1066 | break; | |
8b494222 | 1067 | case M_STRING: |
5eaef520 | 1068 | if (!showstrings) |
1069 | return; | |
1070 | s = "STRING"; | |
1071 | break; | |
8c3b414d | 1072 | case M_KERBEROS: |
5eaef520 | 1073 | if (!showkerberos) |
8b494222 | 1074 | return; |
5eaef520 | 1075 | s = "KERBEROS"; |
1076 | break; | |
5f7b0741 | 1077 | case M_MACHINE: |
1078 | if (!showmachines) | |
1079 | return; | |
1080 | s = "MACHINE"; | |
1081 | break; | |
5eaef520 | 1082 | case M_ANY: |
1083 | printf("%s\n", memberstruct->name); | |
1084 | return; | |
8b494222 | 1085 | } |
1086 | ||
5eaef520 | 1087 | if (verbose) |
2a942305 | 1088 | printf("%s:%s", s, memberstruct->name); |
5eaef520 | 1089 | else |
1090 | { | |
1091 | if (memberstruct->type == M_LIST) | |
2a942305 | 1092 | printf("LIST:%s", memberstruct->name); |
5eaef520 | 1093 | else if (memberstruct->type == M_KERBEROS) |
2a942305 | 1094 | printf("KERBEROS:%s", memberstruct->name); |
5eaef520 | 1095 | else if (memberstruct->type == M_STRING && |
1096 | !strchr(memberstruct->name, '@')) | |
2a942305 | 1097 | printf("STRING:%s", memberstruct->name); |
5f7b0741 | 1098 | else if (memberstruct->type == M_MACHINE) |
1099 | printf("MACHINE:%s", memberstruct->name); | |
5eaef520 | 1100 | else |
2a942305 | 1101 | printf("%s", memberstruct->name); |
1b6b0a57 | 1102 | } |
2a942305 | 1103 | if (showtags && *(memberstruct->tag)) |
1104 | printf(" (%s)\n", memberstruct->tag); | |
1105 | else | |
1106 | printf("\n"); | |
1b6b0a57 | 1107 | } |
1108 | ||
b5b167d6 | 1109 | |
1110 | /* Show the retrieved information about a list */ | |
1111 | ||
7ac48069 | 1112 | int show_list_info(int argc, char **argv, void *hint) |
2d7360ca | 1113 | { |
c55f98d2 | 1114 | printf("List: %s\n", argv[L_NAME]); |
1115 | printf("Description: %s\n", argv[L_DESC]); | |
5eaef520 | 1116 | printf("Flags: %s, %s, and %s\n", |
c55f98d2 | 1117 | atoi(argv[L_ACTIVE]) ? "active" : "inactive", |
1118 | atoi(argv[L_PUBLIC]) ? "public" : "private", | |
1119 | atoi(argv[L_HIDDEN]) ? "hidden" : "visible"); | |
1120 | printf("%s is %sa maillist and is %sa group", argv[L_NAME], | |
1121 | atoi(argv[L_MAILLIST]) ? "" : "not ", | |
1122 | atoi(argv[L_GROUP]) ? "" : "not "); | |
1123 | if (atoi(argv[L_GROUP])) | |
1124 | { | |
1125 | if (atoi(argv[L_NFSGROUP])) | |
1126 | printf(" (and an NFS group)"); | |
1127 | printf(" with GID %d\n", atoi(argv[L_GID])); | |
1128 | } | |
5eaef520 | 1129 | else |
1130 | printf("\n"); | |
c55f98d2 | 1131 | printf("Owner: %s %s\n", argv[L_ACE_TYPE], argv[L_ACE_NAME]); |
c9bbc3e4 | 1132 | if (strcmp(argv[L_MEMACE_TYPE], "NONE")) |
1133 | printf("Membership ACL: %s %s\n", argv[L_MEMACE_TYPE], | |
1134 | argv[L_MEMACE_NAME]); | |
c55f98d2 | 1135 | printf("Last modified by %s with %s on %s\n", |
1136 | argv[L_MODBY], argv[L_MODWITH], argv[L_MODTIME]); | |
5eaef520 | 1137 | return MR_CONT; |
2d7360ca | 1138 | } |
1139 | ||
1140 | ||
763465b8 | 1141 | /* Copy retrieved information about a list into a new argv */ |
1142 | ||
7ac48069 | 1143 | int save_list_info(int argc, char **argv, void *hint) |
763465b8 | 1144 | { |
7ac48069 | 1145 | char **nargv = hint; |
5eaef520 | 1146 | |
d3b945ae | 1147 | for (argc = 0; argc < 14; argc++) |
5eaef520 | 1148 | nargv[argc + 1] = strdup(argv[argc]); |
1149 | return MR_CONT; | |
763465b8 | 1150 | } |
1151 | ||
b5b167d6 | 1152 | /* Show the retrieve list member count */ |
1153 | ||
7ac48069 | 1154 | int show_list_count(int argc, char **argv, void *hint) |
2d7360ca | 1155 | { |
5eaef520 | 1156 | printf("Members: %s\n", argv[0]); |
7ac48069 | 1157 | return MR_CONT; |
2d7360ca | 1158 | } |
1159 | ||
1160 | ||
b5b167d6 | 1161 | /* Recursively find all of the members of listname, and then display them */ |
1162 | ||
7ac48069 | 1163 | void recursive_display_list_members(void) |
1b6b0a57 | 1164 | { |
5eaef520 | 1165 | int status, count, savecount; |
1166 | struct save_queue *lists, *members; | |
1167 | struct member *m, *m1, *data; | |
1168 | ||
1169 | lists = sq_create(); | |
1170 | members = sq_create(); | |
1171 | m = malloc(sizeof(struct member)); | |
1172 | m->type = M_LIST; | |
1173 | m->name = listname; | |
1174 | sq_save_data(lists, m); | |
1175 | ||
1176 | while (sq_get_data(lists, &m)) | |
1177 | { | |
1178 | sq_destroy(memberlist); | |
1179 | memberlist = sq_create(); | |
1180 | status = mr_query("get_members_of_list", 1, &(m->name), | |
7ac48069 | 1181 | get_list_members, memberlist); |
5eaef520 | 1182 | if (status) |
1183 | com_err(whoami, status, "while getting members of list %s", m->name); | |
1184 | while (sq_get_data(memberlist, &m1)) | |
1185 | { | |
1186 | if (m1->type == M_LIST) | |
1187 | unique_add_member(lists, m1); | |
1188 | else | |
1189 | unique_add_member(members, m1); | |
b5b167d6 | 1190 | } |
1191 | } | |
5eaef520 | 1192 | savecount = count = sq_count_elts(members); |
1193 | data = malloc(count * sizeof(struct member)); | |
1194 | count = 0; | |
1195 | while (sq_get_data(members, &m)) | |
1196 | memcpy(&data[count++], m, sizeof(struct member)); | |
1197 | qsort(data, count, sizeof(struct member), membercmp); | |
1198 | for (count = 0; count < savecount; count++) | |
1199 | show_list_member(&data[count]); | |
b5b167d6 | 1200 | } |
1201 | ||
1202 | ||
1203 | /* add a struct member to a queue if that member isn't already there. */ | |
1204 | ||
7ac48069 | 1205 | void unique_add_member(struct save_queue *q, struct member *m) |
b5b167d6 | 1206 | { |
5eaef520 | 1207 | struct save_queue *qp; |
1b6b0a57 | 1208 | |
5eaef520 | 1209 | for (qp = q->q_next; qp != q; qp = qp->q_next) |
1210 | { | |
1211 | if (!membercmp(qp->q_data, m)) | |
1212 | return; | |
b5b167d6 | 1213 | } |
5eaef520 | 1214 | sq_save_data(q, m); |
1b6b0a57 | 1215 | } |
1216 | ||
b5b167d6 | 1217 | |
1218 | /* Collect the retrieved members of the list */ | |
1219 | ||
7ac48069 | 1220 | int get_list_members(int argc, char **argv, void *sq) |
2d7360ca | 1221 | { |
7ac48069 | 1222 | struct save_queue *q = sq; |
5eaef520 | 1223 | struct member *m; |
2d7360ca | 1224 | |
5eaef520 | 1225 | m = malloc(sizeof(struct member)); |
1226 | switch (argv[0][0]) | |
1227 | { | |
2d7360ca | 1228 | case 'U': |
5eaef520 | 1229 | m->type = M_USER; |
1230 | break; | |
2d7360ca | 1231 | case 'L': |
5eaef520 | 1232 | m->type = M_LIST; |
1233 | break; | |
2d7360ca | 1234 | case 'S': |
5eaef520 | 1235 | m->type = M_STRING; |
1236 | break; | |
8c3b414d | 1237 | case 'K': |
5eaef520 | 1238 | m->type = M_KERBEROS; |
1239 | break; | |
5f7b0741 | 1240 | case 'M': |
1241 | m->type = M_MACHINE; | |
1242 | break; | |
2d7360ca | 1243 | } |
7ac48069 | 1244 | m->name = strdup(argv[1]); |
2a942305 | 1245 | if (argc == 3) |
1246 | m->tag = strdup(argv[2]); | |
1247 | else | |
1248 | m->tag = strdup(""); | |
5eaef520 | 1249 | sq_save_data(q, m); |
1250 | return MR_CONT; | |
2d7360ca | 1251 | } |
1252 | ||
1253 | ||
7c02cbdb | 1254 | /* Open file, parse members from file, and put them on the specified queue */ |
7ac48069 | 1255 | void get_members_from_file(char *filename, struct save_queue *queue) |
7c02cbdb | 1256 | { |
5eaef520 | 1257 | FILE *in; |
1258 | char buf[BUFSIZ]; | |
1259 | struct member *memberstruct; | |
1260 | ||
1261 | if (!strcmp(filename, "-")) | |
1262 | in = stdin; | |
1263 | else | |
1264 | { | |
1265 | in = fopen(filename, "r"); | |
1266 | if (!in) | |
1267 | { | |
1268 | com_err(whoami, errno, "while opening %s for input", filename); | |
1269 | exit(2); | |
7c02cbdb | 1270 | } |
1271 | } | |
1272 | ||
5eaef520 | 1273 | while (fgets(buf, BUFSIZ, in)) |
1274 | { | |
1275 | if ((memberstruct = parse_member(buf))) | |
7c02cbdb | 1276 | sq_save_data(queue, memberstruct); |
5eaef520 | 1277 | } |
1278 | if (!feof(in)) | |
1279 | { | |
7c02cbdb | 1280 | com_err(whoami, errno, "while reading from %s", filename); |
e681e918 | 1281 | exit(2); |
1282 | } | |
7c02cbdb | 1283 | } |
1284 | ||
1285 | ||
99bdafb8 | 1286 | /* Collect the possible expansions of the alias MAILHUB */ |
1287 | ||
7ac48069 | 1288 | int collect(int argc, char **argv, void *l) |
99bdafb8 | 1289 | { |
7ac48069 | 1290 | char ***list = l; |
5eaef520 | 1291 | int i; |
1292 | ||
1293 | for (i = 0; (*list)[i]; i++) | |
1294 | ; | |
1295 | *list = realloc(*list, (i + 2) * sizeof(char *)); | |
7ac48069 | 1296 | (*list)[i] = strdup(argv[2]); |
5eaef520 | 1297 | (*list)[i + 1] = NULL; |
1298 | return MR_CONT; | |
99bdafb8 | 1299 | } |
1300 | ||
1301 | ||
b5b167d6 | 1302 | /* Parse a line of input, fetching a member. NULL is returned if a member |
7c02cbdb | 1303 | * is not found. ';' is a comment character. |
b5b167d6 | 1304 | */ |
1305 | ||
44d12d58 | 1306 | struct member *parse_member(char *s) |
2d7360ca | 1307 | { |
44d12d58 | 1308 | struct member *m; |
5eaef520 | 1309 | char *p, *lastchar; |
1310 | ||
1311 | while (*s && isspace(*s)) | |
1312 | s++; | |
1313 | lastchar = p = s; | |
1314 | while (*p && *p != '\n' && *p != ';') | |
1315 | { | |
7c02cbdb | 1316 | if (isprint(*p) && !isspace(*p)) |
1317 | lastchar = p++; | |
1318 | else | |
1319 | p++; | |
5eaef520 | 1320 | } |
1321 | lastchar++; | |
1322 | *lastchar = '\0'; | |
1323 | if (p == s || strlen(s) == 0) | |
1324 | return NULL; | |
1325 | ||
1326 | if (!(m = malloc(sizeof(struct member)))) | |
1327 | return NULL; | |
2a942305 | 1328 | m->tag = strdup(""); |
5eaef520 | 1329 | |
1330 | if ((p = strchr(s, ':'))) | |
1331 | { | |
1332 | *p = '\0'; | |
1333 | m->name = ++p; | |
1334 | if (!strcasecmp("user", s)) | |
1335 | m->type = M_USER; | |
1336 | else if (!strcasecmp("list", s)) | |
1337 | m->type = M_LIST; | |
1338 | else if (!strcasecmp("string", s)) | |
1339 | m->type = M_STRING; | |
1340 | else if (!strcasecmp("kerberos", s)) | |
1341 | m->type = M_KERBEROS; | |
5f7b0741 | 1342 | else if (!strcasecmp("machine", s)) |
1343 | m->type = M_MACHINE; | |
a9c047f0 | 1344 | else if (!strcasecmp("none", s)) |
1345 | m->type = M_NONE; | |
5eaef520 | 1346 | else |
1347 | { | |
1348 | m->type = M_ANY; | |
1349 | *(--p) = ':'; | |
1350 | m->name = s; | |
2d7360ca | 1351 | } |
7ac48069 | 1352 | m->name = strdup(m->name); |
5eaef520 | 1353 | } |
1354 | else | |
1355 | { | |
7ac48069 | 1356 | m->name = strdup(s); |
a9c047f0 | 1357 | m->type = strcasecmp(s, "none") ? M_ANY : M_NONE; |
2d7360ca | 1358 | } |
5eaef520 | 1359 | return m; |
2d7360ca | 1360 | } |
1361 | ||
1362 | ||
5eaef520 | 1363 | /* |
1364 | * This routine two compares members by the following rules: | |
1365 | * 1. A USER is less than a LIST | |
1366 | * 2. A LIST is less than a STRING | |
1367 | * 3. If two members are of the same type, the one alphabetically first | |
1368 | * is less than the other | |
1369 | * It returs < 0 if the first member is less, 0 if they are identical, and | |
1370 | * > 0 if the second member is less (the first member is greater). | |
1371 | */ | |
b5b167d6 | 1372 | |
7ac48069 | 1373 | int membercmp(const void *mem1, const void *mem2) |
2d7360ca | 1374 | { |
7ac48069 | 1375 | const struct member *m1 = mem1, *m2 = mem2; |
1376 | ||
5eaef520 | 1377 | if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type)) |
1378 | return strcmp(m1->name, m2->name); | |
1379 | else | |
1380 | return m1->type - m2->type; | |
2d7360ca | 1381 | } |
b5b167d6 | 1382 | |
1383 | ||
5eaef520 | 1384 | int sq_count_elts(struct save_queue *q) |
b5b167d6 | 1385 | { |
5eaef520 | 1386 | char *foo; |
1387 | int count; | |
b5b167d6 | 1388 | |
5eaef520 | 1389 | count = 0; |
1390 | while (sq_get_data(q, &foo)) | |
1391 | count++; | |
1392 | return count; | |
b5b167d6 | 1393 | } |
e8714e3d | 1394 | |
1395 | char *get_username(void) | |
1396 | { | |
1397 | char *username; | |
1398 | ||
1399 | username = getenv("USER"); | |
1400 | if (!username) | |
1401 | { | |
1402 | username = mrcl_krb_user(); | |
1403 | if (!username) | |
1404 | { | |
1405 | com_err(whoami, 0, "Could not determine username"); | |
1406 | exit(1); | |
1407 | } | |
1408 | } | |
1409 | return username; | |
1410 | } |