]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/extended.c
Fallout from ITS#4986 - remove unused param of select_backend()
[openldap] / servers / slapd / back-ldap / extended.c
1 /* extended.c - ldap backend extended routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Howard Chu for inclusion
18  * in OpenLDAP Software and subsequently enhanced by Pierangelo
19  * Masarati. 
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "back-ldap.h"
29 #include "lber_pvt.h"
30
31 typedef int (ldap_back_exop_f)( Operation *op, SlapReply *rs, ldapconn_t **lc );
32
33 static ldap_back_exop_f ldap_back_exop_passwd;
34 static ldap_back_exop_f ldap_back_exop_generic;
35
36 static struct exop {
37         struct berval           oid;
38         ldap_back_exop_f        *extended;
39 } exop_table[] = {
40         { BER_BVC(LDAP_EXOP_MODIFY_PASSWD),     ldap_back_exop_passwd },
41         { BER_BVNULL, NULL }
42 };
43
44 static int
45 ldap_back_extended_one( Operation *op, SlapReply *rs, ldap_back_exop_f exop )
46 {
47         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
48
49         ldapconn_t      *lc = NULL;
50         LDAPControl     **oldctrls = NULL;
51         int             rc;
52
53         /* FIXME: this needs to be called here, so it is
54          * called twice; maybe we could avoid the 
55          * ldap_back_dobind() call inside each extended()
56          * call ... */
57         if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
58                 return -1;
59         }
60
61         oldctrls = op->o_ctrls;
62         if ( ldap_back_proxy_authz_ctrl( &lc->lc_bound_ndn,
63                 li->li_version, &li->li_idassert, op, rs, &op->o_ctrls ) )
64         {
65                 op->o_ctrls = oldctrls;
66                 send_ldap_extended( op, rs );
67                 rs->sr_text = NULL;
68                 /* otherwise frontend resends result */
69                 rc = rs->sr_err = SLAPD_ABANDON;
70                 goto done;
71         }
72
73         rc = exop( op, rs, &lc );
74
75         if ( op->o_ctrls && op->o_ctrls != oldctrls ) {
76                 free( op->o_ctrls[ 0 ] );
77                 free( op->o_ctrls );
78         }
79         op->o_ctrls = oldctrls;
80
81 done:;
82         if ( lc != NULL ) {
83                 ldap_back_release_conn( li, lc );
84         }
85                         
86         return rc;
87 }
88
89 int
90 ldap_back_extended(
91                 Operation       *op,
92                 SlapReply       *rs )
93 {
94         int     i;
95
96         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
97                 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
98                 {
99                         return ldap_back_extended_one( op, rs, exop_table[i].extended );
100                 }
101         }
102
103         /* if we get here, the exop is known; the best that we can do
104          * is pass it thru as is */
105         /* FIXME: maybe a list of OIDs to pass thru would be safer */
106         return ldap_back_extended_one( op, rs, ldap_back_exop_generic );
107 }
108
109 static int
110 ldap_back_exop_passwd(
111         Operation       *op,
112         SlapReply       *rs,
113         ldapconn_t      **lcp )
114 {
115         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
116
117         ldapconn_t      *lc = *lcp;
118         req_pwdexop_s   *qpw = &op->oq_pwdexop;
119         LDAPMessage     *res;
120         ber_int_t       msgid;
121         int             rc, isproxy, freedn = 0;
122         int             do_retry = 1;
123         char            *text = NULL;
124         struct berval   dn = op->o_req_dn,
125                         ndn = op->o_req_ndn;
126
127         assert( lc != NULL );
128
129         if ( BER_BVISNULL( &ndn ) && op->ore_reqdata != NULL ) {
130                 /* NOTE: most of this code is mutuated
131                  * from slap_passwd_parse(); we can't call
132                  * that function since now the request data
133                  * has been destroyed by NULL-terminating
134                  * the bervals.  Luckily enough, we only need
135                  * the first berval... */
136
137                 ber_tag_t tag;
138                 ber_len_t len = -1;
139                 BerElementBuffer berbuf;
140                 BerElement *ber = (BerElement *)&berbuf;
141
142                 struct berval   tmpid = BER_BVNULL;
143
144                 if ( op->ore_reqdata->bv_len == 0 ) {
145                         return LDAP_PROTOCOL_ERROR;
146                 }
147
148                 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
149                 ber_init2( ber, op->ore_reqdata, 0 );
150
151                 tag = ber_scanf( ber, "{" /*}*/ );
152
153                 if ( tag == LBER_ERROR ) {
154                         return LDAP_PROTOCOL_ERROR;
155                 }
156
157                 tag = ber_peek_tag( ber, &len );
158                 if ( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
159                         tag = ber_scanf( ber, "m", &tmpid );
160
161                         if ( tag == LBER_ERROR ) {
162                                 return LDAP_PROTOCOL_ERROR;
163                         }
164                 }
165
166                 if ( !BER_BVISEMPTY( &tmpid ) ) {
167                         rs->sr_err = dnPrettyNormal( NULL, &tmpid, &dn,
168                                 &ndn, op->o_tmpmemctx );
169                         if ( rs->sr_err != LDAP_SUCCESS ) {
170                                 /* should have been successfully parsed earlier! */
171                                 return rs->sr_err;
172                         }
173                         freedn = 1;
174
175                 } else {
176                         dn = op->o_dn;
177                         ndn = op->o_ndn;
178                 }
179         }
180
181         isproxy = ber_bvcmp( &ndn, &op->o_ndn );
182
183         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n",
184                 dn.bv_val, isproxy ? " (proxy)" : "", 0 );
185
186 retry:
187         rc = ldap_passwd( lc->lc_ld, isproxy ? &dn : NULL,
188                 qpw->rs_old.bv_val ? &qpw->rs_old : NULL,
189                 qpw->rs_new.bv_val ? &qpw->rs_new : NULL,
190                 op->o_ctrls, NULL, &msgid );
191
192         if ( rc == LDAP_SUCCESS ) {
193                 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 ) {
194                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
195                         rs->sr_err = rc;
196
197                 } else {
198                         /* only touch when activity actually took place... */
199                         if ( li->li_idle_timeout && lc ) {
200                                 lc->lc_time = op->o_time;
201                         }
202
203                         /* sigh. parse twice, because parse_passwd
204                          * doesn't give us the err / match / msg info.
205                          */
206                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
207                                         (char **)&rs->sr_matched,
208                                         &text,
209                                         NULL, NULL, 0 );
210
211                         if ( rc == LDAP_SUCCESS ) {
212                                 if ( rs->sr_err == LDAP_SUCCESS ) {
213                                         struct berval   newpw;
214
215                                         /* this never happens because 
216                                          * the frontend is generating 
217                                          * the new password, so when
218                                          * the passwd exop is proxied,
219                                          * it never delegates password
220                                          * generation to the remote server
221                                          */
222                                         rc = ldap_parse_passwd( lc->lc_ld, res,
223                                                         &newpw );
224                                         if ( rc == LDAP_SUCCESS &&
225                                                         !BER_BVISNULL( &newpw ) )
226                                         {
227                                                 rs->sr_type = REP_EXTENDED;
228                                                 rs->sr_rspdata = slap_passwd_return( &newpw );
229                                                 free( newpw.bv_val );
230                                         }
231
232                                 } else {
233                                         rc = rs->sr_err;
234                                 }
235                         }
236                         ldap_msgfree( res );
237                 }
238         }
239
240         if ( rc != LDAP_SUCCESS ) {
241                 rs->sr_err = slap_map_api2result( rs );
242                 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
243                         do_retry = 0;
244                         if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
245                                 goto retry;
246                         }
247                 }
248
249                 if ( LDAP_BACK_QUARANTINE( li ) ) {
250                         ldap_back_quarantine( op, rs );
251                 }
252
253                 if ( text ) rs->sr_text = text;
254                 send_ldap_extended( op, rs );
255                 /* otherwise frontend resends result */
256                 rc = rs->sr_err = SLAPD_ABANDON;
257
258         } else if ( LDAP_BACK_QUARANTINE( li ) ) {
259                 ldap_back_quarantine( op, rs );
260         }
261
262         if ( freedn ) {
263                 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
264                 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
265         }
266
267         /* these have to be freed anyway... */
268         if ( rs->sr_matched ) {
269                 free( (char *)rs->sr_matched );
270                 rs->sr_matched = NULL;
271         }
272
273         if ( text ) {
274                 free( text );
275                 rs->sr_text = NULL;
276         }
277
278         /* in case, cleanup handler */
279         if ( lc == NULL ) {
280                 *lcp = NULL;
281         }
282
283         return rc;
284 }
285
286 static int
287 ldap_back_exop_generic(
288         Operation       *op,
289         SlapReply       *rs,
290         ldapconn_t      **lcp )
291 {
292         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
293
294         ldapconn_t      *lc = *lcp;
295         LDAPMessage     *res;
296         ber_int_t       msgid;
297         int             rc;
298         int             do_retry = 1;
299         char            *text = NULL;
300
301         assert( lc != NULL );
302
303         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_generic(%s, \"%s\")\n",
304                 op->ore_reqoid.bv_val, op->o_req_dn.bv_val, 0 );
305
306 retry:
307         rc = ldap_extended_operation( lc->lc_ld,
308                 op->ore_reqoid.bv_val, op->ore_reqdata,
309                 op->o_ctrls, NULL, &msgid );
310
311         if ( rc == LDAP_SUCCESS ) {
312                 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 ) {
313                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
314                         rs->sr_err = rc;
315
316                 } else {
317                         /* only touch when activity actually took place... */
318                         if ( li->li_idle_timeout && lc ) {
319                                 lc->lc_time = op->o_time;
320                         }
321
322                         /* sigh. parse twice, because parse_passwd
323                          * doesn't give us the err / match / msg info.
324                          */
325                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
326                                         (char **)&rs->sr_matched,
327                                         &text,
328                                         NULL, NULL, 0 );
329                         if ( rc == LDAP_SUCCESS ) {
330                                 if ( rs->sr_err == LDAP_SUCCESS ) {
331                                         rc = ldap_parse_extended_result( lc->lc_ld, res,
332                                                         (char **)&rs->sr_rspoid, &rs->sr_rspdata, 0 );
333                                         if ( rc == LDAP_SUCCESS ) {
334                                                 rs->sr_type = REP_EXTENDED;
335                                         }
336
337                                 } else {
338                                         rc = rs->sr_err;
339                                 }
340                         }
341                         ldap_msgfree( res );
342                 }
343         }
344
345         if ( rc != LDAP_SUCCESS ) {
346                 rs->sr_err = slap_map_api2result( rs );
347                 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
348                         do_retry = 0;
349                         if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
350                                 goto retry;
351                         }
352                 }
353
354                 if ( LDAP_BACK_QUARANTINE( li ) ) {
355                         ldap_back_quarantine( op, rs );
356                 }
357
358                 if ( text ) rs->sr_text = text;
359                 send_ldap_extended( op, rs );
360                 /* otherwise frontend resends result */
361                 rc = rs->sr_err = SLAPD_ABANDON;
362
363         } else if ( LDAP_BACK_QUARANTINE( li ) ) {
364                 ldap_back_quarantine( op, rs );
365         }
366
367         /* these have to be freed anyway... */
368         if ( rs->sr_matched ) {
369                 free( (char *)rs->sr_matched );
370                 rs->sr_matched = NULL;
371         }
372
373         if ( text ) {
374                 free( text );
375                 rs->sr_text = NULL;
376         }
377
378         /* in case, cleanup handler */
379         if ( lc == NULL ) {
380                 *lcp = NULL;
381         }
382
383         return rc;
384 }
385