2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Bacula File Daemon Job processing
31 * Kern Sibbald, October MM
38 #if defined(WIN32_VSS)
41 static pthread_mutex_t vss_mutex = PTHREAD_MUTEX_INITIALIZER;
42 static int enable_vss = 0;
46 * As Windows saves ACLs as part of the standard backup stream
47 * we just pretend here that is has implicit acl support.
49 #if defined(HAVE_ACL) || defined(HAVE_WIN32)
50 const bool have_acl = true;
52 const bool have_acl = false;
55 #if defined(HAVE_XATTR)
56 const bool have_xattr = true;
58 const bool have_xattr = false;
61 extern CLIENT *me; /* our client resource */
63 /* Imported functions */
64 extern int status_cmd(JCR *jcr);
65 extern int qstatus_cmd(JCR *jcr);
66 extern int accurate_cmd(JCR *jcr);
68 /* Forward referenced functions */
69 static int backup_cmd(JCR *jcr);
70 static int bootstrap_cmd(JCR *jcr);
71 static int cancel_cmd(JCR *jcr);
72 static int setdebug_cmd(JCR *jcr);
73 static int estimate_cmd(JCR *jcr);
74 static int hello_cmd(JCR *jcr);
75 static int job_cmd(JCR *jcr);
76 static int fileset_cmd(JCR *jcr);
77 static int level_cmd(JCR *jcr);
78 static int verify_cmd(JCR *jcr);
79 static int restore_cmd(JCR *jcr);
80 static int end_restore_cmd(JCR *jcr);
81 static int storage_cmd(JCR *jcr);
82 static int session_cmd(JCR *jcr);
83 static int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
84 static void filed_free_jcr(JCR *jcr);
85 static int open_sd_read_session(JCR *jcr);
86 static int send_bootstrap_file(JCR *jcr);
87 static int runscript_cmd(JCR *jcr);
88 static int runbefore_cmd(JCR *jcr);
89 static int runafter_cmd(JCR *jcr);
90 static int runbeforenow_cmd(JCR *jcr);
91 static int restore_object_cmd(JCR *jcr);
92 static void set_options(findFOPTS *fo, const char *opts);
93 static void set_storage_auth_key(JCR *jcr, char *key);
95 /* Exported functions */
100 int monitoraccess; /* specify if monitors have access to this function */
104 * The following are the recognized commands from the Director.
106 static struct s_cmds cmds[] = {
107 {"backup", backup_cmd, 0},
108 {"cancel", cancel_cmd, 0},
109 {"setdebug=", setdebug_cmd, 0},
110 {"estimate", estimate_cmd, 0},
111 {"Hello", hello_cmd, 1},
112 {"fileset", fileset_cmd, 0},
113 {"JobId=", job_cmd, 0},
114 {"level = ", level_cmd, 0},
115 {"restore ", restore_cmd, 0},
116 {"endrestore", end_restore_cmd, 0},
117 {"session", session_cmd, 0},
118 {"status", status_cmd, 1},
119 {".status", qstatus_cmd, 1},
120 {"storage ", storage_cmd, 0},
121 {"verify", verify_cmd, 0},
122 {"bootstrap", bootstrap_cmd, 0},
123 {"RunBeforeNow", runbeforenow_cmd, 0},
124 {"RunBeforeJob", runbefore_cmd, 0},
125 {"RunAfterJob", runafter_cmd, 0},
126 {"Run", runscript_cmd, 0},
127 {"accurate", accurate_cmd, 0},
128 {"restoreobject", restore_object_cmd, 0},
129 {NULL, NULL} /* list terminator */
132 /* Commands received from director that need scanning */
133 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
134 static char storaddr[] = "storage address=%s port=%d ssl=%d Authorization=%100s";
135 static char storaddr_v1[] = "storage address=%s port=%d ssl=%d";
136 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
137 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
138 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
139 static char restorecmdR[] = "restore replace=%c prelinks=%d regexwhere=%s\n";
140 static char restoreobjcmd[] = "restoreobject JobId=%u ObjLen=%d ObjInx=%d ObjType=%d FI=%d\n";
141 static char endrestoreobjectcmd[] = "restoreobject end\n";
142 static char verifycmd[] = "verify level=%30s";
143 static char estimatecmd[] = "estimate listing=%d";
144 static char runbefore[] = "RunBeforeJob %s";
145 static char runafter[] = "RunAfterJob %s";
146 static char runscript[] = "Run OnSuccess=%d OnFailure=%d AbortOnError=%d When=%d Command=%s";
148 /* Responses sent to Director */
149 static char errmsg[] = "2999 Invalid command\n";
150 static char no_auth[] = "2998 No Authorization\n";
151 static char invalid_cmd[] = "2997 Invalid command for a Director with Monitor directive enabled.\n";
152 static char OKinc[] = "2000 OK include\n";
153 static char OKest[] = "2000 OK estimate files=%s bytes=%s\n";
154 static char OKlevel[] = "2000 OK level\n";
155 static char OKbackup[] = "2000 OK backup\n";
156 static char OKbootstrap[] = "2000 OK bootstrap\n";
157 static char OKverify[] = "2000 OK verify\n";
158 static char OKrestore[] = "2000 OK restore\n";
159 static char OKsession[] = "2000 OK session\n";
160 static char OKstore[] = "2000 OK storage\n";
161 static char OKstoreend[] = "2000 OK storage end\n";
162 static char OKjob[] = "2000 OK Job %s (%s) %s,%s,%s";
163 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
164 static char BADjob[] = "2901 Bad Job\n";
165 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s"
166 " JobBytes=%s Errors=%u VSS=%d Encrypt=%d\n";
167 static char OKRunBefore[] = "2000 OK RunBefore\n";
168 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
169 static char OKRunAfter[] = "2000 OK RunAfter\n";
170 static char OKRunScript[] = "2000 OK RunScript\n";
171 static char BADcmd[] = "2902 Bad %s\n";
172 static char OKRestoreObject[] = "2000 OK ObjectRestored\n";
175 /* Responses received from Storage Daemon */
176 static char OK_end[] = "3000 OK end\n";
177 static char OK_close[] = "3000 OK close Status = %d\n";
178 static char OK_open[] = "3000 OK open ticket = %d\n";
179 static char OK_data[] = "3000 OK data\n";
180 static char OK_append[] = "3000 OK append data\n";
181 static char OKSDbootstrap[]= "3000 OK bootstrap\n";
184 /* Commands sent to Storage Daemon */
185 static char append_open[] = "append open session\n";
186 static char append_data[] = "append data %d\n";
187 static char append_end[] = "append end session %d\n";
188 static char append_close[] = "append close session %d\n";
189 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
190 static char read_data[] = "read data %d\n";
191 static char read_close[] = "read close session %d\n";
194 * Accept requests from a Director
196 * NOTE! We are running as a separate thread
198 * Send output one line
199 * at a time followed by a zero length transmission.
201 * Return when the connection is terminated or there
204 * Basic task here is:
205 * Authenticate Director (during Hello command).
206 * Accept commands one at a time from the Director
209 * Concerning ClientRunBefore/After, the sequence of events
210 * is rather critical. If they are not done in the right
211 * order one can easily get FD->SD timeouts if the script
214 * The current sequence of events is:
215 * 1. Dir starts job with FD
216 * 2. Dir connects to SD
217 * 3. Dir connects to FD
218 * 4. FD connects to SD
219 * 5. FD gets/runs ClientRunBeforeJob and sends ClientRunAfterJob
220 * 6. Dir sends include/exclude
221 * 7. FD sends data to SD
222 * 8. SD/FD disconnects while SD despools data and attributes (optional)
223 * 9. FD runs ClientRunAfterJob
226 void *handle_client_request(void *dirp)
231 BSOCK *dir = (BSOCK *)dirp;
232 const char jobname[12] = "*Director*";
235 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
236 jcr->dir_bsock = dir;
237 jcr->ff = init_find_files();
238 // save_cwd.save(jcr);
239 jcr->start_time = time(NULL);
240 jcr->RunScripts = New(alist(10, not_owned_by_alist));
241 jcr->last_fname = get_pool_memory(PM_FNAME);
242 jcr->last_fname[0] = 0;
243 jcr->client_name = get_memory(strlen(my_name) + 1);
244 pm_strcpy(jcr->client_name, my_name);
245 bstrncpy(jcr->Job, jobname, sizeof(jobname)); /* dummy */
246 jcr->crypto.pki_sign = me->pki_sign;
247 jcr->crypto.pki_encrypt = me->pki_encrypt;
248 jcr->crypto.pki_keypair = me->pki_keypair;
249 jcr->crypto.pki_signers = me->pki_signers;
250 jcr->crypto.pki_recipients = me->pki_recipients;
252 enable_backup_privileges(NULL, 1 /* ignore_errors */);
254 /**********FIXME******* add command handler error code */
256 for (quit=false; !quit;) {
258 if (dir->recv() < 0) {
259 break; /* connection terminated */
261 dir->msg[dir->msglen] = 0;
262 Dmsg1(100, "<dird: %s", dir->msg);
264 for (i=0; cmds[i].cmd; i++) {
265 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
266 found = true; /* indicate command found */
267 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
269 dir->signal(BNET_EOD);
272 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
273 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
274 dir->fsend(invalid_cmd);
275 dir->signal(BNET_EOD);
278 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
279 if (!cmds[i].func(jcr)) { /* do command */
280 quit = true; /* error or fully terminated, get out */
281 Dmsg1(100, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
286 if (!found) { /* command not found */
293 /* Inform Storage daemon that we are done */
294 if (jcr->store_bsock) {
295 jcr->store_bsock->signal(BNET_TERMINATE);
298 /* Run the after job */
299 run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
301 if (jcr->JobId) { /* send EndJob if running a job */
302 char ed1[50], ed2[50];
303 /* Send termination status back to Dir */
304 dir->fsend(EndJob, jcr->JobStatus, jcr->JobFiles,
305 edit_uint64(jcr->ReadBytes, ed1),
306 edit_uint64(jcr->JobBytes, ed2), jcr->JobErrors, jcr->VSS,
307 jcr->crypto.pki_encrypt);
308 Dmsg1(110, "End FD msg: %s\n", dir->msg);
311 generate_daemon_event(jcr, "JobEnd");
312 generate_plugin_event(jcr, bEventJobEnd);
314 dequeue_messages(jcr); /* send any queued messages */
316 /* Inform Director that we are done */
317 dir->signal(BNET_TERMINATE);
319 free_plugins(jcr); /* release instantiated plugins */
320 free_and_null_pool_memory(jcr->job_metadata);
322 /* Clean up fileset */
323 FF_PKT *ff = jcr->ff;
324 findFILESET *fileset = ff->fileset;
327 /* Delete FileSet Include lists */
328 for (i=0; i<fileset->include_list.size(); i++) {
329 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
330 for (j=0; j<incexe->opts_list.size(); j++) {
331 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
332 for (k=0; k<fo->regex.size(); k++) {
333 regfree((regex_t *)fo->regex.get(k));
335 for (k=0; k<fo->regexdir.size(); k++) {
336 regfree((regex_t *)fo->regexdir.get(k));
338 for (k=0; k<fo->regexfile.size(); k++) {
339 regfree((regex_t *)fo->regexfile.get(k));
342 fo->regexdir.destroy();
343 fo->regexfile.destroy();
345 fo->wilddir.destroy();
346 fo->wildfile.destroy();
347 fo->wildbase.destroy();
349 fo->fstype.destroy();
350 fo->drivetype.destroy();
352 incexe->opts_list.destroy();
353 incexe->name_list.destroy();
354 incexe->plugin_list.destroy();
355 if (incexe->ignoredir) {
356 free(incexe->ignoredir);
359 fileset->include_list.destroy();
361 /* Delete FileSet Exclude lists */
362 for (i=0; i<fileset->exclude_list.size(); i++) {
363 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
364 for (j=0; j<incexe->opts_list.size(); j++) {
365 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
367 fo->regexdir.destroy();
368 fo->regexfile.destroy();
370 fo->wilddir.destroy();
371 fo->wildfile.destroy();
372 fo->wildbase.destroy();
374 fo->fstype.destroy();
375 fo->drivetype.destroy();
377 incexe->opts_list.destroy();
378 incexe->name_list.destroy();
379 incexe->plugin_list.destroy();
380 if (incexe->ignoredir) {
381 free(incexe->ignoredir);
384 fileset->exclude_list.destroy();
388 Dmsg0(100, "Calling term_find_files\n");
389 term_find_files(jcr->ff);
390 // save_cwd.restore(jcr);
391 // save_cwd.release();
393 Dmsg0(100, "Done with term_find_files\n");
394 free_jcr(jcr); /* destroy JCR record */
395 Dmsg0(100, "Done with free_jcr\n");
401 * Hello from Director he must identify himself and provide his
404 static int hello_cmd(JCR *jcr)
406 Dmsg0(120, "Calling Authenticate\n");
407 if (!authenticate_director(jcr)) {
410 Dmsg0(120, "OK Authenticate\n");
411 jcr->authenticated = true;
418 static int cancel_cmd(JCR *jcr)
420 BSOCK *dir = jcr->dir_bsock;
421 char Job[MAX_NAME_LENGTH];
424 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
425 if (!(cjcr=get_jcr_by_full_name(Job))) {
426 dir->fsend(_("2901 Job %s not found.\n"), Job);
428 if (cjcr->store_bsock) {
429 cjcr->store_bsock->set_timed_out();
430 cjcr->store_bsock->set_terminated();
431 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
433 generate_plugin_event(cjcr, bEventCancelCommand, NULL);
434 set_jcr_job_status(cjcr, JS_Canceled);
436 dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
439 dir->fsend(_("2902 Error scanning cancel command.\n"));
441 dir->signal(BNET_EOD);
447 * Set debug level as requested by the Director
450 static int setdebug_cmd(JCR *jcr)
452 BSOCK *dir = jcr->dir_bsock;
453 int level, trace_flag;
455 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
456 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
457 pm_strcpy(jcr->errmsg, dir->msg);
458 dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
462 set_trace(trace_flag);
463 return dir->fsend(OKsetdebug, level);
467 static int estimate_cmd(JCR *jcr)
469 BSOCK *dir = jcr->dir_bsock;
470 char ed1[50], ed2[50];
472 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
473 pm_strcpy(jcr->errmsg, dir->msg);
474 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
475 dir->fsend(_("2992 Bad estimate command.\n"));
479 dir->fsend(OKest, edit_uint64_with_commas(jcr->num_files_examined, ed1),
480 edit_uint64_with_commas(jcr->JobBytes, ed2));
481 dir->signal(BNET_EOD);
486 * Get JobId and Storage Daemon Authorization key from Director
488 static int job_cmd(JCR *jcr)
490 BSOCK *dir = jcr->dir_bsock;
491 POOL_MEM sd_auth_key(PM_MESSAGE);
492 sd_auth_key.check_size(dir->msglen);
494 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
495 &jcr->VolSessionId, &jcr->VolSessionTime,
496 sd_auth_key.c_str()) != 5) {
497 pm_strcpy(jcr->errmsg, dir->msg);
498 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
502 set_storage_auth_key(jcr, sd_auth_key.c_str());
503 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
504 Mmsg(jcr->errmsg, "JobId=%d Job=%s", jcr->JobId, jcr->Job);
505 new_plugins(jcr); /* instantiate plugins for this jcr */
506 generate_plugin_event(jcr, bEventJobStart, (void *)jcr->errmsg);
507 return dir->fsend(OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
510 static int runbefore_cmd(JCR *jcr)
513 BSOCK *dir = jcr->dir_bsock;
514 POOLMEM *cmd = get_memory(dir->msglen+1);
517 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
518 if (sscanf(dir->msg, runbefore, cmd) != 1) {
519 pm_strcpy(jcr->errmsg, dir->msg);
520 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
521 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
527 /* Run the command now */
528 script = new_runscript();
529 script->set_command(cmd);
530 script->when = SCRIPT_Before;
531 ok = script->run(jcr, "ClientRunBeforeJob");
532 free_runscript(script);
536 dir->fsend(OKRunBefore);
539 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
544 static int runbeforenow_cmd(JCR *jcr)
546 BSOCK *dir = jcr->dir_bsock;
548 run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
549 if (job_canceled(jcr)) {
550 dir->fsend(_("2905 Bad RunBeforeNow command.\n"));
551 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
554 dir->fsend(OKRunBeforeNow);
555 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
560 static int runafter_cmd(JCR *jcr)
562 BSOCK *dir = jcr->dir_bsock;
563 POOLMEM *msg = get_memory(dir->msglen+1);
566 Dmsg1(100, "runafter_cmd: %s", dir->msg);
567 if (sscanf(dir->msg, runafter, msg) != 1) {
568 pm_strcpy(jcr->errmsg, dir->msg);
569 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
570 dir->fsend(_("2905 Bad RunAfterJob command.\n"));
576 cmd = new_runscript();
577 cmd->set_command(msg);
578 cmd->on_success = true;
579 cmd->on_failure = false;
580 cmd->when = SCRIPT_After;
582 jcr->RunScripts->append(cmd);
584 free_pool_memory(msg);
585 return dir->fsend(OKRunAfter);
588 static int runscript_cmd(JCR *jcr)
590 BSOCK *dir = jcr->dir_bsock;
591 POOLMEM *msg = get_memory(dir->msglen+1);
592 int on_success, on_failure, fail_on_error;
594 RUNSCRIPT *cmd = new_runscript() ;
596 Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
597 /* Note, we cannot sscanf into bools */
598 if (sscanf(dir->msg, runscript, &on_success,
603 pm_strcpy(jcr->errmsg, dir->msg);
604 Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
605 dir->fsend(_("2905 Bad RunScript command.\n"));
610 cmd->on_success = on_success;
611 cmd->on_failure = on_failure;
612 cmd->fail_on_error = fail_on_error;
615 cmd->set_command(msg);
617 jcr->RunScripts->append(cmd);
619 free_pool_memory(msg);
620 return dir->fsend(OKRunScript);
624 * This reads data sent from the Director from the
625 * RestoreObject table that allows us to get objects
626 * that were backed up (VSS .xml data) and are needed
627 * before starting the restore.
629 static int restore_object_cmd(JCR *jcr)
631 BSOCK *dir = jcr->dir_bsock;
632 POOLMEM *msg = get_memory(dir->msglen+1);
634 restore_object_pkt rop;
636 memset(&rop, 0, sizeof(rop));
637 rop.pkt_size = sizeof(rop);
638 rop.pkt_end = sizeof(rop);
639 Dmsg1(100, "Enter restoreobject_cmd: %s", dir->msg);
640 if (strcmp(dir->msg, endrestoreobjectcmd) == 0) {
641 generate_plugin_event(jcr, bEventRestoreObject, NULL);
643 return dir->fsend(OKRestoreObject);
646 if (sscanf(dir->msg, restoreobjcmd, &rop.JobId, &rop.object_len,
647 &rop.object_index, &rop.object_type, &FileIndex) != 5) {
648 Dmsg0(5, "Bad restore object command\n");
649 pm_strcpy(jcr->errmsg, dir->msg);
650 Jmsg1(jcr, M_FATAL, 0, _("Bad RestoreObject command: %s\n"), jcr->errmsg);
654 Dmsg5(100, "Recv object: JobId=%u objlen=%d objinx=%d objtype=%d FI=%d\n",
655 rop.JobId, rop.object_len, rop.object_index, rop.object_type, FileIndex);
656 /* Read Object name */
657 if (dir->recv() < 0) {
660 Dmsg2(100, "Recv Oname object: len=%d Oname=%s\n", dir->msglen, dir->msg);
661 rop.object_name = bstrdup(dir->msg);
664 if (dir->recv() < 0) {
667 rop.object = dir->msg;
668 Dmsg2(100, "Recv Object: len=%d Object=%s\n", dir->msglen, dir->msg);
670 if (strcmp(rop.object_name, "job_metadata.xml") == 0) {
671 Dmsg0(100, "got job metadata\n");
672 free_and_null_pool_memory(jcr->job_metadata);
673 jcr->job_metadata = rop.object;
677 generate_plugin_event(jcr, bEventRestoreObject, (void *)&rop);
680 if (rop.object_name) {
681 free(rop.object_name);
684 dir->msg = get_pool_memory(PM_MESSAGE);
688 Dmsg1(100, "Send: %s", OKRestoreObject);
692 dir->fsend(_("2909 Bad RestoreObject command.\n"));
699 static bool init_fileset(JCR *jcr)
702 findFILESET *fileset;
711 fileset = (findFILESET *)malloc(sizeof(findFILESET));
712 memset(fileset, 0, sizeof(findFILESET));
713 ff->fileset = fileset;
714 fileset->state = state_none;
715 fileset->include_list.init(1, true);
716 fileset->exclude_list.init(1, true);
720 static findFOPTS *start_options(FF_PKT *ff)
722 int state = ff->fileset->state;
723 findINCEXE *incexe = ff->fileset->incexe;
725 if (state != state_options) {
726 ff->fileset->state = state_options;
727 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
728 memset(fo, 0, sizeof(findFOPTS));
729 fo->regex.init(1, true);
730 fo->regexdir.init(1, true);
731 fo->regexfile.init(1, true);
732 fo->wild.init(1, true);
733 fo->wilddir.init(1, true);
734 fo->wildfile.init(1, true);
735 fo->wildbase.init(1, true);
736 fo->base.init(1, true);
737 fo->fstype.init(1, true);
738 fo->drivetype.init(1, true);
739 incexe->current_opts = fo;
740 incexe->opts_list.append(fo);
742 return incexe->current_opts;
747 * Add fname to include/exclude fileset list. First check for
748 * | and < and if necessary perform command.
750 void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset,
765 p++; /* skip over | */
766 fn = get_pool_memory(PM_FNAME);
767 fn = edit_job_codes(jcr, fn, p, "");
768 bpipe = open_bpipe(fn, 0, "r");
771 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
773 free_pool_memory(fn);
776 free_pool_memory(fn);
777 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
778 strip_trailing_junk(buf);
780 fileset->incexe->name_list.append(new_dlistString(buf));
782 fileset->incexe->plugin_list.append(new_dlistString(buf));
785 if ((stat=close_bpipe(bpipe)) != 0) {
787 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. stat=%d: ERR=%s\n"),
788 p, be.code(stat), be.bstrerror(stat));
793 Dmsg1(100, "Doing < of '%s' include on client.\n", p + 1);
794 p++; /* skip over < */
795 if ((ffd = fopen(p, "rb")) == NULL) {
797 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
801 while (fgets(buf, sizeof(buf), ffd)) {
802 strip_trailing_junk(buf);
803 Dmsg1(100, "%s\n", buf);
805 fileset->incexe->name_list.append(new_dlistString(buf));
807 fileset->incexe->plugin_list.append(new_dlistString(buf));
814 fileset->incexe->name_list.append(new_dlistString(fname));
816 if (me->plugin_directory) {
817 fileset->incexe->plugin_list.append(new_dlistString(fname));
819 Jmsg(jcr, M_FATAL, 0, _("Plugin Directory not defined. Cannot use plugin: \"%\"\n"),
828 * Define a new Exclude block in the FileSet
830 findFILESET *new_exclude(JCR *jcr)
832 FF_PKT *ff = jcr->ff;
833 findFILESET *fileset = ff->fileset;
836 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
837 memset(fileset->incexe, 0, sizeof(findINCEXE));
838 fileset->incexe->opts_list.init(1, true);
839 fileset->incexe->name_list.init();
840 fileset->incexe->plugin_list.init();
841 fileset->exclude_list.append(fileset->incexe);
846 * Define a new Include block in the FileSet
848 findFILESET *new_include(JCR *jcr)
850 FF_PKT *ff = jcr->ff;
851 findFILESET *fileset = ff->fileset;
854 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
855 memset(fileset->incexe, 0, sizeof(findINCEXE));
856 fileset->incexe->opts_list.init(1, true);
857 fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */
858 fileset->incexe->plugin_list.init();
859 fileset->include_list.append(fileset->incexe);
863 int add_regex_to_fileset(JCR *jcr, const char *item, int subcode)
865 FF_PKT *ff = jcr->ff;
867 findFOPTS *current_opts;
872 current_opts = start_options(ff);
873 preg = (regex_t *)malloc(sizeof(regex_t));
874 if (current_opts->flags & FO_IGNORECASE) {
875 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
877 rc = regcomp(preg, item, REG_EXTENDED);
880 regerror(rc, preg, prbuf, sizeof(prbuf));
883 Jmsg(jcr, M_FATAL, 0, _("REGEX %s compile error. ERR=%s\n"), item, prbuf);
887 state = state_options;
888 if (subcode == ' ') {
889 current_opts->regex.append(preg);
890 } else if (subcode == 'D') {
891 current_opts->regexdir.append(preg);
892 } else if (subcode == 'F') {
893 current_opts->regexfile.append(preg);
901 static void add_fileset(JCR *jcr, const char *item)
903 FF_PKT *ff = jcr->ff;
904 findFILESET *fileset = ff->fileset;
905 int state = fileset->state;
906 findFOPTS *current_opts;
908 /* Get code, optional subcode, and position item past the dividing space */
909 Dmsg1(100, "%s\n", item);
914 int subcode = ' '; /* A space is always a valid subcode */
915 if (item[0] != '\0' && item[0] != ' ') {
923 /* Skip all lines we receive after an error */
924 if (state == state_error) {
925 Dmsg0(100, "State=error return\n");
930 * The switch tests the code for validity.
931 * The subcode is always good if it is a space, otherwise we must confirm.
932 * We set state to state_error first assuming the subcode is invalid,
933 * requiring state to be set in cases below that handle subcodes.
935 if (subcode != ' ') {
937 Dmsg0(100, "Set state=error or double code.\n");
941 fileset = new_include(jcr);
944 fileset = new_exclude(jcr);
950 /* File item to include or exclude list */
951 state = state_include;
952 add_file_to_fileset(jcr, item, fileset, true);
955 /* Plugin item to include list */
956 state = state_include;
957 add_file_to_fileset(jcr, item, fileset, false);
960 state = add_regex_to_fileset(jcr, item, subcode);
963 current_opts = start_options(ff);
964 current_opts->base.append(bstrdup(item));
965 state = state_options;
968 current_opts = start_options(ff);
969 state = state_options;
970 if (subcode == ' ') {
971 current_opts->fstype.append(bstrdup(item));
972 } else if (subcode == 'D') {
973 current_opts->drivetype.append(bstrdup(item));
979 current_opts = start_options(ff);
980 state = state_options;
981 if (subcode == ' ') {
982 current_opts->wild.append(bstrdup(item));
983 } else if (subcode == 'D') {
984 current_opts->wilddir.append(bstrdup(item));
985 } else if (subcode == 'F') {
986 current_opts->wildfile.append(bstrdup(item));
987 } else if (subcode == 'B') {
988 current_opts->wildbase.append(bstrdup(item));
994 current_opts = start_options(ff);
995 set_options(current_opts, item);
996 state = state_options;
999 state = state_include;
1000 fileset->incexe->ignoredir = bstrdup(item);
1003 current_opts = start_options(ff);
1004 // current_opts->reader = bstrdup(item);
1005 state = state_options;
1008 current_opts = start_options(ff);
1009 // current_opts->writer = bstrdup(item);
1010 state = state_options;
1013 Jmsg(jcr, M_FATAL, 0, _("Invalid FileSet command: %s\n"), item);
1014 state = state_error;
1017 ff->fileset->state = state;
1020 static bool term_fileset(JCR *jcr)
1022 FF_PKT *ff = jcr->ff;
1024 #ifdef xxx_DEBUG_CODE
1025 findFILESET *fileset = ff->fileset;
1028 for (i=0; i<fileset->include_list.size(); i++) {
1029 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
1031 for (j=0; j<incexe->opts_list.size(); j++) {
1032 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
1033 for (k=0; k<fo->regex.size(); k++) {
1034 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
1036 for (k=0; k<fo->regexdir.size(); k++) {
1037 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
1039 for (k=0; k<fo->regexfile.size(); k++) {
1040 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
1042 for (k=0; k<fo->wild.size(); k++) {
1043 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
1045 for (k=0; k<fo->wilddir.size(); k++) {
1046 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
1048 for (k=0; k<fo->wildfile.size(); k++) {
1049 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
1051 for (k=0; k<fo->wildbase.size(); k++) {
1052 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
1054 for (k=0; k<fo->base.size(); k++) {
1055 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
1057 for (k=0; k<fo->fstype.size(); k++) {
1058 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
1060 for (k=0; k<fo->drivetype.size(); k++) {
1061 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
1064 if (incexe->ignoredir) {
1065 Dmsg1(400, "Z %s\n", incexe->ignoredir);
1068 foreach_dlist(node, &incexe->name_list) {
1069 Dmsg1(400, "F %s\n", node->c_str());
1071 foreach_dlist(node, &incexe->plugin_list) {
1072 Dmsg1(400, "P %s\n", node->c_str());
1075 for (i=0; i<fileset->exclude_list.size(); i++) {
1076 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
1078 for (j=0; j<incexe->opts_list.size(); j++) {
1079 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
1080 for (k=0; k<fo->regex.size(); k++) {
1081 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
1083 for (k=0; k<fo->regexdir.size(); k++) {
1084 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
1086 for (k=0; k<fo->regexfile.size(); k++) {
1087 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
1089 for (k=0; k<fo->wild.size(); k++) {
1090 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
1092 for (k=0; k<fo->wilddir.size(); k++) {
1093 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
1095 for (k=0; k<fo->wildfile.size(); k++) {
1096 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
1098 for (k=0; k<fo->wildbase.size(); k++) {
1099 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
1101 for (k=0; k<fo->base.size(); k++) {
1102 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
1104 for (k=0; k<fo->fstype.size(); k++) {
1105 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
1107 for (k=0; k<fo->drivetype.size(); k++) {
1108 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
1112 foreach_dlist(node, incexe->name_list) {
1113 Dmsg1(400, "F %s\n", node->c_str());
1115 foreach_dlist(node, &incexe->plugin_list) {
1116 Dmsg1(400, "P %s\n", node->c_str());
1120 return ff->fileset->state != state_error;
1125 * As an optimization, we should do this during
1126 * "compile" time in filed/job.c, and keep only a bit mask
1127 * and the Verify options.
1129 static void set_options(findFOPTS *fo, const char *opts)
1135 // Commented out as it is not backward compatible - KES
1137 // fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
1140 for (p=opts; *p; p++) {
1142 case 'a': /* alway replace */
1143 case '0': /* no option */
1146 fo->flags |= FO_EXCLUDE;
1149 fo->flags |= FO_MULTIFS;
1151 case 'h': /* no recursion */
1152 fo->flags |= FO_NO_RECURSION;
1154 case 'H': /* no hard link handling */
1155 fo->flags |= FO_NO_HARDLINK;
1158 fo->flags |= FO_IGNORECASE;
1161 fo->flags |= FO_MD5;
1164 fo->flags |= FO_NOREPLACE;
1166 case 'p': /* use portable data format */
1167 fo->flags |= FO_PORTABLE;
1169 case 'R': /* Resource forks and Finder Info */
1170 fo->flags |= FO_HFSPLUS;
1171 case 'r': /* read fifo */
1172 fo->flags |= FO_READFIFO;
1177 fo->flags |= FO_SHA1;
1182 fo->flags |= FO_SHA256;
1186 fo->flags |= FO_SHA512;
1192 * If 2 or 3 is seen here, SHA2 is not configured, so
1193 * eat the option, and drop back to SHA-1.
1195 if (p[1] == '2' || p[1] == '3') {
1198 fo->flags |= FO_SHA1;
1203 fo->flags |= FO_SPARSE;
1206 fo->flags |= FO_MTIMEONLY;
1209 fo->flags |= FO_KEEPATIME;
1212 fo->flags |= FO_ACL;
1214 case 'V': /* verify options */
1215 /* Copy Verify Options */
1216 for (j=0; *p && *p != ':'; p++) {
1217 fo->VerifyOpts[j] = *p;
1218 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
1222 fo->VerifyOpts[j] = 0;
1224 case 'C': /* accurate options */
1225 /* Copy Accurate Options */
1226 for (j=0; *p && *p != ':'; p++) {
1227 fo->AccurateOpts[j] = *p;
1228 if (j < (int)sizeof(fo->AccurateOpts) - 1) {
1232 fo->AccurateOpts[j] = 0;
1234 case 'J': /* Basejob options */
1235 /* Copy BaseJob Options */
1236 for (j=0; *p && *p != ':'; p++) {
1237 fo->BaseJobOpts[j] = *p;
1238 if (j < (int)sizeof(fo->BaseJobOpts) - 1) {
1242 fo->BaseJobOpts[j] = 0;
1244 case 'P': /* strip path */
1247 for (j=0; *p && *p != ':'; p++) {
1249 if (j < (int)sizeof(strip) - 1) {
1254 fo->strip_path = atoi(strip);
1255 fo->flags |= FO_STRIPPATH;
1256 Dmsg2(100, "strip=%s strip_path=%d\n", strip, fo->strip_path);
1259 fo->flags |= FO_IF_NEWER;
1262 fo->flags |= FO_ENHANCEDWILD;
1264 case 'Z': /* gzip compression */
1265 fo->flags |= FO_GZIP;
1266 fo->GZIP_level = *++p - '0';
1269 fo->flags |= FO_NOATIME;
1272 fo->flags |= FO_CHKCHANGES;
1275 fo->flags |= FO_HONOR_NODUMP;
1278 fo->flags |= FO_XATTR;
1281 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
1289 * Director is passing his Fileset
1291 static int fileset_cmd(JCR *jcr)
1293 BSOCK *dir = jcr->dir_bsock;
1295 #if defined(WIN32_VSS)
1298 sscanf(dir->msg, "fileset vss=%d", &vss);
1302 if (!init_fileset(jcr)) {
1305 while (dir->recv() >= 0) {
1306 strip_trailing_junk(dir->msg);
1307 Dmsg1(500, "Fileset: %s\n", dir->msg);
1308 add_fileset(jcr, dir->msg);
1310 if (!term_fileset(jcr)) {
1313 return dir->fsend(OKinc);
1316 static void free_bootstrap(JCR *jcr)
1318 if (jcr->RestoreBootstrap) {
1319 unlink(jcr->RestoreBootstrap);
1320 free_pool_memory(jcr->RestoreBootstrap);
1321 jcr->RestoreBootstrap = NULL;
1326 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1327 static uint32_t bsr_uniq = 0;
1330 * The Director sends us the bootstrap file, which
1331 * we will in turn pass to the SD.
1332 * Deprecated. The bsr is now sent directly from the
1333 * Director to the SD.
1335 static int bootstrap_cmd(JCR *jcr)
1337 BSOCK *dir = jcr->dir_bsock;
1338 POOLMEM *fname = get_pool_memory(PM_FNAME);
1341 free_bootstrap(jcr);
1344 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1345 jcr->Job, bsr_uniq);
1347 Dmsg1(400, "bootstrap=%s\n", fname);
1348 jcr->RestoreBootstrap = fname;
1349 bs = fopen(fname, "a+b"); /* create file */
1352 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1353 jcr->RestoreBootstrap, be.bstrerror());
1355 * Suck up what he is sending to us so that he will then
1356 * read our error message.
1358 while (dir->recv() >= 0)
1360 free_bootstrap(jcr);
1361 set_jcr_job_status(jcr, JS_ErrorTerminated);
1365 while (dir->recv() >= 0) {
1366 Dmsg1(200, "filed<dird: bootstrap: %s", dir->msg);
1367 fputs(dir->msg, bs);
1371 * Note, do not free the bootstrap yet -- it needs to be
1374 return dir->fsend(OKbootstrap);
1379 * Get backup level from Director
1382 static int level_cmd(JCR *jcr)
1384 BSOCK *dir = jcr->dir_bsock;
1385 POOLMEM *level, *buf = NULL;
1388 level = get_memory(dir->msglen+1);
1389 Dmsg1(100, "level_cmd: %s", dir->msg);
1391 /* keep compatibility with older directors */
1392 if (strstr(dir->msg, "accurate")) {
1393 jcr->accurate = true;
1395 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1398 /* Base backup requested? */
1399 if (strcmp(level, "base") == 0) {
1400 jcr->set_JobLevel(L_BASE);
1401 /* Full backup requested? */
1402 } else if (strcmp(level, "full") == 0) {
1403 jcr->set_JobLevel(L_FULL);
1404 } else if (strstr(level, "differential")) {
1405 jcr->set_JobLevel(L_DIFFERENTIAL);
1408 } else if (strstr(level, "incremental")) {
1409 jcr->set_JobLevel(L_INCREMENTAL);
1413 * We get his UTC since time, then sync the clocks and correct it
1414 * to agree with our clock.
1416 } else if (strcmp(level, "since_utime") == 0) {
1417 buf = get_memory(dir->msglen+1);
1418 utime_t since_time, adj;
1419 btime_t his_time, bt_start, rt=0, bt_adj=0;
1420 if (jcr->getJobLevel() == L_NONE) {
1421 jcr->set_JobLevel(L_SINCE); /* if no other job level set, do it now */
1423 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1424 buf, &mtime_only) != 2) {
1427 since_time = str_to_uint64(buf); /* this is the since time */
1428 Dmsg1(100, "since_time=%lld\n", since_time);
1429 char ed1[50], ed2[50];
1431 * Sync clocks by polling him for the time. We take
1432 * 10 samples of his time throwing out the first two.
1434 for (int i=0; i<10; i++) {
1435 bt_start = get_current_btime();
1436 dir->signal(BNET_BTIME); /* poll for time */
1437 if (dir->recv() <= 0) { /* get response */
1440 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1443 if (i < 2) { /* toss first two results */
1446 his_time = str_to_uint64(buf);
1447 rt = get_current_btime() - bt_start; /* compute round trip time */
1448 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1449 edit_uint64(bt_start, ed2));
1450 bt_adj += bt_start - his_time - rt/2;
1451 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1454 bt_adj = bt_adj / 8; /* compute average time */
1455 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1456 adj = btime_to_utime(bt_adj);
1457 since_time += adj; /* adjust for clock difference */
1458 /* Don't notify if time within 3 seconds */
1459 if (adj > 3 || adj < -3) {
1461 if (adj > 600 || adj < -600) {
1466 Jmsg(jcr, type, 0, _("DIR and FD clocks differ by %lld seconds, FD automatically compensating.\n"), adj);
1468 dir->signal(BNET_EOD);
1470 Dmsg2(100, "adj=%lld since_time=%lld\n", adj, since_time);
1471 jcr->incremental = 1; /* set incremental or decremental backup */
1472 jcr->mtime = since_time; /* set since time */
1473 generate_plugin_event(jcr, bEventSince, (void *)(time_t)jcr->mtime);
1475 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1483 generate_plugin_event(jcr, bEventLevel, (void *)jcr->getJobLevel());
1484 return dir->fsend(OKlevel);
1487 pm_strcpy(jcr->errmsg, dir->msg);
1488 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1497 * Get session parameters from Director -- this is for a Restore command
1498 * This is deprecated. It is now passed via the bsr.
1500 static int session_cmd(JCR *jcr)
1502 BSOCK *dir = jcr->dir_bsock;
1504 Dmsg1(100, "SessionCmd: %s", dir->msg);
1505 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1506 &jcr->VolSessionId, &jcr->VolSessionTime,
1507 &jcr->StartFile, &jcr->EndFile,
1508 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1509 pm_strcpy(jcr->errmsg, dir->msg);
1510 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1514 return dir->fsend(OKsession);
1517 static void set_storage_auth_key(JCR *jcr, char *key)
1519 /* if no key don't update anything */
1525 * We can be contacting multiple storage daemons.
1526 * So, make sure that any old jcr->store_bsock is cleaned up.
1528 if (jcr->store_bsock) {
1529 jcr->store_bsock->destroy();
1530 jcr->store_bsock = NULL;
1534 * We can be contacting multiple storage daemons.
1535 * So, make sure that any old jcr->sd_auth_key is cleaned up.
1537 if (jcr->sd_auth_key) {
1539 * If we already have a Authorization key, director can do multi
1542 Dmsg0(5, "set multi_restore=true\n");
1543 jcr->multi_restore = true;
1544 bfree(jcr->sd_auth_key);
1547 jcr->sd_auth_key = bstrdup(key);
1548 Dmsg0(5, "set sd auth key\n");
1552 * Get address of storage daemon from Director
1555 static int storage_cmd(JCR *jcr)
1557 int stored_port; /* storage daemon port */
1558 int enable_ssl; /* enable ssl to sd */
1559 POOL_MEM sd_auth_key(PM_MESSAGE);
1560 BSOCK *dir = jcr->dir_bsock;
1561 BSOCK *sd = new_bsock(); /* storage daemon bsock */
1564 Dmsg1(100, "StorageCmd: %s", dir->msg);
1565 sd_auth_key.check_size(dir->msglen);
1566 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
1567 &enable_ssl, sd_auth_key.c_str()) != 4) {
1568 if (sscanf(dir->msg, storaddr_v1, &jcr->stored_addr,
1569 &stored_port, &enable_ssl) != 3) {
1570 pm_strcpy(jcr->errmsg, dir->msg);
1571 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1576 set_storage_auth_key(jcr, sd_auth_key.c_str());
1578 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
1580 /* Open command communications with Storage daemon */
1581 /* Try to connect for 1 hour at 10 second intervals */
1583 sd->set_source_address(me->FDsrc_addr);
1584 if (!sd->connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
1585 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
1591 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1592 jcr->stored_addr, stored_port);
1593 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1594 jcr->stored_addr, stored_port);
1597 Dmsg0(110, "Connection OK to SD.\n");
1599 jcr->store_bsock = sd;
1601 sd->fsend("Hello Start Job %s\n", jcr->Job);
1602 if (!authenticate_storagedaemon(jcr)) {
1603 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1606 Dmsg0(110, "Authenticated with SD.\n");
1608 /* Send OK to Director */
1609 return dir->fsend(OKstore);
1612 dir->fsend(BADcmd, "storage");
1621 static int backup_cmd(JCR *jcr)
1623 BSOCK *dir = jcr->dir_bsock;
1624 BSOCK *sd = jcr->store_bsock;
1628 #if defined(WIN32_VSS)
1629 // capture state here, if client is backed up by multiple directors
1630 // and one enables vss and the other does not then enable_vss can change
1631 // between here and where its evaluated after the job completes.
1632 jcr->VSS = g_pVSSClient && enable_vss;
1634 /* Run only one at a time */
1640 * Validate some options given to the backup make sense for the compiled in
1641 * options of this filed.
1643 if (jcr->ff->flags & FO_ACL && !have_acl) {
1644 Jmsg(jcr, M_FATAL, 0, _("ACL support not configured for your machine.\n"));
1647 if (jcr->ff->flags & FO_XATTR && !have_xattr) {
1648 Jmsg(jcr, M_FATAL, 0, _("XATTR support not configured for your machine.\n"));
1652 set_jcr_job_status(jcr, JS_Blocked);
1653 jcr->set_JobType(JT_BACKUP);
1654 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1657 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1658 dir->fsend(BADcmd, "backup");
1662 dir->fsend(OKbackup);
1663 Dmsg1(110, "filed>dird: %s", dir->msg);
1666 * Send Append Open Session to Storage daemon
1668 sd->fsend(append_open);
1669 Dmsg1(110, ">stored: %s", sd->msg);
1671 * Expect to receive back the Ticket number
1673 if (bget_msg(sd) >= 0) {
1674 Dmsg1(110, "<stored: %s", sd->msg);
1675 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1676 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1679 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1681 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1686 * Send Append data command to Storage daemon
1688 sd->fsend(append_data, jcr->Ticket);
1689 Dmsg1(110, ">stored: %s", sd->msg);
1692 * Expect to get OK data
1694 Dmsg1(110, "<stored: %s", sd->msg);
1695 if (!response(jcr, sd, OK_data, "Append Data")) {
1699 generate_daemon_event(jcr, "JobStart");
1700 generate_plugin_event(jcr, bEventStartBackupJob);
1702 #if defined(WIN32_VSS)
1703 /* START VSS ON WIN32 */
1705 if (g_pVSSClient->InitializeForBackup(jcr)) {
1706 generate_plugin_event(jcr, bEventVssBackupAddComponents);
1707 /* tell vss which drives to snapshot */
1708 char szWinDriveLetters[27];
1709 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1710 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1711 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1712 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1715 /* tell user if snapshot creation of a specific drive failed */
1717 for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
1718 if (islower(szWinDriveLetters[i])) {
1719 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1723 /* inform user about writer states */
1724 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)
1725 if (g_pVSSClient->GetWriterState(i) < 1) {
1726 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1731 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1735 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.bstrerror());
1737 run_scripts(jcr, jcr->RunScripts, "ClientAfterVSS");
1742 * Send Files to Storage daemon
1744 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1745 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1746 set_jcr_job_status(jcr, JS_ErrorTerminated);
1747 bnet_suppress_error_messages(sd, 1);
1748 Dmsg0(110, "Error in blast_data.\n");
1750 set_jcr_job_status(jcr, JS_Terminated);
1751 /* Note, the above set status will not override an error */
1752 if (!(jcr->JobStatus == JS_Terminated || jcr->JobStatus == JS_Warnings)) {
1753 bnet_suppress_error_messages(sd, 1);
1754 goto cleanup; /* bail out now */
1757 * Expect to get response to append_data from Storage daemon
1759 if (!response(jcr, sd, OK_append, "Append Data")) {
1760 set_jcr_job_status(jcr, JS_ErrorTerminated);
1765 * Send Append End Data to Storage daemon
1767 sd->fsend(append_end, jcr->Ticket);
1769 if (!response(jcr, sd, OK_end, "Append End")) {
1770 set_jcr_job_status(jcr, JS_ErrorTerminated);
1775 * Send Append Close to Storage daemon
1777 sd->fsend(append_close, jcr->Ticket);
1778 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1779 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1781 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1785 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1788 if (!(SDJobStatus == JS_Terminated || SDJobStatus == JS_Warnings)) {
1789 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1795 #if defined(WIN32_VSS)
1797 Win32ConvCleanupCache();
1802 generate_plugin_event(jcr, bEventEndBackupJob);
1803 return 0; /* return and stop command loop */
1807 * Do a Verify for Director
1810 static int verify_cmd(JCR *jcr)
1812 BSOCK *dir = jcr->dir_bsock;
1813 BSOCK *sd = jcr->store_bsock;
1816 jcr->set_JobType(JT_VERIFY);
1817 if (sscanf(dir->msg, verifycmd, level) != 1) {
1818 dir->fsend(_("2994 Bad verify command: %s\n"), dir->msg);
1822 if (strcasecmp(level, "init") == 0) {
1823 jcr->set_JobLevel(L_VERIFY_INIT);
1824 } else if (strcasecmp(level, "catalog") == 0){
1825 jcr->set_JobLevel(L_VERIFY_CATALOG);
1826 } else if (strcasecmp(level, "volume") == 0){
1827 jcr->set_JobLevel(L_VERIFY_VOLUME_TO_CATALOG);
1828 } else if (strcasecmp(level, "data") == 0){
1829 jcr->set_JobLevel(L_VERIFY_DATA);
1830 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1831 jcr->set_JobLevel(L_VERIFY_DISK_TO_CATALOG);
1833 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1837 dir->fsend(OKverify);
1839 generate_daemon_event(jcr, "JobStart");
1840 generate_plugin_event(jcr, bEventLevel, (void *)jcr->getJobLevel());
1841 generate_plugin_event(jcr, bEventStartVerifyJob);
1843 Dmsg1(110, "filed>dird: %s", dir->msg);
1845 switch (jcr->getJobLevel()) {
1847 case L_VERIFY_CATALOG:
1850 case L_VERIFY_VOLUME_TO_CATALOG:
1851 if (!open_sd_read_session(jcr)) {
1854 start_dir_heartbeat(jcr);
1855 do_verify_volume(jcr);
1856 stop_dir_heartbeat(jcr);
1858 * Send Close session command to Storage daemon
1860 sd->fsend(read_close, jcr->Ticket);
1861 Dmsg1(130, "filed>stored: %s", sd->msg);
1863 /* ****FIXME**** check response */
1864 bget_msg(sd); /* get OK */
1866 /* Inform Storage daemon that we are done */
1867 sd->signal(BNET_TERMINATE);
1870 case L_VERIFY_DISK_TO_CATALOG:
1874 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1878 dir->signal(BNET_EOD);
1879 generate_plugin_event(jcr, bEventEndVerifyJob);
1880 return 0; /* return and terminate command loop */
1884 static bool vss_restore_init_callback(JCR *jcr, int init_type)
1886 switch (init_type) {
1887 case VSS_INIT_RESTORE_AFTER_INIT:
1888 generate_plugin_event(jcr, bEventVssRestoreLoadComponentMetadata);
1890 case VSS_INIT_RESTORE_AFTER_GATHER:
1891 generate_plugin_event(jcr, bEventVssRestoreSetComponentsSelected);
1901 * Do a Restore for Director
1904 static int restore_cmd(JCR *jcr)
1906 BSOCK *dir = jcr->dir_bsock;
1907 BSOCK *sd = jcr->store_bsock;
1909 bool use_regexwhere=false;
1914 * Scan WHERE (base directory for restore) from command
1916 Dmsg0(100, "restore command\n");
1917 #if defined(WIN32_VSS)
1919 /* TODO: this should be given from the director */
1922 Dmsg2(50, "g_pVSSClient = %p, enable_vss = %d\n", g_pVSSClient, enable_vss);
1923 // capture state here, if client is backed up by multiple directors
1924 // and one enables vss and the other does not then enable_vss can change
1925 // between here and where its evaluated after the job completes.
1926 jcr->VSS = g_pVSSClient && enable_vss;
1928 /* Run only one at a time */
1932 /* Pickup where string */
1933 args = get_memory(dir->msglen+1);
1936 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
1937 if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
1938 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1939 pm_strcpy(jcr->errmsg, dir->msg);
1940 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1945 use_regexwhere = true;
1947 /* Turn / into nothing */
1948 if (IsPathSeparator(args[0]) && args[1] == '\0') {
1952 Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
1953 unbash_spaces(args);
1955 if (use_regexwhere) {
1956 jcr->where_bregexp = get_bregexps(args);
1957 if (!jcr->where_bregexp) {
1958 Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
1959 free_pool_memory(args);
1963 jcr->where = bstrdup(args);
1966 free_pool_memory(args);
1967 jcr->replace = replace;
1968 jcr->prefix_links = prefix_links;
1970 dir->fsend(OKrestore);
1971 Dmsg1(110, "filed>dird: %s", dir->msg);
1973 jcr->set_JobType(JT_RESTORE);
1975 set_jcr_job_status(jcr, JS_Blocked);
1977 if (!open_sd_read_session(jcr)) {
1978 set_jcr_job_status(jcr, JS_ErrorTerminated);
1982 set_jcr_job_status(jcr, JS_Running);
1985 * Do restore of files and data
1987 start_dir_heartbeat(jcr);
1988 generate_daemon_event(jcr, "JobStart");
1989 generate_plugin_event(jcr, bEventStartRestoreJob);
1991 #if defined(WIN32_VSS)
1992 /* START VSS ON WIN32 */
1994 if (g_pVSSClient->InitializeForRestore(jcr, vss_restore_init_callback,
1995 (WCHAR *)jcr->job_metadata)) {
1996 /* inform user about writer states */
1998 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++) {
1999 int msg_type = M_INFO;
2000 if (g_pVSSClient->GetWriterState(i) < 1) {
2001 msg_type = M_WARNING;
2004 if (g_pVSSClient->GetWriterState(i) < 1) {
2005 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PreRestore): %s\n"), g_pVSSClient->GetWriterInfo(i));
2011 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.bstrerror());
2013 free_and_null_pool_memory(jcr->job_metadata);
2014 run_scripts(jcr, jcr->RunScripts, "ClientAfterVSS");
2019 stop_dir_heartbeat(jcr);
2021 set_jcr_job_status(jcr, JS_Terminated);
2022 if (jcr->JobStatus != JS_Terminated) {
2023 bnet_suppress_error_messages(sd, 1);
2027 * Send Close session command to Storage daemon
2029 sd->fsend(read_close, jcr->Ticket);
2030 Dmsg1(100, "filed>stored: %s", sd->msg);
2032 bget_msg(sd); /* get OK */
2034 /* Inform Storage daemon that we are done */
2035 sd->signal(BNET_TERMINATE);
2037 #if defined(WIN32_VSS)
2038 /* STOP VSS ON WIN32 */
2039 /* tell vss to close the restore session */
2040 Dmsg0(0, "About to call CloseRestore\n");
2042 Dmsg0(0, "Really about to call CloseRestore\n");
2043 if (g_pVSSClient->CloseRestore()) {
2044 Dmsg0(0, "CloseRestore success\n");
2045 /* inform user about writer states */
2046 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
2047 int msg_type = M_INFO;
2048 if (g_pVSSClient->GetWriterState(i) < 1) {
2049 msg_type = M_WARNING;
2052 Jmsg(jcr, msg_type, 0, _("VSS Writer (RestoreComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
2056 Dmsg1(0, "CloseRestore fail - %08x\n", errno);
2062 bfree_and_null(jcr->where);
2064 if (jcr->JobErrors) {
2065 set_jcr_job_status(jcr, JS_ErrorTerminated);
2068 Dmsg0(100, "Done in job.c\n");
2071 if (jcr->multi_restore) {
2072 Dmsg0(100, OKstoreend);
2073 dir->fsend(OKstoreend);
2074 ret = 1; /* we continue the loop, waiting for next part */
2076 end_restore_cmd(jcr);
2077 ret = 0; /* we stop here */
2083 static int end_restore_cmd(JCR *jcr)
2085 Dmsg0(5, "end_restore_cmd\n");
2086 generate_plugin_event(jcr, bEventEndRestoreJob);
2087 return 0; /* return and terminate command loop */
2090 static int open_sd_read_session(JCR *jcr)
2092 BSOCK *sd = jcr->store_bsock;
2095 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
2098 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
2099 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
2100 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
2102 * Open Read Session with Storage daemon
2104 sd->fsend(read_open, "DummyVolume",
2105 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
2106 jcr->StartBlock, jcr->EndBlock);
2107 Dmsg1(110, ">stored: %s", sd->msg);
2112 if (bget_msg(sd) >= 0) {
2113 Dmsg1(110, "filed<stored: %s", sd->msg);
2114 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
2115 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
2118 Dmsg1(110, "filed: got Ticket=%d\n", jcr->Ticket);
2120 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
2124 if (!send_bootstrap_file(jcr)) {
2129 * Start read of data with Storage daemon
2131 sd->fsend(read_data, jcr->Ticket);
2132 Dmsg1(110, ">stored: %s", sd->msg);
2137 if (!response(jcr, sd, OK_data, "Read Data")) {
2144 * Destroy the Job Control Record and associated
2145 * resources (sockets).
2147 static void filed_free_jcr(JCR *jcr)
2149 if (jcr->store_bsock) {
2150 jcr->store_bsock->close();
2152 free_bootstrap(jcr);
2153 if (jcr->last_fname) {
2154 free_pool_memory(jcr->last_fname);
2156 free_runscripts(jcr->RunScripts);
2157 delete jcr->RunScripts;
2159 if (jcr->JobId != 0)
2160 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
2166 * Get response from Storage daemon to a command we
2167 * sent. Check that the response is OK.
2169 * Returns: 0 on failure
2172 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
2177 if (bget_msg(sd) > 0) {
2178 Dmsg0(110, sd->msg);
2179 if (strcmp(sd->msg, resp) == 0) {
2183 if (job_canceled(jcr)) {
2184 return 0; /* if canceled avoid useless error messages */
2186 if (is_bnet_error(sd)) {
2187 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
2188 cmd, bnet_strerror(sd));
2190 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
2191 cmd, resp, sd->msg);
2196 static int send_bootstrap_file(JCR *jcr)
2200 BSOCK *sd = jcr->store_bsock;
2201 const char *bootstrap = "bootstrap\n";
2204 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
2205 if (!jcr->RestoreBootstrap) {
2208 bs = fopen(jcr->RestoreBootstrap, "rb");
2211 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
2212 jcr->RestoreBootstrap, be.bstrerror());
2213 set_jcr_job_status(jcr, JS_ErrorTerminated);
2216 sd->msglen = pm_strcpy(sd->msg, bootstrap);
2218 while (fgets(buf, sizeof(buf), bs)) {
2219 sd->msglen = Mmsg(sd->msg, "%s", buf);
2222 sd->signal(BNET_EOD);
2224 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
2225 set_jcr_job_status(jcr, JS_ErrorTerminated);
2231 free_bootstrap(jcr);