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