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