Line data Source code
1 : #include "../burp.h"
2 : #include "../alloc.h"
3 : #include "../asfd.h"
4 : #include "../async.h"
5 : #include "../cmd.h"
6 : #include "../conf.h"
7 : #include "../conffile.h"
8 : #include "../fsops.h"
9 : #include "../handy.h"
10 : #include "../incexc_recv.h"
11 : #include "../incexc_send.h"
12 : #include "../iobuf.h"
13 : #include "../log.h"
14 : #include "../prepend.h"
15 : #include "autoupgrade.h"
16 : #include "extra_comms.h"
17 :
18 : #include <librsync.h>
19 :
20 279 : static int append_to_feat(char **feat, const char *str)
21 : {
22 279 : char *tmp=NULL;
23 279 : if(!*feat)
24 : {
25 35 : if(!(*feat=strdup_w(str, __func__)))
26 : return -1;
27 35 : return 0;
28 : }
29 244 : if(!(tmp=prepend(*feat, str)))
30 : return -1;
31 244 : free_w(feat);
32 244 : *feat=tmp;
33 244 : return 0;
34 : }
35 :
36 : // It is unfortunate that we are having to figure out the server-initiated
37 : // restore paths here instead of setting it in a struct sdirs.
38 : // But doing the extra_comms needs to come before setting the sdirs, because
39 : // extra_comms sets up a bunch of settings that sdirs need to know.
40 35 : static char *get_restorepath_proto1(struct conf **cconfs)
41 : {
42 35 : char *tmp=NULL;
43 35 : char *restorepath=NULL;
44 35 : if((tmp=prepend_s(get_string(cconfs[OPT_DIRECTORY]),
45 35 : get_string(cconfs[OPT_CNAME]))))
46 35 : restorepath=prepend_s(tmp, "restore");
47 35 : free_w(&tmp);
48 35 : return restorepath;
49 : }
50 :
51 35 : static char *get_restorepath_proto2(struct conf **cconfs)
52 : {
53 35 : char *tmp1=NULL;
54 35 : char *tmp2=NULL;
55 35 : char *restorepath=NULL;
56 35 : if(!(tmp1=prepend_s(get_string(cconfs[OPT_DIRECTORY]),
57 35 : get_string(cconfs[OPT_DEDUP_GROUP]))))
58 : goto error;
59 35 : if(!(tmp2=prepend_s(tmp1, "clients")))
60 : goto error;
61 35 : free_w(&tmp1);
62 35 : if(!(tmp1=prepend_s(tmp2, get_string(cconfs[OPT_CNAME]))))
63 : goto error;
64 35 : if(!(restorepath=prepend_s(tmp1, "restore")))
65 : goto error;
66 : goto end;
67 : error:
68 0 : free_w(&restorepath);
69 : end:
70 35 : free_w(&tmp1);
71 35 : free_w(&tmp2);
72 35 : return restorepath;
73 : }
74 :
75 35 : static int set_restore_path(struct conf **cconfs, char **feat)
76 : {
77 35 : int ret=-1;
78 35 : char *restorepath1=NULL;
79 35 : char *restorepath2=NULL;
80 35 : if(!(restorepath1=get_restorepath_proto1(cconfs))
81 35 : || !(restorepath2=get_restorepath_proto2(cconfs)))
82 : goto end;
83 35 : if(is_reg_lstat(restorepath1)==1
84 4 : && set_string(cconfs[OPT_RESTORE_PATH], restorepath1))
85 : goto end;
86 35 : else if(is_reg_lstat(restorepath2)==1
87 0 : && set_string(cconfs[OPT_RESTORE_PATH], restorepath2))
88 : goto end;
89 35 : if(get_string(cconfs[OPT_RESTORE_PATH])
90 4 : && append_to_feat(feat, "srestore:"))
91 : goto end;
92 : ret=0;
93 : end:
94 35 : free_w(&restorepath1);
95 35 : free_w(&restorepath2);
96 35 : return ret;
97 : }
98 :
99 : struct vers
100 : {
101 : long min;
102 : long cli;
103 : long ser;
104 : long feat_list;
105 : long directory_tree;
106 : long burp2;
107 : long counters_json;
108 : };
109 :
110 35 : static int send_features(struct asfd *asfd, struct conf **cconfs,
111 : struct vers *vers)
112 : {
113 35 : int ret=-1;
114 35 : char *feat=NULL;
115 35 : enum protocol protocol=get_protocol(cconfs);
116 35 : struct strlist *startdir=get_strlist(cconfs[OPT_STARTDIR]);
117 35 : struct strlist *incglob=get_strlist(cconfs[OPT_INCGLOB]);
118 :
119 35 : if(append_to_feat(&feat, "extra_comms_begin ok:")
120 : /* clients can autoupgrade */
121 35 : || append_to_feat(&feat, "autoupgrade:")
122 : /* clients can give server incexc conf so that the
123 : server knows better what to do on resume */
124 35 : || append_to_feat(&feat, "incexc:")
125 : /* clients can give the server an alternative client
126 : to restore from */
127 35 : || append_to_feat(&feat, "orig_client:")
128 : /* clients can tell the server what kind of system they are. */
129 35 : || append_to_feat(&feat, "uname:"))
130 : goto end;
131 :
132 : /* Clients can receive restore initiated from the server. */
133 35 : if(set_restore_path(cconfs, &feat))
134 : goto end;
135 :
136 : /* Clients can receive incexc conf from the server.
137 : Only give it as an option if the server has some starting
138 : directory configured in the clientconfdir. */
139 35 : if((startdir || incglob)
140 0 : && append_to_feat(&feat, "sincexc:"))
141 : goto end;
142 :
143 35 : if(vers->cli>=vers->counters_json)
144 : {
145 : /* Clients can be sent cntrs on resume/verify/restore. */
146 30 : if(append_to_feat(&feat, "counters_json:"))
147 : goto end;
148 : }
149 :
150 : // We support CMD_MESSAGE.
151 35 : if(append_to_feat(&feat, "msg:"))
152 : goto end;
153 :
154 35 : if(protocol==PROTO_AUTO)
155 : {
156 : /* If the server is configured to use either protocol, let the
157 : client know that it can choose. */
158 25 : logp("Server is using protocol=0 (auto)\n");
159 25 : if(append_to_feat(&feat, "csetproto:"))
160 : goto end;
161 : }
162 : else
163 : {
164 10 : char p[32]="";
165 : /* Tell the client what we are going to use. */
166 10 : logp("Server is using protocol=%d\n", (int)protocol);
167 10 : snprintf(p, sizeof(p), "forceproto=%d:", (int)protocol);
168 10 : if(append_to_feat(&feat, p))
169 : goto end;
170 : }
171 :
172 : #ifndef RS_DEFAULT_STRONG_LEN
173 : if(append_to_feat(&feat, "rshash=blake2:"))
174 : goto end;
175 : #endif
176 :
177 : //printf("feat: %s\n", feat);
178 :
179 35 : if(asfd->write_str(asfd, CMD_GEN, feat))
180 : {
181 1 : logp("problem in extra_comms\n");
182 : goto end;
183 : }
184 :
185 : ret=0;
186 : end:
187 35 : free_w(&feat);
188 35 : return ret;
189 : }
190 :
191 34 : static int extra_comms_read(struct async *as,
192 : struct vers *vers, int *srestore,
193 : char **incexc, struct conf **globalcs, struct conf **cconfs)
194 : {
195 34 : int ret=-1;
196 : struct asfd *asfd;
197 : struct iobuf *rbuf;
198 34 : asfd=as->asfd;
199 34 : rbuf=asfd->rbuf;
200 :
201 : while(1)
202 : {
203 54 : iobuf_free_content(rbuf);
204 54 : if(asfd->read(asfd)) goto end;
205 :
206 54 : if(rbuf->cmd!=CMD_GEN)
207 : {
208 1 : iobuf_log_unexpected(rbuf, __func__);
209 : goto end;
210 : }
211 :
212 53 : if(!strcmp(rbuf->buf, "extra_comms_end"))
213 : {
214 24 : if(asfd->write_str(asfd, CMD_GEN, "extra_comms_end ok"))
215 : goto end;
216 : break;
217 : }
218 29 : else if(!strncmp_w(rbuf->buf, "autoupgrade:"))
219 : {
220 2 : char *os=NULL;
221 2 : const char *autoupgrade_dir=
222 2 : get_string(globalcs[OPT_AUTOUPGRADE_DIR]);
223 2 : os=rbuf->buf+strlen("autoupgrade:");
224 2 : iobuf_free_content(rbuf);
225 2 : if(os && *os && autoupgrade_server(as, vers->ser,
226 : vers->cli, os, get_cntr(globalcs),
227 : autoupgrade_dir))
228 : goto end;
229 : }
230 27 : else if(!strcmp(rbuf->buf, "srestore ok"))
231 : {
232 4 : char *restore_path=get_string(cconfs[OPT_RESTORE_PATH]);
233 4 : if(!restore_path)
234 : {
235 0 : logp("got srestore ok without a restore_path");
236 : goto end;
237 : }
238 :
239 4 : iobuf_free_content(rbuf);
240 : // Client can accept the restore.
241 : // Load the restore config, then send it.
242 4 : *srestore=1;
243 : // Need to wipe out OPT_INCEXDIR, as it is needed for
244 : // srestore includes. If it is not wiped out, it can
245 : // interfere if cconfs[OPT_RESTORE_PATH] contained no
246 : // includes.
247 4 : set_strlist(cconfs[OPT_INCEXCDIR], NULL);
248 4 : if(conf_parse_incexcs_path(cconfs, restore_path)
249 4 : || incexc_send_server_restore(asfd, cconfs))
250 : goto end;
251 : // Do not unlink it here - wait until
252 : // the client says that it wants to do the
253 : // restore.
254 : // Also need to leave it around if the
255 : // restore is to an alternative client, so
256 : // that the code below that reloads the config
257 : // can read it again.
258 : // NOTE: that appears to be in
259 : // src/server/run_action.c::client_can_restore()
260 : //unlink(get_string(cconfs[OPT_RESTORE_PATH]));
261 : }
262 23 : else if(!strcmp(rbuf->buf, "srestore not ok"))
263 : {
264 1 : const char *restore_path=get_string(
265 : cconfs[OPT_RESTORE_PATH]);
266 : // Client will not accept the restore.
267 1 : unlink(restore_path);
268 1 : if(set_string(cconfs[OPT_RESTORE_PATH], NULL))
269 : goto end;
270 1 : logp("Client not accepting server initiated restore.\n");
271 : }
272 22 : else if(!strcmp(rbuf->buf, "sincexc ok"))
273 : {
274 : // Client can accept incexc conf from the
275 : // server.
276 1 : iobuf_free_content(rbuf);
277 1 : if(incexc_send_server(asfd, cconfs))
278 : goto end;
279 : }
280 21 : else if(!strcmp(rbuf->buf, "incexc"))
281 : {
282 : // Client is telling server its incexc
283 : // configuration so that it can better decide
284 : // what to do on resume.
285 1 : iobuf_free_content(rbuf);
286 1 : if(incexc_recv_server(asfd, incexc, globalcs))
287 : goto end;
288 1 : if(*incexc)
289 : {
290 1 : char *tmp=NULL;
291 1 : char comp[32]="";
292 1 : snprintf(comp, sizeof(comp),
293 : "compression = %d\n",
294 : get_int(cconfs[OPT_COMPRESSION]));
295 1 : if(!(tmp=prepend(*incexc, comp)))
296 : goto end;
297 1 : free_w(incexc);
298 1 : *incexc=tmp;
299 : }
300 : }
301 20 : else if(!strcmp(rbuf->buf, "counters_json ok"))
302 : {
303 : // Client can accept counters on
304 : // resume/verify/restore.
305 1 : logp("Client supports being sent json counters.\n");
306 1 : set_int(cconfs[OPT_SEND_CLIENT_CNTR], 1);
307 : }
308 19 : else if(!strncmp_w(rbuf->buf, "uname=")
309 2 : && strlen(rbuf->buf)>strlen("uname="))
310 : {
311 2 : char *uname=rbuf->buf+strlen("uname=");
312 2 : if(!strncasecmp("Windows", uname, strlen("Windows")))
313 1 : set_int(cconfs[OPT_CLIENT_IS_WINDOWS], 1);
314 : }
315 17 : else if(!strncmp_w(rbuf->buf, "orig_client=")
316 3 : && strlen(rbuf->buf)>strlen("orig_client="))
317 : {
318 3 : if(conf_switch_to_orig_client(globalcs, cconfs,
319 : rbuf->buf+strlen("orig_client=")))
320 : goto end;
321 : // If this started out as a server-initiated
322 : // restore, need to load the restore file
323 : // again.
324 2 : if(*srestore)
325 : {
326 1 : if(conf_parse_incexcs_path(cconfs,
327 1 : get_string(cconfs[OPT_RESTORE_PATH])))
328 : goto end;
329 : }
330 2 : if(asfd->write_str(asfd, CMD_GEN, "orig_client ok"))
331 : goto end;
332 : }
333 14 : else if(!strncmp_w(rbuf->buf, "restore_spool="))
334 : {
335 : // Removed.
336 : }
337 14 : else if(!strncmp_w(rbuf->buf, "protocol="))
338 : {
339 11 : char msg[128]="";
340 : // Client wants to set protocol.
341 : enum protocol protocol;
342 : enum protocol cprotocol;
343 11 : const char *cliproto=NULL;
344 11 : protocol=get_protocol(cconfs);
345 11 : cliproto=rbuf->buf+strlen("protocol=");
346 11 : cprotocol=atoi(cliproto);
347 :
348 11 : if(protocol!=PROTO_AUTO)
349 : {
350 6 : if(protocol==cprotocol)
351 : {
352 2 : logp("Client is forcing protocol=%d\n", (int)protocol);
353 2 : continue;
354 : }
355 4 : snprintf(msg, sizeof(msg), "Client is trying to use protocol=%d but server is set to protocol=%d\n", (int)cprotocol, (int)protocol);
356 4 : log_and_send(asfd, msg);
357 5 : goto end;
358 : }
359 5 : else if(cprotocol==PROTO_1)
360 : {
361 2 : set_protocol(cconfs, cprotocol);
362 2 : set_protocol(globalcs, cprotocol);
363 : }
364 3 : else if(cprotocol==PROTO_2)
365 : {
366 2 : set_protocol(cconfs, cprotocol);
367 2 : set_protocol(globalcs, cprotocol);
368 : }
369 : else
370 : {
371 : snprintf(msg, sizeof(msg), "Client is trying to use protocol=%s, which is unknown\n", cliproto);
372 1 : log_and_send(asfd, msg);
373 : goto end;
374 : }
375 4 : logp("Client has set protocol=%d\n",
376 4 : (int)get_protocol(cconfs));
377 : }
378 3 : else if(!strncmp_w(rbuf->buf, "rshash=blake2"))
379 : {
380 : #ifdef RS_DEFAULT_STRONG_LEN
381 1 : logp("Client is trying to use librsync hash blake2, but server does not support it.\n");
382 : goto end;
383 : #else
384 : set_e_rshash(cconfs[OPT_RSHASH], RSHASH_BLAKE2);
385 : set_e_rshash(globalcs[OPT_RSHASH], RSHASH_BLAKE2);
386 : #endif
387 : }
388 2 : else if(!strncmp_w(rbuf->buf, "msg"))
389 : {
390 1 : set_int(cconfs[OPT_MESSAGE], 1);
391 1 : set_int(globalcs[OPT_MESSAGE], 1);
392 : }
393 : else
394 : {
395 1 : iobuf_log_unexpected(rbuf, __func__);
396 : goto end;
397 : }
398 : }
399 :
400 24 : ret=0;
401 : end:
402 34 : iobuf_free_content(rbuf);
403 34 : return ret;
404 : }
405 :
406 39 : static int vers_init(struct vers *vers, struct conf **cconfs)
407 : {
408 : memset(vers, 0, sizeof(struct vers));
409 78 : return ((vers->min=version_to_long("1.2.7"))<0
410 39 : || (vers->cli=version_to_long(get_string(cconfs[OPT_PEER_VERSION])))<0
411 39 : || (vers->ser=version_to_long(VERSION))<0
412 39 : || (vers->feat_list=version_to_long("1.3.0"))<0
413 39 : || (vers->directory_tree=version_to_long("1.3.6"))<0
414 39 : || (vers->burp2=version_to_long("2.0.0"))<0
415 78 : || (vers->counters_json=version_to_long("2.0.46"))<0);
416 : }
417 :
418 39 : int extra_comms(struct async *as,
419 : char **incexc, int *srestore, struct conf **confs, struct conf **cconfs)
420 : {
421 : struct vers vers;
422 : struct asfd *asfd;
423 39 : asfd=as->asfd;
424 : //char *restorepath=NULL;
425 39 : const char *peer_version=NULL;
426 :
427 39 : if(vers_init(&vers, cconfs))
428 : goto error;
429 :
430 39 : if(vers.cli<vers.directory_tree)
431 : {
432 3 : set_int(confs[OPT_DIRECTORY_TREE], 0);
433 3 : set_int(cconfs[OPT_DIRECTORY_TREE], 0);
434 : }
435 :
436 : // Clients before 1.2.7 did not know how to do extra comms, so skip
437 : // this section for them.
438 39 : if(vers.cli<vers.min)
439 : return 0;
440 :
441 37 : if(asfd_read_expect(asfd, CMD_GEN, "extra_comms_begin"))
442 : {
443 1 : logp("problem reading in extra_comms\n");
444 1 : goto error;
445 : }
446 : // Want to tell the clients the extra comms features that are
447 : // supported, so that new clients are more likely to work with old
448 : // servers.
449 36 : if(vers.cli==vers.feat_list)
450 : {
451 : // 1.3.0 did not support the feature list.
452 1 : if(asfd->write_str(asfd, CMD_GEN, "extra_comms_begin ok"))
453 : {
454 1 : logp("problem writing in extra_comms\n");
455 1 : goto error;
456 : }
457 : }
458 : else
459 : {
460 35 : if(send_features(asfd, cconfs, &vers))
461 : goto error;
462 : }
463 :
464 34 : if(extra_comms_read(as, &vers, srestore, incexc, confs, cconfs))
465 : goto error;
466 :
467 24 : peer_version=get_string(cconfs[OPT_PEER_VERSION]);
468 :
469 : // This needs to come after extra_comms_read, as the client might
470 : // have set PROTO_1 or PROTO_2.
471 24 : switch(get_protocol(cconfs))
472 : {
473 : case PROTO_AUTO:
474 : // The protocol has not been specified. Make a choice.
475 14 : if(vers.cli<vers.burp2)
476 : {
477 : // Client is burp-1.x.x, use protocol1.
478 1 : set_protocol(confs, PROTO_1);
479 1 : set_protocol(cconfs, PROTO_1);
480 1 : logp("Client is burp-%s - using protocol=%d\n",
481 : peer_version, PROTO_1);
482 : }
483 : else
484 : {
485 : // Client is burp-2.x.x, use protocol2.
486 : // This will probably never be reached because
487 : // the negotiation will take care of it.
488 : /*
489 : set_protocol(confs, PROTO_2);
490 : set_protocol(cconfs, PROTO_2);
491 : logp("Client is burp-%s - using protocol=%d\n",
492 : peer_version, PROTO_2);
493 : */
494 : // PROTO_1 is safer for now.
495 13 : set_protocol(confs, PROTO_1);
496 13 : set_protocol(cconfs, PROTO_1);
497 13 : logp("Client is burp-%s - using protocol=%d\n",
498 : peer_version, PROTO_1);
499 : }
500 : break;
501 : case PROTO_1:
502 : // It is OK for the client to be burp1 and for the
503 : // server to be forced to protocol1.
504 : break;
505 : case PROTO_2:
506 5 : if(vers.cli>=vers.burp2)
507 : break;
508 2 : logp("protocol=%d is set server side, "
509 : "but client is burp version %s\n",
510 : PROTO_2, peer_version);
511 2 : goto error;
512 : }
513 :
514 22 : if(get_protocol(cconfs)==PROTO_1)
515 : {
516 19 : if(get_e_rshash(cconfs[OPT_RSHASH])==RSHASH_UNSET)
517 : {
518 19 : set_e_rshash(confs[OPT_RSHASH], RSHASH_MD4);
519 19 : set_e_rshash(cconfs[OPT_RSHASH], RSHASH_MD4);
520 : }
521 : }
522 :
523 : return 0;
524 : error:
525 : return -1;
526 : }
|