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-2010, 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 <getopt.h>
00029 #include "i18n.h"
00030 
00031 using namespace std;
00032 using namespace Barry;
00033 
00034 void Usage()
00035 {
00036    int major, minor;
00037    const char *Version = Barry::Version(major, minor);
00038 
00039    cerr
00040    << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
00041    << "        Copyright 2008-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
00042    << "        Using: " << Version << "\n"
00043    << "\n"
00044    << "   -d db     Read database 'db' and sum all its records.\n"
00045    << "             Can be used multiple times to fetch more than one DB\n"
00046    << "   -h        This help\n"
00047    << "   -i        Include Type and Unique record IDs in the checksums\n"
00048    << "   -p pin    PIN of device to talk with\n"
00049    << "             If only one device is plugged in, this flag is optional\n"
00050    << "   -P pass   Simplistic method to specify device password\n"
00051    << "   -v        Dump protocol data during operation\n"
00052    << endl;
00053 }
00054 
00055 class ChecksumParser : public Barry::Parser
00056 {
00057         bool m_IncludeIds;
00058         SHA_CTX m_ctx;
00059 
00060 public:
00061         explicit ChecksumParser(bool IncludeIds)
00062                 : m_IncludeIds(IncludeIds)
00063         {}
00064 
00065         virtual void Clear()
00066         {
00067                 SHA1_Init(&m_ctx);
00068         }
00069 
00070         virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
00071         {
00072                 if( m_IncludeIds ) {
00073                         SHA1_Update(&m_ctx, &RecType, sizeof(RecType));
00074                         SHA1_Update(&m_ctx, &UniqueId, sizeof(UniqueId));
00075                 }
00076         }
00077 
00078         virtual void ParseHeader(const Barry::Data &, size_t &)
00079         {
00080                 // do nothing here, parse it all at once in ParseFields
00081         }
00082 
00083         virtual void ParseFields(const Barry::Data &data, size_t &offset,
00084                                 const Barry::IConverter *ic)
00085         {
00086                 int len = data.GetSize() - offset;
00087                 SHA1_Update(&m_ctx, data.GetData() + offset, len);
00088                 offset += len;
00089         }
00090 
00091         virtual void Store()
00092         {
00093                 unsigned char sha1[SHA_DIGEST_LENGTH];
00094                 SHA1_Final(sha1, &m_ctx);
00095 
00096                 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
00097                         cout << hex << setfill('0') << setw(2)
00098                                 << (unsigned int) sha1[i];
00099                 }
00100                 cout << endl;
00101         }
00102 };
00103 
00104 int main(int argc, char *argv[])
00105 {
00106         INIT_I18N(PACKAGE);
00107 
00108         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00109                                         // stdio for debug messages
00110 
00111         try {
00112 
00113                 uint32_t pin = 0;
00114                 bool
00115                         data_dump = false,
00116                         include_ids = false;
00117                 string password;
00118                 vector<string> dbNames;
00119 
00120                 // process command line options
00121                 for(;;) {
00122                         int cmd = getopt(argc, argv, "d:hip:P:v");
00123                         if( cmd == -1 )
00124                                 break;
00125 
00126                         switch( cmd )
00127                         {
00128                         case 'd':       // show dbname
00129                                 dbNames.push_back(string(optarg));
00130                                 break;
00131 
00132                         case 'i':       // Include IDs
00133                                 include_ids = true;
00134                                 break;
00135 
00136                         case 'p':       // Blackberry PIN
00137                                 pin = strtoul(optarg, NULL, 16);
00138                                 break;
00139 
00140                         case 'P':       // Device password
00141                                 password = optarg;
00142                                 break;
00143 
00144                         case 'v':       // data dump on
00145                                 data_dump = true;
00146                                 break;
00147 
00148                         case 'h':       // help
00149                         default:
00150                                 Usage();
00151                                 return 0;
00152                         }
00153                 }
00154 
00155                 // Display usage info if user appears confused
00156                 if( !dbNames.size() ) {
00157                         Usage();
00158                         return 0;
00159                 }
00160 
00161                 // Initialize the barry library.  Must be called before
00162                 // anything else.
00163                 Barry::Init(data_dump);
00164 
00165                 // Probe the USB bus for Blackberry devices and display.
00166                 Barry::Probe probe;
00167                 int activeDevice = probe.FindActive(pin);
00168                 if( activeDevice == -1 ) {
00169                         cerr << "No device selected, or PIN not found" << endl;
00170                         return 1;
00171                 }
00172 
00173                 // Create our controller object
00174                 Barry::Controller con(probe.Get(activeDevice));
00175                 Barry::Mode::Desktop desktop(con);
00176 
00177                 // Sum all specified databases
00178                 if( dbNames.size() ) {
00179                         vector<string>::iterator b = dbNames.begin();
00180                         ChecksumParser parser(include_ids);
00181 
00182                         desktop.Open(password.c_str());
00183                         for( ; b != dbNames.end(); b++ ) {
00184                                 unsigned int id = desktop.GetDBID(*b);
00185                                 desktop.LoadDatabase(id, parser);
00186                         }
00187                 }
00188 
00189         }
00190         catch( std::exception &e ) {
00191                 std::cerr << e.what() << endl;
00192                 return 1;
00193         }
00194 
00195         return 0;
00196 }
00197 

Generated on 29 Mar 2010 for Barry by  doxygen 1.6.1