]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/verify.c
ae0db6cb5294dbbbbed71535f20dc2ed84f6e31f
[bacula/bacula] / bacula / src / dird / verify.c
1 /*
2  *
3  *   Bacula Director -- verify.c -- responsible for running file verification
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    This routine is run as a separate thread.  There may be more
8  *    work to be done to make it totally reentrant!!!!
9  * 
10  * Current implementation is Catalog verification only (i.e. no
11  *  verification versus tape).
12  *
13  *  Basic tasks done here:
14  *     Open DB
15  *     Open connection with File daemon and pass him commands
16  *       to do the verify.
17  *     When the File daemon sends the attributes, compare them to
18  *       what is in the DB.
19  *
20  *   Version $Id$
21  */
22
23 /*
24    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
25
26    This program is free software; you can redistribute it and/or
27    modify it under the terms of the GNU General Public License as
28    published by the Free Software Foundation; either version 2 of
29    the License, or (at your option) any later version.
30
31    This program is distributed in the hope that it will be useful,
32    but WITHOUT ANY WARRANTY; without even the implied warranty of
33    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34    General Public License for more details.
35
36    You should have received a copy of the GNU General Public
37    License along with this program; if not, write to the Free
38    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
39    MA 02111-1307, USA.
40
41  */
42
43 #include "bacula.h"
44 #include "dird.h"
45 #include "findlib/find.h"
46
47 /* Imported Global Variables */
48 extern int debug_level;
49
50 /* Commands sent to File daemon */
51 static char verifycmd[]    = "verify level=%s\n";
52 static char storaddr[]     = "storage address=%s port=%d\n";
53 static char sessioncmd[]   = "session %s %ld %ld %ld %ld %ld %ld\n";  
54
55 /* Responses received from File daemon */
56 static char OKverify[]    = "2000 OK verify\n";
57 static char OKstore[]     = "2000 OK storage\n";
58 static char OKsession[]   = "2000 OK session\n";
59
60 /* Forward referenced functions */
61 static void verify_cleanup(JCR *jcr, int TermCode);
62 static void prt_fname(JCR *jcr);
63 static int missing_handler(void *ctx, int num_fields, char **row);
64
65 /* 
66  * Do a verification of the specified files against the Catlaog
67  *    
68  *  Returns:  0 on failure
69  *            1 on success
70  */
71 int do_verify(JCR *jcr) 
72 {
73    char *level;
74    BSOCK   *fd;
75    JOB_DBR jr;
76    JobId_t JobId = 0;
77
78    if (!get_or_create_client_record(jcr)) {
79       goto bail_out;
80    }
81
82    Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
83
84    /* If we are doing a verify from the catalog,
85     * we must look up the time and date of the
86     * last full verify.
87     */
88    if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
89       memcpy(&jr, &(jcr->jr), sizeof(jr));
90       if (!db_find_last_jobid(jcr->db, &jr)) {
91          Jmsg(jcr, M_FATAL, 0, _(
92               "Unable to find JobId of previous InitCatalog Job.\n"
93               "Please run a Verify with Level=InitCatalog before\n"
94               "running the current Job.\n"));
95          goto bail_out;
96       }
97       JobId = jr.JobId;
98       Dmsg1(20, "Last full id=%d\n", JobId);
99    } 
100
101    jcr->jr.JobId = jcr->JobId;
102    jcr->jr.StartTime = jcr->start_time;
103    jcr->jr.Level = jcr->JobLevel;
104    if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
105       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
106       goto bail_out;
107    }
108
109    if (!jcr->fname) {
110       jcr->fname = (char *) get_pool_memory(PM_FNAME);
111    }
112
113    jcr->jr.JobId = JobId;      /* save target JobId */
114
115    /* Print Job Start message */
116    Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
117       jcr->JobId, jcr->Job);
118
119    if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
120       memset(&jr, 0, sizeof(jr));
121       jr.JobId = JobId;
122       if (!db_get_job_record(jcr->db, &jr)) {
123          Jmsg(jcr, M_FATAL, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
124          goto bail_out;
125       }
126       if (jr.JobStatus != 'T') {
127          Jmsg(jcr, M_FATAL, 0, _("Last Job %d did not terminate normally. JobStatus=%c\n"),
128             JobId, jr.JobStatus);
129          goto bail_out;
130       }
131       Jmsg(jcr, M_INFO, 0, _("Verifying against JobId=%d Job=%s\n"),
132          JobId, jr.Job); 
133    }
134
135    /* 
136     * If we are verifing a Volume, we need the Storage
137     *   daemon, so open a connection, otherwise, just
138     *   create a dummy authorization key (passed to
139     *   File daemon but not used).
140     */
141    if (jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
142       /*
143        * Now find the Volumes we will need for the Verify 
144        */
145       jcr->VolumeName[0] = 0;
146       if (!db_get_job_volume_names(jcr->db, jr.JobId, &jcr->VolumeName) ||
147            jcr->VolumeName[0] == 0) {
148          Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for verify JobId=%d. %s"), 
149             jr.JobId, db_strerror(jcr->db));
150          goto bail_out;
151       }
152       Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
153       /*
154        * Start conversation with Storage daemon  
155        */
156       jcr->JobStatus = JS_Blocked;
157       if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
158          goto bail_out;
159       }
160       /*
161        * Now start a job with the Storage daemon
162        */
163       if (!start_storage_daemon_job(jcr)) {
164          goto bail_out;
165       }
166       /*
167        * Now start a Storage daemon message thread
168        */
169       if (!start_storage_daemon_message_thread(jcr)) {
170          goto bail_out;
171       }
172       Dmsg0(50, "Storage daemon connection OK\n");
173    } else {
174       jcr->sd_auth_key = bstrdup("dummy");    /* dummy Storage daemon key */
175    }
176    /*
177     * OK, now connect to the File daemon
178     *  and ask him for the files.
179     */
180    jcr->JobStatus = JS_Blocked;
181    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
182       goto bail_out;
183    }
184
185    jcr->JobStatus = JS_Running;
186    fd = jcr->file_bsock;
187
188    Dmsg0(30, ">filed: Send include list\n");
189    if (!send_include_list(jcr)) {
190       goto bail_out;
191    }
192
193    Dmsg0(30, ">filed: Send exclude list\n");
194    if (!send_exclude_list(jcr)) {
195       goto bail_out;
196    }
197
198    /* 
199     * Send Level command to File daemon, as well
200     *   as the Storage address if appropriate.
201     */
202    switch (jcr->JobLevel) {
203       case L_VERIFY_INIT:
204          level = "init";
205          break;
206       case L_VERIFY_CATALOG:
207          level = "catalog";
208          break;
209       case L_VERIFY_VOLUME_TO_CATALOG:
210          /* 
211           * send Storage daemon address to the File daemon
212           */
213          if (jcr->store->SDDport == 0) {
214             jcr->store->SDDport = jcr->store->SDport;
215          }
216          bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
217          if (!response(fd, OKstore, "Storage")) {
218             goto bail_out;
219          }
220          /*
221           * Pass the VolSessionId, VolSessionTime, Start and
222           * end File and Blocks on the session command.
223           */
224          bnet_fsend(fd, sessioncmd, 
225                    jcr->VolumeName,
226                    jr.VolSessionId, jr.VolSessionTime, 
227                    jr.StartFile, jr.EndFile, jr.StartBlock, 
228                    jr.EndBlock);
229          if (!response(fd, OKsession, "Session")) {
230             goto bail_out;
231          }
232          level = "volume";
233          break;
234       case L_VERIFY_DATA:
235          level = "data";
236          break;
237       default:
238          Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->JobLevel);
239          goto bail_out;
240    }
241
242    /* 
243     * Send verify command/level to File daemon
244     */
245    bnet_fsend(fd, verifycmd, level);
246    if (!response(fd, OKverify, "Verify")) {
247       goto bail_out;
248    }
249
250    /*
251     * Now get data back from File daemon and
252     *  compare it to the catalog or store it in the
253     *  catalog depending on the run type.
254     */
255    /* Compare to catalog */
256    switch (jcr->JobLevel) { 
257    case L_VERIFY_CATALOG:
258       Dmsg0(10, "Verify level=catalog\n");
259       get_attributes_and_compare_to_catalog(jcr, JobId);
260       break;
261
262    case L_VERIFY_VOLUME_TO_CATALOG:
263       int stat;
264       Dmsg0(10, "Verify level=volume\n");
265       get_attributes_and_compare_to_catalog(jcr, JobId);
266       stat = jcr->JobStatus;
267       jcr->JobStatus = JS_WaitSD;
268       wait_for_storage_daemon_termination(jcr);
269       /* If we terminate normally, use SD term code, else, use ours */
270       if (stat == JS_Terminated) {
271          jcr->JobStatus = jcr->SDJobStatus;
272       } else {
273          jcr->JobStatus = stat;
274       }
275       break;
276
277    case L_VERIFY_INIT:
278       /* Build catalog */
279       Dmsg0(10, "Verify level=init\n");
280       get_attributes_and_put_in_catalog(jcr);
281       break;
282
283    default:
284       Jmsg1(jcr, M_FATAL, 0, _("Unimplemented verify level %d\n"), jcr->JobLevel);
285       goto bail_out;
286    }
287
288    verify_cleanup(jcr, jcr->JobStatus);
289    return 1;
290
291 bail_out:
292    verify_cleanup(jcr, JS_ErrorTerminated);
293    return 0;
294 }
295
296 /*
297  * Release resources allocated during backup.
298  *
299  */
300 static void verify_cleanup(JCR *jcr, int TermCode)
301 {
302    char sdt[50], edt[50];
303    char ec1[30];
304    char term_code[100];
305    char *term_msg;
306    int msg_type;
307    JobId_t JobId;
308
309    Dmsg0(100, "Enter verify_cleanup()\n");
310
311    JobId = jcr->jr.JobId;
312    jcr->JobStatus = TermCode;
313
314    update_job_end_record(jcr);
315
316    msg_type = M_INFO;                 /* by default INFO message */
317    switch (TermCode) {
318       case JS_Terminated:
319          term_msg = _("Verify OK");
320          break;
321       case JS_ErrorTerminated:
322          term_msg = _("*** Verify Error ***"); 
323          msg_type = M_ERROR;          /* Generate error message */
324          break;
325       case JS_Cancelled:
326          term_msg = _("Verify Cancelled");
327          break;
328       case JS_Differences:
329          term_msg = _("Verify Differences");
330          break;
331       default:
332          term_msg = term_code;
333          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
334          break;
335    }
336    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
337    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
338
339    Jmsg(jcr, msg_type, 0, _("%s\n\
340 JobId:                  %d\n\
341 Job:                    %s\n\
342 FileSet:                %s\n\
343 Verify Level:           %s\n\
344 Client:                 %s\n\
345 Start time:             %s\n\
346 End time:               %s\n\
347 Files Examined:         %s\n\
348 Termination:            %s\n\n"),
349         edt,
350         jcr->jr.JobId,
351         jcr->jr.Job,
352         jcr->fileset->hdr.name,
353         level_to_str(jcr->JobLevel),
354         jcr->client->hdr.name,
355         sdt,
356         edt,
357         edit_uint64_with_commas(jcr->JobFiles, ec1),
358         term_msg);
359
360    Dmsg0(100, "Leave verify_cleanup()\n");
361    if (jcr->fname) {
362       free_memory(jcr->fname);
363       jcr->fname = NULL;
364    }
365 }
366
367 /*
368  * This routine is called only during a Verify
369  */
370 int get_attributes_and_compare_to_catalog(JCR *jcr, JobId_t JobId)
371 {
372    BSOCK   *fd;
373    int n, len;
374    FILE_DBR fdbr;
375    struct stat statf;                 /* file stat */
376    struct stat statc;                 /* catalog stat */
377    int stat = JS_Terminated;
378    char buf[MAXSTRING];
379    POOLMEM *fname = get_pool_memory(PM_MESSAGE);
380    int do_MD5 = FALSE;
381    long file_index = 0;
382
383    memset(&fdbr, 0, sizeof(FILE_DBR));
384    fd = jcr->file_bsock;
385    fdbr.JobId = JobId;
386    jcr->FileIndex = 0;
387    
388    Dmsg0(20, "bdird: waiting to receive file attributes\n");
389    /*
390     * Get Attributes and MD5 Signature from File daemon
391     * We expect:
392     *   FileIndex
393     *   Stream
394     *   Options or MD5
395     *   Filename
396     *   Attributes
397     *   Link name  ???
398     */
399    while ((n=bget_msg(fd, 0)) >= 0 && !job_cancelled(jcr)) {
400       int stream;
401       char *attr, *p, *fn;
402       char Opts_MD5[MAXSTRING];        /* Verify Opts or MD5 signature */
403
404       fname = check_pool_memory_size(fname, fd->msglen);
405       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
406       Dmsg1(400, "Atts+MD5=%s\n", fd->msg);
407       if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream, 
408             Opts_MD5)) != 3) {
409          Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 3 fields got %d\n\
410  mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
411          goto bail_out;
412       }
413       p = fd->msg;
414       skip_nonspaces(&p);             /* skip FileIndex */
415       skip_spaces(&p);
416       skip_nonspaces(&p);             /* skip Stream */
417       skip_spaces(&p);
418       skip_nonspaces(&p);             /* skip Opts_MD5 */   
419       p++;                            /* skip space */
420       fn = fname;
421       while (*p != 0) {
422          *fn++ = *p++;                /* copy filename */
423       }
424       *fn = *p++;                     /* term filename and point to attribs */
425       attr = p;
426       /*
427        * Got attributes stream, decode it
428        */
429       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
430          Dmsg2(400, "file_index=%d attr=%s\n", file_index, attr);
431          jcr->JobFiles++;
432          jcr->FileIndex = file_index;    /* remember attribute file_index */
433          decode_stat(attr, &statf);  /* decode file stat packet */
434          do_MD5 = FALSE;
435          jcr->fn_printed = FALSE;
436          strcpy(jcr->fname, fname);  /* move filename into JCR */
437
438          Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
439          Dmsg1(020, "dird<filed: attr=%s\n", attr);
440
441          /* 
442           * Find equivalent record in the database 
443           */
444          fdbr.FileId = 0;
445          if (!db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr)) {
446             Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
447             Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
448             stat = JS_Differences;
449             continue;
450          } else {
451             /* 
452              * mark file record as visited by stuffing the
453              * current JobId, which is unique, into the MarkId field.
454              */
455             db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
456          }
457
458          Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname, 
459             file_index, Opts_MD5);
460          decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
461          /*
462           * Loop over options supplied by user and verify the
463           * fields he requests.
464           */
465          for (p=Opts_MD5; *p; p++) {
466             switch (*p) {
467             case 'i':                /* compare INODEs */
468                if (statc.st_ino != statf.st_ino) {
469                   prt_fname(jcr);
470                   Jmsg(jcr, M_INFO, 0, _("      st_ino   differ. Cat: %x File: %x\n"), 
471                      statc.st_ino, statf.st_ino);
472                   stat = JS_Differences;
473                }
474                break;
475             case 'p':                /* permissions bits */
476                if (statc.st_mode != statf.st_mode) {
477                   prt_fname(jcr);
478                   Jmsg(jcr, M_INFO, 0, _("      st_mode  differ. Cat: %x File: %x\n"), 
479                      statc.st_mode, statf.st_mode);
480                   stat = JS_Differences;
481                }
482                break;
483             case 'n':                /* number of links */
484                if (statc.st_nlink != statf.st_nlink) {
485                   prt_fname(jcr);
486                   Jmsg(jcr, M_INFO, 0, _("      st_nlink differ. Cat: %d File: %d\n"), 
487                      statc.st_nlink, statf.st_nlink);
488                   stat = JS_Differences;
489                }
490                break;
491             case 'u':                /* user id */
492                if (statc.st_uid != statf.st_uid) {
493                   prt_fname(jcr);
494                   Jmsg(jcr, M_INFO, 0, _("      st_uid   differ. Cat: %d File: %d\n"), 
495                      statc.st_uid, statf.st_uid);
496                   stat = JS_Differences;
497                }
498                break;
499             case 'g':                /* group id */
500                if (statc.st_gid != statf.st_gid) {
501                   prt_fname(jcr);
502                   Jmsg(jcr, M_INFO, 0, _("      st_gid   differ. Cat: %d File: %d\n"), 
503                      statc.st_gid, statf.st_gid);
504                   stat = JS_Differences;
505                }
506                break;
507             case 's':                /* size */
508                if (statc.st_size != statf.st_size) {
509                   prt_fname(jcr);
510                   Jmsg(jcr, M_INFO, 0, _("      st_size  differ. Cat: %d File: %d\n"), 
511                      statc.st_size, statf.st_size);
512                   stat = JS_Differences;
513                }
514                break;
515             case 'a':                /* access time */
516                if (statc.st_atime != statf.st_atime) {
517                   prt_fname(jcr);
518                   Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
519                   stat = JS_Differences;
520                }
521                break;
522             case 'm':
523                if (statc.st_mtime != statf.st_mtime) {
524                   prt_fname(jcr);
525                   Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
526                   stat = JS_Differences;
527                }
528                break;
529             case 'c':                /* ctime */
530                if (statc.st_ctime != statf.st_ctime) {
531                   prt_fname(jcr);
532                   Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
533                   stat = JS_Differences;
534                }
535                break;
536             case 'd':                /* file size decrease */
537                if (statc.st_size > statf.st_size) {
538                   prt_fname(jcr);
539                   Jmsg(jcr, M_INFO, 0, _("      st_size  decrease. Cat: %d File: %d\n"), 
540                      statc.st_size, statf.st_size);
541                   stat = JS_Differences;
542                }
543                break;
544             case '5':                /* compare MD5 */
545                Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
546                do_MD5 = TRUE;
547                break;
548             case ':':
549             case 'V':
550             default:
551                break;
552             }
553          }
554       /*
555        * Got MD5 Signature from Storage daemon
556        *  It came across in the Opts_MD5 field.
557        */
558       } else if (stream == STREAM_MD5_SIGNATURE) {
559          Dmsg2(400, "stream=MD5 inx=%d MD5=%s\n", file_index, Opts_MD5);
560          /* 
561           * When ever we get an MD5 signature is MUST have been
562           * preceded by an attributes record, which sets attr_file_index
563           */
564          if (jcr->FileIndex != (uint32_t)file_index) {
565             Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
566                file_index, jcr->FileIndex);
567             goto bail_out;
568          } 
569          if (do_MD5) {
570             db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
571             if (strcmp(buf, fdbr.MD5) != 0) {
572                prt_fname(jcr);
573                if (debug_level >= 10) {
574                   Jmsg(jcr, M_INFO, 0, _("      MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
575                } else {
576                   Jmsg(jcr, M_INFO, 0, _("      MD5 differs.\n"));
577                }
578                stat = JS_Differences;
579             }
580             do_MD5 = FALSE;
581          }
582       }
583       jcr->JobFiles = file_index;
584    } 
585    if (is_bnet_error(fd)) {
586       Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
587                         n, strerror(errno));
588       goto bail_out;
589    }
590
591    /* Now find all the files that are missing -- i.e. all files in
592     *  the database where the MarkedId != current JobId
593     */
594    jcr->fn_printed = FALSE;
595    sprintf(buf, 
596 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
597 "WHERE File.JobId=%d "
598 "AND File.MarkedId!=%d AND File.PathId=Path.PathId "
599 "AND File.FilenameId=Filename.FilenameId", 
600       JobId, jcr->JobId);
601    /* missing_handler is called for each file found */
602    db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
603    if (jcr->fn_printed) {
604       stat = JS_Differences;
605    }
606    free_pool_memory(fname);
607    jcr->JobStatus = stat;
608    return 1;
609     
610 bail_out:
611    free_pool_memory(fname);
612    jcr->JobStatus = JS_ErrorTerminated;
613    return 0;
614 }
615
616 /*
617  * We are called here for each record that matches the above
618  *  SQL query -- that is for each file contained in the Catalog
619  *  that was not marked earlier. This means that the file in
620  *  question is a missing file (in the Catalog but on on Disk).
621  */
622 static int missing_handler(void *ctx, int num_fields, char **row)
623 {
624    JCR *jcr = (JCR *)ctx;
625
626    if (!jcr->fn_printed) {
627       Jmsg(jcr, M_INFO, 0, "\n");
628       Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
629       jcr->fn_printed = TRUE;
630    }
631    Jmsg(jcr, M_INFO, 0, "      %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
632    return 0;
633 }
634
635
636 /* 
637  * Print filename for verify
638  */
639 static void prt_fname(JCR *jcr)
640 {
641    if (!jcr->fn_printed) {
642       Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
643       jcr->fn_printed = TRUE;
644    }
645 }