2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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 )
31 if ( op->ore_reqdata != NULL ) {
32 /* no request data should be provided */
33 rs->sr_text = "no request data expected";
34 return LDAP_PROTOCOL_ERROR;
37 /* acquire connection lock */
38 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
40 /* can't start TLS if it is already started */
41 if (op->o_conn->c_is_tls != 0) {
42 rs->sr_text = "TLS already started";
43 rc = LDAP_OPERATIONS_ERROR;
47 /* can't start TLS if there are other op's around */
48 if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
49 (LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
50 LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
51 ( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
53 rs->sr_text = "cannot start TLS when operations are outstanding";
54 rc = LDAP_OPERATIONS_ERROR;
58 if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
59 ( op->o_conn->c_dn.bv_len != 0 ) )
61 Statslog( LDAP_DEBUG_STATS,
62 "%s AUTHZ anonymous mech=starttls ssf=0\n",
63 op->o_log_prefix, 0, 0, 0, 0 );
65 /* force to anonymous */
66 connection2anonymous( op->o_conn );
69 if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
70 ( op->o_conn->c_dn.bv_len != 0 ) )
72 rs->sr_text = "cannot start TLS after authentication";
73 rc = LDAP_OPERATIONS_ERROR;
77 /* fail if TLS could not be initialized */
78 if ( slap_tls_ctx == NULL ) {
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 );
99 /* FIXME: RACE CONDITION! we give up lock before sending result
100 * Should be resolved by reworking connection state, not
101 * by moving send here (so as to ensure proper TLS sequencing)
107 #endif /* HAVE_TLS */