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 <ac/string.h>
21 #include <ac/unistd.h>
29 #include <ldap_defaults.h>
32 static char * bind_and_fetch(char *name);
36 add_group( char *name )
38 int idx = 0, prompt = 0;
39 char tmp[BUFSIZ], dn[BUFSIZ];
40 static LDAPMod *attrs[9];
41 LDAPMod init_rdn, init_owner, init_domain,
42 init_errors, init_request, init_joinable;
43 char *init_rdn_value[2], *init_owner_value[2], *init_domain_value[2],
44 *init_errors_value[MAX_VALUES], *init_joinable_value[2],
45 *init_request_value[MAX_VALUES];
48 if (debug & D_TRACE) {
50 printf("->add_group(NULL)\n");
52 printf("->add_group(%s)\n", name);
56 if (bind_status == UD_NOT_BOUND) {
57 if (auth((char *) NULL, 1) < 0) {
63 * If the user did not supply us with a name, prompt them for
66 if ((name == NULL) || (*name == '\0') || !strcasecmp(name, "group")) {
68 printf(" Group to create? ");
70 fetch_buffer(tmp, sizeof(tmp), stdin);
76 /* remove quotes, dots, and underscores. */
77 name = strip_ignore_chars(name);
80 if (isauniqname(name)) {
81 printf(" '%s' could be confused with a U-M uniqname.\n", name);
82 printf(" You can create the group, but you need to make sure that\n");
83 printf(" you reserve the uniqname for use as your groupname\n\n");
84 printf(" Are you sure that you want to do this? ");
86 fetch_buffer(tmp, sizeof(tmp), stdin);
87 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
89 printf("\n Be sure to contact your uniqname administrator to reserve\n");
90 printf(" the uniqname '%s' for use as your group name.\n", name);
93 sprintf(dn, "cn=%s, %s", name, group_base);
96 * Make sure that this group does not already exist.
98 if (vrfy(dn) == TRUE) {
99 printf(" The group \"%s\" already exists.\n", name);
104 * Take the easy way out: Fill in some reasonable values for
105 * the most important fields, and make the user use the modify
106 * command to change them, or to give values to other fields.
108 init_rdn_value[0] = name;
109 init_rdn_value[1] = NULL;
110 init_rdn.mod_op = LDAP_MOD_ADD;
111 init_rdn.mod_type = "cn";
112 init_rdn.mod_values = init_rdn_value;
113 attrs[idx++] = &init_rdn;
115 init_owner_value[0] = bound_dn;
116 init_owner_value[1] = NULL;
117 init_owner.mod_op = LDAP_MOD_ADD;
118 init_owner.mod_type = "owner";
119 init_owner.mod_values = init_owner_value;
120 attrs[idx++] = &init_owner;
123 init_domain_value[0] = "umich.edu";
125 init_domain_value[0] = ".";
127 init_domain_value[1] = NULL;
128 init_domain.mod_op = LDAP_MOD_ADD;
129 init_domain.mod_type = "associatedDomain";
130 init_domain.mod_values = init_domain_value;
131 attrs[idx++] = &init_domain;
133 init_errors_value[0] = bound_dn;
134 init_errors_value[1] = NULL;
135 init_errors.mod_op = LDAP_MOD_ADD;
136 init_errors.mod_type = "ErrorsTo";
137 init_errors.mod_values = init_errors_value;
138 attrs[idx++] = &init_errors;
140 init_request_value[0] = bound_dn;
141 init_request_value[1] = NULL;
142 init_request.mod_op = LDAP_MOD_ADD;
143 init_request.mod_type = "RequestsTo";
144 init_request.mod_values = init_request_value;
145 attrs[idx++] = &init_request;
147 init_joinable_value[0] = "FALSE";
148 init_joinable_value[1] = NULL;
149 init_joinable.mod_op = LDAP_MOD_ADD;
150 init_joinable.mod_type = "joinable";
151 init_joinable.mod_values = init_joinable_value;
152 attrs[idx++] = &init_joinable;
154 /* end it with a NULL */
158 if (debug & D_GROUPS) {
162 printf(" About to call ldap_add()\n");
163 printf(" ld = 0x%x\n", ld);
164 printf(" dn = [%s]\n", dn);
165 for (lpp = attrs, i = 0; *lpp != NULL; lpp++, i++) {
166 printf(" attrs[%1d] code = %s type = %s\n", i,
167 code_to_str((*lpp)->mod_op), (*lpp)->mod_type);
168 for (cpp = (*lpp)->mod_values, j = 0; *cpp != NULL; cpp++, j++)
169 printf(" value #%1d = %s\n", j, *cpp);
170 printf(" value #%1d = NULL\n", j);
176 * Now add this to the LDAP Directory.
178 if (ldap_add_s(ld, dn, attrs) != 0) {
179 ldap_perror(ld, " ldap_add_s");
180 printf(" Group not added.\n");
181 if (prompt) Free(name);
185 printf(" Group \"%s\" has been added to the Directory\n",
189 * We need to blow away the cache here.
191 * Since we first looked up the name before trying to create it,
192 * and that look-up failed, the cache will falsely claim that this
193 * entry does not exist.
195 (void) ldap_flush_cache(ld);
196 if (prompt) Free(name);
201 remove_group( char *name )
203 char *dn, tmp[BUFSIZ];
206 if (debug & D_TRACE) {
208 printf("->remove_group(NULL)\n");
210 printf("->remove_group(%s)\n", name);
213 if ((dn = bind_and_fetch(name)) == NULL)
216 printf("\n The group '%s' will be permanently removed from\n",
218 printf(" the Directory. Are you absolutely sure that you want to\n" ); printf(" remove this entire group? ");
220 fetch_buffer(tmp, sizeof(tmp), stdin);
221 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
225 * Now remove this from the LDAP Directory.
227 if (ldap_delete_s(ld, dn) != 0) {
229 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
230 if (ld_errno == LDAP_INSUFFICIENT_ACCESS)
231 printf(" You do not own the group \"%s\".\n", name);
233 ldap_perror(ld, " ldap_delete_s");
234 printf(" Group not removed.\n");
238 ldap_uncache_entry(ld, dn);
241 printf(" The group has been removed.\n");
243 printf(" The group \"%s\" has been removed.\n", name);
249 x_group( int action, char *name )
252 char *values[2], *group_name;
253 LDAPMod mod, *mods[2];
254 static char *actions[] = { "join", "resign from", NULL };
257 if (debug & D_TRACE) {
259 printf("->x_group(%d, NULL)\n", action);
261 printf("->x_group(%d, %s)\n", action, name);
265 /* the action desired sets the opcode to use */
268 mod.mod_op = LDAP_MOD_ADD;
271 mod.mod_op = LDAP_MOD_DELETE;
274 printf("x_group: %d is not a known action\n", action);
277 if ((group_name = bind_and_fetch(name)) == NULL)
279 vp = Entry.attrs[attr_to_index("joinable")].values;
280 if (action == G_JOIN) {
282 printf(" No one is permitted to join \"%s\"\n", group_name);
286 if (!strcasecmp(*vp, "FALSE")) {
287 printf(" No one is permitted to join \"%s\"\n", group_name);
293 /* fill in the rest of the modification structure */
295 mods[1] = (LDAPMod *) NULL;
296 values[0] = Entry.DN;
298 mod.mod_type = "memberOfGroup";
299 mod.mod_values = values;
302 if (debug & D_GROUPS) {
303 register LDAPMod **lpp;
306 printf(" About to call ldap_modify_s()\n");
307 printf(" ld = 0x%x\n", ld);
308 printf(" dn = [%s]\n", bound_dn);
309 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
310 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
311 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
312 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
313 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
319 if (ldap_modify_s(ld, bound_dn, mods)) {
321 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
322 if ((action == G_JOIN) && (ld_errno == LDAP_TYPE_OR_VALUE_EXISTS))
323 printf(" You are already subscribed to \"%s\"\n", group_name);
324 else if ((action == G_RESIGN) && (ld_errno == LDAP_NO_SUCH_ATTRIBUTE))
325 printf(" You are not subscribed to \"%s\"\n", group_name);
331 ldap_uncache_entry(ld, bound_dn);
335 printf(" You are now subscribed to \"%s\"\n", group_name);
338 printf(" You are no longer subscribed to \"%s\"\n", group_name);
347 bulk_load( char *group )
349 register int idx_mail, idx_x500;
350 register int count_mail, count_x500;
351 char *values_mail[MAX_VALUES + 1], *values_x500[MAX_VALUES + 1];
352 int added_mail_entries = FALSE, added_x500_entries = FALSE;
353 char s[MED_BUF_SIZE];
354 LDAPMod mod, *mods[2];
361 printf("->bulk_load(%s)\n", group);
364 /* you lose if going through MichNet */
367 printf(" Not allowed via UM-X500 connections.\n");
372 /* fetch entries from the file containing the e-mail addresses */
373 printf("\n File to load? ");
375 fetch_buffer(s, sizeof(s), stdin);
380 if ((fp = fopen(s, "r")) == NULL) {
381 perror("bulk_load: fopen");
385 printf(" Loading group members from %s\n", s);
387 /* load them in MAX_VALUES at a time */
389 for (idx_mail = 0, idx_x500 = 0;
390 idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
391 (void) fgets(s, sizeof(s), fp);
399 values_mail[idx_mail++] = strdup(s);
401 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
402 printf(" Could not locate \"%s\" -- skipping.\n", s);
406 values_x500[idx_x500++] = strdup(Entry.DN);
410 values_mail[idx_mail] = NULL;
411 values_x500[idx_x500] = NULL;
412 count_mail = idx_mail;
413 count_x500 = idx_x500;
416 * Add the e-mail addresses.
418 if (count_mail > 0) {
420 mods[1] = (LDAPMod *) NULL;
421 mod.mod_type = "mail";
422 mod.mod_values = values_mail;
423 if (added_mail_entries)
424 mod.mod_op = LDAP_MOD_ADD;
426 mod.mod_op = LDAP_MOD_REPLACE;
429 if (debug & D_GROUPS) {
430 register LDAPMod **lpp;
433 printf(" About to call ldap_modify_s()\n");
434 printf(" ld = 0x%x\n", ld);
435 printf(" dn = [%s]\n", group);
436 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
437 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
438 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
439 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
440 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
444 if (ldap_modify_s(ld, group, mods))
446 for (idx_mail--; idx_mail >= 0; idx_mail--)
447 Free(values_mail[idx_mail]);
448 ldap_uncache_entry(ld, group);
449 added_mail_entries = TRUE;
454 * Add the LDAP style names.
456 if (count_x500 > 0) {
458 mods[1] = (LDAPMod *) NULL;
459 mod.mod_type = "member";
460 mod.mod_values = values_x500;
461 if (added_x500_entries)
462 mod.mod_op = LDAP_MOD_ADD;
464 mod.mod_op = LDAP_MOD_REPLACE;
467 if (debug & D_GROUPS) {
468 register LDAPMod **lpp;
471 printf(" About to call ldap_modify_s()\n");
472 printf(" ld = 0x%x\n", ld);
473 printf(" dn = [%s]\n", group);
474 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
475 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
476 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
477 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
478 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
482 if (ldap_modify_s(ld, group, mods))
484 for (idx_x500--; idx_x500 >= 0; idx_x500--)
485 Free(values_x500[idx_x500]);
486 ldap_uncache_entry(ld, group);
487 added_x500_entries = TRUE;
492 * If both counts were less than the maximum number we
493 * can handle at a time, then we are done.
495 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
503 purge_group( char *group )
507 LDAPMod mod, *mods[2];
508 char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
511 if (debug & D_TRACE) {
513 printf("->purge_group(NULL)\n");
515 printf("->purge_group(%s)\n", group);
518 if (bind_status == UD_NOT_BOUND) {
519 if (auth((char *) NULL, 1) < 0)
523 * If the user did not supply us with a name, prompt them for
526 if ((group == NULL) || (*group == '\0')) {
527 printf("Group to purge? ");
529 fetch_buffer(tmp, sizeof(tmp), stdin);
534 sprintf(dn, "cn=%s, %s", group, group_base);
536 /* make sure the group in question exists */
537 if ((lm = find(group, FALSE)) == (LDAPMessage *) NULL) {
538 printf(" Could not locate group \"%s\"\n", group);
544 /* none of this stuff changes */
546 mods[1] = (LDAPMod *) NULL;
550 mod.mod_values = values;
551 mod.mod_type = "member";
552 mod.mod_op = LDAP_MOD_DELETE;
555 * Now cycle through all of the names in the "members" part of the
556 * group (but not the e-mail address part). Lookup each one, and
557 * if it isn't found, let the user know so s/he can delete it.
559 vp = Entry.attrs[attr_to_index("member")].values;
562 printf(" \"%s\" has no LDAP members. There is nothing to purge.\n", group);
565 for (; *vp != NULL; vp++) {
566 char ans[BUFSIZ], *ufn, *label = "Did not find: ";
567 int len = strlen(label);
572 ufn = my_ldap_dn2ufn(*vp);
573 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
575 printf(" Purge, Keep, Replace, Abort [Keep]? ");
577 fetch_buffer(ans, sizeof(ans), stdin);
578 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
580 if (!strncasecmp(ans, "Abort", strlen(ans))) {
581 ldap_uncache_entry(ld, dn);
584 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
587 if (debug & D_GROUPS) {
588 register LDAPMod **lpp;
591 printf(" About to call ldap_modify_s()\n");
592 printf(" ld = 0x%x\n", ld);
593 printf(" dn = [%s]\n", Entry.DN);
594 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
595 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
596 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
597 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
598 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
603 if (ldap_modify_s(ld, Entry.DN, mods))
606 /* now add the replacement if requested */
607 if (!strncasecmp(ans, "Purge", strlen(ans)))
609 rdns = ldap_explode_dn(*vp, TRUE);
610 if ((lm = find(*rdns, FALSE)) == NULL) {
611 printf(" Could not find a replacement for %s; purged only.\n", *rdns);
613 ldap_value_free(rdns);
616 values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
617 mod.mod_op = LDAP_MOD_ADD;
619 if (debug & D_GROUPS) {
620 register LDAPMod **lpp;
623 printf(" About to call ldap_modify_s()\n");
624 printf(" ld = 0x%x\n", ld);
625 printf(" dn = [%s]\n", Entry.DN);
626 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
627 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
628 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
629 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
630 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
635 if (ldap_modify_s(ld, Entry.DN, mods))
638 ldap_value_free(rdns);
640 /* set this back to DELETE for other purges */
641 mod.mod_op = LDAP_MOD_DELETE;
644 printf(" Did not recognize that answer.\n\n");
648 ldap_uncache_entry(ld, Entry.DN);
650 printf(" No entries were purged.\n");
662 static LDAPMod *mods[2] = { &mod, NULL };
663 static char *values[MAX_VALUES];
667 printf("->tidy()\n");
670 if (bind_status == UD_NOT_BOUND) {
671 if (auth((char *) NULL, 1) < 0) {
676 /* lookup the user, and see to which groups he has subscribed */
677 vp = ldap_explode_dn(bound_dn, TRUE);
678 if ((lm = find(*vp, TRUE)) == (LDAPMessage *) NULL) {
679 printf(" Could not locate \"%s\"\n", *vp);
686 vp = Entry.attrs[attr_to_index("memberOfGroup")].values;
688 printf(" You have not subscribed to any groups.\n");
692 /* now, loop through these groups, deleting the bogus */
693 for ( ; *vp != NULL; vp++) {
697 printf(" \"%s\" is not a valid group name.\n", *vp);
698 values[i++] = strdup(*vp);
699 if ( i >= MAX_VALUES ) {
700 printf( " At most %d invalid groups can be removed at one time; skipping the rest.\n", MAX_VALUES );
704 if (found_one == 0) {
706 printf(" You are not a member of any invalid groups. There is nothing to tidy.\n");
710 /* delete the most heinous entries */
712 mod.mod_values = values;
713 mod.mod_op = LDAP_MOD_DELETE;
714 mod.mod_type = "memberOfGroup";
715 if (ldap_modify_s(ld, bound_dn, mods))
717 ldap_uncache_entry(ld, bound_dn);
719 /* tidy up before we finish tidy_up */
726 * This routine is used to modify lists that can contain either Distinguished
727 * Names or e-mail addresses. This includes things like group members,
728 * the errors-to field in groups, and so on.
731 mod_addrDN( char *group, int offset )
733 char s[BUFSIZ], *new_value /* was member */, *values[2];
735 LDAPMod mod, *mods[2];
740 printf("->mod_addrDN(%s)\n", group);
743 * At this point the user can indicate that he wishes to add values
744 * to the attribute, delete values from the attribute, or replace the
745 * current list of values with a new list. The first two cases
746 * are very straight-forward, but the last case requires a little
747 * extra care and work.
752 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);
754 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);
757 /* initialize the modififier type */
762 printf(" Do you want to Add, Delete, or Replace? ");
764 printf(" Do you want to Add, Delete, Replace, or Bulk load? ");
766 fetch_buffer(s, sizeof(s), stdin);
771 if (!strncasecmp(s, "add", strlen(s))) {
772 mod.mod_op = LDAP_MOD_ADD;
775 else if (!strncasecmp(s, "delete", strlen(s))) {
776 mod.mod_op = LDAP_MOD_DELETE;
779 else if (!strncasecmp(s, "replace", strlen(s))) {
780 mod.mod_op = LDAP_MOD_REPLACE;
783 else if(!strncasecmp(s, "bulk", strlen(s))) {
790 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);
792 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);
795 if (mod.mod_op == LDAP_MOD_REPLACE) {
796 if ( verbose && !confirm_action( "The entire existing list will be overwritten with the new values you are about to enter." )) {
797 printf("\n Modification halted.\n");
803 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);
808 printf("%s? ", attrlist[offset].output_string);
810 fetch_buffer(s, sizeof(s), stdin);
815 * If the string the user has just typed has an @-sign in it,
816 * then we assume it is an e-mail address. In this case, we
817 * just store away whatever it is they have typed.
819 * If the string had no @-sign, then we look in the Directory,
820 * make sure it exists, and if it does, we add that.
822 * If the string begins with a comma, then strip off the
823 * comma, and pass it along to the LDAP server. This is
824 * the way one can force ud to accept a name. Handy for
825 * debugging purposes.
829 mod.mod_type = attrlist[offset].quipu_name;
831 else if (strchr(s, '@') == NULL) {
832 if ((mp = find(s, FALSE)) == (LDAPMessage *) NULL) {
833 printf(" Could not find \"%s\"\n", s);
834 if (verbose && (mod.mod_op == LDAP_MOD_DELETE)){
836 format("I could not find anything that matched what you typed. You might try the \"purge\" command instead. It is used to purge corrupted or unlocatable entries from a group.", 75, 2);
842 new_value = Entry.DN;
843 mod.mod_type = attrlist[offset].quipu_name;
845 else if (mod.mod_op != LDAP_MOD_DELETE) {
847 * Don't screw around with what the user has typed
848 * if they are simply trying to delete a rfc822mailbox
851 * spaces on the left hand side of the e-mail
852 * address are bad news - we know that there
853 * must be a @-sign in the string, or else we
856 * note that this means a value like:
858 * first m. last@host.domain
860 * will be turned into:
862 * first.m..last@host.domain
864 * and the mailer will need to do the right thing
865 * with this; alternatively we could add code that
866 * collapsed multiple dots into a single dot
868 * Don't screw up things like:
870 * "Bryan Beecher" <bryan@umich.edu>
871 * Bryan Beecher <bryan@umich.edu>
874 if (strchr(s, '<') == NULL) {
875 for (cp = s; *cp != '@'; cp++)
876 if (isspace((unsigned char)*cp))
880 strcpy(attrtype, "rfc822");
881 strcat(attrtype, attrlist[offset].quipu_name);
882 mod.mod_type = attrtype;
886 strcpy(attrtype, "rfc822");
887 strcat(attrtype, attrlist[offset].quipu_name);
888 mod.mod_type = attrtype;
891 /* fill in the rest of the ldap_mod() structure */
893 mods[1] = (LDAPMod *) NULL;
895 values[0] = new_value;
897 mod.mod_values = values;
900 if (debug & D_GROUPS) {
904 printf(" About to call ldap_modify_s()\n");
905 printf(" ld = 0x%x\n", ld);
906 printf(" dn = [%s]\n", group);
907 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
908 printf(" mods[%1d] code = %1d\n",
910 printf(" mods[%1d] type = %s\n",
911 i, (*lpp)->mod_type);
912 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
913 printf(" mods[%1d] v[%1d] = %s\n",
919 if (my_ldap_modify_s(ld, group, mods)) {
921 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
922 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE) {
923 printf(" Could not locate value \"%s\"\n",
932 ldap_uncache_entry(ld, group);
935 * If the operation was REPLACE, we now need to "zero out" the
936 * other "half" of the list (e.g., user specified an e-mail
937 * address; now we need to clear the DN part of the list).
939 * NOTE: WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
941 * Also, change the opcode to LDAP_MOD_ADD and give the user an
942 * opportunity to add additional members to the group. We
943 * only take this branch the very first time during a REPLACE
946 if (mod.mod_op == LDAP_MOD_REPLACE) {
947 if (!strncmp(mod.mod_type, "rfc822", 6))
948 mod.mod_type = mod.mod_type + 6;
950 strcpy(attrtype, "rfc822");
951 strcat(attrtype, mod.mod_type);
952 mod.mod_type = attrtype;
956 mod.mod_values = values;
957 mod.mod_op = LDAP_MOD_DELETE;
959 if (debug & D_GROUPS) {
963 printf(" About to call ldap_modify_s()\n");
964 printf(" ld = 0x%x\n", ld);
965 printf(" dn = [%s]\n", group);
966 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
967 printf(" mods[%1d] code = %1d\n",
969 printf(" mods[%1d] type = %s\n",
970 i, (*lpp)->mod_type);
971 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
972 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
976 if (my_ldap_modify_s(ld, group, mods)) {
978 * A "No such attribute" error is no big deal.
979 * We only wanted to clear the attribute anyhow.
982 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
983 if (ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
988 ldap_uncache_entry(ld, group);
990 printf(" \"%s\" has been added\n", new_value);
991 mod.mod_op = LDAP_MOD_ADD;
993 else if (verbose && (mod.mod_op == LDAP_MOD_ADD))
994 printf(" \"%s\" has been added\n", new_value);
995 else if (verbose && (mod.mod_op == LDAP_MOD_DELETE))
996 printf(" \"%s\" has been removed\n", new_value);
1001 my_ldap_modify_s( LDAP *ldap, char *group, LDAPMod **mods )
1003 int was_rfc822member, rc;
1005 was_rfc822member = 0;
1007 if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
1008 mods[0]->mod_type = "mail";
1009 was_rfc822member = 1;
1012 rc = ldap_modify_s(ldap, group, mods);
1014 if (was_rfc822member)
1015 mods[0]->mod_type = "rfc822member";
1021 list_groups( char *who )
1024 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1025 char *work_area[MAX_NUM_NAMES];
1031 if (debug & D_TRACE) {
1033 printf("->list_groups(NULL)\n");
1035 printf("->list_groups(%s)\n", who);
1039 * First, decide what entry we are going to list. If the
1040 * user has not included a name on the list command line,
1041 * we will use the person who was last looked up with a find
1044 * Once we know who to modify, be sure that they exist, and
1045 * parse out their DN.
1048 printf(" List groups belonging to whose entry? ");
1050 fetch_buffer(name, sizeof(name), stdin);
1051 if (name[0] != '\0')
1056 if ((mp = find(who, TRUE)) == NULL) {
1057 (void) ldap_msgfree(mp);
1058 printf(" Could not locate \"%s\" in the Directory.\n", who);
1061 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1063 rdns = ldap_explode_dn(dn, TRUE);
1065 printf("\n Listing groups belonging to \"%s\"\n", *rdns);
1067 /* lookup the groups belonging to this person */
1068 sprintf(filter, "owner=%s", dn);
1070 search_attrs[0] = "cn";
1071 search_attrs[1] = NULL;
1072 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1073 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1074 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1075 ldap_perror(ld, "ldap_search_s");
1076 ldap_value_free(rdns);
1079 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1080 ldap_perror(ld, "ldap_count_entries");
1081 ldap_value_free(rdns);
1085 printf(" %s owns no groups in this portion of the Directory.\n", *rdns);
1086 ldap_value_free(rdns);
1090 printf(" %s owns %d groups.\n\n", *rdns, rc);
1091 print_list(mp, work_area, &rc);
1092 for (i = 1; work_area[i] != NULL; i++)
1095 ldap_value_free(rdns);
1100 bind_and_fetch( char *name )
1103 char tmp[MED_BUF_SIZE];
1106 if (debug & D_TRACE) {
1108 printf("->bind_and_fetch(NULL)\n");
1110 printf("->bind_and_fetch(%s)\n", name);
1113 if (bind_status == UD_NOT_BOUND) {
1114 if (auth((char *) NULL, 1) < 0)
1119 * If the user did not supply us with a name, prompt them for
1122 if ((name == NULL) || (*name == '\0')) {
1125 fetch_buffer(tmp, sizeof(tmp), stdin);
1130 /* remove quotes, dots, and underscores. */
1131 name = strip_ignore_chars(name);
1134 if (debug & D_GROUPS)
1135 printf("Group name = (%s)\n", name);
1138 /* make sure the group in question exists and is joinable */
1139 if ((lm = find(name, TRUE)) == (LDAPMessage *) NULL) {
1140 printf(" Could not locate group \"%s\"\n", name);
1147 if (debug & D_GROUPS)
1148 printf("Group DN = (%s)\n", Entry.DN);
1150 return(strdup(Entry.DN));
1154 list_memberships( char *who )
1157 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1158 char *work_area[MAX_NUM_NAMES];
1164 if (debug & D_TRACE) {
1166 printf("->list_memberships(NULL)\n");
1168 printf("->list_memberships(%s)\n", who);
1172 * First, decide what entry we are going to list. If the
1173 * user has not included a name on the list command line,
1174 * we will use the person who was last looked up with a find
1177 * Once we know who to modify, be sure that they exist, and
1178 * parse out their DN.
1181 printf(" List memberships containing whose entry? ");
1183 fetch_buffer(name, sizeof(name), stdin);
1184 if (name[0] != '\0')
1189 if ((mp = find(who, TRUE)) == NULL) {
1190 (void) ldap_msgfree(mp);
1191 printf(" Could not locate \"%s\" in the Directory.\n", who);
1195 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1196 rdns = ldap_explode_dn(dn, TRUE);
1198 printf("\n Listing memberships of \"%s\"\n", *rdns);
1200 /* lookup the groups belonging to this person */
1201 sprintf(filter, "member=%s", dn);
1203 search_attrs[0] = "cn";
1204 search_attrs[1] = NULL;
1206 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1207 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1208 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1209 ldap_perror(ld, "ldap_search_s");
1211 ldap_value_free(rdns);
1214 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1215 ldap_perror(ld, "ldap_count_entries");
1217 ldap_value_free(rdns);
1221 printf(" %s is not a member of any groups in this portion of the Directory.\n", *rdns);
1223 ldap_value_free(rdns);
1227 printf(" %s is a member of %d groups.\n\n", *rdns, rc);
1230 * print_list fills in the char * array starting at 1, not 0
1232 print_list(mp, work_area, &rc);
1233 for (i = 1; work_area[i] != NULL; i++)
1236 ldap_value_free(rdns);