2573b4702d0a30da8bd87edaff6cd5f055909d8e
[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                 
65         /* Create a new SID for the child process */
66         sid = setsid();
67         if (sid < 0) {
68                 /* Log the failure */
69                 exit(EXIT_FAILURE);
70         }
71         std::string name, dirname;
72         name = getenv("USER");
73         dirname = "/home/" + name + "/.mnenc/";
74         if(mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO ) < 0) {
75                 if(errno != EEXIST) {
76                         exit(EXIT_FAILURE);
77                 }
78         }
79                 
80         /* Change the current working directory */
81         if ((chdir("/tmp/")) < 0) {
82                 /* Log the failure */
83                 exit(EXIT_FAILURE);
84         }
85         
86         /* Close out the standard file descriptors */
87         close(STDIN_FILENO);
88         close(STDOUT_FILENO);
89         close(STDERR_FILENO);
90         
91         /* Daemon-specific initialization goes here */
92
93         mkfifo("./mnencdfifo", 0777);
94         signal(SIGTERM, term); // register a SIGTERM handler
95         //raise(SIGTERM); // will cause term() to run
96         
97         /* The Big Loop */
98         while (1) {
99                 std::string request = m_read();
100                 std::string reply = do_something(request);
101                 m_send(reply);
102                 sleep(1); /* wait 1 second*/
103         }
104         exit(EXIT_SUCCESS);
105 }