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