]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
304fb776122282aa4620f786413d4259004367b0
[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          Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
129          jcr->JobStatus = JS_ErrorTerminated;
130          return 0;
131       }
132    }
133    bnet_sig(fd, BNET_EOF);
134    fd->msg = msgsave;
135    if (!response(fd, OKinc, "Include")) {
136       jcr->JobStatus = JS_ErrorTerminated;
137       return 0;
138    }
139    return 1;
140 }
141
142 /*
143  * Send exclude list to File daemon 
144  */
145 int send_exclude_list(JCR *jcr)
146 {
147    FILESET *fileset;
148    BSOCK   *fd;
149    int i;
150    char *msgsave;
151
152    fd = jcr->file_bsock;
153    fileset = jcr->fileset;
154
155    msgsave = fd->msg;
156    fd->msglen = sprintf(fd->msg, exc);
157    bnet_send(fd);
158    for (i=0; i < fileset->num_excludes; i++) {
159       fd->msglen = strlen(fileset->exclude_array[i]);
160       Dmsg1(20, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]);
161       fd->msg = fileset->exclude_array[i];
162       if (!bnet_send(fd)) {
163          Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
164          jcr->JobStatus = JS_ErrorTerminated;
165          return 0;
166       }
167    }
168    bnet_sig(fd, BNET_EOF);
169    fd->msg = msgsave;
170    if (!response(fd, OKexc, "Exclude")) {
171       jcr->JobStatus = JS_ErrorTerminated;
172       return 0;
173    }
174    return 1;
175 }
176
177
178 /* 
179  * Read the attributes from the File daemon for
180  * a Verify job and store them in the catalog.
181  */
182 int get_attributes_and_put_in_catalog(JCR *jcr)
183 {
184    BSOCK   *fd;
185    int n;
186    ATTR_DBR ar;
187
188    fd = jcr->file_bsock;
189    jcr->jr.FirstIndex = 1;
190    memset(&ar, 0, sizeof(ar));
191
192    Dmsg0(20, "bdird: waiting to receive file attributes\n");
193    /* Pickup file attributes and signature */
194    while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
195
196    /*****FIXME****** improve error handling to stop only on 
197     * really fatal problems, or the number of errors is too
198     * large.
199     */
200        long file_index;
201        int stream, len;
202        char *attr, buf[MAXSTRING];
203        char Opts[MAXSTRING];          /* either Verify opts or MD5 signature */
204
205        /* ***FIXME*** check fname length */
206        if ((len = sscanf(fd->msg, "%ld %d %s %s", &file_index, &stream, 
207              Opts, jcr->fname)) != 4) {
208           Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 4 fields got %d\n\
209 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
210           jcr->JobStatus = JS_ErrorTerminated;
211           return 0;
212        }
213
214        if (stream == STREAM_UNIX_ATTRIBUTES) {
215           jcr->JobFiles++;
216           len = strlen(fd->msg);      /* length before attributes */
217           ar.attr = &fd->msg[len+1];
218           ar.fname = jcr->fname;
219           ar.FileIndex = 0;           /* used as mark field during compare */
220           ar.Stream = stream;
221           ar.link = NULL;
222           ar.JobId = jcr->JobId;
223           ar.ClientId = jcr->ClientId;
224           ar.PathId = 0;
225           ar.FilenameId = 0;
226
227           Dmsg2(11, "dird<filed: stream=%d %s\n", stream, jcr->fname);
228           Dmsg1(20, "dird<filed: attr=%s\n", attr);
229
230           /* ***FIXME*** fix link field */
231           if (!db_create_file_attributes_record(jcr->db, &ar)) {
232              Jmsg1(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
233              jcr->JobStatus = JS_ErrorTerminated;
234              /* This probably should not really be fatal *****FIXME****** */
235              return 0;
236           }
237           jcr->FileIndex = file_index;
238           jcr->FileId = ar.FileId;
239        } else if (stream == STREAM_MD5_SIGNATURE) {
240           if (jcr->FileIndex != (uint32_t)file_index) {
241              Jmsg0(jcr, M_FATAL, 0, _("Got MD5 but not same block as attributes\n"));
242              jcr->JobStatus = JS_ErrorTerminated;
243              return 0;
244           }
245           db_escape_string(buf, Opts, strlen(Opts));
246           Dmsg2(20, "MD5len=%d MD5=%s\n", strlen(buf), buf);
247           if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, buf)) {
248              Jmsg1(jcr, M_WARNING, 0, "%s", db_strerror(jcr->db));
249           }
250        }
251        jcr->jr.JobFiles = jcr->JobFiles = file_index;
252        jcr->jr.LastIndex = file_index;
253    } 
254    if (n < 0) {
255       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
256                         bnet_strerror(fd));
257       jcr->JobStatus = JS_ErrorTerminated;
258       return 0;
259    }
260
261    jcr->JobStatus = JS_Terminated;
262    return 1;
263 }