2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/socket.h>
26 starttls_extop ( Operation *op, SlapReply *rs )
30 if ( op->ore_reqdata != NULL ) {
31 /* no request data should be provided */
32 rs->sr_text = "no request data expected";
33 return LDAP_PROTOCOL_ERROR;
36 /* acquire connection lock */
37 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
39 /* can't start TLS if it is already started */
40 if (op->o_conn->c_is_tls != 0) {
41 rs->sr_text = "TLS already started";
42 rc = LDAP_OPERATIONS_ERROR;
46 /* can't start TLS if there are other op's around */
47 if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
48 (LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
49 LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
50 ( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
52 rs->sr_text = "cannot start TLS when operations are outstanding";
53 rc = LDAP_OPERATIONS_ERROR;
57 if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
58 ( op->o_conn->c_dn.bv_len != 0 ) )
60 Statslog( LDAP_DEBUG_STATS,
61 "%s AUTHZ anonymous mech=starttls ssf=0\n",
62 op->o_log_prefix, 0, 0, 0, 0 );
64 /* force to anonymous */
65 connection2anonymous( op->o_conn );
68 if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
69 ( op->o_conn->c_dn.bv_len != 0 ) )
71 rs->sr_text = "cannot start TLS after authentication";
72 rc = LDAP_OPERATIONS_ERROR;
76 /* fail if TLS could not be initialized */
77 if ( slap_tls_ctx == NULL ) {
78 if (default_referral != NULL) {
79 /* caller will put the referral in the result */
84 rs->sr_text = "Could not initialize TLS";
85 rc = LDAP_UNAVAILABLE;
89 op->o_conn->c_is_tls = 1;
90 op->o_conn->c_needs_tls_accept = 1;
95 /* give up connection lock */
96 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
98 /* FIXME: RACE CONDITION! we give up lock before sending result
99 * Should be resolved by reworking connection state, not
100 * by moving send here (so as to ensure proper TLS sequencing)
106 #endif /* HAVE_TLS */