Implemented most of the protocol
[mnenc] / mnencd.cpp
index b419432..2573b47 100644 (file)
@@ -26,7 +26,7 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * */
 
-
+#include <dirent.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -40,63 +40,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <iostream>
 #include "mnenc.hpp"
 #include "password.hpp"
-
-std::string remove_char(std::string str, char c) {
-       std::string::size_type k = 0;
-       while((k=str.find(c,k))!=str.npos) {
-               str.erase(k, 1);
-       }
-       return str;
-}
-
-std::string remove_chars(std::string str) {
-       std::string chars = " \t\n\b\a-?+\\{[]}'*'";
-       for(int i = 0; i < (signed) chars.size(); i++) {
-               str = remove_char(str, chars[i]);
-       }
-       
-       return str;     
-}
-
-std::string make_filename(std::string user, std::string app) {
-       return remove_chars(app + user);
-}
-std::string get_password(std::string masterpasswd, std::string user, std::string app) {
-       mnenc menc = mnenc();
-       menc.genkey(masterpasswd);
-       std::string key = menc.get_key();
-       password pw = password("", "", key);
-       pw.from_file(make_filename(user, app));
-       return menc.decrypt(key, pw.get_enc());
-}
-
-void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
-       mnenc menc = mnenc();
-       menc.genkey(masterpasswd);
-       std::string key = menc.get_key();
-       password pw = password(menc.encrypt(key, passwd ), "", key);
-       pw.to_file(make_filename(user, app));
-}
-
-std::string do_something(std::string str) {
-       //Nothing here yet
-       return "Blahonga";
-}
-
-std::string m_read() {
-       std::string str;
-       std::ifstream is("fife");
-       getline(is, str);
-       is.close();
-       return str;
-}
-
-void m_send(std::string message) {
-       std::ofstream os("fife");
-       os << message;
-       os.close();
-}
-
+#include "mnencd.hpp"
 
 int main(int argc, char** argv) {
        /* Our process ID and Session ID */
@@ -124,9 +68,17 @@ int main(int argc, char** argv) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }
-       
+       std::string name, dirname;
+       name = getenv("USER");
+       dirname = "/home/" + name + "/.mnenc/";
+       if(mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO ) < 0) {
+               if(errno != EEXIST) {
+                       exit(EXIT_FAILURE);
+               }
+       }
+               
        /* Change the current working directory */
-       if ((chdir("/home/micke/.fifo")) < 0) {
+       if ((chdir("/tmp/")) < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }
@@ -137,14 +89,17 @@ int main(int argc, char** argv) {
        close(STDERR_FILENO);
        
        /* Daemon-specific initialization goes here */
-       
-       mkfifo("./fife", 0777);
 
+       mkfifo("./mnencdfifo", 0777);
+       signal(SIGTERM, term); // register a SIGTERM handler
+       //raise(SIGTERM); // will cause term() to run
+       
        /* The Big Loop */
        while (1) {
-               m_send(do_something(m_read()));
+               std::string request = m_read();
+               std::string reply = do_something(request);
+               m_send(reply);
                sleep(1); /* wait 1 second*/
        }
-       unlink("fife");
        exit(EXIT_SUCCESS);
 }