]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Added connection initialisation and destruction notification. Now backends can regist...
[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(LDAPModList *ml);
24
25 void
26 do_modify(
27     Connection  *conn,
28     Operation   *op
29 )
30 {
31         char            *ndn;
32         char            *last;
33         ber_tag_t       tag;
34         ber_len_t       len;
35         LDAPModList     *modlist;
36         LDAPModList     **modtail;
37 #ifdef LDAP_DEBUG
38         LDAPModList *tmp;
39 #endif
40         Backend         *be;
41
42         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
43
44         /*
45          * Parse the modify request.  It looks like this:
46          *
47          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
48          *              name    DistinguishedName,
49          *              mods    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         if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
64                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
65                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
66                 return;
67         }
68
69         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
70
71         (void) dn_normalize_case( ndn );
72
73         /* collect modifications & save for later */
74         modlist = NULL;
75         modtail = &modlist;
76
77         for ( tag = ber_first_element( op->o_ber, &len, &last );
78             tag != LBER_DEFAULT;
79             tag = ber_next_element( op->o_ber, &len, last ) )
80         {
81                 ber_int_t mop;
82
83                 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
84
85                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
86                     &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
87                     == LBER_ERROR )
88                 {
89                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
90                             "decoding error" );
91                         free( ndn );
92                         free( *modtail );
93                         *modtail = NULL;
94                         modlist_free( modlist );
95                         return;
96                 }
97
98                 (*modtail)->ml_op = mop;
99                 
100                 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
101                     (*modtail)->ml_op != LDAP_MOD_DELETE &&
102                     (*modtail)->ml_op != LDAP_MOD_REPLACE )
103                 {
104                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
105                             "unrecognized modify operation" );
106                         free( ndn );
107                         modlist_free( modlist );
108                         return;
109                 }
110
111                 if ( (*modtail)->ml_bvalues == NULL
112                         && (*modtail)->ml_op != LDAP_MOD_DELETE )
113                 {
114                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
115                             "no values given" );
116                         free( ndn );
117                         modlist_free( modlist );
118                         return;
119                 }
120                 attr_normalize( (*modtail)->ml_type );
121
122                 modtail = &(*modtail)->ml_next;
123         }
124         *modtail = NULL;
125
126 #ifdef LDAP_DEBUG
127         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
128         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
129                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
130                         tmp->ml_op == LDAP_MOD_ADD
131                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
132                                         ? "delete" : "replace"), tmp->ml_type, 0 );
133         }
134 #endif
135
136         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
137             conn->c_connid, op->o_opid, ndn, 0, 0 );
138
139         /*
140          * We could be serving multiple database backends.  Select the
141          * appropriate one, or send a referral to our "referral server"
142          * if we don't hold it.
143          */
144         if ( (be = select_backend( ndn )) == NULL ) {
145                 free( ndn );
146                 modlist_free( modlist );
147                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
148                     default_referral );
149                 return;
150         }
151
152         /* alias suffix if approp */
153         ndn = suffixAlias ( ndn, op, be );
154
155         /*
156          * do the modify if 1 && (2 || 3)
157          * 1) there is a modify function implemented in this backend;
158          * 2) this backend is master for what it holds;
159          * 3) it's a replica and the dn supplied is the update_ndn.
160          */
161         if ( be->be_modify ) {
162                 /* do the update here */
163                 if ( be->be_update_ndn == NULL ||
164                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
165                 {
166                         if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 ) {
167                                 replog( be, LDAP_REQ_MODIFY, ndn, modlist, 0 );
168                         }
169
170                 /* send a referral */
171                 } else {
172                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
173                             default_referral );
174                 }
175         } else {
176                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
177                     "Function not implemented" );
178         }
179
180         free( ndn );
181         modlist_free( modlist );
182 }
183
184 static void
185 modlist_free(
186     LDAPModList *ml
187 )
188 {
189         LDAPModList *next;
190
191         for ( ; ml != NULL; ml = next ) {
192                 next = ml->ml_next;
193
194                 free( ml->ml_type );
195                 if ( ml->ml_bvalues != NULL )
196                         ber_bvecfree( ml->ml_bvalues );
197
198                 free( ml );
199         }
200 }