summaryrefslogtreecommitdiff
path: root/plugins/CrashDumper/src/upload.cpp
blob: 702bc39e96380d6a2b9fc825e67249ade8b458dd (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Miranda Crash Dumper Plugin
Copyright (C) 2008 - 2012 Boris Krasnovskiy All Rights Reserved

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

HNETLIBUSER hNetlibUser;

static void arrayToHex(BYTE* data, size_t datasz, char* res)
{
	char* resptr = res;
	for (unsigned i = 0; i < datasz; i++) {
		const BYTE ch = data[i];

		const char ch0 = (char)(ch >> 4);
		*resptr++ = (char)((ch0 <= 9) ? ('0' + ch0) : (('a' - 10) + ch0));

		const char ch1 = (char)(ch & 0xF);
		*resptr++ = (char)((ch1 <= 9) ? ('0' + ch1) : (('a' - 10) + ch1));
	}
	*resptr = '\0';
}

void GetLoginStr(char* user, size_t szuser, char* pass)
{
	DBVARIANT dbv;

	if (g_plugin.getString("Username", &dbv) == 0) {
		strncpy_s(user, szuser, dbv.pszVal, _TRUNCATE);
		db_free(&dbv);
	}
	else
		user[0] = 0;

	if (g_plugin.getString("Password", &dbv) == 0) {
		BYTE hash[16];
		mir_md5_state_t context;

		mir_md5_init(&context);
		mir_md5_append(&context, (BYTE*)dbv.pszVal, (int)mir_strlen(dbv.pszVal));
		mir_md5_finish(&context, hash);

		arrayToHex(hash, sizeof(hash), pass);

		db_free(&dbv);
	}
	else
		pass[0] = 0;
}

void OpenAuthUrl(const char* url)
{
	char user[64], pass[40];
	GetLoginStr(user, sizeof(user), pass);

	if (user[0] && pass[0]) {
		char str[256];
		mir_snprintf(str, url, user); // XXX: fix me
		mir_snprintf(str, "https://vi.miranda-ng.org/detail/%s", user);
		Utils_OpenUrl(str);
	}
	else Utils_OpenUrl("https://vi.miranda-ng.org/");
}

void CreateAuthString(char* auth)
{
	char user[64], pass[40];
	GetLoginStr(user, sizeof(user), pass);

	char str[110];
	int len = mir_snprintf(str, "%s:%s", user, pass);
	mir_snprintf(auth, 250, "Basic %s", ptrA(mir_base64_encode(str, len)));
}


bool InternetDownloadFile(const char *szUrl, VerTrnsfr* szReq)
{
	int result = 0xBADBAD;
	char* szRedirUrl = nullptr;
	NETLIBHTTPREQUEST nlhr = {};

	// initialize the netlib request
	nlhr.cbSize = sizeof(nlhr);
	nlhr.requestType = REQUEST_POST;
	nlhr.flags = NLHRF_HTTP11 | NLHRF_NODUMP;
	nlhr.szUrl = (char*)szUrl;

	nlhr.headersCount = 6;
	nlhr.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*nlhr.headersCount);
	nlhr.headers[0].szName = "Connection";
	nlhr.headers[0].szValue = "close";
	nlhr.headers[1].szName = "Cache-Control";
	nlhr.headers[1].szValue = "no-cache";
	nlhr.headers[2].szName = "Pragma";
	nlhr.headers[2].szValue = "no-cache";
	nlhr.headers[3].szName = "Content-Type";
	nlhr.headers[3].szValue = "text/plain; charset=utf-8";
	nlhr.headers[4].szName = "AutoUpload";
	nlhr.headers[4].szValue = (char*)(szReq->autot ? "1" : "0");
	nlhr.headers[5].szName = "Authorization";

	char auth[256];
	CreateAuthString(auth);
	nlhr.headers[5].szValue = auth;

	nlhr.pData = szReq->buf;
	nlhr.dataLength = (int)mir_strlen(szReq->buf);

	while (result == 0xBADBAD) {
		// download the page
		NLHR_PTR nlhrReply(Netlib_HttpTransaction(hNetlibUser, &nlhr));
		if (nlhrReply) {
			// if the recieved code is 200 OK
			switch (nlhrReply->resultCode) {
			case 200:
				if (g_plugin.bUploadChanged)
					ProcessVIHash(true);

				ShowMessage(1, TranslateT("Version Info upload successful."));
				result = 0;
				break;

			case 401:
				ShowMessage(0, TranslateT("Cannot upload Version Info. Incorrect username or password"));
				result = 1;
				break;

			case 510:
				ShowMessage(0, TranslateT("Cannot upload Version Info. User is banned"));
				result = 1;
				break;

			case 511:
				ShowMessage(0, TranslateT("Cannot upload Version Info. Daily upload limit exceeded"));
				result = 1;
				break;

			case 301:
			case 302:
			case 307:
				// get the url for the new location and save it to szInfo
				// look for the reply header "Location"
				if (auto *pszUrl = Netlib_GetHeader(nlhrReply, "Location")) {
					size_t rlen = 0;
					if (pszUrl[0] == '/') {
						const char* szPath;
						const char* szPref = strstr(szUrl, "://");
						szPref = szPref ? szPref + 3 : szUrl;
						szPath = strchr(szPref, '/');
						rlen = szPath != nullptr ? szPath - szUrl : mir_strlen(szUrl);
					}

					szRedirUrl = (char*)mir_realloc(szRedirUrl,
						rlen + mir_strlen(pszUrl) * 3 + 1);

					strncpy(szRedirUrl, szUrl, rlen);
					mir_strcpy(szRedirUrl + rlen, pszUrl);

					nlhr.szUrl = szRedirUrl;
				}
				break;

			default:
				result = 1;
				ShowMessage(0, TranslateT("Cannot upload Version Info. Unknown error"));
			}
		}
		else {
			result = 1;
			ShowMessage(0, TranslateT("Cannot upload Version Info. Host unreachable."));
		}
	}

	mir_free(szRedirUrl);
	mir_free(nlhr.headers);

	return result == 0;
}

void __cdecl VersionInfoUploadThread(void* arg)
{
	VerTrnsfr* trn = (VerTrnsfr*)arg;

	char user[64], pass[40];
	GetLoginStr(user, sizeof(user), pass);
	char str[256];
	mir_snprintf(str, "https://vi.miranda-ng.org/en/upload?login=%s&pass=%s", user, pass);

	InternetDownloadFile(str, trn);
	mir_free(trn->buf);
	mir_free(trn);
}

void UploadInit(void)
{
	NETLIBUSER nlu = {};
	nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_NOHTTPSOPTION | NUF_UNICODE;
	nlu.szSettingsModule = (char*)MODULENAME;
	nlu.szDescriptiveName.w = TranslateT("Crash Dumper HTTP connections");
	hNetlibUser = Netlib_RegisterUser(&nlu);
}

void UploadClose(void)
{
	Netlib_CloseHandle(hNetlibUser);
}

bool ProcessVIHash(bool store)
{
	CMStringW buffer;
	PrintVersionInfo(buffer, 0);

	BYTE hash[16];
	mir_md5_state_t context;

	mir_md5_init(&context);
	mir_md5_append(&context, (PBYTE)buffer.c_str(), buffer.GetLength()*sizeof(wchar_t));
	mir_md5_finish(&context, hash);

	char hashstr[40];
	arrayToHex(hash, sizeof(hash), hashstr);

	if (store) {
		g_plugin.setString("VIHash", hashstr);
		return true;
	}

	ptrA VIHash(g_plugin.getStringA("VIHash"));
	if (VIHash == NULL)
		return false;

	return mir_strcmp(hashstr, VIHash) == 0;
}