]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
7cf38b46f1ca47fd7820a186a72ab39588e9cb59
[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 #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 verifycmd[]  = "verify level=%20s\n";
95
96 /* Responses sent to Director */
97 static char errmsg[]       = "2999 Invalid command\n";
98 static char no_auth[]      = "2998 No Authorization\n";
99 static char OKinc[]        = "2000 OK include\n";
100 static char OKest[]        = "2000 OK estimate files=%ld bytes=%ld\n";
101 static char OKexc[]        = "2000 OK exclude\n";
102 static char OKlevel[]      = "2000 OK level\n";
103 static char OKbackup[]     = "2000 OK backup\n";
104 static char OKbootstrap[]  = "2000 OK bootstrap\n";
105 static char OKverify[]     = "2000 OK verify\n";
106 static char OKrestore[]    = "2000 OK restore\n";
107 static char OKsession[]    = "2000 OK session\n";
108 static char OKstore[]      = "2000 OK storage\n";
109 static char OKjob[]        = "2000 OK Job " FDHOST "," DISTNAME "," DISTVER;
110 static char OKsetdebug[]   = "2000 OK setdebug=%d\n";
111 static char BADjob[]       = "2901 Bad Job\n";
112 static char EndRestore[]   = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%" lld "\n";
113 static char EndBackup[]    = "2801 End Backup Job TermCode=%d JobFiles=%u ReadBytes=%" lld " JobBytes=%" lld "\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    strcpy(jcr->client_name, my_name);
164    dir->jcr = (void *)jcr;
165
166    /**********FIXME******* add command handler error code */
167
168    for (quit=0; !quit;) {
169
170       /* Read command */
171       if (bnet_recv(dir) < 0) {
172          break;                       /* connection terminated */
173       }
174       dir->msg[dir->msglen] = 0;
175       Dmsg1(100, "<dird: %s", dir->msg);
176       found = FALSE;
177       for (i=0; cmds[i].cmd; i++) {
178          if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
179             if (!jcr->authenticated && cmds[i].func != hello_cmd) {
180                bnet_fsend(dir, no_auth);
181                break;
182             }
183             if (!cmds[i].func(jcr)) {    /* do command */
184                quit = TRUE;              /* error, get out */
185                Pmsg0(20, "Command error\n");
186             }
187             found = TRUE;            /* indicate command found */
188             break;
189          }
190       }
191       if (!found) {                   /* command not found */
192          bnet_fsend(dir, errmsg);
193          quit = TRUE;
194          break;
195       }
196    }
197    Dmsg0(100, "Calling term_find_files\n");
198    term_find_files(jcr->ff);
199    Dmsg0(100, "Done with term_find_files\n");
200    free_jcr(jcr);                     /* destroy JCR record */
201    Dmsg0(100, "Done with free_jcr\n");
202    return NULL;
203 }
204
205 /*
206  * Hello from Director he must identify himself and provide his 
207  *  password.
208  */
209 static int hello_cmd(JCR *jcr)
210 {
211    Dmsg0(120, "Calling Authenticate\n");
212    if (!authenticate_director(jcr)) {
213       return 0;
214    }
215    Dmsg0(120, "OK Authenticate\n");
216    jcr->authenticated = TRUE;
217    return 1;
218 }
219
220 /*
221  * Cancel a Job
222  */
223 static int cancel_cmd(JCR *jcr)
224 {
225    BSOCK *dir = jcr->dir_bsock;
226    char Job[MAX_NAME_LENGTH];
227    JCR *cjcr;
228
229    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
230       if (!(cjcr=get_jcr_by_full_name(Job))) {
231          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
232       } else {
233          cjcr->JobStatus = JS_Cancelled;
234          free_jcr(cjcr);
235          bnet_fsend(dir, "2001 Job %s marked to be cancelled.\n", Job);
236       }
237    } else {
238       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
239    }
240    bnet_sig(dir, BNET_EOD);
241    return 1;
242 }
243
244
245 /*
246  * Set debug level as requested by the Director
247  *
248  */
249 static int setdebug_cmd(JCR *jcr)
250 {
251    BSOCK *dir = jcr->dir_bsock;
252    int level;
253
254    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
255    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
256       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
257       return 0;   
258    }
259    debug_level = level;
260    return bnet_fsend(dir, OKsetdebug, level);
261 }
262
263
264 static int estimate_cmd(JCR *jcr)
265 {
266    BSOCK *dir = jcr->dir_bsock;
267    make_estimate(jcr);
268    return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
269 }
270
271 /*
272  * Get JobId and Storage Daemon Authorization key from Director
273  */
274 static int job_cmd(JCR *jcr)
275 {
276    BSOCK *dir = jcr->dir_bsock;
277    POOLMEM *sd_auth_key;
278
279    sd_auth_key = get_memory(dir->msglen);
280    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
281               &jcr->VolSessionId, &jcr->VolSessionTime,
282               sd_auth_key) != 5) {
283       bnet_fsend(dir, BADjob);
284       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
285       free_pool_memory(sd_auth_key);
286       return 0;
287    }
288    jcr->sd_auth_key = bstrdup(sd_auth_key);
289    free_pool_memory(sd_auth_key);
290    Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
291    return bnet_fsend(dir, OKjob);
292 }
293
294 /* 
295  * 
296  * Get list of files/directories to include from Director
297  *
298  */
299 static int include_cmd(JCR *jcr)
300 {
301    BSOCK *dir = jcr->dir_bsock;
302
303    while (bnet_recv(dir) >= 0) {
304        dir->msg[dir->msglen] = 0;
305        strip_trailing_junk(dir->msg);
306        Dmsg1(110, "filed<dird: include file %s\n", dir->msg);
307        add_fname_to_include_list(jcr->ff, 1, dir->msg);
308    }
309
310    return bnet_fsend(dir, OKinc);
311 }
312
313 /*
314  * Get list of files to exclude from Director
315  *
316  */
317 static int exclude_cmd(JCR *jcr)
318 {
319    BSOCK *dir = jcr->dir_bsock;
320    char *p;  
321
322    while (bnet_recv(dir) >= 0) {
323        dir->msg[dir->msglen] = 0;
324        strip_trailing_junk(dir->msg);
325        /* Skip leading options */
326        for (p=dir->msg; *p && *p != ' '; p++)
327           { }
328        /* Skip spaces */
329        for ( ; *p && *p == ' '; p++)
330           { }
331        add_fname_to_exclude_list(jcr->ff, p);
332        Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
333    }
334
335    return bnet_fsend(dir, OKexc);
336 }
337
338
339 static int bootstrap_cmd(JCR *jcr)
340 {
341    BSOCK *dir = jcr->dir_bsock;
342    POOLMEM *fname = get_pool_memory(PM_FNAME);
343    FILE *bs;
344
345    if (jcr->RestoreBootstrap) {
346       unlink(jcr->RestoreBootstrap);
347       free_pool_memory(jcr->RestoreBootstrap);
348    }
349    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
350       jcr->Job);
351    Dmsg1(400, "bootstrap=%s\n", fname);
352    jcr->RestoreBootstrap = fname;
353    bs = fopen(fname, "a+");           /* create file */
354    if (!bs) {
355       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
356          jcr->RestoreBootstrap, strerror(errno));
357       free_pool_memory(jcr->RestoreBootstrap);
358       jcr->RestoreBootstrap = NULL;
359       return 0;
360    }
361
362    while (bnet_recv(dir) >= 0) {
363        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
364        fputs(dir->msg, bs);
365    }
366    fclose(bs);
367
368    return bnet_fsend(dir, OKbootstrap);
369 }
370
371
372 /*
373  * Get backup level from Director
374  *
375  */
376 static int level_cmd(JCR *jcr)
377 {
378    BSOCK *dir = jcr->dir_bsock;
379    char *level;
380    struct tm tm;
381    time_t mtime;
382
383    level = (char *) get_memory(dir->msglen);
384    Dmsg1(110, "level_cmd: %s", dir->msg);
385    if (sscanf(dir->msg, "level = %s ", level) != 1) {
386       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
387       free_memory(level);
388       return 0;
389    }
390    /*
391     * Full backup requested
392     */
393    if (strcmp(level, "full") == 0) {
394       jcr->save_level = L_FULL;
395    /* 
396     * Backup requested since <date> <time>
397     *  This form is also used for incremental and differential
398     */
399    } else if (strcmp(level, "since") == 0) {
400       jcr->save_level = L_SINCE;
401       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
402                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
403                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
404          Jmsg1(jcr, M_FATAL, 0, "Bad scan of date/time: %s\n", dir->msg);
405          free_memory(level);
406          return 0;
407       }
408       tm.tm_year -= 1900;
409       tm.tm_mon  -= 1;
410       tm.tm_wday = tm.tm_yday = 0;              
411       tm.tm_isdst = -1;
412       mtime = mktime(&tm);
413       Dmsg1(100, "Got since time: %s", ctime(&mtime));
414       jcr->incremental = 1;
415       jcr->mtime = mtime;
416    } else {
417       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
418       free_memory(level);
419       return 0;
420    }
421    free_memory(level);
422    return bnet_fsend(dir, OKlevel);
423 }
424
425 /*
426  * Get session parameters from Director -- this is for a Restore command
427  */
428 static int session_cmd(JCR *jcr)
429 {
430    BSOCK *dir = jcr->dir_bsock;
431
432    Dmsg1(100, "SessionCmd: %s", dir->msg);
433    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
434               &jcr->VolSessionId, &jcr->VolSessionTime,
435               &jcr->StartFile, &jcr->EndFile, 
436               &jcr->StartBlock, &jcr->EndBlock) != 7) {
437       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
438       return 0;
439    }
440
441    return bnet_fsend(dir, OKsession);
442 }
443
444 /*
445  * Get address of storage daemon from Director
446  *
447  */
448 static int storage_cmd(JCR *jcr)
449 {
450    int stored_port;                /* storage daemon port */
451    BSOCK *dir = jcr->dir_bsock;
452    BSOCK *sd;                         /* storage daemon bsock */
453
454    Dmsg1(100, "StorageCmd: %s", dir->msg);
455    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port) != 2) {
456       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
457       return 0;
458    }
459    Dmsg2(110, "Open storage: %s:%d\n", jcr->stored_addr, stored_port);
460    /* Open command communications with Storage daemon */
461    /* Try to connect for 1 hour at 10 second intervals */
462    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
463                      jcr->stored_addr, NULL, stored_port, 1);
464    if (sd == NULL) {
465       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
466           jcr->stored_addr, stored_port);
467       return 0;
468    }
469
470    jcr->store_bsock = sd;
471
472    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
473    if (!authenticate_storagedaemon(jcr)) {
474       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
475       return 0;
476    }
477    Dmsg0(110, "Authenticated with SD.\n");
478
479    /* Send OK to Director */
480    return bnet_fsend(dir, OKstore);
481 }
482
483
484 /*  
485  * Do a backup. For now, we handle only Full and Incremental.
486  */
487 static int backup_cmd(JCR *jcr)
488
489    BSOCK *dir = jcr->dir_bsock;
490    BSOCK *sd = jcr->store_bsock;
491    int ok = 0;
492    int SDJobStatus;
493
494    jcr->JobStatus = JS_Blocked;
495    jcr->JobType = JT_BACKUP;
496    Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
497
498    if (sd == NULL) {
499       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
500       jcr->JobStatus = JS_ErrorTerminated;
501       goto cleanup;
502    }
503
504    bnet_fsend(dir, OKbackup);
505    Dmsg1(110, "bfiled>dird: %s", dir->msg);
506
507    /* 
508     * Send Append Open Session to Storage daemon
509     */
510    bnet_fsend(sd, append_open);
511    Dmsg1(110, ">stored: %s", sd->msg);
512    /* 
513     * Expect to receive back the Ticket number
514     */
515    if (bnet_recv(sd) >= 0) {
516       Dmsg1(110, "<stored: %s", sd->msg);
517       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
518          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
519          jcr->JobStatus = JS_ErrorTerminated;
520          goto cleanup;
521       }
522       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
523    } else {
524       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
525       jcr->JobStatus = JS_ErrorTerminated;
526       goto cleanup;
527    }
528
529    /* 
530     * Send Append data command to Storage daemon
531     */
532    bnet_fsend(sd, append_data, jcr->Ticket);
533    Dmsg1(110, ">stored: %s", sd->msg);
534
535    /* 
536     * Expect to get OK data 
537     */
538    Dmsg1(110, "<stored: %s", sd->msg);
539    if (!response(jcr, sd, OK_data, "Append Data")) {
540       jcr->JobStatus = JS_ErrorTerminated;
541       goto cleanup;
542    }
543       
544    /*
545     * Send Files to Storage daemon
546     */
547    Dmsg1(110, "begin blast ff=%p\n", jcr->ff);
548    if (!blast_data_to_storage_daemon(jcr, NULL)) {
549       jcr->JobStatus = JS_ErrorTerminated;
550    } else {
551       jcr->JobStatus = JS_Terminated;
552       /* 
553        * Expect to get response to append_data from Storage daemon
554        */
555       if (!response(jcr, sd, OK_append, "Append Data")) {
556          jcr->JobStatus = 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          jcr->JobStatus = 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          jcr->JobStatus = 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          jcr->JobStatus = 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 jcr->JobStatus == JS_Terminated;
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    return bnet_sig(dir, BNET_TERMINATE);
668 }
669
670 /*  
671  * Do a Restore for Director
672  *
673  */
674 static int restore_cmd(JCR *jcr)
675
676    BSOCK *dir = jcr->dir_bsock;
677    BSOCK *sd = jcr->store_bsock;
678    POOLMEM *where;
679    char replace;
680
681    /*
682     * Scan WHERE (base directory for restore) from command
683     */
684    Dmsg0(150, "restore command\n");
685    /* Pickup where string */
686    where = get_memory(dir->msglen+1);
687    *where = 0;
688
689    if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
690       Jmsg(jcr, M_FATAL, 0, _("Bad replace command.\n"));
691       return 0;
692    }
693       
694    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
695    unbash_spaces(where);
696    jcr->where = where;
697    jcr->replace = replace;
698
699    bnet_fsend(dir, OKrestore);
700    Dmsg1(110, "bfiled>dird: %s", dir->msg);
701
702    jcr->JobType = JT_RESTORE;
703    jcr->JobStatus = JS_Blocked;
704
705    if (!open_sd_read_session(jcr)) {
706       return 0;
707    }
708
709    /* 
710     * Do restore of files and data
711     */
712    do_restore(jcr);
713
714    /* 
715     * Send Close session command to Storage daemon
716     */
717    bnet_fsend(sd, read_close, jcr->Ticket);
718    Dmsg1(130, "bfiled>stored: %s", sd->msg);
719
720    /* ****FIXME**** check response */
721    bnet_recv(sd);                     /* get OK */
722
723    /* Inform Storage daemon that we are done */
724    bnet_sig(sd, BNET_TERMINATE);
725
726    bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined, jcr->JobBytes);
727
728    /* Inform Director that we are done */
729    bnet_sig(dir, BNET_TERMINATE);
730
731    Dmsg0(130, "Done in job.c\n");
732    return 1;
733 }
734
735 static int open_sd_read_session(JCR *jcr)
736 {
737    BSOCK *sd = jcr->store_bsock;
738
739    if (!sd) {
740       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
741       return 0;
742    }
743    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
744       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
745    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
746    /* 
747     * Open Read Session with Storage daemon
748     */
749    bnet_fsend(sd, read_open, jcr->VolumeName,
750       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
751       jcr->StartBlock, jcr->EndBlock);
752    Dmsg1(110, ">stored: %s", sd->msg);
753
754    /* 
755     * Get ticket number
756     */
757    if (bnet_recv(sd) >= 0) {
758       Dmsg1(110, "bfiled<stored: %s", sd->msg);
759       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
760          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
761          return 0;
762       }
763       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
764    } else {
765       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
766       return 0;
767    }
768
769    if (!send_bootstrap_file(jcr)) {
770       return 0;
771    }
772
773    /* 
774     * Start read of data with Storage daemon
775     */
776    bnet_fsend(sd, read_data, jcr->Ticket);
777    Dmsg1(110, ">stored: %s", sd->msg);
778
779    /* 
780     * Get OK data
781     */
782    if (!response(jcr, sd, OK_data, "Read Data")) {
783       return 0;
784    }
785    return 1;
786 }
787
788 /* 
789  * Destroy the Job Control Record and associated
790  * resources (sockets).
791  */
792 static void filed_free_jcr(JCR *jcr) 
793 {
794    if (jcr->store_bsock) {
795       bnet_close(jcr->store_bsock);
796    }
797    if (jcr->where) {
798       free_pool_memory(jcr->where);
799    }
800    if (jcr->RestoreBootstrap) {
801       unlink(jcr->RestoreBootstrap);
802       free_pool_memory(jcr->RestoreBootstrap);
803    }
804    if (jcr->last_fname) {
805       free_pool_memory(jcr->last_fname);
806    }
807    return;
808 }
809
810 /*
811  * Get response from Storage daemon to a command we
812  * sent. Check that the response is OK.
813  *
814  *  Returns: 0 on failure
815  *           1 on success
816  */
817 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
818 {
819    int n;
820
821    if (sd->errors) {
822       return 0;
823    }
824    if ((n = bnet_recv(sd)) > 0) {
825       Dmsg0(110, sd->msg);
826       if (strcmp(sd->msg, resp) == 0) {
827          return 1;
828       }
829    } 
830    if (is_bnet_error(sd)) {
831       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
832          cmd, bnet_strerror(sd));
833    } else {
834       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
835          cmd, resp, sd->msg);
836    }
837    return 0;
838 }
839
840 static int send_bootstrap_file(JCR *jcr)
841 {
842    FILE *bs;
843    char buf[2000];
844    BSOCK *sd = jcr->store_bsock;
845    char *bootstrap = "bootstrap\n";
846
847    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
848    if (!jcr->RestoreBootstrap) {
849       return 1;
850    }
851    bs = fopen(jcr->RestoreBootstrap, "r");
852    if (!bs) {
853       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
854          jcr->RestoreBootstrap, strerror(errno));
855       jcr->JobStatus = JS_ErrorTerminated;
856       return 0;
857    }
858    strcpy(sd->msg, bootstrap);  
859    sd->msglen = strlen(sd->msg);
860    bnet_send(sd);
861    while (fgets(buf, sizeof(buf), bs)) {
862       sd->msglen = Mmsg(&sd->msg, "%s", buf);
863       bnet_send(sd);       
864    }
865    bnet_sig(sd, BNET_EOD);
866    fclose(bs);
867    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
868       jcr->JobStatus = JS_ErrorTerminated;
869       return 0;
870    }
871    return 1;
872 }