starttls.c 2.77 KB
Newer Older
Mark Valence's avatar
Mark Valence committed
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2019 The OpenLDAP Foundation.
Mark Valence's avatar
Mark Valence committed
5
6
 * All rights reserved.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8
9
10
11
12
13
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Mark Valence's avatar
Mark Valence committed
14
15
16
17
18
19
 */

#include "portable.h"

#include <stdio.h>
#include <ac/socket.h>
20
#include <ac/string.h>
Mark Valence's avatar
Mark Valence committed
21
22

#include "slap.h"
23
#include "lber_pvt.h"
Mark Valence's avatar
Mark Valence committed
24

25
const struct berval slap_EXOP_START_TLS = BER_BVC(LDAP_EXOP_START_TLS);
Mark Valence's avatar
Mark Valence committed
26

27
#ifdef HAVE_TLS
Mark Valence's avatar
Mark Valence committed
28
int
29
starttls_extop ( Operation *op, SlapReply *rs )
Mark Valence's avatar
Mark Valence committed
30
{
31
	int rc;
32

33
34
35
	Statslog( LDAP_DEBUG_STATS, "%s STARTTLS\n",
	    op->o_log_prefix, 0, 0, 0, 0 );

Howard Chu's avatar
Howard Chu committed
36
	if ( op->ore_reqdata != NULL ) {
37
		/* no request data should be provided */
38
		rs->sr_text = "no request data expected";
39
40
41
		return LDAP_PROTOCOL_ERROR;
	}

42
	/* acquire connection lock */
43
	ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
44

Mark Valence's avatar
Mark Valence committed
45
	/* can't start TLS if it is already started */
46
47
	if (op->o_conn->c_is_tls != 0) {
		rs->sr_text = "TLS already started";
48
49
50
		rc = LDAP_OPERATIONS_ERROR;
		goto done;
	}
Mark Valence's avatar
Mark Valence committed
51

Kurt Zeilenga's avatar
Kurt Zeilenga committed
52
	/* can't start TLS if there are other op's around */
53
54
	if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
			(LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
55
			LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
56
		( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
Kurt Zeilenga's avatar
Kurt Zeilenga committed
57
	{
58
		rs->sr_text = "cannot start TLS when operations are outstanding";
Kurt Zeilenga's avatar
Kurt Zeilenga committed
59
60
61
62
		rc = LDAP_OPERATIONS_ERROR;
		goto done;
	}

63
	if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
64
		( op->o_conn->c_dn.bv_len != 0 ) )
65
	{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66
		Statslog( LDAP_DEBUG_STATS,
67
68
			"%s AUTHZ anonymous mech=starttls ssf=0\n",
			op->o_log_prefix, 0, 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
69

70
		/* force to anonymous */
71
		connection2anonymous( op->o_conn );
72
73
	}

74
	if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
75
		( op->o_conn->c_dn.bv_len != 0 ) )
76
	{
77
		rs->sr_text = "cannot start TLS after authentication";
78
79
		rc = LDAP_OPERATIONS_ERROR;
		goto done;
80
81
	}

82
	/* fail if TLS could not be initialized */
83
84
	if ( slap_tls_ctx == NULL ) {
		if (default_referral != NULL) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
85
			/* caller will put the referral in the result */
86
87
			rc = LDAP_REFERRAL;
			goto done;
88
		}
89

90
		rs->sr_text = "Could not initialize TLS";
91
92
		rc = LDAP_UNAVAILABLE;
		goto done;
93
94
	}

95
96
    op->o_conn->c_is_tls = 1;
    op->o_conn->c_needs_tls_accept = 1;
97

98
99
100
101
    rc = LDAP_SUCCESS;

done:
	/* give up connection lock */
102
	ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
103

Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
	/* FIXME: RACE CONDITION! we give up lock before sending result
105
106
107
108
109
	 * Should be resolved by reworking connection state, not
	 * by moving send here (so as to ensure proper TLS sequencing)
	 */

	return rc;
Mark Valence's avatar
Mark Valence committed
110
111
112
}

#endif	/* HAVE_TLS */