]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
Basic Restore bootstrap implemented -- kes25Jun02
[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, 2001, 2002 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\n";
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(10, ">filed: %s", fd->msg);
89    if (bnet_recv(fd) > 0) {
90        Dmsg1(10, "<filed: %s", fd->msg);
91        if (strcmp(fd->msg, 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        } 
96    } else {
97       Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
98          bnet_strerror(fd));
99       jcr->JobStatus = JS_ErrorTerminated;
100       return 0;
101    }
102    return 1;
103 }
104
105
106 /*
107  * Send include list to File daemon
108  */
109 int send_include_list(JCR *jcr)
110 {
111    FILESET *fileset;
112    BSOCK   *fd;
113    int i;
114    char *msgsave;
115
116    fd = jcr->file_bsock;
117    fileset = jcr->fileset;
118
119    msgsave = fd->msg;
120
121    fd->msglen = sprintf(fd->msg, inc);
122    bnet_send(fd);
123    for (i=0; i < fileset->num_includes; i++) {
124       fd->msglen = strlen(fileset->include_array[i]);
125       Dmsg1(20, "dird>filed: include file: %s\n", fileset->include_array[i]);
126       fd->msg = fileset->include_array[i];
127       if (!bnet_send(fd)) {
128          fd->msg = msgsave;
129          Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
130          jcr->JobStatus = JS_ErrorTerminated;
131          return 0;
132       }
133    }
134    bnet_sig(fd, BNET_EOF);
135    fd->msg = msgsave;
136    if (!response(fd, OKinc, "Include")) {
137       jcr->JobStatus = JS_ErrorTerminated;
138       return 0;
139    }
140    return 1;
141 }
142
143 /*
144  * Send exclude list to File daemon 
145  */
146 int send_exclude_list(JCR *jcr)
147 {
148    FILESET *fileset;
149    BSOCK   *fd;
150    int i;
151    char *msgsave;
152
153    fd = jcr->file_bsock;
154    fileset = jcr->fileset;
155
156    msgsave = fd->msg;
157    fd->msglen = sprintf(fd->msg, exc);
158    bnet_send(fd);
159    for (i=0; i < fileset->num_excludes; i++) {
160       fd->msglen = strlen(fileset->exclude_array[i]);
161       Dmsg1(20, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]);
162       fd->msg = fileset->exclude_array[i];
163       if (!bnet_send(fd)) {
164          Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
165          jcr->JobStatus = JS_ErrorTerminated;
166          return 0;
167       }
168    }
169    bnet_sig(fd, BNET_EOF);
170    fd->msg = msgsave;
171    if (!response(fd, OKexc, "Exclude")) {
172       jcr->JobStatus = JS_ErrorTerminated;
173       return 0;
174    }
175    return 1;
176 }
177
178
179 /* 
180  * Read the attributes from the File daemon for
181  * a Verify job and store them in the catalog.
182  */
183 int get_attributes_and_put_in_catalog(JCR *jcr)
184 {
185    BSOCK   *fd;
186    int n;
187    ATTR_DBR ar;
188
189    fd = jcr->file_bsock;
190    jcr->jr.FirstIndex = 1;
191    memset(&ar, 0, sizeof(ar));
192
193    Dmsg0(20, "bdird: waiting to receive file attributes\n");
194    /* Pickup file attributes and signature */
195    while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
196
197    /*****FIXME****** improve error handling to stop only on 
198     * really fatal problems, or the number of errors is too
199     * large.
200     */
201       long file_index;
202       int stream, len;
203       char *attr, *p, *fn;
204       char Opts_MD5[MAXSTRING];      /* either Verify opts or MD5 signature */
205       char MD5[MAXSTRING];
206
207       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
208       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_MD5)) != 3) {
209          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
210 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
211          jcr->JobStatus = JS_ErrorTerminated;
212          return 0;
213       }
214       p = fd->msg;
215       skip_nonspaces(&p);             /* skip FileIndex */
216       skip_spaces(&p);
217       skip_nonspaces(&p);             /* skip Stream */
218       skip_spaces(&p);
219       skip_nonspaces(&p);             /* skip Opts_MD5 */   
220       p++;                            /* skip space */
221       fn = jcr->fname;
222       while (*p != 0) {
223          *fn++ = *p++;                /* copy filename */
224       }
225       *fn = *p++;                     /* term filename and point to attribs */
226       attr = p;
227
228       if (stream == STREAM_UNIX_ATTRIBUTES) {
229          jcr->JobFiles++;
230          ar.attr = attr;
231          ar.fname = jcr->fname;
232          ar.FileIndex = 0;           /* used as mark field during compare */
233          ar.Stream = stream;
234          ar.link = NULL;
235          ar.JobId = jcr->JobId;
236          ar.ClientId = jcr->ClientId;
237          ar.PathId = 0;
238          ar.FilenameId = 0;
239
240          Dmsg2(11, "dird<filed: stream=%d %s\n", stream, jcr->fname);
241          Dmsg1(20, "dird<filed: attr=%s\n", attr);
242
243          /* ***FIXME*** fix link field */
244          if (!db_create_file_attributes_record(jcr->db, &ar)) {
245             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
246             jcr->JobStatus = JS_Error;
247             continue;
248          }
249          jcr->FileIndex = file_index;
250          jcr->FileId = ar.FileId;
251       } else if (stream == STREAM_MD5_SIGNATURE) {
252          if (jcr->FileIndex != (uint32_t)file_index) {
253             Jmsg0(jcr, M_ERROR, 0, _("Got MD5 but not same block as attributes\n"));
254             jcr->JobStatus = JS_Error;
255             continue;
256          }
257          db_escape_string(MD5, Opts_MD5, strlen(Opts_MD5));
258          Dmsg2(20, "MD5len=%d MD5=%s\n", strlen(MD5), MD5);
259          if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5)) {
260             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
261             jcr->JobStatus = JS_Error;
262          }
263       }
264       jcr->jr.JobFiles = jcr->JobFiles = file_index;
265       jcr->jr.LastIndex = file_index;
266    } 
267    if (n < 0) {
268       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
269                         bnet_strerror(fd));
270       jcr->JobStatus = JS_ErrorTerminated;
271       return 0;
272    }
273
274    jcr->JobStatus = JS_Terminated;
275    return 1;
276 }