]> git.nothing2do.fr Git - diary-shell.git/commitdiff
I've added the ability to connect to a SSL server
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Thu, 8 Aug 2013 11:48:22 +0000 (13:48 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Thu, 8 Aug 2013 11:48:22 +0000 (13:48 +0200)
SSL.cpp [new file with mode: 0644]
SSL.hpp [new file with mode: 0644]
howto-compile-alwaysdata.com.sh
howto-compile.sh
main.cpp
service.sql [new file with mode: 0644]
sqldb.cpp

diff --git a/SSL.cpp b/SSL.cpp
new file mode 100644 (file)
index 0000000..29fbc67
--- /dev/null
+++ b/SSL.cpp
@@ -0,0 +1,79 @@
+#include "SSL.hpp"
+using namespace std;
+
+connection::connection(string a, short unsigned int port){
+  host = gethostbyname (a.c_str());
+  handle = socket (AF_INET, SOCK_STREAM, 0);
+  if (handle == -1)
+  {
+    perror ("Create socket failed");
+    return;
+  }
+  else
+  {
+    server.sin_family = AF_INET;
+    server.sin_port = htons (port);
+    server.sin_addr = *((struct in_addr *) host->h_addr);
+    bzero (&(server.sin_zero), 8);
+    error = connect (handle, (struct sockaddr *) &server,sizeof (struct sockaddr));
+    if (error == -1)
+    {
+      perror ("Connect() failed");
+      return;
+    }
+    // SSL things
+    sslHandle = NULL;
+    sslContext = NULL;
+    // Register the error strings for libcrypto & libssl
+    SSL_load_error_strings ();
+    // Register the available ciphers and digests
+    SSL_library_init ();
+    // New context saying we are a server, and using SSL 2 or 3
+    sslContext = SSL_CTX_new (SSLv23_server_method ());
+    if (sslContext == NULL)ERR_print_errors_fp (stderr);
+    // Create an SSL struct for the connection
+    sslHandle = SSL_new (sslContext);
+    if (sslHandle == NULL)ERR_print_errors_fp (stderr);
+    // Connect the SSL struct to our connection
+    if (!SSL_set_fd (sslHandle, sock))ERR_print_errors_fp (stderr);
+    // Initiate SSL handshake
+    if (SSL_connect (sslHandle) != 1)ERR_print_errors_fp (stderr);
+    else
+    {
+      perror ("SSL_connect failed");
+    }
+  }
+}
+connection::~connection(){
+    if (sock)close(sock);
+    if (sslHandle)
+    {
+      SSL_shutdown (sslHandle);
+      SSL_free (sslHandle);
+    }
+  if (sslContext)
+    SSL_CTX_free (sslContext);
+  //free (c);
+}
+string connection::read (const int readSize){
+    //const int readSize = 1024;
+    char *rc = NULL;
+    int received, count = 0;
+    char buffer[readSize+1];
+    while (1){
+        if (!rc)rc = malloc (readSize * sizeof (char) + 1);
+        else rc = realloc (rc, (count + 1) * readSize * sizeof (char) + 1);
+        received = SSL_read ( sslHandle, buffer, readSize);
+        buffer[received] = '\0';
+        if (received > 0)strcat (rc, buffer);
+        if (received < readSize)break;
+        count++;
+        }
+    return string(rc);
+    }
+int connection::write(string a){
+    SSL_write (sslHandle, a.c_str(), a.size());
+    }
+int connection::status(){
+    cout<<handle<<endl;
+}
diff --git a/SSL.hpp b/SSL.hpp
new file mode 100644 (file)
index 0000000..78adb3c
--- /dev/null
+++ b/SSL.hpp
@@ -0,0 +1,31 @@
+#include <iostream>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <string>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <openssl/rand.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
+
+class connection {
+    SSL *sslHandle;
+    SSL_CTX *sslContext;
+    int error, handle, sock;
+    struct hostent *host;
+    struct sockaddr_in server;
+    public:
+    /*tcp(string, short unsigned int);
+    ssl(string, short unsigned int);*/
+    connection(std::string, short unsigned int);
+    ~connection();
+    std::string read(const int);
+    int write(std::string);
+    int status();
+};
index 9943a4ad6cba44a3be714511f9659743ea44f762..fdf7f9749ceeadc8a8b1245352cbc09dc0ce287e 100755 (executable)
@@ -1,2 +1,2 @@
 rm ~/diary-shell
-g++ main.cpp sqldb.cpp -o ~/diary-shell -I/usr/include/postgresql/ -I.. -lpq -fpermissive -lcrypto -lssl
+g++ main.cpp sqldb.cpp SSL.cpp -o ~/diary-shell -I/usr/include/postgresql/ -I.. -lpq -fpermissive -lcrypto -lssl
index 9b6cea12504a7e93c1677340c5789704b7b4690d..fc42958377ea5e3e08843a950b00afdf31da21bd 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 -lcrypto -lssl
+g++ main.cpp sqldb.cpp SSL.cpp -o diary-shell -L /usr/lib -I .. -I/usr/include/postgresql -lpq -fpermissive -lcrypto -lssl
index 92c64b635533ec60fdcd947f222d7ec0fb84712e..1974daf3a04eafb9e86069a8aab1ab9c878e1577 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -3,6 +3,7 @@
 #include "sqldb.h"
 #include <sstream>
 #include <cstdlib>
+#include "SSL.hpp"
 
 using namespace std;
 string date(string a)//transform any date format (in help) to "YYYY-MM-DD HH:MM:SS.XXXXXX"
@@ -19,14 +20,14 @@ int main(int argc, char * argv[])
     sqlpg diary;
     string tmp, rep, shell="? ";
     int hm;
-    bool print=false;
-    string t="p";
+    bool print=true;
     if (argc>1){
         if (argv[1]==string("p")){
-            print=true;
+            print=!print;
         }
     };
     clog<<"print="<<print<<endl;
+    //connection a;
 
 do{
     clog<<"======================again=================================\n";
@@ -40,6 +41,7 @@ do{
     //cut rep into words[0], words[1], ...
     while (ss >> buf) words.push_back(buf);
     if(words.size()==0)break;//quit
+    //else if (words[0]=="c") a.status();
     else if (words[0]=="p") diary.print();
     else if (words[0]=="status") diary.status();
     else if (words[0]=="pv") diary.printvar();
diff --git a/service.sql b/service.sql
new file mode 100644 (file)
index 0000000..f8867ad
--- /dev/null
@@ -0,0 +1 @@
+insert into service (name, proto, port, server, cmd) values ('gmail', 'https', 443, 'www.gmail.com', '...');
index b4bde2095d3648d1d48645cab3ba5056d05f504a..2b9b93b54b8ef1b474ecddb6c68916a9e7f17b30 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
 using namespace std;
 
 sqlpg::sqlpg() {
-    //src : http://www.ibm.com/developerworks/linux/library/l-openssl/index.html
-    SSL_load_error_strings();
-    ERR_load_BIO_strings();
-    OpenSSL_add_all_algorithms();
-    SSL_library_init();
 
     //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));";
+    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;
     testSQL(PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL));
 
@@ -177,10 +172,6 @@ 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);
@@ -384,22 +375,5 @@ void sqlpg::connect(string service, string name){//I mean "username" but i'm aff
     string pass=PQgetvalue(res, 0, PQfnumber(res, "password"));
     clog<<"service="<<service<<" user="<<name<<" pass="<<pass<<endl;
 
-    string str="jarvis.nothing2do.eu:22";
-    bio = BIO_new_connect(str.c_str());
-    if(bio == NULL)
-    {
-        /* Handle the failure */
-    }
-
-    if(BIO_do_connect(bio) <= 0)
-    {
-        /* Handle failed connection */
-    }
-\r
-    ERR_load_BIO_strings();\r
-    SSL_load_error_strings();\r
-    OpenSSL_add_all_algorithms();
-
-
 }
 #endif