AbominableMonk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, oh-robed-ones,

We have a situation where security policy mandates that the database connection is handled by a super-secret C++ class in an .o file... Fine for C/C++, but I need to connect to the DB from Perl (without using DBI->connect), since I don't have the user/pw, so...

XS/Swig it is.

Passing data such as strings/ints/etc is not an issue using the above wrappers, but what about calling a C function to connect to the DB, and returning that *handle* (pointer presumably?) to Perl for use in subsequent DBI usage such as $dbh->do/execute/etc?

Anyone done something like this? Will it even work? I'm hoping to save some time, and don't want to embark on this journey unless I know it's possible...

Replies are listed 'Best First'.
Re: DB -> C++ -> XS/Swig -> DBI -> Perl
by salva (Canon) on Jan 29, 2014 at 11:09 UTC
    You could fork the DBD source code for your particular database and replace the part where the connection is set up for some other that calls that C++ library.

    It is difficult to give you a more detailed response without knowing the details (which database is being used, what do you get from that C++ library, etc.).

    Maybe you could just open the binary object for that C++ library from Perl and use a couple of regular expressions to extract the database user and password!

Re: DB -> C++ -> XS/Swig -> DBI -> Perl
by Jenda (Abbot) on Jan 29, 2014 at 11:16 UTC

    I think you'll have to provide more information. What database? What OS? What exactly is the "thing" the C++ method returns? How do you use it later?

    There might be a way to create the right DBI object wrapping the connection opened elsewhere, but if there is it will be driver specific.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Apologies: Oracle 10, on Solaris 5.10, using ProC/C++ and Sun C++ 5.9. Perl is 5.8. It's a typical Oracle Connection instance which is returned.

        Have a look at the ora_use_proc_connection option in DBD::Oracle docs. Maybe that can be used to create a DBI object for the connection you get from the C++ method.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: DB -> C++ -> XS/Swig -> DBI -> Perl
by bulk88 (Priest) on Jan 30, 2014 at 00:34 UTC
    Distributing object code is not security. Use a disassembler, or debugger with asm level breakpoints. Put a breakpoint on the start of the asm code of the db connection function call. Look at register esp/rsp, and the C prototype of the db connect function. Look at the C stack as 4/8byte int arrays. On entry to the C function, esp/rsp will be -4/-8 from return address or on the return address, Work your way numerically upwards from the pointer in reg esp. One of those will a string pointer with the db username and password.

      Possibly another way to get username and password: If database and "secret" C++ code run or can run on different machines, connect both machines with a hub (or a managed switch with a "monitor" port), connect a third machine running wireshark, trace packages send from the client to the database. My guess is that the database does not encrypt communication, at least not by default. Probably, wireshark knows the database protocol. In the best case, username and password are send in plain text over the wire.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      That does not answer the question. We all know what you describe can be done.
        Hum the thing is getting old but by the way I've to need the right to access it:
        cpan> install DBD::Oracle CPAN::SQLite not installed, trying to work without Reading 'C:\STRAWB~1\cpan\Metadata' Database was generated on Sun, 01 Jan 2023 22:54:07 GMT Running install for module 'DBD::Oracle' Fetching with LWP: http://cpan.strawberryperl.com/authors/id/Z/ZA/ZARQUON/DBD-Oracle-1.83 +.tar.gz LWP failed with code[501] message[Can't load 'C:\xampp\perl\vendor\lib +/auto/Net/SSLeay/SSLeay.xs.dll' for module Net::SSLeay: load_file:Le +module spÚcifiÚ est introuvable (LWP::Protocol::https not installed)] Warning: no success downloading 'C:\STRAWB~1\cpan\sources\authors\id\Z +\ZA\ZARQUON\DBD-Oracle-1.83.tar.gz.tmp4192'. Giving up on it. Fetching with LWP: http://www.cpan.org/authors/id/Z/ZA/ZARQUON/DBD-Oracle-1.83.tar.gz Fetching with LWP: http://www.cpan.org/authors/id/Z/ZA/ZARQUON/CHECKSUMS Checksum for C:\STRAWB~1\cpan\sources\authors\id\Z\ZA\ZARQUON\DBD-Orac +le-1.83.tar.gz ok Scanning cache C:\STRAWB~1\cpan\build for sizes ...................................................................... +......DONE Configuring Z/ZA/ZARQUON/DBD-Oracle-1.83.tar.gz with Makefile.PL Can't locate DBI.pm in @INC (you may need to install the DBI module) ( +@INC contains: C:/xampp/perl/site/lib C:/xampp/perl/vendor/lib C:/xam +pp/perl/lib .) at Makefile.PL line 20. BEGIN failed--compilation aborted at Makefile.PL line 20. Warning: No success on command[C:\xampp\perl\bin\perl.exe Makefile.PL] ZARQUON/DBD-Oracle-1.83.tar.gz C:\xampp\perl\bin\perl.exe Makefile.PL -- NOT OK Stopping: 'install' failed for 'DBD::Oracle'. Failed during this command: ZARQUON/DBD-Oracle-1.83.tar.gz : writemakefile NO 'C:\x +ampp\perl\bin\perl.exe Makefile.PL' returned status 512 cpan>