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