]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/extended.c
Happy New Year!
[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-2005 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 BI_op_extended ldap_back_exop_passwd;
32
33 static struct exop {
34         struct berval   *oid;
35         BI_op_extended  *extended;
36 } exop_table[] = {
37         { (struct berval *)&slap_EXOP_MODIFY_PASSWD, ldap_back_exop_passwd },
38         { NULL, NULL }
39 };
40
41 int
42 ldap_back_extended(
43                 Operation       *op,
44                 SlapReply       *rs )
45 {
46         int     i;
47
48         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
49                 if ( bvmatch( exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
50                 {
51 #ifdef LDAP_BACK_PROXY_AUTHZ 
52                         struct ldapconn *lc;
53                         LDAPControl     **oldctrls = NULL;
54                         int             rc;
55
56                         /* FIXME: this needs to be called here, so it is
57                          * called twice; maybe we could avoid the 
58                          * ldap_back_dobind() call inside each extended()
59                          * call ... */
60                         lc = ldap_back_getconn( op, rs );
61                         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
62                                 return -1;
63                         }
64
65                         oldctrls = op->o_ctrls;
66                         if ( ldap_back_proxy_authz_ctrl( lc, op, rs,
67                                                 &op->o_ctrls ) )
68                         {
69                                 op->o_ctrls = oldctrls;
70                                 send_ldap_result( op, rs );
71                                 rs->sr_text = NULL;
72                                 return rs->sr_err;
73                         }
74
75                         rc = ( *exop_table[i].extended )( op, rs );
76
77                         if ( op->o_ctrls && op->o_ctrls != oldctrls ) {
78                                 free( op->o_ctrls[ 0 ] );
79                                 free( op->o_ctrls );
80                         }
81                         op->o_ctrls = oldctrls;
82
83                         return rc;
84 #else /* ! LDAP_BACK_PROXY_AUTHZ */
85                         return ( *exop_table[i].extended )( op, rs );
86 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
87                 }
88         }
89
90         rs->sr_text = "not supported within naming context";
91         return LDAP_UNWILLING_TO_PERFORM;
92 }
93
94 int
95 ldap_back_exop_passwd(
96                 Operation       *op,
97                 SlapReply       *rs )
98 {
99         struct ldapconn *lc;
100         req_pwdexop_s   *qpw = &op->oq_pwdexop;
101         LDAPMessage     *res;
102         ber_int_t       msgid;
103         int             rc, isproxy;
104         int             do_retry = 1;
105
106         lc = ldap_back_getconn( op, rs );
107         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
108                 return -1;
109         }
110
111         isproxy = ber_bvcmp( &op->o_req_ndn, &op->o_ndn );
112
113         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n",
114                 op->o_req_dn.bv_val, isproxy ? " (proxy)" : "", 0 );
115
116 retry:
117         rc = ldap_passwd( lc->lc_ld, isproxy ? &op->o_req_dn : NULL,
118                 qpw->rs_old.bv_val ? &qpw->rs_old : NULL,
119                 qpw->rs_new.bv_val ? &qpw->rs_new : NULL,
120                 op->o_ctrls, NULL, &msgid );
121
122         if ( rc == LDAP_SUCCESS ) {
123                 if ( ldap_result( lc->lc_ld, msgid, 1, NULL, &res ) == -1 ) {
124                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc );
125                         ldap_back_freeconn( op, lc );
126                         lc = NULL;
127
128                 } else {
129                         /* sigh. parse twice, because parse_passwd
130                          * doesn't give us the err / match / msg info.
131                          */
132                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
133                                         (char **)&rs->sr_matched,
134                                         (char **)&rs->sr_text,
135                                         NULL, NULL, 0 );
136                         if ( rc == LDAP_SUCCESS ) {
137                                 if ( rs->sr_err == LDAP_SUCCESS ) {
138                                         struct berval   newpw;
139                                         
140                                         rc = ldap_parse_passwd( lc->lc_ld, res,
141                                                         &newpw);
142                                         if ( rc == LDAP_SUCCESS &&
143                                                         !BER_BVISNULL( &newpw ) )
144                                         {
145                                                 rs->sr_type = REP_EXTENDED;
146                                                 rs->sr_rspdata = slap_passwd_return( &newpw );
147                                                 free( newpw.bv_val );
148                                         }
149
150                                 } else {
151                                         rc = rs->sr_err;
152                                 }
153                         }
154                         ldap_msgfree( res );
155                 }
156         }
157         if ( rc != LDAP_SUCCESS ) {
158                 rs->sr_err = slap_map_api2result( rs );
159                 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
160                         do_retry = 0;
161                         if ( ldap_back_retry(lc, op, rs ) ) {
162                                 goto retry;
163                         }
164                 }
165                 send_ldap_result( op, rs );
166                 if ( rs->sr_matched ) {
167                         free( (char *)rs->sr_matched );
168                 }
169                 if ( rs->sr_text ) {
170                         free( (char *)rs->sr_text );
171                 }
172                 rs->sr_matched = NULL;
173                 rs->sr_text = NULL;
174                 rc = -1;
175         }
176
177         return rc;
178 }