]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/extended.c
allow pretty time form in quarantine string
[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-2006 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 static BI_op_extended ldap_back_exop_passwd;
32 static BI_op_extended ldap_back_exop_generic;
33
34 static struct exop {
35         struct berval   oid;
36         BI_op_extended  *extended;
37 } exop_table[] = {
38         { BER_BVC(LDAP_EXOP_MODIFY_PASSWD),     ldap_back_exop_passwd },
39         { BER_BVNULL, NULL }
40 };
41
42 static int
43 ldap_back_extended_one( Operation *op, SlapReply *rs, BI_op_extended exop )
44 {
45         ldapconn_t      *lc;
46         LDAPControl     **oldctrls = NULL;
47         int             rc;
48
49         /* FIXME: this needs to be called here, so it is
50          * called twice; maybe we could avoid the 
51          * ldap_back_dobind() call inside each extended()
52          * call ... */
53         lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
54         if ( !lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
55                 return -1;
56         }
57
58         oldctrls = op->o_ctrls;
59         if ( ldap_back_proxy_authz_ctrl( lc, op, rs, &op->o_ctrls ) ) {
60                 op->o_ctrls = oldctrls;
61                 send_ldap_extended( op, rs );
62                 rs->sr_text = NULL;
63                 /* otherwise frontend resends result */
64                 rc = rs->sr_err = SLAPD_ABANDON;
65                 goto done;
66         }
67
68         rc = exop( op, rs );
69
70         if ( op->o_ctrls && op->o_ctrls != oldctrls ) {
71                 free( op->o_ctrls[ 0 ] );
72                 free( op->o_ctrls );
73         }
74         op->o_ctrls = oldctrls;
75
76 done:;
77         if ( lc != NULL ) {
78                 ldap_back_release_conn( op, rs, lc );
79         }
80                         
81         return rc;
82 }
83
84 int
85 ldap_back_extended(
86                 Operation       *op,
87                 SlapReply       *rs )
88 {
89         int     i;
90
91         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
92                 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
93                 {
94                         return ldap_back_extended_one( op, rs, exop_table[i].extended );
95                 }
96         }
97
98         /* if we get here, the exop is known; the best that we can do
99          * is pass it thru as is */
100         /* FIXME: maybe a list of OIDs to pass thru would be safer */
101         return ldap_back_extended_one( op, rs, ldap_back_exop_generic );
102 }
103
104 static int
105 ldap_back_exop_passwd(
106                 Operation       *op,
107                 SlapReply       *rs )
108 {
109         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
110
111         ldapconn_t      *lc;
112         req_pwdexop_s   *qpw = &op->oq_pwdexop;
113         LDAPMessage     *res;
114         ber_int_t       msgid;
115         int             rc, isproxy;
116         int             do_retry = 1;
117         char *text = NULL;
118
119         lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
120         if ( !lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
121                 return -1;
122         }
123
124         isproxy = ber_bvcmp( &op->o_req_ndn, &op->o_ndn );
125
126         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n",
127                 op->o_req_dn.bv_val, isproxy ? " (proxy)" : "", 0 );
128
129 retry:
130         rc = ldap_passwd( lc->lc_ld, isproxy ? &op->o_req_dn : NULL,
131                 qpw->rs_old.bv_val ? &qpw->rs_old : NULL,
132                 qpw->rs_new.bv_val ? &qpw->rs_new : NULL,
133                 op->o_ctrls, NULL, &msgid );
134
135         if ( rc == LDAP_SUCCESS ) {
136                 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 ) {
137                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
138                         rs->sr_err = rc;
139
140                 } else {
141                         /* sigh. parse twice, because parse_passwd
142                          * doesn't give us the err / match / msg info.
143                          */
144                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
145                                         (char **)&rs->sr_matched,
146                                         &text,
147                                         NULL, NULL, 0 );
148
149                         if ( rc == LDAP_SUCCESS ) {
150                                 if ( rs->sr_err == LDAP_SUCCESS ) {
151                                         struct berval   newpw;
152
153                                         /* this never happens because 
154                                          * the frontend is generating 
155                                          * the new password, so when
156                                          * the passwd exop is proxied,
157                                          * it never delegates password
158                                          * generation to the remote server
159                                          */
160                                         rc = ldap_parse_passwd( lc->lc_ld, res,
161                                                         &newpw );
162                                         if ( rc == LDAP_SUCCESS &&
163                                                         !BER_BVISNULL( &newpw ) )
164                                         {
165                                                 rs->sr_type = REP_EXTENDED;
166                                                 rs->sr_rspdata = slap_passwd_return( &newpw );
167                                                 free( newpw.bv_val );
168                                         }
169
170                                 } else {
171                                         rc = rs->sr_err;
172                                 }
173                         }
174                         ldap_msgfree( res );
175                 }
176         }
177
178         if ( rc != LDAP_SUCCESS ) {
179                 rs->sr_err = slap_map_api2result( rs );
180                 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
181                         do_retry = 0;
182                         if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
183                                 goto retry;
184                         }
185                 }
186
187                 if ( LDAP_BACK_QUARANTINE( li ) ) {
188                         ldap_back_quarantine( op, rs, 1 );
189                 }
190
191                 if ( text ) rs->sr_text = text;
192                 send_ldap_extended( op, rs );
193                 /* otherwise frontend resends result */
194                 rc = rs->sr_err = SLAPD_ABANDON;
195
196         } else if ( LDAP_BACK_QUARANTINE( li ) ) {
197                 ldap_back_quarantine( op, rs, 1 );
198         }
199
200         /* these have to be freed anyway... */
201         if ( rs->sr_matched ) {
202                 free( (char *)rs->sr_matched );
203                 rs->sr_matched = NULL;
204         }
205
206         if ( text ) {
207                 free( text );
208                 rs->sr_text = NULL;
209         }
210
211         if ( lc != NULL ) {
212                 ldap_back_release_conn( op, rs, lc );
213         }
214
215         return rc;
216 }
217
218 static int
219 ldap_back_exop_generic(
220         Operation       *op,
221         SlapReply       *rs )
222 {
223         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
224
225         ldapconn_t      *lc;
226         LDAPMessage     *res;
227         ber_int_t       msgid;
228         int             rc;
229         int             do_retry = 1;
230         char *text = NULL;
231
232         lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
233         if ( !lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
234                 return -1;
235         }
236
237         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_generic(%s, \"%s\")\n",
238                 op->ore_reqoid.bv_val, op->o_req_dn.bv_val, 0 );
239
240 retry:
241         rc = ldap_extended_operation( lc->lc_ld,
242                 op->ore_reqoid.bv_val, op->ore_reqdata,
243                 op->o_ctrls, NULL, &msgid );
244
245         if ( rc == LDAP_SUCCESS ) {
246                 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &res ) == -1 ) {
247                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
248                         rs->sr_err = rc;
249
250                 } else {
251                         /* sigh. parse twice, because parse_passwd
252                          * doesn't give us the err / match / msg info.
253                          */
254                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
255                                         (char **)&rs->sr_matched,
256                                         &text,
257                                         NULL, NULL, 0 );
258                         if ( rc == LDAP_SUCCESS ) {
259                                 if ( rs->sr_err == LDAP_SUCCESS ) {
260                                         rc = ldap_parse_extended_result( lc->lc_ld, res,
261                                                         (char **)&rs->sr_rspoid, &rs->sr_rspdata, 0 );
262                                         if ( rc == LDAP_SUCCESS ) {
263                                                 rs->sr_type = REP_EXTENDED;
264                                         }
265
266                                 } else {
267                                         rc = rs->sr_err;
268                                 }
269                         }
270                         ldap_msgfree( res );
271                 }
272         }
273
274         if ( rc != LDAP_SUCCESS ) {
275                 rs->sr_err = slap_map_api2result( rs );
276                 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
277                         do_retry = 0;
278                         if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
279                                 goto retry;
280                         }
281                 }
282
283                 if ( LDAP_BACK_QUARANTINE( li ) ) {
284                         ldap_back_quarantine( op, rs, 1 );
285                 }
286
287                 if ( text ) rs->sr_text = text;
288                 send_ldap_extended( op, rs );
289                 /* otherwise frontend resends result */
290                 rc = rs->sr_err = SLAPD_ABANDON;
291
292         } else if ( LDAP_BACK_QUARANTINE( li ) ) {
293                 ldap_back_quarantine( op, rs, 1 );
294         }
295
296         /* these have to be freed anyway... */
297         if ( rs->sr_matched ) {
298                 free( (char *)rs->sr_matched );
299                 rs->sr_matched = NULL;
300         }
301
302         if ( text ) {
303                 free( text );
304                 rs->sr_text = NULL;
305         }
306
307         if ( lc != NULL ) {
308                 ldap_back_release_conn( op, rs, lc );
309         }
310
311         return rc;
312 }
313