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