2 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
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.
18 #include <ldapconfig.h>
21 extern LDAPMessage * find();
27 extern char *bound_dn, *group_base;
28 extern int verbose, bind_status;
29 extern struct entry Entry;
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();
49 if (debug & D_TRACE) {
51 printf("->add_group(NULL)\n");
53 printf("->add_group(%s)\n", name);
57 if (bind_status == UD_NOT_BOUND) {
58 if (auth((char *) NULL, 1) < 0) {
64 * If the user did not supply us with a name, prompt them for
67 if ((name == NULL) || (*name == '\0') || !strcasecmp(name, "group")) {
69 printf(" Group to create? ");
71 fetch_buffer(tmp, sizeof(tmp), stdin);
77 /* remove quotes, dots, and underscores. */
78 name = strip_ignore_chars(name);
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? ");
87 fetch_buffer(tmp, sizeof(tmp), stdin);
88 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
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);
94 sprintf(dn, "cn=%s, %s", name, group_base);
97 * Make sure that this group does not already exist.
99 if (vrfy(dn) == TRUE) {
100 printf(" The group \"%s\" already exists.\n", name);
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.
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;
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;
124 init_domain_value[0] = "umich.edu";
126 init_domain_value[0] = ".";
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;
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;
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;
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;
155 /* end it with a NULL */
159 if (debug & D_GROUPS) {
160 register LDAPMod **lpp;
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);
178 * Now add this to the X.500 Directory.
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);
187 printf(" Group \"%s\" has been added to the Directory\n",
191 * We need to blow away the cache here.
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.
197 (void) ldap_flush_cache(ld);
198 if (prompt) Free(name);
202 void remove_group(name)
205 char *dn, tmp[BUFSIZ];
206 static char * bind_and_fetch();
209 if (debug & D_TRACE) {
211 printf("->remove_group(NULL)\n");
213 printf("->remove_group(%s)\n", name);
216 if ((dn = bind_and_fetch(name)) == NULL)
219 printf("\n The group '%s' will be permanently removed from\n",
221 printf(" the Directory. Are you absolutely sure that you want to\n" ); printf(" remove this entire group? ");
223 fetch_buffer(tmp, sizeof(tmp), stdin);
224 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
228 * Now remove this from the X.500 Directory.
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);
234 ldap_perror(ld, " ldap_delete_s");
235 printf(" Group not removed.\n");
239 ldap_uncache_entry(ld, dn);
242 printf(" The group has been removed.\n");
244 printf(" The group \"%s\" has been removed.\n", name);
249 void x_group(action, name)
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();
260 if (debug & D_TRACE) {
262 printf("->x_group(%d, NULL)\n", action);
264 printf("->x_group(%d, %s)\n", action, name);
268 /* the action desired sets the opcode to use */
271 mod.mod_op = LDAP_MOD_ADD;
274 mod.mod_op = LDAP_MOD_DELETE;
277 printf("x_group: %d is not a known action\n", action);
280 if ((group_name = bind_and_fetch(name)) == NULL)
282 vp = Entry.attrs[attr_to_index("joinable")].values;
283 if (action == G_JOIN) {
285 printf(" No one is permitted to join \"%s\"\n", group_name);
289 if (!strcasecmp(*vp, "FALSE")) {
290 printf(" No one is permitted to join \"%s\"\n", group_name);
296 /* fill in the rest of the modification structure */
298 mods[1] = (LDAPMod *) NULL;
299 values[0] = Entry.DN;
301 mod.mod_type = "memberOfGroup";
302 mod.mod_values = values;
305 if (debug & D_GROUPS) {
306 register LDAPMod **lpp;
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);
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);
332 ldap_uncache_entry(ld, bound_dn);
336 printf(" You are now subscribed to \"%s\"\n", group_name);
339 printf(" You are no longer subscribed to \"%s\"\n", group_name);
347 void bulk_load(group)
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];
362 printf("->bulk_load(%s)\n", group);
365 /* you lose if going through MichNet */
368 printf(" Not allowed via UM-X500 connections.\n");
373 /* fetch entries from the file containing the e-mail addresses */
374 printf("\n File to load? ");
376 fetch_buffer(s, sizeof(s), stdin);
381 if ((fp = fopen(s, "r")) == NULL) {
382 perror("bulk_load: fopen");
386 printf(" Loading group members from %s\n", s);
388 /* load them in MAX_VALUES at a time */
390 for (idx_mail = 0, idx_x500 = 0;
391 idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
392 (void) fgets(s, sizeof(s), fp);
400 values_mail[idx_mail++] = strdup(s);
402 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
403 printf(" Could not locate \"%s\" -- skipping.\n", s);
407 values_x500[idx_x500++] = strdup(Entry.DN);
411 values_mail[idx_mail] = NULL;
412 values_x500[idx_x500] = NULL;
413 count_mail = idx_mail;
414 count_x500 = idx_x500;
417 * Add the e-mail addresses.
419 if (count_mail > 0) {
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;
427 mod.mod_op = LDAP_MOD_REPLACE;
430 if (debug & D_GROUPS) {
431 register LDAPMod **lpp;
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);
445 if (ldap_modify_s(ld, group, mods))
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;
455 * Add the X.500 style names.
457 if (count_x500 > 0) {
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;
465 mod.mod_op = LDAP_MOD_REPLACE;
468 if (debug & D_GROUPS) {
469 register LDAPMod **lpp;
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);
483 if (ldap_modify_s(ld, group, mods))
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;
493 * If both counts were less than the maximum number we
494 * can handle at a time, then we are done.
496 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
503 void purge_group(group)
508 LDAPMod mod, *mods[2];
509 char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
510 extern char * my_ldap_dn2ufn();
514 if (debug & D_TRACE) {
516 printf("->purge_group(NULL)\n");
518 printf("->purge_group(%s)\n", group);
521 if (bind_status == UD_NOT_BOUND) {
522 if (auth((char *) NULL, 1) < 0)
526 * If the user did not supply us with a name, prompt them for
529 if ((group == NULL) || (*group == '\0')) {
530 printf("Group to purge? ");
532 fetch_buffer(tmp, sizeof(tmp), stdin);
537 sprintf(dn, "cn=%s, %s", group, group_base);
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);
547 /* none of this stuff changes */
549 mods[1] = (LDAPMod *) NULL;
553 mod.mod_values = values;
554 mod.mod_type = "member";
555 mod.mod_op = LDAP_MOD_DELETE;
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.
562 vp = Entry.attrs[attr_to_index("member")].values;
565 printf(" \"%s\" has no X.500 members. There is nothing to purge.\n", group);
568 for (; *vp != NULL; vp++) {
569 char ans[BUFSIZ], *ufn, *label = "Did not find: ";
570 int len = strlen(label);
575 ufn = my_ldap_dn2ufn(*vp);
576 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
578 printf(" Purge, Keep, Replace, Abort [Keep]? ");
580 fetch_buffer(ans, sizeof(ans), stdin);
581 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
583 if (!strncasecmp(ans, "Abort", strlen(ans))) {
584 ldap_uncache_entry(ld, dn);
587 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
590 if (debug & D_GROUPS) {
591 register LDAPMod **lpp;
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);
606 if (ldap_modify_s(ld, Entry.DN, mods))
609 /* now add the replacement if requested */
610 if (!strncasecmp(ans, "Purge", strlen(ans)))
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);
616 ldap_value_free(rdns);
619 values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
620 mod.mod_op = LDAP_MOD_ADD;
622 if (debug & D_GROUPS) {
623 register LDAPMod **lpp;
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);
638 if (ldap_modify_s(ld, Entry.DN, mods))
641 ldap_value_free(rdns);
643 /* set this back to DELETE for other purges */
644 mod.mod_op = LDAP_MOD_DELETE;
647 printf(" Did not recognize that answer.\n\n");
651 ldap_uncache_entry(ld, Entry.DN);
653 printf(" No entries were purged.\n");
664 static LDAPMod *mods[2] = { &mod, NULL };
665 static char *values[MAX_VALUES];
669 printf("->tidy()\n");
672 if (bind_status == UD_NOT_BOUND) {
673 if (auth((char *) NULL, 1) < 0) {
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);
688 vp = Entry.attrs[attr_to_index("memberOfGroup")].values;
690 printf(" You have not subscribed to any groups.\n");
694 /* now, loop through these groups, deleting the bogus */
695 for ( ; *vp != NULL; vp++) {
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 );
706 if (found_one == 0) {
708 printf(" You are not a member of any invalid groups. There is nothing to tidy.\n");
712 /* delete the most heinous entries */
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))
719 ldap_uncache_entry(ld, bound_dn);
721 /* tidy up before we finish tidy_up */
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.
732 void mod_addrDN(group, offset)
736 extern struct attribute attrlist[];
737 char s[BUFSIZ], *new_value /* was member */, *values[2];
740 LDAPMod mod, *mods[2];
745 printf("->mod_addrDN(%s)\n", group);
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.
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);
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);
762 /* initialize the modififier type */
767 printf(" Do you want to Add, Delete, or Replace? ");
769 printf(" Do you want to Add, Delete, Replace, or Bulk load? ");
771 fetch_buffer(s, sizeof(s), stdin);
776 if (!strncasecmp(s, "add", strlen(s))) {
777 mod.mod_op = LDAP_MOD_ADD;
780 else if (!strncasecmp(s, "delete", strlen(s))) {
781 mod.mod_op = LDAP_MOD_DELETE;
784 else if (!strncasecmp(s, "replace", strlen(s))) {
785 mod.mod_op = LDAP_MOD_REPLACE;
788 else if(!strncasecmp(s, "bulk", strlen(s))) {
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);
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);
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");
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);
813 printf("%s? ", attrlist[offset].output_string);
815 fetch_buffer(s, sizeof(s), stdin);
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.
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.
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.
834 mod.mod_type = attrlist[offset].quipu_name;
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)){
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);
847 new_value = Entry.DN;
848 mod.mod_type = attrlist[offset].quipu_name;
850 else if (mod.mod_op != LDAP_MOD_DELETE) {
852 * Don't screw around with what the user has typed
853 * if they are simply trying to delete a rfc822mailbox
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
861 * note that this means a value like:
863 * first m. last@host.domain
865 * will be turned into:
867 * first.m..last@host.domain
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
873 * Don't screw up things like:
875 * "Bryan Beecher" <bryan@umich.edu>
876 * Bryan Beecher <bryan@umich.edu>
879 if (strchr(s, '<') == NULL) {
880 for (cp = s; *cp != '@'; cp++)
885 strcpy(attrtype, "rfc822");
886 strcat(attrtype, attrlist[offset].quipu_name);
887 mod.mod_type = attrtype;
891 strcpy(attrtype, "rfc822");
892 strcat(attrtype, attrlist[offset].quipu_name);
893 mod.mod_type = attrtype;
896 /* fill in the rest of the ldap_mod() structure */
898 mods[1] = (LDAPMod *) NULL;
900 values[0] = new_value;
902 mod.mod_values = values;
905 if (debug & D_GROUPS) {
906 register LDAPMod **lpp;
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",
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",
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",
935 ldap_uncache_entry(ld, group);
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).
942 * NOTE: WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
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
949 if (mod.mod_op == LDAP_MOD_REPLACE) {
950 if (!strncmp(mod.mod_type, "rfc822", 6))
951 mod.mod_type = mod.mod_type + 6;
953 strcpy(attrtype, "rfc822");
954 strcat(attrtype, mod.mod_type);
955 mod.mod_type = attrtype;
959 mod.mod_values = values;
960 mod.mod_op = LDAP_MOD_DELETE;
962 if (debug & D_GROUPS) {
963 register LDAPMod **lpp;
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",
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);
979 if (my_ldap_modify_s(ld, group, mods)) {
981 * A "No such attribute" error is no big deal.
982 * We only wanted to clear the attribute anyhow.
984 if (ld->ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
989 ldap_uncache_entry(ld, group);
991 printf(" \"%s\" has been added\n", new_value);
992 mod.mod_op = LDAP_MOD_ADD;
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);
1001 my_ldap_modify_s(ldap, group, mods)
1006 int was_rfc822member, rc;
1008 was_rfc822member = 0;
1010 if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
1011 mods[0]->mod_type = "mail";
1012 was_rfc822member = 1;
1015 rc = ldap_modify_s(ldap, group, mods);
1017 if (was_rfc822member)
1018 mods[0]->mod_type = "rfc822member";
1023 void list_groups(who)
1027 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1028 char *work_area[MAX_NUM_NAMES];
1034 if (debug & D_TRACE) {
1036 printf("->list_groups(NULL)\n");
1038 printf("->list_groups(%s)\n", who);
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
1047 * Once we know who to modify, be sure that they exist, and
1048 * parse out their DN.
1051 printf(" List groups belonging to whose entry? ");
1053 fetch_buffer(name, sizeof(name), stdin);
1054 if (name[0] != '\0')
1059 if ((mp = find(who, TRUE)) == NULL) {
1060 (void) ldap_msgfree(mp);
1061 printf(" Could not locate \"%s\" in the Directory.\n", who);
1064 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1066 rdns = ldap_explode_dn(dn, TRUE);
1068 printf("\n Listing groups belonging to \"%s\"\n", *rdns);
1070 /* lookup the groups belonging to this person */
1071 sprintf(filter, "owner=%s", 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);
1082 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1083 ldap_perror(ld, "ldap_count_entries");
1084 ldap_value_free(rdns);
1088 printf(" %s owns no groups in this portion of the Directory.\n", *rdns);
1089 ldap_value_free(rdns);
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++)
1098 ldap_value_free(rdns);
1102 static char * bind_and_fetch(name)
1106 char tmp[MED_BUF_SIZE];
1107 extern char * strip_ignore_chars();
1110 if (debug & D_TRACE) {
1112 printf("->bind_and_fetch(NULL)\n");
1114 printf("->bind_and_fetch(%s)\n", name);
1117 if (bind_status == UD_NOT_BOUND) {
1118 if (auth((char *) NULL, 1) < 0)
1123 * If the user did not supply us with a name, prompt them for
1126 if ((name == NULL) || (*name == '\0')) {
1129 fetch_buffer(tmp, sizeof(tmp), stdin);
1134 /* remove quotes, dots, and underscores. */
1135 name = strip_ignore_chars(name);
1138 if (debug & D_GROUPS)
1139 printf("Group name = (%s)\n", name);
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);
1151 if (debug & D_GROUPS)
1152 printf("Group DN = (%s)\n", Entry.DN);
1154 return(strdup(Entry.DN));
1157 void list_memberships(who)
1161 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1162 char *work_area[MAX_NUM_NAMES];
1168 if (debug & D_TRACE) {
1170 printf("->list_memberships(NULL)\n");
1172 printf("->list_memberships(%s)\n", who);
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
1181 * Once we know who to modify, be sure that they exist, and
1182 * parse out their DN.
1185 printf(" List memberships containing whose entry? ");
1187 fetch_buffer(name, sizeof(name), stdin);
1188 if (name[0] != '\0')
1193 if ((mp = find(who, TRUE)) == NULL) {
1194 (void) ldap_msgfree(mp);
1195 printf(" Could not locate \"%s\" in the Directory.\n", who);
1199 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1200 rdns = ldap_explode_dn(dn, TRUE);
1202 printf("\n Listing memberships of \"%s\"\n", *rdns);
1204 /* lookup the groups belonging to this person */
1205 sprintf(filter, "member=%s", dn);
1207 search_attrs[0] = "cn";
1208 search_attrs[1] = NULL;
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");
1215 ldap_value_free(rdns);
1218 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1219 ldap_perror(ld, "ldap_count_entries");
1221 ldap_value_free(rdns);
1225 printf(" %s is not a member of any groups in this portion of the Directory.\n", *rdns);
1227 ldap_value_free(rdns);
1231 printf(" %s is a member of %d groups.\n\n", *rdns, rc);
1234 * print_list fills in the char * array starting at 1, not 0
1236 print_list(mp, work_area, &rc);
1237 for (i = 1; work_area[i] != NULL; i++)
1240 ldap_value_free(rdns);