summaryrefslogtreecommitdiff
path: root/plugins/New_GPG/src/gpg_wrapper.cpp
blob: c82a17f09dd33f66d1e7e293e372ea3395c126f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright © 2010-24 sss
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include "stdafx.h"

namespace bp = boost::process;

gpg_execution_params::gpg_execution_params()
{ }

gpg_execution_params::~gpg_execution_params()
{ }

void pxEexcute_thread(gpg_execution_params *params)
{
	if (!globals.gpg_valid)
		return;

	CMStringW bin_path(g_plugin.getMStringW("szGpgBinPath"));
	if (_waccess(bin_path, 0)) {
		if (globals.debuglog)
			globals.debuglog << "GPG executable not found";
		params->result = pxNotFound;
		return;
	}

	bp::environment env = boost::this_process::environment();
	env.set("LANGUAGE", "en@quot");
	env.set("LC_ALL", "English");
	env.set("LANG", "C");

	std::vector<std::wstring> argv;
	CMStringW home_dir(g_plugin.getMStringW("szHomePath"));
	if (!home_dir.IsEmpty()) { // this check are required for first run gpg binary validation
		argv.push_back(L"--homedir");
		argv.push_back(home_dir.c_str());
	}

	argv.push_back(L"--display-charset");
	argv.push_back(L"utf-8");
	argv.push_back(L"-z9");
	argv.insert(argv.end(), params->aargv.begin(), params->aargv.end());

	if (globals.debuglog) {
		std::wstring args;
		for (unsigned int i = 0; i < argv.size(); ++i) {
			args += argv[i];
			args += L" ";
		}
		args.erase(args.size() - 1, 1);

		globals.debuglog << "gpg in: " << toUTF8(args);
	}

	params->out.Empty();

	wchar_t mir_path[MAX_PATH];
	PathToAbsoluteW(L"\\", mir_path);

	bp::child *c;
	std::future<std::string> pout, perr;
	boost::asio::io_context ios;
	if (params->bNoOutput)
		c = new bp::child(bin_path.c_str(), argv, bp::windows::hide, bp::std_in.close(), ios);
	else 
		c = new bp::child(bin_path.c_str(), argv, bp::windows::hide, bp::std_in.close(), bp::std_out > pout, bp::std_err > perr, ios);

	params->child = c;

	ios.run();
	c->wait();

	if (!params->bNoOutput) {
		std::string s = pout.get();
		if (!s.empty())
			params->out.Append(s.c_str());

		s = perr.get();
		if (!s.empty())
			params->out.Append(s.c_str());

		params->out.Replace("\r\n", "\n");
		params->out.Replace("\r\r", "");

		if (globals.debuglog)
			globals.debuglog << "gpg out: " << params->out.c_str();
	}

	params->child = nullptr;
	params->code = c->exit_code();
	delete c;

	if (params->code) {
		if (globals.debuglog)
			globals.debuglog << ": warning: wrong gpg exit status, gpg output: " << params->out.c_str();
		params->result = pxSuccessExitCodeInvalid;
	}
	else params->result = pxSuccess;
}

bool gpg_launcher(gpg_execution_params &params, boost::posix_time::time_duration t)
{
	bool ret = true;
	HANDLE hThread = mir_forkThread<gpg_execution_params>(pxEexcute_thread, &params);
	if (WaitForSingleObject(hThread, t.total_milliseconds()) == WAIT_TIMEOUT) {
		ret = false;
		if (params.child)
			params.child->terminate();
		if (globals.debuglog)
			globals.debuglog << "GPG execution timed out, aborted";
	}
	return ret;
}

/////////////////////////////////////////////////////////////////////////////////////////

void pxEexcute_passwd_change_thread(gpg_execution_params_pass *params)
{
	if (!globals.gpg_valid) {
		params->result = pxNotConfigured;
		return;
	}

	CMStringW bin_path(g_plugin.getMStringW("szGpgBinPath"));
	if (_waccess(bin_path, 0)) {
		if (globals.debuglog)
			globals.debuglog << "GPG executable not found";
		params->result = pxNotFound;
		return;
	}

	bp::environment env = boost::this_process::environment();
	env.set("LANGUAGE", "en@quot");
	env.set("LC_ALL", "English");

	std::vector<std::wstring> argv;

	argv.push_back(bin_path.c_str());
	argv.push_back(L"--homedir");
	argv.push_back(g_plugin.getMStringW("szHomePath").c_str());
	argv.push_back(L"--display-charset");
	argv.push_back(L"utf-8");
	argv.push_back(L"-z9");
	argv.insert(argv.end(), params->aargv.begin(), params->aargv.end());

	wchar_t mir_path[MAX_PATH];
	PathToAbsoluteW(L"\\", mir_path);

	bp::child c(bin_path.c_str(), argv, env, boost::process::windows::hide);
	params->child = &c;

	c.wait();

	params->child = nullptr;
}