]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/nagios/check_bacula/authenticate.c
Backport from BEE
[bacula/bacula] / bacula / examples / nagios / check_bacula / 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    Bacula® - The Network Backup Solution
15
16    Copyright (C) 2004-2014 Free Software Foundation Europe e.V.
17
18    The main author of Bacula is Kern Sibbald, with contributions from
19    many others, a complete list can be found in the file AUTHORS.
20    This program is Free Software; you can redistribute it and/or
21    modify it under the terms of version three of the GNU Affero General Public
22    License as published by the Free Software Foundation plus additions
23    that are listed in the file LICENSE.
24
25    This program is distributed in the hope that it will be useful, but
26    WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28    General Public License for more details.
29
30    You should have received a copy of the GNU Affero General Public License
31    along with this program; if not, write to the Free Software
32    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33    02110-1301, USA.
34
35    Bacula® is a registered trademark of John Walker.
36    The licensor of Bacula is the Free Software Foundation Europe
37    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
38    Switzerland, email:ftf@fsfeurope.org.
39 */
40
41 #include "bacula.h"
42 #include "check_bacula.h"
43
44 void senditf(const char *fmt, ...);
45 void sendit(const char *buf);
46
47 /* Commands sent to Director */
48 static char DIRhello[]    = "Hello %s calling\n";
49
50 /* Response from Director */
51 static char DIROKhello[]   = "1000 OK:";
52
53 /* Commands sent to Storage daemon and File daemon and received
54  *  from the User Agent */
55 static char SDFDhello[]    = "Hello Director %s calling\n";
56
57 /* Response from SD */
58 static char SDOKhello[]   = "3000 OK Hello\n";
59 /* Response from FD */
60 static char FDOKhello[] = "2000 OK Hello";
61
62 /* Forward referenced functions */
63
64 /*
65  * Authenticate Director
66  */
67 int authenticate_director(BSOCK *dir, char *dirname, char *password)
68 {
69    int tls_local_need = BNET_TLS_NONE;
70    int tls_remote_need = BNET_TLS_NONE;
71    int compatible = true;
72    char bashed_name[MAX_NAME_LENGTH];
73
74    bstrncpy(bashed_name, dirname, sizeof(bashed_name));
75    bash_spaces(bashed_name);
76
77    /* Timeout Hello after 5 mins */
78    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
79    bnet_fsend(dir, DIRhello, bashed_name);
80
81    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
82        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
83       stop_bsock_timer(tid);
84       return 0;
85    }
86
87    Dmsg1(6, ">dird: %s", dir->msg);
88    if (bnet_recv(dir) <= 0) {
89       stop_bsock_timer(tid);
90       return 0;
91    }
92    Dmsg1(10, "<dird: %s", dir->msg);
93    stop_bsock_timer(tid);
94    if (strncmp(dir->msg, DIROKhello, sizeof(DIROKhello)-1) != 0) {
95       return 0;
96    }
97    return 1;
98 }
99
100 /*
101  * Authenticate Storage daemon connection
102  */
103 int authenticate_storage_daemon(BSOCK *sd, char *sdname, char* password)
104 {
105    char dirname[MAX_NAME_LENGTH];
106    int tls_local_need = BNET_TLS_NONE;
107    int tls_remote_need = BNET_TLS_NONE;
108    int compatible = true;
109
110    /*
111     * Send my name to the Storage daemon then do authentication
112     */
113    bstrncpy(dirname, sdname, sizeof(dirname));
114    bash_spaces(dirname);
115    /* Timeout Hello after 5 mins */
116    btimer_t *tid = start_bsock_timer(sd, 60 * 5);
117    if (!bnet_fsend(sd, SDFDhello, dirname)) {
118       stop_bsock_timer(tid);
119       return 0;
120    }
121    if (!cram_md5_respond(sd, password, &tls_remote_need, &compatible) ||
122        !cram_md5_challenge(sd, password, tls_local_need, compatible)) {
123       stop_bsock_timer(tid);
124       return 0;
125    }
126    Dmsg1(116, ">stored: %s", sd->msg);
127    if (bnet_recv(sd) <= 0) {
128       stop_bsock_timer(tid);
129       return 0;
130    }
131    Dmsg1(110, "<stored: %s", sd->msg);
132    stop_bsock_timer(tid);
133    if (strncmp(sd->msg, SDOKhello, sizeof(SDOKhello)) != 0) {
134       return 0;
135    }
136    return 1;
137 }
138
139 /*
140  * Authenticate File daemon connection
141  */
142 int authenticate_file_daemon(BSOCK *fd, char *fdname, char *password)
143 {
144    char dirname[MAX_NAME_LENGTH];
145    int tls_local_need = BNET_TLS_NONE;
146    int tls_remote_need = BNET_TLS_NONE;
147    int compatible = true;
148
149    /*
150     * Send my name to the File daemon then do authentication
151     */
152    bstrncpy(dirname, fdname, sizeof(dirname));
153    bash_spaces(dirname);
154    /* Timeout Hello after 5 mins */
155    btimer_t *tid = start_bsock_timer(fd, 60 * 5);
156    if (!bnet_fsend(fd, SDFDhello, dirname)) {
157       stop_bsock_timer(tid);
158       return 0;
159    }
160    if (!cram_md5_respond(fd, password, &tls_remote_need, &compatible) ||
161        !cram_md5_challenge(fd, password, tls_local_need, compatible)) {
162       stop_bsock_timer(tid);
163       return 0;
164    }
165    Dmsg1(116, ">filed: %s", fd->msg);
166    if (bnet_recv(fd) <= 0) {
167       stop_bsock_timer(tid);
168       return 0;
169    }
170    Dmsg1(110, "<stored: %s", fd->msg);
171    stop_bsock_timer(tid);
172    if ((strncmp(fd->msg, FDOKhello, strlen(FDOKhello)) != 0)) {
173       return 0;
174    }
175    return 1;
176 }