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