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