From dfa8d9fc3caad3568256c404a0b1c2c0d2c526d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Sun, 20 Jan 2008 22:27:33 +0000 Subject: [PATCH] Mise a jout de sll --- slc/trunk/src/sll/cypher.cc | 96 +++++++++++++++++++++++++++++++++ slc/trunk/src/sll/cypher.hh | 33 ++++++++++++ slc/trunk/src/sll/slm.hh | 1 + sls/trunk/src/client.cc | 2 +- sls/trunk/src/client.hh | 1 + sls/trunk/src/client_console.cc | 7 +++ sls/trunk/src/client_console.hh | 1 + sls/trunk/src/client_daemon.cc | 22 ++++++-- sls/trunk/src/client_daemon.hh | 3 ++ sls/trunk/src/sll/cypher.cc | 96 +++++++++++++++++++++++++++++++++ sls/trunk/src/sll/cypher.hh | 33 ++++++++++++ sls/trunk/src/sll/slm.hh | 1 + 12 files changed, 292 insertions(+), 4 deletions(-) create mode 100644 slc/trunk/src/sll/cypher.cc create mode 100644 slc/trunk/src/sll/cypher.hh create mode 100644 sls/trunk/src/sll/cypher.cc create mode 100644 sls/trunk/src/sll/cypher.hh diff --git a/slc/trunk/src/sll/cypher.cc b/slc/trunk/src/sll/cypher.cc new file mode 100644 index 0000000..0148594 --- /dev/null +++ b/slc/trunk/src/sll/cypher.cc @@ -0,0 +1,96 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + SLL is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with SLL; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "slm.hh" +#include "cypher.hh" +#include "error.hh" + +#include +#include +#include +#include +#include + +/** + * return sha1 of data with base64 encoding + * + * @param data data to sha1 and base64 + * @param size size of data + * + * @return malloc'ed sha1 size + */ +string Cypher::sha1_64(const char *data, size_t size) { + unsigned char md[SHA_DIGEST_LENGTH]; + BIO *bmem, *b64; + BUF_MEM *bptr; + string ret; + + // compute sha1 + SHA1((const unsigned char *) data, size, md); + + // compute b64 + b64 = BIO_new(BIO_f_base64()); + bmem = BIO_new(BIO_s_mem()); + b64 = BIO_push(b64, bmem); + BIO_write(b64, md, SHA_DIGEST_LENGTH); + BIO_flush(b64); + BIO_get_mem_ptr(b64, &bptr); + + if (bptr->length > 0) + ret.insert(0, bptr->data, bptr->length); + else + throw Error(ERR_CYPHER, "Unable to compute sha1_64"); + + BIO_free_all(b64); + + return ret; +} + +/** + * return md5 hexa digest of file @param file. + * + * @return malloc'ed md5 digest in hexadecimal + */ +string Cypher::md5_16(const string &file) { + MD5_CTX ctx; + FILE *fs; + size_t len; + char buf[512]; + char md[MD5_DIGEST_LENGTH]; + char digest[MD5_DIGEST_LENGTH * 2 + 1]; + + if (!MD5_Init(&ctx)) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + if ((fs = fopen(file.c_str(), "r")) == 0) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + while ((len = fread(buf, 1, 512, fs)) > 0) + if (!MD5_Update(&ctx, buf, len)) + break; + + if (!MD5_Final((unsigned char*)md, &ctx)) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + for(len = 0; len < MD5_DIGEST_LENGTH; ++len) + sprintf(digest + (len * 2), "%02x", (unsigned char) md[len]); + + digest[MD5_DIGEST_LENGTH * 2] = 0; + + return digest; +} diff --git a/slc/trunk/src/sll/cypher.hh b/slc/trunk/src/sll/cypher.hh new file mode 100644 index 0000000..2018228 --- /dev/null +++ b/slc/trunk/src/sll/cypher.hh @@ -0,0 +1,33 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + SLL is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with SLL; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef CYPHER_HH +# define CYPHER_HH + +# include + +class Cypher { +public: + // md5 functions + static string md5_16(const string &file); + + // sha1 functions + static string sha1_64(const char *, size_t); +}; + +#endif diff --git a/slc/trunk/src/sll/slm.hh b/slc/trunk/src/sll/slm.hh index e04a2fd..8c6f809 100644 --- a/slc/trunk/src/sll/slm.hh +++ b/slc/trunk/src/sll/slm.hh @@ -47,6 +47,7 @@ enum { ERR_PARSE = 10, ERR_SCREENSZ = 11, ERR_SIGNAL = 12, + ERR_CYPHER = 13, ERR_NOMEM = 41, ERR_UNKNOWN = 42 }; diff --git a/sls/trunk/src/client.cc b/sls/trunk/src/client.cc index 00be2c5..18f8184 100644 --- a/sls/trunk/src/client.cc +++ b/sls/trunk/src/client.cc @@ -59,7 +59,7 @@ Client *Client::login(Connection &c) { } // Print succefful login - std::cout << "CId " << c.getid() << ": Host " << loginbuf + std::cout << "CId " << c.getid() << ": " << real_client->type() << " " << loginbuf << " logged from " << c.getremoteip() << " on port " << c.getremoteport() << ".\n"; diff --git a/sls/trunk/src/client.hh b/sls/trunk/src/client.hh index 1265b19..beb71a3 100644 --- a/sls/trunk/src/client.hh +++ b/sls/trunk/src/client.hh @@ -36,6 +36,7 @@ protected: Client(Connection &c); virtual bool trust(const char *login, const char *pass) = 0; + virtual string type() = 0; protected: Connection c_; string login_; diff --git a/sls/trunk/src/client_console.cc b/sls/trunk/src/client_console.cc index 782b8ae..646f120 100644 --- a/sls/trunk/src/client_console.cc +++ b/sls/trunk/src/client_console.cc @@ -86,6 +86,13 @@ bool Console::trust(const char *login, const char *pass) { return true; } +/** + * Return public type of backend + */ +string Console::type() { + return "User"; +} + /** * Parse and execute * diff --git a/sls/trunk/src/client_console.hh b/sls/trunk/src/client_console.hh index ebdbdec..e563fb0 100644 --- a/sls/trunk/src/client_console.hh +++ b/sls/trunk/src/client_console.hh @@ -32,6 +32,7 @@ protected: Console(Connection &c); virtual bool trust(const char *login, const char *pass); + virtual string type(); void parse(const string &cmd); diff --git a/sls/trunk/src/client_daemon.cc b/sls/trunk/src/client_daemon.cc index c529987..d28a5b6 100644 --- a/sls/trunk/src/client_daemon.cc +++ b/sls/trunk/src/client_daemon.cc @@ -25,6 +25,7 @@ void Daemon::exec() { // send scripts + send_script(); // starting scripts @@ -63,8 +64,23 @@ bool Daemon::trust(const char *login, const char *pass) { // register as logged in DB - // Send - c_.sendln("KO"); + // Send logging accepted + c_.sendln("OK"); + + return true; +} + +/** + * Return public type of backend + */ +string Daemon::type() { + return "Host"; +} + +/** + * Send all script to daemon + * + */ +void Daemon::send_script() { - return false; } diff --git a/sls/trunk/src/client_daemon.hh b/sls/trunk/src/client_daemon.hh index 9fd529c..5d03df4 100644 --- a/sls/trunk/src/client_daemon.hh +++ b/sls/trunk/src/client_daemon.hh @@ -33,6 +33,9 @@ protected: Daemon(Connection &c); virtual bool trust(const char *login, const char *pass); + virtual string type(); + + virtual void send_script(); }; #endif diff --git a/sls/trunk/src/sll/cypher.cc b/sls/trunk/src/sll/cypher.cc new file mode 100644 index 0000000..0148594 --- /dev/null +++ b/sls/trunk/src/sll/cypher.cc @@ -0,0 +1,96 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + SLL is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with SLL; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "slm.hh" +#include "cypher.hh" +#include "error.hh" + +#include +#include +#include +#include +#include + +/** + * return sha1 of data with base64 encoding + * + * @param data data to sha1 and base64 + * @param size size of data + * + * @return malloc'ed sha1 size + */ +string Cypher::sha1_64(const char *data, size_t size) { + unsigned char md[SHA_DIGEST_LENGTH]; + BIO *bmem, *b64; + BUF_MEM *bptr; + string ret; + + // compute sha1 + SHA1((const unsigned char *) data, size, md); + + // compute b64 + b64 = BIO_new(BIO_f_base64()); + bmem = BIO_new(BIO_s_mem()); + b64 = BIO_push(b64, bmem); + BIO_write(b64, md, SHA_DIGEST_LENGTH); + BIO_flush(b64); + BIO_get_mem_ptr(b64, &bptr); + + if (bptr->length > 0) + ret.insert(0, bptr->data, bptr->length); + else + throw Error(ERR_CYPHER, "Unable to compute sha1_64"); + + BIO_free_all(b64); + + return ret; +} + +/** + * return md5 hexa digest of file @param file. + * + * @return malloc'ed md5 digest in hexadecimal + */ +string Cypher::md5_16(const string &file) { + MD5_CTX ctx; + FILE *fs; + size_t len; + char buf[512]; + char md[MD5_DIGEST_LENGTH]; + char digest[MD5_DIGEST_LENGTH * 2 + 1]; + + if (!MD5_Init(&ctx)) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + if ((fs = fopen(file.c_str(), "r")) == 0) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + while ((len = fread(buf, 1, 512, fs)) > 0) + if (!MD5_Update(&ctx, buf, len)) + break; + + if (!MD5_Final((unsigned char*)md, &ctx)) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + for(len = 0; len < MD5_DIGEST_LENGTH; ++len) + sprintf(digest + (len * 2), "%02x", (unsigned char) md[len]); + + digest[MD5_DIGEST_LENGTH * 2] = 0; + + return digest; +} diff --git a/sls/trunk/src/sll/cypher.hh b/sls/trunk/src/sll/cypher.hh new file mode 100644 index 0000000..2018228 --- /dev/null +++ b/sls/trunk/src/sll/cypher.hh @@ -0,0 +1,33 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + SLL is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with SLL; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef CYPHER_HH +# define CYPHER_HH + +# include + +class Cypher { +public: + // md5 functions + static string md5_16(const string &file); + + // sha1 functions + static string sha1_64(const char *, size_t); +}; + +#endif diff --git a/sls/trunk/src/sll/slm.hh b/sls/trunk/src/sll/slm.hh index e04a2fd..8c6f809 100644 --- a/sls/trunk/src/sll/slm.hh +++ b/sls/trunk/src/sll/slm.hh @@ -47,6 +47,7 @@ enum { ERR_PARSE = 10, ERR_SCREENSZ = 11, ERR_SIGNAL = 12, + ERR_CYPHER = 13, ERR_NOMEM = 41, ERR_UNKNOWN = 42 }; -- GitLab