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>
25 #include <ldapconfig.h>
28 static char * bind_and_fetch(char *name);
32 add_group( char *name )
34 register int idx = 0, prompt = 0;
35 char tmp[BUFSIZ], dn[BUFSIZ];
36 static LDAPMod *attrs[9];
37 LDAPMod init_rdn, init_owner, init_domain,
38 init_errors, init_request, init_joinable;
39 char *init_rdn_value[2], *init_owner_value[2], *init_domain_value[2],
40 *init_errors_value[MAX_VALUES], *init_joinable_value[2],
41 *init_request_value[MAX_VALUES];
44 if (debug & D_TRACE) {
46 printf("->add_group(NULL)\n");
48 printf("->add_group(%s)\n", name);
52 if (bind_status == UD_NOT_BOUND) {
53 if (auth((char *) NULL, 1) < 0) {
59 * If the user did not supply us with a name, prompt them for
62 if ((name == NULL) || (*name == '\0') || !strcasecmp(name, "group")) {
64 printf(" Group to create? ");
66 fetch_buffer(tmp, sizeof(tmp), stdin);
72 /* remove quotes, dots, and underscores. */
73 name = strip_ignore_chars(name);
76 if (isauniqname(name)) {
77 printf(" '%s' could be confused with a U-M uniqname.\n", name);
78 printf(" You can create the group, but you need to make sure that\n");
79 printf(" you reserve the uniqname for use as your groupname\n\n");
80 printf(" Are you sure that you want to do this? ");
82 fetch_buffer(tmp, sizeof(tmp), stdin);
83 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
85 printf("\n Be sure to contact your uniqname administrator to reserve\n");
86 printf(" the uniqname '%s' for use as your group name.\n", name);
89 sprintf(dn, "cn=%s, %s", name, group_base);
92 * Make sure that this group does not already exist.
94 if (vrfy(dn) == TRUE) {
95 printf(" The group \"%s\" already exists.\n", name);
100 * Take the easy way out: Fill in some reasonable values for
101 * the most important fields, and make the user use the modify
102 * command to change them, or to give values to other fields.
104 init_rdn_value[0] = name;
105 init_rdn_value[1] = NULL;
106 init_rdn.mod_op = LDAP_MOD_ADD;
107 init_rdn.mod_type = "cn";
108 init_rdn.mod_values = init_rdn_value;
109 attrs[idx++] = &init_rdn;
111 init_owner_value[0] = bound_dn;
112 init_owner_value[1] = NULL;
113 init_owner.mod_op = LDAP_MOD_ADD;
114 init_owner.mod_type = "owner";
115 init_owner.mod_values = init_owner_value;
116 attrs[idx++] = &init_owner;
119 init_domain_value[0] = "umich.edu";
121 init_domain_value[0] = ".";
123 init_domain_value[1] = NULL;
124 init_domain.mod_op = LDAP_MOD_ADD;
125 init_domain.mod_type = "associatedDomain";
126 init_domain.mod_values = init_domain_value;
127 attrs[idx++] = &init_domain;
129 init_errors_value[0] = bound_dn;
130 init_errors_value[1] = NULL;
131 init_errors.mod_op = LDAP_MOD_ADD;
132 init_errors.mod_type = "ErrorsTo";
133 init_errors.mod_values = init_errors_value;
134 attrs[idx++] = &init_errors;
136 init_request_value[0] = bound_dn;
137 init_request_value[1] = NULL;
138 init_request.mod_op = LDAP_MOD_ADD;
139 init_request.mod_type = "RequestsTo";
140 init_request.mod_values = init_request_value;
141 attrs[idx++] = &init_request;
143 init_joinable_value[0] = "FALSE";
144 init_joinable_value[1] = NULL;
145 init_joinable.mod_op = LDAP_MOD_ADD;
146 init_joinable.mod_type = "joinable";
147 init_joinable.mod_values = init_joinable_value;
148 attrs[idx++] = &init_joinable;
150 /* end it with a NULL */
154 if (debug & D_GROUPS) {
155 register LDAPMod **lpp;
158 printf(" About to call ldap_add()\n");
159 printf(" ld = 0x%x\n", ld);
160 printf(" dn = [%s]\n", dn);
161 for (lpp = attrs, i = 0; *lpp != NULL; lpp++, i++) {
162 printf(" attrs[%1d] code = %s type = %s\n", i,
163 code_to_str((*lpp)->mod_op), (*lpp)->mod_type);
164 for (cpp = (*lpp)->mod_values, j = 0; *cpp != NULL; cpp++, j++)
165 printf(" value #%1d = %s\n", j, *cpp);
166 printf(" value #%1d = NULL\n", j);
172 * Now add this to the LDAP Directory.
174 if (ldap_add_s(ld, dn, attrs) != 0) {
175 ldap_perror(ld, " ldap_add_s");
176 printf(" Group not added.\n");
177 if (prompt) Free(name);
181 printf(" Group \"%s\" has been added to the Directory\n",
185 * We need to blow away the cache here.
187 * Since we first looked up the name before trying to create it,
188 * and that look-up failed, the cache will falsely claim that this
189 * entry does not exist.
191 (void) ldap_flush_cache(ld);
192 if (prompt) Free(name);
197 remove_group( char *name )
199 char *dn, tmp[BUFSIZ];
202 if (debug & D_TRACE) {
204 printf("->remove_group(NULL)\n");
206 printf("->remove_group(%s)\n", name);
209 if ((dn = bind_and_fetch(name)) == NULL)
212 printf("\n The group '%s' will be permanently removed from\n",
214 printf(" the Directory. Are you absolutely sure that you want to\n" ); printf(" remove this entire group? ");
216 fetch_buffer(tmp, sizeof(tmp), stdin);
217 if (!(tmp[0] == 'y' || tmp[0] == 'Y'))
221 * Now remove this from the LDAP Directory.
223 if (ldap_delete_s(ld, dn) != 0) {
224 if (ld->ld_errno == LDAP_INSUFFICIENT_ACCESS)
225 printf(" You do not own the group \"%s\".\n", name);
227 ldap_perror(ld, " ldap_delete_s");
228 printf(" Group not removed.\n");
232 ldap_uncache_entry(ld, dn);
235 printf(" The group has been removed.\n");
237 printf(" The group \"%s\" has been removed.\n", name);
243 x_group( int action, char *name )
246 char *values[2], *group_name;
247 LDAPMod mod, *mods[2];
248 static char *actions[] = { "join", "resign from", NULL };
251 if (debug & D_TRACE) {
253 printf("->x_group(%d, NULL)\n", action);
255 printf("->x_group(%d, %s)\n", action, name);
259 /* the action desired sets the opcode to use */
262 mod.mod_op = LDAP_MOD_ADD;
265 mod.mod_op = LDAP_MOD_DELETE;
268 printf("x_group: %d is not a known action\n", action);
271 if ((group_name = bind_and_fetch(name)) == NULL)
273 vp = Entry.attrs[attr_to_index("joinable")].values;
274 if (action == G_JOIN) {
276 printf(" No one is permitted to join \"%s\"\n", group_name);
280 if (!strcasecmp(*vp, "FALSE")) {
281 printf(" No one is permitted to join \"%s\"\n", group_name);
287 /* fill in the rest of the modification structure */
289 mods[1] = (LDAPMod *) NULL;
290 values[0] = Entry.DN;
292 mod.mod_type = "memberOfGroup";
293 mod.mod_values = values;
296 if (debug & D_GROUPS) {
297 register LDAPMod **lpp;
300 printf(" About to call ldap_modify_s()\n");
301 printf(" ld = 0x%x\n", ld);
302 printf(" dn = [%s]\n", bound_dn);
303 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
304 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
305 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
306 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
307 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
313 if (ldap_modify_s(ld, bound_dn, mods)) {
314 if ((action == G_JOIN) && (ld->ld_errno == LDAP_TYPE_OR_VALUE_EXISTS))
315 printf(" You are already subscribed to \"%s\"\n", group_name);
316 else if ((action == G_RESIGN) && (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE))
317 printf(" You are not subscribed to \"%s\"\n", group_name);
323 ldap_uncache_entry(ld, bound_dn);
327 printf(" You are now subscribed to \"%s\"\n", group_name);
330 printf(" You are no longer subscribed to \"%s\"\n", group_name);
339 bulk_load( char *group )
341 register int idx_mail, idx_x500;
342 register int count_mail, count_x500;
343 char *values_mail[MAX_VALUES + 1], *values_x500[MAX_VALUES + 1];
344 int added_mail_entries = FALSE, added_x500_entries = FALSE;
345 char s[MED_BUF_SIZE];
346 LDAPMod mod, *mods[2];
353 printf("->bulk_load(%s)\n", group);
356 /* you lose if going through MichNet */
359 printf(" Not allowed via UM-X500 connections.\n");
364 /* fetch entries from the file containing the e-mail addresses */
365 printf("\n File to load? ");
367 fetch_buffer(s, sizeof(s), stdin);
372 if ((fp = fopen(s, "r")) == NULL) {
373 perror("bulk_load: fopen");
377 printf(" Loading group members from %s\n", s);
379 /* load them in MAX_VALUES at a time */
381 for (idx_mail = 0, idx_x500 = 0;
382 idx_mail < MAX_VALUES && idx_x500 < MAX_VALUES; ) {
383 (void) fgets(s, sizeof(s), fp);
391 values_mail[idx_mail++] = strdup(s);
393 if ((lm = find(s, !verbose)) == (LDAPMessage *) NULL) {
394 printf(" Could not locate \"%s\" -- skipping.\n", s);
398 values_x500[idx_x500++] = strdup(Entry.DN);
402 values_mail[idx_mail] = NULL;
403 values_x500[idx_x500] = NULL;
404 count_mail = idx_mail;
405 count_x500 = idx_x500;
408 * Add the e-mail addresses.
410 if (count_mail > 0) {
412 mods[1] = (LDAPMod *) NULL;
413 mod.mod_type = "mail";
414 mod.mod_values = values_mail;
415 if (added_mail_entries)
416 mod.mod_op = LDAP_MOD_ADD;
418 mod.mod_op = LDAP_MOD_REPLACE;
421 if (debug & D_GROUPS) {
422 register LDAPMod **lpp;
425 printf(" About to call ldap_modify_s()\n");
426 printf(" ld = 0x%x\n", ld);
427 printf(" dn = [%s]\n", group);
428 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
429 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
430 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
431 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
432 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
436 if (ldap_modify_s(ld, group, mods))
438 for (idx_mail--; idx_mail >= 0; idx_mail--)
439 Free(values_mail[idx_mail]);
440 ldap_uncache_entry(ld, group);
441 added_mail_entries = TRUE;
446 * Add the LDAP style names.
448 if (count_x500 > 0) {
450 mods[1] = (LDAPMod *) NULL;
451 mod.mod_type = "member";
452 mod.mod_values = values_x500;
453 if (added_x500_entries)
454 mod.mod_op = LDAP_MOD_ADD;
456 mod.mod_op = LDAP_MOD_REPLACE;
459 if (debug & D_GROUPS) {
460 register LDAPMod **lpp;
463 printf(" About to call ldap_modify_s()\n");
464 printf(" ld = 0x%x\n", ld);
465 printf(" dn = [%s]\n", group);
466 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
467 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
468 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
469 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
470 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
474 if (ldap_modify_s(ld, group, mods))
476 for (idx_x500--; idx_x500 >= 0; idx_x500--)
477 Free(values_x500[idx_x500]);
478 ldap_uncache_entry(ld, group);
479 added_x500_entries = TRUE;
484 * If both counts were less than the maximum number we
485 * can handle at a time, then we are done.
487 if ((count_mail < MAX_VALUES) && (count_x500 < MAX_VALUES))
495 purge_group( char *group )
499 LDAPMod mod, *mods[2];
500 char dn[BUFSIZ], tmp[BUFSIZ], *values[2], **vp, **rdns;
503 if (debug & D_TRACE) {
505 printf("->purge_group(NULL)\n");
507 printf("->purge_group(%s)\n", group);
510 if (bind_status == UD_NOT_BOUND) {
511 if (auth((char *) NULL, 1) < 0)
515 * If the user did not supply us with a name, prompt them for
518 if ((group == NULL) || (*group == '\0')) {
519 printf("Group to purge? ");
521 fetch_buffer(tmp, sizeof(tmp), stdin);
526 sprintf(dn, "cn=%s, %s", group, group_base);
528 /* make sure the group in question exists */
529 if ((lm = find(group, FALSE)) == (LDAPMessage *) NULL) {
530 printf(" Could not locate group \"%s\"\n", group);
536 /* none of this stuff changes */
538 mods[1] = (LDAPMod *) NULL;
542 mod.mod_values = values;
543 mod.mod_type = "member";
544 mod.mod_op = LDAP_MOD_DELETE;
547 * Now cycle through all of the names in the "members" part of the
548 * group (but not the e-mail address part). Lookup each one, and
549 * if it isn't found, let the user know so s/he can delete it.
551 vp = Entry.attrs[attr_to_index("member")].values;
554 printf(" \"%s\" has no LDAP members. There is nothing to purge.\n", group);
557 for (; *vp != NULL; vp++) {
558 char ans[BUFSIZ], *ufn, *label = "Did not find: ";
559 int len = strlen(label);
564 ufn = my_ldap_dn2ufn(*vp);
565 format2(ufn, label, (char *) NULL, 2, 2 + len, col_size);
567 printf(" Purge, Keep, Replace, Abort [Keep]? ");
569 fetch_buffer(ans, sizeof(ans), stdin);
570 if ((ans[0] == '\0') || !strncasecmp(ans, "Keep", strlen(ans)))
572 if (!strncasecmp(ans, "Abort", strlen(ans))) {
573 ldap_uncache_entry(ld, dn);
576 if (!strncasecmp(ans, "Purge", strlen(ans)) || !strncasecmp(ans, "Replace", strlen(ans))) {
579 if (debug & D_GROUPS) {
580 register LDAPMod **lpp;
583 printf(" About to call ldap_modify_s()\n");
584 printf(" ld = 0x%x\n", ld);
585 printf(" dn = [%s]\n", Entry.DN);
586 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
587 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
588 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
589 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
590 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
595 if (ldap_modify_s(ld, Entry.DN, mods))
598 /* now add the replacement if requested */
599 if (!strncasecmp(ans, "Purge", strlen(ans)))
601 rdns = ldap_explode_dn(*vp, TRUE);
602 if ((lm = find(*rdns, FALSE)) == NULL) {
603 printf(" Could not find a replacement for %s; purged only.\n", *rdns);
605 ldap_value_free(rdns);
608 values[0] = ldap_get_dn(ld, ldap_first_entry(ld, lm));
609 mod.mod_op = LDAP_MOD_ADD;
611 if (debug & D_GROUPS) {
612 register LDAPMod **lpp;
615 printf(" About to call ldap_modify_s()\n");
616 printf(" ld = 0x%x\n", ld);
617 printf(" dn = [%s]\n", Entry.DN);
618 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
619 printf(" mods[%1d] code = %1d\n", i, (*lpp)->mod_op);
620 printf(" mods[%1d] type = %s\n", i, (*lpp)->mod_type);
621 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
622 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
627 if (ldap_modify_s(ld, Entry.DN, mods))
630 ldap_value_free(rdns);
632 /* set this back to DELETE for other purges */
633 mod.mod_op = LDAP_MOD_DELETE;
636 printf(" Did not recognize that answer.\n\n");
640 ldap_uncache_entry(ld, Entry.DN);
642 printf(" No entries were purged.\n");
654 static LDAPMod *mods[2] = { &mod, NULL };
655 static char *values[MAX_VALUES];
659 printf("->tidy()\n");
662 if (bind_status == UD_NOT_BOUND) {
663 if (auth((char *) NULL, 1) < 0) {
668 /* lookup the user, and see to which groups he has subscribed */
669 vp = ldap_explode_dn(bound_dn, TRUE);
670 if ((lm = find(*vp, TRUE)) == (LDAPMessage *) NULL) {
671 printf(" Could not locate \"%s\"\n", *vp);
678 vp = Entry.attrs[attr_to_index("memberOfGroup")].values;
680 printf(" You have not subscribed to any groups.\n");
684 /* now, loop through these groups, deleting the bogus */
685 for ( ; *vp != NULL; vp++) {
689 printf(" \"%s\" is not a valid group name.\n", *vp);
690 values[i++] = strdup(*vp);
691 if ( i >= MAX_VALUES ) {
692 printf( " At most %d invalid groups can be removed at one time; skipping the rest.\n", MAX_VALUES );
696 if (found_one == 0) {
698 printf(" You are not a member of any invalid groups. There is nothing to tidy.\n");
702 /* delete the most heinous entries */
704 mod.mod_values = values;
705 mod.mod_op = LDAP_MOD_DELETE;
706 mod.mod_type = "memberOfGroup";
707 if (ldap_modify_s(ld, bound_dn, mods))
709 ldap_uncache_entry(ld, bound_dn);
711 /* tidy up before we finish tidy_up */
718 * This routine is used to modify lists that can contain either Distinguished
719 * Names or e-mail addresses. This includes things like group members,
720 * the errors-to field in groups, and so on.
723 mod_addrDN( char *group, int offset )
725 char s[BUFSIZ], *new_value /* was member */, *values[2];
727 LDAPMod mod, *mods[2];
732 printf("->mod_addrDN(%s)\n", group);
735 * At this point the user can indicate that he wishes to add values
736 * to the attribute, delete values from the attribute, or replace the
737 * current list of values with a new list. The first two cases
738 * are very straight-forward, but the last case requires a little
739 * extra care and work.
744 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);
746 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);
749 /* initialize the modififier type */
754 printf(" Do you want to Add, Delete, or Replace? ");
756 printf(" Do you want to Add, Delete, Replace, or Bulk load? ");
758 fetch_buffer(s, sizeof(s), stdin);
763 if (!strncasecmp(s, "add", strlen(s))) {
764 mod.mod_op = LDAP_MOD_ADD;
767 else if (!strncasecmp(s, "delete", strlen(s))) {
768 mod.mod_op = LDAP_MOD_DELETE;
771 else if (!strncasecmp(s, "replace", strlen(s))) {
772 mod.mod_op = LDAP_MOD_REPLACE;
775 else if(!strncasecmp(s, "bulk", strlen(s))) {
782 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);
784 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);
787 if (mod.mod_op == LDAP_MOD_REPLACE) {
788 if ( verbose && !confirm_action( "The entire existing list will be overwritten with the new values you are about to enter." )) {
789 printf("\n Modification halted.\n");
795 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);
800 printf("%s? ", attrlist[offset].output_string);
802 fetch_buffer(s, sizeof(s), stdin);
807 * If the string the user has just typed has an @-sign in it,
808 * then we assume it is an e-mail address. In this case, we
809 * just store away whatever it is they have typed.
811 * If the string had no @-sign, then we look in the Directory,
812 * make sure it exists, and if it does, we add that.
814 * If the string begins with a comma, then strip off the
815 * comma, and pass it along to the LDAP server. This is
816 * the way one can force ud to accept a name. Handy for
817 * debugging purposes.
821 mod.mod_type = attrlist[offset].quipu_name;
823 else if (strchr(s, '@') == NULL) {
824 if ((mp = find(s, FALSE)) == (LDAPMessage *) NULL) {
825 printf(" Could not find \"%s\"\n", s);
826 if (verbose && (mod.mod_op == LDAP_MOD_DELETE)){
828 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);
834 new_value = Entry.DN;
835 mod.mod_type = attrlist[offset].quipu_name;
837 else if (mod.mod_op != LDAP_MOD_DELETE) {
839 * Don't screw around with what the user has typed
840 * if they are simply trying to delete a rfc822mailbox
843 * spaces on the left hand side of the e-mail
844 * address are bad news - we know that there
845 * must be a @-sign in the string, or else we
848 * note that this means a value like:
850 * first m. last@host.domain
852 * will be turned into:
854 * first.m..last@host.domain
856 * and the mailer will need to do the right thing
857 * with this; alternatively we could add code that
858 * collapsed multiple dots into a single dot
860 * Don't screw up things like:
862 * "Bryan Beecher" <bryan@umich.edu>
863 * Bryan Beecher <bryan@umich.edu>
866 if (strchr(s, '<') == NULL) {
867 for (cp = s; *cp != '@'; cp++)
872 strcpy(attrtype, "rfc822");
873 strcat(attrtype, attrlist[offset].quipu_name);
874 mod.mod_type = attrtype;
878 strcpy(attrtype, "rfc822");
879 strcat(attrtype, attrlist[offset].quipu_name);
880 mod.mod_type = attrtype;
883 /* fill in the rest of the ldap_mod() structure */
885 mods[1] = (LDAPMod *) NULL;
887 values[0] = new_value;
889 mod.mod_values = values;
892 if (debug & D_GROUPS) {
893 register LDAPMod **lpp;
896 printf(" About to call ldap_modify_s()\n");
897 printf(" ld = 0x%x\n", ld);
898 printf(" dn = [%s]\n", group);
899 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
900 printf(" mods[%1d] code = %1d\n",
902 printf(" mods[%1d] type = %s\n",
903 i, (*lpp)->mod_type);
904 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
905 printf(" mods[%1d] v[%1d] = %s\n",
911 if (my_ldap_modify_s(ld, group, mods)) {
912 if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE) {
913 printf(" Could not locate value \"%s\"\n",
922 ldap_uncache_entry(ld, group);
925 * If the operation was REPLACE, we now need to "zero out" the
926 * other "half" of the list (e.g., user specified an e-mail
927 * address; now we need to clear the DN part of the list).
929 * NOTE: WE HAVE ALREADY DONE HALF OF THE REPLACE ABOVE.
931 * Also, change the opcode to LDAP_MOD_ADD and give the user an
932 * opportunity to add additional members to the group. We
933 * only take this branch the very first time during a REPLACE
936 if (mod.mod_op == LDAP_MOD_REPLACE) {
937 if (!strncmp(mod.mod_type, "rfc822", 6))
938 mod.mod_type = mod.mod_type + 6;
940 strcpy(attrtype, "rfc822");
941 strcat(attrtype, mod.mod_type);
942 mod.mod_type = attrtype;
946 mod.mod_values = values;
947 mod.mod_op = LDAP_MOD_DELETE;
949 if (debug & D_GROUPS) {
950 register LDAPMod **lpp;
953 printf(" About to call ldap_modify_s()\n");
954 printf(" ld = 0x%x\n", ld);
955 printf(" dn = [%s]\n", group);
956 for (lpp = mods, i = 1; *lpp != NULL; lpp++, i++) {
957 printf(" mods[%1d] code = %1d\n",
959 printf(" mods[%1d] type = %s\n",
960 i, (*lpp)->mod_type);
961 for (cp = (*lpp)->mod_values, j = 1; *cp != NULL; cp++, j++)
962 printf(" mods[%1d] v[%1d] = %s\n", i, j, *cp);
966 if (my_ldap_modify_s(ld, group, mods)) {
968 * A "No such attribute" error is no big deal.
969 * We only wanted to clear the attribute anyhow.
971 if (ld->ld_errno != LDAP_NO_SUCH_ATTRIBUTE) {
976 ldap_uncache_entry(ld, group);
978 printf(" \"%s\" has been added\n", new_value);
979 mod.mod_op = LDAP_MOD_ADD;
981 else if (verbose && (mod.mod_op == LDAP_MOD_ADD))
982 printf(" \"%s\" has been added\n", new_value);
983 else if (verbose && (mod.mod_op == LDAP_MOD_DELETE))
984 printf(" \"%s\" has been removed\n", new_value);
989 my_ldap_modify_s( LDAP *ldap, char *group, LDAPMod **mods )
991 int was_rfc822member, rc;
993 was_rfc822member = 0;
995 if (!strcasecmp(mods[0]->mod_type, "rfc822member")) {
996 mods[0]->mod_type = "mail";
997 was_rfc822member = 1;
1000 rc = ldap_modify_s(ldap, group, mods);
1002 if (was_rfc822member)
1003 mods[0]->mod_type = "rfc822member";
1009 list_groups( char *who )
1012 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1013 char *work_area[MAX_NUM_NAMES];
1019 if (debug & D_TRACE) {
1021 printf("->list_groups(NULL)\n");
1023 printf("->list_groups(%s)\n", who);
1027 * First, decide what entry we are going to list. If the
1028 * user has not included a name on the list command line,
1029 * we will use the person who was last looked up with a find
1032 * Once we know who to modify, be sure that they exist, and
1033 * parse out their DN.
1036 printf(" List groups belonging to whose entry? ");
1038 fetch_buffer(name, sizeof(name), stdin);
1039 if (name[0] != '\0')
1044 if ((mp = find(who, TRUE)) == NULL) {
1045 (void) ldap_msgfree(mp);
1046 printf(" Could not locate \"%s\" in the Directory.\n", who);
1049 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1051 rdns = ldap_explode_dn(dn, TRUE);
1053 printf("\n Listing groups belonging to \"%s\"\n", *rdns);
1055 /* lookup the groups belonging to this person */
1056 sprintf(filter, "owner=%s", dn);
1058 search_attrs[0] = "cn";
1059 search_attrs[1] = NULL;
1060 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1061 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1062 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1063 ldap_perror(ld, "ldap_search_s");
1064 ldap_value_free(rdns);
1067 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1068 ldap_perror(ld, "ldap_count_entries");
1069 ldap_value_free(rdns);
1073 printf(" %s owns no groups in this portion of the Directory.\n", *rdns);
1074 ldap_value_free(rdns);
1078 printf(" %s owns %d groups.\n\n", *rdns, rc);
1079 print_list(mp, work_area, &rc);
1080 for (i = 1; work_area[i] != NULL; i++)
1083 ldap_value_free(rdns);
1088 bind_and_fetch( char *name )
1091 char tmp[MED_BUF_SIZE];
1094 if (debug & D_TRACE) {
1096 printf("->bind_and_fetch(NULL)\n");
1098 printf("->bind_and_fetch(%s)\n", name);
1101 if (bind_status == UD_NOT_BOUND) {
1102 if (auth((char *) NULL, 1) < 0)
1107 * If the user did not supply us with a name, prompt them for
1110 if ((name == NULL) || (*name == '\0')) {
1113 fetch_buffer(tmp, sizeof(tmp), stdin);
1118 /* remove quotes, dots, and underscores. */
1119 name = strip_ignore_chars(name);
1122 if (debug & D_GROUPS)
1123 printf("Group name = (%s)\n", name);
1126 /* make sure the group in question exists and is joinable */
1127 if ((lm = find(name, TRUE)) == (LDAPMessage *) NULL) {
1128 printf(" Could not locate group \"%s\"\n", name);
1135 if (debug & D_GROUPS)
1136 printf("Group DN = (%s)\n", Entry.DN);
1138 return(strdup(Entry.DN));
1142 list_memberships( char *who )
1145 char name[BUFSIZ], filter[BUFSIZ], *search_attrs[2];
1146 char *work_area[MAX_NUM_NAMES];
1152 if (debug & D_TRACE) {
1154 printf("->list_memberships(NULL)\n");
1156 printf("->list_memberships(%s)\n", who);
1160 * First, decide what entry we are going to list. If the
1161 * user has not included a name on the list command line,
1162 * we will use the person who was last looked up with a find
1165 * Once we know who to modify, be sure that they exist, and
1166 * parse out their DN.
1169 printf(" List memberships containing whose entry? ");
1171 fetch_buffer(name, sizeof(name), stdin);
1172 if (name[0] != '\0')
1177 if ((mp = find(who, TRUE)) == NULL) {
1178 (void) ldap_msgfree(mp);
1179 printf(" Could not locate \"%s\" in the Directory.\n", who);
1183 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
1184 rdns = ldap_explode_dn(dn, TRUE);
1186 printf("\n Listing memberships of \"%s\"\n", *rdns);
1188 /* lookup the groups belonging to this person */
1189 sprintf(filter, "member=%s", dn);
1191 search_attrs[0] = "cn";
1192 search_attrs[1] = NULL;
1194 if ((rc = ldap_search_s(ld, UD_WHERE_ALL_GROUPS_LIVE, LDAP_SCOPE_SUBTREE,
1195 filter, search_attrs, FALSE, &mp)) != LDAP_SUCCESS &&
1196 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
1197 ldap_perror(ld, "ldap_search_s");
1199 ldap_value_free(rdns);
1202 if ((rc = ldap_count_entries(ld, mp)) < 0) {
1203 ldap_perror(ld, "ldap_count_entries");
1205 ldap_value_free(rdns);
1209 printf(" %s is not a member of any groups in this portion of the Directory.\n", *rdns);
1211 ldap_value_free(rdns);
1215 printf(" %s is a member of %d groups.\n\n", *rdns, rc);
1218 * print_list fills in the char * array starting at 1, not 0
1220 print_list(mp, work_area, &rc);
1221 for (i = 1; work_area[i] != NULL; i++)
1224 ldap_value_free(rdns);