Modified ui
[mnenc] / mnencd.cpp
index 52cdace..ddc89e2 100644 (file)
@@ -1,22 +1,32 @@
-//      mdaemon.cpp
-//      
-//      Copyright 2010 Micke Nordin <micke@hal>
-//      
-//      This program 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; either version 2 of the License, or
-//      (at your option) any later version.
-//      
-//      This program 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 this program; if not, write to the Free Software
-//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-//      MA 02110-1301, USA.
+/*
+Copyright (c) 2004, Devin Watson <dmwatson@comcast.net> 
+and 2010 Micke Nordin <mickewiki@gmail.com>
+All rights reserved.
 
 
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+    * Neither the name of mnenc nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL Devin Watson BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+* */
+
+#include <dirent.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <iostream>
 #include "mnenc.hpp"
 #include "password.hpp"
 #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 */
 
 int main(int argc, char** argv) {
        /* Our process ID and Session ID */
@@ -107,19 +61,34 @@ int main(int argc, char** argv) {
        umask(0);
                
        /* Open any logs here */        
        umask(0);
                
        /* Open any logs here */        
-               
+       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);
+               }
+       }
+       /*This is log stuff */
+       std::string logname = "/home/" + name + "/.mnenc/log";
+       std::ofstream log; //Log stream
+       log.open(logname.c_str(), ios::app); // open log
+       time_t rawtime; //Time stuff here
+       struct tm * timeinfo;
+       time ( &rawtime );
+       timeinfo = localtime ( &rawtime );
+       std::string current_time = asctime (timeinfo); //Holds current time
+       
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
-               /* Log the failure */
+               log << current_time << ": Unable to create a new SID for the child process\n";
                exit(EXIT_FAILURE);
        }
        
                exit(EXIT_FAILURE);
        }
        
-
-       
        /* Change the current working directory */
        /* Change the current working directory */
-       if ((chdir("/home/micke/.fifo")) < 0) {
-               /* Log the failure */
+       if ((chdir("/tmp/")) < 0) {
+               log << current_time << ": Could not change to /tmp\n";
                exit(EXIT_FAILURE);
        }
        
                exit(EXIT_FAILURE);
        }
        
@@ -129,14 +98,16 @@ int main(int argc, char** argv) {
        close(STDERR_FILENO);
        
        /* Daemon-specific initialization goes here */
        close(STDERR_FILENO);
        
        /* Daemon-specific initialization goes here */
+       mkfifo("./mnencdfifo", 0777); //This is the fifo for communications
+       signal(SIGTERM, term); // register a SIGTERM handler
+       log.close(); //Closing log 
        
        
-       mkfifo("./fife", 0777);
-
        /* The Big Loop */
        while (1) {
        /* 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*/
        }
                sleep(1); /* wait 1 second*/
        }
-       unlink("fife");
        exit(EXIT_SUCCESS);
 }
        exit(EXIT_SUCCESS);
 }