]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
Big backport from Enterprise
[bacula/bacula] / bacula / src / dird / fd_cmds.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 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_vfull = false;
175    bool do_diff = false;
176    utime_t now;
177    utime_t last_full_time = 0;
178    utime_t last_diff_time;
179    char prev_job[MAX_NAME_LENGTH];
180
181    since[0] = 0;
182    /* If job cloned and a since time already given, use it */
183    if (jcr->cloned && jcr->stime && jcr->stime[0]) {
184       bstrncpy(since, _(", since="), since_len);
185       bstrncat(since, jcr->stime, since_len);
186       return;
187    }
188    /* Make sure stime buffer is allocated */
189    if (!jcr->stime) {
190       jcr->stime = get_pool_memory(PM_MESSAGE);
191    }
192    jcr->PrevJob[0] = jcr->stime[0] = 0;
193    /*
194     * Lookup the last FULL backup job to get the time/date for a
195     * differential or incremental save.
196     */
197    switch (jcr->getJobLevel()) {
198    case L_DIFFERENTIAL:
199    case L_INCREMENTAL:
200       POOLMEM *stime = get_pool_memory(PM_MESSAGE);
201       /* Look up start time of last Full job */
202       now = (utime_t)time(NULL);
203       jcr->jr.JobId = 0;     /* flag to return since time */
204       /*
205        * This is probably redundant, but some of the code below
206        * uses jcr->stime, so don't remove unless you are sure.
207        */
208       if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime, jcr->PrevJob)) {
209          do_full = true;
210       }
211       have_full = db_find_last_job_start_time(jcr, jcr->db, &jcr->jr,
212                                               &stime, prev_job, L_FULL);
213       if (have_full) {
214          last_full_time = str_to_utime(stime);
215       } else {
216          do_full = true;               /* No full, upgrade to one */
217       }
218       Dmsg4(50, "have_full=%d do_full=%d now=%lld full_time=%lld\n", have_full,
219             do_full, now, last_full_time);
220       /* Make sure the last diff is recent enough */
221       if (have_full && jcr->getJobLevel() == L_INCREMENTAL && jcr->job->MaxDiffInterval > 0) {
222          /* Lookup last diff job */
223          if (db_find_last_job_start_time(jcr, jcr->db, &jcr->jr,
224                                          &stime, prev_job, L_DIFFERENTIAL)) {
225             last_diff_time = str_to_utime(stime);
226             /* If no Diff since Full, use Full time */
227             if (last_diff_time < last_full_time) {
228                last_diff_time = last_full_time;
229             }
230             Dmsg2(50, "last_diff_time=%lld last_full_time=%lld\n", last_diff_time,
231                   last_full_time);
232          } else {
233             /* No last differential, so use last full time */
234             last_diff_time = last_full_time;
235             Dmsg1(50, "No last_diff_time setting to full_time=%lld\n", last_full_time);
236          }
237          do_diff = ((now - last_diff_time) >= jcr->job->MaxDiffInterval);
238          Dmsg2(50, "do_diff=%d diffInter=%lld\n", do_diff, jcr->job->MaxDiffInterval);
239       }
240       /* Note, do_full takes precedence over do_vfull and do_diff */
241       if (have_full && jcr->job->MaxFullInterval > 0) {
242          do_full = ((now - last_full_time) >= jcr->job->MaxFullInterval);
243       }
244       else
245       if (have_full && jcr->job->MaxVirtualFullInterval > 0) {
246          do_vfull = ((now - last_full_time) >= jcr->job->MaxVirtualFullInterval);
247       }
248       
249       free_pool_memory(stime);
250
251       if (do_full) {
252          /* No recent Full job found, so upgrade this one to Full */
253          Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
254          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found in catalog. Doing FULL backup.\n"));
255          bsnprintf(since, since_len, _(" (upgraded from %s)"),
256             level_to_str(jcr->getJobLevel()));
257          jcr->setJobLevel(jcr->jr.JobLevel = L_FULL);
258       } else if (do_vfull) {
259          /* No recent Full job found, and MaxVirtualFull is set so upgrade this one to Virtual Full */
260          Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
261          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found in catalog. Doing Virtual FULL backup.\n"));
262          bsnprintf(since, since_len, _(" (upgraded from %s)"),
263             level_to_str(jcr->getJobLevel()));
264          jcr->setJobLevel(jcr->jr.JobLevel = L_VIRTUAL_FULL);
265       } else if (do_diff) {
266          /* No recent diff job found, so upgrade this one to Diff */
267          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Differential backup found in catalog. Doing Differential backup.\n"));
268          bsnprintf(since, since_len, _(" (upgraded from %s)"),
269             level_to_str(jcr->getJobLevel()));
270          jcr->setJobLevel(jcr->jr.JobLevel = L_DIFFERENTIAL);
271       } else {
272          if (jcr->job->rerun_failed_levels) {
273
274             POOLMEM *etime = get_pool_memory(PM_MESSAGE);
275
276             /* Get the end time of our most recent successfull backup for this job */
277             /* This will be used to see if there have been any failures since then */
278             if (db_find_last_job_end_time(jcr, jcr->db, &jcr->jr, &etime, prev_job)) {
279
280                /* See if there are any failed Differential/Full backups since the completion */
281                /* of our last successful backup for this job                                 */
282                if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr,
283                                          etime, JobLevel)) {
284                  /* If our job is an Incremental and we have a failed job then upgrade.              */
285                  /* If our job is a Differential and the failed job is a Full then upgrade.          */
286                  /* Otherwise there is no reason to upgrade.                                         */
287                  if ((jcr->getJobLevel() == L_INCREMENTAL) || 
288                      ((jcr->getJobLevel() == L_DIFFERENTIAL) && (JobLevel == L_FULL))) {
289                     Jmsg(jcr, M_INFO, 0, _("Prior failed job found in catalog. Upgrading to %s.\n"),
290                        level_to_str(JobLevel));
291                     bsnprintf(since, since_len, _(" (upgraded from %s)"),
292                              level_to_str(jcr->getJobLevel()));
293                     jcr->setJobLevel(jcr->jr.JobLevel = JobLevel);
294                     jcr->jr.JobId = jcr->JobId;
295                     break;
296                  }
297                }
298             }
299             free_pool_memory(etime);
300          }
301          bstrncpy(since, _(", since="), since_len);
302          bstrncat(since, jcr->stime, since_len);
303       }
304       jcr->jr.JobId = jcr->JobId;
305       break;
306    }
307    Dmsg3(100, "Level=%c last start time=%s job=%s\n",
308          jcr->getJobLevel(), jcr->stime, jcr->PrevJob);
309 }
310
311 static void send_since_time(JCR *jcr)
312 {
313    BSOCK   *fd = jcr->file_bsock;
314    utime_t stime;
315    char ed1[50];
316
317    stime = str_to_utime(jcr->stime);
318    fd->fsend(levelcmd, "", NT_("since_utime "), edit_uint64(stime, ed1), 0,
319              NT_("prev_job="), jcr->PrevJob);
320    while (bget_dirmsg(fd) >= 0) {  /* allow him to poll us to sync clocks */
321       Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
322    }
323 }
324
325 bool send_bwlimit(JCR *jcr, const char *Job)
326 {
327    BSOCK *fd = jcr->file_bsock;
328    if (jcr->FDVersion >= 4) {
329       fd->fsend(bandwidthcmd, jcr->max_bandwidth, Job);
330       if (!response(jcr, fd, OKBandwidth, "Bandwidth", DISPLAY_ERROR)) {
331          jcr->max_bandwidth = 0;      /* can't set bandwidth limit */
332          return false;
333       }
334    }
335    return true;
336 }
337
338 /*
339  * Send level command to FD.
340  * Used for backup jobs and estimate command.
341  */
342 bool send_level_command(JCR *jcr)
343 {
344    BSOCK   *fd = jcr->file_bsock;
345    const char *accurate = jcr->accurate?"accurate_":"";
346    const char *not_accurate = "";
347    const char *rerunning = jcr->rerunning?" rerunning ":" ";
348    /*
349     * Send Level command to File daemon
350     */
351    switch (jcr->getJobLevel()) {
352    case L_BASE:
353       fd->fsend(levelcmd, not_accurate, "base", rerunning, 0, "", "");
354       break;
355    /* L_NONE is the console, sending something off to the FD */
356    case L_NONE:
357    case L_FULL:
358       fd->fsend(levelcmd, not_accurate, "full", rerunning, 0, "", "");
359       break;
360    case L_DIFFERENTIAL:
361       fd->fsend(levelcmd, accurate, "differential", rerunning, 0, "", "");
362       send_since_time(jcr);
363       break;
364    case L_INCREMENTAL:
365       fd->fsend(levelcmd, accurate, "incremental", rerunning, 0, "", "");
366       send_since_time(jcr);
367       break;
368    case L_SINCE:
369    default:
370       Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
371          jcr->getJobLevel(), jcr->getJobLevel());
372       return 0;
373    }
374    Dmsg1(120, ">filed: %s", fd->msg);
375    if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
376       return false;
377    }
378    return true;
379 }
380
381 /*
382  * Send either an Included or an Excluded list to FD
383  */
384 static bool send_fileset(JCR *jcr)
385 {
386    FILESET *fileset = jcr->fileset;
387    BSOCK   *fd = jcr->file_bsock;
388    STORE   *store = jcr->wstore;
389    int num;
390    bool include = true;
391
392    for ( ;; ) {
393       if (include) {
394          num = fileset->num_includes;
395       } else {
396          num = fileset->num_excludes;
397       }
398       for (int i=0; i<num; i++) {
399          char *item;
400          INCEXE *ie;
401          int j, k;
402
403          if (include) {
404             ie = fileset->include_items[i];
405             fd->fsend("I\n");
406          } else {
407             ie = fileset->exclude_items[i];
408             fd->fsend("E\n");
409          }
410          if (ie->ignoredir) {
411             fd->fsend("Z %s\n", ie->ignoredir);
412          }
413          for (j=0; j<ie->num_opts; j++) {
414             FOPTS *fo = ie->opts_list[j];
415             bool enhanced_wild = false;
416             bool stripped_opts = false;
417             bool compress_disabled = false;
418             char newopts[MAX_FOPTS];
419
420             for (k=0; fo->opts[k]!='\0'; k++) {
421                if (fo->opts[k]=='W') {
422                   enhanced_wild = true;
423                   break;
424                }
425             }
426
427             /*
428              * Strip out compression option Zn if disallowed
429              *  for this Storage.
430              * Strip out dedup option dn if old FD
431              */
432             bool strip_compress = store && !store->AllowCompress;
433             if (strip_compress || jcr->FDVersion >= 11) {
434                int j = 0;
435                for (k=0; fo->opts[k]!='\0'; k++) {
436                   /* Z compress option is followed by the single-digit compress level or 'o' */
437                   if (strip_compress && fo->opts[k]=='Z') {
438                      stripped_opts = true;
439                      compress_disabled = true;
440                      k++;                /* skip level */
441                   } else if (jcr->FDVersion < 11 && fo->opts[k]=='d') {
442                      stripped_opts = true;
443                      k++;              /* skip level */
444                   } else {
445                      newopts[j] = fo->opts[k];
446                      j++;
447                   }
448                }
449                newopts[j] = '\0';
450                if (compress_disabled) {
451                   Jmsg(jcr, M_INFO, 0,
452                       _("FD compression disabled for this Job because AllowCompress=No in Storage resource.\n") );
453                }
454             }
455             if (stripped_opts) {
456                /* Send the new trimmed option set without overwriting fo->opts */
457                fd->fsend("O %s\n", newopts);
458             } else {
459                /* Send the original options */
460                fd->fsend("O %s\n", fo->opts);
461             }
462             for (k=0; k<fo->regex.size(); k++) {
463                fd->fsend("R %s\n", fo->regex.get(k));
464             }
465             for (k=0; k<fo->regexdir.size(); k++) {
466                fd->fsend("RD %s\n", fo->regexdir.get(k));
467             }
468             for (k=0; k<fo->regexfile.size(); k++) {
469                fd->fsend("RF %s\n", fo->regexfile.get(k));
470             }
471             for (k=0; k<fo->wild.size(); k++) {
472                fd->fsend("W %s\n", fo->wild.get(k));
473             }
474             for (k=0; k<fo->wilddir.size(); k++) {
475                fd->fsend("WD %s\n", fo->wilddir.get(k));
476             }
477             for (k=0; k<fo->wildfile.size(); k++) {
478                fd->fsend("WF %s\n", fo->wildfile.get(k));
479             }
480             for (k=0; k<fo->wildbase.size(); k++) {
481                fd->fsend("W%c %s\n", enhanced_wild ? 'B' : 'F', fo->wildbase.get(k));
482             }
483             for (k=0; k<fo->base.size(); k++) {
484                fd->fsend("B %s\n", fo->base.get(k));
485             }
486             for (k=0; k<fo->fstype.size(); k++) {
487                fd->fsend("X %s\n", fo->fstype.get(k));
488             }
489             for (k=0; k<fo->drivetype.size(); k++) {
490                fd->fsend("XD %s\n", fo->drivetype.get(k));
491             }
492             if (fo->plugin) {
493                fd->fsend("G %s\n", fo->plugin);
494             }
495             if (fo->reader) {
496                fd->fsend("D %s\n", fo->reader);
497             }
498             if (fo->writer) {
499                fd->fsend("T %s\n", fo->writer);
500             }
501             fd->fsend("N\n");
502          }
503
504          for (j=0; j<ie->name_list.size(); j++) {
505             item = (char *)ie->name_list.get(j);
506             if (!send_list_item(jcr, "F ", item, fd)) {
507                goto bail_out;
508             }
509          }
510          fd->fsend("N\n");
511          for (j=0; j<ie->plugin_list.size(); j++) {
512             item = (char *)ie->plugin_list.get(j);
513             if (!send_list_item(jcr, "P ", item, fd)) {
514                goto bail_out;
515             }
516          }
517          fd->fsend("N\n");
518       }
519       if (!include) {                 /* If we just did excludes */
520          break;                       /*   all done */
521       }
522       include = false;                /* Now do excludes */
523    }
524
525    fd->signal(BNET_EOD);              /* end of data */
526    if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
527       goto bail_out;
528    }
529    return true;
530
531 bail_out:
532    jcr->setJobStatus(JS_ErrorTerminated);
533    return false;
534
535 }
536
537 static bool send_list_item(JCR *jcr, const char *code, char *item, BSOCK *fd)
538 {
539    BPIPE *bpipe;
540    FILE *ffd;
541    char buf[2000];
542    int optlen, stat;
543    char *p = item;
544
545    switch (*p) {
546    case '|':
547       p++;                      /* skip over the | */
548       fd->msg = edit_job_codes(jcr, fd->msg, p, "");
549       bpipe = open_bpipe(fd->msg, 0, "r");
550       if (!bpipe) {
551          berrno be;
552          Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
553             p, be.bstrerror());
554          return false;
555       }
556       bstrncpy(buf, code, sizeof(buf));
557       Dmsg1(500, "code=%s\n", buf);
558       optlen = strlen(buf);
559       while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
560          fd->msglen = Mmsg(fd->msg, "%s", buf);
561          Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
562          if (!fd->send()) {
563             close_bpipe(bpipe);
564             Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
565             return false;
566          }
567       }
568       if ((stat=close_bpipe(bpipe)) != 0) {
569          berrno be;
570          Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
571             p, be.bstrerror(stat));
572          return false;
573       }
574       break;
575    case '<':
576       p++;                      /* skip over < */
577       if ((ffd = bfopen(p, "rb")) == NULL) {
578          berrno be;
579          Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
580             p, be.bstrerror());
581          return false;
582       }
583       bstrncpy(buf, code, sizeof(buf));
584       Dmsg1(500, "code=%s\n", buf);
585       optlen = strlen(buf);
586       while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
587          fd->msglen = Mmsg(fd->msg, "%s", buf);
588          if (!fd->send()) {
589             fclose(ffd);
590             Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
591             return false;
592          }
593       }
594       fclose(ffd);
595       break;
596    case '\\':
597       p++;                      /* skip over \ */
598       /* Note, fall through wanted */
599    default:
600       pm_strcpy(fd->msg, code);
601       fd->msglen = pm_strcat(fd->msg, p);
602       Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
603       if (!fd->send()) {
604          Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
605          return false;
606       }
607       break;
608    }
609    return true;
610 }
611
612
613 /*
614  * Send include list to File daemon
615  */
616 bool send_include_list(JCR *jcr)
617 {
618    BSOCK *fd = jcr->file_bsock;
619    if (jcr->fileset->new_include) {
620       fd->fsend(filesetcmd,
621                 jcr->fileset->enable_vss ? " vss=1" : "",
622                 jcr->fileset->enable_snapshot ? " snap=1" : "");
623       return send_fileset(jcr);
624    }
625    return true;
626 }
627
628 /*
629  *
630  * Send a include list with only one directory and recurse=no
631  * TODO: Need to display the plugin somewhere
632  *       The main point is that we don't introduce any protocol change
633  */
634 bool send_ls_fileset(JCR *jcr, const char *path)
635 {
636    BSOCK *fd = jcr->file_bsock;
637    fd->fsend(filesetcmd, "" /* no vss */, "" /* no snapshot */);
638
639    fd->fsend("I\n");
640    fd->fsend("O h\n");         /* Limit recursion to one directory */
641    fd->fsend("N\n");
642    fd->fsend("F %s\n", path);
643    fd->fsend("N\n");
644    fd->signal(BNET_EOD);              /* end of data */
645
646    if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
647       return false;
648    }
649    return true;
650 }
651
652 /*
653  * Send exclude list to File daemon
654  *   Under the new scheme, the Exclude list
655  *   is part of the FileSet sent with the
656  *   "include_list" above.
657  */
658 bool send_exclude_list(JCR *jcr)
659 {
660    return true;
661 }
662
663 /* TODO: drop this with runscript.old_proto in bacula 1.42 */
664 static char runbefore[]   = "RunBeforeJob %s\n";
665 static char runafter[]    = "RunAfterJob %s\n";
666 static char OKRunBefore[] = "2000 OK RunBefore\n";
667 static char OKRunAfter[]  = "2000 OK RunAfter\n";
668
669 int send_runscript_with_old_proto(JCR *jcr, int when, POOLMEM *msg)
670 {
671    int ret;
672    Dmsg1(120, "bdird: sending old runcommand to fd '%s'\n",msg);
673    if (when & SCRIPT_Before) {
674       jcr->file_bsock->fsend(runbefore, msg);
675       ret = response(jcr, jcr->file_bsock, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR);
676    } else {
677       jcr->file_bsock->fsend(runafter, msg);
678       ret = response(jcr, jcr->file_bsock, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR);
679    }
680    return ret;
681 } /* END OF TODO */
682
683 /*
684  * Send RunScripts to File daemon
685  * 1) We send all runscript to FD, they can be executed Before, After, or twice
686  * 2) Then, we send a "RunBeforeNow" command to the FD to tell him to do the
687  *    first run_script() call. (ie ClientRunBeforeJob)
688  */
689 int send_runscripts_commands(JCR *jcr)
690 {
691    POOLMEM *msg = get_pool_memory(PM_FNAME);
692    BSOCK *fd = jcr->file_bsock;
693    RUNSCRIPT *cmd;
694    bool launch_before_cmd = false;
695    POOLMEM *ehost = get_pool_memory(PM_FNAME);
696    int result;
697
698    Dmsg0(120, "bdird: sending runscripts to fd\n");
699    if (!jcr->job->RunScripts) {
700       goto norunscript;
701    }
702    foreach_alist(cmd, jcr->job->RunScripts) {
703       if (cmd->can_run_at_level(jcr->getJobLevel()) && cmd->target) {
704          ehost = edit_job_codes(jcr, ehost, cmd->target, "");
705          Dmsg2(200, "bdird: runscript %s -> %s\n", cmd->target, ehost);
706
707          if (strcmp(ehost, jcr->client->name()) == 0) {
708             pm_strcpy(msg, cmd->command);
709             bash_spaces(msg);
710
711             Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", cmd->command);
712
713             /* TODO: remove this with bacula 1.42 */
714             if (cmd->old_proto) {
715                result = send_runscript_with_old_proto(jcr, cmd->when, msg);
716
717             } else {
718                fd->fsend(runscript, cmd->on_success,
719                                     cmd->on_failure,
720                                     cmd->fail_on_error,
721                                     cmd->when,
722                                     msg);
723
724                result = response(jcr, fd, OKRunScript, "RunScript", DISPLAY_ERROR);
725                launch_before_cmd = true;
726             }
727
728             if (!result) {
729                goto bail_out;
730             }
731          }
732          /* TODO : we have to play with other client */
733          /*
734            else {
735            send command to an other client
736            }
737          */
738       }
739    }
740
741    /* Tell the FD to execute the ClientRunBeforeJob */
742    if (launch_before_cmd) {
743       fd->fsend(runbeforenow);
744       if (!response(jcr, fd, OKRunBeforeNow, "RunBeforeNow", DISPLAY_ERROR)) {
745         goto bail_out;
746       }
747    }
748 norunscript:
749    free_pool_memory(msg);
750    free_pool_memory(ehost);
751    return 1;
752
753 bail_out:
754    Jmsg(jcr, M_FATAL, 0, _("Client \"%s\" RunScript failed.\n"), ehost);
755    free_pool_memory(msg);
756    free_pool_memory(ehost);
757    return 0;
758 }
759
760 struct OBJ_CTX {
761    JCR *jcr;
762    int count;
763 };
764
765 static int restore_object_handler(void *ctx, int num_fields, char **row)
766 {
767    OBJ_CTX *octx = (OBJ_CTX *)ctx;
768    JCR *jcr = octx->jcr;
769    BSOCK *fd;
770
771    fd = jcr->file_bsock;
772    if (jcr->is_job_canceled()) {
773       return 1;
774    }
775    /* Old File Daemon doesn't handle restore objects */
776    if (jcr->FDVersion < 3) {
777       Jmsg(jcr, M_WARNING, 0, _("Client \"%s\" may not be used to restore "
778                                 "this job. Please upgrade your client.\n"),
779            jcr->client->name());
780       return 1;
781    }
782
783    if (jcr->FDVersion < 5) {    /* Old version without PluginName */
784       fd->fsend("restoreobject JobId=%s %s,%s,%s,%s,%s,%s\n",
785                 row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
786    } else {
787       /* bash spaces from PluginName */
788       bash_spaces(row[9]);
789       fd->fsend("restoreobject JobId=%s %s,%s,%s,%s,%s,%s,%s\n",
790                 row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[9]);
791    }
792    Dmsg1(010, "Send obj hdr=%s", fd->msg);
793
794    fd->msglen = pm_strcpy(fd->msg, row[7]);
795    fd->send();                            /* send Object name */
796
797    Dmsg1(010, "Send obj: %s\n", fd->msg);
798
799 //   fd->msglen = str_to_uint64(row[1]);   /* object length */
800 //   Dmsg1(000, "obj size: %lld\n", (uint64_t)fd->msglen);
801
802    /* object */
803    db_unescape_object(jcr, jcr->db,
804                       row[8],                /* Object  */
805                       str_to_uint64(row[1]), /* Object length */
806                       &fd->msg, &fd->msglen);
807    fd->send();                           /* send object */
808    octx->count++;
809
810    if (debug_level > 100) {
811       for (int i=0; i < fd->msglen; i++)
812          if (!fd->msg[i])
813             fd->msg[i] = ' ';
814       Dmsg1(000, "Send obj: %s\n", fd->msg);
815    }
816
817    return 0;
818 }
819
820 /*
821  * Send the plugin Restore Objects, which allow the
822  *  plugin to get information early in the restore
823  *  process.  The RestoreObjects were created during
824  *  the backup by the plugin.
825  */
826 bool send_restore_objects(JCR *jcr)
827 {
828    char ed1[50];
829    POOL_MEM query(PM_MESSAGE);
830    BSOCK *fd;
831    OBJ_CTX octx;
832
833    if (!jcr->JobIds || !jcr->JobIds[0]) {
834       return true;
835    }
836    octx.jcr = jcr;
837    octx.count = 0;
838
839    /* restore_object_handler is called for each file found */
840
841    /* send restore objects for all jobs involved  */
842    Mmsg(query, get_restore_objects, jcr->JobIds, FT_RESTORE_FIRST);
843    db_sql_query(jcr->db, query.c_str(), restore_object_handler, (void *)&octx);
844
845    /* send config objects for the current restore job */
846    Mmsg(query, get_restore_objects,
847         edit_uint64(jcr->JobId, ed1), FT_PLUGIN_CONFIG_FILLED);
848    db_sql_query(jcr->db, query.c_str(), restore_object_handler, (void *)&octx);
849
850    /*
851     * Send to FD only if we have at least one restore object.
852     * This permits backward compatibility with older FDs.
853     */
854    if (octx.count > 0) {
855       fd = jcr->file_bsock;
856       fd->fsend("restoreobject end\n");
857       if (!response(jcr, fd, OKRestoreObject, "RestoreObject", DISPLAY_ERROR)) {
858          Jmsg(jcr, M_FATAL, 0, _("RestoreObject failed.\n"));
859          return false;
860       }
861    }
862    return true;
863 }
864
865 /*
866  * Send the plugin a list of component info files.  These
867  *  were files that were created during the backup for
868  *  the VSS plugin.  The list is a list of those component
869  *  files that have been chosen for restore.  We
870  *  send them before the Restore Objects.
871  */
872 bool send_component_info(JCR *jcr)
873 {
874    BSOCK *fd;
875    char buf[2000];
876    bool ok = true;
877
878    if (!jcr->component_fd) {
879       return true;           /* nothing to send */
880    }
881    /* Don't send if old version FD */
882    if (jcr->FDVersion < 6) {
883       goto bail_out;
884    }
885
886    rewind(jcr->component_fd);
887    fd = jcr->file_bsock;
888    fd->fsend(component_info);
889    while (fgets(buf, sizeof(buf), jcr->component_fd)) {
890       fd->fsend("%s", buf);
891       Dmsg1(050, "Send component_info to FD: %s\n", buf);
892    }
893    fd->signal(BNET_EOD);
894    if (!response(jcr, fd, OKComponentInfo, "ComponentInfo", DISPLAY_ERROR)) {
895       Jmsg(jcr, M_FATAL, 0, _("ComponentInfo failed.\n"));
896       ok = false;
897    }
898
899 bail_out:
900    fclose(jcr->component_fd);
901    jcr->component_fd = NULL;
902    unlink(jcr->component_fname);
903    free_and_null_pool_memory(jcr->component_fname);
904    return ok;
905 }
906
907 /*
908  * Read the attributes from the File daemon for
909  * a Verify job and store them in the catalog.
910  */
911 int get_attributes_and_put_in_catalog(JCR *jcr)
912 {
913    BSOCK   *fd;
914    int n = 0;
915    ATTR_DBR *ar = NULL;
916    char digest[MAXSTRING];
917
918    fd = jcr->file_bsock;
919    jcr->jr.FirstIndex = 1;
920    jcr->FileIndex = 0;
921    /* Start transaction allocates jcr->attr and jcr->ar if needed */
922    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
923    ar = jcr->ar;
924
925    Dmsg0(120, "bdird: waiting to receive file attributes\n");
926    /* Pickup file attributes and digest */
927    while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
928       uint32_t file_index;
929       int stream, len;
930       char *p, *fn;
931       char Digest[MAXSTRING];      /* either Verify opts or MD5/SHA1 digest */
932
933       /* Stop here if canceled */
934       if (jcr->is_job_canceled()) {
935          jcr->cached_attribute = false;
936          return 0;
937       }
938
939       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Digest)) != 3) {
940          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
941 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
942          jcr->setJobStatus(JS_ErrorTerminated);
943          jcr->cached_attribute = false;
944          return 0;
945       }
946       p = fd->msg;
947       /* The following three fields were sscanf'ed above so skip them */
948       skip_nonspaces(&p);             /* skip FileIndex */
949       skip_spaces(&p);
950       skip_nonspaces(&p);             /* skip Stream */
951       skip_spaces(&p);
952       skip_nonspaces(&p);             /* skip Opts_Digest */
953       p++;                            /* skip space */
954       Dmsg1(dbglvl, "Stream=%d\n", stream);
955       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
956          if (jcr->cached_attribute) {
957             Dmsg3(dbglvl, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname,
958                ar->attr);
959             if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
960                Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
961             }
962             jcr->cached_attribute = false;
963          }
964          /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
965          fn = jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
966          while (*p != 0) {
967             *fn++ = *p++;                /* copy filename */
968          }
969          *fn = *p++;                     /* term filename and point p to attribs */
970          pm_strcpy(jcr->attr, p);        /* save attributes */
971          jcr->JobFiles++;
972          jcr->FileIndex = file_index;
973          ar->attr = jcr->attr;
974          ar->fname = jcr->fname;
975          ar->FileIndex = file_index;
976          ar->Stream = stream;
977          ar->link = NULL;
978          ar->JobId = jcr->JobId;
979          ar->ClientId = jcr->ClientId;
980          ar->PathId = 0;
981          ar->FilenameId = 0;
982          ar->Digest = NULL;
983          ar->DigestType = CRYPTO_DIGEST_NONE;
984          ar->DeltaSeq = 0;
985          jcr->cached_attribute = true;
986
987          Dmsg2(dbglvl, "dird<filed: stream=%d %s\n", stream, jcr->fname);
988          Dmsg1(dbglvl, "dird<filed: attr=%s\n", ar->attr);
989          jcr->FileId = ar->FileId;
990       /*
991        * First, get STREAM_UNIX_ATTRIBUTES and fill ATTR_DBR structure
992        * Next, we CAN have a CRYPTO_DIGEST, so we fill ATTR_DBR with it (or not)
993        * When we get a new STREAM_UNIX_ATTRIBUTES, we known that we can add file to the catalog
994        * At the end, we have to add the last file
995        */
996       } else if (crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
997          if (jcr->FileIndex != (uint32_t)file_index) {
998             Jmsg3(jcr, M_ERROR, 0, _("%s index %d not same as attributes %d\n"),
999                stream_to_ascii(stream), file_index, jcr->FileIndex);
1000             continue;
1001          }
1002          ar->Digest = digest;
1003          ar->DigestType = crypto_digest_stream_type(stream);
1004          db_escape_string(jcr, jcr->db, digest, Digest, strlen(Digest));
1005          Dmsg4(dbglvl, "stream=%d DigestLen=%d Digest=%s type=%d\n", stream,
1006                strlen(digest), digest, ar->DigestType);
1007       }
1008       jcr->jr.JobFiles = jcr->JobFiles = file_index;
1009       jcr->jr.LastIndex = file_index;
1010    }
1011    if (fd->is_error()) {
1012       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
1013             fd->bstrerror());
1014       jcr->cached_attribute = false;
1015       return 0;
1016    }
1017    if (jcr->cached_attribute) {
1018       Dmsg3(dbglvl, "Cached attr with digest. Stream=%d fname=%s attr=%s\n", ar->Stream,
1019          ar->fname, ar->attr);
1020       if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
1021          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
1022       }
1023       jcr->cached_attribute = false;
1024    }
1025    jcr->setJobStatus(JS_Terminated);
1026    return 1;
1027 }