]> git.sur5r.net Git - openldap/blob - libraries/libldap/add.c
Fix ldap_int_get_controls for optional values
[openldap] / libraries / libldap / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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
102 #ifdef NEW_LOGGING
103         LDAP_LOG ( OPERATION, ENTRY, "ldap_add_ext\n", 0, 0, 0 );
104 #else
105         Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
106 #endif
107         assert( ld != NULL );
108         assert( LDAP_VALID( ld ) );
109         assert( dn != NULL );
110         assert( msgidp != NULL );
111
112         /* check client controls */
113         rc = ldap_int_client_controls( ld, cctrls );
114         if( rc != LDAP_SUCCESS ) return rc;
115
116         /* create a message to send */
117         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
118                 ld->ld_errno = LDAP_NO_MEMORY;
119                 return ld->ld_errno;
120         }
121
122         rc = ber_printf( ber, "{it{s{", /* '}}}' */
123                 ++ld->ld_msgid, LDAP_REQ_ADD, dn );
124
125         if ( rc == -1 ) {
126                 ld->ld_errno = LDAP_ENCODING_ERROR;
127                 ber_free( ber, 1 );
128                 return ld->ld_errno;
129         }
130
131         /* for each attribute in the entry... */
132         for ( i = 0; attrs[i] != NULL; i++ ) {
133                 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
134                         rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
135                             attrs[i]->mod_bvalues );
136                 } else {
137                         rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
138                             attrs[i]->mod_values );
139                 }
140                 if ( rc == -1 ) {
141                         ld->ld_errno = LDAP_ENCODING_ERROR;
142                         ber_free( ber, 1 );
143                         return ld->ld_errno;
144                 }
145         }
146
147         if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
148                 ld->ld_errno = LDAP_ENCODING_ERROR;
149                 ber_free( ber, 1 );
150                 return ld->ld_errno;
151         }
152
153         /* Put Server Controls */
154         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
155                 ber_free( ber, 1 );
156                 return ld->ld_errno;
157         }
158
159         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
160                 ld->ld_errno = LDAP_ENCODING_ERROR;
161                 ber_free( ber, 1 );
162                 return ld->ld_errno;
163         }
164
165         /* send the message */
166         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
167
168         if(*msgidp < 0)
169                 return ld->ld_errno;
170
171         return LDAP_SUCCESS;
172 }
173
174 int
175 ldap_add_ext_s(
176         LDAP *ld,
177         LDAP_CONST char *dn,
178         LDAPMod **attrs,
179         LDAPControl **sctrls,
180         LDAPControl **cctrls )
181 {
182         int             msgid, rc;
183         LDAPMessage     *res;
184
185         rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
186
187         if ( rc != LDAP_SUCCESS )
188                 return( rc );
189
190         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
191                 return( ld->ld_errno );
192
193         return( ldap_result2error( ld, res, 1 ) );
194 }
195
196 int
197 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
198 {
199         return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );
200 }
201