From 94d10d2ce34d04404ec652a27dc8a10e3f994304 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sat, 23 Jun 2007 09:58:34 +0000 Subject: [PATCH] kes Remove some duplicated code in tls.c kes Move more of the bsock member variables into private and access them with methods. kes Add Frank Sweetser's tls-test to the regression suite. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@5066 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/dird/getmsg.c | 2 +- bacula/src/dird/msgchan.c | 4 +- bacula/src/filed/heartbeat.c | 8 +-- bacula/src/filed/job.c | 4 +- bacula/src/jcr.h | 4 +- bacula/src/lib/bnet.c | 4 +- bacula/src/lib/bsock.c | 6 +- bacula/src/lib/bsock.h | 15 +++-- bacula/src/lib/btimers.c | 2 +- bacula/src/lib/jcr.c | 6 +- bacula/src/lib/tls.c | 113 +++++++++++++++++------------------ bacula/src/stored/append.c | 6 +- bacula/src/stored/mac.c | 6 +- bacula/src/stored/spool.c | 2 +- bacula/src/stored/stored.c | 2 +- bacula/src/version.h | 4 +- bacula/technotes-2.1 | 5 ++ 17 files changed, 100 insertions(+), 93 deletions(-) diff --git a/bacula/src/dird/getmsg.c b/bacula/src/dird/getmsg.c index 6ce63bf5e8..7a183ba43b 100644 --- a/bacula/src/dird/getmsg.c +++ b/bacula/src/dird/getmsg.c @@ -121,7 +121,7 @@ int bget_dirmsg(BSOCK *bs) bs->fsend(OK_msg);/* send response */ return n; /* end of data */ case BNET_TERMINATE: - bs->m_terminated = 1; + bs->set_terminated(); return n; case BNET_POLL: bs->fsend(OK_msg); /* send response */ diff --git a/bacula/src/dird/msgchan.c b/bacula/src/dird/msgchan.c index ea1bfa390b..bdf82f7bd3 100644 --- a/bacula/src/dird/msgchan.c +++ b/bacula/src/dird/msgchan.c @@ -409,8 +409,8 @@ void wait_for_storage_daemon_termination(JCR *jcr) V(mutex); if (job_canceled(jcr)) { if (jcr->SD_msg_chan) { - jcr->store_bsock->m_timed_out = 1; - jcr->store_bsock->m_terminated = 1; + jcr->store_bsock->set_timed_out(); + jcr->store_bsock->set_terminated(); Dmsg2(400, "kill jobid=%d use=%d\n", (int)jcr->JobId, jcr->use_count()); pthread_kill(jcr->SD_msg_chan, TIMEOUT_SIGNAL); } diff --git a/bacula/src/filed/heartbeat.c b/bacula/src/filed/heartbeat.c index ef2f455a1f..0d45d1dae2 100644 --- a/bacula/src/filed/heartbeat.c +++ b/bacula/src/filed/heartbeat.c @@ -129,12 +129,12 @@ void stop_heartbeat_monitor(JCR *jcr) } if (jcr->hb_bsock) { - jcr->hb_bsock->m_timed_out = true; /* set timed_out to terminate read */ - jcr->hb_bsock->m_terminated = true; /* set to terminate read */ + jcr->hb_bsock->set_timed_out(); /* set timed_out to terminate read */ + jcr->hb_bsock->set_terminated(); /* set to terminate read */ } if (jcr->hb_dir_bsock) { - jcr->hb_dir_bsock->m_timed_out = true; /* set timed_out to terminate read */ - jcr->hb_dir_bsock->m_terminated = true; /* set to terminate read */ + jcr->hb_dir_bsock->set_timed_out(); /* set timed_out to terminate read */ + jcr->hb_dir_bsock->set_terminated(); /* set to terminate read */ } Dmsg0(100, "Send kill to heartbeat id\n"); pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL); /* make heartbeat thread go away */ diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index ad0d4d68bd..fed4e2f5f0 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -373,8 +373,8 @@ static int cancel_cmd(JCR *jcr) bnet_fsend(dir, _("2901 Job %s not found.\n"), Job); } else { if (cjcr->store_bsock) { - cjcr->store_bsock->m_timed_out = 1; - cjcr->store_bsock->m_terminated = 1; + cjcr->store_bsock->set_timed_out(); + cjcr->store_bsock->set_terminated(); pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL); } set_jcr_job_status(cjcr, JS_Canceled); diff --git a/bacula/src/jcr.h b/bacula/src/jcr.h index 81611df8c2..89e2095cdb 100644 --- a/bacula/src/jcr.h +++ b/bacula/src/jcr.h @@ -283,8 +283,8 @@ public: uint32_t StartBlock; uint32_t EndBlock; pthread_t heartbeat_id; /* id of heartbeat thread */ - volatile BSOCK *hb_bsock; /* duped SD socket */ - volatile BSOCK *hb_dir_bsock; /* duped DIR socket */ + BSOCK *hb_bsock; /* duped SD socket */ + BSOCK *hb_dir_bsock; /* duped DIR socket */ alist *RunScripts; /* Commands to run before and after job */ bool pki_sign; /* Enable PKI Signatures? */ bool pki_encrypt; /* Enable PKI Encryption? */ diff --git a/bacula/src/lib/bnet.c b/bacula/src/lib/bnet.c index beec5573cb..08481133c0 100644 --- a/bacula/src/lib/bnet.c +++ b/bacula/src/lib/bnet.c @@ -110,7 +110,7 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes) { int32_t nleft, nwritten; - if (bsock->m_spool) { + if (bsock->is_spooling()) { nwritten = fwrite(ptr, 1, nbytes, bsock->m_spool_fd); if (nwritten != nbytes) { berrno be; @@ -698,7 +698,7 @@ BSOCK *dup_bsock(BSOCK * osock) if (osock->host()) { bsock->set_host(bstrdup(osock->host())); } - bsock->m_duped = true; + bsock->set_duped(); return bsock; } diff --git a/bacula/src/lib/bsock.c b/bacula/src/lib/bsock.c index 21642ea207..809431f40a 100644 --- a/bacula/src/lib/bsock.c +++ b/bacula/src/lib/bsock.c @@ -287,7 +287,7 @@ bool BSOCK::send() /* send data packet */ timer_start = watchdog_time; /* start timer */ - m_timed_out = 0; + clear_timed_out(); /* Full I/O done in one write */ rc = write_nbytes(this, (char *)hdr, pktsiz); timer_start = 0; /* clear timer */ @@ -376,7 +376,7 @@ int32_t BSOCK::recv() read_seqno++; /* bump sequence number */ timer_start = watchdog_time; /* set start wait time */ - m_timed_out = 0; + clear_timed_out(); /* get data size -- in int32_t */ if ((nbytes = read_nbytes(this, (char *)&pktsiz, sizeof(int32_t))) <= 0) { timer_start = 0; /* clear timer */ @@ -430,7 +430,7 @@ int32_t BSOCK::recv() } timer_start = watchdog_time; /* set start wait time */ - m_timed_out = 0; + clear_timed_out(); /* now read the actual data */ if ((nbytes = read_nbytes(this, msg, pktsiz)) <= 0) { timer_start = 0; /* clear timer */ diff --git a/bacula/src/lib/bsock.h b/bacula/src/lib/bsock.h index aeea39cf39..0f963bafeb 100644 --- a/bacula/src/lib/bsock.h +++ b/bacula/src/lib/bsock.h @@ -58,6 +58,10 @@ private: char *m_host; /* Host name/IP */ int m_port; /* desired port */ btimer_t *m_tid; /* timer id */ + volatile bool m_timed_out: 1; /* timed out in read/write */ + volatile bool m_terminated: 1; /* set when BNET_TERMINATE arrives */ + bool m_duped: 1; /* set if duped BSOCK */ + bool m_spool: 1; /* set for spooling */ void fin_init(JCR * jcr, int sockfd, const char *who, const char *host, int port, struct sockaddr *lclient_addr); @@ -75,10 +79,6 @@ public: int m_blocking; /* blocking state (0 = nonblocking, 1 = blocking) */ volatile int errors; /* incremented for each error on socket */ volatile bool m_suppress_error_msgs: 1; /* set to suppress error messages */ - volatile bool m_timed_out: 1; /* timed out in read/write */ - volatile bool m_terminated: 1; /* set when BNET_TERMINATE arrives */ - bool m_duped: 1; /* set if duped BSOCK */ - bool m_spool: 1; /* set for spooling */ volatile time_t timer_start; /* time started read/write */ volatile time_t timeout; /* timeout BSOCK after this interval */ POOLMEM *msg; /* message pool buffer */ @@ -122,8 +122,15 @@ public: int port() { return m_port; }; JCR *jcr() { return m_jcr; }; JCR *get_jcr() { return m_jcr; }; + bool is_spooling() { return m_spool; }; + bool is_duped() { return m_duped; }; bool is_terminated() { return m_terminated; }; bool is_timed_out() { return m_timed_out; }; + void set_spooling() { m_spool = true; }; + void clear_spooling() { m_spool = false; }; + void set_duped() { m_duped = true; }; + void set_timed_out() { m_timed_out = true; }; + void clear_timed_out() { m_timed_out = false; }; void set_terminated() { m_terminated = true; }; void start_timer(int sec) { m_tid = start_bsock_timer(this, sec); }; void stop_timer() { stop_bsock_timer(m_tid); }; diff --git a/bacula/src/lib/btimers.c b/bacula/src/lib/btimers.c index 5f388fe0fa..2e59cf0f7e 100644 --- a/bacula/src/lib/btimers.c +++ b/bacula/src/lib/btimers.c @@ -225,7 +225,7 @@ static void callback_thread_timer(watchdog_t *self) wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL)); if (wid->type == TYPE_BSOCK && wid->bsock) { - wid->bsock->m_timed_out = true; + wid->bsock->set_timed_out(); } pthread_kill(wid->tid, TIMEOUT_SIGNAL); } diff --git a/bacula/src/lib/jcr.c b/bacula/src/lib/jcr.c index 638c451892..a93e53346f 100644 --- a/bacula/src/lib/jcr.c +++ b/bacula/src/lib/jcr.c @@ -745,7 +745,7 @@ static void jcr_timeout_check(watchdog_t *self) timer_start = fd->timer_start; if (timer_start && (watchdog_time - timer_start) > fd->timeout) { fd->timer_start = 0; /* turn off timer */ - fd->m_timed_out = true; + fd->set_timed_out(); Jmsg(jcr, M_ERROR, 0, _( "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"), watchdog_time - timer_start); @@ -757,7 +757,7 @@ static void jcr_timeout_check(watchdog_t *self) timer_start = fd->timer_start; if (timer_start && (watchdog_time - timer_start) > fd->timeout) { fd->timer_start = 0; /* turn off timer */ - fd->m_timed_out = true; + fd->set_timed_out(); Jmsg(jcr, M_ERROR, 0, _( "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"), watchdog_time - timer_start); @@ -769,7 +769,7 @@ static void jcr_timeout_check(watchdog_t *self) timer_start = fd->timer_start; if (timer_start && (watchdog_time - timer_start) > fd->timeout) { fd->timer_start = 0; /* turn off timer */ - fd->m_timed_out = true; + fd->set_timed_out(); Jmsg(jcr, M_ERROR, 0, _( "Watchdog sending kill after %d secs to thread stalled reading Director.\n"), watchdog_time - timer_start); diff --git a/bacula/src/lib/tls.c b/bacula/src/lib/tls.c index 0a8a15ef61..54a3f88b10 100644 --- a/bacula/src/lib/tls.c +++ b/bacula/src/lib/tls.c @@ -1,24 +1,3 @@ -/* - * tls.c TLS support functions - * - * Author: Landon Fuller - * - * Version $Id$ - * - * This file was contributed to the Bacula project by Landon Fuller - * and Three Rings Design, Inc. - * - * Three Rings Design, Inc. has been granted a perpetual, worldwide, - * non-exclusive, no-charge, royalty-free, irrevocable copyright - * license to reproduce, prepare derivative works of, publicly - * display, publicly perform, sublicense, and distribute the original - * work contributed by Three Rings Design, Inc. and its employees to - * the Bacula project in source or object form. - * - * If you wish to license contributions from Three Rings Design, Inc, - * under an alternate open source license please contact - * Landon Fuller . - */ /* Bacula® - The Network Backup Solution @@ -46,6 +25,27 @@ (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, Switzerland, email:ftf@fsfeurope.org. */ +/* + * tls.c TLS support functions + * + * Author: Landon Fuller + * + * Version $Id$ + * + * This file was contributed to the Bacula project by Landon Fuller + * and Three Rings Design, Inc. + * + * Three Rings Design, Inc. has been granted a perpetual, worldwide, + * non-exclusive, no-charge, royalty-free, irrevocable copyright + * license to reproduce, prepare derivative works of, publicly + * display, publicly perform, sublicense, and distribute the original + * work contributed by Three Rings Design, Inc. and its employees to + * the Bacula project in source or object form. + * + * If you wish to license contributions from Three Rings Design, Inc, + * under an alternate open source license please contact + * Landon Fuller . + */ #include "bacula.h" @@ -431,7 +431,6 @@ err: BIO_free(bio); SSL_free(tls->openssl); free(tls); - return NULL; } @@ -459,11 +458,11 @@ static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server) fdmax = bsock->m_fd + 1; /* Ensure that socket is non-blocking */ - flags = bnet_set_nonblocking(bsock); + flags = bsock->set_nonblocking(); /* start timer */ bsock->timer_start = watchdog_time; - bsock->m_timed_out = 0; + bsock->set_timed_out(false); for (;;) { if (server) { @@ -514,7 +513,7 @@ static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server) cleanup: /* Restore saved flags */ - bnet_restore_blocking(bsock, flags); + bsock->restore_blocking(flags); /* Clear timer */ bsock->timer_start = 0; @@ -529,7 +528,7 @@ cleanup: bool tls_bsock_connect(BSOCK *bsock) { /* SSL_connect(bsock->tls) */ - return (openssl_bsock_session_start(bsock, false)); + return openssl_bsock_session_start(bsock, false); } /* @@ -540,7 +539,7 @@ bool tls_bsock_connect(BSOCK *bsock) bool tls_bsock_accept(BSOCK *bsock) { /* SSL_accept(bsock->tls) */ - return (openssl_bsock_session_start(bsock, true)); + return openssl_bsock_session_start(bsock, true); } /* @@ -575,16 +574,16 @@ void tls_bsock_shutdown(BSOCK *bsock) err = SSL_shutdown(bsock->tls->openssl); switch (SSL_get_error(bsock->tls->openssl, err)) { - case SSL_ERROR_NONE: - break; - case SSL_ERROR_ZERO_RETURN: - /* TLS connection was shut down on us via a TLS protocol-level closure */ - openssl_post_errors(M_ERROR, _("TLS shutdown failure.")); - break; - default: - /* Socket Error Occured */ - openssl_post_errors(M_ERROR, _("TLS shutdown failure.")); - break; + case SSL_ERROR_NONE: + break; + case SSL_ERROR_ZERO_RETURN: + /* TLS connection was shut down on us via a TLS protocol-level closure */ + openssl_post_errors(M_ERROR, _("TLS shutdown failure.")); + break; + default: + /* Socket Error Occured */ + openssl_post_errors(M_ERROR, _("TLS shutdown failure.")); + break; } /* Restore saved flags */ @@ -610,12 +609,11 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b /* start timer */ bsock->timer_start = watchdog_time; - bsock->m_timed_out = 0; + bsock->set_timed_out(false); nleft = nbytes; while (nleft > 0) { - if (write) { nwritten = SSL_write(tls->openssl, ptr, nleft); } else { @@ -630,26 +628,20 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b ptr += nwritten; } break; - case SSL_ERROR_ZERO_RETURN: - /* TLS connection was cleanly shut down */ - openssl_post_errors(M_ERROR, _("TLS read/write failure.")); - goto cleanup; + case SSL_ERROR_WANT_READ: - /* If we timeout of a select, this will be unset */ - FD_SET((unsigned) bsock->m_fd, &fdset); - tv.tv_sec = 10; - tv.tv_usec = 0; - /* Block until we can read */ - select(fdmax, &fdset, NULL, &fdset, &tv); - break; case SSL_ERROR_WANT_WRITE: - /* If we timeout of a select, this will be unset */ - FD_SET((unsigned) bsock->m_fd, &fdset); + /* If we timeout on a select, this will be unset */ + FD_SET((unsigned)bsock->m_fd, &fdset); tv.tv_sec = 10; tv.tv_usec = 0; - /* Block until we can write */ + /* Block until we can read or write */ select(fdmax, NULL, &fdset, &fdset, &tv); break; + + case SSL_ERROR_ZERO_RETURN: + /* TLS connection was cleanly shut down */ + /* Fall through wanted */ default: /* Socket Error Occured */ openssl_post_errors(M_ERROR, _("TLS read/write failure.")); @@ -673,26 +665,29 @@ cleanup: /* Clear timer */ bsock->timer_start = 0; - return nbytes - nleft; } -int tls_bsock_writen(BSOCK *bsock, char *ptr, int32_t nbytes) { +int tls_bsock_writen(BSOCK *bsock, char *ptr, int32_t nbytes) +{ /* SSL_write(bsock->tls->openssl, ptr, nbytes) */ - return (openssl_bsock_readwrite(bsock, ptr, nbytes, true)); + return openssl_bsock_readwrite(bsock, ptr, nbytes, true); } -int tls_bsock_readn(BSOCK *bsock, char *ptr, int32_t nbytes) { +int tls_bsock_readn(BSOCK *bsock, char *ptr, int32_t nbytes) +{ /* SSL_read(bsock->tls->openssl, ptr, nbytes) */ - return (openssl_bsock_readwrite(bsock, ptr, nbytes, false)); + return openssl_bsock_readwrite(bsock, ptr, nbytes, false); } #else /* HAVE_OPENSSL */ # error No TLS implementation available. #endif /* !HAVE_OPENSSL */ -#else + +#else /* TLS NOT enabled, dummy routines substituted */ + /* Dummy routines */ TLS_CONTEXT *new_tls_context(const char *ca_certfile, const char *ca_certdir, diff --git a/bacula/src/stored/append.c b/bacula/src/stored/append.c index f93af93fa3..1bda34c632 100644 --- a/bacula/src/stored/append.c +++ b/bacula/src/stored/append.c @@ -236,17 +236,17 @@ bool do_append_data(JCR *jcr) crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) { if (!jcr->no_attributes) { if (are_attributes_spooled(jcr)) { - jcr->dir_bsock->m_spool = true; + jcr->dir_bsock->set_spooling(); } Dmsg0(850, "Send attributes to dir.\n"); if (!dir_update_file_attributes(dcr, &rec)) { - jcr->dir_bsock->m_spool = false; + jcr->dir_bsock->clear_spooling(); Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"), bnet_strerror(jcr->dir_bsock)); ok = false; break; } - jcr->dir_bsock->m_spool = false; + jcr->dir_bsock->clear_spooling(); } } Dmsg0(650, "Enter bnet_get\n"); diff --git a/bacula/src/stored/mac.c b/bacula/src/stored/mac.c index a1ad6a3a89..16640f4e92 100644 --- a/bacula/src/stored/mac.c +++ b/bacula/src/stored/mac.c @@ -240,16 +240,16 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) { if (!jcr->no_attributes) { if (are_attributes_spooled(jcr)) { - jcr->dir_bsock->m_spool = true; + jcr->dir_bsock->set_spooling(); } Dmsg0(850, "Send attributes to dir.\n"); if (!dir_update_file_attributes(jcr->dcr, rec)) { - jcr->dir_bsock->m_spool = false; + jcr->dir_bsock->clear_spooling(); Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"), bnet_strerror(jcr->dir_bsock)); return false; } - jcr->dir_bsock->m_spool = false; + jcr->dir_bsock->clear_spooling(); } } diff --git a/bacula/src/stored/spool.c b/bacula/src/stored/spool.c index 1c8be6c8e2..62426a0c34 100644 --- a/bacula/src/stored/spool.c +++ b/bacula/src/stored/spool.c @@ -677,6 +677,6 @@ bool close_attr_spool_file(JCR *jcr, BSOCK *bs) unlink(name); free_pool_memory(name); bs->m_spool_fd = NULL; - bs->m_spool = false; + bs->clear_spooling(); return true; } diff --git a/bacula/src/stored/stored.c b/bacula/src/stored/stored.c index 8eff4fc495..996f4fc358 100644 --- a/bacula/src/stored/stored.c +++ b/bacula/src/stored/stored.c @@ -555,7 +555,7 @@ void terminate_stored(int sig) set_jcr_job_status(jcr, JS_Canceled); fd = jcr->file_bsock; if (fd) { - fd->m_timed_out = true; + fd->set_timed_out(); Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId); pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL); /* ***FIXME*** wiffle through all dcrs */ diff --git a/bacula/src/version.h b/bacula/src/version.h index ddd9938c56..5476016a83 100644 --- a/bacula/src/version.h +++ b/bacula/src/version.h @@ -4,8 +4,8 @@ #undef VERSION #define VERSION "2.1.21" -#define BDATE "21 June 2007" -#define LSMDATE "21Jun07" +#define BDATE "23 June 2007" +#define LSMDATE "23Jun07" #define PROG_COPYRIGHT "Copyright (C) %d-2007 Free Software Foundation Europe e.V.\n" #define BYEAR "2007" /* year for copyright messages in progs */ diff --git a/bacula/technotes-2.1 b/bacula/technotes-2.1 index 40e5d68f48..cc0d7ea885 100644 --- a/bacula/technotes-2.1 +++ b/bacula/technotes-2.1 @@ -1,6 +1,11 @@ Technical notes on version 2.1 General: +23Jun07 +kes Remove some duplicated code in tls.c +kes Move more of the bsock member variables into private and access + them with methods. +kes Add Frank Sweetser's tls-test to the regression suite. 22Jun07 kes Add Swedish sv.po file 21Jun07 -- 2.39.5