]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
Fix bootstrap error notification
[bacula/bacula] / bacula / src / filed / job.c
1 /*
2  *  Bacula File Daemon Job processing
3  *
4  *    Kern Sibbald, October MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31 #include "host.h"
32
33 extern char my_name[];
34 extern CLIENT *me;                    /* our client resource */
35                         
36 /* Imported functions */
37 extern int status_cmd(JCR *jcr);
38                                    
39 /* Forward referenced functions */
40 static int backup_cmd(JCR *jcr);
41 static int bootstrap_cmd(JCR *jcr);
42 static int cancel_cmd(JCR *jcr);
43 static int setdebug_cmd(JCR *jcr);
44 static int estimate_cmd(JCR *jcr);
45 static int exclude_cmd(JCR *jcr);
46 static int hello_cmd(JCR *jcr);
47 static int job_cmd(JCR *jcr);
48 static int include_cmd(JCR *jcr);
49 static int level_cmd(JCR *jcr);
50 static int verify_cmd(JCR *jcr);
51 static int restore_cmd(JCR *jcr);
52 static int storage_cmd(JCR *jcr);
53 static int session_cmd(JCR *jcr);
54 static int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd);
55 static void filed_free_jcr(JCR *jcr);
56 static int open_sd_read_session(JCR *jcr);
57 static int send_bootstrap_file(JCR *jcr);
58
59
60 /* Exported functions */
61
62 struct s_cmds {
63    char *cmd;
64    int (*func)(JCR *);
65 };
66
67 /*  
68  * The following are the recognized commands from the Director. 
69  */
70 static struct s_cmds cmds[] = {
71    {"backup",   backup_cmd},
72    {"cancel",   cancel_cmd},
73    {"setdebug=", setdebug_cmd},
74    {"estimate", estimate_cmd},
75    {"exclude",  exclude_cmd},
76    {"Hello",    hello_cmd},
77    {"include",  include_cmd},
78    {"JobId=",   job_cmd},
79    {"level = ", level_cmd},
80    {"restore",  restore_cmd},
81    {"session",  session_cmd},
82    {"status",   status_cmd},
83    {"storage ", storage_cmd},
84    {"verify",   verify_cmd},
85    {"bootstrap",bootstrap_cmd},
86    {NULL,       NULL}                  /* list terminator */
87 };
88
89 /* Commands received from director that need scanning */
90 static char jobcmd[]      = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
91 static char storaddr[]    = "storage address=%s port=%d ssl=%d\n";
92 static char sessioncmd[]  = "session %127s %ld %ld %ld %ld %ld %ld\n";
93 static char restorecmd[]  = "restore replace=%c where=%s\n";
94 static char restorecmd1[] = "restore replace=%c where=\n";
95 static char verifycmd[]   = "verify level=%30s\n";
96
97 /* Responses sent to Director */
98 static char errmsg[]      = "2999 Invalid command\n";
99 static char no_auth[]     = "2998 No Authorization\n";
100 static char OKinc[]       = "2000 OK include\n";
101 static char OKest[]       = "2000 OK estimate files=%ld bytes=%ld\n";
102 static char OKexc[]       = "2000 OK exclude\n";
103 static char OKlevel[]     = "2000 OK level\n";
104 static char OKbackup[]    = "2000 OK backup\n";
105 static char OKbootstrap[] = "2000 OK bootstrap\n";
106 static char OKverify[]    = "2000 OK verify\n";
107 static char OKrestore[]   = "2000 OK restore\n";
108 static char OKsession[]   = "2000 OK session\n";
109 static char OKstore[]     = "2000 OK storage\n";
110 static char OKjob[]       = "2000 OK Job " FDHOST "," DISTNAME "," DISTVER;
111 static char OKsetdebug[]  = "2000 OK setdebug=%d\n";
112 static char BADjob[]      = "2901 Bad Job\n";
113 static char EndRestore[]  = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%s Errors=%u\n";
114 static char EndBackup[]   = "2801 End Backup Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
115
116 /* Responses received from Storage Daemon */
117 static char OK_end[]       = "3000 OK end\n";
118 static char OK_close[]     = "3000 OK close Status = %d\n";
119 static char OK_open[]      = "3000 OK open ticket = %d\n";
120 static char OK_data[]      = "3000 OK data\n";
121 static char OK_append[]    = "3000 OK append data\n";
122 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
123
124
125 /* Commands sent to Storage Daemon */
126 static char append_open[]  = "append open session\n";
127 static char append_data[]  = "append data %d\n";
128 static char append_end[]   = "append end session %d\n";
129 static char append_close[] = "append close session %d\n";
130 static char read_open[]    = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
131 static char read_data[]    = "read data %d\n";
132 static char read_close[]   = "read close session %d\n";
133
134 /* 
135  * Accept requests from a Director
136  *
137  * NOTE! We are running as a separate thread
138  *
139  * Send output one line
140  * at a time followed by a zero length transmission.
141  *
142  * Return when the connection is terminated or there
143  * is an error.
144  *
145  * Basic task here is:
146  *   Authenticate Director (during Hello command).
147  *   Accept commands one at a time from the Director
148  *     and execute them.
149  *
150  */
151 void *handle_client_request(void *dirp)
152 {
153    int i, found, quit;
154    JCR *jcr;
155    BSOCK *dir = (BSOCK *)dirp;
156
157    jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
158    jcr->dir_bsock = dir;
159    jcr->ff = init_find_files();
160    jcr->start_time = time(NULL);
161    jcr->last_fname = get_pool_memory(PM_FNAME);
162    jcr->last_fname[0] = 0;
163    jcr->client_name = get_memory(strlen(my_name) + 1);
164    pm_strcpy(&jcr->client_name, my_name);
165    dir->jcr = (void *)jcr;
166    get_backup_privileges(NULL, 1 /* ignore_errors */);
167
168    /**********FIXME******* add command handler error code */
169
170    for (quit=0; !quit;) {
171
172       /* Read command */
173       if (bnet_recv(dir) < 0) {
174          break;                       /* connection terminated */
175       }
176       dir->msg[dir->msglen] = 0;
177       Dmsg1(100, "<dird: %s", dir->msg);
178       found = FALSE;
179       for (i=0; cmds[i].cmd; i++) {
180          if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
181             if (!jcr->authenticated && cmds[i].func != hello_cmd) {
182                bnet_fsend(dir, no_auth);
183                break;
184             }
185             found = TRUE;                /* indicate command found */
186             if (!cmds[i].func(jcr)) {    /* do command */
187                quit = TRUE;              /* error or fully terminated,  get out */
188                Pmsg0(20, "Command error or Job done.\n");
189             }
190             break;
191          }
192       }
193       if (!found) {                   /* command not found */
194          bnet_fsend(dir, errmsg);
195          quit = TRUE;
196          break;
197       }
198    }
199
200    /* Inform Storage daemon that we are done */
201    if (jcr->store_bsock) {
202       bnet_sig(jcr->store_bsock, BNET_TERMINATE);
203    }
204
205    /* Inform Director that we are done */
206    bnet_sig(dir, BNET_TERMINATE);
207
208    Dmsg0(100, "Calling term_find_files\n");
209    term_find_files((FF_PKT *)jcr->ff);
210    Dmsg0(100, "Done with term_find_files\n");
211    free_jcr(jcr);                     /* destroy JCR record */
212    Dmsg0(100, "Done with free_jcr\n");
213    return NULL;
214 }
215
216 /*
217  * Hello from Director he must identify himself and provide his 
218  *  password.
219  */
220 static int hello_cmd(JCR *jcr)
221 {
222    Dmsg0(120, "Calling Authenticate\n");
223    if (!authenticate_director(jcr)) {
224       return 0;
225    }
226    Dmsg0(120, "OK Authenticate\n");
227    jcr->authenticated = TRUE;
228    return 1;
229 }
230
231 /*
232  * Cancel a Job
233  */
234 static int cancel_cmd(JCR *jcr)
235 {
236    BSOCK *dir = jcr->dir_bsock;
237    char Job[MAX_NAME_LENGTH];
238    JCR *cjcr;
239
240    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
241       if (!(cjcr=get_jcr_by_full_name(Job))) {
242          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
243       } else {
244          set_jcr_job_status(cjcr, JS_Canceled);
245          free_jcr(cjcr);
246          bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
247       }
248    } else {
249       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
250    }
251    bnet_sig(dir, BNET_EOD);
252    return 1;
253 }
254
255
256 /*
257  * Set debug level as requested by the Director
258  *
259  */
260 static int setdebug_cmd(JCR *jcr)
261 {
262    BSOCK *dir = jcr->dir_bsock;
263    int level;
264
265    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
266    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
267       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
268       return 0;   
269    }
270    debug_level = level;
271    return bnet_fsend(dir, OKsetdebug, level);
272 }
273
274
275 static int estimate_cmd(JCR *jcr)
276 {
277    BSOCK *dir = jcr->dir_bsock;
278    make_estimate(jcr);
279    return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
280 }
281
282 /*
283  * Get JobId and Storage Daemon Authorization key from Director
284  */
285 static int job_cmd(JCR *jcr)
286 {
287    BSOCK *dir = jcr->dir_bsock;
288    POOLMEM *sd_auth_key;
289
290    sd_auth_key = get_memory(dir->msglen);
291    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
292               &jcr->VolSessionId, &jcr->VolSessionTime,
293               sd_auth_key) != 5) {
294       bnet_fsend(dir, BADjob);
295       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
296       free_pool_memory(sd_auth_key);
297       return 0;
298    }
299    jcr->sd_auth_key = bstrdup(sd_auth_key);
300    free_pool_memory(sd_auth_key);
301    Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
302    return bnet_fsend(dir, OKjob);
303 }
304
305 #define INC_LIST 0
306 #define EXC_LIST 1
307
308 static void add_fname_to_list(JCR *jcr, char *fname, int list)
309 {
310    char *p;  
311    if (list == INC_LIST) {
312       add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
313    } else {
314       /* Skip leading options -- currently ignored */
315       for (p=fname; *p && *p != ' '; p++)
316          { }
317       /* Skip spaces */
318       for ( ; *p && *p == ' '; p++)
319          { }
320       add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
321    }
322 }
323
324 /* 
325  * 
326  * Get list of files/directories to include from Director
327  *
328  */
329 static int include_cmd(JCR *jcr)
330 {
331    BSOCK *dir = jcr->dir_bsock;
332
333    while (bnet_recv(dir) >= 0) {
334       dir->msg[dir->msglen] = 0;
335       strip_trailing_junk(dir->msg);
336       Dmsg1(010, "include file: %s\n", dir->msg);
337       add_fname_to_list(jcr, dir->msg, INC_LIST);
338    }
339
340    return bnet_fsend(dir, OKinc);
341 }
342
343 /*
344  * Get list of files to exclude from Director
345  *
346  */
347 static int exclude_cmd(JCR *jcr)
348 {
349    BSOCK *dir = jcr->dir_bsock;
350
351    while (bnet_recv(dir) >= 0) {
352       dir->msg[dir->msglen] = 0;
353       strip_trailing_junk(dir->msg);
354       add_fname_to_list(jcr, dir->msg, EXC_LIST);
355       Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
356    }
357
358    return bnet_fsend(dir, OKexc);
359 }
360
361
362 static int bootstrap_cmd(JCR *jcr)
363 {
364    BSOCK *dir = jcr->dir_bsock;
365    POOLMEM *fname = get_pool_memory(PM_FNAME);
366    FILE *bs;
367
368    if (jcr->RestoreBootstrap) {
369       unlink(jcr->RestoreBootstrap);
370       free_pool_memory(jcr->RestoreBootstrap);
371    }
372    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
373       jcr->Job);
374    Dmsg1(400, "bootstrap=%s\n", fname);
375    jcr->RestoreBootstrap = fname;
376    bs = fopen(fname, "a+");           /* create file */
377    if (!bs) {
378       /* 
379        * Suck up what he is sending to us so that he will then
380        *   read our error message.
381        */
382       while (bnet_recv(dir) >= 0)
383         {  }
384
385       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
386          jcr->RestoreBootstrap, strerror(errno));
387       free_pool_memory(jcr->RestoreBootstrap);
388       jcr->RestoreBootstrap = NULL;
389       set_jcr_job_status(jcr, JS_ErrorTerminated);
390       return 0;
391    }
392
393    while (bnet_recv(dir) >= 0) {
394        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
395        fputs(dir->msg, bs);
396    }
397    fclose(bs);
398
399    return bnet_fsend(dir, OKbootstrap);
400 }
401
402
403 /*
404  * Get backup level from Director
405  *
406  */
407 static int level_cmd(JCR *jcr)
408 {
409    BSOCK *dir = jcr->dir_bsock;
410    POOLMEM *level;
411    struct tm tm;
412    time_t mtime;
413
414    level = get_memory(dir->msglen+1);
415    Dmsg1(110, "level_cmd: %s", dir->msg);
416    if (sscanf(dir->msg, "level = %s ", level) != 1) {
417       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
418       free_memory(level);
419       return 0;
420    }
421    /* Base backup requested? */
422    if (strcmp(level, "base") == 0) {
423       jcr->save_level = L_BASE;
424    /* Full backup requested? */ 
425    } else if (strcmp(level, "full") == 0) {
426       jcr->save_level = L_FULL;
427    /* 
428     * Backup requested since <date> <time>
429     *  This form is also used for incremental and differential
430     */
431    } else if (strcmp(level, "since") == 0) {
432       jcr->save_level = L_SINCE;
433       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
434                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
435                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
436          Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
437          free_memory(level);
438          return 0;
439       }
440       tm.tm_year -= 1900;
441       tm.tm_mon  -= 1;
442       tm.tm_wday = tm.tm_yday = 0;              
443       tm.tm_isdst = -1;
444       mtime = mktime(&tm);
445       Dmsg1(100, "Got since time: %s", ctime(&mtime));
446       jcr->incremental = 1;           /* set incremental or decremental backup */
447       jcr->mtime = mtime;             /* set since time */
448    } else {
449       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
450       free_memory(level);
451       return 0;
452    }
453    free_memory(level);
454    return bnet_fsend(dir, OKlevel);
455 }
456
457 /*
458  * Get session parameters from Director -- this is for a Restore command
459  */
460 static int session_cmd(JCR *jcr)
461 {
462    BSOCK *dir = jcr->dir_bsock;
463
464    Dmsg1(100, "SessionCmd: %s", dir->msg);
465    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
466               &jcr->VolSessionId, &jcr->VolSessionTime,
467               &jcr->StartFile, &jcr->EndFile, 
468               &jcr->StartBlock, &jcr->EndBlock) != 7) {
469       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
470       return 0;
471    }
472
473    return bnet_fsend(dir, OKsession);
474 }
475
476 /*
477  * Get address of storage daemon from Director
478  *
479  */
480 static int storage_cmd(JCR *jcr)
481 {
482    int stored_port;                /* storage daemon port */
483    int enable_ssl;                 /* enable ssl to sd */
484    BSOCK *dir = jcr->dir_bsock;
485    BSOCK *sd;                         /* storage daemon bsock */
486
487    Dmsg1(100, "StorageCmd: %s", dir->msg);
488    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
489       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
490       return 0;
491    }
492    Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
493    /* Open command communications with Storage daemon */
494    /* Try to connect for 1 hour at 10 second intervals */
495    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
496                      jcr->stored_addr, NULL, stored_port, 1);
497    if (sd == NULL) {
498       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
499           jcr->stored_addr, stored_port);
500       return 0;
501    }
502
503    jcr->store_bsock = sd;
504
505    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
506    if (!authenticate_storagedaemon(jcr)) {
507       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
508       return 0;
509    }
510    Dmsg0(110, "Authenticated with SD.\n");
511
512    /* Send OK to Director */
513    return bnet_fsend(dir, OKstore);
514 }
515
516
517 /*  
518  * Do a backup. For now, we handle only Full and Incremental.
519  */
520 static int backup_cmd(JCR *jcr)
521
522    BSOCK *dir = jcr->dir_bsock;
523    BSOCK *sd = jcr->store_bsock;
524    int ok = 0;
525    int SDJobStatus;
526    char ed1[50], ed2[50];
527
528    set_jcr_job_status(jcr, JS_Blocked);
529    jcr->JobType = JT_BACKUP;
530    Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
531
532    if (sd == NULL) {
533       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
534       set_jcr_job_status(jcr, JS_ErrorTerminated);
535       goto cleanup;
536    }
537
538    bnet_fsend(dir, OKbackup);
539    Dmsg1(110, "bfiled>dird: %s", dir->msg);
540
541    /* 
542     * Send Append Open Session to Storage daemon
543     */
544    bnet_fsend(sd, append_open);
545    Dmsg1(110, ">stored: %s", sd->msg);
546    /* 
547     * Expect to receive back the Ticket number
548     */
549    if (bget_msg(sd) >= 0) {
550       Dmsg1(110, "<stored: %s", sd->msg);
551       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
552          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
553          set_jcr_job_status(jcr, JS_ErrorTerminated);
554          goto cleanup;
555       }
556       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
557    } else {
558       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
559       set_jcr_job_status(jcr, JS_ErrorTerminated);
560       goto cleanup;
561    }
562
563    /* 
564     * Send Append data command to Storage daemon
565     */
566    bnet_fsend(sd, append_data, jcr->Ticket);
567    Dmsg1(110, ">stored: %s", sd->msg);
568
569    /* 
570     * Expect to get OK data 
571     */
572    Dmsg1(110, "<stored: %s", sd->msg);
573    if (!response(jcr, sd, OK_data, "Append Data")) {
574       set_jcr_job_status(jcr, JS_ErrorTerminated);
575       goto cleanup;
576    }
577       
578    /*
579     * Send Files to Storage daemon
580     */
581    Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
582    if (!blast_data_to_storage_daemon(jcr, NULL)) {
583       set_jcr_job_status(jcr, JS_ErrorTerminated);
584       bnet_suppress_error_messages(sd, 1);
585    } else {
586       set_jcr_job_status(jcr, JS_Terminated);
587       if (jcr->JobStatus != JS_Terminated) {
588          bnet_suppress_error_messages(sd, 1);
589          goto cleanup;                /* bail out now */
590       }
591       /* 
592        * Expect to get response to append_data from Storage daemon
593        */
594       if (!response(jcr, sd, OK_append, "Append Data")) {
595          set_jcr_job_status(jcr, JS_ErrorTerminated);
596          goto cleanup;
597       }
598      
599       /* 
600        * Send Append End Data to Storage daemon
601        */
602       bnet_fsend(sd, append_end, jcr->Ticket);
603       /* Get end OK */
604       if (!response(jcr, sd, OK_end, "Append End")) {
605          set_jcr_job_status(jcr, JS_ErrorTerminated);
606          goto cleanup;
607       }
608
609       /*
610        * Send Append Close to Storage daemon
611        */
612       bnet_fsend(sd, append_close, jcr->Ticket);
613       while (bget_msg(sd) >= 0) {    /* stop on signal or error */
614          if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
615             ok = 1;
616             Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
617          }
618       }
619       if (!ok) {
620          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
621          set_jcr_job_status(jcr, JS_ErrorTerminated);
622          goto cleanup;
623       }
624       if (SDJobStatus != JS_Terminated) {
625          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
626             SDJobStatus);
627          set_jcr_job_status(jcr, JS_ErrorTerminated);
628       }
629    }
630
631 cleanup:
632
633    bnet_fsend(dir, EndBackup, jcr->JobStatus, jcr->JobFiles, 
634       edit_uint64(jcr->ReadBytes, ed1), 
635       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
636
637    return 0;                          /* return and stop command loop */
638 }
639
640 /*  
641  * Do a Verify for Director
642  *
643  */
644 static int verify_cmd(JCR *jcr)
645
646    BSOCK *dir = jcr->dir_bsock;
647    BSOCK *sd  = jcr->store_bsock;
648    char level[100];
649
650    jcr->JobType = JT_VERIFY;
651    if (sscanf(dir->msg, verifycmd, level) != 1) {
652       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
653       return 0;   
654    }
655    if (strcasecmp(level, "init") == 0) {
656       jcr->JobLevel = L_VERIFY_INIT;
657    } else if (strcasecmp(level, "catalog") == 0){
658       jcr->JobLevel = L_VERIFY_CATALOG;
659    } else if (strcasecmp(level, "volume") == 0){
660       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
661    } else if (strcasecmp(level, "data") == 0){
662       jcr->JobLevel = L_VERIFY_DATA;
663    } else {   
664       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
665       return 0;   
666    }
667
668    bnet_fsend(dir, OKverify);
669    Dmsg1(110, "bfiled>dird: %s", dir->msg);
670
671    switch (jcr->JobLevel) {
672    case L_VERIFY_INIT:
673    case L_VERIFY_CATALOG:
674       do_verify(jcr);
675       break;
676    case L_VERIFY_VOLUME_TO_CATALOG:
677       if (!open_sd_read_session(jcr)) {
678          return 0;
679       }
680       start_dir_heartbeat(jcr);
681       do_verify_volume(jcr);
682       stop_dir_heartbeat(jcr);
683       /* 
684        * Send Close session command to Storage daemon
685        */
686       bnet_fsend(sd, read_close, jcr->Ticket);
687       Dmsg1(130, "bfiled>stored: %s", sd->msg);
688
689       /* ****FIXME**** check response */
690       bget_msg(sd);                      /* get OK */
691
692       /* Inform Storage daemon that we are done */
693       bnet_sig(sd, BNET_TERMINATE);
694
695       break;
696    default:
697       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
698       return 0; 
699    }
700
701    /* Inform Director that we are done */
702    bnet_sig(dir, BNET_TERMINATE);
703    return 0;                          /* return and terminate command loop */
704 }
705
706 /*  
707  * Do a Restore for Director
708  *
709  */
710 static int restore_cmd(JCR *jcr)
711
712    BSOCK *dir = jcr->dir_bsock;
713    BSOCK *sd = jcr->store_bsock;
714    POOLMEM *where;
715    char replace;
716    char ed1[50];
717
718    /*
719     * Scan WHERE (base directory for restore) from command
720     */
721    Dmsg0(150, "restore command\n");
722    /* Pickup where string */
723    where = get_memory(dir->msglen+1);
724    *where = 0;
725
726    if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
727       if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
728          Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
729          return 0;
730       }
731       *where = 0;
732    }
733    /* Turn / into nothing */
734    if (where[0] == '/' && where[1] == 0) {
735       where[0] = 0;
736    }
737       
738    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
739    unbash_spaces(where);
740    jcr->where = where;
741    jcr->replace = replace;
742
743    bnet_fsend(dir, OKrestore);
744    Dmsg1(110, "bfiled>dird: %s", dir->msg);
745
746    jcr->JobType = JT_RESTORE;
747
748    set_jcr_job_status(jcr, JS_Blocked);
749    if (!open_sd_read_session(jcr)) {
750       set_jcr_job_status(jcr, JS_ErrorTerminated);
751       goto bail_out;
752    }
753
754    set_jcr_job_status(jcr, JS_Running);
755
756    /* 
757     * Do restore of files and data
758     */
759    start_dir_heartbeat(jcr);
760    do_restore(jcr);
761    stop_dir_heartbeat(jcr);
762    
763    set_jcr_job_status(jcr, JS_Terminated);
764    if (jcr->JobStatus != JS_Terminated) {
765       bnet_suppress_error_messages(sd, 1);
766    }
767
768    /* 
769     * Send Close session command to Storage daemon
770     */
771    bnet_fsend(sd, read_close, jcr->Ticket);
772    Dmsg1(130, "bfiled>stored: %s", sd->msg);
773
774    bget_msg(sd);                      /* get OK */
775
776    /* Inform Storage daemon that we are done */
777    bnet_sig(sd, BNET_TERMINATE);
778
779 bail_out:
780    /* Send termination status back to Dir */
781    bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined, 
782       edit_uint64(jcr->JobBytes, ed1), jcr->Errors);
783
784    /* Inform Director that we are done */
785    bnet_sig(dir, BNET_TERMINATE);
786
787    Dmsg0(130, "Done in job.c\n");
788    return 0;                          /* return and terminate command loop */
789 }
790
791 static int open_sd_read_session(JCR *jcr)
792 {
793    BSOCK *sd = jcr->store_bsock;
794
795    if (!sd) {
796       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
797       return 0;
798    }
799    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
800       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
801    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
802    /* 
803     * Open Read Session with Storage daemon
804     */
805    bnet_fsend(sd, read_open, jcr->VolumeName,
806       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
807       jcr->StartBlock, jcr->EndBlock);
808    Dmsg1(110, ">stored: %s", sd->msg);
809
810    /* 
811     * Get ticket number
812     */
813    if (bget_msg(sd) >= 0) {
814       Dmsg1(110, "bfiled<stored: %s", sd->msg);
815       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
816          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
817          return 0;
818       }
819       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
820    } else {
821       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
822       return 0;
823    }
824
825    if (!send_bootstrap_file(jcr)) {
826       return 0;
827    }
828
829    /* 
830     * Start read of data with Storage daemon
831     */
832    bnet_fsend(sd, read_data, jcr->Ticket);
833    Dmsg1(110, ">stored: %s", sd->msg);
834
835    /* 
836     * Get OK data
837     */
838    if (!response(jcr, sd, OK_data, "Read Data")) {
839       return 0;
840    }
841    return 1;
842 }
843
844 /* 
845  * Destroy the Job Control Record and associated
846  * resources (sockets).
847  */
848 static void filed_free_jcr(JCR *jcr) 
849 {
850    if (jcr->store_bsock) {
851       bnet_close(jcr->store_bsock);
852    }
853    if (jcr->where) {
854       free_pool_memory(jcr->where);
855    }
856    if (jcr->RestoreBootstrap) {
857       unlink(jcr->RestoreBootstrap);
858       free_pool_memory(jcr->RestoreBootstrap);
859    }
860    if (jcr->last_fname) {
861       free_pool_memory(jcr->last_fname);
862    }
863    return;
864 }
865
866 /*
867  * Get response from Storage daemon to a command we
868  * sent. Check that the response is OK.
869  *
870  *  Returns: 0 on failure
871  *           1 on success
872  */
873 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
874 {
875    int n;
876
877    if (sd->errors) {
878       return 0;
879    }
880    if ((n = bget_msg(sd)) > 0) {
881       Dmsg0(110, sd->msg);
882       if (strcmp(sd->msg, resp) == 0) {
883          return 1;
884       }
885    } 
886    if (is_bnet_error(sd)) {
887       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
888          cmd, bnet_strerror(sd));
889    } else {
890       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
891          cmd, resp, sd->msg);
892    }
893    return 0;
894 }
895
896 static int send_bootstrap_file(JCR *jcr)
897 {
898    FILE *bs;
899    char buf[2000];
900    BSOCK *sd = jcr->store_bsock;
901    char *bootstrap = "bootstrap\n";
902
903    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
904    if (!jcr->RestoreBootstrap) {
905       return 1;
906    }
907    bs = fopen(jcr->RestoreBootstrap, "r");
908    if (!bs) {
909       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
910          jcr->RestoreBootstrap, strerror(errno));
911       set_jcr_job_status(jcr, JS_ErrorTerminated);
912       return 0;
913    }
914    strcpy(sd->msg, bootstrap);  
915    sd->msglen = strlen(sd->msg);
916    bnet_send(sd);
917    while (fgets(buf, sizeof(buf), bs)) {
918       sd->msglen = Mmsg(&sd->msg, "%s", buf);
919       bnet_send(sd);       
920    }
921    bnet_sig(sd, BNET_EOD);
922    fclose(bs);
923    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
924       set_jcr_job_status(jcr, JS_ErrorTerminated);
925       return 0;
926    }
927    return 1;
928 }