brecsum.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       brecsum.cc
00003 ///             Generate SHA1 sums of raw Blackberry database records.
00004 ///             This is mostly useful for data verification during testing.
00005 ///
00006 
00007 /*
00008     Copyright (C) 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019     See the GNU General Public License in the COPYING file at the
00020     root directory of this project for more details.
00021 */
00022 
00023 #include <barry/barry.h>
00024 #include <iomanip>
00025 #include <iostream>
00026 #include <vector>
00027 #include <string>
00028 #include <stdlib.h>
00029 #include "i18n.h"
00030 #include "brecsum.h"
00031 #include "barrygetopt.h"
00032 
00033 using namespace std;
00034 using namespace Barry;
00035 
00036 void Usage()
00037 {
00038    int logical, major, minor;
00039    const char *Version = Barry::Version(logical, major, minor);
00040 
00041    cerr
00042    << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
00043    << "        Copyright 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
00044    << "        Using: " << Version << "\n"
00045    << "\n"
00046    << "   -d db     Read database 'db' and sum all its records.\n"
00047    << "             Can be used multiple times to fetch more than one DB\n"
00048    << "   -h        This help\n"
00049    << "   -i        Include DB Name, Type, and Unique record IDs in the checksums\n"
00050    << "   -p pin    PIN of device to talk with\n"
00051    << "             If only one device is plugged in, this flag is optional\n"
00052    << "   -P pass   Simplistic method to specify device password\n"
00053    << "   -v        Dump protocol data during operation\n"
00054    << endl;
00055 }
00056 
00057 int main(int argc, char *argv[])
00058 {
00059         INIT_I18N(PACKAGE);
00060 
00061         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00062                                         // stdio for debug messages
00063 
00064         try {
00065 
00066                 uint32_t pin = 0;
00067                 bool
00068                         data_dump = false,
00069                         include_ids = false;
00070                 string password;
00071                 vector<string> dbNames;
00072 
00073                 // process command line options
00074                 for(;;) {
00075                         int cmd = getopt(argc, argv, "d:hip:P:v");
00076                         if( cmd == -1 )
00077                                 break;
00078 
00079                         switch( cmd )
00080                         {
00081                         case 'd':       // show dbname
00082                                 dbNames.push_back(string(optarg));
00083                                 break;
00084 
00085                         case 'i':       // Include IDs
00086                                 include_ids = true;
00087                                 break;
00088 
00089                         case 'p':       // Blackberry PIN
00090                                 pin = strtoul(optarg, NULL, 16);
00091                                 break;
00092 
00093                         case 'P':       // Device password
00094                                 password = optarg;
00095                                 break;
00096 
00097                         case 'v':       // data dump on
00098                                 data_dump = true;
00099                                 break;
00100 
00101                         case 'h':       // help
00102                         default:
00103                                 Usage();
00104                                 return 0;
00105                         }
00106                 }
00107 
00108                 // Display usage info if user appears confused
00109                 if( !dbNames.size() ) {
00110                         Usage();
00111                         return 0;
00112                 }
00113 
00114                 // Initialize the barry library.  Must be called before
00115                 // anything else.
00116                 Barry::Init(data_dump);
00117 
00118                 // Probe the USB bus for Blackberry devices and display.
00119                 Barry::Probe probe;
00120                 int activeDevice = probe.FindActive(pin);
00121                 if( activeDevice == -1 ) {
00122                         cerr << "No device selected, or PIN not found" << endl;
00123                         return 1;
00124                 }
00125 
00126                 // Create our controller object
00127                 Barry::Controller con(probe.Get(activeDevice));
00128                 Barry::Mode::Desktop desktop(con);
00129 
00130                 // Sum all specified databases
00131                 if( dbNames.size() ) {
00132                         vector<string>::iterator b = dbNames.begin();
00133                         ChecksumParser parser(include_ids);
00134 
00135                         desktop.Open(password.c_str());
00136                         for( ; b != dbNames.end(); b++ ) {
00137                                 unsigned int id = desktop.GetDBID(*b);
00138                                 desktop.LoadDatabase(id, parser);
00139                         }
00140                 }
00141 
00142         }
00143         catch( std::exception &e ) {
00144                 std::cerr << e.what() << endl;
00145                 return 1;
00146         }
00147 
00148         return 0;
00149 }
00150