Debian Codegraph @

membership/quorumd/quorumd.c

raw
1 /*
2 * Linux HA quorum daemon
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 <lha_internal.h>
23
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 #include <sys/socket.h>
27 #endif
28 #ifdef HAVE_SYS_SOCKIO_H
29 #include <sys/sockio.h>
30 #endif
31 #include <pwd.h>
32 #include <grp.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41
42 #include <glib.h>
43 #include <gnutls/gnutls.h>
44 #include <gnutls/x509.h>
45 #include <heartbeat.h>
46 #include <clplumbing/GSource.h>
47 #include <clplumbing/cl_log.h>
48 #include <clplumbing/cl_syslog.h>
49 #include <clplumbing/cl_signal.h>
50 #include <clplumbing/lsb_exitcodes.h>
51 #include <clplumbing/coredumps.h>
52 #include <clplumbing/cl_pidfile.h>
53 #include <ha_msg.h>
54
55 #include <clplumbing/cl_plugin.h>
56 #include <clplumbing/cl_quorumd.h>
57
58 /* x.509 related */
59 #define SERVERKEY HA_HBCONF_DIR"/server-key.pem"
60 #define SERVERCERT HA_HBCONF_DIR"/server-cert.pem"
61 #define CACERT HA_HBCONF_DIR"/ca-cert.pem"
62 #define CACRL HA_HBCONF_DIR"/ca-crl.pem"
63 #define DH_BITS 1024
64
65
66 static int verify_certificate (gnutls_session session, char* CN);
67 static gnutls_session initialize_tls_session (int sd, char* CN);
68 static void initialize_tls_global(void);
69
70 static gnutls_dh_params dh_params;
71 static gnutls_certificate_credentials x509_cred;
72
73 /* Message types */
74
75 #define ENV_PREFIX "HA_"
76 #define KEY_LOGDAEMON "use_logd"
77 #define HADEBUGVAL "HA_debug"
78 #define OPTARGS "skrhvt"
79 #define PID_FILE HA_VARRUNDIR"/quorumd.pid"
80 #define QUORUMD "quorumd"
81
82 #define PORT 5561
83
84 static gboolean sig_handler(int nsig, gpointer user_data);
85
86 static void usage(const char* cmd, int exit_status);
87 static int init_start(void);
88 static int init_stop(const char *pid_file);
89 static int init_status(const char *pid_file, const char *client_name);
90 static void shutdown_quorumd(void);
91 static gboolean sigterm_action(int nsig, gpointer unused);
92
93 static gboolean on_listen(GIOChannel *ch
94 , GIOCondition condition
95 , gpointer data);
96 static struct hb_quorumd_fns* get_protocol(const char* version);
97 static void _load_config_file(gpointer key, gpointer value, gpointer user_data);
98 static void _dump_data(gpointer key, gpointer value, gpointer user_data);
99
100
101 extern int debug_level;
102 static GMainLoop* mainloop = NULL;
103 static GHashTable* protocols = NULL;
104
105 int
106 main(int argc, char ** argv)
107 {
108 int req_restart = FALSE;
109 int req_status = FALSE;
110 int req_stop = FALSE;
111
112 int argerr = 0;
113 int flag;
114 char * inherit_debuglevel;
115
116 while ((flag = getopt(argc, argv, OPTARGS)) != EOF) {
117 switch(flag) {
118 case 'h': /* Help message */
119 usage(QUORUMD, LSB_EXIT_OK);
120 break;
121 case 'v': /* Debug mode, more logs*/
122 ++debug_level;
123 break;
124 case 's': /* Status */
125 req_status = TRUE;
126 break;
127 case 'k': /* Stop (kill) */
128 req_stop = TRUE;
129 break;
130 case 'r': /* Restart */
131 req_restart = TRUE;
132 break;
133 default:
134 ++argerr;
135 break;
136 }
137 }
138
139 if (optind > argc) {
140 quorum_log(LOG_ERR,"WHY WE ARE HERE?");
141 ++argerr;
142 }
143
144 if (argerr) {
145 usage(QUORUMD, LSB_EXIT_GENERIC);
146 }
147
148 inherit_debuglevel = getenv(HADEBUGVAL);
149 if (inherit_debuglevel != NULL) {
150 debug_level = atoi(inherit_debuglevel);
151 if (debug_level > 2) {
152 debug_level = 2;
153 }
154 }
155
156 cl_log_set_entity(QUORUMD);
157 cl_log_enable_stderr(FALSE);
158 cl_log_set_facility(HA_LOG_FACILITY);
159
160 cl_inherit_logging_environment(0);
161
162 if (req_status){
163 return init_status(PID_FILE, QUORUMD);
164 }
165
166 if (req_stop){
167 return init_stop(PID_FILE);
168 }
169
170 if (req_restart) {
171 init_stop(PID_FILE);
172 }
173
174 return init_start();
175 }
176
177 int
178 init_status(const char *pid_file, const char *client_name)
179 {
180 long pid = cl_read_pidfile(pid_file);
181
182 if (pid > 0) {
183 fprintf(stderr, "%s is running [pid: %ld]\n"
184 , client_name, pid);
185 return LSB_STATUS_OK;
186 }
187 fprintf(stderr, "%s is stopped.\n", client_name);
188 return LSB_STATUS_STOPPED;
189 }
190
191 int
192 init_stop(const char *pid_file)
193 {
194 long pid;
195 int rc = LSB_EXIT_OK;
196
197 if (pid_file == NULL) {
198 quorum_log(LOG_ERR, "No pid file specified to kill process");
199 return LSB_EXIT_GENERIC;
200 }
201 pid = cl_read_pidfile(pid_file);
202
203 if (pid > 0) {
204 if (CL_KILL((pid_t)pid, SIGTERM) < 0) {
205 rc = (errno == EPERM
206 ? LSB_EXIT_EPERM : LSB_EXIT_GENERIC);
207 fprintf(stderr, "Cannot kill pid %ld\n", pid);
208 }else{
209 quorum_log(LOG_INFO,
210 "Signal sent to pid=%ld,"
211 " waiting for process to exit",
212 pid);
213
214 while (CL_PID_EXISTS(pid)) {
215 sleep(1);
216 }
217 }
218 }
219 return rc;
220 }
221
222 static const char usagemsg[] = "[-srkhv]\n\ts: status\n\tr: restart"
223 "\n\tk: kill\n\th: help\n\tv: debug\n";
224
225 void
226 usage(const char* cmd, int exit_status)
227 {
228 FILE* stream;
229
230 stream = exit_status ? stderr : stdout;
231
232 fprintf(stream, "usage: %s %s", cmd, usagemsg);
233 fflush(stream);
234
235 exit(exit_status);
236 }
237
238 gboolean
239 sigterm_action(int nsig, gpointer user_data)
240 {
241 shutdown_quorumd();
242 return TRUE;
243 }
244 void
245 shutdown_quorumd(void)
246 {
247 quorum_log(LOG_INFO,"quorumd is shutting down");
248 if (mainloop != NULL && g_main_is_running(mainloop)) {
249 g_main_quit(mainloop);
250 }else {
251 exit(LSB_EXIT_OK);
252 }
253 }
254
255 static void
256 register_pid(gboolean do_fork,
257 gboolean (*shutdown)(int nsig, gpointer userdata))
258 {
259 int j;
260 umask(022);
261
262 for (j = 0; j < 3; ++j) {
263 close(j);
264 (void)open("/dev/null", j == 0 ? O_RDONLY : O_RDONLY);
265 }
266 CL_IGNORE_SIG(SIGINT);
267 CL_IGNORE_SIG(SIGHUP);
268 G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGTERM
269 , shutdown, NULL, NULL);
270 cl_signal_set_interrupt(SIGTERM, 1);
271 cl_signal_set_interrupt(SIGCHLD, 1);
272 /* At least they are harmless, I think. ;-) */
273 cl_signal_set_interrupt(SIGINT, 0);
274 cl_signal_set_interrupt(SIGHUP, 0);
275 }
276
277 gboolean
278 sig_handler(int nsig, gpointer user_data)
279 {
280 switch (nsig) {
281 case SIGUSR1:
282 debug_level++;
283 if (debug_level > 2) {
284 debug_level = 0;
285 }
286 quorum_log(LOG_INFO, "set debug_level to %d", debug_level);
287 break;
288
289 case SIGUSR2:
290 g_hash_table_foreach(protocols, _dump_data, GINT_TO_POINTER(LOG_INFO));
291 break;
292
293 case SIGHUP:
294 g_hash_table_foreach(protocols, _load_config_file, NULL);
295 break;
296
297 default:
298 quorum_log(LOG_WARNING, "sig_handler: Received an "
299 "unexpected signal(%d). Something wrong?.",nsig);
300 }
301
302 return TRUE;
303 }
304
305
306 /* main loop of the daemon*/
307 int
308 init_start ()
309 {
310 int ssock;
311 struct sockaddr_in saddr;
312 GIOChannel* sch;
313
314 /* register pid */
315 if (cl_lock_pidfile(PID_FILE) < 0) {
316 quorum_log(LOG_ERR, "already running: [pid %d]."
317 , cl_read_pidfile(PID_FILE));
318 quorum_log(LOG_ERR, "Startup aborted (already running)."
319 "Shutting down.");
320 exit(100);
321 }
322 register_pid(FALSE, sigterm_action);
323
324 /* enable coredumps */
325 quorum_log(LOG_DEBUG, "Enabling coredumps");
326 cl_cdtocoredir();
327 cl_enable_coredumps(TRUE);
328 cl_set_all_coredump_signal_handlers();
329
330 /* initialize gnutls */
331 initialize_tls_global();
332
333 /* enable dynamic up/down debug level */
334 G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR1,
335 sig_handler, NULL, NULL);
336 G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR2,
337 sig_handler, NULL, NULL);
338 G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGHUP,
339 sig_handler, NULL, NULL);
340
341 /* create the mainloop */
342 mainloop = g_main_new(FALSE);
343
344 /* create the protocal table */
345 protocols = g_hash_table_new(g_str_hash, g_str_equal);
346
347 /* create server socket */
348 ssock = socket(AF_INET, SOCK_STREAM, 0);
349 if (ssock == -1) {
350 quorum_log(LOG_ERR, "Can not create server socket."
351 "Shutting down.");
352 exit(100);
353 }
354 /* bind server socket*/
355 memset(&saddr, '\0', sizeof(saddr));
356 saddr.sin_family = AF_INET;
357 saddr.sin_addr.s_addr = INADDR_ANY;
358 saddr.sin_port = htons(PORT);
359 if (bind(ssock, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
360 quorum_log(LOG_ERR, "Can not bind server socket."
361 "Shutting down.");
362 exit(100);
363 }
364 if (listen(ssock, 10) == -1) {
365 quorum_log(LOG_ERR, "Can not start listen."
366 "Shutting down.");
367 exit(100);
368 }
369
370 /* create source for server socket and add to the mainloop */
371 sch = g_io_channel_unix_new(ssock);
372 g_io_add_watch(sch, G_IO_IN|G_IO_ERR|G_IO_HUP, on_listen, NULL);
373
374 /* run the mainloop */
375 quorum_log(LOG_DEBUG, "main: run the loop...");
376 quorum_log(LOG_INFO, "Started.");
377
378 g_main_run(mainloop);
379
380 /* exit, clean the pid file */
381 if (cl_unlock_pidfile(PID_FILE) == 0) {
382 quorum_log(LOG_DEBUG, "[%s] stopped", QUORUMD);
383 }
384
385 return 0;
386 }
387
388 gboolean
389 on_listen(GIOChannel *ch, GIOCondition condition, gpointer data)
390 {
391 int ssock, csock;
392 unsigned laddr;
393 struct sockaddr_in addr;
394 char buf[MAXMSG];
395 char CN[MAX_DN_LEN];
396 ssize_t len;
397 gnutls_session session;
398 struct hb_quorumd_fns *fns;
399
400 if (condition & G_IO_IN) {
401 /* accept the connection */
402 ssock = g_io_channel_unix_get_fd(ch);
403 laddr = sizeof(addr);
404 csock = accept(ssock, (struct sockaddr*)&addr, &laddr);
405 if (csock == -1) {
406 quorum_log(LOG_ERR, "%s accept socket failed", __FUNCTION__);
407 return TRUE;
408 }
409 memset(CN, 0, MAX_DN_LEN);
410 session = initialize_tls_session(csock, CN);
411 if (session == NULL) {
412 quorum_log(LOG_ERR, "%s tls handshake failed", __FUNCTION__);
413 close(csock);
414 return TRUE;
415 }
416 memset(buf,0,MAXMSG);
417 len = gnutls_record_recv(session, buf, MAXMSG);
418 if (len <= 0) {
419 quorum_log(LOG_ERR, "can't get version info");
420 gnutls_bye (session, GNUTLS_SHUT_WR);
421 gnutls_deinit (session);
422 close(csock);
423 return TRUE;
424 }
425 quorum_debug(LOG_DEBUG, "version:%s(%d)",buf,(int)len);
426 fns = get_protocol(buf);
427 if(fns != NULL) {
428 fns->on_connect(csock,session,CN);
429 }
430 else {
431 quorum_log(LOG_WARNING, "version %s is not supported", buf);
432 gnutls_bye (session, GNUTLS_SHUT_WR);
433 gnutls_deinit (session);
434 close(csock);
435 }
436 }
437 return TRUE;
438 }
439
440 struct hb_quorumd_fns*
441 get_protocol(const char* version)
442 {
443 struct hb_quorumd_fns* protocol;
444 protocol = g_hash_table_lookup(protocols, version);
445
446 if (protocol == NULL) {
447 protocol = cl_load_plugin("quorumd", version);
448 if (protocol != NULL) {
449 if (protocol->init() != -1) {
450 g_hash_table_insert(protocols, strdup(version), protocol);
451 }
452 else {
453 protocol = NULL;
454 }
455 }
456
457 }
458 return protocol;
459 }
460 void
461 _load_config_file(gpointer key, gpointer value, gpointer user_data)
462 {
463 struct hb_quorumd_fns* protocol = (struct hb_quorumd_fns*) value;
464 protocol->load_config_file();
465 }
466 void
467 _dump_data(gpointer key, gpointer value, gpointer user_data)
468 {
469 struct hb_quorumd_fns* protocol = (struct hb_quorumd_fns*) value;
470 protocol->dump_data(GPOINTER_TO_INT(user_data));
471 }
472 int
473 verify_certificate (gnutls_session session, char* CN)
474 {
475 unsigned int cert_list_size;
476 const gnutls_datum *cert_list;
477 int ret;
478 char dn[MAX_DN_LEN];
479 size_t dn_len = MAX_DN_LEN;
480 gnutls_x509_crt cert;
481
482 ret = gnutls_certificate_verify_peers(session);
483
484 if (ret < 0)
485 {
486 quorum_debug(LOG_DEBUG,"gnutls_certificate_verify_peers2 returns error");
487 return -1;
488 }
489 if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509) {
490 quorum_debug(LOG_DEBUG,"The certificate is not a x.509 cert");
491 return -1;
492 }
493 if (gnutls_x509_crt_init (&cert) < 0)
494 {
495 quorum_debug(LOG_DEBUG,"error in gnutls_x509_crt_init");
496 return -1;
497 }
498
499 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
500 if (cert_list == NULL)
501 {
502 quorum_debug(LOG_DEBUG,"No certificate was found!");
503 return -1;
504 }
505
506 if (gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
507 {
508 quorum_debug(LOG_DEBUG,"error parsing certificate");
509 return -1;
510 }
511
512 if (gnutls_x509_crt_get_expiration_time (cert) < time (0))
513 {
514 quorum_debug(LOG_DEBUG,"The certificate has expired");
515 return -1;
516 }
517
518 if (gnutls_x509_crt_get_activation_time (cert) > time (0))
519 {
520 quorum_debug(LOG_DEBUG,"The certificate is not yet activated");
521 return -1;
522 }
523 memset(dn, 0, MAX_DN_LEN);
524 gnutls_x509_crt_get_dn(cert, dn, &dn_len);
525 strncpy(CN, strstr(dn, "CN=")+3, MAX_DN_LEN);
526 CN[MAX_DN_LEN-1]= '\0';
527 quorum_debug(LOG_DEBUG,"The certificate cn:%s",CN);
528 gnutls_x509_crt_deinit (cert);
529
530 return 0;
531 }
532
533 gnutls_session
534 initialize_tls_session (int sd, char* CN)
535 {
536 int ret;
537 gnutls_session session;
538
539 gnutls_init (&session, GNUTLS_SERVER);
540 gnutls_set_default_priority (session);
541 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
542 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE);
543 gnutls_dh_set_prime_bits (session, DH_BITS);
544 gnutls_transport_set_ptr (session, (gnutls_transport_ptr) GINT_TO_POINTER(sd));
545 ret = gnutls_handshake (session);
546 if (ret < 0)
547 {
548 close (sd);
549 gnutls_deinit (session);
550 quorum_log(LOG_WARNING,"handshake failed");
551 return NULL;
552 }
553 if (verify_certificate(session,CN) < 0) {
554 return NULL;
555 }
556 return session;
557 }
558 void initialize_tls_global(void)
559 {
560 gnutls_global_init ();
561
562 gnutls_certificate_allocate_credentials (&x509_cred);
563 gnutls_certificate_set_x509_trust_file (x509_cred, CACERT,
564 GNUTLS_X509_FMT_PEM);
565 gnutls_certificate_set_x509_crl_file (x509_cred, CACRL,
566 GNUTLS_X509_FMT_PEM);
567 gnutls_certificate_set_x509_key_file (x509_cred, SERVERCERT, SERVERKEY,
568 GNUTLS_X509_FMT_PEM);
569 gnutls_dh_params_init (&dh_params);
570 gnutls_dh_params_generate2 (dh_params, DH_BITS);
571
572 gnutls_certificate_set_dh_params (x509_cred, dh_params);
573 }