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 group '%s' will be permanently removed from\n",
220 printf(" the Directory. Are you absolutely sure that you want to\n" ); 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 group \"%s\".\n", name);
235 ldap_perror(ld, " ldap_delete_s");
236 printf(" Group not removed.\n");
240 ldap_uncache_entry(ld, dn);
243 printf(" The group has been removed.\n");
245 printf(" The group \"%s\" has been removed.\n", name);
251 x_group( int action, char *name )
254 char *values[2], *group_name;
255 LDAPMod mod, *mods[2];
256 static char *actions[] = { "join", "resign from", NULL };
259 if (debug & D_TRACE) {
261 printf("->x_group(%d, NULL)\n", action);
263 printf("->x_group(%d, %s)\n", action, name);
267 /* the action desired sets the opcode to use */
270 mod.mod_op = LDAP_MOD_ADD;
273 mod.mod_op = LDAP_MOD_DELETE;
276 printf("x_group: %d is not a known action\n", action);
279 if ((group_name = bind_and_fetch(name)) == NULL)
281 vp = Entry.attrs[attr_to_index("joinable")].values;
282 if (action == G_JOIN) {
284 printf(" No one is permitted to join \"%s\"\n", group_name);
288 if (!strcasecmp(*vp, "FALSE")) {
289 printf(" No one is permitted to join \"%s\"\n", group_name);
295 /* fill in the rest of the modification structure */
297 mods[1] = (LDAPMod *) NULL;
298 values[0] = Entry.DN;
300 mod.mod_type = "memberOfGroup";
301 mod.mod_values = values;
304 if (debug & D_GROUPS) {
305 register LDAPMod **lpp;
308 printf(" About to call ldap_modify_s()\n");
309 printf(" ld = 0x%x\n", ld);
310 printf(" dn = [%s]\n", bound_dn);
311 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
312 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
313 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
314 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
315 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
321 if (ldap_modify_s(ld, bound_dn, mods)) {
323 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
324 if ((action == G_JOIN) && (ld_errno == LDAP_TYPE_OR_VALUE_EXISTS))
325 printf(" You are already subscribed to \"%s\"\n", group_name);
326 else if ((action == G_RESIGN) && (ld_errno == LDAP_NO_SUCH_ATTRIBUTE))
327 printf(" You are not subscribed to \"%s\"\n", group_name);
333 ldap_uncache_entry(ld, bound_dn);
337 printf(" You are now subscribed to \"%s\"\n", group_name);
340 printf(" You are no longer subscribed to \"%s\"\n", group_name);
349 bulk_load( char *group )
351 register int idx_mail, idx_x500;
352 register int count_mail, count_x500;
353 char *values_mail[MAX_VALUES + 1], *values_x500[MAX_VALUES + 1];
354 int added_mail_entries = FALSE, added_x500_entries = FALSE;
355 char s[MED_BUF_SIZE];
356 LDAPMod mod, *mods[2];
363 printf("->bulk_load(%s)\n", group);
366 /* you lose if going through MichNet */
369 printf(" Not allowed via UM-X500 connections.\n");
374 /* fetch entries from the file containing the e-mail addresses */
375 printf("\n File to load? ");
377 fetch_buffer(s, sizeof(s), stdin);
382 if ((fp = fopen(s, "r")) == NULL) {
383 perror("bulk_load: fopen");
387 printf(" Loading group members from %s\n", s);
389 /* load them in MAX_VALUES at a time */
391 for (idx_mail = 0, idx_x500 = 0;
392 idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
393 (void) fgets(s, sizeof(s), fp);
401 values_mail[idx_mail++] = strdup(s);
403 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
404 printf(" Could not locate \"%s\" -- skipping.\n", s);
408 values_x500[idx_x500++] = strdup(Entry.DN);
412 values_mail[idx_mail] = NULL;
413 values_x500[idx_x500] = NULL;
414 count_mail = idx_mail;
415 count_x500 = idx_x500;
418 * Add the e-mail addresses.
420 if (count_mail > 0) {
422 mods[1] = (LDAPMod *) NULL;
423 mod.mod_type = "mail";
424 mod.mod_values = values_mail;
425 if (added_mail_entries)
426 mod.mod_op = LDAP_MOD_ADD;
428 mod.mod_op = LDAP_MOD_REPLACE;
431 if (debug & D_GROUPS) {
432 register LDAPMod **lpp;
435 printf(" About to call ldap_modify_s()\n");
436 printf(" ld = 0x%x\n", ld);
437 printf(" dn = [%s]\n", group);
438 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
439 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
440 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
441 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
442 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
446 if (ldap_modify_s(ld, group, mods))
448 for (idx_mail--; idx_mail >= 0; idx_mail--)
449 Free(values_mail[idx_mail]);
450 ldap_uncache_entry(ld, group);
451 added_mail_entries = TRUE;
456 * Add the LDAP style names.
458 if (count_x500 > 0) {
460 mods[1] = (LDAPMod *) NULL;
461 mod.mod_type = "member";
462 mod.mod_values = values_x500;
463 if (added_x500_entries)
464 mod.mod_op = LDAP_MOD_ADD;
466 mod.mod_op = LDAP_MOD_REPLACE;
469 if (debug & D_GROUPS) {
470 register LDAPMod **lpp;
473 printf(" About to call ldap_modify_s()\n");
474 printf(" ld = 0x%x\n", ld);
475 printf(" dn = [%s]\n", group);
476 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
477 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
478 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
479 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
480 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
484 if (ldap_modify_s(ld, group, mods))
486 for (idx_x500--; idx_x500 >= 0; idx_x500--)
487 Free(values_x500[idx_x500]);
488 ldap_uncache_entry(ld, group);
489 added_x500_entries = TRUE;
494 * If both counts were less than the maximum number we
495 * can handle at a time, then we are done.
497 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
505 purge_group( char *group )
509 LDAPMod mod, *mods[2];
510 char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
513 if (debug & D_TRACE) {
515 printf("->purge_group(NULL)\n");
517 printf("->purge_group(%s)\n", group);
520 if (bind_status == UD_NOT_BOUND) {
521 if (auth((char *) NULL, 1) < 0)
525 * If the user did not supply us with a name, prompt them for
528 if ((group == NULL) || (*group == '\0')) {
529 printf("Group to purge? ");
531 fetch_buffer(tmp, sizeof(tmp), stdin);
536 sprintf(dn, "cn=%s, %s", group, group_base);
538 /* make sure the group in question exists */
539 if ((lm = find(group, FALSE)) == (LDAPMessage *) NULL) {
540 printf(" Could not locate group \"%s\"\n", group);
546 /* none of this stuff changes */
548 mods[1] = (LDAPMod *) NULL;
552 mod.mod_values = values;
553 mod.mod_type = "member";
554 mod.mod_op = LDAP_MOD_DELETE;
557 * Now cycle through all of the names in the "members" part of the
558 * group (but not the e-mail address part). Lookup each one, and
559 * if it isn't found, let the user know so s/he can delete it.
561 vp = Entry.attrs[attr_to_index("member")].values;
564 printf(" \"%s\" has no LDAP members. There is nothing to purge.\n", group);
567 for (; *vp != NULL; vp++) {
568 char ans[BUFSIZ], *ufn, *label = "Did not find: ";
569 int len = strlen(label);
574 ufn = my_ldap_dn2ufn(*vp);
575 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
577 printf(" Purge, Keep, Replace, Abort [Keep]? ");
579 fetch_buffer(ans, sizeof(ans), stdin);
580 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
582 if (!strncasecmp(ans, "Abort", strlen(ans))) {
583 ldap_uncache_entry(ld, dn);
586 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
589 if (debug & D_GROUPS) {
590 register LDAPMod **lpp;
593 printf(" About to call ldap_modify_s()\n");
594 printf(" ld = 0x%x\n", ld);
595 printf(" dn = [%s]\n", Entry.DN);
596 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
597 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
598 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
599 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
600 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
605 if (ldap_modify_s(ld, Entry.DN, mods))
608 /* now add the replacement if requested */
609 if (!strncasecmp(ans, "Purge", strlen(ans)))
611 rdns = ldap_explode_dn(*vp, TRUE);
612 if ((lm = find(*rdns, FALSE)) == NULL) {
613 printf(" Could not find a replacement for %s; purged only.\n", *rdns);
615 ldap_value_free(rdns);
618 values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
619 mod.mod_op = LDAP_MOD_ADD;
621 if (debug & D_GROUPS) {
622 register LDAPMod **lpp;
625 printf(" About to call ldap_modify_s()\n");
626 printf(" ld = 0x%x\n", ld);
627 printf(" dn = [%s]\n", Entry.DN);
628 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
629 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
630 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
631 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
632 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
637 if (ldap_modify_s(ld, Entry.DN, mods))
640 ldap_value_free(rdns);
642 /* set this back to DELETE for other purges */
643 mod.mod_op = LDAP_MOD_DELETE;
646 printf(" Did not recognize that answer.\n\n");
650 ldap_uncache_entry(ld, Entry.DN);
652 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.
733 mod_addrDN( char *group, int offset )
735 char s[BUFSIZ], *new_value /* was member */, *values[2];
737 LDAPMod mod, *mods[2];
742 printf("->mod_addrDN(%s)\n", group);
745 * At this point the user can indicate that he wishes to add values
746 * to the attribute, delete values from the attribute, or replace the
747 * current list of values with a new list. The first two cases
748 * are very straight-forward, but the last case requires a little
749 * extra care and work.
754 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);
756 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);
759 /* initialize the modififier type */
764 printf(" Do you want to Add, Delete, or Replace? ");
766 printf(" Do you want to Add, Delete, Replace, or Bulk load? ");
768 fetch_buffer(s, sizeof(s), stdin);
773 if (!strncasecmp(s, "add", strlen(s))) {
774 mod.mod_op = LDAP_MOD_ADD;
777 else if (!strncasecmp(s, "delete", strlen(s))) {
778 mod.mod_op = LDAP_MOD_DELETE;
781 else if (!strncasecmp(s, "replace", strlen(s))) {
782 mod.mod_op = LDAP_MOD_REPLACE;
785 else if(!strncasecmp(s, "bulk", strlen(s))) {
792 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);
794 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);
797 if (mod.mod_op == LDAP_MOD_REPLACE) {
798 if ( verbose && !confirm_action( "The entire existing list will be overwritten with the new values you are about to enter." )) {
799 printf("\n Modification halted.\n");
805 format("Values may be specified as a name (which is then looked up in the LDAP Directory) or as a domain-style (i.e., user@domain) e-mail address. Simply hit the RETURN key at the prompt when finished.\n", 75, 2);
810 printf("%s? ", attrlist[offset].output_string);
812 fetch_buffer(s, sizeof(s), stdin);
817 * If the string the user has just typed has an @-sign in it,
818 * then we assume it is an e-mail address. In this case, we
819 * just store away whatever it is they have typed.
821 * If the string had no @-sign, then we look in the Directory,
822 * make sure it exists, and if it does, we add that.
824 * If the string begins with a comma, then strip off the
825 * comma, and pass it along to the LDAP server. This is
826 * the way one can force ud to accept a name. Handy for
827 * debugging purposes.
831 mod.mod_type = attrlist[offset].quipu_name;
833 else if (strchr(s, '@') == NULL) {
834 if ((mp = find(s, FALSE)) == (LDAPMessage *) NULL) {
835 printf(" Could not find \"%s\"\n", s);
836 if (verbose && (mod.mod_op == LDAP_MOD_DELETE)){
838 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);
844 new_value = Entry.DN;
845 mod.mod_type = attrlist[offset].quipu_name;
847 else if (mod.mod_op != LDAP_MOD_DELETE) {
849 * Don't screw around with what the user has typed
850 * if they are simply trying to delete a rfc822mailbox
853 * spaces on the left hand side of the e-mail
854 * address are bad news - we know that there
855 * must be a @-sign in the string, or else we
858 * note that this means a value like:
860 * first m. last@host.domain
862 * will be turned into:
864 * first.m..last@host.domain
866 * and the mailer will need to do the right thing
867 * with this; alternatively we could add code that
868 * collapsed multiple dots into a single dot
870 * Don't screw up things like:
872 * "Bryan Beecher" <bryan@umich.edu>
873 * Bryan Beecher <bryan@umich.edu>
876 if (strchr(s, '<') == NULL) {
877 for (cp = s; *cp != '@'; cp++)
878 if (isspace((unsigned char)*cp))
882 strcpy(attrtype, "rfc822");
883 strcat(attrtype, attrlist[offset].quipu_name);
884 mod.mod_type = attrtype;
888 strcpy(attrtype, "rfc822");
889 strcat(attrtype, attrlist[offset].quipu_name);
890 mod.mod_type = attrtype;
893 /* fill in the rest of the ldap_mod() structure */
895 mods[1] = (LDAPMod *) NULL;
897 values[0] = new_value;
899 mod.mod_values = values;
902 if (debug & D_GROUPS) {
906 printf(" About to call ldap_modify_s()\n");
907 printf(" ld = 0x%x\n", ld);
908 printf(" dn = [%s]\n", group);
909 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
910 printf(" mods[%1d] code = %1d\n",
912 printf(" mods[%1d] type = %s\n",
913 i, (*lpp)->mod_type);
914 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
915 printf(" mods[%1d] v[%1d] = %s\n",
921 if (my_ldap_modify_s(ld, group, mods)) {
923 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
924 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE) {
925 printf(" Could not locate value \"%s\"\n",
934 ldap_uncache_entry(ld, group);
937 * If the operation was REPLACE, we now need to "zero out" the
938 * other "half" of the list (e.g., user specified an e-mail
939 * address; now we need to clear the DN part of the list).
941 * NOTE: WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
943 * Also, change the opcode to LDAP_MOD_ADD and give the user an
944 * opportunity to add additional members to the group. We
945 * only take this branch the very first time during a REPLACE
948 if (mod.mod_op == LDAP_MOD_REPLACE) {
949 if (!strncmp(mod.mod_type, "rfc822", 6))
950 mod.mod_type = mod.mod_type + 6;
952 strcpy(attrtype, "rfc822");
953 strcat(attrtype, mod.mod_type);
954 mod.mod_type = attrtype;
958 mod.mod_values = values;
959 mod.mod_op = LDAP_MOD_DELETE;
961 if (debug & D_GROUPS) {
965 printf(" About to call ldap_modify_s()\n");
966 printf(" ld = 0x%x\n", ld);
967 printf(" dn = [%s]\n", group);
968 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
969 printf(" mods[%1d] code = %1d\n",
971 printf(" mods[%1d] type = %s\n",
972 i, (*lpp)->mod_type);
973 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
974 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
978 if (my_ldap_modify_s(ld, group, mods)) {
980 * A "No such attribute" error is no big deal.
981 * We only wanted to clear the attribute anyhow.
984 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
985 if (ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
990 ldap_uncache_entry(ld, group);
992 printf(" \"%s\" has been added\n", new_value);
993 mod.mod_op = LDAP_MOD_ADD;
995 else if (verbose && (mod.mod_op == LDAP_MOD_ADD))
996 printf(" \"%s\" has been added\n", new_value);
997 else if (verbose && (mod.mod_op == LDAP_MOD_DELETE))
998 printf(" \"%s\" has been removed\n", new_value);
1003 my_ldap_modify_s( LDAP *ldap, char *group, LDAPMod **mods )
1005 int was_rfc822member, rc;
1007 was_rfc822member = 0;
1009 if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
1010 mods[0]->mod_type = "mail";
1011 was_rfc822member = 1;
1014 rc = ldap_modify_s(ldap, group, mods);
1016 if (was_rfc822member)
1017 mods[0]->mod_type = "rfc822member";
1023 list_groups( char *who )
1026 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1027 char *work_area[MAX_NUM_NAMES];
1033 if (debug & D_TRACE) {
1035 printf("->list_groups(NULL)\n");
1037 printf("->list_groups(%s)\n", who);
1041 * First, decide what entry we are going to list. If the
1042 * user has not included a name on the list command line,
1043 * we will use the person who was last looked up with a find
1046 * Once we know who to modify, be sure that they exist, and
1047 * parse out their DN.
1050 printf(" List groups belonging to whose entry? ");
1052 fetch_buffer(name, sizeof(name), stdin);
1053 if (name[0] != '\0')
1058 if ((mp = find(who, TRUE)) == NULL) {
1059 (void) ldap_msgfree(mp);
1060 printf(" Could not locate \"%s\" in the Directory.\n", who);
1063 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1065 rdns = ldap_explode_dn(dn, TRUE);
1067 printf("\n Listing groups belonging to \"%s\"\n", *rdns);
1069 /* lookup the groups belonging to this person */
1070 sprintf(filter, "owner=%s", dn);
1072 search_attrs[0] = "cn";
1073 search_attrs[1] = NULL;
1074 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1075 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1076 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1077 ldap_perror(ld, "ldap_search_s");
1078 ldap_value_free(rdns);
1081 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1082 ldap_perror(ld, "ldap_count_entries");
1083 ldap_value_free(rdns);
1087 printf(" %s owns no groups in this portion of the Directory.\n", *rdns);
1088 ldap_value_free(rdns);
1092 printf(" %s owns %d groups.\n\n", *rdns, rc);
1093 print_list(mp, work_area, &rc);
1094 for (i = 1; work_area[i] != NULL; i++)
1097 ldap_value_free(rdns);
1102 bind_and_fetch( char *name )
1105 char tmp[MED_BUF_SIZE];
1108 if (debug & D_TRACE) {
1110 printf("->bind_and_fetch(NULL)\n");
1112 printf("->bind_and_fetch(%s)\n", name);
1115 if (bind_status == UD_NOT_BOUND) {
1116 if (auth((char *) NULL, 1) < 0)
1121 * If the user did not supply us with a name, prompt them for
1124 if ((name == NULL) || (*name == '\0')) {
1127 fetch_buffer(tmp, sizeof(tmp), stdin);
1132 /* remove quotes, dots, and underscores. */
1133 name = strip_ignore_chars(name);
1136 if (debug & D_GROUPS)
1137 printf("Group name = (%s)\n", name);
1140 /* make sure the group in question exists and is joinable */
1141 if ((lm = find(name, TRUE)) == (LDAPMessage *) NULL) {
1142 printf(" Could not locate group \"%s\"\n", name);
1149 if (debug & D_GROUPS)
1150 printf("Group DN = (%s)\n", Entry.DN);
1152 return(strdup(Entry.DN));
1156 list_memberships( char *who )
1159 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1160 char *work_area[MAX_NUM_NAMES];
1166 if (debug & D_TRACE) {
1168 printf("->list_memberships(NULL)\n");
1170 printf("->list_memberships(%s)\n", who);
1174 * First, decide what entry we are going to list. If the
1175 * user has not included a name on the list command line,
1176 * we will use the person who was last looked up with a find
1179 * Once we know who to modify, be sure that they exist, and
1180 * parse out their DN.
1183 printf(" List memberships containing whose entry? ");
1185 fetch_buffer(name, sizeof(name), stdin);
1186 if (name[0] != '\0')
1191 if ((mp = find(who, TRUE)) == NULL) {
1192 (void) ldap_msgfree(mp);
1193 printf(" Could not locate \"%s\" in the Directory.\n", who);
1197 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1198 rdns = ldap_explode_dn(dn, TRUE);
1200 printf("\n Listing memberships of \"%s\"\n", *rdns);
1202 /* lookup the groups belonging to this person */
1203 sprintf(filter, "member=%s", dn);
1205 search_attrs[0] = "cn";
1206 search_attrs[1] = NULL;
1208 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1209 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1210 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1211 ldap_perror(ld, "ldap_search_s");
1213 ldap_value_free(rdns);
1216 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1217 ldap_perror(ld, "ldap_count_entries");
1219 ldap_value_free(rdns);
1223 printf(" %s is not a member of any groups in this portion of the Directory.\n", *rdns);
1225 ldap_value_free(rdns);
1229 printf(" %s is a member of %d groups.\n\n", *rdns, rc);
1232 * print_list fills in the char * array starting at 1, not 0
1234 print_list(mp, work_area, &rc);
1235 for (i = 1; work_area[i] != NULL; i++)
1238 ldap_value_free(rdns);