]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
reorder tests
[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         /* fail if TLS could not be initialized */
63         if (ldap_pvt_tls_get_option(NULL, LDAP_OPT_X_TLS_CERT, &ctx) != 0
64                 || ctx == NULL)
65         {
66                 if (default_referral != NULL) {
67                         /* caller will put the referral in the result */
68                         rc = LDAP_REFERRAL;
69                         goto done;
70                 }
71
72                 *text = "Could not initialize TLS";
73                 rc = LDAP_UNAVAILABLE;
74                 goto done;
75         }
76
77     conn->c_is_tls = 1;
78     conn->c_needs_tls_accept = 1;
79
80     rc = LDAP_SUCCESS;
81
82 done:
83         /* give up connection lock */
84         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
85
86         /*
87          * RACE CONDITION: we give up lock before sending result
88          * Should be resolved by reworking connection state, not
89          * by moving send here (so as to ensure proper TLS sequencing)
90          */
91
92         return rc;
93 }
94
95 #endif  /* HAVE_TLS */