]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
From HEAD
[openldap] / servers / slapd / starttls.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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 <ldap_pvt.h>
22
23 #include "slap.h"
24
25 #ifdef HAVE_TLS
26
27 int
28 starttls_extop ( Operation *op, SlapReply *rs )
29 {
30         void *ctx;
31         int rc;
32
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;
37         }
38
39         /* acquire connection lock */
40         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
41
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;
46                 goto done;
47         }
48
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) ))
54         {
55                 rs->sr_text = "cannot start TLS when operations are outstanding";
56                 rc = LDAP_OPERATIONS_ERROR;
57                 goto done;
58         }
59
60         if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
61                 ( op->o_conn->c_dn.bv_len != 0 ) )
62         {
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 );
66
67                 /* force to anonymous */
68                 connection2anonymous( op->o_conn );
69         }
70
71         if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
72                 ( op->o_conn->c_dn.bv_len != 0 ) )
73         {
74                 rs->sr_text = "cannot start TLS after authentication";
75                 rc = LDAP_OPERATIONS_ERROR;
76                 goto done;
77         }
78
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 */
83                         rc = LDAP_REFERRAL;
84                         goto done;
85                 }
86
87                 rs->sr_text = "Could not initialize TLS";
88                 rc = LDAP_UNAVAILABLE;
89                 goto done;
90         }
91
92     op->o_conn->c_is_tls = 1;
93     op->o_conn->c_needs_tls_accept = 1;
94
95     rc = LDAP_SUCCESS;
96
97 done:
98         /* give up connection lock */
99         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
100
101         /*
102          * RACE CONDITION: we give up lock before sending result
103          * Should be resolved by reworking connection state, not
104          * by moving send here (so as to ensure proper TLS sequencing)
105          */
106
107         return rc;
108 }
109
110 #endif  /* HAVE_TLS */