]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
f358f4e409ce2b18ae734298144663fd219bdf4c
[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  */
14 /*
15    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35 #include "dird.h"
36
37 /* Commands sent to File daemon */
38 static char inc[]         = "include\n";
39 static char exc[]         = "exclude\n";
40 static char jobcmd[]      = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
41
42
43 /* Responses received from File daemon */
44 static char OKinc[]      = "2000 OK include\n";
45 static char OKexc[]      = "2000 OK exclude\n";
46 static char OKjob[]      = "2000 OK Job\n";
47
48 /* Forward referenced functions */
49
50 /* External functions */
51 extern int debug_level;
52 extern DIRRES *director; 
53 extern int FDConnectTimeout;
54
55 /*
56  * Open connection with File daemon. 
57  * Try connecting every 10 seconds, give up after 1 hour.
58  */
59
60 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
61                            int verbose)
62 {
63    BSOCK   *fd;
64
65    fd = bnet_connect(jcr, retry_interval, max_retry_time,
66         _("File daemon"), jcr->client->address, 
67         NULL, jcr->client->FDport, verbose);
68    if (fd == NULL) {
69       jcr->JobStatus = JS_ErrorTerminated;
70       return 0;
71    }
72    Dmsg0(10, "Opened connection with File daemon\n");
73    fd->res = (RES *)jcr->client;      /* save resource in BSOCK */
74    jcr->file_bsock = fd;
75    jcr->JobStatus = JS_Running;
76
77    if (!authenticate_file_daemon(jcr)) {
78       jcr->JobStatus = JS_ErrorTerminated;
79       return 0;
80    }
81         
82    /*
83     * Now send JobId and authorization key
84     */
85    bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId, 
86       jcr->VolSessionTime, jcr->sd_auth_key);
87    Dmsg1(10, ">filed: %s", fd->msg);
88    if (bnet_recv(fd) > 0) {
89        Dmsg1(10, "<filed: %s", fd->msg);
90        if (strcmp(fd->msg, OKjob) != 0) {
91           Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Job command: %s\n"), fd->msg);
92           jcr->JobStatus = JS_ErrorTerminated;
93           return 0;
94        } 
95    } else {
96       Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
97          bnet_strerror(fd));
98       jcr->JobStatus = JS_ErrorTerminated;
99       return 0;
100    }
101    return 1;
102 }
103
104
105 /*
106  * Send include list to File daemon
107  */
108 int send_include_list(JCR *jcr)
109 {
110    FILESET *fileset;
111    BSOCK   *fd;
112    int i;
113    char *msgsave;
114
115    fd = jcr->file_bsock;
116    fileset = jcr->fileset;
117
118    msgsave = fd->msg;
119
120    fd->msglen = sprintf(fd->msg, inc);
121    bnet_send(fd);
122    for (i=0; i < fileset->num_includes; i++) {
123       fd->msglen = strlen(fileset->include_array[i]);
124       Dmsg1(20, "dird>filed: include file: %s\n", fileset->include_array[i]);
125       fd->msg = fileset->include_array[i];
126       if (!bnet_send(fd)) {
127          Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
128          jcr->JobStatus = JS_ErrorTerminated;
129          return 0;
130       }
131    }
132    bnet_sig(fd, BNET_EOF);
133    fd->msg = msgsave;
134    if (!response(fd, OKinc, "Include")) {
135       jcr->JobStatus = JS_ErrorTerminated;
136       return 0;
137    }
138    return 1;
139 }
140
141 /*
142  * Send exclude list to File daemon 
143  */
144 int send_exclude_list(JCR *jcr)
145 {
146    FILESET *fileset;
147    BSOCK   *fd;
148    int i;
149    char *msgsave;
150
151    fd = jcr->file_bsock;
152    fileset = jcr->fileset;
153
154    msgsave = fd->msg;
155    fd->msglen = sprintf(fd->msg, exc);
156    bnet_send(fd);
157    for (i=0; i < fileset->num_excludes; i++) {
158       fd->msglen = strlen(fileset->exclude_array[i]);
159       Dmsg1(20, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]);
160       fd->msg = fileset->exclude_array[i];
161       if (!bnet_send(fd)) {
162          Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
163          jcr->JobStatus = JS_ErrorTerminated;
164          return 0;
165       }
166    }
167    bnet_sig(fd, BNET_EOF);
168    fd->msg = msgsave;
169    if (!response(fd, OKexc, "Exclude")) {
170       jcr->JobStatus = JS_ErrorTerminated;
171       return 0;
172    }
173    return 1;
174 }
175
176
177 /* 
178  * Read the attributes from the File daemon for
179  * a Verify job and store them in the catalog.
180  */
181 int get_attributes_and_put_in_catalog(JCR *jcr)
182 {
183    BSOCK   *fd;
184    int n;
185    ATTR_DBR ar;
186
187    fd = jcr->file_bsock;
188    jcr->jr.FirstIndex = 1;
189    memset(&ar, 0, sizeof(ar));
190
191    Dmsg0(20, "bdird: waiting to receive file attributes\n");
192    /* Pickup file attributes and signature */
193    while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
194
195    /*****FIXME****** improve error handling to stop only on 
196     * really fatal problems, or the number of errors is too
197     * large.
198     */
199        long file_index;
200        int stream, len;
201        char *attr, buf[MAXSTRING];
202        char Opts[MAXSTRING];          /* either Verify opts or MD5 signature */
203
204        /* ***FIXME*** check fname length */
205        if ((len = sscanf(fd->msg, "%ld %d %s %s", &file_index, &stream, 
206              Opts, jcr->fname)) != 4) {
207           Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 4 fields got %d\n\
208 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
209           jcr->JobStatus = JS_ErrorTerminated;
210           return 0;
211        }
212
213        if (stream == STREAM_UNIX_ATTRIBUTES) {
214           len = strlen(fd->msg);      /* length before attributes */
215           ar.attr = &fd->msg[len+1];
216           ar.fname = jcr->fname;
217           ar.FileIndex = 0;           /* used as mark field during compare */
218           ar.Stream = stream;
219           ar.link = NULL;
220           ar.JobId = jcr->JobId;
221           ar.ClientId = jcr->ClientId;
222           ar.PathId = 0;
223           ar.FilenameId = 0;
224
225           Dmsg2(11, "dird<filed: stream=%d %s\n", stream, jcr->fname);
226           Dmsg1(20, "dird<filed: attr=%s\n", attr);
227
228           /* ***FIXME*** fix link field */
229           if (!db_create_file_attributes_record(jcr->db, &ar)) {
230              Jmsg1(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
231              jcr->JobStatus = JS_ErrorTerminated;
232              /* This probably should not really be fatal *****FIXME****** */
233              return 0;
234           }
235           jcr->FileIndex = file_index;
236           jcr->FileId = ar.FileId;
237        } else if (stream == STREAM_MD5_SIGNATURE) {
238           if (jcr->FileIndex != (uint32_t)file_index) {
239              Jmsg0(jcr, M_FATAL, 0, _("Got MD5 but not same block as attributes\n"));
240              jcr->JobStatus = JS_ErrorTerminated;
241              return 0;
242           }
243           db_escape_string(buf, Opts, strlen(Opts));
244           Dmsg2(20, "MD5len=%d MD5=%s\n", strlen(buf), buf);
245           if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, buf)) {
246              Jmsg1(jcr, M_WARNING, 0, "%s", db_strerror(jcr->db));
247           }
248        }
249        jcr->jr.JobFiles = file_index;
250        jcr->jr.LastIndex = file_index;
251    } 
252    if (n < 0) {
253       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
254                         bnet_strerror(fd));
255       jcr->JobStatus = JS_ErrorTerminated;
256       return 0;
257    }
258
259    jcr->JobStatus = JS_Terminated;
260    return 1;
261 }