]> git.sur5r.net Git - openldap/blob - libraries/libldap/modify.c
Suck in HEAD changes since 2.1alpha
[openldap] / libraries / libldap / modify.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  *  modify.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #include "ldap-int.h"
22
23 /*
24  * ldap_modify_ext - initiate an ldap extended modify operation.
25  *
26  * Parameters:
27  *
28  *      ld              LDAP descriptor
29  *      dn              DN of the object to modify
30  *      mods            List of modifications to make.  This is null-terminated
31  *                      array of struct ldapmod's, specifying the modifications
32  *                      to perform.
33  *      sctrls  Server Controls
34  *      cctrls  Client Controls
35  *      msgidp  Message ID pointer
36  *
37  * Example:
38  *      LDAPMod *mods[] = { 
39  *                      { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
40  *                      { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
41  *                      0
42  *              }
43  *      rc=  ldap_modify_ext( ld, dn, mods, sctrls, cctrls, &msgid );
44  */
45 int
46 ldap_modify_ext( LDAP *ld,
47         LDAP_CONST char *dn,
48         LDAPMod **mods,
49         LDAPControl **sctrls,
50         LDAPControl **cctrls,
51         int *msgidp )
52 {
53         BerElement      *ber;
54         int             i, rc;
55
56         /*
57          * A modify request looks like this:
58          *      ModifyRequet ::= SEQUENCE {
59          *              object          DistinguishedName,
60          *              modifications   SEQUENCE OF SEQUENCE {
61          *                      operation       ENUMERATED {
62          *                              add     (0),
63          *                              delete  (1),
64          *                              replace (2)
65          *                      },
66          *                      modification    SEQUENCE {
67          *                              type    AttributeType,
68          *                              values  SET OF AttributeValue
69          *                      }
70          *              }
71          *      }
72          */
73
74 #ifdef NEW_LOGGING
75         LDAP_LOG (( "modify", LDAP_LEVEL_ENTRY, "ldap_modify_ext\n" ));
76 #else
77         Debug( LDAP_DEBUG_TRACE, "ldap_modify_ext\n", 0, 0, 0 );
78 #endif
79
80         /* check client controls */
81         rc = ldap_int_client_controls( ld, cctrls );
82         if( rc != LDAP_SUCCESS ) return rc;
83
84         /* create a message to send */
85         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
86                 return( LDAP_NO_MEMORY );
87         }
88
89         if ( ber_printf( ber, "{it{s{" /*}}}*/, ++ld->ld_msgid, LDAP_REQ_MODIFY, dn )
90             == -1 ) {
91                 ld->ld_errno = LDAP_ENCODING_ERROR;
92                 ber_free( ber, 1 );
93                 return( ld->ld_errno );
94         }
95
96         /* for each modification to be performed... */
97         for ( i = 0; mods[i] != NULL; i++ ) {
98                 if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
99                         rc = ber_printf( ber, "{e{s[V]N}N}",
100                             (ber_int_t) ( mods[i]->mod_op & ~LDAP_MOD_BVALUES ),
101                             mods[i]->mod_type, mods[i]->mod_bvalues );
102                 } else {
103                         rc = ber_printf( ber, "{e{s[v]N}N}",
104                                 (ber_int_t) mods[i]->mod_op,
105                             mods[i]->mod_type, mods[i]->mod_values );
106                 }
107
108                 if ( rc == -1 ) {
109                         ld->ld_errno = LDAP_ENCODING_ERROR;
110                         ber_free( ber, 1 );
111                         return( ld->ld_errno );
112                 }
113         }
114
115         if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
116                 ld->ld_errno = LDAP_ENCODING_ERROR;
117                 ber_free( ber, 1 );
118                 return( ld->ld_errno );
119         }
120
121         /* Put Server Controls */
122         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
123                 ber_free( ber, 1 );
124                 return ld->ld_errno;
125         }
126
127         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
128                 ld->ld_errno = LDAP_ENCODING_ERROR;
129                 ber_free( ber, 1 );
130                 return( ld->ld_errno );
131         }
132
133         /* send the message */
134         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber );
135         return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
136 }
137
138 /*
139  * ldap_modify - initiate an ldap modify operation.
140  *
141  * Parameters:
142  *
143  *      ld              LDAP descriptor
144  *      dn              DN of the object to modify
145  *      mods            List of modifications to make.  This is null-terminated
146  *                      array of struct ldapmod's, specifying the modifications
147  *                      to perform.
148  *
149  * Example:
150  *      LDAPMod *mods[] = { 
151  *                      { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
152  *                      { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
153  *                      0
154  *              }
155  *      msgid = ldap_modify( ld, dn, mods );
156  */
157 int
158 ldap_modify( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
159 {
160         int rc, msgid;
161
162 #ifdef NEW_LOGGING
163         LDAP_LOG (( "modify", LDAP_LEVEL_ENTRY, "ldap_modify\n" ));
164 #else
165         Debug( LDAP_DEBUG_TRACE, "ldap_modify\n", 0, 0, 0 );
166 #endif
167
168         rc = ldap_modify_ext( ld, dn, mods, NULL, NULL, &msgid );
169
170         if ( rc != LDAP_SUCCESS )
171                 return -1;
172
173         return msgid;
174 }
175
176 int
177 ldap_modify_ext_s( LDAP *ld, LDAP_CONST char *dn,
178         LDAPMod **mods, LDAPControl **sctrl, LDAPControl **cctrl )
179 {
180         int             rc;
181         int             msgid;
182         LDAPMessage     *res;
183
184         rc = ldap_modify_ext( ld, dn, mods, sctrl, cctrl, &msgid );
185
186         if ( rc != LDAP_SUCCESS )
187                 return( rc );
188
189         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
190                 return( ld->ld_errno );
191
192         return( ldap_result2error( ld, res, 1 ) );
193 }
194
195 int
196 ldap_modify_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
197 {
198         return ldap_modify_ext_s( ld, dn, mods, NULL, NULL );
199 }
200