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