]> git.sur5r.net Git - openldap/blob - clients/ud/group.c
clients build under FreeBSD
[openldap] / clients / ud / group.c
1 /*
2  * Copyright (c) 1993, 1994  Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  */
13
14 #include "portable.h"
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19
20 #include <lber.h>
21 #include <ldap.h>
22
23 #include <ldapconfig.h>
24 #include "ud.h"
25
26 extern LDAPMessage * find();
27
28 #ifdef DEBUG
29 extern int debug;
30 #endif
31
32 extern char *bound_dn, *group_base;
33 extern int verbose, bind_status;
34 extern struct entry Entry;
35 extern LDAP *ld;
36
37 extern void Free();
38
39 void add_group(name)
40 char *name;
41 {
42         register int i, idx = 0, prompt = 0;
43         char tmp[BUFSIZ], dn[BUFSIZ];
44         static LDAPMod *attrs[9];
45         LDAPMod init_rdn,    init_owner,   init_domain,
46                 init_errors, init_request, init_joinable;
47         char *init_rdn_value[2], *init_owner_value[2], *init_domain_value[2],
48                 *init_errors_value[MAX_VALUES], *init_joinable_value[2],
49                 *init_request_value[MAX_VALUES];
50         extern void ldap_flush_cache();
51         extern char * strip_ignore_chars();
52
53 #ifdef DEBUG
54         if (debug & D_TRACE) {
55                 if (name == NULL)
56                         printf("->add_group(NULL)\n");
57                 else
58                         printf("->add_group(%s)\n", name);
59         }
60 #endif
61
62         if (bind_status == UD_NOT_BOUND) {
63                 if (auth((char *) NULL, 1) < 0) {
64                         return;
65                 }
66         }
67
68         /*
69          *  If the user did not supply us with a name, prompt them for
70          *  a name.
71          */
72         if ((name == NULL) || (*name == '\0') || !strcasecmp(name, "group")) {
73                 ++prompt;
74                 printf("  Group to create? ");
75                 fflush(stdout);
76                 fetch_buffer(tmp, sizeof(tmp), stdin);
77                 if (tmp[0] == '\0')
78                         return;
79                 name = strdup(tmp);
80         }
81
82         /* remove quotes, dots, and underscores. */
83         name = strip_ignore_chars(name);
84
85 #ifdef UOFM
86         if (isauniqname(name)) {
87                 printf(" '%s' could be confused with a U-M uniqname.\n", name);
88                 printf(" You can create the group, but you need to make sure that\n");
89                 printf(" you reserve the uniqname for use as your groupname\n\n");
90                 printf(" Are you sure that you want to do this? ");
91                 fflush(stdout);
92                 fetch_buffer(tmp, sizeof(tmp), stdin);
93                 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
94                         return;
95                 printf("\n Be sure to contact your uniqname administrator to reserve\n");
96                 printf(" the uniqname '%s' for use as your group name.\n", name);
97         }
98 #endif
99         sprintf(dn, "cn=%s, %s", name, group_base);
100
101         /*
102          *  Make sure that this group does not already exist.
103          */
104         if (vrfy(dn) == TRUE) {
105                 printf("  The group \"%s\" already exists.\n", name);
106                 return;
107         }
108
109         /*
110          *  Take the easy way out:  Fill in some reasonable values for
111          *  the most important fields, and make the user use the modify
112          *  command to change them, or to give values to other fields.
113          */
114         init_rdn_value[0] = name;
115         init_rdn_value[1] = NULL;
116         init_rdn.mod_op = LDAP_MOD_ADD;
117         init_rdn.mod_type = "cn";
118         init_rdn.mod_values = init_rdn_value;
119         attrs[idx++] = &init_rdn;
120
121         init_owner_value[0] = bound_dn;
122         init_owner_value[1] = NULL;
123         init_owner.mod_op = LDAP_MOD_ADD;
124         init_owner.mod_type = "owner";
125         init_owner.mod_values = init_owner_value;
126         attrs[idx++] = &init_owner;
127
128 #ifdef UOFM
129         init_domain_value[0] = "umich.edu";
130 #else
131         init_domain_value[0] = ".";
132 #endif
133         init_domain_value[1] = NULL;
134         init_domain.mod_op = LDAP_MOD_ADD;
135         init_domain.mod_type = "associatedDomain";
136         init_domain.mod_values = init_domain_value;
137         attrs[idx++] = &init_domain;
138
139         init_errors_value[0] = bound_dn;
140         init_errors_value[1] = NULL;
141         init_errors.mod_op = LDAP_MOD_ADD;
142         init_errors.mod_type = "ErrorsTo";
143         init_errors.mod_values = init_errors_value;
144         attrs[idx++] = &init_errors;
145
146         init_request_value[0] = bound_dn;
147         init_request_value[1] = NULL;
148         init_request.mod_op = LDAP_MOD_ADD;
149         init_request.mod_type = "RequestsTo";
150         init_request.mod_values = init_request_value;
151         attrs[idx++] = &init_request;
152
153         init_joinable_value[0] = "FALSE";
154         init_joinable_value[1] = NULL;
155         init_joinable.mod_op = LDAP_MOD_ADD;
156         init_joinable.mod_type = "joinable";
157         init_joinable.mod_values = init_joinable_value;
158         attrs[idx++] = &init_joinable;
159
160         /* end it with a NULL */
161         attrs[idx] = NULL;
162
163 #ifdef DEBUG
164         if (debug & D_GROUPS) {
165                 register LDAPMod **lpp;
166                 register char **cpp;
167                 register int j;
168                 extern char * code_to_str();
169                 printf("  About to call ldap_add()\n");
170                 printf("  ld = 0x%x\n", ld);
171                 printf("  dn = [%s]\n", dn);
172                 for (lpp = attrs, i = 0; *lpp != NULL; lpp++, i++) {
173                         printf("  attrs[%1d]  code = %s  type = %s\n", i, 
174                                 code_to_str((*lpp)->mod_op), (*lpp)->mod_type);
175                         for (cpp = (*lpp)->mod_values, j = 0; *cpp != NULL; cpp++, j++)
176                                 printf("    value #%1d = %s\n", j, *cpp);
177                         printf("    value #%1d = NULL\n", j);
178                 }
179         }
180 #endif
181
182         /*
183          *  Now add this to the LDAP Directory.
184          */
185         if (ldap_add_s(ld, dn, attrs) != 0) {
186                 ldap_perror(ld, "  ldap_add_s");
187                 printf("  Group not added.\n");
188                 if (prompt) Free(name);
189                 return;
190         }
191         if (verbose)
192                 printf("  Group \"%s\" has been added to the Directory\n",
193                        name);
194
195         /*
196          *  We need to blow away the cache here.
197          *
198          *  Since we first looked up the name before trying to create it,
199          *  and that look-up failed, the cache will falsely claim that this
200          *  entry does not exist.
201          */
202         (void) ldap_flush_cache(ld);
203         if (prompt) Free(name);
204         return;
205 }
206
207 void remove_group(name)
208 char *name;
209 {
210         char *dn, tmp[BUFSIZ];
211         static char * bind_and_fetch();
212
213 #ifdef DEBUG
214         if (debug & D_TRACE) {
215                 if (name == NULL)
216                         printf("->remove_group(NULL)\n");
217                 else
218                         printf("->remove_group(%s)\n", name);
219         }
220 #endif
221         if ((dn = bind_and_fetch(name)) == NULL)
222                 return;
223
224         printf("\n  The group '%s' will be permanently removed from\n",
225                 name);
226         printf("  the Directory.  Are you absolutely sure that you want to\n" );        printf("  remove this entire group? ");
227         fflush(stdout);
228         fetch_buffer(tmp, sizeof(tmp), stdin);
229         if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
230                 return;
231
232         /*
233          *  Now remove this from the LDAP Directory.
234          */
235         if (ldap_delete_s(ld, dn) != 0) {
236                 if (ld->ld_errno == LDAP_INSUFFICIENT_ACCESS)
237                         printf("  You do not own the group \"%s\".\n", name);
238                 else
239                         ldap_perror(ld, "  ldap_delete_s");
240                 printf("  Group not removed.\n");
241                 Free(dn);
242                 return;
243         }
244         ldap_uncache_entry(ld, dn);
245         if (verbose)
246             if (name == NULL)
247                 printf("  The group has been removed.\n");
248             else
249                 printf("  The group \"%s\" has been removed.\n", name);
250         Free(dn);
251         return;
252 }
253
254 void x_group(action, name)
255 int action;
256 char *name;
257 {
258         char **vp;
259         char *values[2], *group_name;
260         LDAPMod mod, *mods[2];
261         static char *actions[] = { "join", "resign from", NULL };
262         static char * bind_and_fetch();
263
264 #ifdef DEBUG
265         if (debug & D_TRACE) {
266                 if (name == NULL)
267                         printf("->x_group(%d, NULL)\n", action);
268                 else
269                         printf("->x_group(%d, %s)\n", action, name);
270         }
271 #endif
272
273         /* the action desired sets the opcode to use */
274         switch (action) {
275         case G_JOIN:
276                 mod.mod_op = LDAP_MOD_ADD;
277                 break;
278         case G_RESIGN:
279                 mod.mod_op = LDAP_MOD_DELETE;
280                 break;
281         default:
282                 printf("x_group:  %d is not a known action\n", action);
283         }
284
285         if ((group_name = bind_and_fetch(name)) == NULL)
286                 return;
287         vp = Entry.attrs[attr_to_index("joinable")].values;
288         if (action == G_JOIN) {
289                 if (vp == NULL) {
290                         printf("  No one is permitted to join \"%s\"\n", group_name);
291                         Free(group_name);
292                         return;
293                 }
294                 if (!strcasecmp(*vp, "FALSE")) {
295                         printf("  No one is permitted to join \"%s\"\n", group_name);
296                         Free(group_name);
297                         return;
298                 }
299         }
300
301         /*  fill in the rest of the modification structure */
302         mods[0] = &mod;
303         mods[1] = (LDAPMod *) NULL;
304         values[0] = Entry.DN;
305         values[1] = NULL;
306         mod.mod_type = "memberOfGroup";
307         mod.mod_values = values;
308
309 #ifdef DEBUG
310         if (debug & D_GROUPS) {
311                 register LDAPMod **lpp;
312                 register char **cp;
313                 register int i, j;
314                 printf("  About to call ldap_modify_s()\n");
315                 printf("  ld = 0x%x\n", ld);
316                 printf("  dn = [%s]\n", bound_dn);
317                 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
318                         printf("  mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
319                         printf("  mods[%1d] type = %s\n", i, (*lpp)->mod_type);
320                         for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
321                                 printf("  mods[%1d] v[%1d] = %s\n", i, j, *cp);
322                                 
323                 }
324         }
325 #endif
326
327         if (ldap_modify_s(ld, bound_dn, mods)) {
328                 if ((action == G_JOIN) && (ld->ld_errno == LDAP_TYPE_OR_VALUE_EXISTS))
329                         printf("  You are already subscribed to \"%s\"\n", group_name);
330                 else if ((action == G_RESIGN) && (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE))
331                         printf("  You are not subscribed to \"%s\"\n", group_name);
332                 else
333                         mod_perror(ld);
334                 Free(group_name);
335                 return;
336         }
337         ldap_uncache_entry(ld, bound_dn);
338         if (verbose) {
339                 switch (action) {
340                 case G_JOIN:
341                         printf("  You are now subscribed to \"%s\"\n", group_name);
342                         break;
343                 case G_RESIGN:
344                         printf("  You are no longer subscribed to \"%s\"\n", group_name);
345                         break;
346                 }
347         }
348         Free(group_name);
349         return;
350 }
351
352 void bulk_load(group)
353 char *group;
354 {
355         register int idx_mail, idx_x500;
356         register int count_mail, count_x500;
357         char *values_mail[MAX_VALUES + 1], *values_x500[MAX_VALUES + 1];
358         int added_mail_entries = FALSE, added_x500_entries = FALSE;
359         char s[MED_BUF_SIZE];
360         LDAPMod mod, *mods[2];
361         LDAPMessage *lm;
362         FILE *fp;
363         int len;
364
365 #ifdef DEBUG
366         if (debug & D_TRACE)
367                 printf("->bulk_load(%s)\n", group);
368 #endif
369
370         /* you lose if going through MichNet */
371         if ( !isatty( 1 )) {
372 #ifdef UOFM
373                 printf("  Not allowed via UM-X500 connections.\n");
374 #endif
375                 return;
376         }
377
378         /* fetch entries from the file containing the e-mail addresses */
379         printf("\n  File to load? ");
380         fflush(stdout);
381         fetch_buffer(s, sizeof(s), stdin);
382         if (s[0] == '\0') {
383                 return;
384                 /*NOTREACHED*/
385         }
386         if ((fp = fopen(s, "r")) == NULL) {
387                 perror("bulk_load: fopen");
388                 return;
389         }
390         if (verbose)
391                 printf("  Loading group members from %s\n", s);
392
393         /* load them in MAX_VALUES at a time */
394         for (;;) {
395                 for (idx_mail = 0, idx_x500 = 0; 
396                      idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
397                         (void) fgets(s, sizeof(s), fp);
398                         if (feof(fp))
399                                 break;
400                         len = strlen(s) - 1;
401                         if (len == 0)
402                                 continue;
403                         s[len] = '\0';
404                         if (strchr(s, '@'))
405                                 values_mail[idx_mail++] = strdup(s);
406                         else {
407                                 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
408                                         printf("  Could not locate \"%s\" -- skipping.\n", s);
409                                 }
410                                 else {
411                                     parse_answer(lm);
412                                     values_x500[idx_x500++] = strdup(Entry.DN);
413                                 }
414                         }
415                 }
416                 values_mail[idx_mail] = NULL;
417                 values_x500[idx_x500] = NULL;
418                 count_mail = idx_mail;
419                 count_x500 = idx_x500;
420
421                 /*
422                  *  Add the e-mail addresses.
423                  */
424                 if (count_mail > 0) {
425                         mods[0] = &mod;
426                         mods[1] = (LDAPMod *) NULL;
427                         mod.mod_type = "mail";
428                         mod.mod_values = values_mail;
429                         if (added_mail_entries)
430                                 mod.mod_op = LDAP_MOD_ADD;
431                         else
432                                 mod.mod_op = LDAP_MOD_REPLACE;
433
434 #ifdef DEBUG
435                         if (debug & D_GROUPS) {
436                         register LDAPMod **lpp;
437                         register char **cp;
438                         register int i, j;
439                         printf("  About to call ldap_modify_s()\n");
440                         printf("  ld = 0x%x\n", ld);
441                         printf("  dn = [%s]\n", group);
442                         for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
443                                 printf("  mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
444                                 printf("  mods[%1d] type = %s\n", i, (*lpp)->mod_type);
445                                 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
446                                         printf("  mods[%1d] v[%1d] = %s\n", i, j, *cp);
447                                 }
448                         }
449 #endif
450                         if (ldap_modify_s(ld, group, mods))
451                                 mod_perror(ld);
452                         for (idx_mail--; idx_mail >= 0; idx_mail--)
453                                 Free(values_mail[idx_mail]);
454                         ldap_uncache_entry(ld, group);
455                         added_mail_entries = TRUE;
456
457                 }
458
459                 /*
460                  *  Add the LDAP style names.
461                  */
462                 if (count_x500 > 0) {
463                         mods[0] = &mod;
464                         mods[1] = (LDAPMod *) NULL;
465                         mod.mod_type = "member";
466                         mod.mod_values = values_x500;
467                         if (added_x500_entries)
468                                 mod.mod_op = LDAP_MOD_ADD;
469                         else
470                                 mod.mod_op = LDAP_MOD_REPLACE;
471
472 #ifdef DEBUG
473                         if (debug & D_GROUPS) {
474                         register LDAPMod **lpp;
475                         register char **cp;
476                         register int i, j;
477                         printf("  About to call ldap_modify_s()\n");
478                         printf("  ld = 0x%x\n", ld);
479                         printf("  dn = [%s]\n", group);
480                         for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
481                                 printf("  mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
482                                 printf("  mods[%1d] type = %s\n", i, (*lpp)->mod_type);
483                                 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
484                                         printf("  mods[%1d] v[%1d] = %s\n", i, j, *cp);
485                                 }
486                         }
487 #endif
488                         if (ldap_modify_s(ld, group, mods))
489                                 mod_perror(ld);
490                         for (idx_x500--; idx_x500 >= 0; idx_x500--)
491                                 Free(values_x500[idx_x500]);
492                         ldap_uncache_entry(ld, group);
493                         added_x500_entries = TRUE;
494
495                 }
496
497                 /*
498                  *  If both counts were less than the maximum number we
499                  *  can handle at a time, then we are done.
500                  */
501                 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
502                         break;
503         }
504         fclose(fp);
505         return;
506 }
507
508 void purge_group(group)
509 char *group;
510 {
511         int isclean = TRUE;
512         LDAPMessage *lm;
513         LDAPMod mod, *mods[2];
514         char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
515         extern char * my_ldap_dn2ufn();
516         extern int col_size;
517
518 #ifdef DEBUG
519         if (debug & D_TRACE) {
520                 if (group == NULL)
521                         printf("->purge_group(NULL)\n");
522                 else
523                         printf("->purge_group(%s)\n", group);
524         }
525 #endif
526         if (bind_status == UD_NOT_BOUND) {
527                 if (auth((char *) NULL, 1) < 0)
528                         return;
529         }
530         /*
531          *  If the user did not supply us with a name, prompt them for
532          *  a name.
533          */
534         if ((group == NULL) || (*group == '\0')) {
535                 printf("Group to purge? ");
536                 fflush(stdout);
537                 fetch_buffer(tmp, sizeof(tmp), stdin);
538                 if (tmp[0] == '\0')
539                         return;
540                 group = tmp;
541         }
542         sprintf(dn, "cn=%s, %s", group, group_base);
543
544         /* make sure the group in question exists */
545         if ((lm = find(group, FALSE)) == (LDAPMessage *) NULL) {
546                 printf("  Could not locate group \"%s\"\n", group);
547                 return;
548         }
549         parse_answer(lm);
550         ldap_msgfree(lm);
551
552         /* none of this stuff changes */
553         mods[0] = &mod;
554         mods[1] = (LDAPMod *) NULL;
555
556         values[1] = NULL;
557
558         mod.mod_values = values;
559         mod.mod_type = "member";
560         mod.mod_op = LDAP_MOD_DELETE;
561
562         /*
563          *  Now cycle through all of the names in the "members" part of the
564          *  group (but not the e-mail address part).  Lookup each one, and
565          *  if it isn't found, let the user know so s/he can delete it.
566          */
567         vp = Entry.attrs[attr_to_index("member")].values;
568         if (vp == NULL) {
569                 if (verbose)
570                         printf("  \"%s\" has no LDAP members.  There is nothing to purge.\n", group);
571                 return;
572         }
573         for (; *vp != NULL; vp++) {
574                 char ans[BUFSIZ], *ufn, *label = "Did not find:  ";
575                 int len = strlen(label);
576
577                 if (vrfy(*vp))
578                         continue;
579                 isclean = FALSE;
580                 ufn = my_ldap_dn2ufn(*vp);
581                 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
582 ask:
583                 printf("  Purge, Keep, Replace, Abort [Keep]? ");
584                 fflush(stdout);
585                 fetch_buffer(ans, sizeof(ans), stdin);
586                 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
587                         continue;
588                 if (!strncasecmp(ans, "Abort", strlen(ans))) {
589                         ldap_uncache_entry(ld, dn);
590                         return;
591                 }
592                 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
593                         values[0] = *vp;
594 #ifdef DEBUG
595                         if (debug & D_GROUPS) {
596                                 register LDAPMod **lpp;
597                                 register char **cp;
598                                 register int i, j;
599                                 printf("  About to call ldap_modify_s()\n");
600                                 printf("  ld = 0x%x\n", ld);
601                                 printf("  dn = [%s]\n", Entry.DN);
602                                 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
603                                         printf("  mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
604                                         printf("  mods[%1d] type = %s\n", i, (*lpp)->mod_type);
605                                         for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
606                                                 printf("  mods[%1d] v[%1d] = %s\n", i, j, *cp);
607                                                 
608                                 }
609                         }
610 #endif
611                         if (ldap_modify_s(ld, Entry.DN, mods))
612                                 mod_perror(ld);
613
614                         /* now add the replacement if requested */
615                         if (!strncasecmp(ans, "Purge", strlen(ans)))
616                                 continue;
617                         rdns = ldap_explode_dn(*vp, TRUE);
618                         if ((lm = find(*rdns, FALSE)) == NULL) {
619                                 printf("  Could not find a replacement for %s; purged only.\n", *rdns);
620                                 ldap_msgfree(lm);
621                                 ldap_value_free(rdns);
622                                 break;
623                         }
624                         values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
625                         mod.mod_op = LDAP_MOD_ADD;
626 #ifdef DEBUG
627                         if (debug & D_GROUPS) {
628                                 register LDAPMod **lpp;
629                                 register char **cp;
630                                 register int i, j;
631                                 printf("  About to call ldap_modify_s()\n");
632                                 printf("  ld = 0x%x\n", ld);
633                                 printf("  dn = [%s]\n", Entry.DN);
634                                 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
635                                         printf("  mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
636                                         printf("  mods[%1d] type = %s\n", i, (*lpp)->mod_type);
637                                         for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
638                                                 printf("  mods[%1d] v[%1d] = %s\n", i, j, *cp);
639                                                 
640                                 }
641                         }
642 #endif
643                         if (ldap_modify_s(ld, Entry.DN, mods))
644                                 mod_perror(ld);
645                         ldap_msgfree(lm);
646                         ldap_value_free(rdns);
647         
648                         /* set this back to DELETE for other purges */
649                         mod.mod_op = LDAP_MOD_DELETE;
650                 }
651                 else {
652                         printf("  Did not recognize that answer.\n\n");
653                         goto ask;
654                 }
655         }
656         ldap_uncache_entry(ld, Entry.DN);
657         if (isclean)
658                 printf("  No entries were purged.\n");
659         return;
660 }
661
662 void tidy_up()
663 {
664         register int i = 0;
665         int found_one = 0;
666         register char **vp;
667         LDAPMessage *lm;
668         static LDAPMod mod;
669         static LDAPMod *mods[2] = { &mod, NULL };
670         static char *values[MAX_VALUES];
671
672 #ifdef DEBUG
673         if (debug & D_TRACE)
674                 printf("->tidy()\n");
675 #endif
676
677         if (bind_status == UD_NOT_BOUND) {
678                 if (auth((char *) NULL, 1) < 0) {
679                         return;
680                 }
681         }
682
683         /* lookup the user, and see to which groups he has subscribed */
684         vp = ldap_explode_dn(bound_dn, TRUE);
685         if ((lm = find(*vp, TRUE)) == (LDAPMessage *) NULL) {
686                 printf("  Could not locate \"%s\"\n", *vp);
687                 ldap_value_free(vp);
688                 return;
689         }
690         ldap_value_free(vp);
691         parse_answer(lm);
692         ldap_msgfree(lm);
693         vp = Entry.attrs[attr_to_index("memberOfGroup")].values;
694         if (vp == NULL) {
695                 printf("  You have not subscribed to any groups.\n");
696                 return;
697         }
698
699         /* now, loop through these groups, deleting the bogus */
700         for ( ; *vp != NULL; vp++) {
701                 if (vrfy(*vp))
702                         continue;
703                 found_one++;
704                 printf("  \"%s\" is not a valid group name.\n", *vp);
705                 values[i++] = strdup(*vp);
706                 if ( i >= MAX_VALUES ) {
707                         printf( "  At most %d invalid groups can be removed at one time; skipping the rest.\n", MAX_VALUES );
708                         break;
709                 }
710         }
711         if (found_one == 0) {
712                 if (verbose)
713                         printf("  You are not a member of any invalid groups.  There is nothing to tidy.\n");
714                 return;
715         }
716
717         /* delete the most heinous entries */
718         values[i] = NULL;
719         mod.mod_values = values;
720         mod.mod_op = LDAP_MOD_DELETE;
721         mod.mod_type = "memberOfGroup";
722         if (ldap_modify_s(ld, bound_dn, mods))
723                 mod_perror(ld);
724         ldap_uncache_entry(ld, bound_dn);
725
726         /* tidy up before we finish tidy_up */
727         for ( ; i >= 1; i--)
728                 Free(values[i - 1]);
729         return;
730 }
731
732 /*
733  *  This routine is used to modify lists that can contain either Distinguished
734  *  Names or e-mail addresses.  This includes things like group members,
735  *  the errors-to field in groups, and so on.
736  */
737 void mod_addrDN(group, offset)
738 char *group;
739 int offset;
740 {
741         extern struct attribute attrlist[];
742         char s[BUFSIZ], *new_value /* was member */, *values[2];
743         char attrtype[ 64 ];
744         int i;
745         LDAPMod mod, *mods[2];
746         LDAPMessage *mp;
747
748 #ifdef DEBUG
749         if (debug & D_TRACE)
750                 printf("->mod_addrDN(%s)\n", group);
751 #endif
752         /*
753          *  At this point the user can indicate that he wishes to add values
754          *  to the attribute, delete values from the attribute, or replace the 
755          *  current list of values with a new list.  The first two cases
756          *  are very straight-forward, but the last case requires a little
757          *  extra care and work.
758          */
759         if (verbose) {
760                 printf("\n");
761                 if ( !isatty( 1 ))
762                         format("There are three options available at this point.  You may:  Add additional values; Delete values; or Replace the entire list of values with a new list entered interactively.\n", 75, 2);
763                 else
764                         format("There are four options available at this point.  You may:  Add one or more additional values; Delete one or more existing values; Replace the entire list of values with a new list entered interactively; or Bulk load a new list of values from a file, overwriting the existing list.\n", 75, 2);
765         }
766
767         /* initialize the modififier type */
768         mod.mod_type = NULL;
769
770         for (;;) {
771                 if ( !isatty( 1 ))
772                         printf("  Do you want to Add, Delete, or Replace? ");
773                 else
774                         printf("  Do you want to Add, Delete, Replace, or Bulk load? ");
775                 fflush(stdout);
776                 fetch_buffer(s, sizeof(s), stdin);
777                 if (s[0] == '\0') {
778                         return;
779                         /*NOTREACHED*/
780                 }
781                 if (!strncasecmp(s, "add", strlen(s))) {
782                         mod.mod_op = LDAP_MOD_ADD;
783                         break;
784                 }
785                 else if (!strncasecmp(s, "delete", strlen(s))) {
786                         mod.mod_op = LDAP_MOD_DELETE;
787                         break;
788                 }
789                 else if (!strncasecmp(s, "replace", strlen(s))) {
790                         mod.mod_op = LDAP_MOD_REPLACE;
791                         break;
792                 }
793                 else if(!strncasecmp(s, "bulk", strlen(s))) {
794                         bulk_load(group);
795                         return;
796                 }
797                 else if (verbose) {
798                         printf("\n");
799                         if ( !isatty( 1 ))
800                                 format("Did not recognize that response.  Please use 'A' to add, 'D' to delete, or 'R' to replace the entire list with a new list\n", 75, 2);
801                         else
802                                 format("Did not recognize that response.  Please use 'A' to add, 'D' to delete, 'R' to replace the entire list with a new list, or 'B' to bulk load a new list from a file\n", 75, 2);
803                 }
804         }
805         if (mod.mod_op == LDAP_MOD_REPLACE) {
806                 if ( verbose && !confirm_action( "The entire existing list will be overwritten with the new values you are about to enter." )) {
807                         printf("\n  Modification halted.\n");
808                         return;
809                 }
810         }
811         if (verbose) {
812                 printf("\n");
813                 format("Values may be specified as a name (which is then looked up in the LDAP Directory) or as a domain-style (i.e., user@domain) e-mail address.  Simply hit the RETURN key at the prompt when finished.\n", 75, 2);
814                 printf("\n");
815         }
816
817         for (;;) {
818                 printf("%s? ", attrlist[offset].output_string);
819                 fflush(stdout);
820                 fetch_buffer(s, sizeof(s), stdin);
821                 if (s[0] == '\0')
822                         return;
823
824                 /*
825                  *  If the string the user has just typed has an @-sign in it,
826                  *  then we assume it is an e-mail address.  In this case, we
827                  *  just store away whatever it is they have typed.
828                  *
829                  *  If the string had no @-sign, then we look in the Directory,
830                  *  make sure it exists, and if it does, we add that.
831                  *
832                  *  If the string begins with a comma, then strip off the
833                  *  comma, and pass it along to the LDAP server.  This is
834                  *  the way one can force ud to accept a name.  Handy for
835                  *  debugging purposes.
836                  */
837                 if (*s == ',') {
838                         new_value = s + 1;
839                         mod.mod_type = attrlist[offset].quipu_name;
840                 }
841                 else if (strchr(s, '@') == NULL) {
842                         if ((mp = find(s, FALSE)) == (LDAPMessage *) NULL) {
843                                 printf("  Could not find \"%s\"\n", s);
844                                 if (verbose && (mod.mod_op == LDAP_MOD_DELETE)){
845                                         printf("\n");
846                                         format("I could not find anything that matched what you typed.  You might try the \"purge\" command instead.  It is used to purge corrupted or unlocatable entries from a group.", 75, 2);
847                                         printf("\n");
848                                 }
849                                 continue;
850                         }
851                         parse_answer(mp);
852                         new_value = Entry.DN;
853                         mod.mod_type = attrlist[offset].quipu_name;
854                 }
855                 else if (mod.mod_op != LDAP_MOD_DELETE) {
856                         /*
857                          * Don't screw around with what the user has typed
858                          * if they are simply trying to delete a rfc822mailbox
859                          * value.
860                          *
861                          * spaces on the left hand side of the e-mail
862                          * address are bad news - we know that there
863                          * must be a @-sign in the string, or else we
864                          * would not be here
865                          *
866                          * note that this means a value like:
867                          *
868                          *      first m. last@host.domain
869                          *
870                          * will be turned into:
871                          *
872                          *      first.m..last@host.domain
873                          *
874                          * and the mailer will need to do the right thing
875                          * with this; alternatively we could add code that
876                          * collapsed multiple dots into a single dot
877                          *
878                          * Don't screw up things like:
879                          *
880                          *      "Bryan Beecher" <bryan@umich.edu>
881                          *       Bryan Beecher  <bryan@umich.edu>
882                          */
883                         register char *cp;
884                         if (strchr(s, '<') == NULL) {
885                                 for (cp = s; *cp != '@'; cp++)
886                                         if (isspace(*cp))
887                                                 *cp = '.';
888                         }
889                         new_value = s;
890                         strcpy(attrtype, "rfc822");
891                         strcat(attrtype, attrlist[offset].quipu_name);
892                         mod.mod_type = attrtype;
893                 }
894                 else {
895                         new_value = s;
896                         strcpy(attrtype, "rfc822");
897                         strcat(attrtype, attrlist[offset].quipu_name);
898                         mod.mod_type = attrtype;
899                 }
900
901                 /*  fill in the rest of the ldap_mod() structure */
902                 mods[0] = &mod;
903                 mods[1] = (LDAPMod *) NULL;
904
905                 values[0] = new_value;
906                 values[1] = NULL;
907                 mod.mod_values = values;
908
909 #ifdef DEBUG
910                 if (debug & D_GROUPS) {
911                         register LDAPMod **lpp;
912                         register char **cp;
913                         register int i, j;
914                         printf("  About to call ldap_modify_s()\n");
915                         printf("  ld = 0x%x\n", ld);
916                         printf("  dn = [%s]\n", group);
917                         for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
918                                 printf("  mods[%1d] code = %1d\n", 
919                                                         i, (*lpp)->mod_op);
920                                 printf("  mods[%1d] type = %s\n", 
921                                                         i, (*lpp)->mod_type);
922                                 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
923                                         printf("  mods[%1d] v[%1d] = %s\n", 
924                                                                 i, j, *cp);
925                         }
926                 }
927 #endif
928
929                 if (my_ldap_modify_s(ld, group, mods)) {
930                         if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE) {
931                                 printf("  Could not locate value \"%s\"\n", 
932                                                                 new_value);
933                                 continue;
934                         }
935                         else {
936                                 mod_perror(ld);
937                                 return;
938                         }
939                 }
940                 ldap_uncache_entry(ld, group);
941
942                 /*
943                  *  If the operation was REPLACE, we now need to "zero out" the
944                  *  other "half" of the list (e.g., user specified an e-mail
945                  *  address; now we need to clear the DN part of the list).
946                  *
947                  *  NOTE:  WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
948                  *
949                  *  Also, change the opcode to LDAP_MOD_ADD and give the user an
950                  *  opportunity to add additional members to the group.  We
951                  *  only take this branch the very first time during a REPLACE
952                  *  operation.
953                  */
954                 if (mod.mod_op == LDAP_MOD_REPLACE) {
955                         if (!strncmp(mod.mod_type, "rfc822", 6))
956                                 mod.mod_type = mod.mod_type + 6;
957                         else {
958                                 strcpy(attrtype, "rfc822");
959                                 strcat(attrtype, mod.mod_type);
960                                 mod.mod_type = attrtype;
961                         }
962                         mods[0] = &mod;
963                         values[0] = NULL;
964                         mod.mod_values = values;
965                         mod.mod_op = LDAP_MOD_DELETE;
966 #ifdef DEBUG
967                         if (debug & D_GROUPS) {
968                                 register LDAPMod **lpp;
969                                 register char **cp;
970                                 register int i, j;
971                                 printf("  About to call ldap_modify_s()\n");
972                                 printf("  ld = 0x%x\n", ld);
973                                 printf("  dn = [%s]\n", group);
974                                 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
975                                         printf("  mods[%1d] code = %1d\n", 
976                                                         i, (*lpp)->mod_op);
977                                         printf("  mods[%1d] type = %s\n", 
978                                                         i, (*lpp)->mod_type);
979                                         for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
980                                                 printf("  mods[%1d] v[%1d] = %s\n", i, j, *cp);
981                                 }
982                         }
983 #endif
984                         if (my_ldap_modify_s(ld, group, mods)) {
985                                 /*
986                                 *  A "No such attribute" error is no big deal.
987                                 *  We only wanted to clear the attribute anyhow.
988                                 */
989                                 if (ld->ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
990                                         mod_perror(ld);
991                                         return;
992                                 }
993                         }
994                         ldap_uncache_entry(ld, group);
995                         if (verbose)
996                                 printf("  \"%s\" has been added\n", new_value);
997                         mod.mod_op = LDAP_MOD_ADD;
998                 }
999                 else if (verbose && (mod.mod_op == LDAP_MOD_ADD))
1000                         printf("  \"%s\" has been added\n", new_value);
1001                 else if (verbose && (mod.mod_op == LDAP_MOD_DELETE))
1002                         printf("  \"%s\" has been removed\n", new_value);
1003         }
1004 }
1005
1006 my_ldap_modify_s(ldap, group, mods)
1007 LDAP *ldap;
1008 char *group;
1009 LDAPMod *mods[];
1010 {
1011         int     was_rfc822member, rc;
1012
1013         was_rfc822member = 0;
1014
1015         if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
1016                 mods[0]->mod_type = "mail";
1017                 was_rfc822member = 1;
1018         }
1019
1020         rc = ldap_modify_s(ldap, group, mods);
1021
1022         if (was_rfc822member)
1023             mods[0]->mod_type = "rfc822member";
1024
1025         return(rc);
1026 }
1027
1028 void list_groups(who)
1029 char *who;
1030 {
1031         LDAPMessage *mp;
1032         char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1033         char *work_area[MAX_NUM_NAMES];
1034         char *dn, **rdns;
1035         int i, rc;
1036         
1037
1038 #ifdef DEBUG
1039         if (debug & D_TRACE) {
1040                 if (who == NULL)
1041                         printf("->list_groups(NULL)\n");
1042                 else
1043                         printf("->list_groups(%s)\n", who);
1044         }
1045 #endif
1046         /*
1047          *  First, decide what entry we are going to list.  If the
1048          *  user has not included a name on the list command line,
1049          *  we will use the person who was last looked up with a find
1050          *  command.
1051          *
1052          *  Once we know who to modify, be sure that they exist, and
1053          *  parse out their DN.
1054          */
1055         if (who == NULL) {
1056                 printf("  List groups belonging to whose entry? ");
1057                 fflush(stdout);
1058                 fetch_buffer(name, sizeof(name), stdin);
1059                 if (name[0] != '\0')
1060                         who = name;
1061                 else
1062                         return;
1063         }
1064         if ((mp = find(who, TRUE)) == NULL) {
1065                 (void) ldap_msgfree(mp);
1066                 printf("  Could not locate \"%s\" in the Directory.\n", who);
1067                 return;
1068         }
1069         dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1070         ldap_msgfree(mp);
1071         rdns = ldap_explode_dn(dn, TRUE);
1072         if (verbose)
1073                 printf("\n  Listing groups belonging to \"%s\"\n", *rdns);
1074
1075         /* lookup the groups belonging to this person */
1076         sprintf(filter, "owner=%s", dn);
1077         Free(dn);
1078         search_attrs[0] = "cn";
1079         search_attrs[1] = NULL;
1080         if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE, 
1081                 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1082             rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1083                 ldap_perror(ld, "ldap_search_s");
1084                 ldap_value_free(rdns);
1085                 return;
1086         }
1087         if ((rc = ldap_count_entries(ld, mp)) < 0) {
1088                 ldap_perror(ld, "ldap_count_entries");
1089                 ldap_value_free(rdns);
1090                 return;
1091         }
1092         if (rc == 0) {
1093                 printf("  %s owns no groups in this portion of the Directory.\n", *rdns);
1094                 ldap_value_free(rdns);
1095                 return;
1096         }
1097         if (verbose)
1098                 printf("  %s owns %d groups.\n\n", *rdns, rc);
1099         print_list(mp, work_area, &rc);
1100         for (i = 1; work_area[i] != NULL; i++)
1101                 Free(work_area[i]);
1102         ldap_msgfree(mp);
1103         ldap_value_free(rdns);
1104         return;
1105 }
1106
1107 static char * bind_and_fetch(name)
1108 char *name;
1109 {
1110         LDAPMessage *lm;
1111         char tmp[MED_BUF_SIZE];
1112         extern char * strip_ignore_chars();
1113
1114 #ifdef DEBUG
1115         if (debug & D_TRACE) {
1116                 if (name == NULL)
1117                         printf("->bind_and_fetch(NULL)\n");
1118                 else
1119                         printf("->bind_and_fetch(%s)\n", name);
1120         }
1121 #endif
1122         if (bind_status == UD_NOT_BOUND) {
1123                 if (auth((char *) NULL, 1) < 0)
1124                         return(NULL);
1125         }
1126
1127         /*
1128          *  If the user did not supply us with a name, prompt them for
1129          *  a name.
1130          */
1131         if ((name == NULL) || (*name == '\0')) {
1132                 printf("  Group? ");
1133                 fflush(stdout);
1134                 fetch_buffer(tmp, sizeof(tmp), stdin);
1135                 if (tmp[0] == '\0')
1136                         return(NULL);
1137                 name = tmp;
1138         }
1139         /* remove quotes, dots, and underscores. */
1140         name = strip_ignore_chars(name);
1141
1142 #ifdef DEBUG
1143         if (debug & D_GROUPS)
1144                 printf("Group name = (%s)\n", name);
1145 #endif
1146
1147         /* make sure the group in question exists and is joinable */
1148         if ((lm = find(name, TRUE)) == (LDAPMessage *) NULL) {
1149                 printf("  Could not locate group \"%s\"\n", name);
1150                 return(NULL);
1151         }
1152         parse_answer(lm);
1153         ldap_msgfree(lm);
1154
1155 #ifdef DEBUG
1156         if (debug & D_GROUPS)
1157                 printf("Group DN = (%s)\n", Entry.DN);
1158 #endif
1159         return(strdup(Entry.DN));
1160 }
1161
1162 void list_memberships(who)
1163 char *who;
1164 {
1165         LDAPMessage *mp;
1166         char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1167         char *work_area[MAX_NUM_NAMES];
1168         char *dn, **rdns;
1169         int i, rc;
1170         
1171
1172 #ifdef DEBUG
1173         if (debug & D_TRACE) {
1174                 if (who == NULL)
1175                         printf("->list_memberships(NULL)\n");
1176                 else
1177                         printf("->list_memberships(%s)\n", who);
1178         }
1179 #endif
1180         /*
1181          *  First, decide what entry we are going to list.  If the
1182          *  user has not included a name on the list command line,
1183          *  we will use the person who was last looked up with a find
1184          *  command.
1185          *
1186          *  Once we know who to modify, be sure that they exist, and
1187          *  parse out their DN.
1188          */
1189         if (who == NULL) {
1190                 printf("  List memberships containing whose entry? ");
1191                 fflush(stdout);
1192                 fetch_buffer(name, sizeof(name), stdin);
1193                 if (name[0] != '\0')
1194                         who = name;
1195                 else
1196                         return;
1197         }
1198         if ((mp = find(who, TRUE)) == NULL) {
1199                 (void) ldap_msgfree(mp);
1200                 printf("  Could not locate \"%s\" in the Directory.\n", who);
1201                 ldap_msgfree(mp);
1202                 return;
1203         }
1204         dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1205         rdns = ldap_explode_dn(dn, TRUE);
1206         if (verbose)
1207                 printf("\n  Listing memberships of \"%s\"\n", *rdns);
1208
1209         /* lookup the groups belonging to this person */
1210         sprintf(filter, "member=%s", dn);
1211         Free(dn);
1212         search_attrs[0] = "cn";
1213         search_attrs[1] = NULL;
1214         ldap_msgfree(mp);
1215         if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE, 
1216                 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1217             rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1218                 ldap_perror(ld, "ldap_search_s");
1219                 ldap_msgfree(mp);
1220                 ldap_value_free(rdns);
1221                 return;
1222         }
1223         if ((rc = ldap_count_entries(ld, mp)) < 0) {
1224                 ldap_perror(ld, "ldap_count_entries");
1225                 ldap_msgfree(mp);
1226                 ldap_value_free(rdns);
1227                 return;
1228         }
1229         if (rc == 0) {
1230                 printf("  %s is not a member of any groups in this portion of the Directory.\n", *rdns);
1231                 ldap_msgfree(mp);
1232                 ldap_value_free(rdns);
1233                 return;
1234         }
1235         if (verbose)
1236                 printf("  %s is a member of %d groups.\n\n", *rdns, rc);
1237
1238         /*
1239          *  print_list fills in the char * array starting at 1, not 0
1240          */
1241         print_list(mp, work_area, &rc);
1242         for (i = 1; work_area[i] != NULL; i++)
1243                 Free(work_area[i]);
1244         ldap_msgfree(mp);
1245         ldap_value_free(rdns);
1246         return;
1247 }