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";
42 static char levelcmd[] = "level = %s%s mtime_only=%d\n";
43 static char runbefore[] = "RunBeforeJob %s\n";
44 static char runafter[] = "RunAfterJob %s\n";
47 /* Responses received from File daemon */
48 static char OKinc[] = "2000 OK include\n";
49 static char OKexc[] = "2000 OK exclude\n";
50 static char OKjob[] = "2000 OK Job";
51 static char OKbootstrap[] = "2000 OK bootstrap\n";
52 static char OKlevel[] = "2000 OK level\n";
53 static char OKRunBefore[] = "2000 OK RunBefore\n";
54 static char OKRunAfter[] = "2000 OK RunAfter\n";
56 /* Forward referenced functions */
58 /* External functions */
59 extern int debug_level;
60 extern DIRRES *director;
61 extern int FDConnectTimeout;
67 * Open connection with File daemon.
68 * Try connecting every 10 seconds, give up after 1 hour.
71 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
76 fd = bnet_connect(jcr, retry_interval, max_retry_time,
77 _("File daemon"), jcr->client->address,
78 NULL, jcr->client->FDport, verbose);
80 set_jcr_job_status(jcr, JS_ErrorTerminated);
83 Dmsg0(10, "Opened connection with File daemon\n");
84 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
86 set_jcr_job_status(jcr, JS_Running);
88 if (!authenticate_file_daemon(jcr)) {
89 set_jcr_job_status(jcr, JS_ErrorTerminated);
94 * Now send JobId and authorization key
96 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
97 jcr->VolSessionTime, jcr->sd_auth_key);
98 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
99 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
101 Dmsg1(100, ">filed: %s", fd->msg);
102 if (bget_dirmsg(fd) > 0) {
103 Dmsg1(110, "<filed: %s", fd->msg);
104 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
105 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
106 jcr->client->hdr.name, fd->msg);
107 set_jcr_job_status(jcr, JS_ErrorTerminated);
109 } else if (jcr->db) {
111 memset(&cr, 0, sizeof(cr));
112 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
113 cr.AutoPrune = jcr->client->AutoPrune;
114 cr.FileRetention = jcr->client->FileRetention;
115 cr.JobRetention = jcr->client->JobRetention;
116 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
117 if (!db_update_client_record(jcr, jcr->db, &cr)) {
118 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
119 db_strerror(jcr->db));
123 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
125 set_jcr_job_status(jcr, JS_ErrorTerminated);
132 void get_level_since_time(JCR *jcr, char *since, int since_len)
135 * FULL backup job to get the time/date for a
136 * differential or incremental save.
139 jcr->stime = get_pool_memory(PM_MESSAGE);
143 switch (jcr->JobLevel) {
146 /* Look up start time of last job */
148 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
149 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
150 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
151 bsnprintf(since, since_len, " (upgraded from %s)",
152 level_to_str(jcr->JobLevel));
153 jcr->JobLevel = jcr->jr.Level = L_FULL;
155 bstrncpy(since, ", since=", since_len);
156 bstrncat(since, jcr->stime, since_len);
158 Dmsg1(100, "Last start time = %s\n", jcr->stime);
165 * Send level command for backup and estimate
167 int send_level_command(JCR *jcr)
169 BSOCK *fd = jcr->file_bsock;
171 * Send Level command to File daemon
173 switch (jcr->JobLevel) {
175 bnet_fsend(fd, levelcmd, "base", " ", 0);
178 bnet_fsend(fd, levelcmd, "full", " ", 0);
182 bnet_fsend(fd, levelcmd, "since ", jcr->stime, 0);
186 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
187 jcr->JobLevel, jcr->JobLevel);
190 Dmsg1(120, ">filed: %s", fd->msg);
191 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
199 * Send either an Included or an Excluded list to FD
201 static int send_list(JCR *jcr, int list)
207 fd = jcr->file_bsock;
208 fileset = jcr->fileset;
210 if (list == INC_LIST) {
211 num = fileset->num_includes;
213 num = fileset->num_excludes;
216 for (int i=0; i < num; i++) {
226 if (list == INC_LIST) {
227 ie = fileset->include_items[i];
229 ie = fileset->exclude_items[i];
231 fo = ie->opts_list[0];
232 for (int j=0; j<fo->match.size(); j++) {
233 Dmsg1(100, "Match=%s\n", fo->match.get(j));
235 for (int j=0; j<ie->name_list.size(); j++) {
236 p = (char *)ie->name_list.get(j);
239 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
240 bpipe = open_bpipe(fd->msg, 0, "r");
242 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
246 /* Copy File options */
248 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
249 bstrncat(buf, " ", sizeof(buf));
251 bstrncpy(buf, "0 ", sizeof(buf));
253 Dmsg1(100, "Opts=%s\n", buf);
254 optlen = strlen(buf);
255 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
256 fd->msglen = Mmsg(&fd->msg, "%s", buf);
257 Dmsg2(200, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
258 if (!bnet_send(fd)) {
259 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
263 if ((stat=close_bpipe(bpipe)) != 0) {
264 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
265 p, stat, strerror(errno));
270 p++; /* skip over < */
271 if ((ffd = fopen(p, "r")) == NULL) {
272 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
273 list==INC_LIST?"included":"excluded", p, strerror(errno));
276 /* Copy File options */
278 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
279 bstrncat(buf, " ", sizeof(buf));
281 bstrncpy(buf, "0 ", sizeof(buf));
283 Dmsg1(100, "Opts=%s\n", buf);
284 optlen = strlen(buf);
285 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
286 fd->msglen = Mmsg(&fd->msg, "%s", buf);
287 if (!bnet_send(fd)) {
288 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
295 p++; /* skip over \ */
296 /* Note, fall through wanted */
298 Dmsg2(100, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
300 pm_strcpy(&fd->msg, ie->opts_list[0]->opts);
301 pm_strcat(&fd->msg, " ");
303 pm_strcpy(&fd->msg, "0 ");
305 fd->msglen = pm_strcat(&fd->msg, p);
306 Dmsg1(100, "Inc/Exc name=%s\n", fd->msg);
307 if (!bnet_send(fd)) {
308 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
315 bnet_sig(fd, BNET_EOD); /* end of data */
316 if (list == INC_LIST) {
317 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
320 } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
326 set_jcr_job_status(jcr, JS_ErrorTerminated);
332 * Send include list to File daemon
334 int send_include_list(JCR *jcr)
336 BSOCK *fd = jcr->file_bsock;
337 fd->msglen = pm_strcpy(&fd->msg, inc);
339 return send_list(jcr, INC_LIST);
344 * Send exclude list to File daemon
346 int send_exclude_list(JCR *jcr)
348 BSOCK *fd = jcr->file_bsock;
349 fd->msglen = pm_strcpy(&fd->msg, exc);
351 return send_list(jcr, EXC_LIST);
356 * Send bootstrap file if any to the File daemon.
357 * This is used for restore and verify VolumeToCatalog
359 int send_bootstrap_file(JCR *jcr)
363 BSOCK *fd = jcr->file_bsock;
364 char *bootstrap = "bootstrap\n";
366 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
367 if (!jcr->RestoreBootstrap) {
370 bs = fopen(jcr->RestoreBootstrap, "r");
372 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
373 jcr->RestoreBootstrap, strerror(errno));
374 set_jcr_job_status(jcr, JS_ErrorTerminated);
377 bnet_fsend(fd, bootstrap);
378 while (fgets(buf, sizeof(buf), bs)) {
379 bnet_fsend(fd, "%s", buf);
381 bnet_sig(fd, BNET_EOD);
383 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
384 set_jcr_job_status(jcr, JS_ErrorTerminated);
391 * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
393 int send_run_before_and_after_commands(JCR *jcr)
395 POOLMEM *msg = get_pool_memory(PM_FNAME);
396 BSOCK *fd = jcr->file_bsock;
397 if (jcr->job->ClientRunBeforeJob) {
398 pm_strcpy(&msg, jcr->job->ClientRunBeforeJob);
400 bnet_fsend(fd, runbefore, msg);
401 if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
402 set_jcr_job_status(jcr, JS_ErrorTerminated);
403 free_pool_memory(msg);
407 if (jcr->job->ClientRunAfterJob) {
408 fd->msglen = pm_strcpy(&msg, jcr->job->ClientRunAfterJob);
410 bnet_fsend(fd, runafter, msg);
411 if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
412 set_jcr_job_status(jcr, JS_ErrorTerminated);
413 free_pool_memory(msg);
417 free_pool_memory(msg);
423 * Read the attributes from the File daemon for
424 * a Verify job and store them in the catalog.
426 int get_attributes_and_put_in_catalog(JCR *jcr)
432 fd = jcr->file_bsock;
433 jcr->jr.FirstIndex = 1;
434 memset(&ar, 0, sizeof(ar));
437 Dmsg0(120, "bdird: waiting to receive file attributes\n");
438 /* Pickup file attributes and signature */
439 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
441 /*****FIXME****** improve error handling to stop only on
442 * really fatal problems, or the number of errors is too
448 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
451 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
452 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
453 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
454 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
455 set_jcr_job_status(jcr, JS_ErrorTerminated);
459 skip_nonspaces(&p); /* skip FileIndex */
461 skip_nonspaces(&p); /* skip Stream */
463 skip_nonspaces(&p); /* skip Opts_SHA1 */
464 p++; /* skip space */
467 *fn++ = *p++; /* copy filename */
469 *fn = *p++; /* term filename and point to attribs */
472 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
474 jcr->FileIndex = file_index;
476 ar.fname = jcr->fname;
477 ar.FileIndex = file_index;
480 ar.JobId = jcr->JobId;
481 ar.ClientId = jcr->ClientId;
485 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
486 Dmsg1(120, "dird<filed: attr=%s\n", attr);
488 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
489 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
490 set_jcr_job_status(jcr, JS_Error);
493 jcr->FileId = ar.FileId;
494 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
495 if (jcr->FileIndex != (uint32_t)file_index) {
496 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
497 file_index, jcr->FileIndex);
498 set_jcr_job_status(jcr, JS_Error);
501 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
502 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
503 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
504 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
505 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
506 set_jcr_job_status(jcr, JS_Error);
509 jcr->jr.JobFiles = jcr->JobFiles = file_index;
510 jcr->jr.LastIndex = file_index;
512 if (is_bnet_error(fd)) {
513 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
515 set_jcr_job_status(jcr, JS_ErrorTerminated);
519 set_jcr_job_status(jcr, JS_Terminated);