b41943249f518af498b8e4957bcd73d206a241bf
[mnenc] / mnencd.cpp
1 /*
2 Copyright (c) 2004, Devin Watson <dmwatson@comcast.net> 
3 and 2010 Micke Nordin <mickewiki@gmail.com>
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of mnenc nor the
14       names of its contributors may be used to endorse or promote products
15       derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Devin Watson BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * */
28
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <syslog.h>
34 #include <fcntl.h>
35 #include <cstdio>
36 #include <cstdlib>
37 #include <string>
38 #include <cerrno>
39 #include <fstream>
40 #include <iostream>
41 #include "mnenc.hpp"
42 #include "password.hpp"
43
44 std::string remove_char(std::string str, char c) {
45         std::string::size_type k = 0;
46         while((k=str.find(c,k))!=str.npos) {
47                 str.erase(k, 1);
48         }
49         return str;
50 }
51
52 std::string remove_chars(std::string str) {
53         std::string chars = " \t\n\b\a-?+\\{[]}'*'";
54         for(int i = 0; i < (signed) chars.size(); i++) {
55                 str = remove_char(str, chars[i]);
56         }
57         
58         return str;     
59 }
60
61 std::string make_filename(std::string user, std::string app) {
62         return remove_chars(app + user);
63 }
64 std::string get_password(std::string masterpasswd, std::string user, std::string app) {
65         mnenc menc = mnenc();
66         menc.genkey(masterpasswd);
67         std::string key = menc.get_key();
68         password pw = password("", "", key);
69         pw.from_file(make_filename(user, app));
70         return menc.decrypt(key, pw.get_enc());
71 }
72
73 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
74         mnenc menc = mnenc();
75         menc.genkey(masterpasswd);
76         std::string key = menc.get_key();
77         password pw = password(menc.encrypt(key, passwd ), "", key);
78         pw.to_file(make_filename(user, app));
79 }
80
81 std::string do_something(std::string str) {
82         //Nothing here yet
83         return "Blahonga";
84 }
85
86 std::string m_read() {
87         std::string str;
88         std::ifstream is("fife");
89         getline(is, str);
90         is.close();
91         return str;
92 }
93
94 void m_send(std::string message) {
95         std::ofstream os("fife");
96         os << message;
97         os.close();
98 }
99
100
101 int main(int argc, char** argv) {
102         /* Our process ID and Session ID */
103         pid_t pid, sid;
104         
105         /* Fork off the parent process */
106         pid = fork();
107         if (pid < 0) {
108                 exit(EXIT_FAILURE);
109         }
110         /* If we got a good PID, then
111            we can exit the parent process. */
112         if (pid > 0) {
113                 exit(EXIT_SUCCESS);
114         }
115
116         /* Change the file mode mask */
117         umask(0);
118                 
119         /* Open any logs here */        
120                 
121         /* Create a new SID for the child process */
122         sid = setsid();
123         if (sid < 0) {
124                 /* Log the failure */
125                 exit(EXIT_FAILURE);
126         }
127         
128         /* Change the current working directory */
129         if ((chdir("/home/micke/.fifo")) < 0) {
130                 /* Log the failure */
131                 exit(EXIT_FAILURE);
132         }
133         
134         /* Close out the standard file descriptors */
135         close(STDIN_FILENO);
136         close(STDOUT_FILENO);
137         close(STDERR_FILENO);
138         
139         /* Daemon-specific initialization goes here */
140         
141         mkfifo("./fife", 0777);
142
143         /* The Big Loop */
144         while (1) {
145                 m_send(do_something(m_read()));
146                 sleep(1); /* wait 1 second*/
147         }
148         unlink("fife");
149         exit(EXIT_SUCCESS);
150 }