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