/
dir_cmd.c
2211 lines (1948 loc) · 61.9 KB
/
dir_cmd.c
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2011 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.
*/
/*
* This file handles accepting Director Commands
*
* Kern Sibbald, October MM
*/
#include "bareos.h"
#include "filed.h"
#include "ch.h"
#if defined(WIN32_VSS)
#include "vss.h"
static pthread_mutex_t vss_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool enable_vss = false;
#endif
extern bool backup_only_mode;
extern bool restore_only_mode;
/**
* As Windows saves ACLs as part of the standard backup stream
* we just pretend here that is has implicit acl support.
*/
#if defined(HAVE_ACL) || defined(HAVE_WIN32)
const bool have_acl = true;
#else
const bool have_acl = false;
#endif
#if defined(HAVE_XATTR)
const bool have_xattr = true;
#else
const bool have_xattr = false;
#endif
/* Imported functions */
extern bool accurate_cmd(JCR *jcr);
extern bool status_cmd(JCR *jcr);
extern bool qstatus_cmd(JCR *jcr);
extern "C" char *job_code_callback_filed(JCR *jcr, const char* param);
/* Forward referenced functions */
static bool backup_cmd(JCR *jcr);
static bool bootstrap_cmd(JCR *jcr);
static bool cancel_cmd(JCR *jcr);
static bool end_restore_cmd(JCR *jcr);
static bool estimate_cmd(JCR *jcr);
#ifdef DEVELOPER
static bool exit_cmd(JCR *jcr);
#endif
static bool fileset_cmd(JCR *jcr);
static bool job_cmd(JCR *jcr);
static bool level_cmd(JCR *jcr);
static bool pluginoptions_cmd(JCR *jcr);
static bool runafter_cmd(JCR *jcr);
static bool runbeforenow_cmd(JCR *jcr);
static bool runbefore_cmd(JCR *jcr);
static bool runscript_cmd(JCR *jcr);
static bool resolve_cmd(JCR *jcr);
static bool restore_object_cmd(JCR *jcr);
static bool restore_cmd(JCR *jcr);
static bool session_cmd(JCR *jcr);
static bool setauthorization_cmd(JCR *jcr);
static bool setbandwidth_cmd(JCR *jcr);
static bool setdebug_cmd(JCR *jcr);
static bool storage_cmd(JCR *jcr);
static bool sm_dump_cmd(JCR *jcr);
static bool verify_cmd(JCR *jcr);
static bool response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
static void filed_free_jcr(JCR *jcr);
static bool open_sd_read_session(JCR *jcr);
static void set_storage_auth_key(JCR *jcr, char *key);
/* Exported functions */
struct s_cmds {
const char *cmd;
bool (*func)(JCR *);
bool monitoraccess; /* specify if monitors have access to this function */
};
/**
* The following are the recognized commands from the Director.
*
* Keywords are sorted first longest match when the keywords start with the same string.
*/
static struct s_cmds cmds[] = {
{ "accurate", accurate_cmd, false },
{ "backup", backup_cmd, false },
{ "bootstrap", bootstrap_cmd, false },
{ "cancel", cancel_cmd, false },
{ "endrestore", end_restore_cmd, false },
{ "estimate", estimate_cmd, false },
#ifdef DEVELOPER
{ "exit", exit_cmd, false },
#endif
{ "fileset", fileset_cmd, false },
{ "JobId=", job_cmd, false },
{ "level = ", level_cmd, false },
{ "pluginoptions", pluginoptions_cmd, false },
{ "RunAfterJob", runafter_cmd, false },
{ "RunBeforeNow", runbeforenow_cmd, false },
{ "RunBeforeJob", runbefore_cmd, false },
{ "Run", runscript_cmd, false },
{ "restoreobject", restore_object_cmd, false },
{ "restore ", restore_cmd, false },
{ "resolve ", resolve_cmd, false },
{ "session", session_cmd, false },
{ "setauthorization", setauthorization_cmd, false },
{ "setbandwidth=", setbandwidth_cmd, false },
{ "setdebug=", setdebug_cmd, false },
{ "status", status_cmd, true },
{ ".status", qstatus_cmd, true },
{ "storage ", storage_cmd, false },
{ "sm_dump", sm_dump_cmd, false },
{ "verify", verify_cmd, false },
{ NULL, NULL, false } /* list terminator */
};
/*
* Commands received from director that need scanning
*/
static char setauthorizationcmd[] =
"setauthorization Authorization=%100s";
static char setbandwidthcmd[] =
"setbandwidth=%lld Job=%127s";
static char setdebugcmd[] =
"setdebug=%d trace=%d hangup=%d";
static char jobcmd[] =
"JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
static char storaddrv0cmd[] =
"storage address=%s port=%d ssl=%d";
static char storaddrv1cmd[] =
"storage address=%s port=%d ssl=%d Authorization=%100s";
static char sessioncmd[] =
"session %127s %ld %ld %ld %ld %ld %ld\n";
static char restorecmd[] =
"restore replace=%c prelinks=%d where=%s\n";
static char restorecmd1[] =
"restore replace=%c prelinks=%d where=\n";
static char restorecmdR[] =
"restore replace=%c prelinks=%d regexwhere=%s\n";
static char restoreobjcmd[] =
"restoreobject JobId=%u %d,%d,%d,%d,%d,%d,%s";
static char restoreobjcmd1[] =
"restoreobject JobId=%u %d,%d,%d,%d,%d,%d\n";
static char endrestoreobjectcmd[] =
"restoreobject end\n";
static char pluginoptionscmd[] =
"pluginoptions %s";
static char verifycmd[] =
"verify level=%30s";
static char estimatecmd[] =
"estimate listing=%d";
static char runbeforecmd[] =
"RunBeforeJob %s";
static char runaftercmd[] =
"RunAfterJob %s";
static char runscriptcmd[] =
"Run OnSuccess=%d OnFailure=%d AbortOnError=%d When=%d Command=%s";
static char resolvecmd[] =
"resolve %s";
/*
* Responses sent to Director
*/
static char errmsg[] =
"2999 Invalid command\n";
static char invalid_cmd[] =
"2997 Invalid command for a Director with Monitor directive enabled.\n";
static char OkAuthorization[] =
"2000 OK Authorization\n";
static char OKBandwidth[] =
"2000 OK Bandwidth\n";
static char OKinc[] =
"2000 OK include\n";
static char OKest[] =
"2000 OK estimate files=%s bytes=%s\n";
static char OKlevel[] =
"2000 OK level\n";
static char OKbackup[] =
"2000 OK backup\n";
static char OKbootstrap[] =
"2000 OK bootstrap\n";
static char OKverify[] =
"2000 OK verify\n";
static char OKrestore[] =
"2000 OK restore\n";
static char OKsession[] =
"2000 OK session\n";
static char OKstore[] =
"2000 OK storage\n";
static char OKstoreend[] =
"2000 OK storage end\n";
static char OKjob[] =
"2000 OK Job %s (%s) %s,%s,%s";
static char OKsetdebug[] =
"2000 OK setdebug=%d trace=%d hangup=%d\n";
static char BADjob[] =
"2901 Bad Job\n";
static char EndJob[] =
"2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s"
" JobBytes=%s Errors=%u VSS=%d Encrypt=%d\n";
static char OKRunBefore[] =
"2000 OK RunBefore\n";
static char OKRunBeforeNow[] =
"2000 OK RunBeforeNow\n";
static char OKRunAfter[] =
"2000 OK RunAfter\n";
static char OKRunScript[] =
"2000 OK RunScript\n";
static char BadRunBeforeJob[] =
"2905 Bad RunBeforeJob command.\n";
static char FailedRunScript[] =
"2905 Failed RunScript\n";
static char BadRunAfterJob[] =
"2905 Bad RunAfterJob command.\n";
static char BADcmd[] =
"2902 Bad %s\n";
static char OKRestoreObject[] =
"2000 OK ObjectRestored\n";
static char OKPluginOptions[] =
"2000 OK PluginOptions\n";
static char BadPluginOptions[] =
"2905 Bad PluginOptions command.\n";
/*
* Responses received from Storage Daemon
*/
static char OK_end[] =
"3000 OK end\n";
static char OK_close[] =
"3000 OK close Status = %d\n";
static char OK_open[] =
"3000 OK open ticket = %d\n";
static char OK_data[] =
"3000 OK data\n";
static char OK_append[] =
"3000 OK append data\n";
/*
* Commands sent to Storage Daemon
*/
static char append_open[] =
"append open session\n";
static char append_data[] =
"append data %d\n";
static char append_end[] =
"append end session %d\n";
static char append_close[] =
"append close session %d\n";
static char read_open[] =
"read open session = %s %ld %ld %ld %ld %ld %ld\n";
static char read_data[] =
"read data %d\n";
static char read_close[] =
"read close session %d\n";
/*
* See if we are allowed to execute the command issued.
*/
static bool validate_command(JCR *jcr, const char *cmd, alist *allowed_job_cmds)
{
char *allowed_job_cmd;
bool allowed = false;
/*
* If there is no explicit list of allowed cmds allow all cmds.
*/
if (!allowed_job_cmds) {
return true;
}
foreach_alist(allowed_job_cmd, allowed_job_cmds) {
if (bstrcasecmp(cmd, allowed_job_cmd)) {
allowed = true;
break;
}
}
if (!allowed) {
Jmsg(jcr, M_FATAL, 0, _("Illegal \"%s\" command not allowed by Allowed Job Cmds setting of this filed.\n"), cmd);
}
return allowed;
}
static inline void cleanup_fileset(JCR *jcr)
{
findFILESET *fileset;
findINCEXE *incexe;
findFOPTS *fo;
fileset = jcr->ff->fileset;
if (fileset) {
/*
* Delete FileSet Include lists
*/
for (int i = 0; i < fileset->include_list.size(); i++) {
incexe = (findINCEXE *)fileset->include_list.get(i);
for (int j = 0; j < incexe->opts_list.size(); j++) {
fo = (findFOPTS *)incexe->opts_list.get(j);
if (fo->plugin) {
free(fo->plugin);
}
for (int k = 0; k<fo->regex.size(); k++) {
regfree((regex_t *)fo->regex.get(k));
}
for (int k = 0; k<fo->regexdir.size(); k++) {
regfree((regex_t *)fo->regexdir.get(k));
}
for (int k = 0; k<fo->regexfile.size(); k++) {
regfree((regex_t *)fo->regexfile.get(k));
}
if (fo->size_match) {
free(fo->size_match);
}
fo->regex.destroy();
fo->regexdir.destroy();
fo->regexfile.destroy();
fo->wild.destroy();
fo->wilddir.destroy();
fo->wildfile.destroy();
fo->wildbase.destroy();
fo->base.destroy();
fo->fstype.destroy();
fo->drivetype.destroy();
}
incexe->opts_list.destroy();
incexe->name_list.destroy();
incexe->plugin_list.destroy();
incexe->ignoredir.destroy();
}
fileset->include_list.destroy();
/*
* Delete FileSet Exclude lists
*/
for (int i = 0; i<fileset->exclude_list.size(); i++) {
incexe = (findINCEXE *)fileset->exclude_list.get(i);
for (int j = 0; j<incexe->opts_list.size(); j++) {
fo = (findFOPTS *)incexe->opts_list.get(j);
if (fo->size_match) {
free(fo->size_match);
}
fo->regex.destroy();
fo->regexdir.destroy();
fo->regexfile.destroy();
fo->wild.destroy();
fo->wilddir.destroy();
fo->wildfile.destroy();
fo->wildbase.destroy();
fo->base.destroy();
fo->fstype.destroy();
fo->drivetype.destroy();
}
incexe->opts_list.destroy();
incexe->name_list.destroy();
incexe->plugin_list.destroy();
incexe->ignoredir.destroy();
}
fileset->exclude_list.destroy();
free(fileset);
}
jcr->ff->fileset = NULL;
}
/*
* Connection request from an director.
*
* Accept commands one at a time from the Director and execute them.
*
* Concerning ClientRunBefore/After, the sequence of events
* is rather critical. If they are not done in the right
* order one can easily get FD->SD timeouts if the script
* runs a long time.
*
* The current sequence of events is:
* 1. Dir starts job with FD
* 2. Dir connects to SD
* 3. Dir connects to FD
* 4. FD connects to SD
* 5. FD gets/runs ClientRunBeforeJob and sends ClientRunAfterJob
* 6. Dir sends include/exclude
* 7. FD sends data to SD
* 8. SD/FD disconnects while SD despools data and attributes (optional)
* 9. FD runs ClientRunAfterJob
*/
static void *handle_director_connection(BSOCK *dir)
{
JCR *jcr;
bool found;
bool quit = false;
const char jobname[12] = "*Director*";
// saveCWD save_cwd;
#ifdef HAVE_WIN32
prevent_os_suspensions();
#endif
jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
jcr->dir_bsock = dir;
jcr->ff = init_find_files();
// save_cwd.save(jcr);
jcr->start_time = time(NULL);
jcr->RunScripts = New(alist(10, not_owned_by_alist));
jcr->last_fname = get_pool_memory(PM_FNAME);
jcr->last_fname[0] = 0;
jcr->client_name = get_memory(strlen(my_name) + 1);
pm_strcpy(jcr->client_name, my_name);
bstrncpy(jcr->Job, jobname, sizeof(jobname)); /* dummy */
jcr->crypto.pki_sign = me->pki_sign;
jcr->crypto.pki_encrypt = me->pki_encrypt;
jcr->crypto.pki_keypair = me->pki_keypair;
jcr->crypto.pki_signers = me->pki_signers;
jcr->crypto.pki_recipients = me->pki_recipients;
dir->set_jcr(jcr);
enable_backup_privileges(NULL, 1 /* ignore_errors */);
Dmsg0(120, "Calling Authenticate\n");
if (!authenticate_director(jcr)) {
return NULL;
}
Dmsg0(120, "OK Authenticate\n");
jcr->authenticated = true;
/**********FIXME******* add command handler error code */
while (!quit) {
/*
* Read command
*/
if (dir->recv() < 0) {
break; /* connection terminated */
}
dir->msg[dir->msglen] = 0;
Dmsg1(100, "<dird: %s", dir->msg);
found = false;
for (int i = 0; cmds[i].cmd; i++) {
if (bstrncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd))) {
found = true; /* indicate command found */
if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
dir->fsend(invalid_cmd);
dir->signal(BNET_EOD);
break;
}
Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
if (!cmds[i].func(jcr)) { /* do command */
quit = true; /* error or fully terminated, get out */
Dmsg1(100, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
}
break;
}
}
if (!found) { /* command not found */
dir->fsend(errmsg);
quit = true;
break;
}
}
/*
* Inform Storage daemon that we are done
*/
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
}
/*
* Run the after job
*/
if (jcr->RunScripts) {
run_scripts(jcr, jcr->RunScripts, "ClientAfterJob",
(jcr->director && jcr->director->allowed_script_dirs) ?
jcr->director->allowed_script_dirs :
me->allowed_script_dirs);
}
if (jcr->JobId) { /* send EndJob if running a job */
char ed1[50], ed2[50];
/* Send termination status back to Dir */
dir->fsend(EndJob, jcr->JobStatus, jcr->JobFiles,
edit_uint64(jcr->ReadBytes, ed1),
edit_uint64(jcr->JobBytes, ed2), jcr->JobErrors, jcr->VSS,
jcr->crypto.pki_encrypt);
Dmsg1(110, "End FD msg: %s\n", dir->msg);
}
generate_plugin_event(jcr, bEventJobEnd);
dequeue_messages(jcr); /* send any queued messages */
/*
* Inform Director that we are done
*/
dir->signal(BNET_TERMINATE);
free_plugins(jcr); /* release instantiated plugins */
free_and_null_pool_memory(jcr->job_metadata);
/*
* Clean up fileset
*/
cleanup_fileset(jcr);
Dmsg0(100, "Calling term_find_files\n");
term_find_files(jcr->ff);
// save_cwd.restore(jcr);
// save_cwd.release();
jcr->ff = NULL;
Dmsg0(100, "Done with term_find_files\n");
free_jcr(jcr); /* destroy JCR record */
Dmsg0(100, "Done with free_jcr\n");
Dsm_check(100);
garbage_collect_memory_pool();
#ifdef HAVE_WIN32
allow_os_suspensions();
#endif
return NULL;
}
/*
* Connection request. We accept connections either from the Director or the Storage Daemon
*
* NOTE! We are running as a separate thread
*
* Send output one line at a time followed by a zero length transmission.
* Return when the connection is terminated or there is an error.
*
* Basic tasks done here:
* - If it was a connection from an SD, call handle_stored_connection()
* - Otherwise it was a connection from the DIR, call handle_director_connection()
*/
void *handle_connection_request(void *arg)
{
BSOCK *bs = (BSOCK *)arg;
char tbuf[100];
if (bs->recv() <= 0) {
Emsg1(M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->close();
delete bs;
return NULL;
}
Dmsg1(110, "Conn: %s", bs->msg);
/*
* See if its a director making a connection.
*/
if (bstrncmp(bs->msg, "Hello Director", 14)) {
Dmsg1(110, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return handle_director_connection(bs);
}
/*
* See if its a storage daemon making a connection.
*/
if (bstrncmp(bs->msg, "Hello Storage", 13)) {
Dmsg1(110, "Got a SD connection at %s\n", bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return handle_stored_connection(bs);
}
Emsg2(M_ERROR, 0, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->msglen);
return NULL;
}
static bool sm_dump_cmd(JCR *jcr)
{
close_memory_pool();
sm_dump(false, true);
jcr->dir_bsock->fsend("2000 sm_dump OK\n");
return true;
}
/*
* Resolve a hostname
*/
static bool resolve_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
dlist *addr_list;
const char *errstr;
char addresses[2048];
char hostname[2048];
sscanf(dir->msg, resolvecmd, &hostname);
if ((addr_list = bnet_host2ipaddrs(hostname, 0, &errstr)) == NULL) {
dir->fsend(_("%s: Failed to resolve %s\n"), my_name, hostname);
goto bail_out;
}
dir->fsend(_("%s resolves %s to %s\n"),my_name, hostname,
build_addresses_str(addr_list, addresses, sizeof(addresses), false));
free_addresses(addr_list);
bail_out:
dir->signal(BNET_EOD);
return true;
}
#ifdef DEVELOPER
static bool exit_cmd(JCR *jcr)
{
jcr->dir_bsock->fsend("2000 exit OK\n");
terminate_filed(0);
return false;
}
#endif
/**
* Cancel a Job
*/
static bool cancel_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
char Job[MAX_NAME_LENGTH];
JCR *cjcr;
if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
if (!(cjcr=get_jcr_by_full_name(Job))) {
dir->fsend(_("2901 Job %s not found.\n"), Job);
} else {
generate_plugin_event(cjcr, bEventCancelCommand, NULL);
cjcr->setJobStatus(JS_Canceled);
if (cjcr->store_bsock) {
cjcr->store_bsock->set_timed_out();
cjcr->store_bsock->set_terminated();
cjcr->my_thread_send_signal(TIMEOUT_SIGNAL);
}
free_jcr(cjcr);
dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
}
} else {
dir->fsend(_("2902 Error scanning cancel command.\n"));
}
dir->signal(BNET_EOD);
return true;
}
/**
* Set new authorization key as requested by the Director
*/
static bool setauthorization_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
POOL_MEM sd_auth_key(PM_MESSAGE);
sd_auth_key.check_size(dir->msglen);
if (sscanf(dir->msg, setauthorizationcmd, sd_auth_key.c_str()) != 1) {
dir->fsend(BADcmd, "setauthorization");
return false;
}
set_storage_auth_key(jcr, sd_auth_key.c_str());
Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
return dir->fsend(OkAuthorization);
}
/**
* Set bandwidth limit as requested by the Director
*/
static bool setbandwidth_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
int64_t bw = 0;
JCR *cjcr;
char Job[MAX_NAME_LENGTH];
*Job = 0;
if (sscanf(dir->msg, setbandwidthcmd, &bw, Job) != 2 || bw < 0) {
pm_strcpy(jcr->errmsg, dir->msg);
dir->fsend(_("2991 Bad setbandwidth command: %s\n"), jcr->errmsg);
return false;
}
if (*Job) {
if (!(cjcr = get_jcr_by_full_name(Job))) {
dir->fsend(_("2901 Job %s not found.\n"), Job);
} else {
cjcr->max_bandwidth = bw;
if (cjcr->store_bsock) {
cjcr->store_bsock->set_bwlimit(bw);
if (me->allow_bw_bursting) {
cjcr->store_bsock->set_bwlimit_bursting();
}
}
free_jcr(cjcr);
}
} else { /* No job requested, apply globally */
me->max_bandwidth_per_job = bw; /* Overwrite directive */
}
return dir->fsend(OKBandwidth);
}
/**
* Set debug level as requested by the Director
*/
static bool setdebug_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
int32_t level, trace, hangup;
int scan;
Dmsg1(50, "setdebug_cmd: %s", dir->msg);
scan = sscanf(dir->msg, setdebugcmd, &level, &trace, &hangup);
if (scan != 3) {
Dmsg2(20, "sscanf failed: msg=%s scan=%d\n", dir->msg, scan);
if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace) != 2) {
pm_strcpy(jcr->errmsg, dir->msg);
dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
return false;
} else {
hangup = -1;
}
}
if (level >= 0) {
debug_level = level;
}
set_trace(trace);
set_hangup(hangup);
Dmsg3(50, "level=%d trace=%d hangup=%d\n", level, get_trace(), get_hangup());
return dir->fsend(OKsetdebug, level, get_trace(), get_hangup());
}
static bool estimate_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
char ed1[50], ed2[50];
/*
* See if we are allowed to run estimate cmds.
*/
if (!validate_command(jcr, "estimate",
(jcr->director && jcr->director->allowed_job_cmds) ?
jcr->director->allowed_job_cmds :
me->allowed_job_cmds)) {
dir->fsend(_("2992 Bad estimate command.\n"));
return 0;
}
if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
dir->fsend(_("2992 Bad estimate command.\n"));
return false;
}
make_estimate(jcr);
dir->fsend(OKest, edit_uint64_with_commas(jcr->num_files_examined, ed1),
edit_uint64_with_commas(jcr->JobBytes, ed2));
dir->signal(BNET_EOD);
return true;
}
/**
* Get JobId and Storage Daemon Authorization key from Director
*/
static bool job_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
POOL_MEM sd_auth_key(PM_MESSAGE);
sd_auth_key.check_size(dir->msglen);
if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
&jcr->VolSessionId, &jcr->VolSessionTime,
sd_auth_key.c_str()) != 5) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
dir->fsend(BADjob);
return false;
}
set_storage_auth_key(jcr, sd_auth_key.c_str());
Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
Mmsg(jcr->errmsg, "JobId=%d Job=%s", jcr->JobId, jcr->Job);
new_plugins(jcr); /* instantiate plugins for this jcr */
generate_plugin_event(jcr, bEventJobStart, (void *)jcr->errmsg);
#ifdef HAVE_WIN32
return dir->fsend(OKjob, VERSION, LSMDATE, win_os, DISTNAME, DISTVER);
#else
return dir->fsend(OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
#endif
}
static bool runbefore_cmd(JCR *jcr)
{
bool ok;
POOLMEM *cmd;
RUNSCRIPT *script;
BSOCK *dir = jcr->dir_bsock;
if (!me->compatible) {
dir->fsend(BadRunBeforeJob);
return false;
}
Dmsg1(100, "runbefore_cmd: %s", dir->msg);
cmd = get_memory(dir->msglen + 1);
if (sscanf(dir->msg, runbeforecmd, cmd) != 1) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
dir->fsend(BadRunBeforeJob);
free_memory(cmd);
return false;
}
unbash_spaces(cmd);
/*
* Run the command now
*/
script = new_runscript();
script->set_job_code_callback(job_code_callback_filed);
script->set_command(cmd);
script->when = SCRIPT_Before;
free_memory(cmd);
ok = script->run(jcr, "ClientRunBeforeJob");
free_runscript(script);
if (ok) {
dir->fsend(OKRunBefore);
return true;
} else {
dir->fsend(BadRunBeforeJob);
return false;
}
}
static bool runbeforenow_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob",
(jcr->director && jcr->director->allowed_script_dirs) ?
jcr->director->allowed_script_dirs :
me->allowed_script_dirs);
if (job_canceled(jcr)) {
dir->fsend(FailedRunScript);
Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
return false;
} else {
dir->fsend(OKRunBeforeNow);
Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
return true;
}
}
static bool runafter_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
POOLMEM *cmd;
RUNSCRIPT *script;
if (!me->compatible) {
dir->fsend(BadRunAfterJob);
return false;
}
Dmsg1(100, "runafter_cmd: %s", dir->msg);
cmd = get_memory(dir->msglen + 1);
if (sscanf(dir->msg, runaftercmd, cmd) != 1) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
dir->fsend(BadRunAfterJob);
free_memory(cmd);
return false;
}
unbash_spaces(cmd);
script = new_runscript();
script->set_job_code_callback(job_code_callback_filed);
script->set_command(cmd);
script->on_success = true;
script->on_failure = false;
script->when = SCRIPT_After;
free_memory(cmd);
jcr->RunScripts->append(script);
return dir->fsend(OKRunAfter);
}
static bool runscript_cmd(JCR *jcr)
{
POOLMEM *msg;
RUNSCRIPT *cmd;
BSOCK *dir = jcr->dir_bsock;
int on_success, on_failure, fail_on_error;
/*
* See if we are allowed to run runscript cmds.
*/
if (!validate_command(jcr, "runscript",
(jcr->director && jcr->director->allowed_job_cmds) ?
jcr->director->allowed_job_cmds :
me->allowed_job_cmds)) {
dir->fsend(FailedRunScript);
return 0;
}
msg = get_memory(dir->msglen + 1);
cmd = new_runscript();
cmd->set_job_code_callback(job_code_callback_filed);
Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
/*
* Note, we cannot sscanf into bools
*/
if (sscanf(dir->msg, runscriptcmd, &on_success, &on_failure,
&fail_on_error, &cmd->when, msg) != 5) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
dir->fsend(FailedRunScript);
free_runscript(cmd);
free_memory(msg);
return false;
}
cmd->on_success = on_success;
cmd->on_failure = on_failure;
cmd->fail_on_error = fail_on_error;
unbash_spaces(msg);
cmd->set_command(msg);
cmd->debug();
jcr->RunScripts->append(cmd);
free_pool_memory(msg);
return dir->fsend(OKRunScript);
}
/*
* This passes plugin specific options.
*/
static bool pluginoptions_cmd(JCR *jcr)
{
BSOCK *dir = jcr->dir_bsock;
POOLMEM *msg;
msg = get_memory(dir->msglen + 1);
if (sscanf(dir->msg, pluginoptionscmd, msg) != 1) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg1(jcr, M_FATAL, 0, _("Bad Plugin Options command: %s\n"), jcr->errmsg);
dir->fsend(BadPluginOptions);
free_memory(msg);
return false;
}
unbash_spaces(msg);
generate_plugin_event(jcr, bEventNewPluginOptions, (void *)msg);
free_memory(msg);
return dir->fsend(OKPluginOptions);
}
/*
* This reads data sent from the Director from the
* RestoreObject table that allows us to get objects
* that were backed up (VSS .xml data) and are needed
* before starting the restore.
*/