1*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gate /* 4*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public 5*0Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file 6*0Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of 7*0Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/ 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS 10*0Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 11*0Sstevel@tonic-gate * implied. See the License for the specific language governing 12*0Sstevel@tonic-gate * rights and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released 15*0Sstevel@tonic-gate * March 31, 1998. 16*0Sstevel@tonic-gate * 17*0Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape 18*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are 19*0Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All 20*0Sstevel@tonic-gate * Rights Reserved. 21*0Sstevel@tonic-gate * 22*0Sstevel@tonic-gate * Contributor(s): 23*0Sstevel@tonic-gate */ 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan. 26*0Sstevel@tonic-gate * All rights reserved. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate /* 29*0Sstevel@tonic-gate * add.c 30*0Sstevel@tonic-gate */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #if 0 33*0Sstevel@tonic-gate #ifndef lint 34*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n"; 35*0Sstevel@tonic-gate #endif 36*0Sstevel@tonic-gate #endif 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #include "ldap-int.h" 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate /* 41*0Sstevel@tonic-gate * ldap_add - initiate an ldap add operation. Parameters: 42*0Sstevel@tonic-gate * 43*0Sstevel@tonic-gate * ld LDAP descriptor 44*0Sstevel@tonic-gate * dn DN of the entry to add 45*0Sstevel@tonic-gate * mods List of attributes for the entry. This is a null- 46*0Sstevel@tonic-gate * terminated array of pointers to LDAPMod structures. 47*0Sstevel@tonic-gate * only the type and values in the structures need be 48*0Sstevel@tonic-gate * filled in. 49*0Sstevel@tonic-gate * 50*0Sstevel@tonic-gate * Example: 51*0Sstevel@tonic-gate * LDAPMod *attrs[] = { 52*0Sstevel@tonic-gate * { 0, "cn", { "babs jensen", "babs", 0 } }, 53*0Sstevel@tonic-gate * { 0, "sn", { "jensen", 0 } }, 54*0Sstevel@tonic-gate * { 0, "objectClass", { "person", 0 } }, 55*0Sstevel@tonic-gate * 0 56*0Sstevel@tonic-gate * } 57*0Sstevel@tonic-gate * msgid = ldap_add( ld, dn, attrs ); 58*0Sstevel@tonic-gate */ 59*0Sstevel@tonic-gate int 60*0Sstevel@tonic-gate LDAP_CALL 61*0Sstevel@tonic-gate ldap_add( LDAP *ld, const char *dn, LDAPMod **attrs ) 62*0Sstevel@tonic-gate { 63*0Sstevel@tonic-gate int msgid; 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 ); 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate if ( ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid ) 68*0Sstevel@tonic-gate == LDAP_SUCCESS ) { 69*0Sstevel@tonic-gate return( msgid ); 70*0Sstevel@tonic-gate } else { 71*0Sstevel@tonic-gate return( -1 ); /* error is in ld handle */ 72*0Sstevel@tonic-gate } 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate /* 77*0Sstevel@tonic-gate * LDAPv3 extended add. 78*0Sstevel@tonic-gate * Returns an LDAP error code. 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate int 81*0Sstevel@tonic-gate LDAP_CALL 82*0Sstevel@tonic-gate ldap_add_ext( LDAP *ld, const char *dn, LDAPMod **attrs, 83*0Sstevel@tonic-gate LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp ) 84*0Sstevel@tonic-gate { 85*0Sstevel@tonic-gate BerElement *ber; 86*0Sstevel@tonic-gate int i, rc, lderr; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate /* 89*0Sstevel@tonic-gate * An add request looks like this: 90*0Sstevel@tonic-gate * AddRequest ::= SEQUENCE { 91*0Sstevel@tonic-gate * entry DistinguishedName, 92*0Sstevel@tonic-gate * attrs SEQUENCE OF SEQUENCE { 93*0Sstevel@tonic-gate * type AttributeType, 94*0Sstevel@tonic-gate * values SET OF AttributeValue 95*0Sstevel@tonic-gate * } 96*0Sstevel@tonic-gate * } 97*0Sstevel@tonic-gate */ 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 ); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) { 102*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp )) 106*0Sstevel@tonic-gate { 107*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL ); 108*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( attrs ) 111*0Sstevel@tonic-gate || msgidp == NULL ) { 112*0Sstevel@tonic-gate lderr = LDAP_PARAM_ERROR; 113*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL ); 114*0Sstevel@tonic-gate return( lderr ); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate if ( dn == NULL ) { 118*0Sstevel@tonic-gate dn = ""; 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK ); 122*0Sstevel@tonic-gate *msgidp = ++ld->ld_msgid; 123*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK ); 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate /* see if we should add to the cache */ 126*0Sstevel@tonic-gate if ( ld->ld_cache_on && ld->ld_cache_add != NULL ) { 127*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK ); 128*0Sstevel@tonic-gate if ( (rc = (ld->ld_cache_add)( ld, *msgidp, LDAP_REQ_ADD, dn, 129*0Sstevel@tonic-gate attrs )) != 0 ) { 130*0Sstevel@tonic-gate *msgidp = rc; 131*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK ); 132*0Sstevel@tonic-gate return( LDAP_SUCCESS ); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK ); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /* create a message to send */ 138*0Sstevel@tonic-gate if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber )) 139*0Sstevel@tonic-gate != LDAP_SUCCESS ) { 140*0Sstevel@tonic-gate return( lderr ); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate if ( ber_printf( ber, "{it{s{", *msgidp, LDAP_REQ_ADD, dn ) 144*0Sstevel@tonic-gate == -1 ) { 145*0Sstevel@tonic-gate lderr = LDAP_ENCODING_ERROR; 146*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL ); 147*0Sstevel@tonic-gate ber_free( ber, 1 ); 148*0Sstevel@tonic-gate return( lderr ); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* for each attribute in the entry... */ 152*0Sstevel@tonic-gate for ( i = 0; attrs[i] != NULL; i++ ) { 153*0Sstevel@tonic-gate if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) { 154*0Sstevel@tonic-gate rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type, 155*0Sstevel@tonic-gate attrs[i]->mod_bvalues ); 156*0Sstevel@tonic-gate } else { 157*0Sstevel@tonic-gate rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type, 158*0Sstevel@tonic-gate attrs[i]->mod_values ); 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate if ( rc == -1 ) { 161*0Sstevel@tonic-gate lderr = LDAP_ENCODING_ERROR; 162*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL ); 163*0Sstevel@tonic-gate ber_free( ber, 1 ); 164*0Sstevel@tonic-gate return( lderr ); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate if ( ber_printf( ber, "}}" ) == -1 ) { 169*0Sstevel@tonic-gate lderr = LDAP_ENCODING_ERROR; 170*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL ); 171*0Sstevel@tonic-gate ber_free( ber, 1 ); 172*0Sstevel@tonic-gate return( lderr ); 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber )) 176*0Sstevel@tonic-gate != LDAP_SUCCESS ) { 177*0Sstevel@tonic-gate ber_free( ber, 1 ); 178*0Sstevel@tonic-gate return( lderr ); 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* send the message */ 182*0Sstevel@tonic-gate rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_ADD, 183*0Sstevel@tonic-gate (char *) dn, ber ); 184*0Sstevel@tonic-gate *msgidp = rc; 185*0Sstevel@tonic-gate return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS ); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate int 189*0Sstevel@tonic-gate LDAP_CALL 190*0Sstevel@tonic-gate ldap_add_s( LDAP *ld, const char *dn, LDAPMod **attrs ) 191*0Sstevel@tonic-gate { 192*0Sstevel@tonic-gate return( ldap_add_ext_s( ld, dn, attrs, NULL, NULL )); 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate int LDAP_CALL 196*0Sstevel@tonic-gate ldap_add_ext_s( LDAP *ld, const char *dn, LDAPMod **attrs, 197*0Sstevel@tonic-gate LDAPControl **serverctrls, LDAPControl **clientctrls ) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate int err, msgid; 200*0Sstevel@tonic-gate LDAPMessage *res; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate if (( err = ldap_add_ext( ld, dn, attrs, serverctrls, clientctrls, 203*0Sstevel@tonic-gate &msgid )) != LDAP_SUCCESS ) { 204*0Sstevel@tonic-gate return( err ); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) { 208*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) ); 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate return( ldap_result2error( ld, res, 1 ) ); 212*0Sstevel@tonic-gate } 213