From 2a420034bbce946e9e135c6048bafb545acdb69f Mon Sep 17 00:00:00 2001 From: Norbert Moutarde Date: Thu, 8 Aug 2013 13:48:22 +0200 Subject: [PATCH] I've added the ability to connect to a SSL server --- SSL.cpp | 79 +++++++++++++++++++++++++++++++++ SSL.hpp | 31 +++++++++++++ howto-compile-alwaysdata.com.sh | 2 +- howto-compile.sh | 2 +- main.cpp | 8 ++-- service.sql | 1 + sqldb.cpp | 28 +----------- 7 files changed, 119 insertions(+), 32 deletions(-) create mode 100644 SSL.cpp create mode 100644 SSL.hpp create mode 100644 service.sql diff --git a/SSL.cpp b/SSL.cpp new file mode 100644 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< +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +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(); +}; diff --git a/howto-compile-alwaysdata.com.sh b/howto-compile-alwaysdata.com.sh index 9943a4a..fdf7f97 100755 --- a/howto-compile-alwaysdata.com.sh +++ b/howto-compile-alwaysdata.com.sh @@ -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 diff --git a/howto-compile.sh b/howto-compile.sh index 9b6cea1..fc42958 100755 --- a/howto-compile.sh +++ b/howto-compile.sh @@ -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 diff --git a/main.cpp b/main.cpp index 92c64b6..1974daf 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include "sqldb.h" #include #include +#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="<> 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 index 0000000..f8867ad --- /dev/null +++ b/service.sql @@ -0,0 +1 @@ +insert into service (name, proto, port, server, cmd) values ('gmail', 'https', 443, 'www.gmail.com', '...'); diff --git a/sqldb.cpp b/sqldb.cpp index b4bde20..2b9b93b 100644 --- a/sqldb.cpp +++ b/sqldb.cpp @@ -11,18 +11,13 @@ 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="<