]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
update for new backend types
[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         int rc;
29
30         if ( op->ore_reqdata != NULL ) {
31                 /* no request data should be provided */
32                 rs->sr_text = "no request data expected";
33                 return LDAP_PROTOCOL_ERROR;
34         }
35
36         /* acquire connection lock */
37         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
38
39         /* can't start TLS if it is already started */
40         if (op->o_conn->c_is_tls != 0) {
41                 rs->sr_text = "TLS already started";
42                 rc = LDAP_OPERATIONS_ERROR;
43                 goto done;
44         }
45
46         /* can't start TLS if there are other op's around */
47         if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
48                         (LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
49                         LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
50                 ( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
51         {
52                 rs->sr_text = "cannot start TLS when operations are outstanding";
53                 rc = LDAP_OPERATIONS_ERROR;
54                 goto done;
55         }
56
57         if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
58                 ( op->o_conn->c_dn.bv_len != 0 ) )
59         {
60                 Statslog( LDAP_DEBUG_STATS,
61                         "%s AUTHZ anonymous mech=starttls ssf=0\n",
62                         op->o_log_prefix, 0, 0, 0, 0 );
63
64                 /* force to anonymous */
65                 connection2anonymous( op->o_conn );
66         }
67
68         if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
69                 ( op->o_conn->c_dn.bv_len != 0 ) )
70         {
71                 rs->sr_text = "cannot start TLS after authentication";
72                 rc = LDAP_OPERATIONS_ERROR;
73                 goto done;
74         }
75
76         /* fail if TLS could not be initialized */
77         if ( slap_tls_ctx == NULL ) {
78                 if (default_referral != NULL) {
79                         /* caller will put the referral in the result */
80                         rc = LDAP_REFERRAL;
81                         goto done;
82                 }
83
84                 rs->sr_text = "Could not initialize TLS";
85                 rc = LDAP_UNAVAILABLE;
86                 goto done;
87         }
88
89     op->o_conn->c_is_tls = 1;
90     op->o_conn->c_needs_tls_accept = 1;
91
92     rc = LDAP_SUCCESS;
93
94 done:
95         /* give up connection lock */
96         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
97
98         /* FIXME: RACE CONDITION! we give up lock before sending result
99          * Should be resolved by reworking connection state, not
100          * by moving send here (so as to ensure proper TLS sequencing)
101          */
102
103         return rc;
104 }
105
106 #endif  /* HAVE_TLS */