3 // Copyright 2010 Micke Nordin <mickewiki@gmail.com>
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 3 of the License, or
8 // (at your option) any later version.
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.
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 #ifndef _mnencd_h_included_
20 #define _mnencd_h_included_
28 std::string masterpasswd = "";
30 std::string remove_char(std::string str, char c) {
31 std::string::size_type k = 0;
32 while((k=str.find(c,k))!=str.npos) {
38 std::string remove_chars(std::string str) {
39 std::string chars = " \t\n\b\a-?+\\{[]}'*'";
40 for(int i = 0; i < (signed) chars.size(); i++) {
41 str = remove_char(str, chars[i]);
47 std::string make_filename(std::string user, std::string app) {
49 name = getenv("USER");
50 return "/home/" + name + "/.mnenc/" + remove_chars(app + user);
52 std::string get_password(std::string user, std::string app) {
55 if(menc.from_file(make_filename(user, app))) {
63 void put_password(std::string passwd, std::string user, std::string app) {
64 mnenc micke; //Declare a encryptor/decryptor
65 micke.encrypt(passwd); //Encrypt the password Blahonga
66 micke.to_file(make_filename(user, app)); //Save to file
70 std::string open_connection(std::string str) {
75 std::string do_something(std::string str) { //Handle requests for secure pipe
77 std::string reply = "";
78 if( proc == 0 ) { // child
79 reply = open_connection( str );
80 } else if( proc > 0 ) { // parent
81 } else { // fork failed
87 std::string do_something_else(std::string str) { //Handle requests
89 std::vector<std::string> request; //Incomming message stored here
90 request = explode(str, "|"); //explode request with function from php.hpp
92 if(request[0] == "0_Unlock") { //Unlock keyring
93 if(masterpasswd == "") { //If master password is not yet set
94 masterpasswd = request[1]; //set password
95 return "201_Created\n";
96 } else if(masterpasswd!= "") { //If master password is allready set
97 return "403_Forbidden\n";
98 } else { //If something else is wrong
99 return "400_Bad_Request " + str + '\n';
103 else if(request[0] == "10_Encrypt") { //encrypt password
104 if(masterpasswd == "") { //If master password is not yet set
105 return "412_Precondition_Failed\n";
106 } else { //If we have a master password to encrypt with
107 put_password(request[3], request[2], request[1]);
108 return "201_Created\n";
111 }else if(request[0] == "20_Decrypt") { //decrypt password
112 if(masterpasswd == "") { //If master password is not yet set
113 return "412_Precondition_Failed\n";
114 } else { //If we have a master password to encrypt with
115 std::string pw = get_password(request[2], request[1]);
118 message = "500_Internal_Server_Error\n";
120 message = "201_Created " + pw + "\n";
124 }else if(request[0] == "30_Check") { //check if password file exists
127 file.open(make_filename(request[2], request[1]).c_str(), ios::in);
128 if( file.is_open() ) {
132 if(masterpasswd == "") { //If master password is not yet set
133 return "412_Precondition_Failed\n";
136 return "201_Created\n";
138 return "404_Not_Found";
143 return "400_Bad_Request " + str + '\n';
147 std::string m_read() {
149 std::ifstream is("mnencdfifo");
155 void m_send(std::string message) {
156 std::ofstream os("mnencdfifo");
163 unlink("/tmp/mnencdfifo");