3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
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.
23 #include <ac/string.h>
26 #include <ac/unistd.h>
34 #include "ldap_defaults.h"
37 static char * bind_and_fetch(char *name);
41 add_group( char *name )
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];
53 if (debug & D_TRACE) {
55 printf("->add_group(NULL)\n");
57 printf("->add_group(%s)\n", name);
61 if (bind_status == UD_NOT_BOUND) {
62 if (auth((char *) NULL, 1) < 0) {
68 * If the user did not supply us with a name, prompt them for
71 if ((name == NULL) || (*name == '\0') || !strcasecmp(name, "group")) {
73 printf(" Group to create? ");
75 fetch_buffer(tmp, sizeof(tmp), stdin);
81 /* remove quotes, dots, and underscores. */
82 name = strip_ignore_chars(name);
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? ");
91 fetch_buffer(tmp, sizeof(tmp), stdin);
92 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
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);
98 sprintf(dn, "cn=%s, %s", name, group_base);
101 * Make sure that this group does not already exist.
103 if (vrfy(dn) == TRUE) {
104 printf(" The group \"%s\" already exists.\n", name);
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.
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;
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;
128 init_domain_value[0] = "umich.edu";
130 init_domain_value[0] = ".";
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;
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;
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;
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;
159 /* end it with a NULL */
163 if (debug & D_GROUPS) {
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);
181 * Now add this to the LDAP Directory.
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);
190 printf(" Group \"%s\" has been added to the Directory\n",
194 * We need to blow away the cache here.
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.
200 (void) ldap_flush_cache(ld);
201 if (prompt) Free(name);
206 remove_group( char *name )
208 char *dn, tmp[BUFSIZ];
211 if (debug & D_TRACE) {
213 printf("->remove_group(NULL)\n");
215 printf("->remove_group(%s)\n", name);
218 if ((dn = bind_and_fetch(name)) == NULL)
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? ");
225 fetch_buffer(tmp, sizeof(tmp), stdin);
226 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
230 * Now remove this from the LDAP Directory.
232 if (ldap_delete_s(ld, dn) != 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);
238 ldap_perror(ld, " ldap_delete_s");
239 printf(" Group not removed.\n");
243 ldap_uncache_entry(ld, dn);
247 printf(" The group has been removed.\n");
249 printf(" The group \"%s\" has been removed.\n", name);
256 x_group( int action, char *name )
259 char *values[2], *group_name;
260 LDAPMod mod, *mods[2];
263 if (debug & D_TRACE) {
265 printf("->x_group(%d, NULL)\n", action);
267 printf("->x_group(%d, %s)\n", action, name);
271 /* the action desired sets the opcode to use */
274 mod.mod_op = LDAP_MOD_ADD;
277 mod.mod_op = LDAP_MOD_DELETE;
280 printf("x_group: %d is not a known action\n", action);
283 if ((group_name = bind_and_fetch(name)) == NULL)
285 vp = Entry.attrs[attr_to_index("joinable")].values;
286 if (action == G_JOIN) {
288 printf(" No one is permitted to join \"%s\"\n", group_name);
292 if (!strcasecmp(*vp, "FALSE")) {
293 printf(" No one is permitted to join \"%s\"\n", group_name);
299 /* fill in the rest of the modification structure */
301 mods[1] = (LDAPMod *) NULL;
302 values[0] = Entry.DN;
304 mod.mod_type = "memberOfGroup";
305 mod.mod_values = values;
308 if (debug & D_GROUPS) {
309 register LDAPMod **lpp;
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);
325 if (ldap_modify_s(ld, bound_dn, mods)) {
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);
337 ldap_uncache_entry(ld, bound_dn);
341 printf(" You are now subscribed to \"%s\"\n", group_name);
344 printf(" You are no longer subscribed to \"%s\"\n", group_name);
353 bulk_load( char *group )
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];
367 printf("->bulk_load(%s)\n", group);
370 /* you lose if going through MichNet */
373 printf(" Not allowed via UM-X500 connections.\n");
378 /* fetch entries from the file containing the e-mail addresses */
379 printf("\n File to load? ");
381 fetch_buffer(s, sizeof(s), stdin);
386 if ((fp = fopen(s, "r")) == NULL) {
387 perror("bulk_load: fopen");
391 printf(" Loading group members from %s\n", s);
393 /* load them in MAX_VALUES at a time */
395 for (idx_mail = 0, idx_x500 = 0;
396 idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
397 (void) fgets(s, sizeof(s), fp);
405 values_mail[idx_mail++] = strdup(s);
407 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
408 printf(" Could not locate \"%s\" -- skipping.\n", s);
412 values_x500[idx_x500++] = strdup(Entry.DN);
416 values_mail[idx_mail] = NULL;
417 values_x500[idx_x500] = NULL;
418 count_mail = idx_mail;
419 count_x500 = idx_x500;
422 * Add the e-mail addresses.
424 if (count_mail > 0) {
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;
432 mod.mod_op = LDAP_MOD_REPLACE;
435 if (debug & D_GROUPS) {
436 register LDAPMod **lpp;
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);
450 if (ldap_modify_s(ld, group, mods))
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;
460 * Add the LDAP style names.
462 if (count_x500 > 0) {
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;
470 mod.mod_op = LDAP_MOD_REPLACE;
473 if (debug & D_GROUPS) {
474 register LDAPMod **lpp;
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);
488 if (ldap_modify_s(ld, group, mods))
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;
498 * If both counts were less than the maximum number we
499 * can handle at a time, then we are done.
501 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
509 purge_group( char *group )
513 LDAPMod mod, *mods[2];
514 char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
517 if (debug & D_TRACE) {
519 printf("->purge_group(NULL)\n");
521 printf("->purge_group(%s)\n", group);
524 if (bind_status == UD_NOT_BOUND) {
525 if (auth((char *) NULL, 1) < 0)
529 * If the user did not supply us with a name, prompt them for
532 if ((group == NULL) || (*group == '\0')) {
533 printf("Group to purge? ");
535 fetch_buffer(tmp, sizeof(tmp), stdin);
540 sprintf(dn, "cn=%s, %s", group, group_base);
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);
550 /* none of this stuff changes */
552 mods[1] = (LDAPMod *) NULL;
556 mod.mod_values = values;
557 mod.mod_type = "member";
558 mod.mod_op = LDAP_MOD_DELETE;
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.
565 vp = Entry.attrs[attr_to_index("member")].values;
568 printf(" \"%s\" has no LDAP members. There is nothing to purge.\n", group);
571 for (; *vp != NULL; vp++) {
572 char ans[BUFSIZ], *ufn, *label = "Did not find: ";
573 int len = strlen(label);
578 ufn = my_ldap_dn2ufn(*vp);
579 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
581 printf(" Purge, Keep, Replace, Abort [Keep]? ");
583 fetch_buffer(ans, sizeof(ans), stdin);
584 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
586 if (!strncasecmp(ans, "Abort", strlen(ans))) {
587 ldap_uncache_entry(ld, dn);
590 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
593 if (debug & D_GROUPS) {
594 register LDAPMod **lpp;
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);
609 if (ldap_modify_s(ld, Entry.DN, mods))
612 /* now add the replacement if requested */
613 if (!strncasecmp(ans, "Purge", strlen(ans)))
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);
619 ldap_value_free(rdns);
622 values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
623 mod.mod_op = LDAP_MOD_ADD;
625 if (debug & D_GROUPS) {
626 register LDAPMod **lpp;
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);
641 if (ldap_modify_s(ld, Entry.DN, mods))
644 ldap_value_free(rdns);
646 /* set this back to DELETE for other purges */
647 mod.mod_op = LDAP_MOD_DELETE;
650 printf(" Did not recognize that answer.\n\n");
654 ldap_uncache_entry(ld, Entry.DN);
656 printf(" No entries were purged.\n");
668 static LDAPMod *mods[2] = { &mod, NULL };
669 static char *values[MAX_VALUES];
673 printf("->tidy()\n");
676 if (bind_status == UD_NOT_BOUND) {
677 if (auth((char *) NULL, 1) < 0) {
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);
692 vp = Entry.attrs[attr_to_index("memberOfGroup")].values;
694 printf(" You have not subscribed to any groups.\n");
698 /* now, loop through these groups, deleting the bogus */
699 for ( ; *vp != NULL; vp++) {
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 );
710 if (found_one == 0) {
712 printf(" You are not a member of any invalid groups. There is nothing to tidy.\n");
716 /* delete the most heinous entries */
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))
723 ldap_uncache_entry(ld, bound_dn);
725 /* tidy up before we finish tidy_up */
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.
737 mod_addrDN( char *group, int offset )
739 char s[BUFSIZ], *new_value /* was member */, *values[2];
741 LDAPMod mod, *mods[2];
746 printf("->mod_addrDN(%s)\n", group);
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.
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);
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);
763 /* initialize the modififier type */
768 printf(" Do you want to Add, Delete, or Replace? ");
770 printf(" Do you want to Add, Delete, Replace, or Bulk load? ");
772 fetch_buffer(s, sizeof(s), stdin);
777 if (!strncasecmp(s, "add", strlen(s))) {
778 mod.mod_op = LDAP_MOD_ADD;
781 else if (!strncasecmp(s, "delete", strlen(s))) {
782 mod.mod_op = LDAP_MOD_DELETE;
785 else if (!strncasecmp(s, "replace", strlen(s))) {
786 mod.mod_op = LDAP_MOD_REPLACE;
789 else if(!strncasecmp(s, "bulk", strlen(s))) {
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);
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);
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");
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);
814 printf("%s? ", attrlist[offset].output_string);
816 fetch_buffer(s, sizeof(s), stdin);
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.
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.
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.
835 mod.mod_type = attrlist[offset].quipu_name;
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)){
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);
848 new_value = Entry.DN;
849 mod.mod_type = attrlist[offset].quipu_name;
851 else if (mod.mod_op != LDAP_MOD_DELETE) {
853 * Don't screw around with what the user has typed
854 * if they are simply trying to delete a rfc822mailbox
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
862 * note that this means a value like:
864 * first m. last@host.domain
866 * will be turned into:
868 * first.m..last@host.domain
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
874 * Don't screw up things like:
876 * "Bryan Beecher" <bryan@umich.edu>
877 * Bryan Beecher <bryan@umich.edu>
880 if (strchr(s, '<') == NULL) {
881 for (cp = s; *cp != '@'; cp++)
882 if (isspace((unsigned char)*cp))
886 strcpy(attrtype, "rfc822");
887 strcat(attrtype, attrlist[offset].quipu_name);
888 mod.mod_type = attrtype;
892 strcpy(attrtype, "rfc822");
893 strcat(attrtype, attrlist[offset].quipu_name);
894 mod.mod_type = attrtype;
897 /* fill in the rest of the ldap_mod() structure */
899 mods[1] = (LDAPMod *) NULL;
901 values[0] = new_value;
903 mod.mod_values = values;
906 if (debug & D_GROUPS) {
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",
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",
925 if (my_ldap_modify_s(ld, group, mods)) {
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",
938 ldap_uncache_entry(ld, group);
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).
945 * NOTE: WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
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
952 if (mod.mod_op == LDAP_MOD_REPLACE) {
953 if (!strncmp(mod.mod_type, "rfc822", 6))
954 mod.mod_type = mod.mod_type + 6;
956 strcpy(attrtype, "rfc822");
957 strcat(attrtype, mod.mod_type);
958 mod.mod_type = attrtype;
962 mod.mod_values = values;
963 mod.mod_op = LDAP_MOD_DELETE;
965 if (debug & D_GROUPS) {
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",
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);
982 if (my_ldap_modify_s(ld, group, mods)) {
984 * A "No such attribute" error is no big deal.
985 * We only wanted to clear the attribute anyhow.
988 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
989 if (ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
994 ldap_uncache_entry(ld, group);
996 printf(" \"%s\" has been added\n", new_value);
997 mod.mod_op = LDAP_MOD_ADD;
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);
1007 my_ldap_modify_s( LDAP *ldap, char *group, LDAPMod **mods )
1009 int was_rfc822member, rc;
1011 was_rfc822member = 0;
1013 if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
1014 mods[0]->mod_type = "mail";
1015 was_rfc822member = 1;
1018 rc = ldap_modify_s(ldap, group, mods);
1020 if (was_rfc822member)
1021 mods[0]->mod_type = "rfc822member";
1027 list_groups( char *who )
1030 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1031 char *work_area[MAX_NUM_NAMES];
1037 if (debug & D_TRACE) {
1039 printf("->list_groups(NULL)\n");
1041 printf("->list_groups(%s)\n", who);
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
1050 * Once we know who to modify, be sure that they exist, and
1051 * parse out their DN.
1054 printf(" List groups belonging to whose entry? ");
1056 fetch_buffer(name, sizeof(name), stdin);
1057 if (name[0] != '\0')
1062 if ((mp = find(who, TRUE)) == NULL) {
1063 (void) ldap_msgfree(mp);
1064 printf(" Could not locate \"%s\" in the Directory.\n", who);
1067 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1069 rdns = ldap_explode_dn(dn, TRUE);
1071 printf("\n Listing groups belonging to \"%s\"\n", *rdns);
1073 /* lookup the groups belonging to this person */
1074 sprintf(filter, "owner=%s", 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);
1085 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1086 ldap_perror(ld, "ldap_count_entries");
1087 ldap_value_free(rdns);
1091 printf(" %s owns no groups in this portion of the Directory.\n", *rdns);
1092 ldap_value_free(rdns);
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++)
1101 ldap_value_free(rdns);
1106 bind_and_fetch( char *name )
1109 char tmp[MED_BUF_SIZE];
1112 if (debug & D_TRACE) {
1114 printf("->bind_and_fetch(NULL)\n");
1116 printf("->bind_and_fetch(%s)\n", name);
1119 if (bind_status == UD_NOT_BOUND) {
1120 if (auth((char *) NULL, 1) < 0)
1125 * If the user did not supply us with a name, prompt them for
1128 if ((name == NULL) || (*name == '\0')) {
1131 fetch_buffer(tmp, sizeof(tmp), stdin);
1136 /* remove quotes, dots, and underscores. */
1137 name = strip_ignore_chars(name);
1140 if (debug & D_GROUPS)
1141 printf("Group name = (%s)\n", name);
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);
1153 if (debug & D_GROUPS)
1154 printf("Group DN = (%s)\n", Entry.DN);
1156 return(strdup(Entry.DN));
1160 list_memberships( char *who )
1163 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1164 char *work_area[MAX_NUM_NAMES];
1170 if (debug & D_TRACE) {
1172 printf("->list_memberships(NULL)\n");
1174 printf("->list_memberships(%s)\n", who);
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
1183 * Once we know who to modify, be sure that they exist, and
1184 * parse out their DN.
1187 printf(" List memberships containing whose entry? ");
1189 fetch_buffer(name, sizeof(name), stdin);
1190 if (name[0] != '\0')
1195 if ((mp = find(who, TRUE)) == NULL) {
1196 (void) ldap_msgfree(mp);
1197 printf(" Could not locate \"%s\" in the Directory.\n", who);
1201 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1202 rdns = ldap_explode_dn(dn, TRUE);
1204 printf("\n Listing memberships of \"%s\"\n", *rdns);
1206 /* lookup the groups belonging to this person */
1207 sprintf(filter, "member=%s", dn);
1209 search_attrs[0] = "cn";
1210 search_attrs[1] = NULL;
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");
1217 ldap_value_free(rdns);
1220 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1221 ldap_perror(ld, "ldap_count_entries");
1223 ldap_value_free(rdns);
1227 printf(" %s is not a member of any groups in this portion of the Directory.\n", *rdns);
1229 ldap_value_free(rdns);
1233 printf(" %s is a member of %d groups.\n\n", *rdns, rc);
1236 * print_list fills in the char * array starting at 1, not 0
1238 print_list(mp, work_area, &rc);
1239 for (i = 1; work_area[i] != NULL; i++)
1242 ldap_value_free(rdns);