]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bsock.c
Updates
[bacula/bacula] / bacula / src / lib / bsock.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Network Utility Routines
30  *
31  *  by Kern Sibbald
32  *
33  *   Version $Id: $
34  */
35
36
37 #include "bacula.h"
38 #include "jcr.h"
39 #include <netdb.h>
40
41 #ifndef ENODATA                    /* not defined on BSD systems */
42 #define ENODATA EPIPE
43 #endif
44
45 #ifdef HAVE_WIN32
46 #define socketRead(fd, buf, len)  ::recv(fd, buf, len, 0)
47 #define socketWrite(fd, buf, len) ::send(fd, buf, len, 0)
48 #define socketClose(fd)           ::closesocket(fd)
49 #else
50 #define socketRead(fd, buf, len)  ::read(fd, buf, len)
51 #define socketWrite(fd, buf, len) ::write(fd, buf, len)
52 #define socketClose(fd)           ::close(fd)
53 #endif
54
55 /*
56  * This is a non-class BSOCK "constructor"  because we want to 
57  *   call the Bacula smartalloc routines instead of new.
58  */
59 BSOCK *new_bsock()
60 {
61    BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
62    bsock->init();
63    return bsock;
64 }
65
66 void BSOCK::init()
67 {
68    memset(this, 0, sizeof(BSOCK));
69    m_blocking = 1;
70    msg = get_pool_memory(PM_MESSAGE);
71    errmsg = get_pool_memory(PM_MESSAGE);
72    /*
73     * ****FIXME**** reduce this to a few hours once
74     *   heartbeats are implemented
75     */
76    timeout = 60 * 60 * 6 * 24;   /* 6 days timeout */
77 }
78
79 /*
80  * This is our "class destructor" that ensures that we use
81  *   smartalloc rather than the system free().
82  */
83 void BSOCK::free_bsock()
84 {
85    destroy();
86 }
87
88 void BSOCK::free_tls()
89 {
90    free_tls_connection(this->tls);
91    this->tls = NULL;
92 }   
93
94 /*
95  * Try to connect to host for max_retry_time at retry_time intervals.
96  *   Note, you must have called the constructor prior to calling
97  *   this routine.
98  */
99 bool BSOCK::connect(JCR * jcr, int retry_interval, utime_t max_retry_time,
100                     utime_t heart_beat,
101                     const char *name, char *host, char *service, int port,
102                     int verbose)
103 {
104    bool ok = false;
105    int i;
106    int fatal = 0;
107    time_t begin_time = time(NULL);
108    time_t now;
109    btimer_t *tid = NULL;
110
111    /* Try to trap out of OS call when time expires */
112    if (max_retry_time) {
113       tid = start_thread_timer(jcr, pthread_self(), (uint32_t)max_retry_time);
114    }
115    
116    for (i = 0; !open(jcr, name, host, service, port, heart_beat, &fatal);
117         i -= retry_interval) {
118       berrno be;
119       if (fatal || (jcr && job_canceled(jcr))) {
120          goto bail_out;
121       }
122       Dmsg4(100, "Unable to connect to %s on %s:%d. ERR=%s\n",
123             name, host, port, be.bstrerror());
124       if (i < 0) {
125          i = 60 * 5;               /* complain again in 5 minutes */
126          if (verbose)
127             Qmsg4(jcr, M_WARNING, 0, _(
128                "Could not connect to %s on %s:%d. ERR=%s\n"
129                "Retrying ...\n"), name, host, port, be.bstrerror());
130       }
131       bmicrosleep(retry_interval, 0);
132       now = time(NULL);
133       if (begin_time + max_retry_time <= now) {
134          Qmsg4(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
135                name, host, port, be.bstrerror());
136          goto bail_out;
137       }
138    }
139    ok = true;
140
141 bail_out:
142    if (tid) {
143       stop_thread_timer(tid);
144    }
145    return ok;
146 }
147
148
149 /*       
150  * Finish initialization of the pocket structure.
151  */
152 void BSOCK::fin_init(JCR * jcr, int sockfd, const char *who, const char *host, int port,
153             struct sockaddr *lclient_addr)
154 {
155    Dmsg3(100, "who=%s host=%s port=%d\n", who, host, port);
156    m_fd = sockfd;
157    set_who(bstrdup(who));
158    set_host(bstrdup(host));
159    set_port(port);
160    memcpy(&client_addr, lclient_addr, sizeof(client_addr));
161    set_jcr(jcr);
162 }
163
164 /*
165  * Open a TCP connection to the server
166  * Returns NULL
167  * Returns BSOCK * pointer on success
168  *
169  */
170 bool BSOCK::open(JCR *jcr, const char *name, char *host, char *service,
171             int port, utime_t heart_beat, int *fatal)
172 {
173    int sockfd = -1;
174    dlist *addr_list;
175    IPADDR *ipaddr;
176    bool connected = false;
177    int turnon = 1;
178    const char *errstr;
179    int save_errno = 0;
180
181    /*
182     * Fill in the structure serv_addr with the address of
183     * the server that we want to connect with.
184     */
185    if ((addr_list = bnet_host2ipaddrs(host, 0, &errstr)) == NULL) {
186       /* Note errstr is not malloc'ed */
187       Qmsg2(jcr, M_ERROR, 0, _("gethostbyname() for host \"%s\" failed: ERR=%s\n"),
188             host, errstr);
189       Dmsg2(100, "bnet_host2ipaddrs() for host %s failed: ERR=%s\n",
190             host, errstr);
191       *fatal = 1;
192       return false;
193    }
194
195    foreach_dlist(ipaddr, addr_list) {
196       ipaddr->set_port_net(htons(port));
197       char allbuf[256 * 10];
198       char curbuf[256];
199       Dmsg2(100, "Current %sAll %s\n",
200                    ipaddr->build_address_str(curbuf, sizeof(curbuf)),
201                    build_addresses_str(addr_list, allbuf, sizeof(allbuf)));
202       /* Open a TCP socket */
203       if ((sockfd = socket(ipaddr->get_family(), SOCK_STREAM, 0)) < 0) {
204          berrno be;
205          save_errno = errno;
206          *fatal = 1;
207          Pmsg3(000, _("Socket open error. proto=%d port=%d. ERR=%s\n"),
208             ipaddr->get_family(), ipaddr->get_port_host_order(), be.bstrerror());
209          continue;
210       }
211       /*
212        * Keep socket from timing out from inactivity
213        */
214       if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
215          berrno be;
216          Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
217                be.bstrerror());
218       }
219 #if defined(TCP_KEEPIDLE)
220       if (heart_beat) {
221          int opt = heart_beat
222          if (setsockopt(sockfd, IPPROTO_IP, TCP_KEEPIDLE, (sockopt_val_t)&opt, sizeof(opt)) < 0) {
223             berrno be;
224             Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPIDLE on socket: %s\n"),
225                   be.bstrerror());
226          }
227       }
228 #endif
229
230       /* connect to server */
231       if (::connect(sockfd, ipaddr->get_sockaddr(), ipaddr->get_sockaddr_len()) < 0) {
232          save_errno = errno;
233          socketClose(sockfd);
234          continue;
235       }
236       *fatal = 0;
237       connected = true;
238       break;
239    }
240
241    if (!connected) {
242       free_addresses(addr_list);
243       errno = save_errno | b_errno_win32;
244       return false;
245    }
246    /*
247     * Keep socket from timing out from inactivity
248     *   Do this a second time out of paranoia
249     */
250    if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
251       berrno be;
252       Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
253             be.bstrerror());
254    }
255    fin_init(jcr, sockfd, name, host, port, ipaddr->get_sockaddr());
256    free_addresses(addr_list);
257    return true;
258 }
259
260
261
262 /*
263  * Send a message over the network. The send consists of
264  * two network packets. The first is sends a 32 bit integer containing
265  * the length of the data packet which follows.
266  *
267  * Returns: false on failure
268  *          true  on success
269  */
270 bool BSOCK::send()
271 {
272    int32_t rc;
273    int32_t pktsiz;
274    int32_t *hdr;
275
276    if (errors || is_terminated() || msglen > 1000000) {
277       return false;
278    }
279    /* Compute total packet length */
280    if (msglen <= 0) {
281       pktsiz = sizeof(pktsiz);               /* signal, no data */
282    } else {
283       pktsiz = msglen + sizeof(pktsiz);      /* data */
284    }
285    /* Store packet length at head of message -- note, we
286     *  have reserved an int32_t just before msg, so we can
287     *  store there 
288     */
289    hdr = (int32_t *)(msg - (int)sizeof(pktsiz));
290    *hdr = htonl(msglen);                     /* store signal/length */
291
292    out_msg_no++;            /* increment message number */
293
294    /* send data packet */
295    timer_start = watchdog_time;  /* start timer */
296    clear_timed_out();
297    /* Full I/O done in one write */
298    rc = write_nbytes(this, (char *)hdr, pktsiz);
299    timer_start = 0;         /* clear timer */
300    if (rc != pktsiz) {
301       errors++;
302       if (errno == 0) {
303          b_errno = EIO;
304       } else {
305          b_errno = errno;
306       }
307       if (rc < 0) {
308          if (!m_suppress_error_msgs) {
309             Qmsg5(m_jcr, M_ERROR, 0,
310                   _("Write error sending %d bytes to %s:%s:%d: ERR=%s\n"), 
311                   msglen, m_who,
312                   m_host, m_port, this->bstrerror());
313          }
314       } else {
315          Qmsg5(m_jcr, M_ERROR, 0,
316                _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"),
317                msglen, m_who, m_host, m_port, rc);
318       }
319       return false;
320    }
321    return true;
322 }
323
324 /*
325  * Format and send a message
326  *  Returns: false on error
327  *           true  on success
328  */
329 bool BSOCK::fsend(const char *fmt, ...)
330 {
331    va_list arg_ptr;
332    int maxlen;
333
334    if (errors || is_terminated()) {
335       return false;
336    }
337    /* This probably won't work, but we vsnprintf, then if we
338     * get a negative length or a length greater than our buffer
339     * (depending on which library is used), the printf was truncated, so
340     * get a bigger buffer and try again.
341     */
342    for (;;) {
343       maxlen = sizeof_pool_memory(msg) - 1;
344       va_start(arg_ptr, fmt);
345       msglen = bvsnprintf(msg, maxlen, fmt, arg_ptr);
346       va_end(arg_ptr);
347       if (msglen > 0 && msglen < (maxlen - 5)) {
348          break;
349       }
350       msg = realloc_pool_memory(msg, maxlen + maxlen / 2);
351    }
352    return send();
353 }
354
355 /*
356  * Receive a message from the other end. Each message consists of
357  * two packets. The first is a header that contains the size
358  * of the data that follows in the second packet.
359  * Returns number of bytes read (may return zero)
360  * Returns -1 on signal (BNET_SIGNAL)
361  * Returns -2 on hard end of file (BNET_HARDEOF)
362  * Returns -3 on error  (BNET_ERROR)
363  *
364  *  Unfortunately, it is a bit complicated because we have these
365  *    four return types:
366  *    1. Normal data
367  *    2. Signal including end of data stream
368  *    3. Hard end of file
369  *    4. Error
370  *  Using is_bnet_stop() and is_bnet_error() you can figure this all out.
371  */
372 int32_t BSOCK::recv()
373 {
374    int32_t nbytes;
375    int32_t pktsiz;
376
377    msg[0] = 0;
378    msglen = 0;
379    if (errors || is_terminated()) {
380       return BNET_HARDEOF;
381    }
382
383    read_seqno++;            /* bump sequence number */
384    timer_start = watchdog_time;  /* set start wait time */
385    clear_timed_out();
386    /* get data size -- in int32_t */
387    if ((nbytes = read_nbytes(this, (char *)&pktsiz, sizeof(int32_t))) <= 0) {
388       timer_start = 0;      /* clear timer */
389       /* probably pipe broken because client died */
390       if (errno == 0) {
391          b_errno = ENODATA;
392       } else {
393          b_errno = errno;
394       }
395       errors++;
396       return BNET_HARDEOF;         /* assume hard EOF received */
397    }
398    timer_start = 0;         /* clear timer */
399    if (nbytes != sizeof(int32_t)) {
400       errors++;
401       b_errno = EIO;
402       Qmsg5(m_jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
403             sizeof(int32_t), nbytes, m_who, m_host, m_port);
404       return BNET_ERROR;
405    }
406
407    pktsiz = ntohl(pktsiz);         /* decode no. of bytes that follow */
408
409    if (pktsiz == 0) {              /* No data transferred */
410       timer_start = 0;      /* clear timer */
411       in_msg_no++;
412       msglen = 0;
413       return 0;                    /* zero bytes read */
414    }
415
416    /* If signal or packet size too big */
417    if (pktsiz < 0 || pktsiz > 1000000) {
418       if (pktsiz > 0) {            /* if packet too big */
419          Qmsg3(m_jcr, M_FATAL, 0,
420                _("Packet size too big from \"%s:%s:%d. Terminating connection.\n"),
421                m_who, m_host, m_port);
422          pktsiz = BNET_TERMINATE;  /* hang up */
423       }
424       if (pktsiz == BNET_TERMINATE) {
425          set_terminated();
426       }
427       timer_start = 0;      /* clear timer */
428       b_errno = ENODATA;
429       msglen = pktsiz;      /* signal code */
430       return BNET_SIGNAL;          /* signal */
431    }
432
433    /* Make sure the buffer is big enough + one byte for EOS */
434    if (pktsiz >= (int32_t) sizeof_pool_memory(msg)) {
435       msg = realloc_pool_memory(msg, pktsiz + 100);
436    }
437
438    timer_start = watchdog_time;  /* set start wait time */
439    clear_timed_out();
440    /* now read the actual data */
441    if ((nbytes = read_nbytes(this, msg, pktsiz)) <= 0) {
442       timer_start = 0;      /* clear timer */
443       if (errno == 0) {
444          b_errno = ENODATA;
445       } else {
446          b_errno = errno;
447       }
448       errors++;
449       Qmsg4(m_jcr, M_ERROR, 0, _("Read error from %s:%s:%d: ERR=%s\n"),
450             m_who, m_host, m_port, this->bstrerror());
451       return BNET_ERROR;
452    }
453    timer_start = 0;         /* clear timer */
454    in_msg_no++;
455    msglen = nbytes;
456    if (nbytes != pktsiz) {
457       b_errno = EIO;
458       errors++;
459       Qmsg5(m_jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
460             pktsiz, nbytes, m_who, m_host, m_port);
461       return BNET_ERROR;
462    }
463    /* always add a zero by to properly terminate any
464     * string that was send to us. Note, we ensured above that the
465     * buffer is at least one byte longer than the message length.
466     */
467    msg[nbytes] = 0; /* terminate in case it is a string */
468    sm_check(__FILE__, __LINE__, false);
469    return nbytes;                  /* return actual length of message */
470 }
471
472
473 /*
474  * Send a signal
475  */
476 bool BSOCK::signal(int signal)
477 {
478    msglen = signal;
479    if (signal == BNET_TERMINATE) {
480       m_suppress_error_msgs = true;
481    }
482    return send();
483 }
484
485 /* 
486  * Despool spooled attributes
487  */
488 bool BSOCK::despool(void update_attr_spool_size(ssize_t size), ssize_t tsize)
489 {
490    int32_t pktsiz;
491    size_t nbytes;
492    ssize_t last = 0, size = 0;
493    int count = 0;
494
495    rewind(m_spool_fd);
496
497 #if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
498    posix_fadvise(fileno(m_spool_fd), 0, 0, POSIX_FADV_WILLNEED);
499 #endif
500
501    while (fread((char *)&pktsiz, 1, sizeof(int32_t), m_spool_fd) ==
502           sizeof(int32_t)) {
503       size += sizeof(int32_t);
504       msglen = ntohl(pktsiz);
505       if (msglen > 0) {
506          if (msglen > (int32_t) sizeof_pool_memory(msg)) {
507             msg = realloc_pool_memory(msg, msglen + 1);
508          }
509          nbytes = fread(msg, 1, msglen, m_spool_fd);
510          if (nbytes != (size_t) msglen) {
511             berrno be;
512             Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, msglen);
513             Qmsg1(get_jcr(), M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
514                   be.bstrerror());
515             update_attr_spool_size(tsize - last);
516             return false;
517          }
518          size += nbytes;
519          if ((++count & 0x3F) == 0) {
520             update_attr_spool_size(size - last);
521             last = size;
522          }
523       }
524       send();
525    }
526    update_attr_spool_size(tsize - last);
527    if (ferror(m_spool_fd)) {
528       berrno be;
529       Qmsg1(get_jcr(), M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
530             be.bstrerror());
531       return false;
532    }
533    return true;
534 }
535
536 /*
537  * Return the string for the error that occurred
538  * on the socket. Only the first error is retained.
539  */
540 const char *BSOCK::bstrerror()
541 {
542    berrno be;
543    if (errmsg == NULL) {
544       errmsg = get_pool_memory(PM_MESSAGE);
545    }
546    pm_strcpy(errmsg, be.bstrerror(b_errno));
547    return errmsg;
548 }
549
550 int BSOCK::get_peer(char *buf, socklen_t buflen) 
551 {
552 #if !defined(HAVE_WIN32)
553     if (peer_addr.sin_family == 0) {
554         socklen_t salen = sizeof(peer_addr);
555         int rval = (getpeername)(m_fd, (struct sockaddr *)&peer_addr, &salen);
556         if (rval < 0) return rval;
557     }
558     if (!inet_ntop(peer_addr.sin_family, &peer_addr.sin_addr, buf, buflen))
559         return -1;
560
561     return 0;
562 #else
563     return -1;
564 #endif
565 }
566
567 /*
568  * Set the network buffer size, suggested size is in size.
569  *  Actual size obtained is returned in bs->msglen
570  *
571  *  Returns: false on failure
572  *           true  on success
573  */
574 bool BSOCK::set_buffer_size(uint32_t size, int rw)
575 {
576    uint32_t dbuf_size, start_size;
577 #if defined(IP_TOS) && defined(IPTOS_THROUGHPUT)
578    int opt;
579    opt = IPTOS_THROUGHPUT;
580    setsockopt(fd, IPPROTO_IP, IP_TOS, (sockopt_val_t)&opt, sizeof(opt));
581 #endif
582
583    if (size != 0) {
584       dbuf_size = size;
585    } else {
586       dbuf_size = DEFAULT_NETWORK_BUFFER_SIZE;
587    }
588    start_size = dbuf_size;
589    if ((msg = realloc_pool_memory(msg, dbuf_size + 100)) == NULL) {
590       Qmsg0(get_jcr(), M_FATAL, 0, _("Could not malloc BSOCK data buffer\n"));
591       return false;
592    }
593    if (rw & BNET_SETBUF_READ) {
594       while ((dbuf_size > TAPE_BSIZE) && (setsockopt(m_fd, SOL_SOCKET,
595               SO_RCVBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
596          berrno be;
597          Qmsg1(get_jcr(), M_ERROR, 0, _("sockopt error: %s\n"), be.bstrerror());
598          dbuf_size -= TAPE_BSIZE;
599       }
600       Dmsg1(200, "set network buffer size=%d\n", dbuf_size);
601       if (dbuf_size != start_size) {
602          Qmsg1(get_jcr(), M_WARNING, 0,
603                _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
604       }
605       if (dbuf_size % TAPE_BSIZE != 0) {
606          Qmsg1(get_jcr(), M_ABORT, 0,
607                _("Network buffer size %d not multiple of tape block size.\n"),
608                dbuf_size);
609       }
610    }
611    if (size != 0) {
612       dbuf_size = size;
613    } else {
614       dbuf_size = DEFAULT_NETWORK_BUFFER_SIZE;
615    }
616    start_size = dbuf_size;
617    if (rw & BNET_SETBUF_WRITE) {
618       while ((dbuf_size > TAPE_BSIZE) && (setsockopt(m_fd, SOL_SOCKET,
619               SO_SNDBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
620          berrno be;
621          Qmsg1(get_jcr(), M_ERROR, 0, _("sockopt error: %s\n"), be.bstrerror());
622          dbuf_size -= TAPE_BSIZE;
623       }
624       Dmsg1(900, "set network buffer size=%d\n", dbuf_size);
625       if (dbuf_size != start_size) {
626          Qmsg1(get_jcr(), M_WARNING, 0,
627                _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
628       }
629       if (dbuf_size % TAPE_BSIZE != 0) {
630          Qmsg1(get_jcr(), M_ABORT, 0,
631                _("Network buffer size %d not multiple of tape block size.\n"),
632                dbuf_size);
633       }
634    }
635
636    msglen = dbuf_size;
637    return true;
638 }
639
640 /*
641  * Set socket non-blocking
642  * Returns previous socket flag
643  */
644 int BSOCK::set_nonblocking()
645 {
646 #ifndef HAVE_WIN32
647    int oflags;
648
649    /* Get current flags */
650    if ((oflags = fcntl(m_fd, F_GETFL, 0)) < 0) {
651       berrno be;
652       Jmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_GETFL error. ERR=%s\n"), be.bstrerror());
653    }
654
655    /* Set O_NONBLOCK flag */
656    if ((fcntl(m_fd, F_SETFL, oflags|O_NONBLOCK)) < 0) {
657       berrno be;
658       Jmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
659    }
660
661    m_blocking = 0;
662    return oflags;
663 #else
664    int flags;
665    u_long ioctlArg = 1;
666
667    flags = m_blocking;
668    ioctlsocket(m_fd, FIONBIO, &ioctlArg);
669    m_blocking = 0;
670
671    return flags;
672 #endif
673 }
674
675 /*
676  * Set socket blocking
677  * Returns previous socket flags
678  */
679 int BSOCK::set_blocking()
680 {
681 #ifndef HAVE_WIN32
682    int oflags;
683    /* Get current flags */
684    if ((oflags = fcntl(m_fd, F_GETFL, 0)) < 0) {
685       berrno be;
686       Jmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_GETFL error. ERR=%s\n"), be.bstrerror());
687    }
688
689    /* Set O_NONBLOCK flag */
690    if ((fcntl(m_fd, F_SETFL, oflags & ~O_NONBLOCK)) < 0) {
691       berrno be;
692       Jmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
693    }
694
695    m_blocking = 1;
696    return oflags;
697 #else
698    int flags;
699    u_long ioctlArg = 0;
700
701    flags = m_blocking;
702    ioctlsocket(m_fd, FIONBIO, &ioctlArg);
703    m_blocking = 1;
704
705    return flags;
706 #endif
707 }
708
709 /*
710  * Restores socket flags
711  */
712 void BSOCK::restore_blocking (int flags) 
713 {
714 #ifndef HAVE_WIN32
715    if ((fcntl(m_fd, F_SETFL, flags)) < 0) {
716       berrno be;
717       Jmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
718    }
719
720    m_blocking = (flags & O_NONBLOCK) ? true : false;
721 #else
722    u_long ioctlArg = flags;
723
724    ioctlsocket(m_fd, FIONBIO, &ioctlArg);
725    m_blocking = 1;
726 #endif
727 }
728
729 /*
730  * Wait for a specified time for data to appear on
731  * the BSOCK connection.
732  *
733  *   Returns: 1 if data available
734  *            0 if timeout
735  *           -1 if error
736  */
737 int BSOCK::wait_data(int sec)
738 {
739    fd_set fdset;
740    struct timeval tv;
741
742    FD_ZERO(&fdset);
743    FD_SET((unsigned)m_fd, &fdset);
744    for (;;) {
745       tv.tv_sec = sec;
746       tv.tv_usec = 0;
747       switch (select(m_fd + 1, &fdset, NULL, NULL, &tv)) {
748       case 0:                      /* timeout */
749          b_errno = 0;
750          return 0;
751       case -1:
752          b_errno = errno;
753          if (errno == EINTR) {
754             continue;
755          }
756          return -1;                /* error return */
757       default:
758          b_errno = 0;
759          return 1;
760       }
761    }
762 }
763
764 /*
765  * As above, but returns on interrupt
766  */
767 int BSOCK::wait_data_intr(int sec)
768 {
769    fd_set fdset;
770    struct timeval tv;
771
772    FD_ZERO(&fdset);
773    FD_SET((unsigned)m_fd, &fdset);
774    tv.tv_sec = sec;
775    tv.tv_usec = 0;
776    switch (select(m_fd + 1, &fdset, NULL, NULL, &tv)) {
777    case 0:                      /* timeout */
778       b_errno = 0;
779       return 0;
780    case -1:
781       b_errno = errno;
782       return -1;                /* error return */
783    default:
784       b_errno = 0;
785    }
786    return 1;
787 }
788
789 /*
790  * Note, this routine closes and destroys all the sockets
791  *  that are open including the duped ones.
792  */
793 #ifndef SHUT_RDWR
794 #define SHUT_RDWR 2
795 #endif
796
797 void BSOCK::close()
798 {
799    BSOCK *bsock = this;
800    BSOCK *next;
801
802    for (; bsock; bsock = next) {
803       next = bsock->m_next;           /* get possible pointer to next before destoryed */
804       if (!bsock->m_duped) {
805          /* Shutdown tls cleanly. */
806          if (bsock->tls) {
807             tls_bsock_shutdown(bsock);
808             free_tls_connection(bsock->tls);
809             bsock->tls = NULL;
810          }
811          if (bsock->is_timed_out()) {
812             shutdown(bsock->m_fd, SHUT_RDWR);   /* discard any pending I/O */
813          }
814          socketClose(bsock->m_fd);      /* normal close */
815       }
816       bsock->destroy();
817    }
818    return;
819 }
820
821 void BSOCK::destroy()
822 {
823    if (msg) {
824       free_pool_memory(msg);
825       msg = NULL;
826    } else {
827       ASSERT(1 == 0);              /* double close */
828    }
829    if (errmsg) {
830       free_pool_memory(errmsg);
831       errmsg = NULL;
832    }
833    if (m_who) {
834       free(m_who);
835       m_who = NULL;
836    }
837    if (m_host) {
838       free(m_host);
839       m_host = NULL;
840    }
841    free(this);
842 }
843
844 /* Commands sent to Director */
845 static char hello[]    = "Hello %s calling\n";
846
847 /* Response from Director */
848 static char OKhello[]   = "1000 OK:";
849
850 /*
851  * Authenticate Director
852  */
853 bool BSOCK::authenticate_director(const char *name, const char *password,
854                TLS_CONTEXT *tls_ctx, char *msg, int msglen)
855 {
856    int tls_local_need = BNET_TLS_NONE;
857    int tls_remote_need = BNET_TLS_NONE;
858    int compatible = true;
859    char bashed_name[MAX_NAME_LENGTH];
860    BSOCK *dir = this;        /* for readability */
861
862    msg[0] = 0;
863    /*
864     * Send my name to the Director then do authentication
865     */
866
867    /* Timeout Hello after 15 secs */
868    dir->start_timer(15);
869    dir->fsend(hello, bashed_name);
870
871    if (get_tls_enable(tls_ctx)) {
872       tls_local_need = get_tls_enable(tls_ctx) ? BNET_TLS_REQUIRED : BNET_TLS_OK;
873    }
874
875    /* respond to Dir challenge */
876    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
877        /* Now challenge dir */
878        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
879       bsnprintf(msg, msglen, _("Director authorization problem at \"%s:%d\"\n"),
880          dir->host(), dir->port());
881       goto bail_out;
882    }
883
884    /* Verify that the remote host is willing to meet our TLS requirements */
885    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
886       bsnprintf(msg, msglen, _("Authorization problem:"
887              " Remote server at \"%s:%d\" did not advertise required TLS support.\n"),
888              dir->host(), dir->port());
889       goto bail_out;
890    }
891
892    /* Verify that we are willing to meet the remote host's requirements */
893    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
894       bsnprintf(msg, msglen, _("Authorization problem with Director at \"%s:%d\":"
895                      " Remote server requires TLS.\n"),
896                      dir->host(), dir->port());
897
898       goto bail_out;
899    }
900
901    /* Is TLS Enabled? */
902    if (have_tls) {
903       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
904          /* Engage TLS! Full Speed Ahead! */
905          if (!bnet_tls_client(tls_ctx, dir, NULL)) {
906             bsnprintf(msg, msglen, _("TLS negotiation failed with Director at \"%s:%d\"\n"),
907                dir->host(), dir->port());
908             goto bail_out;
909          }
910       }
911    }
912
913    Dmsg1(6, ">dird: %s", dir->msg);
914    if (dir->recv() <= 0) {
915       dir->stop_timer();
916       bsnprintf(msg, msglen, _("Bad response to Hello command: ERR=%s\n"
917                       "The Director at \"%s:%d\" is probably not running.\n"),
918                     dir->bstrerror(), dir->host(), dir->port());
919       return false;
920    }
921
922   dir->stop_timer();
923    Dmsg1(10, "<dird: %s", dir->msg);
924    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
925       bsnprintf(msg, msglen, _("Director at \"%s:%d\" rejected Hello command\n"),
926          dir->host(), dir->port());
927       return false;
928    } else {
929       bsnprintf(msg, msglen, "%s", dir->msg);
930    }
931    return true;
932
933 bail_out:
934    dir->stop_timer();
935    bsnprintf(msg, msglen, _("Authorization problem with Director at \"%s:%d\"\n"
936              "Most likely the passwords do not agree.\n"
937              "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
938              "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"), 
939              dir->host(), dir->port());
940    return false;
941 }