]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tray-monitor/authenticate.c
Fix copyright dates.
[bacula/bacula] / bacula / src / tray-monitor / authenticate.c
1 /*
2  *
3  *   Bacula authentication. Provides authentication with
4  *     File and Storage daemons.
5  *
6  *     Nicolas Boichat, August MMIV
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *
12  */
13 /*
14    Copyright (C) 2004 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License
18    as published by the Free Software Foundation; either version 2
19    of the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
29    MA 02111-1307, USA.
30  */
31
32 #include "bacula.h"
33 #include "tray-monitor.h"
34 #include "jcr.h"
35
36 void senditf(const char *fmt, ...);
37 void sendit(const char *buf); 
38
39 /* Commands sent to Storage daemon and File daemon and received
40  *  from the User Agent */
41 static char hello[]    = "Hello Director %s calling\n";
42
43 /* Response from SD */
44 static char OKhello[]   = "3000 OK Hello\n";
45 /* Response from FD */
46 static char FDOKhello[] = "2000 OK Hello\n";
47
48 /* Forward referenced functions */
49
50 /*
51  * Authenticate Storage daemon connection
52  */
53 int authenticate_storage_daemon(JCR *jcr, MONITOR *monitor, STORE* store)
54 {
55    BSOCK *sd = jcr->store_bsock;
56    char dirname[MAX_NAME_LENGTH];
57    int ssl_need = BNET_SSL_NONE;
58
59    /* 
60     * Send my name to the Storage daemon then do authentication
61     */
62    bstrncpy(dirname, monitor->hdr.name, sizeof(dirname));
63    bash_spaces(dirname);
64    /* Timeout Hello after 5 mins */
65    btimer_t *tid = start_bsock_timer(sd, 60 * 5);
66    if (!bnet_fsend(sd, hello, dirname)) {
67       stop_bsock_timer(tid);
68       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
69       return 0;
70    }
71    if (!cram_md5_get_auth(sd, store->password, ssl_need) || 
72        !cram_md5_auth(sd, store->password, ssl_need)) {
73       stop_bsock_timer(tid);
74       Jmsg0(jcr, M_FATAL, 0, _("Director and Storage daemon passwords or names not the same.\n"   
75        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
76       return 0;
77    }
78    Dmsg1(116, ">stored: %s", sd->msg);
79    if (bnet_recv(sd) <= 0) {
80       stop_bsock_timer(tid);
81       Jmsg1(jcr, M_FATAL, 0, _("bdird<stored: bad response to Hello command: ERR=%s\n"),
82          bnet_strerror(sd));
83       return 0;
84    }
85    Dmsg1(110, "<stored: %s", sd->msg);
86    stop_bsock_timer(tid);
87    if (strncmp(sd->msg, OKhello, sizeof(OKhello)) != 0) {
88       Jmsg0(jcr, M_FATAL, 0, _("Storage daemon rejected Hello command\n"));
89       return 0;
90    }
91    return 1;
92 }
93
94 /*
95  * Authenticate File daemon connection
96  */
97 int authenticate_file_daemon(JCR *jcr, MONITOR *monitor, CLIENT* client)
98 {
99    BSOCK *fd = jcr->file_bsock;
100    char dirname[MAX_NAME_LENGTH];
101    int ssl_need = BNET_SSL_NONE;
102
103    /* 
104     * Send my name to the File daemon then do authentication
105     */
106    bstrncpy(dirname, monitor->hdr.name, sizeof(dirname));
107    bash_spaces(dirname);
108    /* Timeout Hello after 5 mins */
109    btimer_t *tid = start_bsock_timer(fd, 60 * 5);
110    if (!bnet_fsend(fd, hello, dirname)) {
111       stop_bsock_timer(tid);
112       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
113       return 0;
114    }
115    if (!cram_md5_get_auth(fd, client->password, ssl_need) || 
116        !cram_md5_auth(fd, client->password, ssl_need)) {
117       stop_bsock_timer(tid);
118       Jmsg(jcr, M_FATAL, 0, _("Director and File daemon passwords or names not the same.\n"   
119        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
120       return 0;
121    }
122    Dmsg1(116, ">filed: %s", fd->msg);
123    if (bnet_recv(fd) <= 0) {
124       stop_bsock_timer(tid);
125       Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon to Hello command: ERR=%s\n"),
126          bnet_strerror(fd));
127       return 0;
128    }
129    Dmsg1(110, "<stored: %s", fd->msg);
130    stop_bsock_timer(tid);
131    if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) != 0) {
132       Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Hello command\n"));
133       return 0;
134    }
135    return 1;
136 }