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