3 * Bacula Director -- fd_cmds.c -- send commands to File daemon
5 * Kern Sibbald, October MM
7 * This routine is run as a separate thread. There may be more
8 * work to be done to make it totally reentrant!!!!
10 * Utility functions for sending info to File Daemon.
11 * These functions are used by both backup and verify.
16 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
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";
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";
49 /* Forward referenced functions */
51 /* External functions */
52 extern int debug_level;
53 extern DIRRES *director;
54 extern int FDConnectTimeout;
57 * Open connection with File daemon.
58 * Try connecting every 10 seconds, give up after 1 hour.
61 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
66 fd = bnet_connect(jcr, retry_interval, max_retry_time,
67 _("File daemon"), jcr->client->address,
68 NULL, jcr->client->FDport, verbose);
70 set_jcr_job_status(jcr, JS_ErrorTerminated);
73 Dmsg0(10, "Opened connection with File daemon\n");
74 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
76 set_jcr_job_status(jcr, JS_Running);
78 if (!authenticate_file_daemon(jcr)) {
79 set_jcr_job_status(jcr, JS_ErrorTerminated);
84 * Now send JobId and authorization key
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));
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);
100 /***** ***FIXME***** update Client Uname */
103 Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
105 set_jcr_job_status(jcr, JS_ErrorTerminated);
113 * Send include list to File daemon
115 int send_include_list(JCR *jcr)
120 fd = jcr->file_bsock;
121 fileset = jcr->fileset;
123 fd->msglen = sprintf(fd->msg, inc);
125 for (int i=0; i < fileset->num_includes; i++) {
133 ie = fileset->include_items[i];
134 for (int j=0; j<ie->num_names; j++) {
135 p = ie->name_list[j];
138 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
139 bpipe = open_bpipe(fd->msg, 0, "r");
141 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
145 /* Copy File options */
147 strcpy(buf, ie->opts_list[0]->opts);
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"));
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));
168 if ((ffd = fopen(p, "r")) == NULL) {
169 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
173 /* Copy File options */
175 strcpy(buf, ie->opts_list[0]->opts);
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"));
192 pm_strcpy(&fd->msg, ie->opts_list[0]->opts);
193 pm_strcat(&fd->msg, " ");
195 pm_strcpy(&fd->msg, "0 ");
197 pm_strcat(&fd->msg, ie->name_list[j]);
198 Dmsg1(000, "Include name=%s\n", fd->msg);
199 fd->msglen = strlen(fd->msg);
200 if (!bnet_send(fd)) {
201 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
208 bnet_sig(fd, BNET_EOD); /* end of data */
209 if (!response(fd, OKinc, "Include")) {
215 set_jcr_job_status(jcr, JS_ErrorTerminated);
221 * Send exclude list to File daemon
223 int send_exclude_list(JCR *jcr)
228 fd = jcr->file_bsock;
229 fileset = jcr->fileset;
231 fd->msglen = sprintf(fd->msg, exc);
233 for (int i=0; i < fileset->num_excludes; i++) {
235 ie = fileset->exclude_items[i];
236 for (int j=0; j<ie->num_names; j++) {
237 pm_strcpy(&fd->msg, ie->name_list[j]);
238 fd->msglen = strlen(fd->msg);
239 Dmsg1(200, "Exclude name: %s\n", fd->msg);
240 if (!bnet_send(fd)) {
241 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
242 set_jcr_job_status(jcr, JS_ErrorTerminated);
247 bnet_sig(fd, BNET_EOD);
248 if (!response(fd, OKexc, "Exclude")) {
249 set_jcr_job_status(jcr, JS_ErrorTerminated);
257 * Read the attributes from the File daemon for
258 * a Verify job and store them in the catalog.
260 int get_attributes_and_put_in_catalog(JCR *jcr)
266 fd = jcr->file_bsock;
267 jcr->jr.FirstIndex = 1;
268 memset(&ar, 0, sizeof(ar));
271 Dmsg0(120, "bdird: waiting to receive file attributes\n");
272 /* Pickup file attributes and signature */
273 while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
275 /*****FIXME****** improve error handling to stop only on
276 * really fatal problems, or the number of errors is too
282 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
285 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
286 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
287 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
288 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
289 set_jcr_job_status(jcr, JS_ErrorTerminated);
293 skip_nonspaces(&p); /* skip FileIndex */
295 skip_nonspaces(&p); /* skip Stream */
297 skip_nonspaces(&p); /* skip Opts_SHA1 */
298 p++; /* skip space */
301 *fn++ = *p++; /* copy filename */
303 *fn = *p++; /* term filename and point to attribs */
306 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
308 jcr->FileIndex = file_index;
310 ar.fname = jcr->fname;
311 ar.FileIndex = file_index;
314 ar.JobId = jcr->JobId;
315 ar.ClientId = jcr->ClientId;
319 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
320 Dmsg1(120, "dird<filed: attr=%s\n", attr);
322 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
323 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
324 set_jcr_job_status(jcr, JS_Error);
327 jcr->FileId = ar.FileId;
328 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
329 if (jcr->FileIndex != (uint32_t)file_index) {
330 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
331 file_index, jcr->FileIndex);
332 set_jcr_job_status(jcr, JS_Error);
335 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
336 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
337 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
338 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
339 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
340 set_jcr_job_status(jcr, JS_Error);
343 jcr->jr.JobFiles = jcr->JobFiles = file_index;
344 jcr->jr.LastIndex = file_index;
346 if (is_bnet_error(fd)) {
347 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
349 set_jcr_job_status(jcr, JS_ErrorTerminated);
353 set_jcr_job_status(jcr, JS_Terminated);