]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
Additional message plus exclusion fix
[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\n";
92 static char sessioncmd[]  = "session %s %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=%20s\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=%" lld "\n";
114 static char EndBackup[]   = "2801 End Backup Job TermCode=%d JobFiles=%u ReadBytes=%" lld " JobBytes=%" lld "\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
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                Pmsg0(20, "Command error\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    Dmsg0(100, "Calling term_find_files\n");
199    term_find_files((FF_PKT *)jcr->ff);
200    Dmsg0(100, "Done with term_find_files\n");
201    free_jcr(jcr);                     /* destroy JCR record */
202    Dmsg0(100, "Done with free_jcr\n");
203    return NULL;
204 }
205
206 /*
207  * Hello from Director he must identify himself and provide his 
208  *  password.
209  */
210 static int hello_cmd(JCR *jcr)
211 {
212    Dmsg0(120, "Calling Authenticate\n");
213    if (!authenticate_director(jcr)) {
214       return 0;
215    }
216    Dmsg0(120, "OK Authenticate\n");
217    jcr->authenticated = TRUE;
218    return 1;
219 }
220
221 /*
222  * Cancel a Job
223  */
224 static int cancel_cmd(JCR *jcr)
225 {
226    BSOCK *dir = jcr->dir_bsock;
227    char Job[MAX_NAME_LENGTH];
228    JCR *cjcr;
229
230    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
231       if (!(cjcr=get_jcr_by_full_name(Job))) {
232          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
233       } else {
234          set_jcr_job_status(cjcr, JS_Cancelled);
235          free_jcr(cjcr);
236          bnet_fsend(dir, "2001 Job %s marked to be cancelled.\n", Job);
237       }
238    } else {
239       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
240    }
241    bnet_sig(dir, BNET_EOD);
242    return 1;
243 }
244
245
246 /*
247  * Set debug level as requested by the Director
248  *
249  */
250 static int setdebug_cmd(JCR *jcr)
251 {
252    BSOCK *dir = jcr->dir_bsock;
253    int level;
254
255    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
256    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
257       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
258       return 0;   
259    }
260    debug_level = level;
261    return bnet_fsend(dir, OKsetdebug, level);
262 }
263
264
265 static int estimate_cmd(JCR *jcr)
266 {
267    BSOCK *dir = jcr->dir_bsock;
268    make_estimate(jcr);
269    return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
270 }
271
272 /*
273  * Get JobId and Storage Daemon Authorization key from Director
274  */
275 static int job_cmd(JCR *jcr)
276 {
277    BSOCK *dir = jcr->dir_bsock;
278    POOLMEM *sd_auth_key;
279
280    sd_auth_key = get_memory(dir->msglen);
281    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
282               &jcr->VolSessionId, &jcr->VolSessionTime,
283               sd_auth_key) != 5) {
284       bnet_fsend(dir, BADjob);
285       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
286       free_pool_memory(sd_auth_key);
287       return 0;
288    }
289    jcr->sd_auth_key = bstrdup(sd_auth_key);
290    free_pool_memory(sd_auth_key);
291    Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
292    return bnet_fsend(dir, OKjob);
293 }
294
295 /* 
296  * 
297  * Get list of files/directories to include from Director
298  *
299  */
300 static int include_cmd(JCR *jcr)
301 {
302    BSOCK *dir = jcr->dir_bsock;
303
304    while (bnet_recv(dir) >= 0) {
305        dir->msg[dir->msglen] = 0;
306        strip_trailing_junk(dir->msg);
307        Dmsg1(010, "include file: %s\n", dir->msg);
308        add_fname_to_include_list((FF_PKT *)jcr->ff, 1, dir->msg);
309    }
310
311    return bnet_fsend(dir, OKinc);
312 }
313
314 /*
315  * Get list of files to exclude from Director
316  *
317  */
318 static int exclude_cmd(JCR *jcr)
319 {
320    BSOCK *dir = jcr->dir_bsock;
321
322    while (bnet_recv(dir) >= 0) {
323        dir->msg[dir->msglen] = 0;
324        strip_trailing_junk(dir->msg);
325        add_fname_to_exclude_list((FF_PKT *)jcr->ff, dir->msg);
326        Dmsg1(110, "<dird: exclude file :%s:\n", dir->msg);
327    }
328
329    return bnet_fsend(dir, OKexc);
330 }
331
332
333 static int bootstrap_cmd(JCR *jcr)
334 {
335    BSOCK *dir = jcr->dir_bsock;
336    POOLMEM *fname = get_pool_memory(PM_FNAME);
337    FILE *bs;
338
339    if (jcr->RestoreBootstrap) {
340       unlink(jcr->RestoreBootstrap);
341       free_pool_memory(jcr->RestoreBootstrap);
342    }
343    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
344       jcr->Job);
345    Dmsg1(400, "bootstrap=%s\n", fname);
346    jcr->RestoreBootstrap = fname;
347    bs = fopen(fname, "a+");           /* create file */
348    if (!bs) {
349       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
350          jcr->RestoreBootstrap, strerror(errno));
351       free_pool_memory(jcr->RestoreBootstrap);
352       jcr->RestoreBootstrap = NULL;
353       set_jcr_job_status(jcr, JS_ErrorTerminated);
354       return 0;
355    }
356
357    while (bnet_recv(dir) >= 0) {
358        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
359        fputs(dir->msg, bs);
360    }
361    fclose(bs);
362
363    return bnet_fsend(dir, OKbootstrap);
364 }
365
366
367 /*
368  * Get backup level from Director
369  *
370  */
371 static int level_cmd(JCR *jcr)
372 {
373    BSOCK *dir = jcr->dir_bsock;
374    char *level;
375    struct tm tm;
376    time_t mtime;
377
378    level = (char *) get_memory(dir->msglen);
379    Dmsg1(110, "level_cmd: %s", dir->msg);
380    if (sscanf(dir->msg, "level = %s ", level) != 1) {
381       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
382       free_memory(level);
383       return 0;
384    }
385    /*
386     * Full backup requested
387     */
388    if (strcmp(level, "full") == 0) {
389       jcr->save_level = L_FULL;
390    /* 
391     * Backup requested since <date> <time>
392     *  This form is also used for incremental and differential
393     */
394    } else if (strcmp(level, "since") == 0) {
395       jcr->save_level = L_SINCE;
396       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
397                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
398                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
399          Jmsg1(jcr, M_FATAL, 0, "Bad scan of date/time: %s\n", dir->msg);
400          free_memory(level);
401          return 0;
402       }
403       tm.tm_year -= 1900;
404       tm.tm_mon  -= 1;
405       tm.tm_wday = tm.tm_yday = 0;              
406       tm.tm_isdst = -1;
407       mtime = mktime(&tm);
408       Dmsg1(100, "Got since time: %s", ctime(&mtime));
409       jcr->incremental = 1;
410       jcr->mtime = mtime;
411    } else {
412       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
413       free_memory(level);
414       return 0;
415    }
416    free_memory(level);
417    return bnet_fsend(dir, OKlevel);
418 }
419
420 /*
421  * Get session parameters from Director -- this is for a Restore command
422  */
423 static int session_cmd(JCR *jcr)
424 {
425    BSOCK *dir = jcr->dir_bsock;
426
427    Dmsg1(100, "SessionCmd: %s", dir->msg);
428    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
429               &jcr->VolSessionId, &jcr->VolSessionTime,
430               &jcr->StartFile, &jcr->EndFile, 
431               &jcr->StartBlock, &jcr->EndBlock) != 7) {
432       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
433       return 0;
434    }
435
436    return bnet_fsend(dir, OKsession);
437 }
438
439 /*
440  * Get address of storage daemon from Director
441  *
442  */
443 static int storage_cmd(JCR *jcr)
444 {
445    int stored_port;                /* storage daemon port */
446    BSOCK *dir = jcr->dir_bsock;
447    BSOCK *sd;                         /* storage daemon bsock */
448
449    Dmsg1(100, "StorageCmd: %s", dir->msg);
450    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port) != 2) {
451       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
452       return 0;
453    }
454    Dmsg2(110, "Open storage: %s:%d\n", jcr->stored_addr, stored_port);
455    /* Open command communications with Storage daemon */
456    /* Try to connect for 1 hour at 10 second intervals */
457    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
458                      jcr->stored_addr, NULL, stored_port, 1);
459    if (sd == NULL) {
460       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
461           jcr->stored_addr, stored_port);
462       return 0;
463    }
464
465    jcr->store_bsock = sd;
466
467    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
468    if (!authenticate_storagedaemon(jcr)) {
469       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
470       return 0;
471    }
472    Dmsg0(110, "Authenticated with SD.\n");
473
474    /* Send OK to Director */
475    return bnet_fsend(dir, OKstore);
476 }
477
478
479 /*  
480  * Do a backup. For now, we handle only Full and Incremental.
481  */
482 static int backup_cmd(JCR *jcr)
483
484    BSOCK *dir = jcr->dir_bsock;
485    BSOCK *sd = jcr->store_bsock;
486    int ok = 0;
487    int SDJobStatus;
488
489    set_jcr_job_status(jcr, JS_Blocked);
490    jcr->JobType = JT_BACKUP;
491    Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
492
493    if (sd == NULL) {
494       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
495       set_jcr_job_status(jcr, JS_ErrorTerminated);
496       goto cleanup;
497    }
498
499    bnet_fsend(dir, OKbackup);
500    Dmsg1(110, "bfiled>dird: %s", dir->msg);
501
502    /* 
503     * Send Append Open Session to Storage daemon
504     */
505    bnet_fsend(sd, append_open);
506    Dmsg1(110, ">stored: %s", sd->msg);
507    /* 
508     * Expect to receive back the Ticket number
509     */
510    if (bnet_recv(sd) >= 0) {
511       Dmsg1(110, "<stored: %s", sd->msg);
512       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
513          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
514          set_jcr_job_status(jcr, JS_ErrorTerminated);
515          goto cleanup;
516       }
517       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
518    } else {
519       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
520       set_jcr_job_status(jcr, JS_ErrorTerminated);
521       goto cleanup;
522    }
523
524    /* 
525     * Send Append data command to Storage daemon
526     */
527    bnet_fsend(sd, append_data, jcr->Ticket);
528    Dmsg1(110, ">stored: %s", sd->msg);
529
530    /* 
531     * Expect to get OK data 
532     */
533    Dmsg1(110, "<stored: %s", sd->msg);
534    if (!response(jcr, sd, OK_data, "Append Data")) {
535       set_jcr_job_status(jcr, JS_ErrorTerminated);
536       goto cleanup;
537    }
538       
539    /*
540     * Send Files to Storage daemon
541     */
542    Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
543    if (!blast_data_to_storage_daemon(jcr, NULL)) {
544       set_jcr_job_status(jcr, JS_ErrorTerminated);
545       bnet_suppress_error_messages(sd, 1);
546    } else {
547       set_jcr_job_status(jcr, JS_Terminated);
548       if (jcr->JobStatus != JS_Terminated) {
549          bnet_suppress_error_messages(sd, 1);
550          goto cleanup;                /* bail out now */
551       }
552       /* 
553        * Expect to get response to append_data from Storage daemon
554        */
555       if (!response(jcr, sd, OK_append, "Append Data")) {
556          set_jcr_job_status(jcr, JS_ErrorTerminated);
557          goto cleanup;
558       }
559      
560       /* 
561        * Send Append End Data to Storage daemon
562        */
563       bnet_fsend(sd, append_end, jcr->Ticket);
564       /* Get end OK */
565       if (!response(jcr, sd, OK_end, "Append End")) {
566          set_jcr_job_status(jcr, JS_ErrorTerminated);
567          goto cleanup;
568       }
569
570       /*
571        * Send Append Close to Storage daemon
572        */
573       bnet_fsend(sd, append_close, jcr->Ticket);
574       while (bnet_recv(sd) >= 0) {    /* stop on signal or error */
575          if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
576             ok = 1;
577             Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
578          }
579       }
580       if (!ok) {
581          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
582          set_jcr_job_status(jcr, JS_ErrorTerminated);
583          goto cleanup;
584       }
585       if (SDJobStatus != JS_Terminated) {
586          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
587             SDJobStatus);
588          set_jcr_job_status(jcr, JS_ErrorTerminated);
589       }
590    }
591
592 cleanup:
593
594    /* Inform Storage daemon that we are done */
595    if (sd) {
596       bnet_sig(sd, BNET_TERMINATE);
597    }
598
599    bnet_fsend(dir, EndBackup, jcr->JobStatus, jcr->JobFiles, jcr->ReadBytes, jcr->JobBytes);
600
601    /* Inform Director that we are done */
602    bnet_sig(dir, BNET_TERMINATE);
603
604    return 0;                          /* return and stop command loop */
605 }
606
607 /*  
608  * Do a Verify for Director
609  *
610  */
611 static int verify_cmd(JCR *jcr)
612
613    BSOCK *dir = jcr->dir_bsock;
614    BSOCK *sd  = jcr->store_bsock;
615    char level[100];
616
617    jcr->JobType = JT_VERIFY;
618    if (sscanf(dir->msg, verifycmd, level) != 1) {
619       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
620       return 0;   
621    }
622    if (strcasecmp(level, "init") == 0) {
623       jcr->JobLevel = L_VERIFY_INIT;
624    } else if (strcasecmp(level, "catalog") == 0){
625       jcr->JobLevel = L_VERIFY_CATALOG;
626    } else if (strcasecmp(level, "volume") == 0){
627       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
628    } else if (strcasecmp(level, "data") == 0){
629       jcr->JobLevel = L_VERIFY_DATA;
630    } else {   
631       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
632       return 0;   
633    }
634
635    bnet_fsend(dir, OKverify);
636    Dmsg1(110, "bfiled>dird: %s", dir->msg);
637
638    switch (jcr->JobLevel) {
639    case L_VERIFY_INIT:
640    case L_VERIFY_CATALOG:
641       do_verify(jcr);
642       break;
643    case L_VERIFY_VOLUME_TO_CATALOG:
644       if (!open_sd_read_session(jcr)) {
645          return 0;
646       }
647       do_verify_volume(jcr);
648       /* 
649        * Send Close session command to Storage daemon
650        */
651       bnet_fsend(sd, read_close, jcr->Ticket);
652       Dmsg1(130, "bfiled>stored: %s", sd->msg);
653
654       /* ****FIXME**** check response */
655       bnet_recv(sd);                     /* get OK */
656
657       /* Inform Storage daemon that we are done */
658       bnet_sig(sd, BNET_TERMINATE);
659
660       break;
661    default:
662       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
663       return 0; 
664    }
665
666    /* Inform Director that we are done */
667    bnet_sig(dir, BNET_TERMINATE);
668    return 0;                          /* return and terminate command loop */
669 }
670
671 /*  
672  * Do a Restore for Director
673  *
674  */
675 static int restore_cmd(JCR *jcr)
676
677    BSOCK *dir = jcr->dir_bsock;
678    BSOCK *sd = jcr->store_bsock;
679    POOLMEM *where;
680    char replace;
681
682    /*
683     * Scan WHERE (base directory for restore) from command
684     */
685    Dmsg0(150, "restore command\n");
686    /* Pickup where string */
687    where = get_memory(dir->msglen+1);
688    *where = 0;
689
690    if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
691       if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
692          Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
693          return 0;
694       }
695       *where = 0;
696    }
697    /* Turn / into nothing */
698    if (where[0] == '/' && where[1] == 0) {
699       where[0] = 0;
700    }
701       
702    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
703    unbash_spaces(where);
704    jcr->where = where;
705    jcr->replace = replace;
706
707    bnet_fsend(dir, OKrestore);
708    Dmsg1(110, "bfiled>dird: %s", dir->msg);
709
710    jcr->JobType = JT_RESTORE;
711
712    set_jcr_job_status(jcr, JS_Blocked);
713    if (!open_sd_read_session(jcr)) {
714       set_jcr_job_status(jcr, JS_ErrorTerminated);
715       goto bail_out;
716    }
717
718    set_jcr_job_status(jcr, JS_Running);
719
720    /* 
721     * Do restore of files and data
722     */
723    do_restore(jcr);
724
725    set_jcr_job_status(jcr, JS_Terminated);
726    if (jcr->JobStatus != JS_Terminated) {
727       bnet_suppress_error_messages(sd, 1);
728    }
729
730    /* 
731     * Send Close session command to Storage daemon
732     */
733    bnet_fsend(sd, read_close, jcr->Ticket);
734    Dmsg1(130, "bfiled>stored: %s", sd->msg);
735
736    bnet_recv(sd);                     /* get OK */
737
738    /* Inform Storage daemon that we are done */
739    bnet_sig(sd, BNET_TERMINATE);
740
741 bail_out:
742    /* Send termination status back to Dir */
743    bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined, 
744               jcr->JobBytes);
745
746    /* Inform Director that we are done */
747    bnet_sig(dir, BNET_TERMINATE);
748
749    Dmsg0(130, "Done in job.c\n");
750    return 0;                          /* return and terminate command loop */
751 }
752
753 static int open_sd_read_session(JCR *jcr)
754 {
755    BSOCK *sd = jcr->store_bsock;
756
757    if (!sd) {
758       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
759       return 0;
760    }
761    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
762       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
763    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
764    /* 
765     * Open Read Session with Storage daemon
766     */
767    bnet_fsend(sd, read_open, jcr->VolumeName,
768       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
769       jcr->StartBlock, jcr->EndBlock);
770    Dmsg1(110, ">stored: %s", sd->msg);
771
772    /* 
773     * Get ticket number
774     */
775    if (bnet_recv(sd) >= 0) {
776       Dmsg1(110, "bfiled<stored: %s", sd->msg);
777       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
778          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
779          return 0;
780       }
781       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
782    } else {
783       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
784       return 0;
785    }
786
787    if (!send_bootstrap_file(jcr)) {
788       return 0;
789    }
790
791    /* 
792     * Start read of data with Storage daemon
793     */
794    bnet_fsend(sd, read_data, jcr->Ticket);
795    Dmsg1(110, ">stored: %s", sd->msg);
796
797    /* 
798     * Get OK data
799     */
800    if (!response(jcr, sd, OK_data, "Read Data")) {
801       return 0;
802    }
803    return 1;
804 }
805
806 /* 
807  * Destroy the Job Control Record and associated
808  * resources (sockets).
809  */
810 static void filed_free_jcr(JCR *jcr) 
811 {
812    if (jcr->store_bsock) {
813       bnet_close(jcr->store_bsock);
814    }
815    if (jcr->where) {
816       free_pool_memory(jcr->where);
817    }
818    if (jcr->RestoreBootstrap) {
819       unlink(jcr->RestoreBootstrap);
820       free_pool_memory(jcr->RestoreBootstrap);
821    }
822    if (jcr->last_fname) {
823       free_pool_memory(jcr->last_fname);
824    }
825    return;
826 }
827
828 /*
829  * Get response from Storage daemon to a command we
830  * sent. Check that the response is OK.
831  *
832  *  Returns: 0 on failure
833  *           1 on success
834  */
835 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
836 {
837    int n;
838
839    if (sd->errors) {
840       return 0;
841    }
842    if ((n = bnet_recv(sd)) > 0) {
843       Dmsg0(110, sd->msg);
844       if (strcmp(sd->msg, resp) == 0) {
845          return 1;
846       }
847    } 
848    if (is_bnet_error(sd)) {
849       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
850          cmd, bnet_strerror(sd));
851    } else {
852       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
853          cmd, resp, sd->msg);
854    }
855    return 0;
856 }
857
858 static int send_bootstrap_file(JCR *jcr)
859 {
860    FILE *bs;
861    char buf[2000];
862    BSOCK *sd = jcr->store_bsock;
863    char *bootstrap = "bootstrap\n";
864
865    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
866    if (!jcr->RestoreBootstrap) {
867       return 1;
868    }
869    bs = fopen(jcr->RestoreBootstrap, "r");
870    if (!bs) {
871       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
872          jcr->RestoreBootstrap, strerror(errno));
873       set_jcr_job_status(jcr, JS_ErrorTerminated);
874       return 0;
875    }
876    strcpy(sd->msg, bootstrap);  
877    sd->msglen = strlen(sd->msg);
878    bnet_send(sd);
879    while (fgets(buf, sizeof(buf), bs)) {
880       sd->msglen = Mmsg(&sd->msg, "%s", buf);
881       bnet_send(sd);       
882    }
883    bnet_sig(sd, BNET_EOD);
884    fclose(bs);
885    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
886       set_jcr_job_status(jcr, JS_ErrorTerminated);
887       return 0;
888    }
889    return 1;
890 }