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