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