]> git.sur5r.net Git - openldap/blob - libraries/libldap/modify.c
More files that didn't get merged properly.
[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 #include <ac/time.h>
19
20 #include "lber.h"
21 #include "ldap.h"
22 #include "ldap-int.h"
23
24 /*
25  * ldap_modify - initiate an ldap (and X.500) modify operation.  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  *
33  * Example:
34  *      LDAPMod *mods[] = { 
35  *                      { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
36  *                      { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
37  *                      0
38  *              }
39  *      msgid = ldap_modify( ld, dn, mods );
40  */
41 int
42 ldap_modify( LDAP *ld, char *dn, LDAPMod **mods )
43 {
44         BerElement      *ber;
45         int             i, rc;
46
47         /*
48          * A modify request looks like this:
49          *      ModifyRequet ::= SEQUENCE {
50          *              object          DistinguishedName,
51          *              modifications   SEQUENCE OF SEQUENCE {
52          *                      operation       ENUMERATED {
53          *                              add     (0),
54          *                              delete  (1),
55          *                              replace (2)
56          *                      },
57          *                      modification    SEQUENCE {
58          *                              type    AttributeType,
59          *                              values  SET OF AttributeValue
60          *                      }
61          *              }
62          *      }
63          */
64
65         Debug( LDAP_DEBUG_TRACE, "ldap_modify\n", 0, 0, 0 );
66
67         /* create a message to send */
68         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
69                 return( -1 );
70         }
71
72         if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_MODIFY, dn )
73             == -1 ) {
74                 ld->ld_errno = LDAP_ENCODING_ERROR;
75                 ber_free( ber, 1 );
76                 return( -1 );
77         }
78
79         /* for each modification to be performed... */
80         for ( i = 0; mods[i] != NULL; i++ ) {
81                 if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
82                         rc = ber_printf( ber, "{e{s[V]}}",
83                             mods[i]->mod_op & ~LDAP_MOD_BVALUES,
84                             mods[i]->mod_type, mods[i]->mod_bvalues );
85                 } else {
86                         rc = ber_printf( ber, "{e{s[v]}}", mods[i]->mod_op,
87                             mods[i]->mod_type, mods[i]->mod_values );
88                 }
89
90                 if ( rc == -1 ) {
91                         ld->ld_errno = LDAP_ENCODING_ERROR;
92                         ber_free( ber, 1 );
93                         return( -1 );
94                 }
95         }
96
97         if ( ber_printf( ber, "}}}" ) == -1 ) {
98                 ld->ld_errno = LDAP_ENCODING_ERROR;
99                 ber_free( ber, 1 );
100                 return( -1 );
101         }
102
103         /* send the message */
104         return( ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber ));
105 }
106
107 int
108 ldap_modify_s( LDAP *ld, char *dn, LDAPMod **mods )
109 {
110         int             msgid;
111         LDAPMessage     *res;
112
113         if ( (msgid = ldap_modify( ld, dn, mods )) == -1 )
114                 return( ld->ld_errno );
115
116         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
117                 return( ld->ld_errno );
118
119         return( ldap_result2error( ld, res, 1 ) );
120 }
121