]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
Split out routines into new files + Implement Verify VolumeToCatalog using BSR
[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 EndJob[]      = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
114
115 /* Responses received from Storage Daemon */
116 static char OK_end[]       = "3000 OK end\n";
117 static char OK_close[]     = "3000 OK close Status = %d\n";
118 static char OK_open[]      = "3000 OK open ticket = %d\n";
119 static char OK_data[]      = "3000 OK data\n";
120 static char OK_append[]    = "3000 OK append data\n";
121 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
122
123
124 /* Commands sent to Storage Daemon */
125 static char append_open[]  = "append open session\n";
126 static char append_data[]  = "append data %d\n";
127 static char append_end[]   = "append end session %d\n";
128 static char append_close[] = "append close session %d\n";
129 static char read_open[]    = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
130 static char read_data[]    = "read data %d\n";
131 static char read_close[]   = "read close session %d\n";
132
133 /* 
134  * Accept requests from a Director
135  *
136  * NOTE! We are running as a separate thread
137  *
138  * Send output one line
139  * at a time followed by a zero length transmission.
140  *
141  * Return when the connection is terminated or there
142  * is an error.
143  *
144  * Basic task here is:
145  *   Authenticate Director (during Hello command).
146  *   Accept commands one at a time from the Director
147  *     and execute them.
148  *
149  */
150 void *handle_client_request(void *dirp)
151 {
152    int i, found, quit;
153    JCR *jcr;
154    BSOCK *dir = (BSOCK *)dirp;
155
156    jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
157    jcr->dir_bsock = dir;
158    jcr->ff = init_find_files();
159    jcr->start_time = time(NULL);
160    jcr->last_fname = get_pool_memory(PM_FNAME);
161    jcr->last_fname[0] = 0;
162    jcr->client_name = get_memory(strlen(my_name) + 1);
163    pm_strcpy(&jcr->client_name, my_name);
164    dir->jcr = jcr;
165    get_backup_privileges(NULL, 1 /* ignore_errors */);
166
167    /**********FIXME******* add command handler error code */
168
169    for (quit=0; !quit;) {
170
171       /* Read command */
172       if (bnet_recv(dir) < 0) {
173          break;                       /* connection terminated */
174       }
175       dir->msg[dir->msglen] = 0;
176       Dmsg1(100, "<dird: %s", dir->msg);
177       found = FALSE;
178       for (i=0; cmds[i].cmd; i++) {
179          if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
180             if (!jcr->authenticated && cmds[i].func != hello_cmd) {
181                bnet_fsend(dir, no_auth);
182                break;
183             }
184             found = TRUE;                /* indicate command found */
185             if (!cmds[i].func(jcr)) {    /* do command */
186                quit = TRUE;              /* error or fully terminated,  get out */
187                Dmsg0(20, "Command error or Job done.\n");
188             }
189             break;
190          }
191       }
192       if (!found) {                   /* command not found */
193          bnet_fsend(dir, errmsg);
194          quit = TRUE;
195          break;
196       }
197    }
198
199    /* Inform Storage daemon that we are done */
200    if (jcr->store_bsock) {
201       bnet_sig(jcr->store_bsock, BNET_TERMINATE);
202    }
203
204    /* Inform Director that we are done */
205    bnet_sig(dir, BNET_TERMINATE);
206
207    Dmsg0(100, "Calling term_find_files\n");
208    term_find_files((FF_PKT *)jcr->ff);
209    Dmsg0(100, "Done with term_find_files\n");
210    free_jcr(jcr);                     /* destroy JCR record */
211    Dmsg0(100, "Done with free_jcr\n");
212    return NULL;
213 }
214
215 /*
216  * Hello from Director he must identify himself and provide his 
217  *  password.
218  */
219 static int hello_cmd(JCR *jcr)
220 {
221    Dmsg0(120, "Calling Authenticate\n");
222    if (!authenticate_director(jcr)) {
223       return 0;
224    }
225    Dmsg0(120, "OK Authenticate\n");
226    jcr->authenticated = TRUE;
227    return 1;
228 }
229
230 /*
231  * Cancel a Job
232  */
233 static int cancel_cmd(JCR *jcr)
234 {
235    BSOCK *dir = jcr->dir_bsock;
236    char Job[MAX_NAME_LENGTH];
237    JCR *cjcr;
238
239    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
240       if (!(cjcr=get_jcr_by_full_name(Job))) {
241          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
242       } else {
243          set_jcr_job_status(cjcr, JS_Canceled);
244          free_jcr(cjcr);
245          bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
246       }
247    } else {
248       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
249    }
250    bnet_sig(dir, BNET_EOD);
251    return 1;
252 }
253
254
255 /*
256  * Set debug level as requested by the Director
257  *
258  */
259 static int setdebug_cmd(JCR *jcr)
260 {
261    BSOCK *dir = jcr->dir_bsock;
262    int level;
263
264    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
265    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
266       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
267       return 0;   
268    }
269    debug_level = level;
270    return bnet_fsend(dir, OKsetdebug, level);
271 }
272
273
274 static int estimate_cmd(JCR *jcr)
275 {
276    BSOCK *dir = jcr->dir_bsock;
277    make_estimate(jcr);
278    return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
279 }
280
281 /*
282  * Get JobId and Storage Daemon Authorization key from Director
283  */
284 static int job_cmd(JCR *jcr)
285 {
286    BSOCK *dir = jcr->dir_bsock;
287    POOLMEM *sd_auth_key;
288
289    sd_auth_key = get_memory(dir->msglen);
290    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
291               &jcr->VolSessionId, &jcr->VolSessionTime,
292               sd_auth_key) != 5) {
293       bnet_fsend(dir, BADjob);
294       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
295       free_pool_memory(sd_auth_key);
296       return 0;
297    }
298    jcr->sd_auth_key = bstrdup(sd_auth_key);
299    free_pool_memory(sd_auth_key);
300    Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
301    return bnet_fsend(dir, OKjob);
302 }
303
304 #define INC_LIST 0
305 #define EXC_LIST 1
306
307 static void add_fname_to_list(JCR *jcr, char *fname, int list)
308 {
309    char *p;  
310    if (list == INC_LIST) {
311       add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
312    } else {
313       /* Skip leading options -- currently ignored */
314       for (p=fname; *p && *p != ' '; p++)
315          { }
316       /* Skip spaces */
317       for ( ; *p && *p == ' '; p++)
318          { }
319       add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
320    }
321 }
322
323 /* 
324  * 
325  * Get list of files/directories to include from Director
326  *
327  */
328 static int include_cmd(JCR *jcr)
329 {
330    BSOCK *dir = jcr->dir_bsock;
331
332    while (bnet_recv(dir) >= 0) {
333       dir->msg[dir->msglen] = 0;
334       strip_trailing_junk(dir->msg);
335       Dmsg1(010, "include file: %s\n", dir->msg);
336       add_fname_to_list(jcr, dir->msg, INC_LIST);
337    }
338
339    return bnet_fsend(dir, OKinc);
340 }
341
342 /*
343  * Get list of files to exclude from Director
344  *
345  */
346 static int exclude_cmd(JCR *jcr)
347 {
348    BSOCK *dir = jcr->dir_bsock;
349
350    while (bnet_recv(dir) >= 0) {
351       dir->msg[dir->msglen] = 0;
352       strip_trailing_junk(dir->msg);
353       add_fname_to_list(jcr, dir->msg, EXC_LIST);
354       Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
355    }
356
357    return bnet_fsend(dir, OKexc);
358 }
359
360
361 static int bootstrap_cmd(JCR *jcr)
362 {
363    BSOCK *dir = jcr->dir_bsock;
364    POOLMEM *fname = get_pool_memory(PM_FNAME);
365    FILE *bs;
366
367    if (jcr->RestoreBootstrap) {
368       unlink(jcr->RestoreBootstrap);
369       free_pool_memory(jcr->RestoreBootstrap);
370    }
371    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
372       jcr->Job);
373    Dmsg1(400, "bootstrap=%s\n", fname);
374    jcr->RestoreBootstrap = fname;
375    bs = fopen(fname, "a+");           /* create file */
376    if (!bs) {
377       /* 
378        * Suck up what he is sending to us so that he will then
379        *   read our error message.
380        */
381       while (bnet_recv(dir) >= 0)
382         {  }
383
384       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
385          jcr->RestoreBootstrap, strerror(errno));
386       free_pool_memory(jcr->RestoreBootstrap);
387       jcr->RestoreBootstrap = NULL;
388       set_jcr_job_status(jcr, JS_ErrorTerminated);
389       return 0;
390    }
391
392    while (bnet_recv(dir) >= 0) {
393        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
394        fputs(dir->msg, bs);
395    }
396    fclose(bs);
397
398    return bnet_fsend(dir, OKbootstrap);
399 }
400
401
402 /*
403  * Get backup level from Director
404  *
405  */
406 static int level_cmd(JCR *jcr)
407 {
408    BSOCK *dir = jcr->dir_bsock;
409    POOLMEM *level;
410    struct tm tm;
411    time_t mtime;
412
413    level = get_memory(dir->msglen+1);
414    Dmsg1(110, "level_cmd: %s", dir->msg);
415    if (sscanf(dir->msg, "level = %s ", level) != 1) {
416       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
417       free_memory(level);
418       return 0;
419    }
420    /* Base backup requested? */
421    if (strcmp(level, "base") == 0) {
422       jcr->save_level = L_BASE;
423    /* Full backup requested? */ 
424    } else if (strcmp(level, "full") == 0) {
425       jcr->save_level = L_FULL;
426    /* 
427     * Backup requested since <date> <time>
428     *  This form is also used for incremental and differential
429     */
430    } else if (strcmp(level, "since") == 0) {
431       jcr->save_level = L_SINCE;
432       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
433                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
434                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
435          Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
436          free_memory(level);
437          return 0;
438       }
439       tm.tm_year -= 1900;
440       tm.tm_mon  -= 1;
441       tm.tm_wday = tm.tm_yday = 0;              
442       tm.tm_isdst = -1;
443       mtime = mktime(&tm);
444       Dmsg1(100, "Got since time: %s", ctime(&mtime));
445       jcr->incremental = 1;           /* set incremental or decremental backup */
446       jcr->mtime = mtime;             /* set since time */
447    } else {
448       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
449       free_memory(level);
450       return 0;
451    }
452    free_memory(level);
453    return bnet_fsend(dir, OKlevel);
454 }
455
456 /*
457  * Get session parameters from Director -- this is for a Restore command
458  */
459 static int session_cmd(JCR *jcr)
460 {
461    BSOCK *dir = jcr->dir_bsock;
462
463    Dmsg1(100, "SessionCmd: %s", dir->msg);
464    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
465               &jcr->VolSessionId, &jcr->VolSessionTime,
466               &jcr->StartFile, &jcr->EndFile, 
467               &jcr->StartBlock, &jcr->EndBlock) != 7) {
468       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
469       return 0;
470    }
471
472    return bnet_fsend(dir, OKsession);
473 }
474
475 /*
476  * Get address of storage daemon from Director
477  *
478  */
479 static int storage_cmd(JCR *jcr)
480 {
481    int stored_port;                /* storage daemon port */
482    int enable_ssl;                 /* enable ssl to sd */
483    BSOCK *dir = jcr->dir_bsock;
484    BSOCK *sd;                         /* storage daemon bsock */
485
486    Dmsg1(100, "StorageCmd: %s", dir->msg);
487    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
488       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
489       return 0;
490    }
491    Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
492    /* Open command communications with Storage daemon */
493    /* Try to connect for 1 hour at 10 second intervals */
494    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
495                      jcr->stored_addr, NULL, stored_port, 1);
496    if (sd == NULL) {
497       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
498           jcr->stored_addr, stored_port);
499       return 0;
500    }
501
502    jcr->store_bsock = sd;
503
504    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
505    if (!authenticate_storagedaemon(jcr)) {
506       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
507       return 0;
508    }
509    Dmsg0(110, "Authenticated with SD.\n");
510
511    /* Send OK to Director */
512    return bnet_fsend(dir, OKstore);
513 }
514
515
516 /*  
517  * Do a backup. For now, we handle only Full and Incremental.
518  */
519 static int backup_cmd(JCR *jcr)
520
521    BSOCK *dir = jcr->dir_bsock;
522    BSOCK *sd = jcr->store_bsock;
523    int ok = 0;
524    int SDJobStatus;
525    char ed1[50], ed2[50];
526
527    set_jcr_job_status(jcr, JS_Blocked);
528    jcr->JobType = JT_BACKUP;
529    Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
530
531    if (sd == NULL) {
532       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
533       goto cleanup;
534    }
535
536    bnet_fsend(dir, OKbackup);
537    Dmsg1(110, "bfiled>dird: %s", dir->msg);
538
539    /* 
540     * Send Append Open Session to Storage daemon
541     */
542    bnet_fsend(sd, append_open);
543    Dmsg1(110, ">stored: %s", sd->msg);
544    /* 
545     * Expect to receive back the Ticket number
546     */
547    if (bget_msg(sd) >= 0) {
548       Dmsg1(110, "<stored: %s", sd->msg);
549       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
550          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
551          goto cleanup;
552       }
553       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
554    } else {
555       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
556       goto cleanup;
557    }
558
559    /* 
560     * Send Append data command to Storage daemon
561     */
562    bnet_fsend(sd, append_data, jcr->Ticket);
563    Dmsg1(110, ">stored: %s", sd->msg);
564
565    /* 
566     * Expect to get OK data 
567     */
568    Dmsg1(110, "<stored: %s", sd->msg);
569    if (!response(jcr, sd, OK_data, "Append Data")) {
570       goto cleanup;
571    }
572       
573    /*
574     * Send Files to Storage daemon
575     */
576    Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
577    if (!blast_data_to_storage_daemon(jcr, NULL)) {
578       set_jcr_job_status(jcr, JS_ErrorTerminated);
579       bnet_suppress_error_messages(sd, 1);
580    } else {
581       set_jcr_job_status(jcr, JS_Terminated);
582       if (jcr->JobStatus != JS_Terminated) {
583          bnet_suppress_error_messages(sd, 1);
584          goto cleanup;                /* bail out now */
585       }
586       /* 
587        * Expect to get response to append_data from Storage daemon
588        */
589       if (!response(jcr, sd, OK_append, "Append Data")) {
590          set_jcr_job_status(jcr, JS_ErrorTerminated);
591          goto cleanup;
592       }
593      
594       /* 
595        * Send Append End Data to Storage daemon
596        */
597       bnet_fsend(sd, append_end, jcr->Ticket);
598       /* Get end OK */
599       if (!response(jcr, sd, OK_end, "Append End")) {
600          set_jcr_job_status(jcr, JS_ErrorTerminated);
601          goto cleanup;
602       }
603
604       /*
605        * Send Append Close to Storage daemon
606        */
607       bnet_fsend(sd, append_close, jcr->Ticket);
608       while (bget_msg(sd) >= 0) {    /* stop on signal or error */
609          if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
610             ok = 1;
611             Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
612          }
613       }
614       if (!ok) {
615          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
616          goto cleanup;
617       }
618       if (SDJobStatus != JS_Terminated) {
619          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
620             SDJobStatus);
621       }
622    }
623
624 cleanup:
625
626    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
627       edit_uint64(jcr->ReadBytes, ed1), 
628       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
629
630    return 0;                          /* return and stop command loop */
631 }
632
633 /*  
634  * Do a Verify for Director
635  *
636  */
637 static int verify_cmd(JCR *jcr)
638
639    BSOCK *dir = jcr->dir_bsock;
640    BSOCK *sd  = jcr->store_bsock;
641    char level[100], ed1[50], ed2[50];
642
643    jcr->JobType = JT_VERIFY;
644    if (sscanf(dir->msg, verifycmd, level) != 1) {
645       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
646       return 0;   
647    }
648    if (strcasecmp(level, "init") == 0) {
649       jcr->JobLevel = L_VERIFY_INIT;
650    } else if (strcasecmp(level, "catalog") == 0){
651       jcr->JobLevel = L_VERIFY_CATALOG;
652    } else if (strcasecmp(level, "volume") == 0){
653       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
654    } else if (strcasecmp(level, "data") == 0){
655       jcr->JobLevel = L_VERIFY_DATA;
656    } else {   
657       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
658       return 0;   
659    }
660
661    bnet_fsend(dir, OKverify);
662    Dmsg1(110, "bfiled>dird: %s", dir->msg);
663
664    switch (jcr->JobLevel) {
665    case L_VERIFY_INIT:
666    case L_VERIFY_CATALOG:
667       do_verify(jcr);
668       break;
669    case L_VERIFY_VOLUME_TO_CATALOG:
670       if (!open_sd_read_session(jcr)) {
671          return 0;
672       }
673       start_dir_heartbeat(jcr);
674       do_verify_volume(jcr);
675       stop_dir_heartbeat(jcr);
676       /* 
677        * Send Close session command to Storage daemon
678        */
679       bnet_fsend(sd, read_close, jcr->Ticket);
680       Dmsg1(130, "bfiled>stored: %s", sd->msg);
681
682       /* ****FIXME**** check response */
683       bget_msg(sd);                      /* get OK */
684
685       /* Inform Storage daemon that we are done */
686       bnet_sig(sd, BNET_TERMINATE);
687
688       break;
689    default:
690       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
691       return 0; 
692    }
693
694    bnet_sig(dir, BNET_EOD);
695
696    /* Send termination status back to Dir */
697    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
698       edit_uint64(jcr->ReadBytes, ed1), 
699       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
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], ed2[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
781    /* Send termination status back to Dir */
782    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
783       edit_uint64(jcr->ReadBytes, ed1), 
784       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
785
786    /* Inform Director that we are done */
787    bnet_sig(dir, BNET_TERMINATE);
788
789    Dmsg0(130, "Done in job.c\n");
790    return 0;                          /* return and terminate command loop */
791 }
792
793 static int open_sd_read_session(JCR *jcr)
794 {
795    BSOCK *sd = jcr->store_bsock;
796
797    if (!sd) {
798       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
799       return 0;
800    }
801    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
802       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
803    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
804    /* 
805     * Open Read Session with Storage daemon
806     */
807    bnet_fsend(sd, read_open, jcr->VolumeName,
808       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
809       jcr->StartBlock, jcr->EndBlock);
810    Dmsg1(110, ">stored: %s", sd->msg);
811
812    /* 
813     * Get ticket number
814     */
815    if (bget_msg(sd) >= 0) {
816       Dmsg1(110, "bfiled<stored: %s", sd->msg);
817       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
818          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
819          return 0;
820       }
821       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
822    } else {
823       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
824       return 0;
825    }
826
827    if (!send_bootstrap_file(jcr)) {
828       return 0;
829    }
830
831    /* 
832     * Start read of data with Storage daemon
833     */
834    bnet_fsend(sd, read_data, jcr->Ticket);
835    Dmsg1(110, ">stored: %s", sd->msg);
836
837    /* 
838     * Get OK data
839     */
840    if (!response(jcr, sd, OK_data, "Read Data")) {
841       return 0;
842    }
843    return 1;
844 }
845
846 /* 
847  * Destroy the Job Control Record and associated
848  * resources (sockets).
849  */
850 static void filed_free_jcr(JCR *jcr) 
851 {
852    if (jcr->store_bsock) {
853       bnet_close(jcr->store_bsock);
854    }
855    if (jcr->where) {
856       free_pool_memory(jcr->where);
857    }
858    if (jcr->RestoreBootstrap) {
859       unlink(jcr->RestoreBootstrap);
860       free_pool_memory(jcr->RestoreBootstrap);
861    }
862    if (jcr->last_fname) {
863       free_pool_memory(jcr->last_fname);
864    }
865    return;
866 }
867
868 /*
869  * Get response from Storage daemon to a command we
870  * sent. Check that the response is OK.
871  *
872  *  Returns: 0 on failure
873  *           1 on success
874  */
875 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
876 {
877    int n;
878
879    if (sd->errors) {
880       return 0;
881    }
882    if ((n = bget_msg(sd)) > 0) {
883       Dmsg0(110, sd->msg);
884       if (strcmp(sd->msg, resp) == 0) {
885          return 1;
886       }
887    } 
888    if (is_bnet_error(sd)) {
889       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
890          cmd, bnet_strerror(sd));
891    } else {
892       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
893          cmd, resp, sd->msg);
894    }
895    return 0;
896 }
897
898 static int send_bootstrap_file(JCR *jcr)
899 {
900    FILE *bs;
901    char buf[2000];
902    BSOCK *sd = jcr->store_bsock;
903    char *bootstrap = "bootstrap\n";
904
905    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
906    if (!jcr->RestoreBootstrap) {
907       return 1;
908    }
909    bs = fopen(jcr->RestoreBootstrap, "r");
910    if (!bs) {
911       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
912          jcr->RestoreBootstrap, strerror(errno));
913       set_jcr_job_status(jcr, JS_ErrorTerminated);
914       return 0;
915    }
916    strcpy(sd->msg, bootstrap);  
917    sd->msglen = strlen(sd->msg);
918    bnet_send(sd);
919    while (fgets(buf, sizeof(buf), bs)) {
920       sd->msglen = Mmsg(&sd->msg, "%s", buf);
921       bnet_send(sd);       
922    }
923    bnet_sig(sd, BNET_EOD);
924    fclose(bs);
925    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
926       set_jcr_job_status(jcr, JS_ErrorTerminated);
927       return 0;
928    }
929    return 1;
930 }