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 : #ifdef HAVE_BLAKE2
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 2 : static int do_autoupgrade(struct asfd *asfd, struct vers *vers,
192 : struct conf **globalcs)
193 : {
194 2 : int ret=-1;
195 2 : char *os=NULL;
196 2 : struct iobuf *rbuf=asfd->rbuf;
197 2 : const char *autoupgrade_dir=get_string(globalcs[OPT_AUTOUPGRADE_DIR]);
198 :
199 2 : if(!(os=strdup_w(rbuf->buf+strlen("autoupgrade:"), __func__)))
200 : goto end;
201 2 : iobuf_free_content(rbuf);
202 2 : ret=0;
203 2 : if(os && *os)
204 : {
205 : // Sanitise path separators
206 7 : for(char *i=os; *i; ++i)
207 7 : if(*i == '/' || *i == '\\' || *i == ':')
208 0 : *i='-';
209 :
210 1 : ret=autoupgrade_server(asfd, vers->ser,
211 : vers->cli, os, get_cntr(globalcs),
212 : autoupgrade_dir);
213 : }
214 : end:
215 2 : free_w(&os);
216 2 : return ret;
217 : }
218 :
219 34 : static int extra_comms_read(struct async *as,
220 : struct vers *vers, int *srestore,
221 : char **incexc, struct conf **globalcs, struct conf **cconfs)
222 : {
223 34 : int ret=-1;
224 : struct asfd *asfd;
225 : struct iobuf *rbuf;
226 34 : asfd=as->asfd;
227 34 : rbuf=asfd->rbuf;
228 :
229 : while(1)
230 : {
231 54 : iobuf_free_content(rbuf);
232 54 : if(asfd->read(asfd)) goto end;
233 :
234 54 : if(rbuf->cmd!=CMD_GEN)
235 : {
236 1 : iobuf_log_unexpected(rbuf, __func__);
237 : goto end;
238 : }
239 :
240 53 : if(!strcmp(rbuf->buf, "extra_comms_end"))
241 : {
242 24 : if(asfd->write_str(asfd, CMD_GEN, "extra_comms_end ok"))
243 : goto end;
244 : break;
245 : }
246 29 : else if(!strncmp_w(rbuf->buf, "autoupgrade:"))
247 : {
248 2 : if(do_autoupgrade(asfd, vers, globalcs))
249 : goto end;
250 : }
251 27 : else if(!strcmp(rbuf->buf, "srestore ok"))
252 : {
253 4 : char *restore_path=get_string(cconfs[OPT_RESTORE_PATH]);
254 4 : if(!restore_path)
255 : {
256 0 : logp("got srestore ok without a restore_path");
257 : goto end;
258 : }
259 :
260 4 : iobuf_free_content(rbuf);
261 : // Client can accept the restore.
262 : // Load the restore config, then send it.
263 4 : *srestore=1;
264 : // Need to wipe out OPT_INCEXDIR, as it is needed for
265 : // srestore includes. If it is not wiped out, it can
266 : // interfere if cconfs[OPT_RESTORE_PATH] contained no
267 : // includes.
268 4 : set_strlist(cconfs[OPT_INCEXCDIR], NULL);
269 4 : if(conf_parse_incexcs_path(cconfs, restore_path)
270 4 : || incexc_send_server_restore(asfd, cconfs))
271 : goto end;
272 : // Do not unlink it here - wait until
273 : // the client says that it wants to do the
274 : // restore.
275 : // Also need to leave it around if the
276 : // restore is to an alternative client, so
277 : // that the code below that reloads the config
278 : // can read it again.
279 : // NOTE: that appears to be in
280 : // src/server/run_action.c::client_can_restore()
281 : //unlink(get_string(cconfs[OPT_RESTORE_PATH]));
282 : }
283 23 : else if(!strcmp(rbuf->buf, "srestore not ok"))
284 : {
285 1 : const char *restore_path=get_string(
286 : cconfs[OPT_RESTORE_PATH]);
287 : // Client will not accept the restore.
288 1 : if (restore_path)
289 1 : unlink(restore_path);
290 1 : if(set_string(cconfs[OPT_RESTORE_PATH], NULL))
291 : goto end;
292 1 : logp("Client not accepting server initiated restore.\n");
293 : }
294 22 : else if(!strcmp(rbuf->buf, "sincexc ok"))
295 : {
296 : // Client can accept incexc conf from the
297 : // server.
298 1 : iobuf_free_content(rbuf);
299 1 : if(incexc_send_server(asfd, cconfs))
300 : goto end;
301 : }
302 21 : else if(!strcmp(rbuf->buf, "incexc"))
303 : {
304 : // Client is telling server its incexc
305 : // configuration so that it can better decide
306 : // what to do on resume.
307 1 : iobuf_free_content(rbuf);
308 1 : if(incexc_recv_server(asfd, incexc, globalcs))
309 : goto end;
310 1 : if(*incexc)
311 : {
312 1 : char *tmp=NULL;
313 1 : char comp[32]="";
314 1 : snprintf(comp, sizeof(comp),
315 : "compression = %d\n",
316 : get_int(cconfs[OPT_COMPRESSION]));
317 1 : if(!(tmp=prepend(*incexc, comp)))
318 : goto end;
319 1 : free_w(incexc);
320 1 : *incexc=tmp;
321 : }
322 : }
323 20 : else if(!strcmp(rbuf->buf, "counters_json ok"))
324 : {
325 : // Client can accept counters on
326 : // resume/verify/restore.
327 1 : logp("Client supports being sent json counters.\n");
328 1 : set_int(cconfs[OPT_SEND_CLIENT_CNTR], 1);
329 : }
330 19 : else if(!strncmp_w(rbuf->buf, "uname=")
331 2 : && strlen(rbuf->buf)>strlen("uname="))
332 : {
333 2 : char *uname=rbuf->buf+strlen("uname=");
334 2 : if(!strncasecmp("Windows", uname, strlen("Windows")))
335 1 : set_int(cconfs[OPT_CLIENT_IS_WINDOWS], 1);
336 : }
337 17 : else if(!strncmp_w(rbuf->buf, "orig_client=")
338 3 : && strlen(rbuf->buf)>strlen("orig_client="))
339 : {
340 3 : if(conf_switch_to_orig_client(globalcs, cconfs,
341 3 : rbuf->buf+strlen("orig_client=")))
342 : goto end;
343 : // If this started out as a server-initiated
344 : // restore, need to load the restore file
345 : // again.
346 2 : if(*srestore)
347 : {
348 1 : if(conf_parse_incexcs_path(cconfs,
349 1 : get_string(cconfs[OPT_RESTORE_PATH])))
350 : goto end;
351 : }
352 2 : if(asfd->write_str(asfd, CMD_GEN, "orig_client ok"))
353 : goto end;
354 : }
355 14 : else if(!strncmp_w(rbuf->buf, "restore_spool="))
356 : {
357 : // Removed.
358 : }
359 14 : else if(!strncmp_w(rbuf->buf, "protocol="))
360 : {
361 11 : char msg[128]="";
362 : // Client wants to set protocol.
363 : enum protocol protocol;
364 : enum protocol cprotocol;
365 11 : const char *cliproto=NULL;
366 11 : protocol=get_protocol(cconfs);
367 11 : cliproto=rbuf->buf+strlen("protocol=");
368 11 : cprotocol=atoi(cliproto);
369 :
370 11 : if(protocol!=PROTO_AUTO)
371 : {
372 6 : if(protocol==cprotocol)
373 : {
374 2 : logp("Client is forcing protocol=%d\n", (int)protocol);
375 2 : continue;
376 : }
377 4 : snprintf(msg, sizeof(msg), "Client is trying to use protocol=%d but server is set to protocol=%d\n", (int)cprotocol, (int)protocol);
378 4 : log_and_send(asfd, msg);
379 5 : goto end;
380 : }
381 5 : else if(cprotocol==PROTO_1)
382 : {
383 2 : set_protocol(cconfs, cprotocol);
384 2 : set_protocol(globalcs, cprotocol);
385 : }
386 3 : else if(cprotocol==PROTO_2)
387 : {
388 2 : set_protocol(cconfs, cprotocol);
389 2 : set_protocol(globalcs, cprotocol);
390 : }
391 : else
392 : {
393 1 : snprintf(msg, sizeof(msg), "Client is trying to use protocol=%s, which is unknown\n", cliproto);
394 1 : log_and_send(asfd, msg);
395 : goto end;
396 : }
397 4 : logp("Client has set protocol=%d\n",
398 4 : (int)get_protocol(cconfs));
399 : }
400 3 : else if(!strncmp_w(rbuf->buf, "rshash=blake2"))
401 : {
402 : #ifdef HAVE_BLAKE2
403 : set_e_rshash(cconfs[OPT_RSHASH], RSHASH_BLAKE2);
404 : set_e_rshash(globalcs[OPT_RSHASH], RSHASH_BLAKE2);
405 : #else
406 1 : logp("Client is trying to use librsync hash blake2, but server does not support it.\n");
407 : goto end;
408 : #endif
409 : }
410 2 : else if(!strncmp_w(rbuf->buf, "msg"))
411 : {
412 1 : set_int(cconfs[OPT_MESSAGE], 1);
413 1 : set_int(globalcs[OPT_MESSAGE], 1);
414 : }
415 : else
416 : {
417 1 : iobuf_log_unexpected(rbuf, __func__);
418 : goto end;
419 : }
420 : }
421 :
422 24 : ret=0;
423 : end:
424 34 : iobuf_free_content(rbuf);
425 34 : return ret;
426 : }
427 :
428 39 : static int vers_init(struct vers *vers, struct conf **cconfs)
429 : {
430 39 : memset(vers, 0, sizeof(struct vers));
431 39 : return ((vers->min=version_to_long("1.2.7"))<0
432 39 : || (vers->cli=version_to_long(get_string(cconfs[OPT_PEER_VERSION])))<0
433 39 : || (vers->ser=version_to_long(PACKAGE_VERSION))<0
434 39 : || (vers->feat_list=version_to_long("1.3.0"))<0
435 39 : || (vers->directory_tree=version_to_long("1.3.6"))<0
436 39 : || (vers->burp2=version_to_long("2.0.0"))<0
437 78 : || (vers->counters_json=version_to_long("2.0.46"))<0);
438 : }
439 :
440 39 : int extra_comms(struct async *as,
441 : char **incexc, int *srestore, struct conf **confs, struct conf **cconfs)
442 : {
443 : struct vers vers;
444 : struct asfd *asfd;
445 39 : asfd=as->asfd;
446 : //char *restorepath=NULL;
447 39 : const char *peer_version=NULL;
448 :
449 39 : if(vers_init(&vers, cconfs))
450 : goto error;
451 :
452 39 : if(vers.cli<vers.directory_tree)
453 : {
454 3 : set_int(confs[OPT_DIRECTORY_TREE], 0);
455 3 : set_int(cconfs[OPT_DIRECTORY_TREE], 0);
456 : }
457 :
458 : // Clients before 1.2.7 did not know how to do extra comms, so skip
459 : // this section for them.
460 39 : if(vers.cli<vers.min)
461 : return 0;
462 :
463 37 : if(asfd_read_expect(asfd, CMD_GEN, "extra_comms_begin"))
464 : {
465 1 : logp("problem reading in extra_comms\n");
466 1 : goto error;
467 : }
468 : // Want to tell the clients the extra comms features that are
469 : // supported, so that new clients are more likely to work with old
470 : // servers.
471 36 : if(vers.cli==vers.feat_list)
472 : {
473 : // 1.3.0 did not support the feature list.
474 1 : if(asfd->write_str(asfd, CMD_GEN, "extra_comms_begin ok"))
475 : {
476 1 : logp("problem writing in extra_comms\n");
477 1 : goto error;
478 : }
479 : }
480 : else
481 : {
482 35 : if(send_features(asfd, cconfs, &vers))
483 : goto error;
484 : }
485 :
486 34 : if(extra_comms_read(as, &vers, srestore, incexc, confs, cconfs))
487 : goto error;
488 :
489 24 : peer_version=get_string(cconfs[OPT_PEER_VERSION]);
490 :
491 : // This needs to come after extra_comms_read, as the client might
492 : // have set PROTO_1 or PROTO_2.
493 24 : switch(get_protocol(cconfs))
494 : {
495 : case PROTO_AUTO:
496 : // The protocol has not been specified. Make a choice.
497 14 : if(vers.cli<vers.burp2)
498 : {
499 : // Client is burp-1.x.x, use protocol1.
500 1 : set_protocol(confs, PROTO_1);
501 1 : set_protocol(cconfs, PROTO_1);
502 1 : logp("Client is %s-%s - using protocol=%d\n",
503 : PACKAGE_TARNAME,
504 : peer_version, PROTO_1);
505 : }
506 : else
507 : {
508 : // Client is burp-2.x.x, use protocol2.
509 : // This will probably never be reached because
510 : // the negotiation will take care of it.
511 : /*
512 : set_protocol(confs, PROTO_2);
513 : set_protocol(cconfs, PROTO_2);
514 : logp("Client is %s-%s - using protocol=%d\n",
515 : PACKAGE_TARNAME,
516 : peer_version, PROTO_2);
517 : */
518 : // PROTO_1 is safer for now.
519 13 : set_protocol(confs, PROTO_1);
520 13 : set_protocol(cconfs, PROTO_1);
521 13 : logp("Client is %s-%s - using protocol=%d\n",
522 : PACKAGE_TARNAME,
523 : peer_version, PROTO_1);
524 : }
525 : break;
526 : case PROTO_1:
527 : // It is OK for the client to be burp1 and for the
528 : // server to be forced to protocol1.
529 : break;
530 : case PROTO_2:
531 5 : if(vers.cli>=vers.burp2)
532 : break;
533 2 : logp("protocol=%d is set server side, "
534 : "but client is %s version %s\n",
535 : PROTO_2, PACKAGE_TARNAME, peer_version);
536 2 : goto error;
537 : }
538 :
539 22 : if(get_protocol(cconfs)==PROTO_1)
540 : {
541 19 : if(get_e_rshash(cconfs[OPT_RSHASH])==RSHASH_UNSET)
542 : {
543 19 : set_e_rshash(confs[OPT_RSHASH], RSHASH_MD4);
544 19 : set_e_rshash(cconfs[OPT_RSHASH], RSHASH_MD4);
545 : }
546 : }
547 :
548 : return 0;
549 : error:
550 : return -1;
551 : }
|