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