-
Notifications
You must be signed in to change notification settings - Fork 256
/
bsock_test.cc
310 lines (248 loc) · 8.92 KB
/
bsock_test.cc
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2018 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.
*/
#include "gtest/gtest.h"
#include <iostream>
#include <fstream>
#include <thread>
#include <future>
#include "include/bareos.h"
#include "console/console_conf.h"
#include "include/jcr.h"
class UaContext {
BareosSocket *UA_sock;
};
class StorageResource;
#include "dird/authenticate.h"
#define MIN_MSG_LEN 15
#define MAX_MSG_LEN 175 + 25
#define PORT 54321
#define HOST "127.0.0.1"
#define CONSOLENAME "testconsole"
#define CONSOLEPASSWORD "secret"
static void InitForTest()
{
setlocale(LC_ALL, "");
bindtextdomain("bareos", LOCALEDIR);
textdomain("bareos");
debug_level = 0;
SetTrace(0);
working_directory = "/tmp";
MyNameIs(0, NULL, "bsock_test");
InitMsg(NULL, NULL);
}
int create_accepted_server_socket(int port)
{
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
return new_socket;
}
void start_bareos_server(std::promise<bool> *promise, std::string console_name,
std::string console_password, std::string server_address, int server_port, bool enable_tls_psk)
{
int newsockfd = create_accepted_server_socket(server_port);
bool retval = false;
BareosSocket *bs;
bs = New(BareosSocketTCP);
bs->sleep_time_after_authentication_error = 0;
bs->fd_ = newsockfd;
bs->SetWho(bstrdup("client"));
memset(&bs->peer_addr, 0, sizeof(bs->peer_addr));
char *name = (char *)console_name.c_str();
s_password *password = new (s_password);
password->encoding = p_encoding_md5;
password->value = (char *)console_password.c_str();
ConsoleResource *cons = new (ConsoleResource);
cons->tls_psk.enable = enable_tls_psk;
if (bs->recv() <= 0) {
Dmsg1(10, _("Connection request from %s failed.\n"), bs->who());
//Bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->close();
}
// Do a sanity check on the message received
if (bs->message_length < MIN_MSG_LEN || bs->message_length > MAX_MSG_LEN) {
Dmsg2(10, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->message_length);
//Bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->close();
}
Dmsg1(10, "Cons->Dir: %s", bs->msg);
if (bs->AuthenticateInboundConnection(NULL, "Console", name, *password, cons)) {
bs->fsend(_("1000 OK: %s Version: %s (%s)\n"), my_name, VERSION, BDATE);
Dmsg0(10, "Server: inbound auth successful\n");
retval = true;
} else {
Dmsg0(10, "Server: inbound auth failed\n");
}
promise->set_value(retval);
bs->close();
}
int connect_to_server(std::string console_name, std::string console_password,
std::string server_address, int server_port, bool enable_tls_psk)
{
utime_t heart_beat = 0;
char errmsg[1024];
int errmsg_len = sizeof(errmsg);
JobControlRecord jcr;
memset(&jcr, 0, sizeof(jcr));
char *name = (char *)console_name.c_str();
DirectorResource *dir = new (DirectorResource);
dir->address = (char *)server_address.c_str();
dir->DIRport = htons(server_port);
dir->tls_psk.enable = enable_tls_psk;
s_password *password = new (s_password);
password->encoding = p_encoding_md5;
password->value = (char *)console_password.c_str();
BareosSocketTCP *UA_sock = New(BareosSocketTCP);
UA_sock->sleep_time_after_authentication_error = 0;
jcr.dir_bsock = UA_sock;
if (!UA_sock->connect(NULL, 1, 15, heart_beat, "Director daemon", (char *)server_address.c_str(),
NULL, server_port, false)) {
Dmsg0(10, "socket connect failed\n");
delete UA_sock;
return false;
} else {
Dmsg0(10, "socket connect OK\n");
if (!UA_sock->AuthenticateWithDirector(&jcr, name, *password, errmsg, errmsg_len, dir)) {
Emsg0(M_ERROR, 0, "Authenticate Failed\n");
UA_sock->close();
return false;
} else {
Dmsg0(10, "Authenticate Connect to Server successful!\n");
UA_sock->close();
return true;
}
}
}
std::string client_cons_name;
std::string client_cons_password;
std::string server_cons_name;
std::string server_cons_password;
int port = PORT;
TEST(bsock, auth_works)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;
InitForTest();
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, port, false);
Dmsg0(10, "connecting to server\n");
ASSERT_TRUE(connect_to_server(client_cons_name, client_cons_password, HOST, port, false));
server_thread.join();
ASSERT_TRUE(future.get());
}
TEST(bsock, auth_works_with_different_names)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = "differentclientname";
server_cons_password = client_cons_password;
InitForTest();
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, port, false);
Dmsg0(10, "connecting to server\n");
ASSERT_TRUE(connect_to_server(client_cons_name, client_cons_password, HOST, port, false));
server_thread.join();
ASSERT_TRUE(future.get());
}
TEST(bsock, auth_fails_with_different_passwords)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = client_cons_name;
server_cons_password = "a_different_password";
InitForTest();
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, port, false);
Dmsg0(10, "connecting to server\n");
ASSERT_FALSE(connect_to_server(client_cons_name, client_cons_password, HOST, port, false));
server_thread.join();
ASSERT_FALSE(future.get());
}
TEST(bsock, auth_works_with_tls_psk)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;
InitForTest();
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, port, true);
Dmsg0(10, "connecting to server\n");
ASSERT_TRUE(connect_to_server(client_cons_name, client_cons_password, HOST, port, true));
server_thread.join();
ASSERT_TRUE(future.get());
}
TEST(bsock, auth_fails_with_different_names_with_tls_psk)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = "differentclientname";
server_cons_password = client_cons_password;
InitForTest();
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, port, true);
Dmsg0(10, "connecting to server\n");
ASSERT_FALSE(connect_to_server(client_cons_name, client_cons_password, HOST, port, true));
server_thread.join();
ASSERT_FALSE(future.get());
}