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