]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Use LDAP_MOD_SOFTADD instead of LDAP_MOD_ADD when adding the new rdn as
[openldap] / servers / slapd / modify.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #include "slap.h"
22
23 static void     modlist_free(LDAPMod *mods);
24
25
26 void
27 do_modify(
28     Connection  *conn,
29     Operation   *op
30 )
31 {
32         char            *ndn;
33         char            *last;
34         unsigned long   tag, len;
35         LDAPMod         *mods, *tmp;
36         LDAPMod         **modtail;
37         Backend         *be;
38
39         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
40
41         /*
42          * Parse the modify request.  It looks like this:
43          *
44          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
45          *              name    DistinguishedName,
46          *              mods    SEQUENCE OF SEQUENCE {
47          *                      operation       ENUMERATED {
48          *                              add     (0),
49          *                              delete  (1),
50          *                              replace (2)
51          *                      },
52          *                      modification    SEQUENCE {
53          *                              type    AttributeType,
54          *                              values  SET OF AttributeValue
55          *                      }
56          *              }
57          *      }
58          */
59
60         if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
61                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
62                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
63                 return;
64         }
65
66         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
67
68         (void) dn_normalize_case( ndn );
69
70         /* collect modifications & save for later */
71         mods = NULL;
72         modtail = &mods;
73         for ( tag = ber_first_element( op->o_ber, &len, &last );
74             tag != LBER_DEFAULT;
75             tag = ber_next_element( op->o_ber, &len, last ) )
76         {
77                 (*modtail) = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
78
79                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->mod_op,
80                     &(*modtail)->mod_type, &(*modtail)->mod_bvalues )
81                     == LBER_ERROR )
82                 {
83                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
84                             "decoding error" );
85                         free( ndn );
86                         free( *modtail );
87                         *modtail = NULL;
88                         modlist_free( mods );
89                         return;
90                 }
91
92                 if ( (*modtail)->mod_op != LDAP_MOD_ADD &&
93                     (*modtail)->mod_op != LDAP_MOD_DELETE &&
94                     (*modtail)->mod_op != LDAP_MOD_REPLACE )
95                 {
96                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
97                             "unrecognized modify operation" );
98                         free( ndn );
99                         modlist_free( mods );
100                         return;
101                 }
102
103                 if ( (*modtail)->mod_bvalues == NULL && (*modtail)->mod_op
104                   != LDAP_MOD_DELETE ) {
105                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
106                             "no values given" );
107                         free( ndn );
108                         modlist_free( mods );
109                         return;
110                 }
111                 attr_normalize( (*modtail)->mod_type );
112
113                 modtail = &(*modtail)->mod_next;
114         }
115         *modtail = NULL;
116
117 #ifdef LDAP_DEBUG
118         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
119         for ( tmp = mods; tmp != NULL; tmp = tmp->mod_next ) {
120                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n", tmp->mod_op
121                     == LDAP_MOD_ADD ? "add" : (tmp->mod_op == LDAP_MOD_DELETE ?
122                     "delete" : "replace"), tmp->mod_type, 0 );
123         }
124 #endif
125
126         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
127             conn->c_connid, op->o_opid, ndn, 0, 0 );
128
129         /*
130          * We could be serving multiple database backends.  Select the
131          * appropriate one, or send a referral to our "referral server"
132          * if we don't hold it.
133          */
134         if ( (be = select_backend( ndn )) == NULL ) {
135                 free( ndn );
136                 modlist_free( mods );
137                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
138                     default_referral );
139                 return;
140         }
141
142         /* alias suffix if approp */
143         ndn = suffixAlias ( ndn, op, be );
144
145         /*
146          * do the modify if 1 && (2 || 3)
147          * 1) there is a modify function implemented in this backend;
148          * 2) this backend is master for what it holds;
149          * 3) it's a replica and the dn supplied is the update_ndn.
150          */
151         if ( be->be_modify != NULL ) {
152                 /* do the update here */
153                 if ( be->be_update_ndn == NULL ||
154                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
155                 {
156
157                         if ( (*be->be_modify)( be, conn, op, ndn, mods ) == 0 ) {
158                                 replog( be, LDAP_REQ_MODIFY, ndn, mods, 0 );
159                         }
160
161                 /* send a referral */
162                 } else {
163                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
164                             default_referral );
165                 }
166         } else {
167                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
168                     "Function not implemented" );
169         }
170
171         free( ndn );
172         modlist_free( mods );
173 }
174
175 static void
176 modlist_free(
177     LDAPMod     *mods
178 )
179 {
180         LDAPMod *next;
181
182         for ( ; mods != NULL; mods = next ) {
183                 next = mods->mod_next;
184                 free( mods->mod_type );
185                 if ( mods->mod_bvalues != NULL )
186                         ber_bvecfree( mods->mod_bvalues );
187                 free( mods );
188         }
189 }