]> git.sur5r.net Git - openldap/blob - libraries/libldap/modify.c
Vienna Bulk Commit
[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 )) == NULLBER ) {
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                             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]}}", mods[i]->mod_op,
95                             mods[i]->mod_type, mods[i]->mod_values );
96                 }
97
98                 if ( rc == -1 ) {
99                         ld->ld_errno = LDAP_ENCODING_ERROR;
100                         ber_free( ber, 1 );
101                         return( ld->ld_errno );
102                 }
103         }
104
105         if ( ber_printf( ber, "}}" ) == -1 ) {
106                 ld->ld_errno = LDAP_ENCODING_ERROR;
107                 ber_free( ber, 1 );
108                 return( ld->ld_errno );
109         }
110
111         /* Put Server Controls */
112         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
113                 ber_free( ber, 1 );
114                 return ld->ld_errno;
115         }
116
117         if ( ber_printf( ber, "}" ) == -1 ) {
118                 ld->ld_errno = LDAP_ENCODING_ERROR;
119                 ber_free( ber, 1 );
120                 return( ld->ld_errno );
121         }
122
123         /* send the message */
124         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber );
125         return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
126 }
127
128 /*
129  * ldap_modify - initiate an ldap modify operation.
130  *
131  * Parameters:
132  *
133  *      ld              LDAP descriptor
134  *      dn              DN of the object to modify
135  *      mods            List of modifications to make.  This is null-terminated
136  *                      array of struct ldapmod's, specifying the modifications
137  *                      to perform.
138  *
139  * Example:
140  *      LDAPMod *mods[] = { 
141  *                      { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
142  *                      { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
143  *                      0
144  *              }
145  *      msgid = ldap_modify( ld, dn, mods );
146  */
147 int
148 ldap_modify( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
149 {
150         int rc, msgid;
151
152         Debug( LDAP_DEBUG_TRACE, "ldap_modify\n", 0, 0, 0 );
153
154         rc = ldap_modify_ext( ld, dn, mods, NULL, NULL, &msgid );
155
156         if ( rc != LDAP_SUCCESS )
157                 return -1;
158
159         return msgid;
160 }
161
162 int
163 ldap_modify_ext_s( LDAP *ld, LDAP_CONST char *dn,
164         LDAPMod **mods, LDAPControl **sctrl, LDAPControl **cctrl )
165 {
166         int             rc;
167         int             msgid;
168         LDAPMessage     *res;
169
170         rc = ldap_modify_ext( ld, dn, mods, sctrl, cctrl, &msgid );
171
172         if ( rc != LDAP_SUCCESS )
173                 return( rc );
174
175         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
176                 return( ld->ld_errno );
177
178         return( ldap_result2error( ld, res, 1 ) );
179 }
180
181 int
182 ldap_modify_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
183 {
184         return ldap_modify_ext_s( ld, dn, mods, NULL, NULL );
185 }
186