]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
46dbf83b91de5510fe2682a295159804f2e30381
[openldap] / servers / slapd / starttls.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2002 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 (
25         Connection *conn,
26         Operation *op,
27         const char * reqoid,
28         struct berval * reqdata,
29         char ** rspoid,
30         struct berval ** rspdata,
31         LDAPControl ***rspctrls,
32         const char ** text,
33         BerVarray * refs )
34 {
35         void *ctx;
36         int rc;
37
38         if ( reqdata != NULL ) {
39                 /* no request data should be provided */
40                 *text = "no request data expected";
41                 return LDAP_PROTOCOL_ERROR;
42         }
43
44         /* acquire connection lock */
45         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
46
47         /* can't start TLS if it is already started */
48         if (conn->c_is_tls != 0) {
49                 *text = "TLS already started";
50                 rc = LDAP_OPERATIONS_ERROR;
51                 goto done;
52         }
53
54         /* can't start TLS if there are other op's around */
55         if (( !LDAP_STAILQ_EMPTY(&conn->c_ops) &&
56                         (LDAP_STAILQ_FIRST(&conn->c_ops) != op ||
57                         LDAP_STAILQ_NEXT(op, o_next) != NULL)) ||
58                 ( !LDAP_STAILQ_EMPTY(&conn->c_pending_ops) ))
59         {
60                 *text = "cannot start TLS when operations are outstanding";
61                 rc = LDAP_OPERATIONS_ERROR;
62                 goto done;
63         }
64
65         if ( !( global_disallows & SLAP_DISALLOW_TLS_2_ANON ) &&
66                 ( conn->c_dn.bv_len != 0 ) )
67         {
68                 Statslog( LDAP_DEBUG_STATS,
69                         "conn=%lu op=%lu AUTHZ anonymous mech=starttls ssf=0",
70                         op->o_connid, op->o_opid, 0, 0, 0 );
71
72                 /* force to anonymous */
73                 connection2anonymous( conn );
74         }
75
76         if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
77                 ( conn->c_dn.bv_len != 0 ) )
78         {
79                 *text = "cannot start TLS after authentication";
80                 rc = LDAP_OPERATIONS_ERROR;
81                 goto done;
82         }
83
84         /* fail if TLS could not be initialized */
85         if (ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &ctx ) != 0
86                 || ctx == NULL)
87         {
88                 if (default_referral != NULL) {
89                         /* caller will put the referral in the result */
90                         rc = LDAP_REFERRAL;
91                         goto done;
92                 }
93
94                 *text = "Could not initialize TLS";
95                 rc = LDAP_UNAVAILABLE;
96                 goto done;
97         }
98
99     conn->c_is_tls = 1;
100     conn->c_needs_tls_accept = 1;
101
102     rc = LDAP_SUCCESS;
103
104 done:
105         /* give up connection lock */
106         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
107
108         /*
109          * RACE CONDITION: we give up lock before sending result
110          * Should be resolved by reworking connection state, not
111          * by moving send here (so as to ensure proper TLS sequencing)
112          */
113
114         return rc;
115 }
116
117 #endif  /* HAVE_TLS */