00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <barry/barry.h>
00023 #include <iomanip>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <vector>
00028 #include <string>
00029 #include <algorithm>
00030 #include <getopt.h>
00031
00032
00033 using namespace std;
00034 using namespace Barry;
00035
00036 void Usage()
00037 {
00038 int major, minor;
00039 const char *Version = Barry::Version(major, minor);
00040
00041 cerr
00042 << "btool - Command line USB Blackberry Test Tool\n"
00043 << " Copyright 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
00044 << " Using: " << Version << "\n"
00045 << " Compiled "
00046 #ifdef __BARRY_BOOST_MODE__
00047 << "with"
00048 #else
00049 << "without"
00050 #endif
00051 << " Boost support\n"
00052 << "\n"
00053 << " -B bus Specify which USB bus to search on\n"
00054 << " -N dev Specify which system device, using system specific string\n"
00055 << "\n"
00056 << " -c dn Convert address book database to LDIF format, using the\n"
00057 << " specified baseDN\n"
00058 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
00059 << " Defaults to 'cn'\n"
00060 << " -d db Load database 'db' FROM device and dump to screen\n"
00061 << " Can be used multiple times to fetch more than one DB\n"
00062 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
00063 << " string separated by a comma, holding the read,write\n"
00064 << " endpoint pair. Example: -e 83,5\n"
00065 << " Note: Endpoints are specified in hex.\n"
00066 << " You should never need to use this option.\n"
00067 #ifdef __BARRY_BOOST_MODE__
00068 << " -f file Filename to save or load handheld data to/from\n"
00069 #endif
00070 << " -h This help\n"
00071 << " -i cs International charset for string conversions\n"
00072 << " Valid values here are available with 'iconv --list'\n"
00073 << " -l List devices\n"
00074 << " -L List Contact field names\n"
00075 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
00076 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
00077 << " Unmap: ldif name alone\n"
00078 << " -M List current LDIF mapping\n"
00079 << " -n Use null parser on all databases.\n"
00080 << " -p pin PIN of device to talk with\n"
00081 << " If only one device is plugged in, this flag is optional\n"
00082 << " -P pass Simplistic method to specify device password\n"
00083 << " -s db Save database 'db' TO device from data loaded from -f file\n"
00084 << " -S Show list of supported database parsers\n"
00085 << " -t Show database database table\n"
00086 << " -T db Show record state table for given database\n"
00087 << " -v Dump protocol data during operation\n"
00088 << " -X Reset device\n"
00089 << " -z Use non-threaded sockets\n"
00090 << " -Z Use threaded socket router (default)\n"
00091 << "\n"
00092 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
00093 << "\n"
00094 << " -r # Record index number as seen in the -T state table.\n"
00095 << " This overrides the default -d behaviour, and only\n"
00096 << " downloads the one specified record, sending to stdout.\n"
00097 << " -R # Same as -r, but also clears the record's dirty flags.\n"
00098 << " -D # Record index number as seen in the -T state table,\n"
00099 << " which indicates the record to delete. Used with the -d\n"
00100 << " command to specify the database.\n"
00101 << endl;
00102 }
00103
00104 class Contact2Ldif
00105 {
00106 public:
00107 Barry::ContactLdif &ldif;
00108
00109 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
00110
00111 void operator()(const Contact &rec)
00112 {
00113 ldif.DumpLdif(cout, rec);
00114 }
00115 };
00116
00117 template <class Record>
00118 struct Store
00119 {
00120 std::vector<Record> records;
00121 mutable typename std::vector<Record>::const_iterator rec_it;
00122 std::string filename;
00123 bool load;
00124 int count;
00125
00126 Store(const string &filename, bool load)
00127 : rec_it(records.end()),
00128 filename(filename),
00129 load(load),
00130 count(0)
00131 {
00132 #ifdef __BARRY_BOOST_MODE__
00133 try {
00134
00135 if( load && filename.size() ) {
00136
00137 cout << "Loading: " << filename << endl;
00138 ifstream ifs(filename.c_str());
00139 std::string dbName;
00140 getline(ifs, dbName);
00141 boost::archive::text_iarchive ia(ifs);
00142 ia >> records;
00143 cout << records.size()
00144 << " records loaded from '"
00145 << filename << "'" << endl;
00146 sort(records.begin(), records.end());
00147 rec_it = records.begin();
00148
00149
00150 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
00151 for( ; beg != end; beg++ ) {
00152 cout << (*beg) << endl;
00153 }
00154 }
00155
00156 } catch( boost::archive::archive_exception &ae ) {
00157 cerr << "Archive exception in ~Store(): "
00158 << ae.what() << endl;
00159 }
00160 #endif
00161 }
00162 ~Store()
00163 {
00164 cout << "Store counted " << dec << count << " records." << endl;
00165 #ifdef __BARRY_BOOST_MODE__
00166 try {
00167
00168 if( !load && filename.size() ) {
00169
00170 cout << "Saving: " << filename << endl;
00171 const std::vector<Record> &r = records;
00172 ofstream ofs(filename.c_str());
00173 ofs << Record::GetDBName() << endl;
00174 boost::archive::text_oarchive oa(ofs);
00175 oa << r;
00176 cout << dec << r.size() << " records saved to '"
00177 << filename << "'" << endl;
00178 }
00179
00180 } catch( boost::archive::archive_exception &ae ) {
00181 cerr << "Archive exception in ~Store(): "
00182 << ae.what() << endl;
00183 }
00184 #endif
00185 }
00186
00187
00188 void operator()(const Record &rec)
00189 {
00190 count++;
00191 std::cout << rec << std::endl;
00192 records.push_back(rec);
00193 }
00194
00195
00196 bool operator()(Record &rec, unsigned int databaseId) const
00197 {
00198 if( rec_it == records.end() )
00199 return false;
00200 rec = *rec_it;
00201 rec_it++;
00202 return true;
00203 }
00204 };
00205
00206 class DataDumpParser : public Barry::Parser
00207 {
00208 uint32_t m_id;
00209
00210 public:
00211 virtual void Clear() {}
00212
00213 virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
00214 {
00215 m_id = UniqueId;
00216 }
00217
00218 virtual void ParseHeader(const Data &, size_t &) {}
00219
00220 virtual void ParseFields(const Barry::Data &data, size_t &offset,
00221 const IConverter *ic)
00222 {
00223 std::cout << "Raw record dump for record: "
00224 << std::hex << m_id << std::endl;
00225 std::cout << data << std::endl;
00226 }
00227
00228 virtual void Store() {}
00229 };
00230
00231 auto_ptr<Parser> GetParser(const string &name, const string &filename, bool null_parser)
00232 {
00233 if( null_parser ) {
00234
00235 return auto_ptr<Parser>( new DataDumpParser );
00236 }
00237
00238 else if( name == Contact::GetDBName() ) {
00239 return auto_ptr<Parser>(
00240 new RecordParser<Contact, Store<Contact> > (
00241 new Store<Contact>(filename, false)));
00242 }
00243 else if( name == Message::GetDBName() ) {
00244 return auto_ptr<Parser>(
00245 new RecordParser<Message, Store<Message> > (
00246 new Store<Message>(filename, false)));
00247 }
00248 else if( name == Calendar::GetDBName() ) {
00249 return auto_ptr<Parser>(
00250 new RecordParser<Calendar, Store<Calendar> > (
00251 new Store<Calendar>(filename, false)));
00252 }
00253 else if( name == ServiceBook::GetDBName() ) {
00254 return auto_ptr<Parser>(
00255 new RecordParser<ServiceBook, Store<ServiceBook> > (
00256 new Store<ServiceBook>(filename, false)));
00257 }
00258
00259 else if( name == Memo::GetDBName() ) {
00260 return auto_ptr<Parser>(
00261 new RecordParser<Memo, Store<Memo> > (
00262 new Store<Memo>(filename, false)));
00263 }
00264 else if( name == Task::GetDBName() ) {
00265 return auto_ptr<Parser>(
00266 new RecordParser<Task, Store<Task> > (
00267 new Store<Task>(filename, false)));
00268 }
00269 else if( name == PINMessage::GetDBName() ) {
00270 return auto_ptr<Parser>(
00271 new RecordParser<PINMessage, Store<PINMessage> > (
00272 new Store<PINMessage>(filename, false)));
00273 }
00274 else if( name == SavedMessage::GetDBName() ) {
00275 return auto_ptr<Parser>(
00276 new RecordParser<SavedMessage, Store<SavedMessage> > (
00277 new Store<SavedMessage>(filename, false)));
00278 }
00279 else if( name == Folder::GetDBName() ) {
00280 return auto_ptr<Parser>(
00281 new RecordParser<Folder, Store<Folder> > (
00282 new Store<Folder>(filename, false)));
00283 }
00284 else if( name == Timezone::GetDBName() ) {
00285 return auto_ptr<Parser>(
00286 new RecordParser<Timezone, Store<Timezone> > (
00287 new Store<Timezone>(filename, false)));
00288 }
00289 else {
00290
00291 return auto_ptr<Parser>( new DataDumpParser );
00292 }
00293 }
00294
00295 auto_ptr<Builder> GetBuilder(const string &name, const string &filename)
00296 {
00297
00298 if( name == Contact::GetDBName() ) {
00299 return auto_ptr<Builder>(
00300 new RecordBuilder<Contact, Store<Contact> > (
00301 new Store<Contact>(filename, true)));
00302 }
00303 else if( name == Calendar::GetDBName() ) {
00304 return auto_ptr<Builder>(
00305 new RecordBuilder<Calendar, Store<Calendar> > (
00306 new Store<Calendar>(filename, true)));
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 else {
00331 throw std::runtime_error("No Builder available for database");
00332 }
00333 }
00334
00335 void ShowParsers()
00336 {
00337 cout << "Supported Database parsers:\n"
00338 << " Address Book\n"
00339 << " Messages\n"
00340 << " Calendar\n"
00341 << " Service Book\n"
00342 << " Memos\n"
00343 << " Tasks\n"
00344 << " PIN Messages\n"
00345 << " Saved Email Messages\n"
00346 << " Folders\n"
00347 << " Time Zones (read only)\n"
00348 << "\n"
00349 << "Supported Database builders:\n"
00350 << " Address Book\n"
00351 << " Calendar\n"
00352 << endl;
00353 }
00354
00355 struct StateTableCommand
00356 {
00357 char flag;
00358 bool clear;
00359 unsigned int index;
00360
00361 StateTableCommand(char f, bool c, unsigned int i)
00362 : flag(f), clear(c), index(i) {}
00363 };
00364
00365 bool SplitMap(const string &map, string &ldif, string &read, string &write)
00366 {
00367 string::size_type a = map.find(',');
00368 if( a == string::npos )
00369 return false;
00370
00371 string::size_type b = map.find(',', a+1);
00372 if( b == string::npos )
00373 return false;
00374
00375 ldif.assign(map, 0, a);
00376 read.assign(map, a + 1, b - a - 1);
00377 write.assign(map, b + 1, map.size() - b - 1);
00378
00379 return ldif.size() && read.size() && write.size();
00380 }
00381
00382 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
00383 {
00384 for( vector<string>::const_iterator i = mapCommands.begin();
00385 i != mapCommands.end();
00386 ++i )
00387 {
00388
00389 if( i->find(',') == string::npos ) {
00390
00391 cerr << "Unmapping: " << *i << endl;
00392 ldif.Unmap(*i);
00393 }
00394 else {
00395 cerr << "Mapping: " << *i << endl;
00396
00397
00398 string ldifname, read, write;
00399 if( SplitMap(*i, ldifname, read, write) ) {
00400 if( !ldif.Map(ldifname, read, write) ) {
00401 cerr << "Read/Write name unknown: " << *i << endl;
00402 }
00403 }
00404 else {
00405 cerr << "Invalid map format: " << *i << endl;
00406 }
00407 }
00408 }
00409 }
00410
00411 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
00412 {
00413 int read, write;
00414 char comma;
00415 istringstream iss(arg);
00416 iss >> hex >> read >> comma >> write;
00417 if( !iss )
00418 return false;
00419 epp->read = read;
00420 epp->write = write;
00421 return true;
00422 }
00423
00424 int main(int argc, char *argv[])
00425 {
00426 cout.sync_with_stdio(true);
00427
00428
00429 try {
00430
00431 uint32_t pin = 0;
00432 bool list_only = false,
00433 show_dbdb = false,
00434 ldif_contacts = false,
00435 data_dump = false,
00436 reset_device = false,
00437 list_contact_fields = false,
00438 list_ldif_map = false,
00439 epp_override = false,
00440 threaded_sockets = true,
00441 record_state = false,
00442 null_parser = false;
00443 string ldifBaseDN, ldifDnAttr;
00444 string filename;
00445 string password;
00446 string busname;
00447 string devname;
00448 string iconvCharset;
00449 vector<string> dbNames, saveDbNames, mapCommands;
00450 vector<StateTableCommand> stCommands;
00451 Usb::EndpointPair epOverride;
00452
00453
00454 for(;;) {
00455 int cmd = getopt(argc, argv, "B:c:C:d:D:e:f:hi:lLm:MnN:p:P:r:R:Ss:tT:vXzZ");
00456 if( cmd == -1 )
00457 break;
00458
00459 switch( cmd )
00460 {
00461 case 'B':
00462 busname = optarg;
00463 break;
00464
00465 case 'c':
00466 ldif_contacts = true;
00467 ldifBaseDN = optarg;
00468 break;
00469
00470 case 'C':
00471 ldifDnAttr = optarg;
00472 break;
00473
00474 case 'd':
00475 dbNames.push_back(string(optarg));
00476 break;
00477
00478 case 'D':
00479 stCommands.push_back(
00480 StateTableCommand('D', false, atoi(optarg)));
00481 break;
00482
00483 case 'e':
00484 if( !ParseEpOverride(optarg, &epOverride) ) {
00485 Usage();
00486 return 1;
00487 }
00488 epp_override = true;
00489 break;
00490
00491 case 'f':
00492 #ifdef __BARRY_BOOST_MODE__
00493 filename = optarg;
00494 #else
00495 cerr << "-f option not supported - no Boost "
00496 "serialization support available\n";
00497 return 1;
00498 #endif
00499 break;
00500
00501 case 'i':
00502 iconvCharset = optarg;
00503 break;
00504
00505 case 'l':
00506 list_only = true;
00507 break;
00508
00509 case 'L':
00510 list_contact_fields = true;
00511 break;
00512
00513 case 'm':
00514 mapCommands.push_back(string(optarg));
00515 break;
00516
00517 case 'M':
00518 list_ldif_map = true;
00519 break;
00520
00521 case 'n':
00522 null_parser = true;
00523 break;
00524
00525 case 'N':
00526 devname = optarg;
00527 break;
00528
00529 case 'p':
00530 pin = strtoul(optarg, NULL, 16);
00531 break;
00532
00533 case 'P':
00534 password = optarg;
00535 break;
00536
00537 case 'r':
00538 stCommands.push_back(
00539 StateTableCommand('r', false, atoi(optarg)));
00540 break;
00541
00542 case 'R':
00543 stCommands.push_back(
00544 StateTableCommand('r', true, atoi(optarg)));
00545 break;
00546
00547 case 's':
00548 saveDbNames.push_back(string(optarg));
00549 break;
00550
00551 case 'S':
00552 ShowParsers();
00553 return 0;
00554
00555 case 't':
00556 show_dbdb = true;
00557 break;
00558
00559 case 'T':
00560 record_state = true;
00561 dbNames.push_back(string(optarg));
00562 break;
00563
00564 case 'v':
00565 data_dump = true;
00566 break;
00567
00568 case 'X':
00569 reset_device = true;
00570 break;
00571
00572 case 'z':
00573 threaded_sockets = false;
00574 break;
00575
00576 case 'Z':
00577 threaded_sockets = true;
00578 break;
00579
00580 case 'h':
00581 default:
00582 Usage();
00583 return 0;
00584 }
00585 }
00586
00587
00588
00589 Barry::Init(data_dump);
00590
00591
00592 auto_ptr<IConverter> ic;
00593 if( iconvCharset.size() ) {
00594 ic.reset( new IConverter(iconvCharset.c_str(), true) );
00595 }
00596
00597
00598 ContactLdif ldif(ldifBaseDN);
00599 DoMapping(ldif, mapCommands);
00600 if( ldifDnAttr.size() ) {
00601 if( !ldif.SetDNAttr(ldifDnAttr) ) {
00602 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
00603 }
00604 }
00605
00606
00607
00608
00609 Barry::Probe probe(busname.c_str(), devname.c_str());
00610 int activeDevice = -1;
00611
00612
00613 if( probe.GetFailCount() ) {
00614 if( ldif_contacts )
00615 cout << "# ";
00616 cout << "Blackberry device errors with errors during probe:" << endl;
00617 for( int i = 0; i < probe.GetFailCount(); i++ ) {
00618 if( ldif_contacts )
00619 cout << "# ";
00620 cout << probe.GetFailMsg(i) << endl;
00621 }
00622 }
00623
00624
00625 if( ldif_contacts )
00626 cout << "# ";
00627 cout << "Blackberry devices found:" << endl;
00628 for( int i = 0; i < probe.GetCount(); i++ ) {
00629 if( ldif_contacts )
00630 cout << "# ";
00631 if( data_dump )
00632 probe.Get(i).DumpAll(cout);
00633 else
00634 cout << probe.Get(i);
00635 cout << endl;
00636 if( probe.Get(i).m_pin == pin )
00637 activeDevice = i;
00638 }
00639
00640 if( list_only )
00641 return 0;
00642
00643 if( activeDevice == -1 ) {
00644 if( pin == 0 ) {
00645
00646 if( probe.GetCount() == 1 )
00647 activeDevice = 0;
00648 else {
00649 cerr << "No device selected" << endl;
00650 return 1;
00651 }
00652 }
00653 else {
00654 cerr << "PIN " << setbase(16) << pin
00655 << " not found" << endl;
00656 return 1;
00657 }
00658 }
00659
00660 if( ldif_contacts )
00661 cout << "# ";
00662 cout << "Using device (PIN): " << setbase(16)
00663 << probe.Get(activeDevice).m_pin << endl;
00664
00665 if( reset_device ) {
00666 Usb::Device dev(probe.Get(activeDevice).m_dev);
00667 dev.Reset();
00668 return 0;
00669 }
00670
00671
00672 Barry::ProbeResult device = probe.Get(activeDevice);
00673 if( epp_override ) {
00674 device.m_ep.read = epOverride.read;
00675 device.m_ep.write = epOverride.write;
00676 device.m_ep.type = 2;
00677 cout << "Endpoint pair (read,write) overridden with: "
00678 << hex
00679 << (unsigned int) device.m_ep.read << ","
00680 << (unsigned int) device.m_ep.write << endl;
00681 }
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693 auto_ptr<SocketRoutingQueue> router;
00694 auto_ptr<Barry::Controller> pcon;
00695 if( threaded_sockets ) {
00696 router.reset( new SocketRoutingQueue );
00697 router->SpinoffSimpleReadThread();
00698 pcon.reset( new Barry::Controller(device, *router) );
00699 }
00700 else {
00701 pcon.reset( new Barry::Controller(device) );
00702 }
00703
00704 Barry::Controller &con = *pcon;
00705 Barry::Mode::Desktop desktop(con, *ic);
00706
00707
00708
00709
00710
00711
00712
00713 if( show_dbdb ) {
00714
00715 desktop.Open(password.c_str());
00716 cout << desktop.GetDBDB() << endl;
00717 }
00718
00719
00720 if( list_contact_fields ) {
00721 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
00722 cout.fill(' ');
00723 cout << " " << left << setw(20) << n->name << ": "
00724 << n->description << endl;
00725 }
00726 }
00727
00728
00729 if( list_ldif_map ) {
00730 cout << ldif << endl;
00731 }
00732
00733
00734
00735 if( ldif_contacts ) {
00736
00737 desktop.Open(password.c_str());
00738
00739
00740
00741 Contact2Ldif storage(ldif);
00742
00743
00744 desktop.LoadDatabaseByType<Barry::Contact>(storage);
00745 }
00746
00747
00748 if( record_state ) {
00749 if( dbNames.size() == 0 ) {
00750 cout << "No db names to process" << endl;
00751 return 1;
00752 }
00753
00754 desktop.Open(password.c_str());
00755
00756 vector<string>::iterator b = dbNames.begin();
00757 for( ; b != dbNames.end(); b++ ) {
00758 unsigned int id = desktop.GetDBID(*b);
00759 RecordStateTable state;
00760 desktop.GetRecordStateTable(id, state);
00761 cout << "Record state table for: " << *b << endl;
00762 cout << state;
00763 }
00764 return 0;
00765 }
00766
00767
00768 if( stCommands.size() ) {
00769 if( dbNames.size() != 1 ) {
00770 cout << "Must have 1 db name to process" << endl;
00771 return 1;
00772 }
00773
00774 desktop.Open(password.c_str());
00775 unsigned int id = desktop.GetDBID(dbNames[0]);
00776 auto_ptr<Parser> parse = GetParser(dbNames[0],filename,null_parser);
00777
00778 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
00779 desktop.GetRecord(id, stCommands[i].index, *parse.get());
00780
00781 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
00782 cout << "Clearing record's dirty flags..." << endl;
00783 desktop.ClearDirty(id, stCommands[i].index);
00784 }
00785
00786 if( stCommands[i].flag == 'D' ) {
00787 desktop.DeleteRecord(id, stCommands[i].index);
00788 }
00789 }
00790
00791 return 0;
00792 }
00793
00794
00795
00796
00797 if( dbNames.size() ) {
00798 vector<string>::iterator b = dbNames.begin();
00799
00800 desktop.Open(password.c_str());
00801 for( ; b != dbNames.end(); b++ ) {
00802 auto_ptr<Parser> parse = GetParser(*b,filename,null_parser);
00803 unsigned int id = desktop.GetDBID(*b);
00804 desktop.LoadDatabase(id, *parse.get());
00805 }
00806 }
00807
00808
00809
00810 if( saveDbNames.size() ) {
00811 vector<string>::iterator b = saveDbNames.begin();
00812
00813 desktop.Open(password.c_str());
00814 for( ; b != saveDbNames.end(); b++ ) {
00815 auto_ptr<Builder> build =
00816 GetBuilder(*b, filename);
00817 unsigned int id = desktop.GetDBID(*b);
00818 desktop.SaveDatabase(id, *build);
00819 }
00820 }
00821
00822 }
00823 catch( Usb::Error &ue) {
00824 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00825 return 1;
00826 }
00827 catch( Barry::Error &se ) {
00828 std::cerr << "Barry::Error caught: " << se.what() << endl;
00829 return 1;
00830 }
00831 catch( std::exception &e ) {
00832 std::cerr << "std::exception caught: " << e.what() << endl;
00833 return 1;
00834 }
00835
00836 return 0;
00837 }
00838