]> git.sur5r.net Git - openldap/blob - libraries/libldap/add.c
blind fix of value_match when SLAP_NVALUES is set
[openldap] / libraries / libldap / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  add.c
11  */
12
13 /*
14  * An add request looks like this:
15  *      AddRequest ::= SEQUENCE {
16  *              entry   DistinguishedName,
17  *              attrs   SEQUENCE OF SEQUENCE {
18  *                      type    AttributeType,
19  *                      values  SET OF AttributeValue
20  *              }
21  *      }
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31
32 #include "ldap-int.h"
33
34 /*
35  * ldap_add - initiate an ldap add operation.  Parameters:
36  *
37  *      ld              LDAP descriptor
38  *      dn              DN of the entry to add
39  *      mods            List of attributes for the entry.  This is a null-
40  *                      terminated array of pointers to LDAPMod structures.
41  *                      only the type and values in the structures need be
42  *                      filled in.
43  *
44  * Example:
45  *      LDAPMod *attrs[] = { 
46  *                      { 0, "cn", { "babs jensen", "babs", 0 } },
47  *                      { 0, "sn", { "jensen", 0 } },
48  *                      { 0, "objectClass", { "person", 0 } },
49  *                      0
50  *              }
51  *      msgid = ldap_add( ld, dn, attrs );
52  */
53 int
54 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
55 {
56         int rc;
57         int msgid;
58
59         rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
60
61         if ( rc != LDAP_SUCCESS )
62                 return -1;
63
64         return msgid;
65 }
66
67
68 /*
69  * ldap_add_ext - initiate an ldap extended add operation.  Parameters:
70  *
71  *      ld              LDAP descriptor
72  *      dn              DN of the entry to add
73  *      mods            List of attributes for the entry.  This is a null-
74  *                      terminated array of pointers to LDAPMod structures.
75  *                      only the type and values in the structures need be
76  *                      filled in.
77  *      sctrl   Server Controls
78  *      cctrl   Client Controls
79  *      msgidp  Message ID pointer
80  *
81  * Example:
82  *      LDAPMod *attrs[] = { 
83  *                      { 0, "cn", { "babs jensen", "babs", 0 } },
84  *                      { 0, "sn", { "jensen", 0 } },
85  *                      { 0, "objectClass", { "person", 0 } },
86  *                      0
87  *              }
88  *      rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
89  */
90 int
91 ldap_add_ext(
92         LDAP *ld,
93         LDAP_CONST char *dn,
94         LDAPMod **attrs,
95         LDAPControl **sctrls,
96         LDAPControl **cctrls,
97         int     *msgidp )
98 {
99         BerElement      *ber;
100         int             i, rc;
101         ber_int_t       id;
102
103 #ifdef NEW_LOGGING
104         LDAP_LOG ( OPERATION, ENTRY, "ldap_add_ext\n", 0, 0, 0 );
105 #else
106         Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
107 #endif
108         assert( ld != NULL );
109         assert( LDAP_VALID( ld ) );
110         assert( dn != NULL );
111         assert( msgidp != NULL );
112
113         /* check client controls */
114         rc = ldap_int_client_controls( ld, cctrls );
115         if( rc != LDAP_SUCCESS ) return rc;
116
117         /* create a message to send */
118         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
119                 ld->ld_errno = LDAP_NO_MEMORY;
120                 return ld->ld_errno;
121         }
122
123         LDAP_NEXT_MSGID(ld, id);
124         rc = ber_printf( ber, "{it{s{", /* '}}}' */
125                 id, LDAP_REQ_ADD, dn );
126
127         if ( rc == -1 ) {
128                 ld->ld_errno = LDAP_ENCODING_ERROR;
129                 ber_free( ber, 1 );
130                 return ld->ld_errno;
131         }
132
133         /* for each attribute in the entry... */
134         for ( i = 0; attrs[i] != NULL; i++ ) {
135                 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
136                         rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
137                             attrs[i]->mod_bvalues );
138                 } else {
139                         rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
140                             attrs[i]->mod_values );
141                 }
142                 if ( rc == -1 ) {
143                         ld->ld_errno = LDAP_ENCODING_ERROR;
144                         ber_free( ber, 1 );
145                         return ld->ld_errno;
146                 }
147         }
148
149         if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
150                 ld->ld_errno = LDAP_ENCODING_ERROR;
151                 ber_free( ber, 1 );
152                 return ld->ld_errno;
153         }
154
155         /* Put Server Controls */
156         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
157                 ber_free( ber, 1 );
158                 return ld->ld_errno;
159         }
160
161         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
162                 ld->ld_errno = LDAP_ENCODING_ERROR;
163                 ber_free( ber, 1 );
164                 return ld->ld_errno;
165         }
166
167         /* send the message */
168         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
169
170         if(*msgidp < 0)
171                 return ld->ld_errno;
172
173         return LDAP_SUCCESS;
174 }
175
176 int
177 ldap_add_ext_s(
178         LDAP *ld,
179         LDAP_CONST char *dn,
180         LDAPMod **attrs,
181         LDAPControl **sctrls,
182         LDAPControl **cctrls )
183 {
184         int             msgid, rc;
185         LDAPMessage     *res;
186
187         rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
188
189         if ( rc != LDAP_SUCCESS )
190                 return( rc );
191
192         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
193                 return( ld->ld_errno );
194
195         return( ldap_result2error( ld, res, 1 ) );
196 }
197
198 int
199 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
200 {
201         return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );
202 }
203