]>
Commit | Line | Data |
---|---|---|
21f715a9 AC |
1 | /* |
2 | * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>. | |
3 | * | |
4 | * Permission to use, copy, modify, and/or distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice is present in all copies. | |
7 | * | |
8 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
9 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
10 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
11 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
12 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
13 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
14 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
15 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
16 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
17 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
18 | * POSSIBILITY OF SUCH DAMAGE. | |
19 | */ | |
20 | ||
21 | #include "stdinc.h" | |
22 | #include "client.h" | |
23 | #include "common.h" | |
24 | #include "match.h" | |
25 | #include "hash.h" | |
26 | #include "ircd.h" | |
27 | #include "numeric.h" | |
28 | #include "send.h" | |
29 | #include "msg.h" | |
30 | #include "modules.h" | |
31 | #include "sslproc.h" | |
77d3d2db | 32 | #include "s_assert.h" |
ed385364 | 33 | #include "s_serv.h" |
77d3d2db | 34 | #include "logger.h" |
21f715a9 | 35 | |
428ca87b | 36 | static int mr_starttls(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
21f715a9 AC |
37 | |
38 | struct Message starttls_msgtab = { | |
7baa37a9 | 39 | "STARTTLS", 0, 0, 0, 0, |
21f715a9 AC |
40 | {{mr_starttls, 0}, mg_ignore, mg_ignore, mg_ignore, mg_ignore, mg_ignore} |
41 | }; | |
42 | ||
43 | mapi_clist_av1 starttls_clist[] = { &starttls_msgtab, NULL }; | |
44 | ||
0416a2cc AC |
45 | unsigned int CLICAP_TLS = 0; |
46 | ||
47 | static int | |
48 | _modinit(void) | |
49 | { | |
50 | #ifdef HAVE_LIBCRYPTO | |
51 | CLICAP_TLS = capability_put(cli_capindex, "tls", NULL); | |
52 | #endif | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static void | |
57 | _moddeinit(void) | |
58 | { | |
59 | #ifdef HAVE_LIBCRYPTO | |
60 | capability_orphan(cli_capindex, "tls"); | |
61 | #endif | |
62 | } | |
63 | ||
64 | DECLARE_MODULE_AV1(starttls, _modinit, _moddeinit, starttls_clist, NULL, NULL, "$Revision$"); | |
21f715a9 AC |
65 | |
66 | static int | |
428ca87b | 67 | mr_starttls(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
21f715a9 AC |
68 | { |
69 | #ifdef HAVE_LIBCRYPTO | |
70 | ssl_ctl_t *ctl; | |
71 | rb_fde_t *F[2]; | |
72 | ||
73 | if (!MyConnect(client_p)) | |
74 | return 0; | |
75 | ||
2b177879 MM |
76 | if (IsSSL(client_p)) |
77 | { | |
78 | sendto_one_numeric(client_p, ERR_STARTTLS, form_str(ERR_STARTTLS), "Nested TLS handshake not allowed"); | |
79 | return 1; | |
80 | } | |
81 | ||
8ff07125 JT |
82 | if (!ssl_ok || !get_ssld_count()) |
83 | { | |
84 | sendto_one_numeric(client_p, ERR_STARTTLS, form_str(ERR_STARTTLS), "TLS is not configured"); | |
85 | return 1; | |
86 | } | |
87 | ||
21f715a9 AC |
88 | if (rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &F[0], &F[1], "STARTTLS ssld session") == -1) |
89 | { | |
90 | ilog_error("error creating SSL/TLS socketpair for ssld slave"); | |
c4e81ae9 | 91 | sendto_one_numeric(client_p, ERR_STARTTLS, form_str(ERR_STARTTLS), "Unable to create SSL/TLS socketpair for ssld offload slave"); |
21f715a9 AC |
92 | return 1; |
93 | } | |
94 | ||
95 | s_assert(client_p->localClient != NULL); | |
96 | ||
97 | /* clear out any remaining plaintext lines */ | |
98 | rb_linebuf_donebuf(&client_p->localClient->buf_recvq); | |
99 | ||
100 | sendto_one_numeric(client_p, RPL_STARTTLS, form_str(RPL_STARTTLS)); | |
101 | send_queued(client_p); | |
102 | ||
103 | ctl = start_ssld_accept(client_p->localClient->F, F[1], rb_get_fd(F[0])); | |
104 | if (ctl != NULL) | |
105 | { | |
106 | client_p->localClient->F = F[0]; | |
107 | client_p->localClient->ssl_ctl = ctl; | |
108 | SetSSL(client_p); | |
109 | } | |
110 | else | |
111 | return 1; | |
112 | ||
b6e02c25 | 113 | #else |
8ff07125 | 114 | sendto_one_numeric(client_p, ERR_STARTTLS, form_str(ERR_STARTTLS), "TLS is not configured"); |
b6e02c25 | 115 | #endif |
21f715a9 AC |
116 | return 0; |
117 | } |