summary refs log tree commit diff stats
path: root/plugins/checksum/checksum.c
blob: b5bf1bc5bab0d10913089de8440a5be92615b90b (plain) (blame)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* XChat-WDK
 * Copyright (c) 2010 Berke Viktor.
 *
 * Use of OpenSSL SHA256 interface: http://adamlamers.com/?p=5
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/sha.h>

#include "xchat-plugin.h"

#define BUFSIZE 32768
#define DEFAULT_MAX_HASH_SIZE 4294967296					/* default size is 4 GB */

#ifndef snprintf
#define snprintf _snprintf
#endif
#ifndef stat64
#define stat64 _stat64
#endif

static xchat_plugin *ph;									/* plugin handle */
static int config_fail;										/* variable for config availability */

static void
sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65])
{
	int i;
	for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
	{
		sprintf (outputBuffer + (i * 2), "%02x", hash[i]);
	}
	outputBuffer[64] = 0;
}

static void
sha256 (char *string, char outputBuffer[65])
{
	int i;
	unsigned char hash[SHA256_DIGEST_LENGTH];
	SHA256_CTX sha256;

	SHA256_Init (&sha256);
	SHA256_Update (&sha256, string, strlen(string));
	SHA256_Final (hash, &sha256);

	for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
	{
		sprintf (outputBuffer + (i * 2), "%02x", hash[i]);
	}
	outputBuffer[64] = 0;
}

static int
sha256_file (char *path, char outputBuffer[65])
{
	int bytesRead;
	unsigned char *buffer;
	unsigned char hash[SHA256_DIGEST_LENGTH];
	SHA256_CTX sha256;

	FILE *file = fopen (path, "rb");
	if (!file)
	{
		return -534;
	}

	SHA256_Init (&sha256);
	buffer = malloc (BUFSIZE);
	bytesRead = 0;

	if (!buffer)
	{
		return ENOMEM;
	}

	while ((bytesRead = fread (buffer, 1, BUFSIZE, file)))
	{
		SHA256_Update (&sha256, buffer, bytesRead);
	}

	SHA256_Final (hash, &sha256);
	sha256_hash_string (hash, outputBuffer);

	fclose (file);
	free (buffer);
	return 0;
}

static void
init ()
{
	/* check whether the config file exists, if it doesn't, try to create it */
	FILE * file_in;
	FILE * file_out;
	char buffer[1024];

	snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs"));

	if ((file_in = fopen (buffer, "r")) == NULL)
	{
		if ((file_out = fopen (buffer, "w")) == NULL)
		{
			config_fail = 1;
		} else
		{
			config_fail = 0;
			fprintf (file_out, "%llu\n", DEFAULT_MAX_HASH_SIZE);
		}
	}

	fclose (file_in);
	fclose (file_out);
}

static unsigned long long
get_max_hash_size ()
{
	FILE * file_in;
	char buffer[1024];
	unsigned long long max_hash_size;

	if (config_fail)
	{
		return DEFAULT_MAX_HASH_SIZE;
	} else
	{
		snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs"));
		file_in = fopen (buffer, "r");
		fscanf (file_in, "%llu", &max_hash_size);

		fclose (file_in);
		return max_hash_size;
	}
}

static void
print_size ()
{
	xchat_printf (ph, "File size limit for checksums (in bytes): %llu", get_max_hash_size ());
}

static void
increase_max_hash_size ()
{
	unsigned long long size;
	FILE * file_out;
	char buffer[1024];

	if (config_fail)
	{
		xchat_printf (ph, "Config file is unavailable, defaulting to 4 GB");
	} else
	{
		size = get_max_hash_size ();
		if (size <= ULLONG_MAX/2)
		{
			size *= 2;
		}
		
		snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs"));
		file_out = fopen (buffer, "w");
		fprintf (file_out, "%llu\n", size);
		fclose (file_out);
		xchat_printf (ph, "New file size limit for checksums (in bytes): %llu", size);
	}
}

static void
decrease_max_hash_size ()
{
	unsigned long long size;
	FILE * file_out;
	char buffer[1024];

	if (config_fail)
	{
		xchat_printf (ph, "Config file is unavailable, defaulting to 4 GB");
	} else
	{
		size = get_max_hash_size ();
		if (size >= 2)
		{
			size /= 2;
		}
		
		snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs"));
		file_out = fopen (buffer, "w");
		fprintf (file_out, "%llu\n", size);
		fclose (file_out);
		xchat_printf (ph, "New file size limit for checksums (in bytes): %llu", size);
	}
}

static int
dccrecv_cb (char *word[], void *userdata)
{
	int result;
	struct stat64 buffer;									/* buffer for storing file info */
	char sum[65];											/* buffer for checksum */

	result = stat64 (word[2], &buffer);
	if (result == 0)										/* stat returns 0 on success */
	{
		if (buffer.st_size <= get_max_hash_size ())
		{
			sha256_file (word[2], sum);						/* word[2] is the full filename */
			/* try to print the checksum in the privmsg tab of the sender */
			xchat_set_context (ph, xchat_find_context (ph, NULL, word[3]));
			xchat_printf (ph, "SHA-256 checksum for %s (local):  %s\n", word[1], sum);
		} else
		{
			xchat_set_context (ph, xchat_find_context (ph, NULL, word[3]));
			xchat_printf (ph, "SHA-256 checksum for %s (local):  (size limit reached, no checksum calculated, you can increase it with /CHECKSUM_INC)\n", word[1]);
		}
	} else
	{
		xchat_printf (ph, "File access error");
	}

	return XCHAT_EAT_NONE;
}

static int
dccoffer_cb (char *word[], void *userdata)
{
	int result;
	struct stat64 buffer;									/* buffer for storing file info */
	char sum[65];											/* buffer for checksum */

	result = stat64 (word[2], &buffer);
	if (result == 0)										/* stat returns 0 on success */
	{
		if (buffer.st_size <= get_max_hash_size ())
		{
			sha256_file (word[3], sum);						/* word[3] is the full filename */
			xchat_commandf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): %s", word[2], word[1], sum);
		} else
		{
			xchat_set_context (ph, xchat_find_context (ph, NULL, word[3]));
			xchat_printf (ph, "SHA-256 checksum for %s (local):  (size limit reached, no checksum calculated, you can increase it with /CHECKSUM_INC)\n", word[1]);
		}
	} else
	{
		xchat_printf (ph, "File access error");
	}

	return XCHAT_EAT_NONE;
}

static void
checksum (char *userdata[])
{
	if (!stricmp("GET", userdata[2]))
	{
		print_size ();
	} else if (!stricmp("INC", userdata[2]))
	{
		increase_max_hash_size ();
	} else if (!stricmp("DEC", userdata[2]))
	{
		decrease_max_hash_size ();
	} else
	{
		xchat_printf (ph, "Usage: /CHECKSUM GET|INC|DEC");
	}
}

int
xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
{
	ph = plugin_handle;

	*plugin_name = "Checksum";
	*plugin_desc = "Calculate checksum for DCC file transfers";
	*plugin_version = "2.0";

	init ();

	xchat_hook_command (ph, "CHECKSUM", XCHAT_PRI_NORM, checksum, "Usage: /CHECKSUM GET|INC|DEC", 0);
	xchat_hook_print (ph, "DCC RECV Complete", XCHAT_PRI_NORM, dccrecv_cb, NULL);
	xchat_hook_print (ph, "DCC Offer", XCHAT_PRI_NORM, dccoffer_cb, NULL);

	xchat_print (ph, "Checksum plugin loaded\n");
	return 1;
}

int
xchat_plugin_deinit (void)
{
	xchat_print (ph, "Checksum plugin unloaded\n");
	return 1;
}