52cdace355bb0ad4cdae047f00174135787d71ac
[mnenc] / mnencd.cpp
1 //      mdaemon.cpp
2 //      
3 //      Copyright 2010 Micke Nordin <micke@hal>
4 //      
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 2 of the License, or
8 //      (at your option) any later version.
9 //      
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //      
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program; if not, write to the Free Software
17 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 //      MA 02110-1301, USA.
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <syslog.h>
24 #include <fcntl.h>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <string>
28 #include <cerrno>
29 #include <fstream>
30 #include <iostream>
31 #include "mnenc.hpp"
32 #include "password.hpp"
33
34 std::string remove_char(std::string str, char c) {
35         std::string::size_type k = 0;
36         while((k=str.find(c,k))!=str.npos) {
37                 str.erase(k, 1);
38         }
39         return str;
40 }
41
42 std::string remove_chars(std::string str) {
43         std::string chars = " \t\n\b\a-?+\\{[]}'*'";
44         for(int i = 0; i < (signed) chars.size(); i++) {
45                 str = remove_char(str, chars[i]);
46         }
47         
48         return str;     
49 }
50
51 std::string make_filename(std::string user, std::string app) {
52         return remove_chars(app + user);
53 }
54 std::string get_password(std::string masterpasswd, std::string user, std::string app) {
55         mnenc menc = mnenc();
56         menc.genkey(masterpasswd);
57         std::string key = menc.get_key();
58         password pw = password("", "", key);
59         pw.from_file(make_filename(user, app));
60         return menc.decrypt(key, pw.get_enc());
61 }
62
63 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
64         mnenc menc = mnenc();
65         menc.genkey(masterpasswd);
66         std::string key = menc.get_key();
67         password pw = password(menc.encrypt(key, passwd ), "", key);
68         pw.to_file(make_filename(user, app));
69 }
70
71 std::string do_something(std::string str) {
72         //Nothing here yet
73         return "Blahonga";
74 }
75
76 std::string m_read() {
77         std::string str;
78         std::ifstream is("fife");
79         getline(is, str);
80         is.close();
81         return str;
82 }
83
84 void m_send(std::string message) {
85         std::ofstream os("fife");
86         os << message;
87         os.close();
88 }
89
90
91 int main(int argc, char** argv) {
92         /* Our process ID and Session ID */
93         pid_t pid, sid;
94         
95         /* Fork off the parent process */
96         pid = fork();
97         if (pid < 0) {
98                 exit(EXIT_FAILURE);
99         }
100         /* If we got a good PID, then
101            we can exit the parent process. */
102         if (pid > 0) {
103                 exit(EXIT_SUCCESS);
104         }
105
106         /* Change the file mode mask */
107         umask(0);
108                 
109         /* Open any logs here */        
110                 
111         /* Create a new SID for the child process */
112         sid = setsid();
113         if (sid < 0) {
114                 /* Log the failure */
115                 exit(EXIT_FAILURE);
116         }
117         
118
119         
120         /* Change the current working directory */
121         if ((chdir("/home/micke/.fifo")) < 0) {
122                 /* Log the failure */
123                 exit(EXIT_FAILURE);
124         }
125         
126         /* Close out the standard file descriptors */
127         close(STDIN_FILENO);
128         close(STDOUT_FILENO);
129         close(STDERR_FILENO);
130         
131         /* Daemon-specific initialization goes here */
132         
133         mkfifo("./fife", 0777);
134
135         /* The Big Loop */
136         while (1) {
137                 m_send(do_something(m_read()));
138                 sleep(1); /* wait 1 second*/
139         }
140         unlink("fife");
141         exit(EXIT_SUCCESS);
142 }