]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
e8170ad69056d1115a21482d181073aa318b6566
[openldap] / servers / slapd / starttls.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/socket.h>
20
21 #include "slap.h"
22
23 #ifdef HAVE_TLS
24
25 int
26 starttls_extop ( Operation *op, SlapReply *rs )
27 {
28         void *ctx;
29         int rc;
30
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;
35         }
36
37         /* acquire connection lock */
38         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
39
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;
44                 goto done;
45         }
46
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) ))
52         {
53                 rs->sr_text = "cannot start TLS when operations are outstanding";
54                 rc = LDAP_OPERATIONS_ERROR;
55                 goto done;
56         }
57
58         if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
59                 ( op->o_conn->c_dn.bv_len != 0 ) )
60         {
61                 Statslog( LDAP_DEBUG_STATS,
62                         "%s AUTHZ anonymous mech=starttls ssf=0\n",
63                         op->o_log_prefix, 0, 0, 0, 0 );
64
65                 /* force to anonymous */
66                 connection2anonymous( op->o_conn );
67         }
68
69         if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
70                 ( op->o_conn->c_dn.bv_len != 0 ) )
71         {
72                 rs->sr_text = "cannot start TLS after authentication";
73                 rc = LDAP_OPERATIONS_ERROR;
74                 goto done;
75         }
76
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 */
81                         rc = LDAP_REFERRAL;
82                         goto done;
83                 }
84
85                 rs->sr_text = "Could not initialize TLS";
86                 rc = LDAP_UNAVAILABLE;
87                 goto done;
88         }
89
90     op->o_conn->c_is_tls = 1;
91     op->o_conn->c_needs_tls_accept = 1;
92
93     rc = LDAP_SUCCESS;
94
95 done:
96         /* give up connection lock */
97         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
98
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)
102          */
103
104         return rc;
105 }
106
107 #endif  /* HAVE_TLS */