]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
- Apply all of Preben's patches, but revert to old backup.c
[bacula/bacula] / bacula / src / dird / fd_cmds.c
1 /*
2  *
3  *   Bacula Director -- fd_cmds.c -- send commands to File daemon
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    This routine is run as a separate thread.  There may be more
8  *    work to be done to make it totally reentrant!!!!
9  *
10  *  Utility functions for sending info to File Daemon.
11  *   These functions are used by both backup and verify.
12  *
13  *   Version $Id$
14  */
15 /*
16    Copyright (C) 2000-2004 Kern Sibbald and John Walker
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of
21    the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public
29    License along with this program; if not, write to the Free
30    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31    MA 02111-1307, USA.
32
33  */
34
35 #include "bacula.h"
36 #include "dird.h"
37
38 /* Commands sent to File daemon */
39 static char inc[]         = "include\n";
40 static char exc[]         = "exclude\n";
41 static char fileset[]     = "fileset\n"; /* set full fileset */
42 static char jobcmd[]      = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
43 /* Note, mtime_only is not used here -- implemented as file option */
44 static char levelcmd[]    = "level = %s%s mtime_only=%d\n";
45 static char runbefore[]   = "RunBeforeJob %s\n";
46 static char runafter[]    = "RunAfterJob %s\n";
47
48
49 /* Responses received from File daemon */
50 static char OKinc[]       = "2000 OK include\n";
51 static char OKexc[]       = "2000 OK exclude\n";
52 static char OKjob[]       = "2000 OK Job";
53 static char OKbootstrap[] = "2000 OK bootstrap\n";
54 static char OKlevel[]     = "2000 OK level\n";
55 static char OKRunBefore[] = "2000 OK RunBefore\n";
56 static char OKRunAfter[]  = "2000 OK RunAfter\n";
57
58 /* Forward referenced functions */
59
60 /* External functions */
61 extern int debug_level;
62 extern DIRRES *director;
63 extern int FDConnectTimeout;
64
65 #define INC_LIST 0
66 #define EXC_LIST 1
67
68 /*
69  * Open connection with File daemon.
70  * Try connecting every retry_interval (default 10 sec), and
71  *   give up after max_retry_time (default 30 mins).
72  */
73
74 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
75                            int verbose)
76 {
77    BSOCK   *fd;
78
79    if (!jcr->file_bsock) {
80       fd = bnet_connect(jcr, retry_interval, max_retry_time,
81            _("File daemon"), jcr->client->address,
82            NULL, jcr->client->FDport, verbose);
83       if (fd == NULL) {
84          set_jcr_job_status(jcr, JS_ErrorTerminated);
85          return 0;
86       }
87       Dmsg0(10, "Opened connection with File daemon\n");
88    } else {
89       fd = jcr->file_bsock;           /* use existing connection */
90    }
91    fd->res = (RES *)jcr->client;      /* save resource in BSOCK */
92    jcr->file_bsock = fd;
93    set_jcr_job_status(jcr, JS_Running);
94
95    if (!authenticate_file_daemon(jcr)) {
96       set_jcr_job_status(jcr, JS_ErrorTerminated);
97       return 0;
98    }
99
100    /*
101     * Now send JobId and authorization key
102     */
103    bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
104       jcr->VolSessionTime, jcr->sd_auth_key);
105    if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
106       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
107    }
108    Dmsg1(100, ">filed: %s", fd->msg);
109    if (bget_dirmsg(fd) > 0) {
110        Dmsg1(110, "<filed: %s", fd->msg);
111        if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
112           Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
113              jcr->client->hdr.name, fd->msg);
114           set_jcr_job_status(jcr, JS_ErrorTerminated);
115           return 0;
116        } else if (jcr->db) {
117           CLIENT_DBR cr;
118           memset(&cr, 0, sizeof(cr));
119           bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
120           cr.AutoPrune = jcr->client->AutoPrune;
121           cr.FileRetention = jcr->client->FileRetention;
122           cr.JobRetention = jcr->client->JobRetention;
123           bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
124           if (!db_update_client_record(jcr, jcr->db, &cr)) {
125              Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
126                 db_strerror(jcr->db));
127           }
128        }
129    } else {
130       Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
131          bnet_strerror(fd));
132       set_jcr_job_status(jcr, JS_ErrorTerminated);
133       return 0;
134    }
135    return 1;
136 }
137
138 /*
139  * This subroutine edits the last job start time into a
140  *   "since=date/time" buffer that is returned in the
141  *   variable since.  This is used for display purposes in
142  *   the job report.  The time in jcr->stime is later
143  *   passed to tell the File daemon what to do.
144  */
145 void get_level_since_time(JCR *jcr, char *since, int since_len)
146 {
147    int JobLevel;
148    /* Lookup the last FULL backup job to get the time/date for a
149     * differential or incremental save.
150     */
151    if (!jcr->stime) {
152       jcr->stime = get_pool_memory(PM_MESSAGE);
153    }
154    jcr->stime[0] = 0;
155    since[0] = 0;
156    switch (jcr->JobLevel) {
157    case L_DIFFERENTIAL:
158    case L_INCREMENTAL:
159       /* Look up start time of last job */
160       jcr->jr.JobId = 0;     /* flag for db_find_job_start time */
161       if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
162          /* No job found, so upgrade this one to Full */
163          Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
164          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
165          bsnprintf(since, since_len, " (upgraded from %s)",
166             level_to_str(jcr->JobLevel));
167          jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
168       } else {
169          if (jcr->job->rerun_failed_levels) {
170             if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr, jcr->stime, JobLevel)) {
171                Jmsg(jcr, M_INFO, 0, _("Prior failed job found. Upgrading to %s.\n"),
172                   level_to_str(JobLevel));
173                bsnprintf(since, since_len, " (upgraded from %s)",
174                   level_to_str(jcr->JobLevel));
175                jcr->JobLevel = jcr->jr.JobLevel = JobLevel;
176                jcr->jr.JobId = jcr->JobId;
177                break;
178             }
179          }
180          bstrncpy(since, ", since=", since_len);
181          bstrncat(since, jcr->stime, since_len);
182       }
183       jcr->jr.JobId = jcr->JobId;
184       break;
185    }
186    Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
187 }
188
189
190 /*
191  * Send level command to FD.
192  * Used for backup jobs and estimate command.
193  */
194 int send_level_command(JCR *jcr)
195 {
196    BSOCK   *fd = jcr->file_bsock;
197    utime_t stime;
198    char ed1[50];
199    /*
200     * Send Level command to File daemon
201     */
202    switch (jcr->JobLevel) {
203    case L_BASE:
204       bnet_fsend(fd, levelcmd, "base", " ", 0);
205       break;
206    /* L_NONE is the console, sending something off to the FD */
207    case L_NONE:
208    case L_FULL:
209       bnet_fsend(fd, levelcmd, "full", " ", 0);
210       break;
211    case L_DIFFERENTIAL:
212    case L_INCREMENTAL:
213       stime = str_to_utime(jcr->stime);
214       bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
215       while (bget_dirmsg(fd) >= 0) {  /* allow him to poll us to sync clocks */
216          Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
217       }
218       break;
219    case L_SINCE:
220    default:
221       Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
222          jcr->JobLevel, jcr->JobLevel);
223       return 0;
224    }
225    Dmsg1(120, ">filed: %s", fd->msg);
226    if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
227       return 0;
228    }
229    return 1;
230 }
231
232
233 /*
234  * Send either an Included or an Excluded list to FD
235  */
236 static int send_list(JCR *jcr, int list)
237 {
238    FILESET *fileset;
239    BSOCK   *fd;
240    int num;
241
242    fd = jcr->file_bsock;
243    fileset = jcr->fileset;
244
245    if (list == INC_LIST) {
246       num = fileset->num_includes;
247    } else {
248       num = fileset->num_excludes;
249    }
250
251    for (int i=0; i<num; i++) {
252       BPIPE *bpipe;
253       FILE *ffd;
254       char buf[2000];
255       char *p;
256       int optlen, stat;
257       INCEXE *ie;
258
259
260       if (list == INC_LIST) {
261          ie = fileset->include_items[i];
262       } else {
263          ie = fileset->exclude_items[i];
264       }
265       for (int j=0; j<ie->name_list.size(); j++) {
266          p = (char *)ie->name_list.get(j);
267          switch (*p) {
268          case '|':
269             p++;                      /* skip over the | */
270             fd->msg = edit_job_codes(jcr, fd->msg, p, "");
271             bpipe = open_bpipe(fd->msg, 0, "r");
272             if (!bpipe) {
273                berrno be;
274                Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
275                   p, be.strerror());
276                goto bail_out;
277             }
278             /* Copy File options */
279             if (ie->num_opts) {
280                bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
281                bstrncat(buf, " ", sizeof(buf));
282             } else {
283                bstrncpy(buf, "0 ", sizeof(buf));
284             }
285             Dmsg1(500, "Opts=%s\n", buf);
286             optlen = strlen(buf);
287             while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
288                fd->msglen = Mmsg(fd->msg, "%s", buf);
289                Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
290                if (!bnet_send(fd)) {
291                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
292                   goto bail_out;
293                }
294             }
295             if ((stat=close_bpipe(bpipe)) != 0) {
296                berrno be;
297                Jmsg(jcr, M_FATAL, 0, _("Error running program %p: ERR=%s\n"),
298                   p, be.strerror(stat));
299                goto bail_out;
300             }
301             break;
302          case '<':
303             p++;                      /* skip over < */
304             if ((ffd = fopen(p, "r")) == NULL) {
305                Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
306                   list==INC_LIST?"included":"excluded", p, strerror(errno));
307                goto bail_out;
308             }
309             /* Copy File options */
310             if (ie->num_opts) {
311                bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
312                bstrncat(buf, " ", sizeof(buf));
313             } else {
314                bstrncpy(buf, "0 ", sizeof(buf));
315             }
316             Dmsg1(500, "Opts=%s\n", buf);
317             optlen = strlen(buf);
318             while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
319                fd->msglen = Mmsg(fd->msg, "%s", buf);
320                if (!bnet_send(fd)) {
321                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
322                   goto bail_out;
323                }
324             }
325             fclose(ffd);
326             break;
327          case '\\':
328             p++;                      /* skip over \ */
329             /* Note, fall through wanted */
330          default:
331             if (ie->num_opts) {
332                Dmsg2(500, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
333                pm_strcpy(fd->msg, ie->opts_list[0]->opts);
334                pm_strcat(fd->msg, " ");
335             } else {
336                pm_strcpy(fd->msg, "0 ");
337             }
338             fd->msglen = pm_strcat(fd->msg, p);
339             Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
340             if (!bnet_send(fd)) {
341                Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
342                goto bail_out;
343             }
344             break;
345          }
346       }
347    }
348    bnet_sig(fd, BNET_EOD);            /* end of data */
349    if (list == INC_LIST) {
350       if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
351          goto bail_out;
352       }
353    } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
354         goto bail_out;
355    }
356    return 1;
357
358 bail_out:
359    set_jcr_job_status(jcr, JS_ErrorTerminated);
360    return 0;
361
362 }
363
364
365 /*
366  * Send either an Included or an Excluded list to FD
367  */
368 static int send_fileset(JCR *jcr)
369 {
370    FILESET *fileset = jcr->fileset;
371    BSOCK   *fd = jcr->file_bsock;
372    int num;
373    bool include = true;
374
375    for ( ;; ) {
376       if (include) {
377          num = fileset->num_includes;
378       } else {
379          num = fileset->num_excludes;
380       }
381       for (int i=0; i<num; i++) {
382          BPIPE *bpipe;
383          FILE *ffd;
384          char buf[2000];
385          char *p;
386          int optlen, stat;
387          INCEXE *ie;
388          int j, k;
389
390          if (include) {
391             ie = fileset->include_items[i];
392             bnet_fsend(fd, "I\n");
393          } else {
394             ie = fileset->exclude_items[i];
395             bnet_fsend(fd, "E\n");
396          }
397          for (j=0; j<ie->num_opts; j++) {
398             FOPTS *fo = ie->opts_list[j];
399             bnet_fsend(fd, "O %s\n", fo->opts);
400             for (k=0; k<fo->regex.size(); k++) {
401                bnet_fsend(fd, "R %s\n", fo->regex.get(k));
402             }
403             for (k=0; k<fo->regexdir.size(); k++) {
404                bnet_fsend(fd, "RD %s\n", fo->regexdir.get(k));
405             }
406             for (k=0; k<fo->regexfile.size(); k++) {
407                bnet_fsend(fd, "RF %s\n", fo->regexfile.get(k));
408             }
409             for (k=0; k<fo->wild.size(); k++) {
410                bnet_fsend(fd, "W %s\n", fo->wild.get(k));
411             }
412             for (k=0; k<fo->wilddir.size(); k++) {
413                bnet_fsend(fd, "WD %s\n", fo->wilddir.get(k));
414             }
415             for (k=0; k<fo->wildfile.size(); k++) {
416                bnet_fsend(fd, "WF %s\n", fo->wildfile.get(k));
417             }
418             for (k=0; k<fo->base.size(); k++) {
419                bnet_fsend(fd, "B %s\n", fo->base.get(k));
420             }
421             for (k=0; k<fo->fstype.size(); k++) {
422                bnet_fsend(fd, "X %s\n", fo->fstype.get(k));
423             }
424             if (fo->reader) {
425                bnet_fsend(fd, "D %s\n", fo->reader);
426             }
427             if (fo->writer) {
428                bnet_fsend(fd, "T %s\n", fo->writer);
429             }
430             bnet_fsend(fd, "N\n");
431          }
432
433          for (j=0; j<ie->name_list.size(); j++) {
434             p = (char *)ie->name_list.get(j);
435             switch (*p) {
436             case '|':
437                p++;                      /* skip over the | */
438                fd->msg = edit_job_codes(jcr, fd->msg, p, "");
439                bpipe = open_bpipe(fd->msg, 0, "r");
440                if (!bpipe) {
441                   berrno be;
442                   Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
443                      p, be.strerror());
444                   goto bail_out;
445                }
446                bstrncpy(buf, "F ", sizeof(buf));
447                Dmsg1(500, "Opts=%s\n", buf);
448                optlen = strlen(buf);
449                while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
450                   fd->msglen = Mmsg(fd->msg, "%s", buf);
451                   Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
452                   if (!bnet_send(fd)) {
453                      Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
454                      goto bail_out;
455                   }
456                }
457                if ((stat=close_bpipe(bpipe)) != 0) {
458                   berrno be;
459                   Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
460                      p, be.strerror(stat));
461                   goto bail_out;
462                }
463                break;
464             case '<':
465                p++;                      /* skip over < */
466                if ((ffd = fopen(p, "r")) == NULL) {
467                   berrno be;
468                   Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
469                      p, be.strerror());
470                   goto bail_out;
471                }
472                bstrncpy(buf, "F ", sizeof(buf));
473                Dmsg1(500, "Opts=%s\n", buf);
474                optlen = strlen(buf);
475                while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
476                   fd->msglen = Mmsg(fd->msg, "%s", buf);
477                   if (!bnet_send(fd)) {
478                      Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
479                      goto bail_out;
480                   }
481                }
482                fclose(ffd);
483                break;
484             case '\\':
485                p++;                      /* skip over \ */
486                /* Note, fall through wanted */
487             default:
488                pm_strcpy(fd->msg, "F ");
489                fd->msglen = pm_strcat(fd->msg, p);
490                Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
491                if (!bnet_send(fd)) {
492                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
493                   goto bail_out;
494                }
495                break;
496             }
497          }
498          bnet_fsend(fd, "N\n");
499       }
500       if (!include) {                 /* If we just did excludes */
501          break;                       /*   all done */
502       }
503       include = false;                /* Now do excludes */
504    }
505
506    bnet_sig(fd, BNET_EOD);            /* end of data */
507    if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
508       goto bail_out;
509    }
510    return 1;
511
512 bail_out:
513    set_jcr_job_status(jcr, JS_ErrorTerminated);
514    return 0;
515
516 }
517
518
519 /*
520  * Send include list to File daemon
521  */
522 int send_include_list(JCR *jcr)
523 {
524    BSOCK *fd = jcr->file_bsock;
525    if (jcr->fileset->new_include) {
526       bnet_fsend(fd, fileset);
527       return send_fileset(jcr);
528    } else {
529       bnet_fsend(fd, inc);
530    }
531    return send_list(jcr, INC_LIST);
532 }
533
534
535 /*
536  * Send exclude list to File daemon
537  */
538 int send_exclude_list(JCR *jcr)
539 {
540    BSOCK *fd = jcr->file_bsock;
541    if (jcr->fileset->new_include) {
542       return 1;
543    }
544    bnet_fsend(fd, exc);
545    return send_list(jcr, EXC_LIST);
546 }
547
548
549 /*
550  * Send bootstrap file if any to the File daemon.
551  *  This is used for restore and verify VolumeToCatalog
552  */
553 int send_bootstrap_file(JCR *jcr)
554 {
555    FILE *bs;
556    char buf[1000];
557    BSOCK *fd = jcr->file_bsock;
558    const char *bootstrap = "bootstrap\n";
559
560    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
561    if (!jcr->RestoreBootstrap) {
562       return 1;
563    }
564    bs = fopen(jcr->RestoreBootstrap, "r");
565    if (!bs) {
566       berrno be;
567       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
568          jcr->RestoreBootstrap, be.strerror());
569       set_jcr_job_status(jcr, JS_ErrorTerminated);
570       return 0;
571    }
572    bnet_fsend(fd, bootstrap);
573    while (fgets(buf, sizeof(buf), bs)) {
574       bnet_fsend(fd, "%s", buf);
575    }
576    bnet_sig(fd, BNET_EOD);
577    fclose(bs);
578    if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
579       set_jcr_job_status(jcr, JS_ErrorTerminated);
580       return 0;
581    }
582    return 1;
583 }
584
585 /*
586  * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
587  */
588 int send_run_before_and_after_commands(JCR *jcr)
589 {
590    POOLMEM *msg = get_pool_memory(PM_FNAME);
591    BSOCK *fd = jcr->file_bsock;
592    if (jcr->job->ClientRunBeforeJob) {
593       pm_strcpy(msg, jcr->job->ClientRunBeforeJob);
594       bash_spaces(msg);
595       bnet_fsend(fd, runbefore, msg);
596       if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
597          set_jcr_job_status(jcr, JS_ErrorTerminated);
598          free_pool_memory(msg);
599          return 0;
600       }
601    }
602    if (jcr->job->ClientRunAfterJob) {
603       fd->msglen = pm_strcpy(msg, jcr->job->ClientRunAfterJob);
604       bash_spaces(msg);
605       bnet_fsend(fd, runafter, msg);
606       if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
607          set_jcr_job_status(jcr, JS_ErrorTerminated);
608          free_pool_memory(msg);
609          return 0;
610       }
611    }
612    free_pool_memory(msg);
613    return 1;
614 }
615
616
617 /*
618  * Read the attributes from the File daemon for
619  * a Verify job and store them in the catalog.
620  */
621 int get_attributes_and_put_in_catalog(JCR *jcr)
622 {
623    BSOCK   *fd;
624    int n = 0;
625    ATTR_DBR ar;
626
627    fd = jcr->file_bsock;
628    jcr->jr.FirstIndex = 1;
629    memset(&ar, 0, sizeof(ar));
630    jcr->FileIndex = 0;
631
632    Dmsg0(120, "bdird: waiting to receive file attributes\n");
633    /* Pickup file attributes and signature */
634    while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
635
636    /*****FIXME****** improve error handling to stop only on
637     * really fatal problems, or the number of errors is too
638     * large.
639     */
640       long file_index;
641       int stream, len;
642       char *attr, *p, *fn;
643       char Opts_SIG[MAXSTRING];      /* either Verify opts or MD5/SHA1 signature */
644       char SIG[MAXSTRING];
645
646       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
647       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
648          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
649 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
650          set_jcr_job_status(jcr, JS_ErrorTerminated);
651          return 0;
652       }
653       p = fd->msg;
654       skip_nonspaces(&p);             /* skip FileIndex */
655       skip_spaces(&p);
656       skip_nonspaces(&p);             /* skip Stream */
657       skip_spaces(&p);
658       skip_nonspaces(&p);             /* skip Opts_SHA1 */
659       p++;                            /* skip space */
660       fn = jcr->fname;
661       while (*p != 0) {
662          *fn++ = *p++;                /* copy filename */
663       }
664       *fn = *p++;                     /* term filename and point to attribs */
665       attr = p;
666
667       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
668          jcr->JobFiles++;
669          jcr->FileIndex = file_index;
670          ar.attr = attr;
671          ar.fname = jcr->fname;
672          ar.FileIndex = file_index;
673          ar.Stream = stream;
674          ar.link = NULL;
675          ar.JobId = jcr->JobId;
676          ar.ClientId = jcr->ClientId;
677          ar.PathId = 0;
678          ar.FilenameId = 0;
679
680          Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
681          Dmsg1(120, "dird<filed: attr=%s\n", attr);
682
683          if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
684             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
685             set_jcr_job_status(jcr, JS_Error);
686             continue;
687          }
688          jcr->FileId = ar.FileId;
689       } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
690          if (jcr->FileIndex != (uint32_t)file_index) {
691             Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
692                file_index, jcr->FileIndex);
693             set_jcr_job_status(jcr, JS_Error);
694             continue;
695          }
696          db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
697          Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
698          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
699                    stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
700             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
701             set_jcr_job_status(jcr, JS_Error);
702          }
703       }
704       jcr->jr.JobFiles = jcr->JobFiles = file_index;
705       jcr->jr.LastIndex = file_index;
706    }
707    if (is_bnet_error(fd)) {
708       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
709                         bnet_strerror(fd));
710       set_jcr_job_status(jcr, JS_ErrorTerminated);
711       return 0;
712    }
713
714    set_jcr_job_status(jcr, JS_Terminated);
715    return 1;
716 }