]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
274bb7426df2196d247c38149de4d2ca1d251bef
[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    enable_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    int mtime_only;
413
414    level = get_memory(dir->msglen+1);
415    Dmsg1(110, "level_cmd: %s", dir->msg);
416    if (sscanf(dir->msg, "level = %s ", level) != 1) {
417       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
418       free_memory(level);
419       return 0;
420    }
421    /* Base backup requested? */
422    if (strcmp(level, "base") == 0) {
423       jcr->save_level = L_BASE;
424    /* Full backup requested? */ 
425    } else if (strcmp(level, "full") == 0) {
426       jcr->save_level = L_FULL;
427    /* 
428     * Backup requested since <date> <time>
429     *  This form is also used for incremental and differential
430     */
431    } else if (strcmp(level, "since") == 0) {
432       jcr->save_level = L_SINCE;
433       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d mtime_only=%d", 
434                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
435                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &mtime_only) != 7) {
436          Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
437          free_memory(level);
438          return 0;
439       }
440       tm.tm_year -= 1900;
441       tm.tm_mon  -= 1;
442       tm.tm_wday = tm.tm_yday = 0;              
443       tm.tm_isdst = -1;
444       mtime = mktime(&tm);
445       Dmsg2(100, "Got since time: %s mtime_only=%d\n", ctime(&mtime), mtime_only);
446       jcr->incremental = 1;           /* set incremental or decremental backup */
447       jcr->mtime = mtime;             /* set since time */
448       jcr->mtime_only = mtime_only;   /* and what to compare */
449    } else {
450       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
451       free_memory(level);
452       return 0;
453    }
454    free_memory(level);
455    return bnet_fsend(dir, OKlevel);
456 }
457
458 /*
459  * Get session parameters from Director -- this is for a Restore command
460  */
461 static int session_cmd(JCR *jcr)
462 {
463    BSOCK *dir = jcr->dir_bsock;
464
465    Dmsg1(100, "SessionCmd: %s", dir->msg);
466    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
467               &jcr->VolSessionId, &jcr->VolSessionTime,
468               &jcr->StartFile, &jcr->EndFile, 
469               &jcr->StartBlock, &jcr->EndBlock) != 7) {
470       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
471       return 0;
472    }
473
474    return bnet_fsend(dir, OKsession);
475 }
476
477 /*
478  * Get address of storage daemon from Director
479  *
480  */
481 static int storage_cmd(JCR *jcr)
482 {
483    int stored_port;                /* storage daemon port */
484    int enable_ssl;                 /* enable ssl to sd */
485    BSOCK *dir = jcr->dir_bsock;
486    BSOCK *sd;                         /* storage daemon bsock */
487
488    Dmsg1(100, "StorageCmd: %s", dir->msg);
489    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
490       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
491       return 0;
492    }
493    Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
494    /* Open command communications with Storage daemon */
495    /* Try to connect for 1 hour at 10 second intervals */
496    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
497                      jcr->stored_addr, NULL, stored_port, 1);
498    if (sd == NULL) {
499       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
500           jcr->stored_addr, stored_port);
501       return 0;
502    }
503
504    jcr->store_bsock = sd;
505
506    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
507    if (!authenticate_storagedaemon(jcr)) {
508       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
509       return 0;
510    }
511    Dmsg0(110, "Authenticated with SD.\n");
512
513    /* Send OK to Director */
514    return bnet_fsend(dir, OKstore);
515 }
516
517
518 /*  
519  * Do a backup. For now, we handle only Full and Incremental.
520  */
521 static int backup_cmd(JCR *jcr)
522
523    BSOCK *dir = jcr->dir_bsock;
524    BSOCK *sd = jcr->store_bsock;
525    int ok = 0;
526    int SDJobStatus;
527    char ed1[50], ed2[50];
528
529    set_jcr_job_status(jcr, JS_Blocked);
530    jcr->JobType = JT_BACKUP;
531    Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
532
533    if (sd == NULL) {
534       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
535       goto cleanup;
536    }
537
538    bnet_fsend(dir, OKbackup);
539    Dmsg1(110, "bfiled>dird: %s", dir->msg);
540
541    /* 
542     * Send Append Open Session to Storage daemon
543     */
544    bnet_fsend(sd, append_open);
545    Dmsg1(110, ">stored: %s", sd->msg);
546    /* 
547     * Expect to receive back the Ticket number
548     */
549    if (bget_msg(sd) >= 0) {
550       Dmsg1(110, "<stored: %s", sd->msg);
551       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
552          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
553          goto cleanup;
554       }
555       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
556    } else {
557       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
558       goto cleanup;
559    }
560
561    /* 
562     * Send Append data command to Storage daemon
563     */
564    bnet_fsend(sd, append_data, jcr->Ticket);
565    Dmsg1(110, ">stored: %s", sd->msg);
566
567    /* 
568     * Expect to get OK data 
569     */
570    Dmsg1(110, "<stored: %s", sd->msg);
571    if (!response(jcr, sd, OK_data, "Append Data")) {
572       goto cleanup;
573    }
574       
575    /*
576     * Send Files to Storage daemon
577     */
578    Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
579    if (!blast_data_to_storage_daemon(jcr, NULL)) {
580       set_jcr_job_status(jcr, JS_ErrorTerminated);
581       bnet_suppress_error_messages(sd, 1);
582    } else {
583       set_jcr_job_status(jcr, JS_Terminated);
584       if (jcr->JobStatus != JS_Terminated) {
585          bnet_suppress_error_messages(sd, 1);
586          goto cleanup;                /* bail out now */
587       }
588       /* 
589        * Expect to get response to append_data from Storage daemon
590        */
591       if (!response(jcr, sd, OK_append, "Append Data")) {
592          set_jcr_job_status(jcr, JS_ErrorTerminated);
593          goto cleanup;
594       }
595      
596       /* 
597        * Send Append End Data to Storage daemon
598        */
599       bnet_fsend(sd, append_end, jcr->Ticket);
600       /* Get end OK */
601       if (!response(jcr, sd, OK_end, "Append End")) {
602          set_jcr_job_status(jcr, JS_ErrorTerminated);
603          goto cleanup;
604       }
605
606       /*
607        * Send Append Close to Storage daemon
608        */
609       bnet_fsend(sd, append_close, jcr->Ticket);
610       while (bget_msg(sd) >= 0) {    /* stop on signal or error */
611          if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
612             ok = 1;
613             Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
614          }
615       }
616       if (!ok) {
617          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
618          goto cleanup;
619       }
620       if (SDJobStatus != JS_Terminated) {
621          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
622             SDJobStatus);
623       }
624    }
625
626 cleanup:
627
628    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
629       edit_uint64(jcr->ReadBytes, ed1), 
630       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
631
632    return 0;                          /* return and stop command loop */
633 }
634
635 /*  
636  * Do a Verify for Director
637  *
638  */
639 static int verify_cmd(JCR *jcr)
640
641    BSOCK *dir = jcr->dir_bsock;
642    BSOCK *sd  = jcr->store_bsock;
643    char level[100], ed1[50], ed2[50];
644
645    jcr->JobType = JT_VERIFY;
646    if (sscanf(dir->msg, verifycmd, level) != 1) {
647       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
648       return 0;   
649    }
650    if (strcasecmp(level, "init") == 0) {
651       jcr->JobLevel = L_VERIFY_INIT;
652    } else if (strcasecmp(level, "catalog") == 0){
653       jcr->JobLevel = L_VERIFY_CATALOG;
654    } else if (strcasecmp(level, "volume") == 0){
655       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
656    } else if (strcasecmp(level, "data") == 0){
657       jcr->JobLevel = L_VERIFY_DATA;
658    } else {   
659       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
660       return 0;   
661    }
662
663    bnet_fsend(dir, OKverify);
664    Dmsg1(110, "bfiled>dird: %s", dir->msg);
665
666    switch (jcr->JobLevel) {
667    case L_VERIFY_INIT:
668    case L_VERIFY_CATALOG:
669       do_verify(jcr);
670       break;
671    case L_VERIFY_VOLUME_TO_CATALOG:
672       if (!open_sd_read_session(jcr)) {
673          return 0;
674       }
675       start_dir_heartbeat(jcr);
676       do_verify_volume(jcr);
677       stop_dir_heartbeat(jcr);
678       /* 
679        * Send Close session command to Storage daemon
680        */
681       bnet_fsend(sd, read_close, jcr->Ticket);
682       Dmsg1(130, "bfiled>stored: %s", sd->msg);
683
684       /* ****FIXME**** check response */
685       bget_msg(sd);                      /* get OK */
686
687       /* Inform Storage daemon that we are done */
688       bnet_sig(sd, BNET_TERMINATE);
689
690       break;
691    default:
692       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
693       return 0; 
694    }
695
696    bnet_sig(dir, BNET_EOD);
697
698    /* Send termination status back to Dir */
699    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
700       edit_uint64(jcr->ReadBytes, ed1), 
701       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
702
703    /* Inform Director that we are done */
704    bnet_sig(dir, BNET_TERMINATE);
705    return 0;                          /* return and terminate command loop */
706 }
707
708 /*  
709  * Do a Restore for Director
710  *
711  */
712 static int restore_cmd(JCR *jcr)
713
714    BSOCK *dir = jcr->dir_bsock;
715    BSOCK *sd = jcr->store_bsock;
716    POOLMEM *where;
717    char replace;
718    char ed1[50], ed2[50];
719
720    /*
721     * Scan WHERE (base directory for restore) from command
722     */
723    Dmsg0(150, "restore command\n");
724    /* Pickup where string */
725    where = get_memory(dir->msglen+1);
726    *where = 0;
727
728    if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
729       if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
730          Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
731          return 0;
732       }
733       *where = 0;
734    }
735    /* Turn / into nothing */
736    if (where[0] == '/' && where[1] == 0) {
737       where[0] = 0;
738    }
739       
740    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
741    unbash_spaces(where);
742    jcr->where = where;
743    jcr->replace = replace;
744
745    bnet_fsend(dir, OKrestore);
746    Dmsg1(110, "bfiled>dird: %s", dir->msg);
747
748    jcr->JobType = JT_RESTORE;
749
750    set_jcr_job_status(jcr, JS_Blocked);
751    if (!open_sd_read_session(jcr)) {
752       set_jcr_job_status(jcr, JS_ErrorTerminated);
753       goto bail_out;
754    }
755
756    set_jcr_job_status(jcr, JS_Running);
757
758    /* 
759     * Do restore of files and data
760     */
761    start_dir_heartbeat(jcr);
762    do_restore(jcr);
763    stop_dir_heartbeat(jcr);
764    
765    set_jcr_job_status(jcr, JS_Terminated);
766    if (jcr->JobStatus != JS_Terminated) {
767       bnet_suppress_error_messages(sd, 1);
768    }
769
770    /* 
771     * Send Close session command to Storage daemon
772     */
773    bnet_fsend(sd, read_close, jcr->Ticket);
774    Dmsg1(130, "bfiled>stored: %s", sd->msg);
775
776    bget_msg(sd);                      /* get OK */
777
778    /* Inform Storage daemon that we are done */
779    bnet_sig(sd, BNET_TERMINATE);
780
781 bail_out:
782
783    /* Send termination status back to Dir */
784    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
785       edit_uint64(jcr->ReadBytes, ed1), 
786       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
787
788    /* Inform Director that we are done */
789    bnet_sig(dir, BNET_TERMINATE);
790
791    Dmsg0(130, "Done in job.c\n");
792    return 0;                          /* return and terminate command loop */
793 }
794
795 static int open_sd_read_session(JCR *jcr)
796 {
797    BSOCK *sd = jcr->store_bsock;
798
799    if (!sd) {
800       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
801       return 0;
802    }
803    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
804       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
805    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
806    /* 
807     * Open Read Session with Storage daemon
808     */
809    bnet_fsend(sd, read_open, jcr->VolumeName,
810       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
811       jcr->StartBlock, jcr->EndBlock);
812    Dmsg1(110, ">stored: %s", sd->msg);
813
814    /* 
815     * Get ticket number
816     */
817    if (bget_msg(sd) >= 0) {
818       Dmsg1(110, "bfiled<stored: %s", sd->msg);
819       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
820          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
821          return 0;
822       }
823       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
824    } else {
825       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
826       return 0;
827    }
828
829    if (!send_bootstrap_file(jcr)) {
830       return 0;
831    }
832
833    /* 
834     * Start read of data with Storage daemon
835     */
836    bnet_fsend(sd, read_data, jcr->Ticket);
837    Dmsg1(110, ">stored: %s", sd->msg);
838
839    /* 
840     * Get OK data
841     */
842    if (!response(jcr, sd, OK_data, "Read Data")) {
843       return 0;
844    }
845    return 1;
846 }
847
848 /* 
849  * Destroy the Job Control Record and associated
850  * resources (sockets).
851  */
852 static void filed_free_jcr(JCR *jcr) 
853 {
854    if (jcr->store_bsock) {
855       bnet_close(jcr->store_bsock);
856    }
857    if (jcr->where) {
858       free_pool_memory(jcr->where);
859    }
860    if (jcr->RestoreBootstrap) {
861       unlink(jcr->RestoreBootstrap);
862       free_pool_memory(jcr->RestoreBootstrap);
863    }
864    if (jcr->last_fname) {
865       free_pool_memory(jcr->last_fname);
866    }
867    return;
868 }
869
870 /*
871  * Get response from Storage daemon to a command we
872  * sent. Check that the response is OK.
873  *
874  *  Returns: 0 on failure
875  *           1 on success
876  */
877 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
878 {
879    int n;
880
881    if (sd->errors) {
882       return 0;
883    }
884    if ((n = bget_msg(sd)) > 0) {
885       Dmsg0(110, sd->msg);
886       if (strcmp(sd->msg, resp) == 0) {
887          return 1;
888       }
889    } 
890    if (is_bnet_error(sd)) {
891       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
892          cmd, bnet_strerror(sd));
893    } else {
894       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
895          cmd, resp, sd->msg);
896    }
897    return 0;
898 }
899
900 static int send_bootstrap_file(JCR *jcr)
901 {
902    FILE *bs;
903    char buf[2000];
904    BSOCK *sd = jcr->store_bsock;
905    char *bootstrap = "bootstrap\n";
906
907    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
908    if (!jcr->RestoreBootstrap) {
909       return 1;
910    }
911    bs = fopen(jcr->RestoreBootstrap, "r");
912    if (!bs) {
913       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
914          jcr->RestoreBootstrap, strerror(errno));
915       set_jcr_job_status(jcr, JS_ErrorTerminated);
916       return 0;
917    }
918    strcpy(sd->msg, bootstrap);  
919    sd->msglen = strlen(sd->msg);
920    bnet_send(sd);
921    while (fgets(buf, sizeof(buf), bs)) {
922       sd->msglen = Mmsg(&sd->msg, "%s", buf);
923       bnet_send(sd);       
924    }
925    bnet_sig(sd, BNET_EOD);
926    fclose(bs);
927    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
928       set_jcr_job_status(jcr, JS_ErrorTerminated);
929       return 0;
930    }
931    return 1;
932 }