]> git.sur5r.net Git - openldap/blob - libraries/libldap/modify.c
VC++ Port: round 1
[openldap] / libraries / libldap / modify.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  modify.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
13
14 #include <stdio.h>
15
16 #include <ac/socket.h>
17 #include <ac/string.h>
18
19 #include "lber.h"
20 #include "ldap.h"
21 #include "ldap-int.h"
22
23 /*
24  * ldap_modify - initiate an ldap (and X.500) modify operation.  Parameters:
25  *
26  *      ld              LDAP descriptor
27  *      dn              DN of the object to modify
28  *      mods            List of modifications to make.  This is null-terminated
29  *                      array of struct ldapmod's, specifying the modifications
30  *                      to perform.
31  *
32  * Example:
33  *      LDAPMod *mods[] = { 
34  *                      { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
35  *                      { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
36  *                      0
37  *              }
38  *      msgid = ldap_modify( ld, dn, mods );
39  */
40 int
41 ldap_modify( LDAP *ld, char *dn, LDAPMod **mods )
42 {
43         BerElement      *ber;
44         int             i, rc;
45
46         /*
47          * A modify request looks like this:
48          *      ModifyRequet ::= SEQUENCE {
49          *              object          DistinguishedName,
50          *              modifications   SEQUENCE OF SEQUENCE {
51          *                      operation       ENUMERATED {
52          *                              add     (0),
53          *                              delete  (1),
54          *                              replace (2)
55          *                      },
56          *                      modification    SEQUENCE {
57          *                              type    AttributeType,
58          *                              values  SET OF AttributeValue
59          *                      }
60          *              }
61          *      }
62          */
63
64         Debug( LDAP_DEBUG_TRACE, "ldap_modify\n", 0, 0, 0 );
65
66         /* create a message to send */
67         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
68                 return( -1 );
69         }
70
71         if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_MODIFY, dn )
72             == -1 ) {
73                 ld->ld_errno = LDAP_ENCODING_ERROR;
74                 ber_free( ber, 1 );
75                 return( -1 );
76         }
77
78         /* for each modification to be performed... */
79         for ( i = 0; mods[i] != NULL; i++ ) {
80                 if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
81                         rc = ber_printf( ber, "{e{s[V]}}",
82                             mods[i]->mod_op & ~LDAP_MOD_BVALUES,
83                             mods[i]->mod_type, mods[i]->mod_bvalues );
84                 } else {
85                         rc = ber_printf( ber, "{e{s[v]}}", mods[i]->mod_op,
86                             mods[i]->mod_type, mods[i]->mod_values );
87                 }
88
89                 if ( rc == -1 ) {
90                         ld->ld_errno = LDAP_ENCODING_ERROR;
91                         ber_free( ber, 1 );
92                         return( -1 );
93                 }
94         }
95
96         if ( ber_printf( ber, "}}}" ) == -1 ) {
97                 ld->ld_errno = LDAP_ENCODING_ERROR;
98                 ber_free( ber, 1 );
99                 return( -1 );
100         }
101
102         /* send the message */
103         return( ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber ));
104 }
105
106 int
107 ldap_modify_s( LDAP *ld, char *dn, LDAPMod **mods )
108 {
109         int             msgid;
110         LDAPMessage     *res;
111
112         if ( (msgid = ldap_modify( ld, dn, mods )) == -1 )
113                 return( ld->ld_errno );
114
115         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
116                 return( ld->ld_errno );
117
118         return( ldap_result2error( ld, res, 1 ) );
119 }
120