]> git.nothing2do.fr Git - diary-shell.git/commitdiff
I try to make diary get my mail... (or anything else on web)
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Mon, 29 Jul 2013 14:49:35 +0000 (16:49 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Mon, 29 Jul 2013 14:49:35 +0000 (16:49 +0200)
alias.sql
howto-compile.sh
main.cpp
sqldb.cpp
sqldb.h
tables1.sql

index b15d644aa42125c8610dc0abb4435b9908bdf649..577369582387add062f3575e3c193e1d473c9594 100644 (file)
--- a/alias.sql
+++ b/alias.sql
@@ -18,6 +18,7 @@ insert into alias (nom, cmd, argc, help) values ('help', 'select help from alias
 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)');
+
index fdb115c01e63a487848fd3d33da0f6f091004692..9b6cea12504a7e93c1677340c5789704b7b4690d 100755 (executable)
@@ -1,2 +1,2 @@
 rm diary-shell
-g++ main.cpp sqldb.cpp -o diary-shell -L /usr/lib -I .. -I/usr/include/postgresql -lpq -fpermissive
+g++ main.cpp sqldb.cpp -o diary-shell -L /usr/lib -I .. -I/usr/include/postgresql -lpq -fpermissive -lcrypto -lssl
index e8bc072086ab96e7ace5f7aad53b526ea140bc15..92c64b635533ec60fdcd947f222d7ec0fb84712e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -42,7 +42,8 @@ do{
     if(words.size()==0)break;//quit
     else if (words[0]=="p") diary.print();
     else if (words[0]=="status") diary.status();
-    else if (words[0]=="con") diary.printvar();
+    else if (words[0]=="pv") diary.printvar();
+    else if ((words[0]=="connect")&&(words.size()==3)) diary.connect(words[1], words[2]);
     else if (words[0]=="!") {
         string a=rep.substr(2);
         diary.exec(a);
index b69dbadd647343a69d56f10611496c16d14bd069..922bde057f1334a60d8efbc4bae3695f1a011079 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
 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));
@@ -172,6 +179,10 @@ void sqlpg::printvar(){
 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);
@@ -343,9 +354,9 @@ void sqlpg::getvar(){
     };
     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()};
@@ -354,13 +365,27 @@ void sqlpg::connect(string service, string name){
     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;
+
 
 
 }
diff --git a/sqldb.h b/sqldb.h
index 2783691cf59c2adb8c88bc3e39d228c18183b009..1e712579702aee84ed38465292b1a5802991d3a1 100644 (file)
--- a/sqldb.h
+++ b/sqldb.h
@@ -7,6 +7,11 @@
 #include <vector>
 #include <cstdio>
 #include <map>
+#include <openssl/conf.h>
+#include <openssl/evp.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#include <openssl/bio.h>
 
 int isdate(std::string);//return 1 for YYYY-MM-DD and 2 for HH:MM:SS
 
index 721369f25ab2a1a3c0e481500f3354e6e9925ba5..af9f26014c4614cc081c0cd09665f6f5c3d9afef 100755 (executable)
@@ -45,3 +45,12 @@ username text,
 password text,
 constraint uc_credentialID UNIQUE (service, username)
 );
+
+create table if not exists service (
+id serial primary key,
+name text,
+proto text,
+port integer,
+server text,
+cmd text,
+);