]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
572b2451636db42b00e56a2d64f2f46e39321803
[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       set_jcr_job_status(jcr, 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    set_jcr_job_status(jcr, JS_Running);
77
78    if (!authenticate_file_daemon(jcr)) {
79       set_jcr_job_status(jcr, 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    if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
89       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
90    }
91    Dmsg1(100, ">filed: %s", fd->msg);
92    if (bnet_recv(fd) > 0) {
93        Dmsg1(110, "<filed: %s", fd->msg);
94        if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
95           Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"), 
96              jcr->client->hdr.name, fd->msg);
97           set_jcr_job_status(jcr, JS_ErrorTerminated);
98           return 0;
99        } else {
100           /***** ***FIXME***** update Client Uname */
101        }
102    } else {
103       Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
104          bnet_strerror(fd));
105       set_jcr_job_status(jcr, JS_ErrorTerminated);
106       return 0;
107    }
108    return 1;
109 }
110
111
112 /*
113  * Send include list to File daemon
114  */
115 int send_include_list(JCR *jcr)
116 {
117    FILESET *fileset;
118    BSOCK   *fd;
119    int i;   
120
121    fd = jcr->file_bsock;
122    fileset = jcr->fileset;
123
124    fd->msglen = sprintf(fd->msg, inc);
125    bnet_send(fd);
126    for (i=0; i < fileset->num_includes; i++) {
127       BPIPE *bpipe;
128       FILE *ffd;
129       char buf[1000];
130       char *p;
131       int optlen, stat;
132       INCEXE *ie;
133
134       Dmsg1(120, "dird>filed: include file: %s\n", fileset->include_array[i]);
135       ie = fileset->include_array[i];
136       p = ie->name;
137       switch (*p++) {
138       case '|':
139          fd->msg = edit_job_codes(jcr, fd->msg, p, "");
140          bpipe = open_bpipe(fd->msg, 0, "r");
141          if (!bpipe) {
142             Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
143                p, strerror(errno));
144             goto bail_out;
145          }
146          /* Copy File options */
147          strcpy(buf, ie->opts);
148          optlen = strlen(buf);
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          strcpy(buf, ie->opts);
171          optlen = strlen(buf);
172          while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
173             fd->msglen = Mmsg(&fd->msg, "%s", buf);
174             if (!bnet_send(fd)) {
175                Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
176                goto bail_out;
177             }
178          }
179          fclose(ffd);
180          break;
181       default:
182          strcpy(fd->msg, ie->opts);
183          pm_strcat(&fd->msg, ie->name);
184          Dmsg1(000, "Include name=%s\n", ie->name);
185          fd->msglen = strlen(fd->msg);
186          if (!bnet_send(fd)) {
187             Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
188             goto bail_out;
189          }
190          break;
191       }
192    }
193    bnet_sig(fd, BNET_EOD);            /* end of data */
194    if (!response(fd, OKinc, "Include")) {
195       goto bail_out;
196    }
197    return 1;
198
199 bail_out:
200    set_jcr_job_status(jcr, JS_ErrorTerminated);
201    return 0;
202
203 }
204
205 /*
206  * Send exclude list to File daemon 
207  */
208 int send_exclude_list(JCR *jcr)
209 {
210    FILESET *fileset;
211    BSOCK   *fd;
212    int i;
213
214    fd = jcr->file_bsock;
215    fileset = jcr->fileset;
216
217    fd->msglen = sprintf(fd->msg, exc);
218    bnet_send(fd);
219    for (i=0; i < fileset->num_excludes; i++) {
220       pm_strcpy(&fd->msg, fileset->exclude_array[i]->name);
221       fd->msglen = strlen(fd->msg);
222       Dmsg1(000, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]->name);
223       if (!bnet_send(fd)) {
224          Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
225          set_jcr_job_status(jcr, JS_ErrorTerminated);
226          return 0;
227       }
228    }
229    bnet_sig(fd, BNET_EOD);
230    if (!response(fd, OKexc, "Exclude")) {
231       set_jcr_job_status(jcr, JS_ErrorTerminated);
232       return 0;
233    }
234    return 1;
235 }
236
237
238 /* 
239  * Read the attributes from the File daemon for
240  * a Verify job and store them in the catalog.
241  */
242 int get_attributes_and_put_in_catalog(JCR *jcr)
243 {
244    BSOCK   *fd;
245    int n = 0;
246    ATTR_DBR ar;
247
248    fd = jcr->file_bsock;
249    jcr->jr.FirstIndex = 1;
250    memset(&ar, 0, sizeof(ar));
251    jcr->FileIndex = 0;
252
253    Dmsg0(120, "bdird: waiting to receive file attributes\n");
254    /* Pickup file attributes and signature */
255    while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
256
257    /*****FIXME****** improve error handling to stop only on 
258     * really fatal problems, or the number of errors is too
259     * large.
260     */
261       long file_index;
262       int stream, len;
263       char *attr, *p, *fn;
264       char Opts_SIG[MAXSTRING];      /* either Verify opts or MD5/SHA1 signature */
265       char SIG[MAXSTRING];
266
267       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
268       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
269          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
270 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
271          set_jcr_job_status(jcr, JS_ErrorTerminated);
272          return 0;
273       }
274       p = fd->msg;
275       skip_nonspaces(&p);             /* skip FileIndex */
276       skip_spaces(&p);
277       skip_nonspaces(&p);             /* skip Stream */
278       skip_spaces(&p);
279       skip_nonspaces(&p);             /* skip Opts_SHA1 */   
280       p++;                            /* skip space */
281       fn = jcr->fname;
282       while (*p != 0) {
283          *fn++ = *p++;                /* copy filename */
284       }
285       *fn = *p++;                     /* term filename and point to attribs */
286       attr = p;
287
288       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
289          jcr->JobFiles++;
290          jcr->FileIndex = file_index;
291          ar.attr = attr;
292          ar.fname = jcr->fname;
293          ar.FileIndex = file_index;
294          ar.Stream = stream;
295          ar.link = NULL;
296          ar.JobId = jcr->JobId;
297          ar.ClientId = jcr->ClientId;
298          ar.PathId = 0;
299          ar.FilenameId = 0;
300
301          Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
302          Dmsg1(120, "dird<filed: attr=%s\n", attr);
303
304          if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
305             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
306             set_jcr_job_status(jcr, JS_Error);
307             continue;
308          }
309          jcr->FileId = ar.FileId;
310       } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
311          if (jcr->FileIndex != (uint32_t)file_index) {
312             Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
313                file_index, jcr->FileIndex);
314             set_jcr_job_status(jcr, JS_Error);
315             continue;
316          }
317          db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
318          Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
319          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG, 
320                    stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
321             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
322             set_jcr_job_status(jcr, JS_Error);
323          }
324       }
325       jcr->jr.JobFiles = jcr->JobFiles = file_index;
326       jcr->jr.LastIndex = file_index;
327    } 
328    if (is_bnet_error(fd)) {
329       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
330                         bnet_strerror(fd));
331       set_jcr_job_status(jcr, JS_ErrorTerminated);
332       return 0;
333    }
334
335    set_jcr_job_status(jcr, JS_Terminated);
336    return 1;
337 }