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