Debian Codegraph @

membership/quorumd/quorumdtest.c

raw
1 /*
2 * Test client for Linux HA quormd daemon test client
3 *
4 * Author: Huang Zhen <[email protected]>
5 * Copyright (c) 2006 International Business Machines
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <gnutls/gnutls.h>
32 #include <gnutls/x509.h>
33
34 #include <ha_msg.h>
35
36
37 #define DH_BITS 1024
38 #define MAX_BUF 1024
39 #define CACERT "/etc/ha.d/ca-cert.pem"
40 #define CLIENTKEY "/etc/ha.d/client-key.pem"
41 #define CLIENTCERT "/etc/ha.d/client-cert.pem"
42
43 static int verify_certificate (gnutls_session session);
44 static gnutls_session initialize_tls_session (int sd);
45 static void initialize_tls_global(void);
46
47 static gnutls_certificate_credentials xcred;
48
49 int sock = 0;
50
51 int main (int argc, char* argv[])
52 {
53 struct sockaddr_in addr;
54 struct ha_msg* msg = NULL;
55 struct ha_msg* ret = NULL;
56 const char* version = "2_0_8";
57 struct hostent* hp;
58 int i;
59 int quorum;
60 size_t len;
61 char* s = NULL;
62 char buf[MAXMSG];
63 gnutls_session session;
64 int t_interval;
65 /* initialize gnutls */
66 initialize_tls_global();
67
68 /* create socket */
69 sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
70 if (sock == -1 ) {
71 return -1;
72 }
73
74 /* connect to server*/
75 memset(&addr, 0, sizeof(addr));
76 addr.sin_family = AF_INET;
77 hp = gethostbyname("pluto");
78 memcpy(&addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
79 addr.sin_port = htons(5561);
80 if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
81 close(sock);
82 return -1;
83 }
84 session = initialize_tls_session(sock);
85 if (session == NULL) {
86 return -1;
87 }
88 gnutls_record_send(session, version, strlen(version)+1);
89
90 msg = ha_msg_new(10);
91 ha_msg_add(msg, "t","init");
92 ha_msg_add(msg, "cl_name","mycluster");
93
94 s = msg2wirefmt(msg, &len);
95 gnutls_record_send(session, s, len);
96 len = gnutls_record_recv(session, buf, MAXMSG);
97 ret = wirefmt2msg(buf, len, FALSE);
98
99 printf("result:%s\n",ha_msg_value(ret, "result"));
100 ha_msg_value_int(ret, "interval", &t_interval);
101 for (i = 0; i < 20; i++ )
102 {
103 msg = ha_msg_new(10);
104 ha_msg_add(msg, "t","quorum");
105 ha_msg_add_int(msg, "nodenum", 2);
106 ha_msg_add_int(msg, "weight", 200);
107
108 s = msg2wirefmt(msg, &len);
109 gnutls_record_send(session, s, len);
110 len = gnutls_record_recv(session, buf, MAXMSG);
111 ret = wirefmt2msg(buf, len, FALSE);
112 printf("result:%s\n",ha_msg_value(ret, "result"));
113 ha_msg_value_int(ret, "quorum", &quorum);
114 printf("quorum:%d\n",quorum);
115
116 ha_msg_del(ret);
117 ha_msg_del(msg);
118 sleep(t_interval/1000);
119 }
120 gnutls_bye (session, GNUTLS_SHUT_WR);
121 gnutls_deinit (session);
122 close(sock);
123 return 0;
124 }
125 void
126 initialize_tls_global(void)
127 {
128 gnutls_global_init ();
129 gnutls_certificate_allocate_credentials (&xcred);
130 gnutls_certificate_set_x509_trust_file (xcred, CACERT, GNUTLS_X509_FMT_PEM);
131 gnutls_certificate_set_x509_key_file (xcred, CLIENTCERT, CLIENTKEY, GNUTLS_X509_FMT_PEM);
132 }
133
134 gnutls_session
135 initialize_tls_session (int sd)
136 {
137 int ret;
138 gnutls_session session;
139 const int cert_type_priority[2] = { GNUTLS_CRT_X509,0};
140
141 gnutls_init (&session, GNUTLS_CLIENT);
142 gnutls_set_default_priority (session);
143 gnutls_certificate_type_set_priority (session, cert_type_priority);
144 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
145 gnutls_transport_set_ptr (session, (gnutls_transport_ptr) GINT_TO_POINTER(sd));
146 ret = gnutls_handshake (session);
147 if (ret < 0)
148 {
149 close (sd);
150 gnutls_deinit (session);
151 fprintf (stderr, "*** Handshake failed\n");
152 gnutls_perror (ret);
153 return NULL;
154 }
155 verify_certificate(session);
156 return session;
157 }
158
159 int
160 verify_certificate (gnutls_session session)
161 {
162 unsigned int cert_list_size;
163 const gnutls_datum *cert_list;
164 int ret;
165 gnutls_x509_crt cert;
166
167 ret = gnutls_certificate_verify_peers (session);
168
169 if (ret < 0)
170 {
171 printf("gnutls_certificate_verify_peers2 returns error.\n");
172 return -1;
173 }
174 /*
175 if (status & GNUTLS_CERT_INVALID) {
176 printf("The certificate is not trusted.\n");
177 return -1;
178 }
179 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
180 printf("The certificate hasn't got a known issuer.\n");
181 return -1;
182 }
183 if (status & GNUTLS_CERT_REVOKED) {
184 printf("The certificate has been revoked.\n");
185 return -1;
186 }
187 */
188 if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509) {
189 printf("The certificate is not a x.509 cert\n");
190 return -1;
191 }
192 if (gnutls_x509_crt_init (&cert) < 0)
193 {
194 printf("error in gnutls_x509_crt_init\n");
195 return -1;
196 }
197
198 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
199 if (cert_list == NULL)
200 {
201 printf("No certificate was found!\n");
202 return -1;
203 }
204
205 if (gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
206 {
207 printf("error parsing certificate\n");
208 return -1;
209 }
210
211 if (gnutls_x509_crt_get_expiration_time (cert) < time (0))
212 {
213 printf("The certificate has expired\n");
214 return -1;
215 }
216
217 if (gnutls_x509_crt_get_activation_time (cert) > time (0))
218 {
219 printf("The certificate is not yet activated\n");
220 return -1;
221 }
222 gnutls_x509_crt_deinit (cert);
223
224 return 0;
225 }