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>
28 starttls_extop ( Operation *op, SlapReply *rs )
33 if ( op->ore_reqdata != NULL ) {
34 /* no request data should be provided */
35 rs->sr_text = "no request data expected";
36 return LDAP_PROTOCOL_ERROR;
39 /* acquire connection lock */
40 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
42 /* can't start TLS if it is already started */
43 if (op->o_conn->c_is_tls != 0) {
44 rs->sr_text = "TLS already started";
45 rc = LDAP_OPERATIONS_ERROR;
49 /* can't start TLS if there are other op's around */
50 if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
51 (LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
52 LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
53 ( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
55 rs->sr_text = "cannot start TLS when operations are outstanding";
56 rc = LDAP_OPERATIONS_ERROR;
60 if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
61 ( op->o_conn->c_dn.bv_len != 0 ) )
63 Statslog( LDAP_DEBUG_STATS,
64 "conn=%lu op=%lu AUTHZ anonymous mech=starttls ssf=0\n",
65 op->o_connid, op->o_opid, 0, 0, 0 );
67 /* force to anonymous */
68 connection2anonymous( op->o_conn );
71 if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
72 ( op->o_conn->c_dn.bv_len != 0 ) )
74 rs->sr_text = "cannot start TLS after authentication";
75 rc = LDAP_OPERATIONS_ERROR;
79 /* fail if TLS could not be initialized */
80 if ( slap_tls_ctx == NULL ) {
81 if (default_referral != NULL) {
82 /* caller will put the referral in the result */
87 rs->sr_text = "Could not initialize TLS";
88 rc = LDAP_UNAVAILABLE;
92 op->o_conn->c_is_tls = 1;
93 op->o_conn->c_needs_tls_accept = 1;
98 /* give up connection lock */
99 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
101 /* FIXME: RACE CONDITION! we give up lock before sending result
102 * Should be resolved by reworking connection state, not
103 * by moving send here (so as to ensure proper TLS sequencing)
109 #endif /* HAVE_TLS */