]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bls.c
Fix escape_string buffer overflows
[bacula/bacula] / bacula / src / stored / bls.c
1 /*
2  *
3  *  Dumb program to do an "ls" of a Bacula 1.0 mortal file.
4  *
5  *   Version $Id$
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29 #include "findlib/find.h"
30
31 static void do_blocks(char *infname);
32 static void do_jobs(char *infname);
33 static void do_ls(char *fname);
34 static void do_close(JCR *jcr);
35 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
36 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
37
38 static DEVICE *dev;
39 static int dump_label = FALSE;
40 static int list_blocks = FALSE;
41 static int list_jobs = FALSE;
42 static int verbose = 0;
43 static DEV_RECORD *rec;
44 static DEV_BLOCK *block;
45 static JCR *jcr;
46 static SESSION_LABEL sessrec;
47 static uint32_t num_files = 0;
48 static long record_file_index;
49
50 #define CONFIG_FILE "bacula-sd.conf"
51 char *configfile;
52
53
54 static FF_PKT ff;
55
56 static BSR *bsr = NULL;
57
58 static void usage()
59 {
60    fprintf(stderr,
61 "\nVersion: " VERSION " (" DATE ")\n\n"
62 "Usage: bls [-d debug_level] <physical-device-name>\n"
63 "       -b <file>       specify a bootstrap file\n"
64 "       -c <file>       specify a config file\n"
65 "       -d <level>      specify debug level\n"
66 "       -e <file>       exclude list\n"
67 "       -i <file>       include list\n"
68 "       -j              list jobs\n"
69 "       -k              list blocks\n"
70 "       -L              list tape label\n"
71 "    (none of above)    list saved files\n"
72 "       -v              be verbose\n"
73 "       -?              print this message\n\n");
74    exit(1);
75 }
76
77
78 int main (int argc, char *argv[])
79 {
80    int i, ch;
81    FILE *fd;
82    char line[1000];
83
84    working_directory = "/tmp";
85    my_name_is(argc, argv, "bls");
86    init_msg(NULL, NULL);              /* initialize message handler */
87
88    memset(&ff, 0, sizeof(ff));
89    init_include_exclude_files(&ff);
90
91    while ((ch = getopt(argc, argv, "b:c:d:e:i:jkLtv?")) != -1) {
92       switch (ch) {
93          case 'b':
94             bsr = parse_bsr(NULL, optarg);
95 //          dump_bsr(bsr);
96             break;
97
98          case 'c':                    /* specify config file */
99             if (configfile != NULL) {
100                free(configfile);
101             }
102             configfile = bstrdup(optarg);
103             break;
104
105          case 'd':                    /* debug level */
106             debug_level = atoi(optarg);
107             if (debug_level <= 0)
108                debug_level = 1; 
109             break;
110
111          case 'e':                    /* exclude list */
112             if ((fd = fopen(optarg, "r")) == NULL) {
113                Pmsg2(0, _("Could not open exclude file: %s, ERR=%s\n"),
114                   optarg, strerror(errno));
115                exit(1);
116             }
117             while (fgets(line, sizeof(line), fd) != NULL) {
118                strip_trailing_junk(line);
119                Dmsg1(100, "add_exclude %s\n", line);
120                add_fname_to_exclude_list(&ff, line);
121             }
122             fclose(fd);
123             break;
124
125          case 'i':                    /* include list */
126             if ((fd = fopen(optarg, "r")) == NULL) {
127                Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
128                   optarg, strerror(errno));
129                exit(1);
130             }
131             while (fgets(line, sizeof(line), fd) != NULL) {
132                strip_trailing_junk(line);
133                Dmsg1(100, "add_include %s\n", line);
134                add_fname_to_include_list(&ff, 0, line);
135             }
136             fclose(fd);
137             break;
138
139          case 'j':
140             list_jobs = TRUE;
141             break;
142
143          case 'k':
144             list_blocks = TRUE;
145             break;
146
147          case 'L':
148             dump_label = TRUE;
149             break;
150
151          case 'v':
152             verbose++;
153             break;
154
155          case '?':
156          default:
157             usage();
158
159       }  
160    }
161    argc -= optind;
162    argv += optind;
163
164    if (!argc) {
165       Pmsg0(0, "No archive name specified\n");
166       usage();
167    }
168
169    if (configfile == NULL) {
170       configfile = bstrdup(CONFIG_FILE);
171    }
172
173    parse_config(configfile);
174
175
176    if (ff.included_files_list == NULL) {
177       add_fname_to_include_list(&ff, 0, "/");
178    }
179
180    for (i=0; i < argc; i++) {
181       jcr = setup_jcr("bls", argv[i], bsr);
182       dev = setup_to_access_device(jcr, 1);   /* acquire for read */
183       if (!dev) {
184          exit(1);
185       }
186       rec = new_record();
187       block = new_block(dev);
188       /*
189        * Assume that we have already read the volume label.
190        * If on second or subsequent volume, adjust buffer pointer 
191        */
192       if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
193          Pmsg1(0, "\n\
194 Warning, this Volume is a continuation of Volume %s\n",
195                 dev->VolHdr.PrevVolName);
196       }
197
198       if (list_blocks) {
199          do_blocks(argv[i]);
200       } else if (list_jobs) {
201          do_jobs(argv[i]);
202       } else {
203          do_ls(argv[i]);
204       }
205       do_close(jcr);
206    }
207    if (bsr) {
208       free_bsr(bsr);
209    }
210    return 0;
211 }
212
213
214 static void do_close(JCR *jcr)
215 {
216    release_device(jcr, dev);
217    term_dev(dev);
218    free_record(rec);
219    free_block(block);
220    free_jcr(jcr);
221 }
222
223
224 /* List just block information */
225 static void do_blocks(char *infname)
226 {
227    if (verbose) {
228       dump_volume_label(dev);
229       rec = new_record();
230    }
231    for ( ;; ) {
232       if (!read_block_from_device(dev, block)) {
233          Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
234          if (dev->state & ST_EOT) {
235             if (!mount_next_read_volume(jcr, dev, block)) {
236                printf("End of File on device\n");
237                break;
238             }
239             DEV_RECORD *record;
240             record = new_record();
241             read_block_from_device(dev, block);
242             read_record_from_block(block, record);
243             get_session_record(dev, record, &sessrec);
244             free_record(record);
245             printf("Volume %s mounted.\n", jcr->VolumeName);
246             continue;
247          }
248          if (dev->state & ST_EOF) {
249             Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
250             Dmsg0(20, "read_record got eof. try again\n");
251             continue;
252          }
253          if (dev->state & ST_SHORT) {
254             Emsg0(M_INFO, 0, dev->errmsg);
255             continue;
256          }
257          display_error_status(dev);
258          break;
259       }
260       Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
261          block->BlockNumber, block->block_len, block->BlockVer,
262          block->VolSessionId, block->VolSessionTime);
263       if (verbose == 1) {
264          read_record_from_block(block, rec);
265          Pmsg7(-1, "Block: %u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
266               block->BlockNumber, block->block_len,
267               FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
268               stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
269          rec->remainder = 0;
270       } else if (verbose > 1) {
271          dump_block(block, "");
272       } else {
273          printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
274       }
275
276    }
277    return;
278 }
279
280 /*
281  * We are only looking for labels or in particula Job Session records
282  */
283 static void jobs_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
284 {
285    if (rec->FileIndex < 0) {
286       dump_label_record(dev, rec, verbose);
287    }
288    rec->remainder = 0;
289 }
290
291 /* Do list job records */
292 static void do_jobs(char *infname)
293 {
294    read_records(jcr, dev, jobs_cb, mount_next_read_volume);
295 }
296
297 /* Do an ls type listing of an archive */
298 static void do_ls(char *infname)
299 {
300    if (dump_label) {
301       dump_volume_label(dev);
302       return;
303    }
304
305    read_records(jcr, dev, record_cb, mount_next_read_volume);
306    printf("%u files found.\n", num_files);
307 }
308
309 /*
310  * Called here for each record from read_records()
311  */
312 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
313 {
314    char fname[2000];
315    struct stat statp;
316    int type;
317
318    if (rec->FileIndex < 0) {
319       get_session_record(dev, rec, &sessrec);
320       return;
321    }
322    /* File Attributes stream */
323    if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
324       char *ap, *fp;
325       sscanf(rec->data, "%ld %d", &record_file_index, &type);
326       if (record_file_index != rec->FileIndex) {
327          Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
328             rec->FileIndex, record_file_index);
329       }
330       ap = rec->data;
331
332       while (*ap++ != ' ')         /* skip record file index */
333          ;
334       while (*ap++ != ' ')         /* skip type */
335          ;
336       /* Save filename and position to attributes */
337       fp = fname;
338       while (*ap != 0) {
339          *fp++  = *ap++;
340       }
341       *fp = *ap++;                 /* terminate filename & point to attribs */
342
343       decode_stat(ap, &statp);
344       /* Skip to link name */  
345       while (*ap++ != 0)
346          ;
347       if (file_is_included(&ff, fname) && !file_is_excluded(&ff, fname)) {
348          print_ls_output(fname, ap, type, &statp);
349          num_files++;
350       }
351    }
352    return;
353 }
354
355
356 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
357 {
358    char *rtype;
359    memset(sessrec, 0, sizeof(sessrec));
360    switch (rec->FileIndex) {
361       case PRE_LABEL:
362          rtype = "Fresh Volume Label";   
363          break;
364       case VOL_LABEL:
365          rtype = "Volume Label";
366          unser_volume_label(dev, rec);
367          break;
368       case SOS_LABEL:
369          rtype = "Begin Session";
370          unser_session_label(sessrec, rec);
371          break;
372       case EOS_LABEL:
373          rtype = "End Session";
374          break;
375       case EOM_LABEL:
376          rtype = "End of Media";
377          break;
378       default:
379          rtype = "Unknown";
380          break;
381    }
382    Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
383          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
384 }
385
386
387 /* Dummies to replace askdir.c */
388 int     dir_get_volume_info(JCR *jcr, int writing) { return 1;}
389 int     dir_find_next_appendable_volume(JCR *jcr) { return 1;}
390 int     dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
391 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
392 int     dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
393 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
394 int     dir_send_job_status(JCR *jcr) {return 1;}
395
396
397 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
398 {
399    fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
400       jcr->VolumeName, dev_name(dev));
401    getchar();   
402    return 1;
403 }