]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
ff164a4c61a178fa85713389b4de5401f564b1d9
[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 (ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &ctx ) != 0
81                 || ctx == NULL)
82         {
83                 if (default_referral != NULL) {
84                         /* caller will put the referral in the result */
85                         rc = LDAP_REFERRAL;
86                         goto done;
87                 }
88
89                 rs->sr_text = "Could not initialize TLS";
90                 rc = LDAP_UNAVAILABLE;
91                 goto done;
92         }
93
94     op->o_conn->c_is_tls = 1;
95     op->o_conn->c_needs_tls_accept = 1;
96
97     rc = LDAP_SUCCESS;
98
99 done:
100         /* give up connection lock */
101         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
102
103         /*
104          * RACE CONDITION: we give up lock before sending result
105          * Should be resolved by reworking connection state, not
106          * by moving send here (so as to ensure proper TLS sequencing)
107          */
108
109         return rc;
110 }
111
112 #endif  /* HAVE_TLS */