3 * Copyright 1999-2003 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
15 #include <ac/socket.h>
24 starttls_extop ( Operation *op, SlapReply *rs )
29 if ( op->ore_reqdata != NULL ) {
30 /* no request data should be provided */
31 rs->sr_text = "no request data expected";
32 return LDAP_PROTOCOL_ERROR;
35 /* acquire connection lock */
36 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
38 /* can't start TLS if it is already started */
39 if (op->o_conn->c_is_tls != 0) {
40 rs->sr_text = "TLS already started";
41 rc = LDAP_OPERATIONS_ERROR;
45 /* can't start TLS if there are other op's around */
46 if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
47 (LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
48 LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
49 ( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
51 rs->sr_text = "cannot start TLS when operations are outstanding";
52 rc = LDAP_OPERATIONS_ERROR;
56 if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
57 ( op->o_conn->c_dn.bv_len != 0 ) )
59 Statslog( LDAP_DEBUG_STATS,
60 "conn=%lu op=%lu AUTHZ anonymous mech=starttls ssf=0\n",
61 op->o_connid, op->o_opid, 0, 0, 0 );
63 /* force to anonymous */
64 connection2anonymous( op->o_conn );
67 if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
68 ( op->o_conn->c_dn.bv_len != 0 ) )
70 rs->sr_text = "cannot start TLS after authentication";
71 rc = LDAP_OPERATIONS_ERROR;
75 /* fail if TLS could not be initialized */
76 if (ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &ctx ) != 0
79 if (default_referral != NULL) {
80 /* caller will put the referral in the result */
85 rs->sr_text = "Could not initialize TLS";
86 rc = LDAP_UNAVAILABLE;
90 op->o_conn->c_is_tls = 1;
91 op->o_conn->c_needs_tls_accept = 1;
96 /* give up connection lock */
97 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
100 * RACE CONDITION: we give up lock before sending result
101 * Should be resolved by reworking connection state, not
102 * by moving send here (so as to ensure proper TLS sequencing)
108 #endif /* HAVE_TLS */