summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/chainingdb/cb_bind.c
blob: dee83b3b69e930fd365cfbc71fbcbf3c21bf4555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * do so, delete this exception statement from your version. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
#include "cb.h"

static void
cb_free_bervals( struct berval **bvs );


static int
cb_sasl_bind_once_s( cb_conn_pool *pool, char *dn, int method, char * mechanism,
        struct berval *creds, LDAPControl **reqctrls,
        char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
        LDAPControl ***resctrlsp , int * status);

/*
 * Attempt to chain a bind request off to "srvr." We return an LDAP error
 * code that indicates whether we successfully got a response from the
 * other server or not.  If we succeed, we return LDAP_SUCCESS and *lderrnop
 * is set to the result code from the remote server.
 *
 * Note that in the face of "ldap server down" or "ldap connect failed" errors
 * we make up to "tries" attempts to bind to the remote server.  Since we
 * are only interested in recovering silently when the remote server is up
 * but decided to close our connection, we retry without pausing between
 * attempts.
 */

static int
cb_sasl_bind_s(Slapi_PBlock * pb, cb_conn_pool *pool, int tries,
        char *dn, int method,char * mechanism, struct berval *creds, LDAPControl **reqctrls,
        char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
        LDAPControl ***resctrlsp ,int *status) {

    int         rc;
 
    do {
         /* check to see if operation has been abandoned...*/

	if (LDAP_AUTH_SIMPLE!=method)
		return LDAP_AUTH_METHOD_NOT_SUPPORTED;

        if ( slapi_op_abandoned( pb )) {
            rc = LDAP_USER_CANCELLED;
        } else {
            rc = cb_sasl_bind_once_s( pool, dn, method,mechanism, creds, reqctrls,
                     matcheddnp, errmsgp, refurlsp, resctrlsp ,status);
        }
    } while ( CB_LDAP_CONN_ERROR( rc ) && --tries > 0 );
       
    return( rc );
}

static int
cb_sasl_bind_once_s( cb_conn_pool *pool, char *dn, int method, char * mechanism,
        struct berval *creds, LDAPControl **reqctrls,
        char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
        LDAPControl ***resctrlsp , int * status) {

    int                 rc, msgid;
    char                **referrals;
    struct timeval      timeout_copy, *timeout;
    LDAPMessage         *result=NULL;
    LDAP                *ld=NULL;
    char 		*cnxerrbuf=NULL;
    cb_outgoing_conn	*cnx;
    int version=LDAP_VERSION3;
	
    /* Grab an LDAP connection to use for this bind. */

    PR_RWLock_Rlock(pool->rwl_config_lock);
    timeout_copy.tv_sec = pool->conn.bind_timeout.tv_sec;
    timeout_copy.tv_usec = pool->conn.bind_timeout.tv_usec;
    PR_RWLock_Unlock(pool->rwl_config_lock);

    if (( rc = cb_get_connection( pool, &ld ,&cnx, NULL, &cnxerrbuf)) != LDAP_SUCCESS ) {
	*errmsgp=cnxerrbuf;
        goto release_and_return;
    }
       
    /* Send the bind operation (need to retry on LDAP_SERVER_DOWN) */
    
    ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );

    if (( rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, creds, reqctrls,
                NULL, &msgid )) != LDAP_SUCCESS ) {
        goto release_and_return;
    }

	/* XXXSD what is the exact semantics of bind_to ? it is used to get a
	connection handle and later to bind ==> bind op may last 2*bind_to
	from the user point of view 
	confusion comes from teh fact that bind to is used 2for 3 differnt thinks,	
	*/

    /*
     * determine timeout value (how long we will wait for a response)
     * if timeout is zero'd, we wait indefinitely.
     */
    if ( timeout_copy.tv_sec == 0 && timeout_copy.tv_usec == 0 ) {
        timeout = NULL;
    } else {
	timeout = &timeout_copy;
    }
       
    /*
     * Wait for a result.
     */
    rc = ldap_result( ld, msgid, 1, timeout, &result );
 
    /*
     * Interpret the result.
     */

   if ( rc == 0 ) {            /* timeout */
        /*
         * Timed out waiting for a reply from the server.
         */
        rc = LDAP_TIMEOUT;
    } else if ( rc < 0 ) {

        /* Some other error occurred (no result received). */
	char * matcheddnp2, * errmsgp2;
	matcheddnp2=errmsgp2=NULL;

        rc = ldap_get_lderrno( ld, &matcheddnp2, &errmsgp2 );

	/* Need to allocate errmsgs */
	if (matcheddnp2)
		*matcheddnp=slapi_ch_strdup(matcheddnp2);
	if (errmsgp2)
		*errmsgp=slapi_ch_strdup(errmsgp2);
	
	if ( LDAP_SUCCESS != rc )  {
        	slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
			"cb_sasl_bind_once_s failed (%s)\n",ldap_err2string(rc));
	}
    } else {

        /* Got a result from remote server -- parse it.*/

	char * matcheddnp2, * errmsgp2;
	matcheddnp2=errmsgp2=NULL;
	*resctrlsp=NULL;
        rc = ldap_parse_result( ld, result, status, matcheddnp, errmsgp,
                &referrals, resctrlsp, 1 );
        if ( referrals != NULL ) {
            *refurlsp = referrals2berval( referrals );
            ldap_value_free( referrals );
        }
	/* realloc matcheddn & errmsg because the mem alloc model */
	/* may differ from malloc				  */
	if (matcheddnp2) {
		*matcheddnp=slapi_ch_strdup(matcheddnp2);
		ldap_memfree(matcheddnp2);
	}
	if (errmsgp2) {
		*errmsgp=slapi_ch_strdup(errmsgp2);
		ldap_memfree(errmsgp2);
	}

    }

release_and_return:
    if ( ld != NULL ) {
        cb_release_op_connection( pool, ld, CB_LDAP_CONN_ERROR( rc ));
    }
       
    return( rc );
}

int
chainingdb_bind( Slapi_PBlock *pb ) {

	int 			status=LDAP_SUCCESS;
	int 			allocated_errmsg;
	int 			rc=LDAP_SUCCESS;
	cb_backend_instance 	*cb;
	Slapi_Backend		*be;
	char                    *dn;
        int                     method;
        struct berval           *creds, **urls;
	char 			*matcheddn,*errmsg;
    	LDAPControl         	**reqctrls, **resctrls, **ctrls;
	char 			* mechanism;
	int 			freectrls=1;
	int 			bind_retry;
	
        if ( LDAP_SUCCESS != (rc = cb_forward_operation(pb) )) {
        	cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
                return SLAPI_BIND_FAIL;
        }

	ctrls=NULL;
	/* don't add proxy auth control. use this call to check for supported   */
	/* controls only.							*/
        if ( LDAP_SUCCESS != ( rc = cb_update_controls( pb, NULL, &ctrls, 0 )) ) {
                cb_send_ldap_result( pb, rc, NULL, NULL, 0, NULL );
		if (ctrls)
			ldap_controls_free(ctrls);
                return SLAPI_BIND_FAIL;
        }
	if (ctrls)
		ldap_controls_free(ctrls);

        slapi_pblock_get( pb, SLAPI_BACKEND, &be );
        slapi_pblock_get( pb, SLAPI_BIND_TARGET, &dn );
        slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method );
	slapi_pblock_get( pb, SLAPI_BIND_SASLMECHANISM, &mechanism);
        slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &creds );
        slapi_pblock_get( pb, SLAPI_REQCONTROLS, &reqctrls );
        cb = cb_get_instance(be);

	if ( NULL == dn ) 
		dn="";

        /* always allow noauth simple binds */
        if (( method == LDAP_AUTH_SIMPLE) && creds->bv_len == 0 ) {
                return( SLAPI_BIND_ANONYMOUS );
        }

        cb_update_monitor_info(pb,cb,SLAPI_OPERATION_BIND);

	matcheddn=errmsg=NULL;
    	allocated_errmsg = 0;
	resctrls=NULL;
	urls=NULL;

	/* Check wether the chaining BE is available or not */
        if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
	  return -1;
        }

        PR_RWLock_Rlock(cb->rwl_config_lock);
	bind_retry=cb->bind_retry;
        PR_RWLock_Unlock(cb->rwl_config_lock);

	if ( LDAP_SUCCESS == (rc = cb_sasl_bind_s(pb, cb->bind_pool, bind_retry, dn,method,mechanism,
		creds,reqctrls,&matcheddn,&errmsg,&urls,&resctrls, &status))) {
        	rc = status;
            	allocated_errmsg = 1;
	} else
	if ( LDAP_USER_CANCELLED != rc ) {
   		errmsg = ldap_err2string( rc );
		if (rc == LDAP_TIMEOUT) {
		  cb_ping_farm(cb,NULL,0);
		}
            	rc = LDAP_OPERATIONS_ERROR;
	}

 	if ( rc != LDAP_USER_CANCELLED ) {  /* not abandoned */
        	if ( resctrls != NULL ) {
            		slapi_pblock_set( pb, SLAPI_RESCONTROLS, resctrls );
			freectrls=0;
        	}

		if ( rc != LDAP_SUCCESS ) {
        		cb_send_ldap_result( pb, rc, matcheddn, errmsg, 0, urls );
		}
    	}

    	if ( urls != NULL ) {
        	cb_free_bervals( urls );
    	}
    	if ( freectrls && ( resctrls != NULL )) {
        	ldap_controls_free( resctrls );
    	}
        slapi_ch_free((void **)& matcheddn );    	
    	if ( allocated_errmsg && errmsg != NULL ) {
        	slapi_ch_free((void **)& errmsg );
    	}

	return ((rc == LDAP_SUCCESS ) ? SLAPI_BIND_SUCCESS : SLAPI_BIND_FAIL );
}

static void
cb_free_bervals( struct berval **bvs )
{
    int         i;

    if ( bvs != NULL ) {
        for ( i = 0; bvs[ i ] != NULL; ++i ) {
            slapi_ch_free( (void **)&bvs[ i ] );
        }
    }    
    slapi_ch_free( (void **)&bvs );
}