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