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