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