--- /dev/null
+#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;
+}
--- /dev/null
+#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();
+};
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
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
#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"
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";
//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();
--- /dev/null
+insert into service (name, proto, port, server, cmd) values ('gmail', 'https', 443, 'www.gmail.com', '...');
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));
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);
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