]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
Basic Restore bootstrap implemented -- kes25Jun02
[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, 2001, 2002 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
32 extern char my_name[];
33 extern CLIENT *me;                    /* our client resource */
34                         
35 /* Imported functions */
36 extern int status_cmd(JCR *jcr);
37                                    
38 /* Forward referenced functions */
39 static int backup_cmd(JCR *jcr);
40 static int bootstrap_cmd(JCR *jcr);
41 static int cancel_cmd(JCR *jcr);
42 static int setdebug_cmd(JCR *jcr);
43 static int estimate_cmd(JCR *jcr);
44 static int exclude_cmd(JCR *jcr);
45 static int hello_cmd(JCR *jcr);
46 static int job_cmd(JCR *jcr);
47 static int include_cmd(JCR *jcr);
48 static int level_cmd(JCR *jcr);
49 static int verify_cmd(JCR *jcr);
50 static int restore_cmd(JCR *jcr);
51 static int storage_cmd(JCR *jcr);
52 static int session_cmd(JCR *jcr);
53 static int response(BSOCK *sd, char *resp, char *cmd);
54 static void filed_free_jcr(JCR *jcr);
55 static int open_sd_read_session(JCR *jcr);
56 static int send_bootstrap_file(JCR *jcr);
57
58
59 /* Exported functions */
60
61 struct s_cmds {
62    char *cmd;
63    int (*func)(JCR *);
64 };
65
66 /*  
67  * The following are the recognized commands from the Director. 
68  */
69 static struct s_cmds cmds[] = {
70    {"backup",   backup_cmd},
71    {"cancel",   cancel_cmd},
72    {"setdebug=", setdebug_cmd},
73    {"estimate", estimate_cmd},
74    {"exclude",  exclude_cmd},
75    {"Hello",    hello_cmd},
76    {"include",  include_cmd},
77    {"JobId=",   job_cmd},
78    {"level = ", level_cmd},
79    {"restore",  restore_cmd},
80    {"session",  session_cmd},
81    {"status",   status_cmd},
82    {"storage ", storage_cmd},
83    {"verify",   verify_cmd},
84    {"bootstrap",bootstrap_cmd},
85    {NULL,       NULL}                  /* list terminator */
86 };
87
88 /* Commands received from director that need scanning */
89 static char jobcmd[]     = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
90 static char storaddr[]   = "storage address=%s port=%d\n";
91 static char sessioncmd[] = "session %s %ld %ld %ld %ld %ld %ld\n";
92 static char restorecmd[] = "restore where=%s\n";
93 static char verifycmd[]  = "verify level=%20s\n";
94
95 /* Responses sent to Director */
96 static char errmsg[]       = "2999 Invalid command\n";
97 static char no_auth[]      = "2998 No Authorization\n";
98 static char OKinc[]        = "2000 OK include\n";
99 static char OKest[]        = "2000 OK estimate files=%ld bytes=%ld\n";
100 static char OKexc[]        = "2000 OK exclude\n";
101 static char OKlevel[]      = "2000 OK level\n";
102 static char OKbackup[]     = "2000 OK backup\n";
103 static char OKbootstrap[]  = "2000 OK bootstrap\n";
104 static char OKverify[]     = "2000 OK verify\n";
105 static char OKrestore[]    = "2000 OK restore\n";
106 static char OKsession[]    = "2000 OK session\n";
107 static char OKstore[]      = "2000 OK storage\n";
108 static char OKjob[]        = "2000 OK Job\n";
109 static char OKsetdebug[]   = "2000 OK setdebug=%d\n";
110 static char BADjob[]       = "2901 Bad Job\n";
111
112 /* Responses received from Storage Daemon */
113 static char OK_end[]       = "3000 OK end\n";
114 static char OK_open[]      = "3000 OK open ticket = %d\n";
115 static char OK_data[]      = "3000 OK data\n";
116 static char OK_append[]    = "3000 OK append data\n";
117 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
118
119
120 /* Commands sent to Storage Daemon */
121 static char append_open[]  = "append open session\n";
122 static char append_data[]  = "append data %d\n";
123 static char append_end[]   = "append end session %d\n";
124 static char append_close[] = "append close session %d\n";
125 static char read_open[]    = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
126 static char read_data[]    = "read data %d\n";
127 static char read_close[]   = "read close session %d\n";
128
129 /* 
130  * Accept requests from a Director
131  *
132  * NOTE! We are running as a separate thread
133  *
134  * Send output one line
135  * at a time followed by a zero length transmission.
136  *
137  * Return when the connection is terminated or there
138  * is an error.
139  *
140  * Basic task here is:
141  *   Authenticate Director (during Hello command).
142  *   Accept commands one at a time from the Director
143  *     and execute them.
144  *
145  */
146 void *handle_client_request(void *dirp)
147 {
148    int i, found, quit;
149    JCR *jcr;
150    BSOCK *dir = (BSOCK *)dirp;
151
152    jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
153    jcr->dir_bsock = dir;
154    jcr->ff = init_find_files();
155    jcr->start_time = time(NULL);
156    jcr->last_fname = get_pool_memory(PM_FNAME);
157    jcr->client_name = get_memory(strlen(my_name) + 1);
158    strcpy(jcr->client_name, my_name);
159
160    /**********FIXME******* add command handler error code */
161
162    for (quit=0; !quit;) {
163
164       /* Read command */
165       if (bnet_recv(dir) <= 0) {
166          break;                       /* connection terminated */
167       }
168       dir->msg[dir->msglen] = 0;
169       Dmsg1(9, "<dird: %s", dir->msg);
170       found = FALSE;
171       for (i=0; cmds[i].cmd; i++) {
172          if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
173             if (!jcr->authenticated && cmds[i].func != hello_cmd) {
174                bnet_fsend(dir, no_auth);
175                break;
176             }
177             if (!cmds[i].func(jcr)) {    /* do command */
178                quit = TRUE;              /* error, get out */
179                Dmsg0(20, "Command error\n");
180             }
181             found = TRUE;            /* indicate command found */
182             break;
183          }
184       }
185       if (!found) {                   /* command not found */
186          bnet_fsend(dir, errmsg);
187          quit = TRUE;
188          break;
189       }
190    }
191    Dmsg0(20, "Calling term_find_files\n");
192    term_find_files(jcr->ff);
193    Dmsg0(20, "Done with term_find_files\n");
194    free_jcr(jcr);                     /* destroy JCR record */
195    Dmsg0(20, "Done with free_jcr\n");
196    return NULL;
197 }
198
199 /*
200  * Hello from Director he must identify himself and provide his 
201  *  password.
202  */
203 static int hello_cmd(JCR *jcr)
204 {
205    Dmsg0(20, "Calling Authenticate\n");
206    if (!authenticate_director(jcr)) {
207       return 0;
208    }
209    Dmsg0(20, "OK Authenticate\n");
210    jcr->authenticated = TRUE;
211    return 1;
212 }
213
214 /*
215  * Cancel a Job
216  */
217 static int cancel_cmd(JCR *jcr)
218 {
219    BSOCK *dir = jcr->dir_bsock;
220    char Job[MAX_NAME_LENGTH];
221    JCR *cjcr;
222
223    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
224       if (!(cjcr=get_jcr_by_full_name(Job))) {
225          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
226       } else {
227          cjcr->JobStatus = JS_Cancelled;
228          free_jcr(cjcr);
229          bnet_fsend(dir, "2001 Job %s marked to be cancelled.\n", Job);
230       }
231    } else {
232       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
233    }
234    bnet_sig(dir, BNET_EOF);
235    return 1;
236 }
237
238
239 /*
240  * Set debug level as requested by the Director
241  *
242  */
243 static int setdebug_cmd(JCR *jcr)
244 {
245    BSOCK *dir = jcr->dir_bsock;
246    int level;
247
248    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
249    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
250       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
251       return 0;   
252    }
253    debug_level = level;
254    return bnet_fsend(dir, OKsetdebug, level);
255 }
256
257
258 static int estimate_cmd(JCR *jcr)
259 {
260    BSOCK *dir = jcr->dir_bsock;
261    make_estimate(jcr);
262    return bnet_fsend(dir, OKest, jcr->JobFiles, jcr->JobBytes);
263 }
264
265 /*
266  * Get JobId and Storage Daemon Authorization key from Director
267  */
268 static int job_cmd(JCR *jcr)
269 {
270    BSOCK *dir = jcr->dir_bsock;
271    POOLMEM *sd_auth_key;
272
273    sd_auth_key = get_memory(dir->msglen);
274    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
275               &jcr->VolSessionId, &jcr->VolSessionTime,
276               sd_auth_key) != 5) {
277       bnet_fsend(dir, BADjob);
278       Emsg1(M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
279       free_pool_memory(sd_auth_key);
280       return 0;
281    }
282    jcr->sd_auth_key = bstrdup(sd_auth_key);
283    free_pool_memory(sd_auth_key);
284    Dmsg2(20, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
285    return bnet_fsend(dir, OKjob);
286 }
287
288 /* 
289  * 
290  * Get list of files/directories to include from Director
291  *
292  */
293 static int include_cmd(JCR *jcr)
294 {
295    BSOCK *dir = jcr->dir_bsock;
296
297    while (bnet_recv(dir) > 0) {
298        dir->msg[dir->msglen] = 0;
299        strip_trailing_junk(dir->msg);
300        Dmsg1(10, "filed<dird: include file %s\n", dir->msg);
301        add_fname_to_include_list(jcr->ff, 1, dir->msg);
302    }
303
304    return bnet_fsend(dir, OKinc);
305 }
306
307 /*
308  * Get list of files to exclude from Director
309  *
310  */
311 static int exclude_cmd(JCR *jcr)
312 {
313    BSOCK *dir = jcr->dir_bsock;
314    char *p;  
315
316    while (bnet_recv(dir) > 0) {
317        dir->msg[dir->msglen] = 0;
318        strip_trailing_junk(dir->msg);
319        /* Skip leading options */
320        for (p=dir->msg; *p && *p != ' '; p++)
321           { }
322        /* Skip spaces */
323        for ( ; *p && *p == ' '; p++)
324           { }
325        add_fname_to_exclude_list(jcr->ff, p);
326        Dmsg1(10, "<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       return 0;
354    }
355
356    while (bnet_recv(dir) > 0) {
357        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
358        fputs(dir->msg, bs);
359    }
360    fclose(bs);
361
362    return bnet_fsend(dir, OKbootstrap);
363 }
364
365
366 /*
367  * Get backup level from Director
368  *
369  */
370 static int level_cmd(JCR *jcr)
371 {
372    BSOCK *dir = jcr->dir_bsock;
373    char *level;
374    struct tm tm;
375    time_t mtime;
376
377    level = (char *) get_memory(dir->msglen);
378    Dmsg1(10, "level_cmd: %s", dir->msg);
379    if (sscanf(dir->msg, "level = %s ", level) != 1) {
380       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
381       free_memory(level);
382       return 0;
383    }
384    /*
385     * Full backup requested
386     */
387    if (strcmp(level, "full") == 0) {
388       jcr->save_level = L_FULL;
389    /* 
390     * Backup requested since <date> <time>
391     *  This form is also used for incremental and differential
392     */
393    } else if (strcmp(level, "since") == 0) {
394       jcr->save_level = L_SINCE;
395       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
396                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
397                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
398          Jmsg1(jcr, M_FATAL, 0, "Bad scan of date/time: %s\n", dir->msg);
399          free_memory(level);
400          return 0;
401       }
402       tm.tm_year -= 1900;
403       tm.tm_mon  -= 1;
404       tm.tm_wday = tm.tm_yday = 0;              
405       tm.tm_isdst = -1;
406       mtime = mktime(&tm);
407       Dmsg1(90, "Got since time: %s", ctime(&mtime));
408       jcr->incremental = 1;
409       jcr->mtime = mtime;
410    } else {
411       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
412       free_memory(level);
413       return 0;
414    }
415    free_memory(level);
416    return bnet_fsend(dir, OKlevel);
417 }
418
419 /*
420  * Get session parameters from Director -- this is for a Restore command
421  */
422 static int session_cmd(JCR *jcr)
423 {
424    BSOCK *dir = jcr->dir_bsock;
425
426    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
427               &jcr->VolSessionId, &jcr->VolSessionTime,
428               &jcr->StartFile, &jcr->EndFile, 
429               &jcr->StartBlock, &jcr->EndBlock) != 7) {
430       Emsg1(M_FATAL, 0, "Bad session command: %s", dir->msg);
431       return 0;
432    }
433
434    return bnet_fsend(dir, OKsession);
435 }
436
437 /*
438  * Get address of storage daemon from Director
439  *
440  */
441 static int storage_cmd(JCR *jcr)
442 {
443    int stored_port;                /* storage daemon port */
444    BSOCK *dir = jcr->dir_bsock;
445    BSOCK *sd;                         /* storage daemon bsock */
446
447    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port) != 2) {
448       Emsg1(M_FATAL, 0, _("Bad storage command: %s\n"), dir->msg);
449       return 0;
450    }
451    Dmsg2(30, "Got storage: %s:%d\n", jcr->stored_addr, stored_port);
452    /* Open command communications with Storage daemon */
453    /* Try to connect for 1 hour at 10 second intervals */
454    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
455                      jcr->stored_addr, NULL, stored_port, 1);
456    if (sd == NULL) {
457       Jmsg2(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
458           jcr->stored_addr, stored_port);
459       return 0;
460    }
461
462    jcr->store_bsock = sd;
463
464    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
465    if (!authenticate_storagedaemon(jcr)) {
466       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
467       return 0;
468    }
469
470    /* Send OK to Director */
471    return bnet_fsend(dir, OKstore);
472 }
473
474
475 /*  
476  * Do a backup. For now, we handle only Full and Incremental.
477  */
478 static int backup_cmd(JCR *jcr)
479
480    int data_port;
481    BSOCK *dir = jcr->dir_bsock;
482    BSOCK *sd = jcr->store_bsock;
483    int len;
484
485    jcr->JobStatus = JS_Blocked;
486    jcr->JobType = JT_BACKUP;
487    Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
488
489    if (sd == NULL) {
490       Emsg0(M_FATAL, 0, _("Cannot contact Storage daemon\n"));
491       jcr->JobStatus = JS_ErrorTerminated;
492       goto cleanup;
493    }
494
495    bnet_fsend(dir, OKbackup);
496    Dmsg1(10, "bfiled>dird: %s", dir->msg);
497
498    /* 
499     * Send Append Open Session to Storage daemon
500     */
501    bnet_fsend(sd, append_open);
502    Dmsg1(10, ">stored: %s", sd->msg);
503    /* 
504     * Expect to receive back the Ticket number
505     */
506    if (bnet_recv(sd) > 0) {
507       Dmsg1(10, "<stored: %s", sd->msg);
508       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
509          Emsg1(M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
510          jcr->JobStatus = JS_ErrorTerminated;
511          goto cleanup;
512       }
513       Dmsg1(10, "Got Ticket=%d\n", jcr->Ticket);
514    } else {
515       Emsg0(M_FATAL, 0, _("Bad response from stored to open command\n"));
516       jcr->JobStatus = JS_ErrorTerminated;
517       goto cleanup;
518    }
519
520    /* 
521     * Send Append data command to Storage daemon
522     */
523    bnet_fsend(sd, append_data, jcr->Ticket);
524    Dmsg1(10, ">stored: %s", sd->msg);
525
526    /* 
527     * Expect to get OK data 
528     */
529    Dmsg1(10, "<stored: %s", sd->msg);
530    if (!response(sd, OK_data, "Append Data")) {
531       jcr->JobStatus = JS_ErrorTerminated;
532       goto cleanup;
533    }
534       
535    /*
536     * Send Files to Storage daemon
537     */
538    Dmsg1(100, "begin blast ff=%p\n", jcr->ff);
539    if (!blast_data_to_storage_daemon(jcr, NULL, data_port)) {
540       jcr->JobStatus = JS_ErrorTerminated;
541    } else {
542       jcr->JobStatus = JS_Terminated;
543       /* 
544        * Expect to get response to append_data from Storage daemon
545        */
546       if (!response(sd, OK_append, "Append Data")) {
547          jcr->JobStatus = JS_ErrorTerminated;
548          goto cleanup;
549       }
550      
551       /* 
552        * Send Append End Data to Storage daemon
553        */
554       bnet_fsend(sd, append_end, jcr->Ticket);
555       /* Get end OK */
556       if (!response(sd, OK_end, "Append End")) {
557          jcr->JobStatus = JS_ErrorTerminated;
558          goto cleanup;
559       }
560
561       /*
562        * Send Append Close to Storage daemon
563        */
564       bnet_fsend(sd, append_close, jcr->Ticket);
565       while ((len = bnet_recv(sd)) > 0) {
566           /* discard anything else returned from SD */
567       }
568       if (len < 0) {
569          Emsg2(M_FATAL, 0, _("<stored: net_recv len=%d: ERR=%s\n"), len, bnet_strerror(sd));
570          jcr->JobStatus = JS_ErrorTerminated;
571       }
572    }
573
574 cleanup:
575
576    /* Inform Storage daemon that we are done */
577    if (sd) {
578       bnet_sig(sd, BNET_EOF);
579    }
580
581    /* Inform Director that we are done */
582    bnet_sig(dir, BNET_EOF);
583
584    return jcr->JobStatus == JS_Terminated;
585 }
586
587 /*  
588  * Do a Verify for Director
589  *
590  */
591 static int verify_cmd(JCR *jcr)
592
593    BSOCK *dir = jcr->dir_bsock;
594    char level[100];
595
596    jcr->JobType = JT_VERIFY;
597    if (sscanf(dir->msg, verifycmd, level) != 1) {
598       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
599       return 0;   
600    }
601    if (strcasecmp(level, "init") == 0) {
602       jcr->JobLevel = L_VERIFY_INIT;
603    } else if (strcasecmp(level, "catalog") == 0){
604       jcr->JobLevel = L_VERIFY_CATALOG;
605    } else if (strcasecmp(level, "volume") == 0){
606       jcr->JobLevel = L_VERIFY_VOLUME;
607    } else if (strcasecmp(level, "data") == 0){
608       jcr->JobLevel = L_VERIFY_DATA;
609    } else {   
610       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
611       return 0;   
612    }
613
614    bnet_fsend(dir, OKverify);
615    Dmsg1(10, "bfiled>dird: %s", dir->msg);
616
617    do_verify(jcr);
618
619    /* Inform Director that we are done */
620    return bnet_sig(dir, BNET_EOF);
621 }
622
623 /*  
624  * Do a Restore for Director
625  *
626  */
627 static int restore_cmd(JCR *jcr)
628
629    BSOCK *dir = jcr->dir_bsock;
630    BSOCK *sd = jcr->store_bsock;
631    POOLMEM *where;
632
633    /*
634     * Scan WHERE (base directory for restore) from command
635     */
636    Dmsg0(50, "restore command\n");
637    /* Pickup where string */
638    where = get_memory(dir->msglen+1);
639    *where = 0;
640    sscanf(dir->msg, restorecmd, where);
641    Dmsg1(50, "Got where=%s\n", where);
642    jcr->where = where;
643
644    bnet_fsend(dir, OKrestore);
645    Dmsg1(10, "bfiled>dird: %s", dir->msg);
646
647    jcr->JobType = JT_RESTORE;
648    jcr->JobStatus = JS_Blocked;
649
650    if (!open_sd_read_session(jcr)) {
651       return 0;
652    }
653
654    /* 
655     * Do restore of files and data
656     */
657    do_restore(jcr);
658
659    /* 
660     * Send Close session command to Storage daemon
661     */
662    bnet_fsend(sd, read_close, jcr->Ticket);
663    Dmsg1(30, "bfiled>stored: %s", sd->msg);
664
665    /* ****FIXME**** check response */
666    bnet_recv(sd);                     /* get OK */
667
668    /* Inform Storage daemon that we are done */
669    bnet_sig(sd, BNET_EOF);
670
671    /* Inform Director that we are done */
672    bnet_sig(dir, BNET_EOF);
673
674    Dmsg0(30, "Done in job.c\n");
675    return 1;
676 }
677
678 static int open_sd_read_session(JCR *jcr)
679 {
680    int len;
681    BSOCK *sd = jcr->store_bsock;
682
683    if (!sd) {
684       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
685       return 0;
686    }
687    Dmsg4(20, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
688       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
689    Dmsg2(20, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
690    /* 
691     * Open Read Session with Storage daemon
692     */
693    bnet_fsend(sd, read_open, jcr->VolumeName,
694       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
695       jcr->StartBlock, jcr->EndBlock);
696    Dmsg1(10, ">stored: %s", sd->msg);
697
698    /* 
699     * Get ticket number
700     */
701    if ((len = bnet_recv(sd)) > 0) {
702       Dmsg1(10, "bfiled<stored: %s", sd->msg);
703       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
704          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
705          return 0;
706       }
707       Dmsg1(10, "bfiled: got Ticket=%d\n", jcr->Ticket);
708    } else {
709       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
710       return 0;
711    }
712
713    if (!send_bootstrap_file(jcr)) {
714       return 0;
715    }
716
717    /* 
718     * Start read of data with Storage daemon
719     */
720    bnet_fsend(sd, read_data, jcr->Ticket);
721    Dmsg1(10, ">stored: %s", sd->msg);
722
723    /* 
724     * Get OK data
725     */
726    if (!response(sd, OK_data, "Read Data")) {
727       return 0;
728    }
729    return 1;
730 }
731
732 /* 
733  * Destroy the Job Control Record and associated
734  * resources (sockets).
735  */
736 static void filed_free_jcr(JCR *jcr) 
737 {
738    if (jcr->store_bsock) {
739       bnet_close(jcr->store_bsock);
740    }
741    if (jcr->where) {
742       free_pool_memory(jcr->where);
743    }
744    if (jcr->RestoreBootstrap) {
745       unlink(jcr->RestoreBootstrap);
746       free_pool_memory(jcr->RestoreBootstrap);
747    }
748    if (jcr->last_fname) {
749       free_pool_memory(jcr->last_fname);
750    }
751    return;
752 }
753
754 /*
755  * Get response from Storage daemon to a command we
756  * sent. Check that the response is OK.
757  *
758  *  Returns: 0 on failure
759  *           1 on success
760  */
761 int response(BSOCK *sd, char *resp, char *cmd)
762 {
763    int n;
764
765    if (sd->errors) {
766       return 0;
767    }
768    if ((n = bnet_recv(sd)) > 0) {
769       Dmsg0(10, sd->msg);
770       if (strcmp(sd->msg, resp) == 0) {
771          return 1;
772       }
773    } 
774    /* ********FIXME******** segfault if the following is executed */
775    if (n > 0) {
776       Emsg3(M_FATAL, 0, _("<stored: bad response to %s: wanted: %s, got: %s\n"),
777          cmd, resp, sd->msg);
778    } else {
779       Emsg2(M_FATAL, 0, _("<stored: bad response to %s command: ERR=%s\n"),
780          cmd, bnet_strerror(sd));
781    }
782    return 0;
783 }
784
785 static int send_bootstrap_file(JCR *jcr)
786 {
787    FILE *bs;
788    char buf[1000];
789    BSOCK *sd = jcr->store_bsock;
790    char *bootstrap = "bootstrap\n";
791
792    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
793    if (!jcr->RestoreBootstrap) {
794       return 1;
795    }
796    bs = fopen(jcr->RestoreBootstrap, "r");
797    if (!bs) {
798       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
799          jcr->RestoreBootstrap, strerror(errno));
800       jcr->JobStatus = JS_ErrorTerminated;
801       return 0;
802    }
803    strcpy(sd->msg, bootstrap);  
804    sd->msglen = strlen(sd->msg);
805    bnet_send(sd);
806    while (fgets(buf, sizeof(buf), bs)) {
807       sd->msglen = Mmsg(&sd->msg, "%s", buf);
808       bnet_send(sd);       
809    }
810    bnet_sig(sd, BNET_EOF);
811    fclose(bs);
812    if (!response(sd, OKSDbootstrap, "Bootstrap")) {
813       jcr->JobStatus = JS_ErrorTerminated;
814       return 0;
815    }
816    return 1;
817 }