/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Communicator client code, released * March 31, 1998. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998-1999 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ /* * Modify an entry: * - replace any existing "mail" attribute values with "babs@example.com" * - add a new value to the "description" attribute */ /* * Common definitions for ldap example programs. * */ #include #include #include #include #include /* * Host name of LDAP server */ #define MY_HOST "localhost" /* * Port number where LDAP server is running */ #define MY_PORT LDAP_PORT /* * DN of directory manager entry. This entry should have write access to * the entire directory. */ #define MGR_DN "cn=Directory Manager" /* * Password for manager DN. */ #define MGR_PW "wmekjslfbndk" #define ENTRYDN "cn=Accounting Managers,ou=Groups,o=my.com" #define MAXMEMBER 1000 int main( int argc, char **argv ) { LDAP *ld; LDAPMod **mods; char **vals0, buf[ 256 ]; char *p; time_t start, end; int i; int do_once = 0; int maxmember = MAXMEMBER; char *type = "uniqueMember"; #define ONEMOD 1 #define MANYMOD 2 for (i = 1; i < argc; ++i) { char *val = argv[i]; if (!strcmp(val, "-n")) { maxmember = atoi(argv[++i]); } else if (!strcmp(val, "-x")) { /* switch type */ type = "eduPersonEntitlement"; } else if (!strcasecmp(val, "many")) { do_once = MANYMOD; } else { do_once = ONEMOD; } } mods = (LDAPMod **)malloc(sizeof(LDAPMod *)*(maxmember+1)); vals0 = (char **)malloc(sizeof(char *)*(2*maxmember+1)); if ((NULL == mods) || (NULL == vals0)) { perror( "malloc" ); return( 1 ); } /* get an LDAP session handle and authenticate */ if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) { perror( "ldap_init" ); return( 1 ); } if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_simple_bind_s" ); return( 1 ); } if (ONEMOD == do_once) { LDAPMod mod0; for (i = 0; i < maxmember; i++) { int count = 0; count = asprintf(&p, "uid=tUser%d, ou=PeoplE, o=mY.cOm", i); if (0 == count) { fprintf(stderr, "Generating \"uid=tUser%d, ou=PeoplE, o=mY.cOm\" failed", i); perror("asprintf"); return 1; } vals0[i] = p; } vals0[i] = NULL; time( &start ); fprintf(stderr, "Start %d modify (One Mod): %s\n", maxmember, ctime(&start)); mod0.mod_op = LDAP_MOD_ADD; mod0.mod_type = type; mod0.mod_values = vals0; mods[ 0 ] = &mod0; mods[ 1 ] = NULL; /* make the change and clean up after ourselves */ if ( ldap_modify_s( ld, ENTRYDN, mods ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_modify_s" ); return( 1 ); } time( &end ); fprintf(stderr, "End %d modify (One Mod): %s (%d sec)\n", maxmember, ctime(&end), end - start); for (i = 0; i < maxmember && vals0[i]; i++) { free(vals0[i]); } } else if (MANYMOD == do_once) { LDAPMod *modp; for (i = 0; i < maxmember; i++) { int count = 0; modp = (LDAPMod *)malloc(sizeof(LDAPMod)); if (NULL == modp) { fprintf(stderr, "Allocating %d-th mod failed\n", i); return 1; } count = asprintf(&p, "uid=tUser%d, ou=PeoplE, o=mY.cOm", i); if (0 == count) { fprintf(stderr, "Generating \"uid=tUser%d, ou=PeoplE, o=mY.cOm\" failed", i); perror("asprintf"); return 1; } vals0[2*i] = p; vals0[2*i+1] = NULL; modp->mod_op = LDAP_MOD_ADD; modp->mod_type = type; modp->mod_values = &vals0[2*i]; mods[i] = modp; } mods[i] = NULL; time( &start ); fprintf(stderr, "Start %d modify (Many): %s\n", maxmember, ctime(&start)); /* make the change and clean up after ourselves */ if ( ldap_modify_s( ld, ENTRYDN, mods ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_modify_s" ); return( 1 ); } time( &end ); fprintf(stderr, "End %d modify (Many): %s (%d sec)\n", maxmember, ctime(&end), end - start); for (i = 0; i < 2*maxmember+1; i++) { if (vals0[i]) { free(vals0[i]); } } for (i = 0; i < maxmember+1; i++) { if (mods[i]) { free(mods[i]); } } } else { LDAPMod mod0; sprintf(buf, "uid=tUser"); p = buf + strlen(buf); time( &start ); fprintf(stderr, "Start %d modify: %s\n", maxmember, ctime(&start)); for (i = 0; i < maxmember; i++) { /* construct the list of modifications to make */ sprintf(p, "%d, ou=PeoplE, o=mY.cOm", i); mod0.mod_op = LDAP_MOD_ADD; mod0.mod_type = type; vals0[0] = buf; vals0[1] = NULL; mod0.mod_values = vals0; mods[ 0 ] = &mod0; mods[ 1 ] = NULL; /* make the change and clean up after ourselves */ if ( ldap_modify_s( ld, ENTRYDN, mods ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_modify_s" ); return( 1 ); } } time( &end ); fprintf(stderr, "End %d modify: %s (%d sec)\n", maxmember, ctime(&end), end - start); } ldap_unbind( ld ); printf( "modification was successful\n" ); free(mods); free(vals0); return( 0 ); }