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