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