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