insert into alias (nom, cmd, argc, help) values ('print', 'select cmd,inf from alias where nom=$1 and argc=$2;', 2, 'print SQL command of an alias (...)');
insert into alias (nom, cmd, argc, inf, help) values ('default', 'INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2::text);', 2 , '1', 'default command');
insert into alias (nom, cmd, argc, inf, help) values ('aliasadd', 'insert into alias (nom, argc, cmd) values ( $1, $2, $3 );', 3, '1', 'add alias (with form : aliasadd "name" "argc" "SQL")');
-insert into alias (nom, cmd, argc, help) values ('getpass', 'select username,password from credential where service=$1;', 1, 'command to retrieve username and password with a service name (perhaps more than one)');
-insert into alias (nom, cmd, argc, help) values ('getpass', 'select distinct service from credential;', 0, 'get a list of service you have credential for');
+insert into alias (nom, cmd, argc, help) values ('get', 'select service,password from credential where username=$1;', 1, 'command to retrieve service and password with a user name (perhaps more than one)');
+insert into alias (nom, cmd, argc, help) values ('get', 'select distinct service from credential;', 0, 'get a list of service you have credential for');
insert into alias (nom, cmd, argc, help) values ('getpass', 'select password from credential where service=$1 and username=$2;', 2, 'retrieve one saved password (as service and user has unique constraint)');
+
using namespace std;
sqlpg::sqlpg() {
+ /* Load the human readable error strings for libcrypto */
+ ERR_load_crypto_strings();
+ /* Load all digest and cipher algorithms */
+ OpenSSL_add_all_algorithms();
+ /* Load config file, and other important initialisation */
+ OPENSSL_config(NULL);
+ EVP_PKEY *EVP_PKEY_new(void);
- //connect = "host=postgresql1.alwaysdata.com port=5432 dbname=nothing2do.eu_diary user=nothing2do.eu password=x connect_timeout=10";
+ //strconnect = "host=postgresql1.alwaysdata.com port=5432 dbname=nothing2do.eu_diary user=nothing2do.eu password=x connect_timeout=10";
strconnect = "dbname=bob user=bob password=pass connect_timeout=10";
//conn = PQconnectStart(var.c_str());
conn = PQconnectdb(strconnect.c_str());
clog<<"prepare SQL statement\n";
string cmd="select cmd,argc,inf from alias where ((nom = $1) and (argc = $2)) OR ((nom = $1) and (inf<>0) and (argc < $2));";
clog<<"SQL selectalias="<<cmd<<endl;
- res=PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL);
- testSQL(res);
+ testSQL(PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL));
+
}
int sqlpg::status(){
string err=toString(PQstatus(conn));
sqlpg::~sqlpg() {
PQclear(res);
PQfinish(conn);
+ /* Removes all digests and ciphers */
+ EVP_cleanup();
+ /* Remove error strings */
+ ERR_free_strings();
}
void sqlpg::print(){
int l=PQntuples(res);
};
clog<<"value.size="<<value.size()<<endl;
};
-void sqlpg::connect(string service, string name){
+void sqlpg::connect(string service, string name){//I mean "username" but i'm scared to user "username" cause pgsql interpret it ...
clog<<"connect start here\n";
- string nom="connect", as=toString(2), i;
+ string nom="getpass", as=toString(2), i;
int argca=2;//it won't work if I use argca in place of 2 :-(
bool inf=0;
const char *valuea[2]={nom.c_str(), as.c_str()};
res=PQexecPrepared(conn, "selectalias", argca, valuea, lengtha, binarya, 0);
testSQL(res);
if (PQntuples(res)!=1){
- cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
+ cerr<<"getpass ! (pqntuples(res)="<<PQntuples(res)<<")\n";
return;
}
string cmd2;
cmd2=PQgetvalue(res, 0, 0);
- clog<<"alias()->commande : "<<cmd2<<endl;
-//res=PQprepare(conn, "pass", )
+ clog<<"connect()|getpass->commande : "<<cmd2<<endl;
+ testSQL(PQprepare(conn, "getpass", cmd2.c_str(), 2, NULL));
+ const char * value[2];
+ int length[2];
+ int binary[2];
+ value[0]=service.c_str();
+ length[0]=service.size();
+ binary[0]=0;
+ value[1]=name.c_str();
+ length[1]=name.size();
+ binary[1]=0;
+ res=PQexecPrepared(conn, "getpass", 2, value, length, binary, 0);
+ testSQL(res);
+ string pass=PQgetvalue(res, 0, PQfnumber(res, "password"));
+ clog<<"service="<<service<<" user="<<name<<" pass="<<pass<<endl;
+
}