NAME
DBIx::PasswordIniFile - Create DBI connections with password and other params stored in .ini files.
SYNOPSIS
use DBIx::PasswordIniFile;
$conn = DBIx::PasswordIniFile->new(
-file => 'path/to/file.ini',
-section => 'db_config_section',
-key => 'encrypt_decrypt_key',
-cipher => 'name_of_encryption_module'
);
$conn->connect( \%attributes ); # or
$conn->connect();
$conn->connectCached( \%attributes ); # or
$conn->connectCached();
$encrypted_passw = $conn->changePassword('new_password');
$conn = DBIx::PasswordIniFile->getCachedConnection( 'path/to/file.ini' );
$hash_ref = DBIx::PasswordIniFile->getCache();
$dbh = $conn->dbh();
DESCRIPTION
Lets you create a DBI connection with parameters stored in a .ini style file. The password is stored encrypted.
This module is similar to DBIx::Password. The differences are that DBI connection parameters aren't stored as part of the module source code (but in an external .ini style file), and that this module lets you only one virtual user (i.e. one connection) per .ini file.
Like <DBIx::Password>, this is a subclass of DBI, so you may call DBI function objects using DBIx::PasswordIniFile objects.
FUNCTIONS
Note:
[ and ] around words in syntax of functions below mean optional, and not array reference.
$conn = DBIx::PasswordIniFile->new( -file=>'path/to/file.ini' [, ... ])
Creates a DBIx::PasswordIniFile object from DBI connection parameters specified in path/to/file.ini file.
Apart from -file, other (optional) arguments are:
-section => 'db_config_section'-
If specified,
db_config_sectionis the section of the.inifile where DBI connection parameters live. If not specified, assumes that DBI connection parameters are in a section with one of these names:dsn connect connection database db virtual userAlso, if attributes have to be specified, specify them as properties of another section with same name and
_attributesat the end. For example, if your.inifile has aconnectsection, connection attributes (if specified) are assumed to be inconnection_attributessection. If has avirtual usersection, attributes are assumed to be invirtual user_attributes, and so on.Note:
Connection attributes are those you specify as last argument of DBI
connectmethod. See DBI for more details.Allowed properties as DBI connection parameters are:
driver database host port username password dsnIf
driver=ODBCthendsn,usernameandpasswordare mandatory, and all other parameters are ignored. Ifdriverisn't ODBC, then all parameters exceptdatabase,usernameandpasswordare optional.Properties/Values in
..._attributessection aren't predefined and are used as key/value pairs for\%attrargument when DBIconnectmethod is called.All propertie values are stored as plain text in
.inifile, exceptpasswordvalue, that is stored encrypted using an encription algorithm (default is Blowfish_PP).Below is an example of
.inifile content:[db_config_section] driver=mysql database=suppliers host=www.freesql.org port=3306 username=ecastilla password=52616e646f6d495621c18a03330fee46600ace348beeec28 [db_config_section_attributes] PrintError=1 RaiseError=0 AutoCommit=1This is an example owith ODBC:
[db_config_section] driver=ODBC dsn=FreeSQLOther sections and properties of the
.inifile are ignored, and do not cause any undesired effect. This lets you use non dedicated.inifiles for storing DBI connection parameters.The specified
.inifile must be a compatibleConfig::IniFilesconfig file (with default syntax, see Config::IniFiles for detailed syntax). Briefly:Lines beginning with
#are comments and are ignored. Also, blank lines are ignored. Use this for readability purposes.A section name is a string (including whitespaces) between
[and].Each section has one or more property/value pairs. Each property/value pair is specified with the syntax
property=valueOne property/value pair per line.
-key => 'encrypt_decrypt_key'and-cipher => 'name_of_encryption_module'-
If specified,
-keyand-cipherare the encryption/decription key used for storing/eading the password in.inifile and the cipher algoritm (default is 'Blowfish_PP'). Note that at least one encription algorithm have to be installed (they live inCrypt::spacename).
Once a DBIx::PasswordIniFile object is created, use it to call DBI object methods. For example:
use DBIx::PasswordIniFile;
$conn = new DBIx::PasswordIniFile( -file => 'my.ini');
$conn->connect();
...
$conn->disconnect(); # DBI object method.
$conn->connect( [\%attributes] )
Calls DBI->connect with values stored in .ini file specified in new. \%attributes refers to last parameter of DBI->connect.
If specified, \%attributes take precedence over any conflicting stored in ..._attributes section of .ini file.
$conn->connectCached( [\%attributes] )
Same as connect, but caches a copy of $conn object.
Cached objects may be retrieved with getCachedConnection.
$encrypted_passw = $conn->changePassword('new_password')
Replaces the encrypted password stored in .ini file with the result of encrypting new_password password (so, new_password is the new password in clear form).
Returns the new encrypted password saved in .ini file.
$conn = DBIx::PasswordIniFile->getCachedConnection( 'path/to/file.ini' )
Returns a valid DBIx::PasswordIniFile object corresponding to the .ini file argument, if its connectCached was launched. Or returns undef if argument doesn't correspond to a cached connection.
$cache = DBIx::PasswordIniFile->getCache()
Return a hash reference that is the cache. Keys are object references converted to strings and values are valid DBIx::PasswordIniFile objects.
$dbh = $conn->dbh()
Returns the DBI database handler object (a DBIx::PasswordIniFile object is a composition of a DBI object among others).
SECURITY CONSIDERATIONS
In .ini file, password is stored encrypted, and never in clear form. But note that the mechanism is not completely secured because passwords are stored clear in memory. A hack may do a memory dump and see the password.
Although with this limitation, I think the module is a good balance between security and simplicity.
REQUISITES
Perl v5.8.6 or above has to be installed. If not, an error
Free to wrong pool XXX not YYY during global destruction
is displayed, and Perl crashes.
An encription module has to be installed. Default is to use Crypt::Blowfish_PP for encription and decription. If not installed, specify your preferred (without Crypt:: prefix).
SEE ALSO
There is an utility called encryptpassw.pl that takes a .ini file and replaces the password param value with its encrypted form.
DBI, Config::IniFiles, DBIx::Password.
COPYRIGHT
Copyright 2005-2008 Enrique Castilla.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
AUTHOR
Enrique Castilla <mailto:ecastillacontreras@yahoo.es|ecastillacontreras@yahoo.es>.