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