/
authenticate.cpp
223 lines (189 loc) · 6.88 KB
/
authenticate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2004-2008 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Bareos authentication. Provides authentication with File and Storage daemons.
*
* Nicolas Boichat, August MMIV
*/
#include "monitoritem.h"
#include "authenticate.h"
#include "jcr.h"
#include "monitoritemthread.h"
void senditf(const char *fmt, ...);
void sendit(const char *buf);
/* Commands sent to Director */
static char DIRhello[] = "Hello %s calling\n";
/* Response from Director */
static char DIROKhello[] = "1000 OK:";
/* Commands sent to Storage daemon and File daemon and received
* from the User Agent */
static char SDFDhello[] = "Hello Director %s calling\n";
/* Response from SD */
static char SDOKhello[] = "3000 OK Hello\n";
/* Response from FD */
static char FDOKhello[] = "2000 OK Hello";
/* Forward referenced functions */
/*
* Authenticate Director
*/
static bool authenticate_director(JCR *jcr)
{
const MONITORRES *monitor = MonitorItemThread::instance()->getMonitor();
BSOCK *dir = jcr->dir_bsock;
int tls_local_need = BNET_TLS_NONE;
int tls_remote_need = BNET_TLS_NONE;
bool compatible = true;
char bashed_name[MAX_NAME_LENGTH];
char *password;
bstrncpy(bashed_name, monitor->hdr.name, sizeof(bashed_name));
bash_spaces(bashed_name);
password = monitor->password;
/* Timeout Hello after 5 mins */
btimer_t *tid = start_bsock_timer(dir, 60 * 5);
dir->fsend(DIRhello, bashed_name);
if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
!cram_md5_challenge(dir, password, tls_local_need, compatible)) {
stop_bsock_timer(tid);
Jmsg(jcr, M_FATAL, 0, _("Director authorization problem.\n"
"Most likely the passwords do not agree.\n"
"Please see %s for help.\n"), MANUAL_AUTH_URL);
return false;
}
Dmsg1(6, ">dird: %s", dir->msg);
if (dir->recv() <= 0) {
stop_bsock_timer(tid);
Jmsg1(jcr, M_FATAL, 0, _("Bad response to Hello command: ERR=%s\n"),
dir->bstrerror());
return false;
}
Dmsg1(10, "<dird: %s", dir->msg);
stop_bsock_timer(tid);
if (strncmp(dir->msg, DIROKhello, sizeof(DIROKhello)-1) != 0) {
Jmsg0(jcr, M_FATAL, 0, _("Director rejected Hello command\n"));
return false;
} else {
Jmsg0(jcr, M_INFO, 0, dir->msg);
}
return true;
}
/*
* Authenticate Storage daemon connection
*/
static bool authenticate_storage_daemon(JCR *jcr, STORERES* store)
{
const MONITORRES *monitor = MonitorItemThread::instance()->getMonitor();
BSOCK *sd = jcr->store_bsock;
char dirname[MAX_NAME_LENGTH];
int tls_local_need = BNET_TLS_NONE;
int tls_remote_need = BNET_TLS_NONE;
bool compatible = true;
/*
* Send my name to the Storage daemon then do authentication
*/
bstrncpy(dirname, monitor->hdr.name, sizeof(dirname));
bash_spaces(dirname);
/* Timeout Hello after 5 mins */
btimer_t *tid = start_bsock_timer(sd, 60 * 5);
if (!sd->fsend(SDFDhello, dirname)) {
stop_bsock_timer(tid);
Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
return false;
}
if (!cram_md5_respond(sd, store->password, &tls_remote_need, &compatible) ||
!cram_md5_challenge(sd, store->password, tls_local_need, compatible)) {
stop_bsock_timer(tid);
Jmsg0(jcr, M_FATAL, 0, _("Director and Storage daemon passwords or names not the same.\n"
"Please see " MANUAL_AUTH_URL " for help.\n"));
return false;
}
Dmsg1(116, ">stored: %s", sd->msg);
if (sd->recv() <= 0) {
stop_bsock_timer(tid);
Jmsg1(jcr, M_FATAL, 0, _("bdird<stored: bad response to Hello command: ERR=%s\n"),
sd->bstrerror());
return false;
}
Dmsg1(110, "<stored: %s", sd->msg);
stop_bsock_timer(tid);
if (strncmp(sd->msg, SDOKhello, sizeof(SDOKhello)) != 0) {
Jmsg0(jcr, M_FATAL, 0, _("Storage daemon rejected Hello command\n"));
return false;
}
return true;
}
/*
* Authenticate File daemon connection
*/
static bool authenticate_file_daemon(JCR *jcr, CLIENTRES* client)
{
const MONITORRES *monitor = MonitorItemThread::instance()->getMonitor();
BSOCK *fd = jcr->file_bsock;
char dirname[MAX_NAME_LENGTH];
int tls_local_need = BNET_TLS_NONE;
int tls_remote_need = BNET_TLS_NONE;
bool compatible = true;
/*
* Send my name to the File daemon then do authentication
*/
bstrncpy(dirname, monitor->hdr.name, sizeof(dirname));
bash_spaces(dirname);
/* Timeout Hello after 5 mins */
btimer_t *tid = start_bsock_timer(fd, 60 * 5);
if (!fd->fsend(SDFDhello, dirname)) {
stop_bsock_timer(tid);
Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
return false;
}
if (!cram_md5_respond(fd, client->password, &tls_remote_need, &compatible) ||
!cram_md5_challenge(fd, client->password, tls_local_need, compatible)) {
stop_bsock_timer(tid);
Jmsg(jcr, M_FATAL, 0, _("Director and File daemon passwords or names not the same.\n"
"Please see %s for help.\n"), MANUAL_AUTH_URL);
return false;
}
Dmsg1(116, ">filed: %s", fd->msg);
if (fd->recv() <= 0) {
stop_bsock_timer(tid);
Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon to Hello command: ERR=%s\n"),
fd->bstrerror());
return false;
}
Dmsg1(110, "<stored: %s", fd->msg);
stop_bsock_timer(tid);
if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)-1) != 0) {
Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Hello command\n"));
return false;
}
return true;
}
bool authenticate_daemon(MonitorItem* item, JCR *jcr)
{
switch (item->type()) {
case R_DIRECTOR:
return authenticate_director(jcr);
case R_CLIENT:
return authenticate_file_daemon(jcr, (CLIENTRES*)item->resource());
case R_STORAGE:
return authenticate_storage_daemon(jcr, (STORERES*)item->resource());
default:
printf(_("Error, currentitem is not a Client or a Storage..\n"));
return false;
}
return false;
}