]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
3f79864aaa516b598e1827654a5a0faeb0e228de
[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
120    fd = jcr->file_bsock;
121    fileset = jcr->fileset;
122
123    fd->msglen = sprintf(fd->msg, inc);
124    bnet_send(fd);
125    for (int i=0; i < fileset->num_includes; i++) {
126       BPIPE *bpipe;
127       FILE *ffd;
128       char buf[1000];
129       char *p;
130       int optlen, stat;
131       INCEXE *ie;
132
133       ie = fileset->include_items[i];
134       for (int j=0; j<ie->num_names; j++) {
135          p = ie->name_list[j];
136          switch (*p++) {
137          case '|':
138             fd->msg = edit_job_codes(jcr, fd->msg, p, "");
139             bpipe = open_bpipe(fd->msg, 0, "r");
140             if (!bpipe) {
141                Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
142                   p, strerror(errno));
143                goto bail_out;
144             }
145             /* Copy File options */
146             if (ie->fileopts) {
147                strcpy(buf, ie->fileopts->opts);
148             } else {
149                strcpy(buf, ie->opts);
150             }
151             strcat(buf, " ");
152             optlen = strlen(buf);
153             while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
154                fd->msglen = Mmsg(&fd->msg, "%s", buf);
155                Dmsg2(200, "Including len=%d: %s", fd->msglen, fd->msg);
156                if (!bnet_send(fd)) {
157                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
158                   goto bail_out;
159                }
160             }
161             if ((stat=close_bpipe(bpipe)) != 0) {
162                Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
163                   p, stat, strerror(errno));
164                goto bail_out;
165             }
166             break;
167          case '<':
168             if ((ffd = fopen(p, "r")) == NULL) {
169                Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
170                   p, strerror(errno));
171                goto bail_out;
172             }
173             /* Copy File options */
174             if (ie->fileopts) {
175                strcpy(buf, ie->fileopts->opts);
176             } else {
177                strcpy(buf, ie->opts);
178             }
179             strcat(buf, " ");
180             optlen = strlen(buf);
181             while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
182                fd->msglen = Mmsg(&fd->msg, "%s", buf);
183                if (!bnet_send(fd)) {
184                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
185                   goto bail_out;
186                }
187             }
188             fclose(ffd);
189             break;
190          default:
191             if (ie->fileopts) {
192                pm_strcpy(&fd->msg, ie->fileopts->opts);
193                Dmsg1(200, "Fileopts=%s\n", fd->msg);
194             } else {
195                pm_strcpy(&fd->msg, ie->opts);
196             }
197             pm_strcat(&fd->msg, " ");
198             pm_strcat(&fd->msg, ie->name_list[j]);
199             Dmsg1(200, "Include name=%s\n", fd->msg);
200             fd->msglen = strlen(fd->msg);
201             if (!bnet_send(fd)) {
202                Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
203                goto bail_out;
204             }
205             break;
206          }
207       }
208    }
209    bnet_sig(fd, BNET_EOD);            /* end of data */
210    if (!response(fd, OKinc, "Include")) {
211       goto bail_out;
212    }
213    return 1;
214
215 bail_out:
216    set_jcr_job_status(jcr, JS_ErrorTerminated);
217    return 0;
218
219 }
220
221 /*
222  * Send exclude list to File daemon 
223  */
224 int send_exclude_list(JCR *jcr)
225 {
226    FILESET *fileset;
227    BSOCK   *fd;
228
229    fd = jcr->file_bsock;
230    fileset = jcr->fileset;
231
232    fd->msglen = sprintf(fd->msg, exc);
233    bnet_send(fd);
234    for (int i=0; i < fileset->num_excludes; i++) {
235       INCEXE *ie;
236       ie = fileset->exclude_items[i];
237       for (int j=0; j<ie->num_names; j++) {
238          pm_strcpy(&fd->msg, ie->name_list[j]);
239          fd->msglen = strlen(fd->msg);
240          Dmsg1(200, "Exclude name: %s\n", fd->msg);
241          if (!bnet_send(fd)) {
242             Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
243             set_jcr_job_status(jcr, JS_ErrorTerminated);
244             return 0;
245          }
246       }
247    }
248    bnet_sig(fd, BNET_EOD);
249    if (!response(fd, OKexc, "Exclude")) {
250       set_jcr_job_status(jcr, JS_ErrorTerminated);
251       return 0;
252    }
253    return 1;
254 }
255
256
257 /* 
258  * Read the attributes from the File daemon for
259  * a Verify job and store them in the catalog.
260  */
261 int get_attributes_and_put_in_catalog(JCR *jcr)
262 {
263    BSOCK   *fd;
264    int n = 0;
265    ATTR_DBR ar;
266
267    fd = jcr->file_bsock;
268    jcr->jr.FirstIndex = 1;
269    memset(&ar, 0, sizeof(ar));
270    jcr->FileIndex = 0;
271
272    Dmsg0(120, "bdird: waiting to receive file attributes\n");
273    /* Pickup file attributes and signature */
274    while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
275
276    /*****FIXME****** improve error handling to stop only on 
277     * really fatal problems, or the number of errors is too
278     * large.
279     */
280       long file_index;
281       int stream, len;
282       char *attr, *p, *fn;
283       char Opts_SIG[MAXSTRING];      /* either Verify opts or MD5/SHA1 signature */
284       char SIG[MAXSTRING];
285
286       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
287       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
288          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
289 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
290          set_jcr_job_status(jcr, JS_ErrorTerminated);
291          return 0;
292       }
293       p = fd->msg;
294       skip_nonspaces(&p);             /* skip FileIndex */
295       skip_spaces(&p);
296       skip_nonspaces(&p);             /* skip Stream */
297       skip_spaces(&p);
298       skip_nonspaces(&p);             /* skip Opts_SHA1 */   
299       p++;                            /* skip space */
300       fn = jcr->fname;
301       while (*p != 0) {
302          *fn++ = *p++;                /* copy filename */
303       }
304       *fn = *p++;                     /* term filename and point to attribs */
305       attr = p;
306
307       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
308          jcr->JobFiles++;
309          jcr->FileIndex = file_index;
310          ar.attr = attr;
311          ar.fname = jcr->fname;
312          ar.FileIndex = file_index;
313          ar.Stream = stream;
314          ar.link = NULL;
315          ar.JobId = jcr->JobId;
316          ar.ClientId = jcr->ClientId;
317          ar.PathId = 0;
318          ar.FilenameId = 0;
319
320          Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
321          Dmsg1(120, "dird<filed: attr=%s\n", attr);
322
323          if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
324             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
325             set_jcr_job_status(jcr, JS_Error);
326             continue;
327          }
328          jcr->FileId = ar.FileId;
329       } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
330          if (jcr->FileIndex != (uint32_t)file_index) {
331             Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
332                file_index, jcr->FileIndex);
333             set_jcr_job_status(jcr, JS_Error);
334             continue;
335          }
336          db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
337          Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
338          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG, 
339                    stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
340             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
341             set_jcr_job_status(jcr, JS_Error);
342          }
343       }
344       jcr->jr.JobFiles = jcr->JobFiles = file_index;
345       jcr->jr.LastIndex = file_index;
346    } 
347    if (is_bnet_error(fd)) {
348       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
349                         bnet_strerror(fd));
350       set_jcr_job_status(jcr, JS_ErrorTerminated);
351       return 0;
352    }
353
354    set_jcr_job_status(jcr, JS_Terminated);
355    return 1;
356 }