]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
Partial modification of copyrights as per agreement with FSFE
[bacula/bacula] / bacula / src / dird / fd_cmds.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  *   Bacula Director -- fd_cmds.c -- send commands to File daemon
22  *
23  *     Kern Sibbald, October MM
24  *
25  *    This routine is run as a separate thread.  There may be more
26  *    work to be done to make it totally reentrant!!!!
27  *
28  *  Utility functions for sending info to File Daemon.
29  *   These functions are used by both backup and verify.
30  *
31  */
32
33 #include "bacula.h"
34 #include "dird.h"
35 #include "findlib/find.h"
36
37 const int dbglvl = 400;
38
39 /* Commands sent to File daemon */
40 static char filesetcmd[]  = "fileset%s%s\n"; /* set full fileset */
41 static char jobcmd[]      = "JobId=%s Job=%s SDid=%u SDtime=%u Authorization=%s\n";
42 /* Note, mtime_only is not used here -- implemented as file option */
43 static char levelcmd[]    = "level = %s%s%s mtime_only=%d %s%s\n";
44 static char runscript[]   = "Run OnSuccess=%u OnFailure=%u AbortOnError=%u When=%u Command=%s\n";
45 static char runbeforenow[]= "RunBeforeNow\n";
46 static char bandwidthcmd[] = "setbandwidth=%lld Job=%s\n";
47 static char component_info[] = "component_info\n";
48
49 /* Responses received from File daemon */
50 static char OKinc[]          = "2000 OK include\n";
51 static char OKjob[]          = "2000 OK Job";
52 static char OKlevel[]        = "2000 OK level\n";
53 static char OKRunScript[]    = "2000 OK RunScript\n";
54 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
55 static char OKRestoreObject[] = "2000 OK ObjectRestored\n";
56 static char OKComponentInfo[] = "2000 OK ComponentInfo\n";
57 static char OKBandwidth[]    = "2000 OK Bandwidth\n";
58
59 /* Forward referenced functions */
60 static bool send_list_item(JCR *jcr, const char *code, char *item, BSOCK *fd);
61
62 /* External functions */
63 extern DIRRES *director;
64 extern int FDConnectTimeout;
65
66 #define INC_LIST 0
67 #define EXC_LIST 1
68
69 /*
70  * Open connection with File daemon.
71  * Try connecting every retry_interval (default 10 sec), and
72  *   give up after max_retry_time (default 30 mins).
73  */
74
75 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
76                            int verbose)
77 {
78    BSOCK   *fd = jcr->file_bsock;
79    char ed1[30];
80    utime_t heart_beat;
81
82    if (!jcr->client) {
83       Jmsg(jcr, M_FATAL, 0, _("File daemon not defined for current Job\n"));
84       Dmsg0(10, "No Client defined for the job.\n");
85       return 0;
86    }
87
88    if (jcr->client->heartbeat_interval) {
89       heart_beat = jcr->client->heartbeat_interval;
90    } else {
91       heart_beat = director->heartbeat_interval;
92    }
93
94    if (!is_bsock_open(jcr->file_bsock)) {
95       char name[MAX_NAME_LENGTH + 100];
96
97       if (!fd) {
98          fd = jcr->file_bsock = new_bsock();
99       }
100       bstrncpy(name, _("Client: "), sizeof(name));
101       bstrncat(name, jcr->client->name(), sizeof(name));
102
103       fd->set_source_address(director->DIRsrc_addr);
104       if (!fd->connect(jcr,retry_interval,max_retry_time, heart_beat, name, jcr->client->address,
105            NULL, jcr->client->FDport, verbose)) {
106          fd->close();
107          jcr->setJobStatus(JS_ErrorTerminated);
108          return 0;
109       }
110       Dmsg0(10, "Opened connection with File daemon\n");
111    }
112    fd->res = (RES *)jcr->client;      /* save resource in BSOCK */
113    jcr->setJobStatus(JS_Running);
114
115    if (!authenticate_file_daemon(jcr)) {
116       jcr->setJobStatus(JS_ErrorTerminated);
117       Dmsg0(10, "Authentication error with FD.\n");
118       return 0;
119    }
120
121    /*
122     * Now send JobId and authorization key
123     */
124    if (jcr->sd_auth_key == NULL) {
125       jcr->sd_auth_key = bstrdup("dummy");
126    }
127    fd->fsend(jobcmd, edit_int64(jcr->JobId, ed1), jcr->Job, jcr->VolSessionId,
128              jcr->VolSessionTime, jcr->sd_auth_key);
129    if (!jcr->keep_sd_auth_key && strcmp(jcr->sd_auth_key, "dummy")) {
130       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
131    }
132    Dmsg1(100, ">filed: %s", fd->msg);
133    if (bget_dirmsg(fd) > 0) {
134        Dmsg1(110, "<filed: %s", fd->msg);
135        if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
136           Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
137              jcr->client->hdr.name, fd->msg);
138           jcr->setJobStatus(JS_ErrorTerminated);
139           return 0;
140        } else if (jcr->db) {
141           CLIENT_DBR cr;
142           memset(&cr, 0, sizeof(cr));
143           bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
144           cr.AutoPrune = jcr->client->AutoPrune;
145           cr.FileRetention = jcr->client->FileRetention;
146           cr.JobRetention = jcr->client->JobRetention;
147           bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
148           if (!db_update_client_record(jcr, jcr->db, &cr)) {
149              Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
150                 db_strerror(jcr->db));
151           }
152        }
153    } else {
154       Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
155          fd->bstrerror());
156       jcr->setJobStatus(JS_ErrorTerminated);
157       return 0;
158    }
159    return 1;
160 }
161
162 /*
163  * This subroutine edits the last job start time into a
164  *   "since=date/time" buffer that is returned in the
165  *   variable since.  This is used for display purposes in
166  *   the job report.  The time in jcr->stime is later
167  *   passed to tell the File daemon what to do.
168  */
169 void get_level_since_time(JCR *jcr, char *since, int since_len)
170 {
171    int JobLevel;
172    bool have_full;
173    bool do_full = false;
174    bool do_diff = false;
175    utime_t now;
176    utime_t last_full_time = 0;
177    utime_t last_diff_time;
178    char prev_job[MAX_NAME_LENGTH];
179
180    since[0] = 0;
181    /* If job cloned and a since time already given, use it */
182    if (jcr->cloned && jcr->stime && jcr->stime[0]) {
183       bstrncpy(since, _(", since="), since_len);
184       bstrncat(since, jcr->stime, since_len);
185       return;
186    }
187    /* Make sure stime buffer is allocated */
188    if (!jcr->stime) {
189       jcr->stime = get_pool_memory(PM_MESSAGE);
190    }
191    jcr->PrevJob[0] = jcr->stime[0] = 0;
192    /*
193     * Lookup the last FULL backup job to get the time/date for a
194     * differential or incremental save.
195     */
196    switch (jcr->getJobLevel()) {
197    case L_DIFFERENTIAL:
198    case L_INCREMENTAL:
199       POOLMEM *stime = get_pool_memory(PM_MESSAGE);
200       /* Look up start time of last Full job */
201       now = (utime_t)time(NULL);
202       jcr->jr.JobId = 0;     /* flag to return since time */
203       /*
204        * This is probably redundant, but some of the code below
205        * uses jcr->stime, so don't remove unless you are sure.
206        */
207       if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime, jcr->PrevJob)) {
208          do_full = true;
209       }
210       have_full = db_find_last_job_start_time(jcr, jcr->db, &jcr->jr,
211                                               &stime, prev_job, L_FULL);
212       if (have_full) {
213          last_full_time = str_to_utime(stime);
214       } else {
215          do_full = true;               /* No full, upgrade to one */
216       }
217       Dmsg4(50, "have_full=%d do_full=%d now=%lld full_time=%lld\n", have_full,
218             do_full, now, last_full_time);
219       /* Make sure the last diff is recent enough */
220       if (have_full && jcr->getJobLevel() == L_INCREMENTAL && jcr->job->MaxDiffInterval > 0) {
221          /* Lookup last diff job */
222          if (db_find_last_job_start_time(jcr, jcr->db, &jcr->jr,
223                                          &stime, prev_job, L_DIFFERENTIAL)) {
224             last_diff_time = str_to_utime(stime);
225             /* If no Diff since Full, use Full time */
226             if (last_diff_time < last_full_time) {
227                last_diff_time = last_full_time;
228             }
229             Dmsg2(50, "last_diff_time=%lld last_full_time=%lld\n", last_diff_time,
230                   last_full_time);
231          } else {
232             /* No last differential, so use last full time */
233             last_diff_time = last_full_time;
234             Dmsg1(50, "No last_diff_time setting to full_time=%lld\n", last_full_time);
235          }
236          do_diff = ((now - last_diff_time) >= jcr->job->MaxDiffInterval);
237          Dmsg2(50, "do_diff=%d diffInter=%lld\n", do_diff, jcr->job->MaxDiffInterval);
238       }
239       /* Note, do_full takes precedence over do_diff */
240       if (have_full && jcr->job->MaxFullInterval > 0) {
241          do_full = ((now - last_full_time) >= jcr->job->MaxFullInterval);
242       }
243       free_pool_memory(stime);
244
245       if (do_full) {
246          /* No recent Full job found, so upgrade this one to Full */
247          Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
248          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found in catalog. Doing FULL backup.\n"));
249          bsnprintf(since, since_len, _(" (upgraded from %s)"),
250             level_to_str(jcr->getJobLevel()));
251          jcr->setJobLevel(jcr->jr.JobLevel = L_FULL);
252        } else if (do_diff) {
253          /* No recent diff job found, so upgrade this one to Diff */
254          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Differential backup found in catalog. Doing Differential backup.\n"));
255          bsnprintf(since, since_len, _(" (upgraded from %s)"),
256             level_to_str(jcr->getJobLevel()));
257          jcr->setJobLevel(jcr->jr.JobLevel = L_DIFFERENTIAL);
258       } else {
259          if (jcr->job->rerun_failed_levels) {
260             if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr,
261                                          jcr->stime, JobLevel)) {
262                Jmsg(jcr, M_INFO, 0, _("Prior failed job found in catalog. Upgrading to %s.\n"),
263                   level_to_str(JobLevel));
264                bsnprintf(since, since_len, _(" (upgraded from %s)"),
265                   level_to_str(jcr->getJobLevel()));
266                jcr->setJobLevel(jcr->jr.JobLevel = JobLevel);
267                jcr->jr.JobId = jcr->JobId;
268                break;
269             }
270          }
271          bstrncpy(since, _(", since="), since_len);
272          bstrncat(since, jcr->stime, since_len);
273       }
274       jcr->jr.JobId = jcr->JobId;
275       break;
276    }
277    Dmsg3(100, "Level=%c last start time=%s job=%s\n",
278          jcr->getJobLevel(), jcr->stime, jcr->PrevJob);
279 }
280
281 static void send_since_time(JCR *jcr)
282 {
283    BSOCK   *fd = jcr->file_bsock;
284    utime_t stime;
285    char ed1[50];
286
287    stime = str_to_utime(jcr->stime);
288    fd->fsend(levelcmd, "", NT_("since_utime "), edit_uint64(stime, ed1), 0,
289              NT_("prev_job="), jcr->PrevJob);
290    while (bget_dirmsg(fd) >= 0) {  /* allow him to poll us to sync clocks */
291       Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
292    }
293 }
294
295 bool send_bwlimit(JCR *jcr, const char *Job)
296 {
297    BSOCK *fd = jcr->file_bsock;
298    if (jcr->FDVersion >= 4) {
299       fd->fsend(bandwidthcmd, jcr->max_bandwidth, Job);
300       if (!response(jcr, fd, OKBandwidth, "Bandwidth", DISPLAY_ERROR)) {
301          jcr->max_bandwidth = 0;      /* can't set bandwidth limit */
302          return false;
303       }
304    }
305    return true;
306 }
307
308 /*
309  * Send level command to FD.
310  * Used for backup jobs and estimate command.
311  */
312 bool send_level_command(JCR *jcr)
313 {
314    BSOCK   *fd = jcr->file_bsock;
315    const char *accurate = jcr->accurate?"accurate_":"";
316    const char *not_accurate = "";
317    const char *rerunning = jcr->rerunning?" rerunning ":" ";
318    /*
319     * Send Level command to File daemon
320     */
321    switch (jcr->getJobLevel()) {
322    case L_BASE:
323       fd->fsend(levelcmd, not_accurate, "base", rerunning, 0, "", "");
324       break;
325    /* L_NONE is the console, sending something off to the FD */
326    case L_NONE:
327    case L_FULL:
328       fd->fsend(levelcmd, not_accurate, "full", rerunning, 0, "", "");
329       break;
330    case L_DIFFERENTIAL:
331       fd->fsend(levelcmd, accurate, "differential", rerunning, 0, "", "");
332       send_since_time(jcr);
333       break;
334    case L_INCREMENTAL:
335       fd->fsend(levelcmd, accurate, "incremental", rerunning, 0, "", "");
336       send_since_time(jcr);
337       break;
338    case L_SINCE:
339    default:
340       Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
341          jcr->getJobLevel(), jcr->getJobLevel());
342       return 0;
343    }
344    Dmsg1(120, ">filed: %s", fd->msg);
345    if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
346       return false;
347    }
348    return true;
349 }
350
351 /*
352  * Send either an Included or an Excluded list to FD
353  */
354 static bool send_fileset(JCR *jcr)
355 {
356    FILESET *fileset = jcr->fileset;
357    BSOCK   *fd = jcr->file_bsock;
358    STORE   *store = jcr->wstore;
359    int num;
360    bool include = true;
361
362    for ( ;; ) {
363       if (include) {
364          num = fileset->num_includes;
365       } else {
366          num = fileset->num_excludes;
367       }
368       for (int i=0; i<num; i++) {
369          char *item;
370          INCEXE *ie;
371          int j, k;
372
373          if (include) {
374             ie = fileset->include_items[i];
375             fd->fsend("I\n");
376          } else {
377             ie = fileset->exclude_items[i];
378             fd->fsend("E\n");
379          }
380          if (ie->ignoredir) {
381             fd->fsend("Z %s\n", ie->ignoredir);
382          }
383          for (j=0; j<ie->num_opts; j++) {
384             FOPTS *fo = ie->opts_list[j];
385             bool enhanced_wild = false;
386             bool stripped_opts = false;
387             bool compress_disabled = false;
388             char newopts[MAX_FOPTS];
389
390             for (k=0; fo->opts[k]!='\0'; k++) {
391                if (fo->opts[k]=='W') {
392                   enhanced_wild = true;
393                   break;
394                }
395             }
396
397             /*
398              * Strip out compression option Zn if disallowed
399              *  for this Storage.
400              * Strip out dedup option dn if old FD
401              */
402             bool strip_compress = store && !store->AllowCompress;
403             if (strip_compress || jcr->FDVersion >= 11) {
404                int j = 0;
405                for (k=0; fo->opts[k]!='\0'; k++) {
406                   /* Z compress option is followed by the single-digit compress level or 'o' */
407                   if (strip_compress && fo->opts[k]=='Z') {
408                      stripped_opts = true;
409                      compress_disabled = true;
410                      k++;                /* skip level */
411                   } else if (jcr->FDVersion < 11 && fo->opts[k]=='d') {
412                      stripped_opts = true;
413                      k++;              /* skip level */
414                   } else {
415                      newopts[j] = fo->opts[k];
416                      j++;
417                   }
418                }
419                newopts[j] = '\0';
420                if (compress_disabled) {
421                   Jmsg(jcr, M_INFO, 0,
422                       _("FD compression disabled for this Job because AllowCompress=No in Storage resource.\n") );
423                }
424             }
425             if (stripped_opts) {
426                /* Send the new trimmed option set without overwriting fo->opts */
427                fd->fsend("O %s\n", newopts);
428             } else {
429                /* Send the original options */
430                fd->fsend("O %s\n", fo->opts);
431             }
432             for (k=0; k<fo->regex.size(); k++) {
433                fd->fsend("R %s\n", fo->regex.get(k));
434             }
435             for (k=0; k<fo->regexdir.size(); k++) {
436                fd->fsend("RD %s\n", fo->regexdir.get(k));
437             }
438             for (k=0; k<fo->regexfile.size(); k++) {
439                fd->fsend("RF %s\n", fo->regexfile.get(k));
440             }
441             for (k=0; k<fo->wild.size(); k++) {
442                fd->fsend("W %s\n", fo->wild.get(k));
443             }
444             for (k=0; k<fo->wilddir.size(); k++) {
445                fd->fsend("WD %s\n", fo->wilddir.get(k));
446             }
447             for (k=0; k<fo->wildfile.size(); k++) {
448                fd->fsend("WF %s\n", fo->wildfile.get(k));
449             }
450             for (k=0; k<fo->wildbase.size(); k++) {
451                fd->fsend("W%c %s\n", enhanced_wild ? 'B' : 'F', fo->wildbase.get(k));
452             }
453             for (k=0; k<fo->base.size(); k++) {
454                fd->fsend("B %s\n", fo->base.get(k));
455             }
456             for (k=0; k<fo->fstype.size(); k++) {
457                fd->fsend("X %s\n", fo->fstype.get(k));
458             }
459             for (k=0; k<fo->drivetype.size(); k++) {
460                fd->fsend("XD %s\n", fo->drivetype.get(k));
461             }
462             if (fo->plugin) {
463                fd->fsend("G %s\n", fo->plugin);
464             }
465             if (fo->reader) {
466                fd->fsend("D %s\n", fo->reader);
467             }
468             if (fo->writer) {
469                fd->fsend("T %s\n", fo->writer);
470             }
471             fd->fsend("N\n");
472          }
473
474          for (j=0; j<ie->name_list.size(); j++) {
475             item = (char *)ie->name_list.get(j);
476             if (!send_list_item(jcr, "F ", item, fd)) {
477                goto bail_out;
478             }
479          }
480          fd->fsend("N\n");
481          for (j=0; j<ie->plugin_list.size(); j++) {
482             item = (char *)ie->plugin_list.get(j);
483             if (!send_list_item(jcr, "P ", item, fd)) {
484                goto bail_out;
485             }
486          }
487          fd->fsend("N\n");
488       }
489       if (!include) {                 /* If we just did excludes */
490          break;                       /*   all done */
491       }
492       include = false;                /* Now do excludes */
493    }
494
495    fd->signal(BNET_EOD);              /* end of data */
496    if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
497       goto bail_out;
498    }
499    return true;
500
501 bail_out:
502    jcr->setJobStatus(JS_ErrorTerminated);
503    return false;
504
505 }
506
507 static bool send_list_item(JCR *jcr, const char *code, char *item, BSOCK *fd)
508 {
509    BPIPE *bpipe;
510    FILE *ffd;
511    char buf[2000];
512    int optlen, stat;
513    char *p = item;
514
515    switch (*p) {
516    case '|':
517       p++;                      /* skip over the | */
518       fd->msg = edit_job_codes(jcr, fd->msg, p, "");
519       bpipe = open_bpipe(fd->msg, 0, "r");
520       if (!bpipe) {
521          berrno be;
522          Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
523             p, be.bstrerror());
524          return false;
525       }
526       bstrncpy(buf, code, sizeof(buf));
527       Dmsg1(500, "code=%s\n", buf);
528       optlen = strlen(buf);
529       while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
530          fd->msglen = Mmsg(fd->msg, "%s", buf);
531          Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
532          if (!fd->send()) {
533             close_bpipe(bpipe);
534             Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
535             return false;
536          }
537       }
538       if ((stat=close_bpipe(bpipe)) != 0) {
539          berrno be;
540          Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
541             p, be.bstrerror(stat));
542          return false;
543       }
544       break;
545    case '<':
546       p++;                      /* skip over < */
547       if ((ffd = fopen(p, "rb")) == NULL) {
548          berrno be;
549          Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
550             p, be.bstrerror());
551          return false;
552       }
553       bstrncpy(buf, code, sizeof(buf));
554       Dmsg1(500, "code=%s\n", buf);
555       optlen = strlen(buf);
556       while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
557          fd->msglen = Mmsg(fd->msg, "%s", buf);
558          if (!fd->send()) {
559             fclose(ffd);
560             Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
561             return false;
562          }
563       }
564       fclose(ffd);
565       break;
566    case '\\':
567       p++;                      /* skip over \ */
568       /* Note, fall through wanted */
569    default:
570       pm_strcpy(fd->msg, code);
571       fd->msglen = pm_strcat(fd->msg, p);
572       Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
573       if (!fd->send()) {
574          Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
575          return false;
576       }
577       break;
578    }
579    return true;
580 }
581
582
583 /*
584  * Send include list to File daemon
585  */
586 bool send_include_list(JCR *jcr)
587 {
588    BSOCK *fd = jcr->file_bsock;
589    if (jcr->fileset->new_include) {
590       fd->fsend(filesetcmd,
591                 jcr->fileset->enable_vss ? " vss=1" : "",
592                 jcr->fileset->enable_snapshot ? " snap=1" : "");
593       return send_fileset(jcr);
594    }
595    return true;
596 }
597
598 /*
599  * Send exclude list to File daemon
600  *   Under the new scheme, the Exclude list
601  *   is part of the FileSet sent with the
602  *   "include_list" above.
603  */
604 bool send_exclude_list(JCR *jcr)
605 {
606    return true;
607 }
608
609 /* TODO: drop this with runscript.old_proto in bacula 1.42 */
610 static char runbefore[]   = "RunBeforeJob %s\n";
611 static char runafter[]    = "RunAfterJob %s\n";
612 static char OKRunBefore[] = "2000 OK RunBefore\n";
613 static char OKRunAfter[]  = "2000 OK RunAfter\n";
614
615 int send_runscript_with_old_proto(JCR *jcr, int when, POOLMEM *msg)
616 {
617    int ret;
618    Dmsg1(120, "bdird: sending old runcommand to fd '%s'\n",msg);
619    if (when & SCRIPT_Before) {
620       jcr->file_bsock->fsend(runbefore, msg);
621       ret = response(jcr, jcr->file_bsock, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR);
622    } else {
623       jcr->file_bsock->fsend(runafter, msg);
624       ret = response(jcr, jcr->file_bsock, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR);
625    }
626    return ret;
627 } /* END OF TODO */
628
629 /*
630  * Send RunScripts to File daemon
631  * 1) We send all runscript to FD, they can be executed Before, After, or twice
632  * 2) Then, we send a "RunBeforeNow" command to the FD to tell him to do the
633  *    first run_script() call. (ie ClientRunBeforeJob)
634  */
635 int send_runscripts_commands(JCR *jcr)
636 {
637    POOLMEM *msg = get_pool_memory(PM_FNAME);
638    BSOCK *fd = jcr->file_bsock;
639    RUNSCRIPT *cmd;
640    bool launch_before_cmd = false;
641    POOLMEM *ehost = get_pool_memory(PM_FNAME);
642    int result;
643
644    Dmsg0(120, "bdird: sending runscripts to fd\n");
645
646    foreach_alist(cmd, jcr->job->RunScripts) {
647       if (cmd->can_run_at_level(jcr->getJobLevel()) && cmd->target) {
648          ehost = edit_job_codes(jcr, ehost, cmd->target, "");
649          Dmsg2(200, "bdird: runscript %s -> %s\n", cmd->target, ehost);
650
651          if (strcmp(ehost, jcr->client->name()) == 0) {
652             pm_strcpy(msg, cmd->command);
653             bash_spaces(msg);
654
655             Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", cmd->command);
656
657             /* TODO: remove this with bacula 1.42 */
658             if (cmd->old_proto) {
659                result = send_runscript_with_old_proto(jcr, cmd->when, msg);
660
661             } else {
662                fd->fsend(runscript, cmd->on_success,
663                                     cmd->on_failure,
664                                     cmd->fail_on_error,
665                                     cmd->when,
666                                     msg);
667
668                result = response(jcr, fd, OKRunScript, "RunScript", DISPLAY_ERROR);
669                launch_before_cmd = true;
670             }
671
672             if (!result) {
673                goto bail_out;
674             }
675          }
676          /* TODO : we have to play with other client */
677          /*
678            else {
679            send command to an other client
680            }
681          */
682       }
683    }
684
685    /* Tell the FD to execute the ClientRunBeforeJob */
686    if (launch_before_cmd) {
687       fd->fsend(runbeforenow);
688       if (!response(jcr, fd, OKRunBeforeNow, "RunBeforeNow", DISPLAY_ERROR)) {
689         goto bail_out;
690       }
691    }
692    free_pool_memory(msg);
693    free_pool_memory(ehost);
694    return 1;
695
696 bail_out:
697    Jmsg(jcr, M_FATAL, 0, _("Client \"%s\" RunScript failed.\n"), ehost);
698    free_pool_memory(msg);
699    free_pool_memory(ehost);
700    return 0;
701 }
702
703 struct OBJ_CTX {
704    JCR *jcr;
705    int count;
706 };
707
708 static int restore_object_handler(void *ctx, int num_fields, char **row)
709 {
710    OBJ_CTX *octx = (OBJ_CTX *)ctx;
711    JCR *jcr = octx->jcr;
712    BSOCK *fd;
713
714    fd = jcr->file_bsock;
715    if (jcr->is_job_canceled()) {
716       return 1;
717    }
718    /* Old File Daemon doesn't handle restore objects */
719    if (jcr->FDVersion < 3) {
720       Jmsg(jcr, M_WARNING, 0, _("Client \"%s\" may not be used to restore "
721                                 "this job. Please upgrade your client.\n"),
722            jcr->client->name());
723       return 1;
724    }
725
726    if (jcr->FDVersion < 5) {    /* Old version without PluginName */
727       fd->fsend("restoreobject JobId=%s %s,%s,%s,%s,%s,%s\n",
728                 row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
729    } else {
730       /* bash spaces from PluginName */
731       bash_spaces(row[9]);
732       fd->fsend("restoreobject JobId=%s %s,%s,%s,%s,%s,%s,%s\n",
733                 row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[9]);
734    }
735    Dmsg1(010, "Send obj hdr=%s", fd->msg);
736
737    fd->msglen = pm_strcpy(fd->msg, row[7]);
738    fd->send();                            /* send Object name */
739
740    Dmsg1(010, "Send obj: %s\n", fd->msg);
741
742 //   fd->msglen = str_to_uint64(row[1]);   /* object length */
743 //   Dmsg1(000, "obj size: %lld\n", (uint64_t)fd->msglen);
744
745    /* object */
746    db_unescape_object(jcr, jcr->db,
747                       row[8],                /* Object  */
748                       str_to_uint64(row[1]), /* Object length */
749                       &fd->msg, &fd->msglen);
750    fd->send();                           /* send object */
751    octx->count++;
752
753    if (debug_level > 100) {
754       for (int i=0; i < fd->msglen; i++)
755          if (!fd->msg[i])
756             fd->msg[i] = ' ';
757       Dmsg1(000, "Send obj: %s\n", fd->msg);
758    }
759
760    return 0;
761 }
762
763 /*
764  * Send the plugin Restore Objects, which allow the
765  *  plugin to get information early in the restore
766  *  process.  The RestoreObjects were created during
767  *  the backup by the plugin.
768  */
769 bool send_restore_objects(JCR *jcr)
770 {
771    char ed1[50];
772    POOL_MEM query(PM_MESSAGE);
773    BSOCK *fd;
774    OBJ_CTX octx;
775
776    if (!jcr->JobIds || !jcr->JobIds[0]) {
777       return true;
778    }
779    octx.jcr = jcr;
780    octx.count = 0;
781
782    /* restore_object_handler is called for each file found */
783
784    /* send restore objects for all jobs involved  */
785    Mmsg(query, get_restore_objects, jcr->JobIds, FT_RESTORE_FIRST);
786    db_sql_query(jcr->db, query.c_str(), restore_object_handler, (void *)&octx);
787
788    /* send config objects for the current restore job */
789    Mmsg(query, get_restore_objects,
790         edit_uint64(jcr->JobId, ed1), FT_PLUGIN_CONFIG_FILLED);
791    db_sql_query(jcr->db, query.c_str(), restore_object_handler, (void *)&octx);
792
793    /*
794     * Send to FD only if we have at least one restore object.
795     * This permits backward compatibility with older FDs.
796     */
797    if (octx.count > 0) {
798       fd = jcr->file_bsock;
799       fd->fsend("restoreobject end\n");
800       if (!response(jcr, fd, OKRestoreObject, "RestoreObject", DISPLAY_ERROR)) {
801          Jmsg(jcr, M_FATAL, 0, _("RestoreObject failed.\n"));
802          return false;
803       }
804    }
805    return true;
806 }
807
808 /*
809  * Send the plugin a list of component info files.  These
810  *  were files that were created during the backup for
811  *  the VSS plugin.  The list is a list of those component
812  *  files that have been chosen for restore.  We
813  *  send them before the Restore Objects.
814  */
815 bool send_component_info(JCR *jcr)
816 {
817    BSOCK *fd;
818    char buf[2000];
819    bool ok = true;
820
821    if (!jcr->component_fd) {
822       return true;           /* nothing to send */
823    }
824    /* Don't send if old version FD */
825    if (jcr->FDVersion < 6) {
826       goto bail_out;
827    }
828
829    rewind(jcr->component_fd);
830    fd = jcr->file_bsock;
831    fd->fsend(component_info);
832    while (fgets(buf, sizeof(buf), jcr->component_fd)) {
833       fd->fsend("%s", buf);
834       Dmsg1(050, "Send component_info to FD: %s\n", buf);
835    }
836    fd->signal(BNET_EOD);
837    if (!response(jcr, fd, OKComponentInfo, "ComponentInfo", DISPLAY_ERROR)) {
838       Jmsg(jcr, M_FATAL, 0, _("ComponentInfo failed.\n"));
839       ok = false;
840    }
841
842 bail_out:
843    fclose(jcr->component_fd);
844    jcr->component_fd = NULL;
845    unlink(jcr->component_fname);
846    free_and_null_pool_memory(jcr->component_fname);
847    return ok;
848 }
849
850 /*
851  * Read the attributes from the File daemon for
852  * a Verify job and store them in the catalog.
853  */
854 int get_attributes_and_put_in_catalog(JCR *jcr)
855 {
856    BSOCK   *fd;
857    int n = 0;
858    ATTR_DBR *ar = NULL;
859    char digest[MAXSTRING];
860
861    fd = jcr->file_bsock;
862    jcr->jr.FirstIndex = 1;
863    jcr->FileIndex = 0;
864    /* Start transaction allocates jcr->attr and jcr->ar if needed */
865    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
866    ar = jcr->ar;
867
868    Dmsg0(120, "bdird: waiting to receive file attributes\n");
869    /* Pickup file attributes and digest */
870    while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
871       uint32_t file_index;
872       int stream, len;
873       char *p, *fn;
874       char Digest[MAXSTRING];      /* either Verify opts or MD5/SHA1 digest */
875
876       /* Stop here if canceled */
877       if (jcr->is_job_canceled()) {
878          jcr->cached_attribute = false;
879          return 0;
880       }
881
882       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Digest)) != 3) {
883          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
884 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
885          jcr->setJobStatus(JS_ErrorTerminated);
886          jcr->cached_attribute = false;
887          return 0;
888       }
889       p = fd->msg;
890       /* The following three fields were sscanf'ed above so skip them */
891       skip_nonspaces(&p);             /* skip FileIndex */
892       skip_spaces(&p);
893       skip_nonspaces(&p);             /* skip Stream */
894       skip_spaces(&p);
895       skip_nonspaces(&p);             /* skip Opts_Digest */
896       p++;                            /* skip space */
897       Dmsg1(dbglvl, "Stream=%d\n", stream);
898       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
899          if (jcr->cached_attribute) {
900             Dmsg3(dbglvl, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname,
901                ar->attr);
902             if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
903                Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
904             }
905             jcr->cached_attribute = false;
906          }
907          /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
908          fn = jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
909          while (*p != 0) {
910             *fn++ = *p++;                /* copy filename */
911          }
912          *fn = *p++;                     /* term filename and point p to attribs */
913          pm_strcpy(jcr->attr, p);        /* save attributes */
914          jcr->JobFiles++;
915          jcr->FileIndex = file_index;
916          ar->attr = jcr->attr;
917          ar->fname = jcr->fname;
918          ar->FileIndex = file_index;
919          ar->Stream = stream;
920          ar->link = NULL;
921          ar->JobId = jcr->JobId;
922          ar->ClientId = jcr->ClientId;
923          ar->PathId = 0;
924          ar->FilenameId = 0;
925          ar->Digest = NULL;
926          ar->DigestType = CRYPTO_DIGEST_NONE;
927          ar->DeltaSeq = 0;
928          jcr->cached_attribute = true;
929
930          Dmsg2(dbglvl, "dird<filed: stream=%d %s\n", stream, jcr->fname);
931          Dmsg1(dbglvl, "dird<filed: attr=%s\n", ar->attr);
932          jcr->FileId = ar->FileId;
933       /*
934        * First, get STREAM_UNIX_ATTRIBUTES and fill ATTR_DBR structure
935        * Next, we CAN have a CRYPTO_DIGEST, so we fill ATTR_DBR with it (or not)
936        * When we get a new STREAM_UNIX_ATTRIBUTES, we known that we can add file to the catalog
937        * At the end, we have to add the last file
938        */
939       } else if (crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
940          if (jcr->FileIndex != (uint32_t)file_index) {
941             Jmsg3(jcr, M_ERROR, 0, _("%s index %d not same as attributes %d\n"),
942                stream_to_ascii(stream), file_index, jcr->FileIndex);
943             continue;
944          }
945          ar->Digest = digest;
946          ar->DigestType = crypto_digest_stream_type(stream);
947          db_escape_string(jcr, jcr->db, digest, Digest, strlen(Digest));
948          Dmsg4(dbglvl, "stream=%d DigestLen=%d Digest=%s type=%d\n", stream,
949                strlen(digest), digest, ar->DigestType);
950       }
951       jcr->jr.JobFiles = jcr->JobFiles = file_index;
952       jcr->jr.LastIndex = file_index;
953    }
954    if (fd->is_error()) {
955       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
956             fd->bstrerror());
957       jcr->cached_attribute = false;
958       return 0;
959    }
960    if (jcr->cached_attribute) {
961       Dmsg3(dbglvl, "Cached attr with digest. Stream=%d fname=%s attr=%s\n", ar->Stream,
962          ar->fname, ar->attr);
963       if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
964          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
965       }
966       jcr->cached_attribute = false;
967    }
968    jcr->setJobStatus(JS_Terminated);
969    return 1;
970 }