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