]>
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); |
8e0b3fc5 | 663 | mrcl_com_err(whoami); |
375e818a | 664 | if (status == MRCL_REJECT) |
665 | { | |
375e818a | 666 | success = 0; |
667 | break; | |
668 | } | |
669 | else if (memberstruct->type == M_ANY && status != MR_SUCCESS) | |
5eaef520 | 670 | { |
671 | /* if user is trying to add something which isn't a | |
672 | remote string, or a list, or a user, and didn't | |
673 | explicitly specify `STRING:', it's probably a typo */ | |
674 | com_err(whoami, MR_NO_MATCH, "while adding member %s to %s", | |
675 | memberstruct->name, listname); | |
676 | success = 0; | |
677 | break; | |
25978f25 | 678 | } |
5eaef520 | 679 | |
680 | membervec[1] = "STRING"; | |
2a942305 | 681 | status = mr_query("add_tagged_member_to_list", 4, membervec, |
7ac48069 | 682 | NULL, NULL); |
5eaef520 | 683 | if (status != MR_SUCCESS) |
684 | { | |
d44cee72 | 685 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 686 | memberstruct->name, listname); |
e681e918 | 687 | success = 0; |
688 | } | |
5eaef520 | 689 | break; |
8c3b414d | 690 | case M_KERBEROS: |
5eaef520 | 691 | membervec[1] = "KERBEROS"; |
375e818a | 692 | status = mrcl_validate_kerberos_member(membervec[2], &membervec[2]); |
693 | if (mrcl_get_message()) | |
694 | mrcl_com_err(whoami); | |
2a942305 | 695 | status = mr_query("add_tagged_member_to_list", 4, membervec, |
7ac48069 | 696 | NULL, NULL); |
5eaef520 | 697 | if (status != MR_SUCCESS) |
698 | { | |
d44cee72 | 699 | com_err(whoami, status, "while adding member %s to %s", |
8c3b414d | 700 | memberstruct->name, listname); |
e681e918 | 701 | success = 0; |
702 | } | |
375e818a | 703 | free(membervec[2]); |
2d7360ca | 704 | } |
705 | } | |
706 | ||
5eaef520 | 707 | /* Process the delete list */ |
708 | while (sq_get_data(dellist, &memberstruct)) | |
709 | { | |
710 | membervec[0] = listname; | |
711 | membervec[2] = memberstruct->name; | |
712 | if (verbose) | |
713 | { | |
714 | printf("Deleting member "); | |
715 | show_list_member(memberstruct); | |
b5b167d6 | 716 | } |
5eaef520 | 717 | switch (memberstruct->type) |
718 | { | |
2d7360ca | 719 | case M_ANY: |
720 | case M_USER: | |
5eaef520 | 721 | membervec[1] = "USER"; |
722 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 723 | NULL, NULL); |
5eaef520 | 724 | if (status == MR_SUCCESS) |
725 | break; | |
726 | else if ((status != MR_USER && status != MR_NO_MATCH) || | |
727 | memberstruct->type != M_ANY) | |
728 | { | |
729 | com_err(whoami, status, "while deleting member %s from %s", | |
730 | memberstruct->name, listname); | |
731 | success = 0; | |
2d7360ca | 732 | break; |
2d7360ca | 733 | } |
734 | case M_LIST: | |
5eaef520 | 735 | membervec[1] = "LIST"; |
736 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 737 | NULL, NULL); |
5eaef520 | 738 | if (status == MR_SUCCESS) |
739 | break; | |
740 | else if ((status != MR_LIST && status != MR_NO_MATCH) || | |
741 | memberstruct->type != M_ANY) | |
742 | { | |
743 | if (status == MR_PERM && memberstruct->type == M_ANY && | |
e8714e3d | 744 | !strcmp(membervec[2], get_username())) |
5eaef520 | 745 | { |
746 | /* M_ANY means we've fallen through from the user | |
747 | * case. The user is trying to remove himself from | |
748 | * a list, but we got MR_USER or MR_NO_MATCH above, | |
749 | * meaning he's not really on it, and we got MR_PERM | |
750 | * when trying to remove LIST:$USER because he's not | |
751 | * on the acl. That error is useless, so return | |
752 | * MR_NO_MATCH instead. However, this will generate the | |
753 | * wrong error if the user was trying to remove the list | |
754 | * with his username from a list he doesn't administrate | |
755 | * without explicitly specifying "list:". | |
756 | */ | |
40637dba | 757 | status = MR_NO_MATCH; |
758 | } | |
5eaef520 | 759 | com_err(whoami, status, "while deleting member %s from %s", |
760 | memberstruct->name, listname); | |
761 | success = 0; | |
762 | break; | |
2d7360ca | 763 | } |
764 | case M_STRING: | |
5eaef520 | 765 | membervec[1] = "STRING"; |
766 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 767 | NULL, NULL); |
5eaef520 | 768 | if (status == MR_STRING && memberstruct->type == M_ANY) |
769 | { | |
d44cee72 | 770 | com_err(whoami, 0, " Unable to find member %s to delete from %s", |
2d7360ca | 771 | memberstruct->name, listname); |
e681e918 | 772 | success = 0; |
e8714e3d | 773 | if (!strcmp(membervec[0], get_username())) |
5eaef520 | 774 | { |
775 | fprintf(stderr, "(If you were trying to remove yourself " | |
776 | "from the list \"%s\",\n", membervec[2]); | |
777 | fprintf(stderr, "the correct command is \"blanche %s -d " | |
778 | "%s\".)\n", membervec[2], membervec[0]); | |
779 | } | |
780 | } | |
781 | else if (status != MR_SUCCESS) | |
782 | { | |
d44cee72 | 783 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 784 | memberstruct->name, listname); |
e681e918 | 785 | success = 0; |
786 | } | |
5eaef520 | 787 | break; |
8c3b414d | 788 | case M_KERBEROS: |
5eaef520 | 789 | membervec[1] = "KERBEROS"; |
790 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 791 | NULL, NULL); |
375e818a | 792 | if (status == MR_STRING || status == MR_NO_MATCH) |
793 | { | |
794 | /* Try canonicalizing the Kerberos principal and trying | |
795 | * again. If we succeed, print the message from mrcl. | |
796 | * Otherwise, just pretend we never did this and print | |
797 | * the original error message. | |
798 | */ | |
799 | mrcl_validate_kerberos_member(membervec[2], &membervec[2]); | |
800 | if (mrcl_get_message()) | |
801 | { | |
802 | if (mr_query("delete_member_from_list", 3, membervec, | |
803 | NULL, NULL) == MR_SUCCESS) | |
804 | mrcl_com_err(whoami); | |
805 | status = MR_SUCCESS; | |
806 | } | |
807 | } | |
5eaef520 | 808 | if (status != MR_SUCCESS) |
809 | { | |
d44cee72 | 810 | com_err(whoami, status, "while deleting member %s from %s", |
8c3b414d | 811 | memberstruct->name, listname); |
e681e918 | 812 | success = 0; |
813 | } | |
2d7360ca | 814 | } |
815 | } | |
816 | ||
2a942305 | 817 | /* Process the tag list */ |
818 | while (sq_get_data(taglist, &memberstruct)) | |
819 | { | |
820 | membervec[0] = listname; | |
821 | membervec[2] = memberstruct->name; | |
822 | membervec[3] = memberstruct->tag; | |
823 | if (verbose) | |
824 | { | |
825 | printf("Tagging member "); | |
826 | show_list_member(memberstruct); | |
827 | } | |
828 | switch (memberstruct->type) | |
829 | { | |
830 | case M_ANY: | |
831 | case M_USER: | |
832 | membervec[1] = "USER"; | |
833 | status = mr_query("tag_member_of_list", 4, membervec, | |
834 | NULL, NULL); | |
835 | if (status == MR_SUCCESS) | |
836 | break; | |
837 | else if ((status != MR_USER && status != MR_NO_MATCH) || | |
838 | memberstruct->type != M_ANY) | |
839 | { | |
840 | com_err(whoami, status, "while changing tag on member %s of %s", | |
841 | memberstruct->name, listname); | |
842 | success = 0; | |
843 | break; | |
844 | } | |
845 | case M_LIST: | |
846 | membervec[1] = "LIST"; | |
847 | status = mr_query("tag_member_of_list", 4, membervec, | |
848 | NULL, NULL); | |
849 | if (status == MR_SUCCESS) | |
850 | break; | |
851 | else if ((status != MR_LIST && status != MR_NO_MATCH) || | |
852 | memberstruct->type != M_ANY) | |
853 | { | |
854 | com_err(whoami, status, "while changing tag on member %s of %s", | |
855 | memberstruct->name, listname); | |
856 | success = 0; | |
857 | break; | |
858 | } | |
859 | case M_STRING: | |
860 | membervec[1] = "STRING"; | |
861 | status = mr_query("tag_member_of_list", 4, membervec, | |
862 | NULL, NULL); | |
863 | if (status == MR_STRING && memberstruct->type == M_ANY) | |
864 | { | |
865 | com_err(whoami, 0, " Unable to find member %s on list %s", | |
866 | memberstruct->name, listname); | |
867 | success = 0; | |
868 | } | |
869 | else if (status != MR_SUCCESS) | |
870 | { | |
871 | com_err(whoami, status, "while retagging member %s on %s", | |
872 | memberstruct->name, listname); | |
873 | success = 0; | |
874 | } | |
875 | break; | |
876 | case M_KERBEROS: | |
877 | membervec[1] = "KERBEROS"; | |
878 | status = mr_query("tag_member_of_list", 4, membervec, | |
879 | NULL, NULL); | |
375e818a | 880 | if (status == MR_STRING || status == MR_NO_MATCH) |
881 | { | |
882 | /* Try canonicalizing the Kerberos principal and trying | |
883 | * again. If we succeed, print the message from mrcl. | |
884 | * Otherwise, just pretend we never did this and print | |
885 | * the original error message. | |
886 | */ | |
887 | mrcl_validate_kerberos_member(membervec[2], &membervec[2]); | |
888 | if (mrcl_get_message()) | |
889 | { | |
890 | if (mr_query("tag_member_of_list", 4, membervec, | |
891 | NULL, NULL) == MR_SUCCESS) | |
892 | mrcl_com_err(whoami); | |
893 | status = MR_SUCCESS; | |
894 | } | |
895 | } | |
2a942305 | 896 | if (status != MR_SUCCESS) |
897 | { | |
898 | com_err(whoami, status, "while changing tag on member %s of %s", | |
899 | memberstruct->name, listname); | |
900 | success = 0; | |
901 | } | |
902 | } | |
903 | } | |
904 | ||
5eaef520 | 905 | /* Display the members of the list now, if requested */ |
906 | if (memberflg) | |
907 | { | |
908 | if (recursflg) | |
909 | recursive_display_list_members(); | |
910 | else | |
911 | { | |
2a942305 | 912 | status = mr_query(showtags ? "get_tagged_members_of_list" : |
913 | "get_members_of_list", 1, &listname, | |
7ac48069 | 914 | get_list_members, memberlist); |
5eaef520 | 915 | if (status) |
916 | com_err(whoami, status, "while getting members of list %s", | |
917 | listname); | |
918 | while (sq_get_data(memberlist, &memberstruct)) | |
919 | show_list_member(memberstruct); | |
b5b167d6 | 920 | } |
921 | } | |
2d7360ca | 922 | |
5eaef520 | 923 | /* We're done! */ |
924 | mr_disconnect(); | |
925 | exit(success ? 0 : 1); | |
2d7360ca | 926 | } |
927 | ||
7ac48069 | 928 | void usage(char **argv) |
2d7360ca | 929 | { |
533bacb3 | 930 | #define USAGE_OPTIONS_FORMAT " %-39s%s\n" |
5eaef520 | 931 | fprintf(stderr, "Usage: %s listname [options]\n", argv[0]); |
932 | fprintf(stderr, "Options are\n"); | |
533bacb3 | 933 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose", |
5eaef520 | 934 | "-C | -create"); |
533bacb3 | 935 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-m | -members", |
5eaef520 | 936 | "-R | -rename newname"); |
533bacb3 | 937 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-u | -users", |
5eaef520 | 938 | "-P | -public"); |
533bacb3 | 939 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-l | -lists", |
5eaef520 | 940 | "-NP | -private"); |
533bacb3 | 941 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-s | -strings", |
5eaef520 | 942 | "-A | -active"); |
533bacb3 | 943 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-k | -kerberos", |
5eaef520 | 944 | "-I | -inactive"); |
533bacb3 | 945 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info", |
5eaef520 | 946 | "-V | -visible"); |
533bacb3 | 947 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-r | -recursive", |
5eaef520 | 948 | "-H | -hidden"); |
533bacb3 | 949 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -add member", |
5eaef520 | 950 | "-M | -mail"); |
533bacb3 | 951 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -delete member", |
5eaef520 | 952 | "-NM | -notmail"); |
533bacb3 | 953 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-al | -addlist filename", |
5eaef520 | 954 | "-G | -group"); |
533bacb3 | 955 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dl | -deletelist filename", |
5eaef520 | 956 | "-NG | -notgroup"); |
533bacb3 | 957 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-f | -file filename", |
c55f98d2 | 958 | "-N | -nfs"); |
533bacb3 | 959 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-at | -addtagged member tag", |
c55f98d2 | 960 | "-NN | -notnfs"); |
533bacb3 | 961 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ct | -changetag member tag", |
c55f98d2 | 962 | "-D | -desc description"); |
963 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-t | -tags", | |
964 | "-O | -owner owner"); | |
533bacb3 | 965 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-n | -noauth", |
c9bbc3e4 | 966 | "-MA | -memacl membership_acl"); |
967 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-db | -database host[:port]", | |
968 | ""); | |
5eaef520 | 969 | exit(1); |
2d7360ca | 970 | } |
971 | ||
972 | ||
b5b167d6 | 973 | /* Display the members stored in the queue */ |
1b6b0a57 | 974 | |
7ac48069 | 975 | void show_list_member(struct member *memberstruct) |
b5b167d6 | 976 | { |
5eaef520 | 977 | char *s = ""; |
8b494222 | 978 | |
5eaef520 | 979 | switch (memberstruct->type) |
980 | { | |
8b494222 | 981 | case M_USER: |
5eaef520 | 982 | if (!showusers) |
983 | return; | |
984 | s = "USER"; | |
985 | break; | |
8b494222 | 986 | case M_LIST: |
5eaef520 | 987 | if (!showlists) |
988 | return; | |
989 | s = "LIST"; | |
990 | break; | |
8b494222 | 991 | case M_STRING: |
5eaef520 | 992 | if (!showstrings) |
993 | return; | |
994 | s = "STRING"; | |
995 | break; | |
8c3b414d | 996 | case M_KERBEROS: |
5eaef520 | 997 | if (!showkerberos) |
8b494222 | 998 | return; |
5eaef520 | 999 | s = "KERBEROS"; |
1000 | break; | |
1001 | case M_ANY: | |
1002 | printf("%s\n", memberstruct->name); | |
1003 | return; | |
8b494222 | 1004 | } |
1005 | ||
5eaef520 | 1006 | if (verbose) |
2a942305 | 1007 | printf("%s:%s", s, memberstruct->name); |
5eaef520 | 1008 | else |
1009 | { | |
1010 | if (memberstruct->type == M_LIST) | |
2a942305 | 1011 | printf("LIST:%s", memberstruct->name); |
5eaef520 | 1012 | else if (memberstruct->type == M_KERBEROS) |
2a942305 | 1013 | printf("KERBEROS:%s", memberstruct->name); |
5eaef520 | 1014 | else if (memberstruct->type == M_STRING && |
1015 | !strchr(memberstruct->name, '@')) | |
2a942305 | 1016 | printf("STRING:%s", memberstruct->name); |
5eaef520 | 1017 | else |
2a942305 | 1018 | printf("%s", memberstruct->name); |
1b6b0a57 | 1019 | } |
2a942305 | 1020 | if (showtags && *(memberstruct->tag)) |
1021 | printf(" (%s)\n", memberstruct->tag); | |
1022 | else | |
1023 | printf("\n"); | |
1b6b0a57 | 1024 | } |
1025 | ||
b5b167d6 | 1026 | |
1027 | /* Show the retrieved information about a list */ | |
1028 | ||
7ac48069 | 1029 | int show_list_info(int argc, char **argv, void *hint) |
2d7360ca | 1030 | { |
c55f98d2 | 1031 | printf("List: %s\n", argv[L_NAME]); |
1032 | printf("Description: %s\n", argv[L_DESC]); | |
5eaef520 | 1033 | printf("Flags: %s, %s, and %s\n", |
c55f98d2 | 1034 | atoi(argv[L_ACTIVE]) ? "active" : "inactive", |
1035 | atoi(argv[L_PUBLIC]) ? "public" : "private", | |
1036 | atoi(argv[L_HIDDEN]) ? "hidden" : "visible"); | |
1037 | printf("%s is %sa maillist and is %sa group", argv[L_NAME], | |
1038 | atoi(argv[L_MAILLIST]) ? "" : "not ", | |
1039 | atoi(argv[L_GROUP]) ? "" : "not "); | |
1040 | if (atoi(argv[L_GROUP])) | |
1041 | { | |
1042 | if (atoi(argv[L_NFSGROUP])) | |
1043 | printf(" (and an NFS group)"); | |
1044 | printf(" with GID %d\n", atoi(argv[L_GID])); | |
1045 | } | |
5eaef520 | 1046 | else |
1047 | printf("\n"); | |
c55f98d2 | 1048 | printf("Owner: %s %s\n", argv[L_ACE_TYPE], argv[L_ACE_NAME]); |
c9bbc3e4 | 1049 | if (strcmp(argv[L_MEMACE_TYPE], "NONE")) |
1050 | printf("Membership ACL: %s %s\n", argv[L_MEMACE_TYPE], | |
1051 | argv[L_MEMACE_NAME]); | |
c55f98d2 | 1052 | printf("Last modified by %s with %s on %s\n", |
1053 | argv[L_MODBY], argv[L_MODWITH], argv[L_MODTIME]); | |
5eaef520 | 1054 | return MR_CONT; |
2d7360ca | 1055 | } |
1056 | ||
1057 | ||
763465b8 | 1058 | /* Copy retrieved information about a list into a new argv */ |
1059 | ||
7ac48069 | 1060 | int save_list_info(int argc, char **argv, void *hint) |
763465b8 | 1061 | { |
7ac48069 | 1062 | char **nargv = hint; |
5eaef520 | 1063 | |
d3b945ae | 1064 | for (argc = 0; argc < 14; argc++) |
5eaef520 | 1065 | nargv[argc + 1] = strdup(argv[argc]); |
1066 | return MR_CONT; | |
763465b8 | 1067 | } |
1068 | ||
b5b167d6 | 1069 | /* Show the retrieve list member count */ |
1070 | ||
7ac48069 | 1071 | int show_list_count(int argc, char **argv, void *hint) |
2d7360ca | 1072 | { |
5eaef520 | 1073 | printf("Members: %s\n", argv[0]); |
7ac48069 | 1074 | return MR_CONT; |
2d7360ca | 1075 | } |
1076 | ||
1077 | ||
b5b167d6 | 1078 | /* Recursively find all of the members of listname, and then display them */ |
1079 | ||
7ac48069 | 1080 | void recursive_display_list_members(void) |
1b6b0a57 | 1081 | { |
5eaef520 | 1082 | int status, count, savecount; |
1083 | struct save_queue *lists, *members; | |
1084 | struct member *m, *m1, *data; | |
1085 | ||
1086 | lists = sq_create(); | |
1087 | members = sq_create(); | |
1088 | m = malloc(sizeof(struct member)); | |
1089 | m->type = M_LIST; | |
1090 | m->name = listname; | |
1091 | sq_save_data(lists, m); | |
1092 | ||
1093 | while (sq_get_data(lists, &m)) | |
1094 | { | |
1095 | sq_destroy(memberlist); | |
1096 | memberlist = sq_create(); | |
1097 | status = mr_query("get_members_of_list", 1, &(m->name), | |
7ac48069 | 1098 | get_list_members, memberlist); |
5eaef520 | 1099 | if (status) |
1100 | com_err(whoami, status, "while getting members of list %s", m->name); | |
1101 | while (sq_get_data(memberlist, &m1)) | |
1102 | { | |
1103 | if (m1->type == M_LIST) | |
1104 | unique_add_member(lists, m1); | |
1105 | else | |
1106 | unique_add_member(members, m1); | |
b5b167d6 | 1107 | } |
1108 | } | |
5eaef520 | 1109 | savecount = count = sq_count_elts(members); |
1110 | data = malloc(count * sizeof(struct member)); | |
1111 | count = 0; | |
1112 | while (sq_get_data(members, &m)) | |
1113 | memcpy(&data[count++], m, sizeof(struct member)); | |
1114 | qsort(data, count, sizeof(struct member), membercmp); | |
1115 | for (count = 0; count < savecount; count++) | |
1116 | show_list_member(&data[count]); | |
b5b167d6 | 1117 | } |
1118 | ||
1119 | ||
1120 | /* add a struct member to a queue if that member isn't already there. */ | |
1121 | ||
7ac48069 | 1122 | void unique_add_member(struct save_queue *q, struct member *m) |
b5b167d6 | 1123 | { |
5eaef520 | 1124 | struct save_queue *qp; |
1b6b0a57 | 1125 | |
5eaef520 | 1126 | for (qp = q->q_next; qp != q; qp = qp->q_next) |
1127 | { | |
1128 | if (!membercmp(qp->q_data, m)) | |
1129 | return; | |
b5b167d6 | 1130 | } |
5eaef520 | 1131 | sq_save_data(q, m); |
1b6b0a57 | 1132 | } |
1133 | ||
b5b167d6 | 1134 | |
1135 | /* Collect the retrieved members of the list */ | |
1136 | ||
7ac48069 | 1137 | int get_list_members(int argc, char **argv, void *sq) |
2d7360ca | 1138 | { |
7ac48069 | 1139 | struct save_queue *q = sq; |
5eaef520 | 1140 | struct member *m; |
2d7360ca | 1141 | |
5eaef520 | 1142 | m = malloc(sizeof(struct member)); |
1143 | switch (argv[0][0]) | |
1144 | { | |
2d7360ca | 1145 | case 'U': |
5eaef520 | 1146 | m->type = M_USER; |
1147 | break; | |
2d7360ca | 1148 | case 'L': |
5eaef520 | 1149 | m->type = M_LIST; |
1150 | break; | |
2d7360ca | 1151 | case 'S': |
5eaef520 | 1152 | m->type = M_STRING; |
1153 | break; | |
8c3b414d | 1154 | case 'K': |
5eaef520 | 1155 | m->type = M_KERBEROS; |
1156 | break; | |
2d7360ca | 1157 | } |
7ac48069 | 1158 | m->name = strdup(argv[1]); |
2a942305 | 1159 | if (argc == 3) |
1160 | m->tag = strdup(argv[2]); | |
1161 | else | |
1162 | m->tag = strdup(""); | |
5eaef520 | 1163 | sq_save_data(q, m); |
1164 | return MR_CONT; | |
2d7360ca | 1165 | } |
1166 | ||
1167 | ||
7c02cbdb | 1168 | /* Open file, parse members from file, and put them on the specified queue */ |
7ac48069 | 1169 | void get_members_from_file(char *filename, struct save_queue *queue) |
7c02cbdb | 1170 | { |
5eaef520 | 1171 | FILE *in; |
1172 | char buf[BUFSIZ]; | |
1173 | struct member *memberstruct; | |
1174 | ||
1175 | if (!strcmp(filename, "-")) | |
1176 | in = stdin; | |
1177 | else | |
1178 | { | |
1179 | in = fopen(filename, "r"); | |
1180 | if (!in) | |
1181 | { | |
1182 | com_err(whoami, errno, "while opening %s for input", filename); | |
1183 | exit(2); | |
7c02cbdb | 1184 | } |
1185 | } | |
1186 | ||
5eaef520 | 1187 | while (fgets(buf, BUFSIZ, in)) |
1188 | { | |
1189 | if ((memberstruct = parse_member(buf))) | |
7c02cbdb | 1190 | sq_save_data(queue, memberstruct); |
5eaef520 | 1191 | } |
1192 | if (!feof(in)) | |
1193 | { | |
7c02cbdb | 1194 | com_err(whoami, errno, "while reading from %s", filename); |
e681e918 | 1195 | exit(2); |
1196 | } | |
7c02cbdb | 1197 | } |
1198 | ||
1199 | ||
99bdafb8 | 1200 | /* Collect the possible expansions of the alias MAILHUB */ |
1201 | ||
7ac48069 | 1202 | int collect(int argc, char **argv, void *l) |
99bdafb8 | 1203 | { |
7ac48069 | 1204 | char ***list = l; |
5eaef520 | 1205 | int i; |
1206 | ||
1207 | for (i = 0; (*list)[i]; i++) | |
1208 | ; | |
1209 | *list = realloc(*list, (i + 2) * sizeof(char *)); | |
7ac48069 | 1210 | (*list)[i] = strdup(argv[2]); |
5eaef520 | 1211 | (*list)[i + 1] = NULL; |
1212 | return MR_CONT; | |
99bdafb8 | 1213 | } |
1214 | ||
1215 | ||
b5b167d6 | 1216 | /* Parse a line of input, fetching a member. NULL is returned if a member |
7c02cbdb | 1217 | * is not found. ';' is a comment character. |
b5b167d6 | 1218 | */ |
1219 | ||
44d12d58 | 1220 | struct member *parse_member(char *s) |
2d7360ca | 1221 | { |
44d12d58 | 1222 | struct member *m; |
5eaef520 | 1223 | char *p, *lastchar; |
1224 | ||
1225 | while (*s && isspace(*s)) | |
1226 | s++; | |
1227 | lastchar = p = s; | |
1228 | while (*p && *p != '\n' && *p != ';') | |
1229 | { | |
7c02cbdb | 1230 | if (isprint(*p) && !isspace(*p)) |
1231 | lastchar = p++; | |
1232 | else | |
1233 | p++; | |
5eaef520 | 1234 | } |
1235 | lastchar++; | |
1236 | *lastchar = '\0'; | |
1237 | if (p == s || strlen(s) == 0) | |
1238 | return NULL; | |
1239 | ||
1240 | if (!(m = malloc(sizeof(struct member)))) | |
1241 | return NULL; | |
2a942305 | 1242 | m->tag = strdup(""); |
5eaef520 | 1243 | |
1244 | if ((p = strchr(s, ':'))) | |
1245 | { | |
1246 | *p = '\0'; | |
1247 | m->name = ++p; | |
1248 | if (!strcasecmp("user", s)) | |
1249 | m->type = M_USER; | |
1250 | else if (!strcasecmp("list", s)) | |
1251 | m->type = M_LIST; | |
1252 | else if (!strcasecmp("string", s)) | |
1253 | m->type = M_STRING; | |
1254 | else if (!strcasecmp("kerberos", s)) | |
1255 | m->type = M_KERBEROS; | |
a9c047f0 | 1256 | else if (!strcasecmp("none", s)) |
1257 | m->type = M_NONE; | |
5eaef520 | 1258 | else |
1259 | { | |
1260 | m->type = M_ANY; | |
1261 | *(--p) = ':'; | |
1262 | m->name = s; | |
2d7360ca | 1263 | } |
7ac48069 | 1264 | m->name = strdup(m->name); |
5eaef520 | 1265 | } |
1266 | else | |
1267 | { | |
7ac48069 | 1268 | m->name = strdup(s); |
a9c047f0 | 1269 | m->type = strcasecmp(s, "none") ? M_ANY : M_NONE; |
2d7360ca | 1270 | } |
5eaef520 | 1271 | return m; |
2d7360ca | 1272 | } |
1273 | ||
1274 | ||
5eaef520 | 1275 | /* |
1276 | * This routine two compares members by the following rules: | |
1277 | * 1. A USER is less than a LIST | |
1278 | * 2. A LIST is less than a STRING | |
1279 | * 3. If two members are of the same type, the one alphabetically first | |
1280 | * is less than the other | |
1281 | * It returs < 0 if the first member is less, 0 if they are identical, and | |
1282 | * > 0 if the second member is less (the first member is greater). | |
1283 | */ | |
b5b167d6 | 1284 | |
7ac48069 | 1285 | int membercmp(const void *mem1, const void *mem2) |
2d7360ca | 1286 | { |
7ac48069 | 1287 | const struct member *m1 = mem1, *m2 = mem2; |
1288 | ||
5eaef520 | 1289 | if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type)) |
1290 | return strcmp(m1->name, m2->name); | |
1291 | else | |
1292 | return m1->type - m2->type; | |
2d7360ca | 1293 | } |
b5b167d6 | 1294 | |
1295 | ||
5eaef520 | 1296 | int sq_count_elts(struct save_queue *q) |
b5b167d6 | 1297 | { |
5eaef520 | 1298 | char *foo; |
1299 | int count; | |
b5b167d6 | 1300 | |
5eaef520 | 1301 | count = 0; |
1302 | while (sq_get_data(q, &foo)) | |
1303 | count++; | |
1304 | return count; | |
b5b167d6 | 1305 | } |
e8714e3d | 1306 | |
1307 | char *get_username(void) | |
1308 | { | |
1309 | char *username; | |
1310 | ||
1311 | username = getenv("USER"); | |
1312 | if (!username) | |
1313 | { | |
1314 | username = mrcl_krb_user(); | |
1315 | if (!username) | |
1316 | { | |
1317 | com_err(whoami, 0, "Could not determine username"); | |
1318 | exit(1); | |
1319 | } | |
1320 | } | |
1321 | return username; | |
1322 | } |