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