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