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