]> git.sur5r.net Git - openldap/blob - servers/slapd/starttls.c
Fix comment
[openldap] / servers / slapd / starttls.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999 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         SLAP_EXTOP_CALLBACK_FN cb,
24         Connection *conn,
25         Operation *op,
26         char * reqoid,
27         struct berval * reqdata,
28         char ** rspoid,
29         struct berval ** rspdata,
30         LDAPControl ***rspctrls,
31         char ** text,
32         struct berval *** refs )
33 {
34         void *ctx;
35         int rc;
36
37         if ( reqdata != NULL ) {
38                 /* no request data should be provided */
39                 *text = "no request data expected";
40                 return LDAP_PROTOCOL_ERROR;
41         }
42
43         /* acquire connection lock */
44         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
45
46         /* can't start TLS if it is already started */
47         if (conn->c_is_tls != 0) {
48                 *text = "TLS already started";
49                 rc = LDAP_OPERATIONS_ERROR;
50                 goto done;
51         }
52
53         /* fail if TLS could not be initialized */
54         if (ldap_pvt_tls_get_option(NULL, LDAP_OPT_X_TLS_CERT, &ctx) != 0
55                 || ctx == NULL)
56         {
57                 if (default_referral != NULL) {
58                         /* caller will put the referral in the result */
59                         rc = LDAP_REFERRAL;
60                         goto done;
61                 }
62
63                 *text = "Could not initialize TLS";
64                 rc = LDAP_UNAVAILABLE;
65                 goto done;
66         }
67
68         /* can't start TLS if there are other op's around */
69         if (( conn->c_ops != NULL &&
70                         (conn->c_ops != op || op->o_next != NULL)) ||
71                 ( conn->c_pending_ops != NULL))
72         {
73                 *text = "cannot start TLS when operations our outstanding";
74                 rc = LDAP_OPERATIONS_ERROR;
75                 goto done;
76         }
77
78     conn->c_is_tls = 1;
79     conn->c_needs_tls_accept = 1;
80
81     rc = LDAP_SUCCESS;
82
83 done:
84         /* give up connection lock */
85         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
86
87         /*
88          * RACE CONDITION: we give up lock before sending result
89          * Should be resolved by reworking connection state, not
90          * by moving send here (so as to ensure proper TLS sequencing)
91          */
92
93         return rc;
94 }
95
96 #endif  /* HAVE_TLS */