1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  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
do_ssl_stuff(TH_ARGS * args)50 do_ssl_stuff(	TH_ARGS*	args)
51 {
52 	const char*	s = "Huhuhuhuuu";
53 	Vio*		server_vio;
54 	int		err;
55         unsigned long   ssl_error;
56 	DBUG_ENTER("do_ssl_stuff");
57 
58 	server_vio = vio_new(args->sd, VIO_TYPE_TCPIP, TRUE);
59 
60 	/* ----------------------------------------------- */
61 	/* TCP connection is ready. Do server side SSL. */
62 
63 	err = write(server_vio->sd,(uchar*)s, strlen(s));
64 	sslaccept(args->ssl_acceptor,server_vio,60L,&ssl_error);
65 	err = server_vio->write(server_vio,(uchar*)s, strlen(s));
66 	DBUG_VOID_RETURN;
67 }
68 
69 static void*
client_thread(void * arg)70 client_thread(	void*	arg)
71 {
72   my_thread_init();
73   do_ssl_stuff((TH_ARGS*)arg);
74   return 0;
75 }
76 
77 int
main(int argc,char ** argv)78 main(int argc __attribute__((unused)), char** argv)
79 {
80 	char	server_key[] = "../SSL/server-key.pem",
81 		server_cert[] = "../SSL/server-cert.pem";
82 	char	ca_file[] = "../SSL/cacert.pem",
83 		*ca_path = 0,
84 		*cipher = 0;
85 	struct	st_VioSSLFd*	ssl_acceptor;
86 	pthread_t	th;
87 	TH_ARGS		th_args;
88 
89 
90 	struct sockaddr_in sa_serv;
91 	struct sockaddr_in sa_cli;
92 	int listen_sd;
93 	int err;
94         size_socket client_len;
95 	int	reuseaddr = 1; /* better testing, uh? */
96 
97 	MY_INIT(argv[0]);
98         DBUG_PROCESS(argv[0]);
99         DBUG_PUSH(default_dbug_option);
100 
101 	printf("Server key/cert : %s/%s\n", server_key, server_cert);
102 	if (ca_file!=0)
103 
104 		printf("CAfile          : %s\n", ca_file);
105 	if (ca_path!=0)
106 		printf("CApath          : %s\n", ca_path);
107 
108         th_args.ssl_acceptor = ssl_acceptor = new_VioSSLAcceptorFd(server_key, server_cert, ca_file, ca_path,cipher);
109 
110 	/* ----------------------------------------------- */
111 	/* Prepare TCP socket for receiving connections */
112 
113 	listen_sd = socket (AF_INET, SOCK_STREAM, 0);
114 	setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(&reuseaddr));
115 
116 	memset (&sa_serv, '\0', sizeof(sa_serv));
117 	sa_serv.sin_family      = AF_INET;
118 	sa_serv.sin_addr.s_addr = INADDR_ANY;
119 	sa_serv.sin_port        = htons (1111);          /* Server Port number */
120 
121 	err = bind(listen_sd, (struct sockaddr*) &sa_serv,
122 	     sizeof (sa_serv));
123 
124 	/* Receive a TCP connection. */
125 
126 	err = listen (listen_sd, 5);
127 	client_len = sizeof(sa_cli);
128 	th_args.sd = accept (listen_sd, (struct sockaddr*) &sa_cli, &client_len);
129 	close (listen_sd);
130 
131 	printf ("Connection from %lx, port %x\n",
132 		  (long)sa_cli.sin_addr.s_addr, sa_cli.sin_port);
133 
134 	/* ----------------------------------------------- */
135 	/* TCP connection is ready. Do server side SSL. */
136 
137 	err = pthread_create(&th, NULL, client_thread, (void*)&th_args);
138 	DBUG_PRINT("info", ("pthread_create: %d", err));
139 	pthread_join(th, NULL);
140 
141 #if 0
142 	if (err<=0) {
143 		my_free(ssl_acceptor);
144 		fatal_error("server:SSL_write");
145 	}
146 #endif /* 0 */
147 
148 	my_free(ssl_acceptor);
149 	return 0;
150 }
151 #else /* HAVE_OPENSSL */
152 
main()153 int main() {
154 return 0;
155 }
156 #endif /* HAVE_OPENSSL */
157