10Sstevel@tonic-gate /*
2*3857Sstevel * Portions Copyright 1998 Sun Microsystems, Inc. All rights reserved.
3*3857Sstevel * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan.
80Sstevel@tonic-gate * All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * add.c
110Sstevel@tonic-gate */
120Sstevel@tonic-gate
130Sstevel@tonic-gate #ifndef lint
140Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
150Sstevel@tonic-gate #endif
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include <stdio.h>
180Sstevel@tonic-gate #include <string.h>
190Sstevel@tonic-gate
200Sstevel@tonic-gate #ifdef MACOS
210Sstevel@tonic-gate #include "macos.h"
220Sstevel@tonic-gate #endif /* MACOS */
230Sstevel@tonic-gate
240Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
250Sstevel@tonic-gate #include <malloc.h>
260Sstevel@tonic-gate #include "msdos.h"
270Sstevel@tonic-gate #endif /* DOS */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #endif /* !MACOS && !DOS */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include "lber.h"
350Sstevel@tonic-gate #include "ldap.h"
360Sstevel@tonic-gate #include "ldap-private.h"
370Sstevel@tonic-gate #include "ldap-int.h"
380Sstevel@tonic-gate
390Sstevel@tonic-gate
ldap_build_add_req(LDAP * ld,char * dn,LDAPMod ** attrs,LDAPControl ** serverctrls)400Sstevel@tonic-gate BerElement * ldap_build_add_req(LDAP *ld, char *dn, LDAPMod **attrs,
410Sstevel@tonic-gate LDAPControl ** serverctrls)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate BerElement * ber;
440Sstevel@tonic-gate int rc, i;
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * An add request looks like this:
480Sstevel@tonic-gate * AddRequest ::= [APPLICATION 8] SEQUENCE {
490Sstevel@tonic-gate * entry DistinguishedName,
500Sstevel@tonic-gate * attrs SEQUENCE OF SEQUENCE {
510Sstevel@tonic-gate * type AttributeType,
520Sstevel@tonic-gate * values SET OF AttributeValue
530Sstevel@tonic-gate * }
540Sstevel@tonic-gate * }
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate /* create a message to send */
580Sstevel@tonic-gate if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
590Sstevel@tonic-gate ld->ld_errno = LDAP_NO_MEMORY;
600Sstevel@tonic-gate return( NULLBER );
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_ADD, dn )
640Sstevel@tonic-gate == -1 ) {
650Sstevel@tonic-gate ld->ld_errno = LDAP_ENCODING_ERROR;
660Sstevel@tonic-gate ber_free( ber, 1 );
670Sstevel@tonic-gate return( NULLBER );
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate /* for each attribute in the entry... */
710Sstevel@tonic-gate for ( i = 0; attrs[i] != NULL; i++ ) {
720Sstevel@tonic-gate if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
730Sstevel@tonic-gate rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
740Sstevel@tonic-gate attrs[i]->mod_values );
750Sstevel@tonic-gate } else {
760Sstevel@tonic-gate rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
770Sstevel@tonic-gate attrs[i]->mod_values );
780Sstevel@tonic-gate }
790Sstevel@tonic-gate if ( rc == -1 ) {
800Sstevel@tonic-gate ld->ld_errno = LDAP_ENCODING_ERROR;
810Sstevel@tonic-gate ber_free( ber, 1 );
820Sstevel@tonic-gate return(NULLBER);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate if ( ber_printf( ber, "}}" ) == -1 ) {
870Sstevel@tonic-gate ld->ld_errno = LDAP_ENCODING_ERROR;
880Sstevel@tonic-gate ber_free( ber, 1 );
890Sstevel@tonic-gate return( NULLBER );
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate /* LDAPv3 */
930Sstevel@tonic-gate /* Code controls if any */
940Sstevel@tonic-gate if (serverctrls && serverctrls[0]) {
950Sstevel@tonic-gate if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
960Sstevel@tonic-gate ld->ld_errno = LDAP_ENCODING_ERROR;
970Sstevel@tonic-gate ber_free( ber, 1 );
980Sstevel@tonic-gate return( NULLBER );
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate } else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
1010Sstevel@tonic-gate /* Otherwise, is there any global server ctrls ? */
1020Sstevel@tonic-gate if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
1030Sstevel@tonic-gate ld->ld_errno = LDAP_ENCODING_ERROR;
1040Sstevel@tonic-gate ber_free( ber, 1 );
1050Sstevel@tonic-gate return( NULLBER );
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if ( ber_printf( ber, "}" ) == -1 ) {
1100Sstevel@tonic-gate ld->ld_errno = LDAP_ENCODING_ERROR;
1110Sstevel@tonic-gate ber_free( ber, 1 );
1120Sstevel@tonic-gate return( NULLBER );
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate return (ber);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate * ldap_add - initiate an ldap (and X.500) add operation. Parameters:
1210Sstevel@tonic-gate *
1220Sstevel@tonic-gate * ld LDAP descriptor
1230Sstevel@tonic-gate * dn DN of the entry to add
1240Sstevel@tonic-gate * mods List of attributes for the entry. This is a null-
1250Sstevel@tonic-gate * terminated array of pointers to LDAPMod structures.
1260Sstevel@tonic-gate * only the type and values in the structures need be
1270Sstevel@tonic-gate * filled in.
1280Sstevel@tonic-gate *
1290Sstevel@tonic-gate * Example:
1300Sstevel@tonic-gate * LDAPMod *attrs[] = {
1310Sstevel@tonic-gate * { 0, "cn", { "babs jensen", "babs", 0 } },
1320Sstevel@tonic-gate * { 0, "sn", { "jensen", 0 } },
1330Sstevel@tonic-gate * { 0, "objectClass", { "person", 0 } },
1340Sstevel@tonic-gate * 0
1350Sstevel@tonic-gate * }
1360Sstevel@tonic-gate * msgid = ldap_add( ld, dn, attrs );
1370Sstevel@tonic-gate */
ldap_add(LDAP * ld,char * dn,LDAPMod ** attrs)1380Sstevel@tonic-gate int ldap_add( LDAP *ld, char *dn, LDAPMod **attrs )
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate BerElement *ber;
1410Sstevel@tonic-gate int rv;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate #ifdef _REENTRANT
1440Sstevel@tonic-gate LOCK_LDAP(ld);
1450Sstevel@tonic-gate #endif
1460Sstevel@tonic-gate Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 87, "ldap_add\n"), 0, 0, 0 );
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if ((ber = ldap_build_add_req(ld, dn, attrs, NULL)) == NULLBER){
1490Sstevel@tonic-gate #ifdef _REENTRANT
1500Sstevel@tonic-gate UNLOCK_LDAP(ld);
1510Sstevel@tonic-gate #endif
1520Sstevel@tonic-gate return (-1);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /* send the message */
1560Sstevel@tonic-gate rv = send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
1570Sstevel@tonic-gate #ifdef _REENTRANT
1580Sstevel@tonic-gate UNLOCK_LDAP(ld);
1590Sstevel@tonic-gate #endif
1600Sstevel@tonic-gate return (rv);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate int
ldap_add_s(LDAP * ld,char * dn,LDAPMod ** attrs)1640Sstevel@tonic-gate ldap_add_s( LDAP *ld, char *dn, LDAPMod **attrs )
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate int msgid;
1670Sstevel@tonic-gate LDAPMessage *res;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if ( (msgid = ldap_add( ld, dn, attrs )) == -1 )
1700Sstevel@tonic-gate return( ld->ld_errno );
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
1730Sstevel@tonic-gate return( ld->ld_errno );
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate return( ldap_result2error( ld, res, 1 ) );
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /* ldapv3 API extensions */
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate * ldap_add_ext - initiate an ldap (and X.500) add operation. Parameters:
1820Sstevel@tonic-gate *
1830Sstevel@tonic-gate * ld LDAP descriptor
1840Sstevel@tonic-gate * dn DN of the entry to add
1850Sstevel@tonic-gate * attrs List of attributes for the entry. This is a null-
1860Sstevel@tonic-gate * terminated array of pointers to LDAPMod structures.
1870Sstevel@tonic-gate * only the type and values in the structures need be
1880Sstevel@tonic-gate * filled in.
1890Sstevel@tonic-gate * serverctrls List of server controls. This is a null-terminated
1900Sstevel@tonic-gate * array of pointers to LDAPControl structures.
1910Sstevel@tonic-gate * clientctrls List of client controls.
1920Sstevel@tonic-gate *
1930Sstevel@tonic-gate * Example:
1940Sstevel@tonic-gate * LDAPMod *attrs[] = {
1950Sstevel@tonic-gate * { 0, "cn", { "babs jensen", "babs", 0 } },
1960Sstevel@tonic-gate * { 0, "sn", { "jensen", 0 } },
1970Sstevel@tonic-gate * { 0, "objectClass", { "person", 0 } },
1980Sstevel@tonic-gate * 0
1990Sstevel@tonic-gate * }
2000Sstevel@tonic-gate *
2010Sstevel@tonic-gate * retcode = ldap_add_ext( ld, dn, attrs, srvctrls, cltctrls, &msgid );
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate
ldap_add_ext(LDAP * ld,char * dn,LDAPMod ** attrs,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)2040Sstevel@tonic-gate int ldap_add_ext(LDAP *ld, char *dn, LDAPMod **attrs,
2050Sstevel@tonic-gate LDAPControl ** serverctrls, LDAPControl **clientctrls, int *msgidp)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate BerElement *ber;
2080Sstevel@tonic-gate int i, rc;
2090Sstevel@tonic-gate int rv;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate #ifdef _REENTRANT
2120Sstevel@tonic-gate LOCK_LDAP(ld);
2130Sstevel@tonic-gate #endif
2140Sstevel@tonic-gate Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 87, "ldap_add\n"), 0, 0, 0 );
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if ((ber = ldap_build_add_req(ld, dn, attrs, serverctrls)) == NULLBER){
2170Sstevel@tonic-gate rv = ld->ld_errno;
2180Sstevel@tonic-gate if (rv == LDAP_SUCCESS)
2190Sstevel@tonic-gate rv = LDAP_OTHER;
2200Sstevel@tonic-gate #ifdef _REENTRANT
2210Sstevel@tonic-gate UNLOCK_LDAP(ld);
2220Sstevel@tonic-gate #endif
2230Sstevel@tonic-gate return (rv);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /* send the message */
2270Sstevel@tonic-gate rv = send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
2280Sstevel@tonic-gate if (rv == -1) {
2290Sstevel@tonic-gate rv = ld->ld_errno;
2300Sstevel@tonic-gate if (rv == LDAP_SUCCESS){
2310Sstevel@tonic-gate rv = LDAP_OTHER;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate #ifdef _REENTRANT
2350Sstevel@tonic-gate UNLOCK_LDAP(ld);
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate return (rv);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate *msgidp = rv;
2410Sstevel@tonic-gate #ifdef _REENTRANT
2420Sstevel@tonic-gate UNLOCK_LDAP(ld);
2430Sstevel@tonic-gate #endif
2440Sstevel@tonic-gate return (LDAP_SUCCESS);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
ldap_add_ext_s(LDAP * ld,char * dn,LDAPMod ** attrs,LDAPControl ** serverctrls,LDAPControl ** clientctrls)2470Sstevel@tonic-gate int ldap_add_ext_s(LDAP *ld, char *dn, LDAPMod **attrs,
2480Sstevel@tonic-gate LDAPControl ** serverctrls, LDAPControl **clientctrls)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate int msgid;
2510Sstevel@tonic-gate int retcode = LDAP_SUCCESS;
2520Sstevel@tonic-gate LDAPMessage *res;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if ((retcode = ldap_add_ext(ld, dn, attrs, serverctrls, clientctrls, &msgid)) != LDAP_SUCCESS)
2550Sstevel@tonic-gate return (retcode);
2560Sstevel@tonic-gate if (ldap_result(ld, msgid, 1, (struct timeval *)NULL, &res ) == -1)
2570Sstevel@tonic-gate return (ld->ld_errno );
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate #ifdef _REENTRANT
2600Sstevel@tonic-gate LOCK_LDAP(ld);
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate retcode = ldap_parse_result(ld, res, &ld->ld_errno, &ld->ld_matched, &ld->ld_error,
2630Sstevel@tonic-gate &ld->ld_referrals, &ld->ld_ret_ctrls, 1);
2640Sstevel@tonic-gate if (retcode == LDAP_SUCCESS)
2650Sstevel@tonic-gate retcode = ld->ld_errno;
2660Sstevel@tonic-gate #ifdef _REENTRANT
2670Sstevel@tonic-gate UNLOCK_LDAP(ld);
2680Sstevel@tonic-gate #endif
2690Sstevel@tonic-gate return (retcode);
2700Sstevel@tonic-gate }
271