Starting to move stuff in to a lib
[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 #include <dirent.h>
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 #include "mnencd.hpp"
44
45 int main(int argc, char** argv) {
46         /* Our process ID and Session ID */
47         pid_t pid, sid;
48         
49         /* Fork off the parent process */
50         pid = fork();
51         if (pid < 0) {
52                 exit(EXIT_FAILURE);
53         }
54         /* If we got a good PID, then
55            we can exit the parent process. */
56         if (pid > 0) {
57                 exit(EXIT_SUCCESS);
58         }
59
60         /* Change the file mode mask */
61         umask(0);
62                 
63         /* Open any logs here */        
64         std::string name, dirname;
65         name = getenv("USER");
66         dirname = "/home/" + name + "/.mnenc/";
67         if(mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO ) < 0) {
68                 if(errno != EEXIST) {
69                         exit(EXIT_FAILURE);
70                 }
71         }
72         /*This is log stuff */
73         std::string logname = "/home/" + name + "/.mnenc/log";
74         std::ofstream log; //Log stream
75         log.open(logname.c_str(), ios::app); // open log
76         time_t rawtime; //Time stuff here
77         struct tm * timeinfo;
78         time ( &rawtime );
79         timeinfo = localtime ( &rawtime );
80         std::string current_time = asctime (timeinfo); //Holds current time
81         
82         /* Create a new SID for the child process */
83         sid = setsid();
84         if (sid < 0) {
85                 log << current_time << ": Unable to create a new SID for the child process\n";
86                 exit(EXIT_FAILURE);
87         }
88         
89         /* Change the current working directory */
90         if ((chdir("/tmp/")) < 0) {
91                 log << current_time << ": Could not change to /tmp\n";
92                 exit(EXIT_FAILURE);
93         }
94         
95         /* Close out the standard file descriptors */
96         close(STDIN_FILENO);
97         close(STDOUT_FILENO);
98         close(STDERR_FILENO);
99         
100         /* Daemon-specific initialization goes here */
101         mkfifo("./mnencdfifo", 0777); //This is the fifo for communications
102         signal(SIGTERM, term); // register a SIGTERM handler
103         log.close(); //Closing log 
104         
105         /* The Big Loop */
106         while (1) {
107                 std::string request = m_read();
108                 std::string reply = do_something(request);
109                 m_send(reply);
110                 sleep(1); /* wait 1 second*/
111         }
112         exit(EXIT_SUCCESS);
113 }