bidentify.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       bidentify.cc
00003 ///             Tool for probing identifying Blackberry devices
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include <barry/barry.h>
00023 #include <iostream>
00024 #include <iomanip>
00025 #include "barrygetopt.h"
00026 #include "i18n.h"
00027 
00028 using namespace std;
00029 using namespace Barry;
00030 
00031 void Usage()
00032 {
00033    int logical, major, minor;
00034    const char *Version = Barry::Version(logical, major, minor);
00035 
00036    cerr
00037    << "bidentify - USB Blackberry Identifier Tool\n"
00038    << "            Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
00039    << "            Using: " << Version << "\n"
00040    << "\n"
00041    << "   -B bus    Specify which USB bus to search on\n"
00042    << "   -N dev    Specify which system device, using system specific string\n"
00043    << "\n"
00044    << "   -c        If used with -m, show both hex and dec where possible\n"
00045    << "   -m        Also show the device's ESN / MEID / IMEI\n"
00046    << "   -h        This help\n"
00047    << "   -v        Dump protocol data during operation\n"
00048    << endl;
00049 }
00050 
00051 std::string FetchMEID(Barry::Mode::Desktop &desktop)
00052 {
00053         using namespace Barry;
00054 
00055         unsigned int dbId = desktop.GetDBID(HandheldAgent::GetDBName());
00056         RecordStateTable state;
00057         desktop.GetRecordStateTable(dbId, state);
00058 
00059         unsigned int recId = HandheldAgent::GetMEIDRecordId();
00060 
00061         RecordStateTable::IndexType meid_index;
00062         if( state.GetIndex(recId, &meid_index) ) {
00063                 NullStore<HandheldAgent> store;
00064                 RecordParser<HandheldAgent, NullStore<HandheldAgent> > parser(store);
00065                 desktop.GetRecord(dbId, meid_index, parser);
00066                 return parser.GetRecord().MEID;
00067         }
00068         else {
00069                 return string();
00070         }
00071 }
00072 
00073 int main(int argc, char *argv[])
00074 {
00075         INIT_I18N(PACKAGE);
00076 
00077         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00078                                         // stdio for debug messages
00079 
00080         try {
00081 
00082                 bool data_dump = false,
00083                         get_meid = false,
00084                         show_both = false;
00085                 string busname;
00086                 string devname;
00087 
00088                 // process command line options
00089                 for(;;) {
00090                         int cmd = getopt(argc, argv, "B:hN:vmc");
00091                         if( cmd == -1 )
00092                                 break;
00093 
00094                         switch( cmd )
00095                         {
00096                         case 'B':       // busname
00097                                 busname = optarg;
00098                                 break;
00099 
00100                         case 'N':       // Devname
00101                                 devname = optarg;
00102                                 break;
00103 
00104                         case 'c':       // show both hex and dec
00105                                 show_both = true;
00106                                 break;
00107 
00108                         case 'm':       // get meid / esn
00109                                 get_meid = true;
00110                                 break;
00111 
00112                         case 'v':       // data dump on
00113                                 data_dump = true;
00114                                 break;
00115 
00116                         case 'h':       // help
00117                         default:
00118                                 Usage();
00119                                 return 0;
00120                         }
00121                 }
00122 
00123                 Barry::Init(data_dump, &std::cerr);
00124                 Barry::Probe probe(busname.c_str(), devname.c_str());
00125 
00126                 // show all successfully found devices
00127                 for( int i = 0; i < probe.GetCount(); i++ ) {
00128                         const ProbeResult &pr = probe.Get(i);
00129                         cout    << pr.m_pin.Str() << ", "
00130                                 << pr.m_description;
00131 
00132                         if( get_meid ) try {
00133                                 Barry::Controller con(pr);
00134                                 Barry::Mode::Desktop desktop(con);
00135                                 desktop.Open();
00136 
00137                                 // store as temporary in case of exceptions
00138                                 string meid = FetchMEID(desktop);
00139                                 cout << ", " << meid;
00140 
00141                                 if( show_both ) {
00142                                         if( HandheldAgent::IsESNHex(meid) ) {
00143                                                 cout << " (" << HandheldAgent::ESNHex2Dec(meid) << ")";
00144                                         }
00145                                         else if( HandheldAgent::IsESNDec(meid) ) {
00146                                                 cout << " (" << HandheldAgent::ESNDec2Hex(meid) << ")";
00147                                         }
00148                                 }
00149                         }
00150                         catch( Barry::BadPassword & ) {
00151                                 // ignore password protected devices
00152                         }
00153                         catch( Barry::Error &e ) {
00154                                 // output on cerr so as not to mess up
00155                                 // the identify output
00156                                 cerr << "Error on PIN: " << pr.m_pin.Str() << ": " << e.what() << endl;
00157                         }
00158 
00159                         // finish the identity line
00160                         cout << endl;
00161                 }
00162 
00163                 return probe.GetFailCount();
00164 
00165         }
00166         catch( std::exception &e ) {
00167                 cerr << "exception caught: " << e.what() << endl;
00168                 return 1;
00169         }
00170 }
00171