1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
#include <my_global.h> |
|
17 |
#ifdef HAVE_OPENSSL
|
|
18 |
#include <my_sys.h> |
|
19 |
#include <m_string.h> |
|
20 |
#include <m_ctype.h> |
|
21 |
#include "mysql.h" |
|
22 |
#include "errmsg.h" |
|
23 |
#include <my_dir.h> |
|
24 |
#include <my_getopt.h> |
|
25 |
#include <signal.h> |
|
26 |
#include <violite.h> |
|
27 |
||
28 |
const char *VER="0.2"; |
|
29 |
||
30 |
||
31 |
#ifndef DBUG_OFF
|
|
32 |
const char *default_dbug_option="d:t:O,-"; |
|
33 |
#endif
|
|
34 |
||
35 |
#if 0
|
|
36 |
static void
|
|
37 |
fatal_error( const char* r)
|
|
38 |
{
|
|
39 |
perror(r);
|
|
40 |
exit(0);
|
|
41 |
}
|
|
42 |
#endif
|
|
43 |
||
44 |
typedef struct { |
|
45 |
int sd; |
|
46 |
struct st_VioSSLFd* ssl_acceptor; |
|
47 |
} TH_ARGS; |
|
48 |
||
49 |
static void |
|
50 |
do_ssl_stuff( TH_ARGS* args) |
|
51 |
{
|
|
52 |
const char* s = "Huhuhuhuuu"; |
|
53 |
Vio* server_vio; |
|
54 |
int err; |
|
55 |
DBUG_ENTER("do_ssl_stuff"); |
|
56 |
||
57 |
server_vio = vio_new(args->sd, VIO_TYPE_TCPIP, TRUE); |
|
58 |
||
59 |
/* ----------------------------------------------- */
|
|
60 |
/* TCP connection is ready. Do server side SSL. */
|
|
61 |
||
62 |
err = write(server_vio->sd,(uchar*)s, strlen(s)); |
|
63 |
sslaccept(args->ssl_acceptor,server_vio,60L); |
|
64 |
err = server_vio->write(server_vio,(uchar*)s, strlen(s)); |
|
65 |
DBUG_VOID_RETURN; |
|
66 |
}
|
|
67 |
||
68 |
static void* |
|
69 |
client_thread( void* arg) |
|
70 |
{
|
|
71 |
my_thread_init(); |
|
72 |
do_ssl_stuff((TH_ARGS*)arg); |
|
73 |
return 0; |
|
74 |
}
|
|
75 |
||
76 |
int
|
|
77 |
main(int argc __attribute__((unused)), char** argv) |
|
78 |
{
|
|
79 |
char server_key[] = "../SSL/server-key.pem", |
|
80 |
server_cert[] = "../SSL/server-cert.pem"; |
|
81 |
char ca_file[] = "../SSL/cacert.pem", |
|
82 |
*ca_path = 0, |
|
83 |
*cipher = 0; |
|
84 |
struct st_VioSSLFd* ssl_acceptor; |
|
85 |
pthread_t th; |
|
86 |
TH_ARGS th_args; |
|
87 |
||
88 |
||
89 |
struct sockaddr_in sa_serv; |
|
90 |
struct sockaddr_in sa_cli; |
|
91 |
int listen_sd; |
|
92 |
int err; |
|
93 |
size_socket client_len; |
|
94 |
int reuseaddr = 1; /* better testing, uh? */ |
|
95 |
||
96 |
MY_INIT(argv[0]); |
|
97 |
DBUG_PROCESS(argv[0]); |
|
98 |
DBUG_PUSH(default_dbug_option); |
|
99 |
||
100 |
printf("Server key/cert : %s/%s\n", server_key, server_cert); |
|
101 |
if (ca_file!=0) |
|
102 |
||
103 |
printf("CAfile : %s\n", ca_file); |
|
104 |
if (ca_path!=0) |
|
105 |
printf("CApath : %s\n", ca_path); |
|
106 |
||
107 |
th_args.ssl_acceptor = ssl_acceptor = new_VioSSLAcceptorFd(server_key, server_cert, ca_file, ca_path,cipher); |
|
108 |
||
109 |
/* ----------------------------------------------- */
|
|
110 |
/* Prepare TCP socket for receiving connections */
|
|
111 |
||
112 |
listen_sd = socket (AF_INET, SOCK_STREAM, 0); |
|
113 |
setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(&reuseaddr)); |
|
114 |
||
115 |
memset (&sa_serv, '\0', sizeof(sa_serv)); |
|
116 |
sa_serv.sin_family = AF_INET; |
|
117 |
sa_serv.sin_addr.s_addr = INADDR_ANY; |
|
118 |
sa_serv.sin_port = htons (1111); /* Server Port number */ |
|
119 |
||
120 |
err = bind(listen_sd, (struct sockaddr*) &sa_serv, |
|
121 |
sizeof (sa_serv)); |
|
122 |
||
123 |
/* Receive a TCP connection. */
|
|
124 |
||
125 |
err = listen (listen_sd, 5); |
|
126 |
client_len = sizeof(sa_cli); |
|
127 |
th_args.sd = accept (listen_sd, (struct sockaddr*) &sa_cli, &client_len); |
|
128 |
close (listen_sd); |
|
129 |
||
130 |
printf ("Connection from %lx, port %x\n", |
|
131 |
(long)sa_cli.sin_addr.s_addr, sa_cli.sin_port); |
|
132 |
||
133 |
/* ----------------------------------------------- */
|
|
134 |
/* TCP connection is ready. Do server side SSL. */
|
|
135 |
||
136 |
err = pthread_create(&th, NULL, client_thread, (void*)&th_args); |
|
137 |
DBUG_PRINT("info", ("pthread_create: %d", err)); |
|
138 |
pthread_join(th, NULL); |
|
139 |
||
140 |
#if 0
|
|
141 |
if (err<=0) {
|
|
142 |
my_free((uchar*)ssl_acceptor,MYF(0));
|
|
143 |
fatal_error("server:SSL_write");
|
|
144 |
}
|
|
145 |
#endif /* 0 */
|
|
146 |
||
147 |
my_free((uchar*)ssl_acceptor,MYF(0)); |
|
148 |
return 0; |
|
149 |
}
|
|
150 |
#else /* HAVE_OPENSSL */ |
|
151 |
||
152 |
int main() { |
|
153 |
return 0; |
|
154 |
}
|
|
155 |
#endif /* HAVE_OPENSSL */ |