Back to index

lightning-sunbird  0.9+nobinonly
add.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Add a new entry to the directory.
00040  *
00041  * Instead of calling the synchronous ldap_add_s() routine, we call
00042  * the asynchronous routine ldap_add() and poll for results using
00043  * ldap_result().
00044  *
00045  * Since it is an error to attempt to add an entry which already exists,
00046  * you cannot run this example program twice in a row.  You can use the
00047  * adel.c example program to delete the entry which this example adds.
00048  *
00049  */
00050 
00051 #include "examples.h"
00052 
00053 static void do_other_work();
00054 unsigned long global_counter = 0;
00055 static void free_mods( LDAPMod **mods );
00056 
00057 #define       NMODS  5
00058 
00059 int
00060 main( int argc, char **argv )
00061 {
00062     LDAP             *ld;
00063     LDAPMessage             *result;
00064     char             *dn;
00065     int                     i;
00066     int                     rc;
00067     int                     msgid;
00068     int                     finished;
00069     struct timeval   zerotime;
00070     LDAPMod          **mods;
00071 
00072     char *objectclass_values[] = { "top", "person", "organizationalPerson",
00073                                "inetOrgPerson", NULL };
00074     char *cn_values[] = { "William B Jensen", "William Jensen", "Bill Jensen",
00075                        NULL };
00076     char *sn_values[] = { "Jensen", NULL };
00077     char *givenname_values[] = { "William", "Bill", NULL };
00078     char *telephonenumber_values[] = { "+1 415 555 1212", NULL };
00079 
00080     zerotime.tv_sec = zerotime.tv_usec = 0L;
00081 
00082     /* Specify the DN we're adding */
00083     dn = "cn=William B Jensen, " PEOPLE_BASE;    /* see examples.h */
00084 
00085     /* get a handle to an LDAP connection */
00086     if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {
00087        perror( "ldap_init" );
00088        return( 1 );
00089     }
00090 
00091     /* authenticate to the directory as the Directory Manager */
00092     if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) {
00093        ldap_perror( ld, "ldap_simple_bind_s" );
00094        return( 1 );
00095     }
00096 
00097     /* Construct the array of values to add */
00098     mods = ( LDAPMod ** ) malloc(( NMODS + 1 ) * sizeof( LDAPMod * ));
00099     if ( mods == NULL ) {
00100        fprintf( stderr, "Cannot allocate memory for mods array\n" );
00101     }
00102     for ( i = 0; i < NMODS; i++ ) {
00103        if (( mods[ i ] = ( LDAPMod * ) malloc( sizeof( LDAPMod ))) == NULL ) {
00104            fprintf( stderr, "Cannot allocate memory for mods element\n" );
00105            exit( 1 );
00106        }
00107     }
00108     mods[ 0 ]->mod_op = 0;
00109     mods[ 0 ]->mod_type = "objectclass";
00110     mods[ 0 ]->mod_values = objectclass_values;
00111     mods[ 1 ]->mod_op = 0;
00112     mods[ 1 ]->mod_type = "cn";
00113     mods[ 1 ]->mod_values = cn_values;
00114     mods[ 2 ]->mod_op = 0;
00115     mods[ 2 ]->mod_type = "sn";
00116     mods[ 2 ]->mod_values = sn_values;
00117     mods[ 3 ]->mod_op = 0;
00118     mods[ 3 ]->mod_type = "givenname";
00119     mods[ 3 ]->mod_values = givenname_values;
00120     mods[ 4 ]->mod_op = 0;
00121     mods[ 4 ]->mod_type = "telephonenumber";
00122     mods[ 4 ]->mod_values = telephonenumber_values;
00123     mods[ 5 ] = NULL;
00124 
00125 
00126     /* Initiate the add operation */
00127     if (( msgid = ldap_add( ld, dn, mods )) < 0 ) {
00128        ldap_perror( ld, "ldap_add" );
00129        free_mods( mods );
00130        return( 1 );
00131     }
00132 
00133     /* Poll for the result */
00134     finished = 0;
00135     while ( !finished ) {
00136        rc = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &result );
00137        switch ( rc ) {
00138        case -1:
00139            /* some error occurred */
00140            ldap_perror( ld, "ldap_result" );
00141            free_mods( mods );
00142            return( 1 );
00143        case 0:
00144            /* Timeout was exceeded.  No entries are ready for retrieval */
00145            break;
00146        default:
00147            /* Should be finished here */
00148            finished = 1;
00149            if (( rc = ldap_result2error( ld, result, 0 )) == LDAP_SUCCESS ) {
00150               printf( "Entry added successfully.  I counted to %ld "
00151                      "while waiting.\n", global_counter );
00152            } else {
00153               printf( "Error while adding entry: %s\n",
00154                      ldap_err2string( rc ));
00155            }
00156            ldap_msgfree( result );
00157        }
00158        do_other_work();
00159     }
00160     ldap_unbind( ld );
00161     free_mods( mods );
00162     return 0;
00163 }
00164 
00165 
00166 
00167 /*
00168  * Free a mods array.
00169  */
00170 static void
00171 free_mods( LDAPMod **mods )
00172 {
00173     int i;
00174 
00175     for ( i = 0; i < NMODS; i++ ) {
00176        free( mods[ i ] );
00177     }
00178     free( mods );
00179 }
00180        
00181 
00182 /*
00183  * Perform other work while polling for results.  This doesn't do anything
00184  * useful, but it could.
00185  */
00186 static void
00187 do_other_work()
00188 {
00189     global_counter++;
00190 }