]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
97bbab084ac9dd34cbd9fd11cb3a06b1333133b0
[openldap] / servers / slapd / starttls.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2000 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 "slap.h"
18
19 #ifdef HAVE_TLS
20
21 int
22 starttls_extop (
23         Connection *conn,
24         Operation *op,
25         const char * reqoid,
26         struct berval * reqdata,
27         char ** rspoid,
28         struct berval ** rspdata,
29         LDAPControl ***rspctrls,
30         const char ** text,
31         struct berval *** refs )
32 {
33         void *ctx;
34         int rc;
35
36         if ( reqdata != NULL ) {
37                 /* no request data should be provided */
38                 *text = "no request data expected";
39                 return LDAP_PROTOCOL_ERROR;
40         }
41
42         /* acquire connection lock */
43         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
44
45         /* can't start TLS if it is already started */
46         if (conn->c_is_tls != 0) {
47                 *text = "TLS already started";
48                 rc = LDAP_OPERATIONS_ERROR;
49                 goto done;
50         }
51
52         /* can't start TLS if there are other op's around */
53         if (( conn->c_ops != NULL &&
54                         (conn->c_ops != op || op->o_next != NULL)) ||
55                 ( conn->c_pending_ops != NULL))
56         {
57                 *text = "cannot start TLS when operations our outstanding";
58                 rc = LDAP_OPERATIONS_ERROR;
59                 goto done;
60         }
61
62         if ( ( global_disallows & SLAP_DISALLOW_TLS_AUTHC ) &&
63                 ( conn->c_dn != NULL ) )
64         {
65                 *text = "cannot start TLS after authentication";
66                 rc = LDAP_OPERATIONS_ERROR;
67                 goto done;
68         }
69
70         if ( ( global_allows & SLAP_ALLOW_TLS_2_ANON ) &&
71                 ( conn->c_dn != NULL ) )
72         {
73                 /* force to anonymous */
74                 connection2anonymous( conn );
75         }
76
77         /* fail if TLS could not be initialized */
78         if (ldap_pvt_tls_get_option(NULL, LDAP_OPT_X_TLS_CERT, &ctx) != 0
79                 || ctx == NULL)
80         {
81                 if (default_referral != NULL) {
82                         /* caller will put the referral in the result */
83                         rc = LDAP_REFERRAL;
84                         goto done;
85                 }
86
87                 *text = "Could not initialize TLS";
88                 rc = LDAP_UNAVAILABLE;
89                 goto done;
90         }
91
92     conn->c_is_tls = 1;
93     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( &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 */