3 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
19 #include <ac/string.h>
22 #include <ac/unistd.h>
31 #include "ldap_defaults.h"
34 static char * bind_and_fetch(char *name);
38 add_group( char *name )
40 int idx = 0, prompt = 0;
41 char tmp[BUFSIZ], dn[BUFSIZ];
42 static LDAPMod *attrs[9];
43 LDAPMod init_rdn, init_owner, init_domain,
44 init_errors, init_request, init_joinable;
45 char *init_rdn_value[2], *init_owner_value[2], *init_domain_value[2],
46 *init_errors_value[MAX_VALUES], *init_joinable_value[2],
47 *init_request_value[MAX_VALUES];
50 if (debug & D_TRACE) {
52 printf("->add_group(NULL)\n");
54 printf("->add_group(%s)\n", name);
58 if (bind_status == UD_NOT_BOUND) {
59 if (auth((char *) NULL, 1) < 0) {
65 * If the user did not supply us with a name, prompt them for
68 if ((name == NULL) || (*name == '\0') || !strcasecmp(name, "group")) {
70 printf(" Group to create? ");
72 fetch_buffer(tmp, sizeof(tmp), stdin);
78 /* remove quotes, dots, and underscores. */
79 name = strip_ignore_chars(name);
82 if (isauniqname(name)) {
83 printf(" '%s' could be confused with a U-M uniqname.\n", name);
84 printf(" You can create the group, but you need to make sure that\n");
85 printf(" you reserve the uniqname for use as your groupname\n\n");
86 printf(" Are you sure that you want to do this? ");
88 fetch_buffer(tmp, sizeof(tmp), stdin);
89 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
91 printf("\n Be sure to contact your uniqname administrator to reserve\n");
92 printf(" the uniqname '%s' for use as your group name.\n", name);
95 sprintf(dn, "cn=%s, %s", name, group_base);
98 * Make sure that this group does not already exist.
100 if (vrfy(dn) == TRUE) {
101 printf(" The group \"%s\" already exists.\n", name);
106 * Take the easy way out: Fill in some reasonable values for
107 * the most important fields, and make the user use the modify
108 * command to change them, or to give values to other fields.
110 init_rdn_value[0] = name;
111 init_rdn_value[1] = NULL;
112 init_rdn.mod_op = LDAP_MOD_ADD;
113 init_rdn.mod_type = "cn";
114 init_rdn.mod_values = init_rdn_value;
115 attrs[idx++] = &init_rdn;
117 init_owner_value[0] = bound_dn;
118 init_owner_value[1] = NULL;
119 init_owner.mod_op = LDAP_MOD_ADD;
120 init_owner.mod_type = "owner";
121 init_owner.mod_values = init_owner_value;
122 attrs[idx++] = &init_owner;
125 init_domain_value[0] = "umich.edu";
127 init_domain_value[0] = ".";
129 init_domain_value[1] = NULL;
130 init_domain.mod_op = LDAP_MOD_ADD;
131 init_domain.mod_type = "associatedDomain";
132 init_domain.mod_values = init_domain_value;
133 attrs[idx++] = &init_domain;
135 init_errors_value[0] = bound_dn;
136 init_errors_value[1] = NULL;
137 init_errors.mod_op = LDAP_MOD_ADD;
138 init_errors.mod_type = "ErrorsTo";
139 init_errors.mod_values = init_errors_value;
140 attrs[idx++] = &init_errors;
142 init_request_value[0] = bound_dn;
143 init_request_value[1] = NULL;
144 init_request.mod_op = LDAP_MOD_ADD;
145 init_request.mod_type = "RequestsTo";
146 init_request.mod_values = init_request_value;
147 attrs[idx++] = &init_request;
149 init_joinable_value[0] = "FALSE";
150 init_joinable_value[1] = NULL;
151 init_joinable.mod_op = LDAP_MOD_ADD;
152 init_joinable.mod_type = "joinable";
153 init_joinable.mod_values = init_joinable_value;
154 attrs[idx++] = &init_joinable;
156 /* end it with a NULL */
160 if (debug & D_GROUPS) {
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 LDAP 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);
203 remove_group( char *name )
205 char *dn, tmp[BUFSIZ];
208 if (debug & D_TRACE) {
210 printf("->remove_group(NULL)\n");
212 printf("->remove_group(%s)\n", name);
215 if ((dn = bind_and_fetch(name)) == NULL)
218 printf("\n The entry\n '%s'\n will be permanently removed from", dn);
219 printf(" the Directory.\n Are you absolutely sure that you want to" );
220 printf(" remove this entire group? ");
222 fetch_buffer(tmp, sizeof(tmp), stdin);
223 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
227 * Now remove this from the LDAP Directory.
229 if (ldap_delete_s(ld, dn) != 0) {
231 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
232 if (ld_errno == LDAP_INSUFFICIENT_ACCESS)
233 printf(" You do not own the entry\n\t\"%s\".\n", dn);
235 ldap_perror(ld, " ldap_delete_s");
236 printf(" Group not removed.\n");
240 ldap_uncache_entry(ld, dn);
244 printf(" The group has been removed.\n");
246 printf(" The group \"%s\" has been removed.\n", name);
253 x_group( int action, char *name )
256 char *values[2], *group_name;
257 LDAPMod mod, *mods[2];
258 static char *actions[] = { "join", "resign from", NULL };
261 if (debug & D_TRACE) {
263 printf("->x_group(%d, NULL)\n", action);
265 printf("->x_group(%d, %s)\n", action, name);
269 /* the action desired sets the opcode to use */
272 mod.mod_op = LDAP_MOD_ADD;
275 mod.mod_op = LDAP_MOD_DELETE;
278 printf("x_group: %d is not a known action\n", action);
281 if ((group_name = bind_and_fetch(name)) == NULL)
283 vp = Entry.attrs[attr_to_index("joinable")].values;
284 if (action == G_JOIN) {
286 printf(" No one is permitted to join \"%s\"\n", group_name);
290 if (!strcasecmp(*vp, "FALSE")) {
291 printf(" No one is permitted to join \"%s\"\n", group_name);
297 /* fill in the rest of the modification structure */
299 mods[1] = (LDAPMod *) NULL;
300 values[0] = Entry.DN;
302 mod.mod_type = "memberOfGroup";
303 mod.mod_values = values;
306 if (debug & D_GROUPS) {
307 register LDAPMod **lpp;
310 printf(" About to call ldap_modify_s()\n");
311 printf(" ld = 0x%x\n", ld);
312 printf(" dn = [%s]\n", bound_dn);
313 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
314 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
315 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
316 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
317 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
323 if (ldap_modify_s(ld, bound_dn, mods)) {
325 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
326 if ((action == G_JOIN) && (ld_errno == LDAP_TYPE_OR_VALUE_EXISTS))
327 printf(" You are already subscribed to \"%s\"\n", group_name);
328 else if ((action == G_RESIGN) && (ld_errno == LDAP_NO_SUCH_ATTRIBUTE))
329 printf(" You are not subscribed to \"%s\"\n", group_name);
335 ldap_uncache_entry(ld, bound_dn);
339 printf(" You are now subscribed to \"%s\"\n", group_name);
342 printf(" You are no longer subscribed to \"%s\"\n", group_name);
351 bulk_load( char *group )
353 register int idx_mail, idx_x500;
354 register int count_mail, count_x500;
355 char *values_mail[MAX_VALUES + 1], *values_x500[MAX_VALUES + 1];
356 int added_mail_entries = FALSE, added_x500_entries = FALSE;
357 char s[MED_BUF_SIZE];
358 LDAPMod mod, *mods[2];
365 printf("->bulk_load(%s)\n", group);
368 /* you lose if going through MichNet */
371 printf(" Not allowed via UM-X500 connections.\n");
376 /* fetch entries from the file containing the e-mail addresses */
377 printf("\n File to load? ");
379 fetch_buffer(s, sizeof(s), stdin);
384 if ((fp = fopen(s, "r")) == NULL) {
385 perror("bulk_load: fopen");
389 printf(" Loading group members from %s\n", s);
391 /* load them in MAX_VALUES at a time */
393 for (idx_mail = 0, idx_x500 = 0;
394 idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
395 (void) fgets(s, sizeof(s), fp);
403 values_mail[idx_mail++] = strdup(s);
405 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
406 printf(" Could not locate \"%s\" -- skipping.\n", s);
410 values_x500[idx_x500++] = strdup(Entry.DN);
414 values_mail[idx_mail] = NULL;
415 values_x500[idx_x500] = NULL;
416 count_mail = idx_mail;
417 count_x500 = idx_x500;
420 * Add the e-mail addresses.
422 if (count_mail > 0) {
424 mods[1] = (LDAPMod *) NULL;
425 mod.mod_type = "mail";
426 mod.mod_values = values_mail;
427 if (added_mail_entries)
428 mod.mod_op = LDAP_MOD_ADD;
430 mod.mod_op = LDAP_MOD_REPLACE;
433 if (debug & D_GROUPS) {
434 register LDAPMod **lpp;
437 printf(" About to call ldap_modify_s()\n");
438 printf(" ld = 0x%x\n", ld);
439 printf(" dn = [%s]\n", group);
440 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
441 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
442 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
443 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
444 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
448 if (ldap_modify_s(ld, group, mods))
450 for (idx_mail--; idx_mail >= 0; idx_mail--)
451 Free(values_mail[idx_mail]);
452 ldap_uncache_entry(ld, group);
453 added_mail_entries = TRUE;
458 * Add the LDAP style names.
460 if (count_x500 > 0) {
462 mods[1] = (LDAPMod *) NULL;
463 mod.mod_type = "member";
464 mod.mod_values = values_x500;
465 if (added_x500_entries)
466 mod.mod_op = LDAP_MOD_ADD;
468 mod.mod_op = LDAP_MOD_REPLACE;
471 if (debug & D_GROUPS) {
472 register LDAPMod **lpp;
475 printf(" About to call ldap_modify_s()\n");
476 printf(" ld = 0x%x\n", ld);
477 printf(" dn = [%s]\n", group);
478 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
479 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
480 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
481 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
482 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
486 if (ldap_modify_s(ld, group, mods))
488 for (idx_x500--; idx_x500 >= 0; idx_x500--)
489 Free(values_x500[idx_x500]);
490 ldap_uncache_entry(ld, group);
491 added_x500_entries = TRUE;
496 * If both counts were less than the maximum number we
497 * can handle at a time, then we are done.
499 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
507 purge_group( char *group )
511 LDAPMod mod, *mods[2];
512 char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
515 if (debug & D_TRACE) {
517 printf("->purge_group(NULL)\n");
519 printf("->purge_group(%s)\n", group);
522 if (bind_status == UD_NOT_BOUND) {
523 if (auth((char *) NULL, 1) < 0)
527 * If the user did not supply us with a name, prompt them for
530 if ((group == NULL) || (*group == '\0')) {
531 printf("Group to purge? ");
533 fetch_buffer(tmp, sizeof(tmp), stdin);
538 sprintf(dn, "cn=%s, %s", group, group_base);
540 /* make sure the group in question exists */
541 if ((lm = find(group, FALSE)) == (LDAPMessage *) NULL) {
542 printf(" Could not locate group \"%s\"\n", group);
548 /* none of this stuff changes */
550 mods[1] = (LDAPMod *) NULL;
554 mod.mod_values = values;
555 mod.mod_type = "member";
556 mod.mod_op = LDAP_MOD_DELETE;
559 * Now cycle through all of the names in the "members" part of the
560 * group (but not the e-mail address part). Lookup each one, and
561 * if it isn't found, let the user know so s/he can delete it.
563 vp = Entry.attrs[attr_to_index("member")].values;
566 printf(" \"%s\" has no LDAP members. There is nothing to purge.\n", group);
569 for (; *vp != NULL; vp++) {
570 char ans[BUFSIZ], *ufn, *label = "Did not find: ";
571 int len = strlen(label);
576 ufn = my_ldap_dn2ufn(*vp);
577 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
579 printf(" Purge, Keep, Replace, Abort [Keep]? ");
581 fetch_buffer(ans, sizeof(ans), stdin);
582 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
584 if (!strncasecmp(ans, "Abort", strlen(ans))) {
585 ldap_uncache_entry(ld, dn);
588 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
591 if (debug & D_GROUPS) {
592 register LDAPMod **lpp;
595 printf(" About to call ldap_modify_s()\n");
596 printf(" ld = 0x%x\n", ld);
597 printf(" dn = [%s]\n", Entry.DN);
598 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
599 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
600 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
601 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
602 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
607 if (ldap_modify_s(ld, Entry.DN, mods))
610 /* now add the replacement if requested */
611 if (!strncasecmp(ans, "Purge", strlen(ans)))
613 rdns = ldap_explode_dn(*vp, TRUE);
614 if ((lm = find(*rdns, FALSE)) == NULL) {
615 printf(" Could not find a replacement for %s; purged only.\n", *rdns);
617 ldap_value_free(rdns);
620 values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
621 mod.mod_op = LDAP_MOD_ADD;
623 if (debug & D_GROUPS) {
624 register LDAPMod **lpp;
627 printf(" About to call ldap_modify_s()\n");
628 printf(" ld = 0x%x\n", ld);
629 printf(" dn = [%s]\n", Entry.DN);
630 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
631 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
632 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
633 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
634 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
639 if (ldap_modify_s(ld, Entry.DN, mods))
642 ldap_value_free(rdns);
644 /* set this back to DELETE for other purges */
645 mod.mod_op = LDAP_MOD_DELETE;
648 printf(" Did not recognize that answer.\n\n");
652 ldap_uncache_entry(ld, Entry.DN);
654 printf(" No entries were purged.\n");
666 static LDAPMod *mods[2] = { &mod, NULL };
667 static char *values[MAX_VALUES];
671 printf("->tidy()\n");
674 if (bind_status == UD_NOT_BOUND) {
675 if (auth((char *) NULL, 1) < 0) {
680 /* lookup the user, and see to which groups he has subscribed */
681 vp = ldap_explode_dn(bound_dn, TRUE);
682 if ((lm = find(*vp, TRUE)) == (LDAPMessage *) NULL) {
683 printf(" Could not locate \"%s\"\n", *vp);
690 vp = Entry.attrs[attr_to_index("memberOfGroup")].values;
692 printf(" You have not subscribed to any groups.\n");
696 /* now, loop through these groups, deleting the bogus */
697 for ( ; *vp != NULL; vp++) {
701 printf(" \"%s\" is not a valid group name.\n", *vp);
702 values[i++] = strdup(*vp);
703 if ( i >= MAX_VALUES ) {
704 printf( " At most %d invalid groups can be removed at one time; skipping the rest.\n", MAX_VALUES );
708 if (found_one == 0) {
710 printf(" You are not a member of any invalid groups. There is nothing to tidy.\n");
714 /* delete the most heinous entries */
716 mod.mod_values = values;
717 mod.mod_op = LDAP_MOD_DELETE;
718 mod.mod_type = "memberOfGroup";
719 if (ldap_modify_s(ld, bound_dn, mods))
721 ldap_uncache_entry(ld, bound_dn);
723 /* tidy up before we finish tidy_up */
730 * This routine is used to modify lists that can contain either Distinguished
731 * Names or e-mail addresses. This includes things like group members,
732 * the errors-to field in groups, and so on.
735 mod_addrDN( char *group, int offset )
737 char s[BUFSIZ], *new_value /* was member */, *values[2];
739 LDAPMod mod, *mods[2];
744 printf("->mod_addrDN(%s)\n", group);
747 * At this point the user can indicate that he wishes to add values
748 * to the attribute, delete values from the attribute, or replace the
749 * current list of values with a new list. The first two cases
750 * are very straight-forward, but the last case requires a little
751 * extra care and work.
756 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 format("There are four options available at this point. You may: Add one or more additional values; Delete one or more existing values; Replace the entire list of values with a new list entered interactively; or Bulk load a new list of values from a file, overwriting the existing list.\n", 75, 2);
761 /* initialize the modififier type */
766 printf(" Do you want to Add, Delete, or Replace? ");
768 printf(" Do you want to Add, Delete, Replace, or Bulk load? ");
770 fetch_buffer(s, sizeof(s), stdin);
775 if (!strncasecmp(s, "add", strlen(s))) {
776 mod.mod_op = LDAP_MOD_ADD;
779 else if (!strncasecmp(s, "delete", strlen(s))) {
780 mod.mod_op = LDAP_MOD_DELETE;
783 else if (!strncasecmp(s, "replace", strlen(s))) {
784 mod.mod_op = LDAP_MOD_REPLACE;
787 else if(!strncasecmp(s, "bulk", strlen(s))) {
794 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 format("Did not recognize that response. Please use 'A' to add, 'D' to delete, 'R' to replace the entire list with a new list, or 'B' to bulk load a new list from a file\n", 75, 2);
799 if (mod.mod_op == LDAP_MOD_REPLACE) {
800 if ( verbose && !confirm_action( "The entire existing list will be overwritten with the new values you are about to enter." )) {
801 printf("\n Modification halted.\n");
807 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);
812 printf("%s? ", attrlist[offset].output_string);
814 fetch_buffer(s, sizeof(s), stdin);
819 * If the string the user has just typed has an @-sign in it,
820 * then we assume it is an e-mail address. In this case, we
821 * just store away whatever it is they have typed.
823 * If the string had no @-sign, then we look in the Directory,
824 * make sure it exists, and if it does, we add that.
826 * If the string begins with a comma, then strip off the
827 * comma, and pass it along to the LDAP server. This is
828 * the way one can force ud to accept a name. Handy for
829 * debugging purposes.
833 mod.mod_type = attrlist[offset].quipu_name;
835 else if (strchr(s, '@') == NULL) {
836 if ((mp = find(s, FALSE)) == (LDAPMessage *) NULL) {
837 printf(" Could not find \"%s\"\n", s);
838 if (verbose && (mod.mod_op == LDAP_MOD_DELETE)){
840 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);
846 new_value = Entry.DN;
847 mod.mod_type = attrlist[offset].quipu_name;
849 else if (mod.mod_op != LDAP_MOD_DELETE) {
851 * Don't screw around with what the user has typed
852 * if they are simply trying to delete a rfc822mailbox
855 * spaces on the left hand side of the e-mail
856 * address are bad news - we know that there
857 * must be a @-sign in the string, or else we
860 * note that this means a value like:
862 * first m. last@host.domain
864 * will be turned into:
866 * first.m..last@host.domain
868 * and the mailer will need to do the right thing
869 * with this; alternatively we could add code that
870 * collapsed multiple dots into a single dot
872 * Don't screw up things like:
874 * "Bryan Beecher" <bryan@umich.edu>
875 * Bryan Beecher <bryan@umich.edu>
878 if (strchr(s, '<') == NULL) {
879 for (cp = s; *cp != '@'; cp++)
880 if (isspace((unsigned char)*cp))
884 strcpy(attrtype, "rfc822");
885 strcat(attrtype, attrlist[offset].quipu_name);
886 mod.mod_type = attrtype;
890 strcpy(attrtype, "rfc822");
891 strcat(attrtype, attrlist[offset].quipu_name);
892 mod.mod_type = attrtype;
895 /* fill in the rest of the ldap_mod() structure */
897 mods[1] = (LDAPMod *) NULL;
899 values[0] = new_value;
901 mod.mod_values = values;
904 if (debug & D_GROUPS) {
908 printf(" About to call ldap_modify_s()\n");
909 printf(" ld = 0x%x\n", ld);
910 printf(" dn = [%s]\n", group);
911 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
912 printf(" mods[%1d] code = %1d\n",
914 printf(" mods[%1d] type = %s\n",
915 i, (*lpp)->mod_type);
916 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
917 printf(" mods[%1d] v[%1d] = %s\n",
923 if (my_ldap_modify_s(ld, group, mods)) {
925 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
926 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE) {
927 printf(" Could not locate value \"%s\"\n",
936 ldap_uncache_entry(ld, group);
939 * If the operation was REPLACE, we now need to "zero out" the
940 * other "half" of the list (e.g., user specified an e-mail
941 * address; now we need to clear the DN part of the list).
943 * NOTE: WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
945 * Also, change the opcode to LDAP_MOD_ADD and give the user an
946 * opportunity to add additional members to the group. We
947 * only take this branch the very first time during a REPLACE
950 if (mod.mod_op == LDAP_MOD_REPLACE) {
951 if (!strncmp(mod.mod_type, "rfc822", 6))
952 mod.mod_type = mod.mod_type + 6;
954 strcpy(attrtype, "rfc822");
955 strcat(attrtype, mod.mod_type);
956 mod.mod_type = attrtype;
960 mod.mod_values = values;
961 mod.mod_op = LDAP_MOD_DELETE;
963 if (debug & D_GROUPS) {
967 printf(" About to call ldap_modify_s()\n");
968 printf(" ld = 0x%x\n", ld);
969 printf(" dn = [%s]\n", group);
970 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
971 printf(" mods[%1d] code = %1d\n",
973 printf(" mods[%1d] type = %s\n",
974 i, (*lpp)->mod_type);
975 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
976 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
980 if (my_ldap_modify_s(ld, group, mods)) {
982 * A "No such attribute" error is no big deal.
983 * We only wanted to clear the attribute anyhow.
986 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
987 if (ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
992 ldap_uncache_entry(ld, group);
994 printf(" \"%s\" has been added\n", new_value);
995 mod.mod_op = LDAP_MOD_ADD;
997 else if (verbose && (mod.mod_op == LDAP_MOD_ADD))
998 printf(" \"%s\" has been added\n", new_value);
999 else if (verbose && (mod.mod_op == LDAP_MOD_DELETE))
1000 printf(" \"%s\" has been removed\n", new_value);
1005 my_ldap_modify_s( LDAP *ldap, char *group, LDAPMod **mods )
1007 int was_rfc822member, rc;
1009 was_rfc822member = 0;
1011 if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
1012 mods[0]->mod_type = "mail";
1013 was_rfc822member = 1;
1016 rc = ldap_modify_s(ldap, group, mods);
1018 if (was_rfc822member)
1019 mods[0]->mod_type = "rfc822member";
1025 list_groups( char *who )
1028 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1029 char *work_area[MAX_NUM_NAMES];
1035 if (debug & D_TRACE) {
1037 printf("->list_groups(NULL)\n");
1039 printf("->list_groups(%s)\n", who);
1043 * First, decide what entry we are going to list. If the
1044 * user has not included a name on the list command line,
1045 * we will use the person who was last looked up with a find
1048 * Once we know who to modify, be sure that they exist, and
1049 * parse out their DN.
1052 printf(" List groups belonging to whose entry? ");
1054 fetch_buffer(name, sizeof(name), stdin);
1055 if (name[0] != '\0')
1060 if ((mp = find(who, TRUE)) == NULL) {
1061 (void) ldap_msgfree(mp);
1062 printf(" Could not locate \"%s\" in the Directory.\n", who);
1065 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1067 rdns = ldap_explode_dn(dn, TRUE);
1069 printf("\n Listing groups belonging to \"%s\"\n", *rdns);
1071 /* lookup the groups belonging to this person */
1072 sprintf(filter, "owner=%s", dn);
1074 search_attrs[0] = "cn";
1075 search_attrs[1] = NULL;
1076 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1077 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1078 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1079 ldap_perror(ld, "ldap_search_s");
1080 ldap_value_free(rdns);
1083 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1084 ldap_perror(ld, "ldap_count_entries");
1085 ldap_value_free(rdns);
1089 printf(" %s owns no groups in this portion of the Directory.\n", *rdns);
1090 ldap_value_free(rdns);
1094 printf(" %s owns %d groups.\n\n", *rdns, rc);
1095 print_list(mp, work_area, &rc);
1096 for (i = 1; work_area[i] != NULL; i++)
1099 ldap_value_free(rdns);
1104 bind_and_fetch( char *name )
1107 char tmp[MED_BUF_SIZE];
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));
1158 list_memberships( char *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);