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