]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
43066ba136e47312b17bf13bb27e3bca883f8347
[openldap] / servers / slapd / starttls.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2003 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15 #include <ac/socket.h>
16
17 #include <ldap_pvt.h>
18
19 #include "slap.h"
20
21 #ifdef HAVE_TLS
22
23 int
24 starttls_extop ( Operation *op, SlapReply *rs )
25 {
26         void *ctx;
27         int rc;
28
29         if ( op->oq_extended.rs_reqdata != NULL ) {
30                 /* no request data should be provided */
31                 rs->sr_text = "no request data expected";
32                 return LDAP_PROTOCOL_ERROR;
33         }
34
35         /* acquire connection lock */
36         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
37
38         /* can't start TLS if it is already started */
39         if (op->o_conn->c_is_tls != 0) {
40                 rs->sr_text = "TLS already started";
41                 rc = LDAP_OPERATIONS_ERROR;
42                 goto done;
43         }
44
45         /* can't start TLS if there are other op's around */
46         if (( !LDAP_STAILQ_EMPTY(&op->o_conn->c_ops) &&
47                         (LDAP_STAILQ_FIRST(&op->o_conn->c_ops) != op ||
48                         LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
49                 ( !LDAP_STAILQ_EMPTY(&op->o_conn->c_pending_ops) ))
50         {
51                 rs->sr_text = "cannot start TLS when operations are outstanding";
52                 rc = LDAP_OPERATIONS_ERROR;
53                 goto done;
54         }
55
56         if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
57                 ( op->o_conn->c_dn.bv_len != 0 ) )
58         {
59                 Statslog( LDAP_DEBUG_STATS,
60                         "conn=%lu op=%lu AUTHZ anonymous mech=starttls ssf=0",
61                         op->o_connid, op->o_opid, 0, 0, 0 );
62
63                 /* force to anonymous */
64                 connection2anonymous( op->o_conn );
65         }
66
67         if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
68                 ( op->o_conn->c_dn.bv_len != 0 ) )
69         {
70                 rs->sr_text = "cannot start TLS after authentication";
71                 rc = LDAP_OPERATIONS_ERROR;
72                 goto done;
73         }
74
75         /* fail if TLS could not be initialized */
76         if (ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &ctx ) != 0
77                 || ctx == NULL)
78         {
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         /*
100          * RACE CONDITION: we give up lock before sending result
101          * Should be resolved by reworking connection state, not
102          * by moving send here (so as to ensure proper TLS sequencing)
103          */
104
105         return rc;
106 }
107
108 #endif  /* HAVE_TLS */