Project

General

Profile

0001-core-check-free-space-in-upload-directory.patch

Proposed patch for feature - saulwold, 2018-10-08 22:20

View differences:

src/request.c
9 9
#include "sock_addr.h"
10 10

  
11 11
#include <sys/stat.h>
12
#include <sys/statvfs.h>
12 13

  
13 14
#include <limits.h>
14 15
#include <stdlib.h>
15 16
#include <string.h>
16 17
#include <sys-strings.h>
17 18

  
19
#include <errno.h>
20

  
21
static size_t get_tempdirs_free_space(server *srv)
22
{
23
	int i;
24
	int valid = 0;
25
	size_t total = 0;
26
	array *dirs = srv->srvconf.upload_tempdirs;
27

  
28
	for (i = 0; i < (int)dirs->used; ++i) {
29
		struct statvfs stat;
30
		const char *name = ((data_string *)dirs->data[i])->value->ptr;
31
		int ret = statvfs(name, &stat);
32

  
33
		if (ret >= 0) {
34
			size_t df = (size_t)(stat.f_bsize * stat.f_bfree);
35
			total += df;
36
			valid = 1;
37
		}
38
		else {
39
			log_error_write(srv, __FILE__, __LINE__, "ssss",
40
					"dir:", name,
41
					"error:", strerror(errno));
42
		}
43
	}
44

  
45
	return (valid) ? total : SSIZE_MAX;
46
}
47

  
18 48
static int request_check_hostname(buffer *host) {
19 49
	enum { DOMAINLABEL, TOPLABEL } stage = TOPLABEL;
20 50
	size_t i;
......
1149 1179
			con->http_status = 411;
1150 1180
			goto failure;
1151 1181
		}
1182
		/* content-length is larger than 64k */
1183
		if (con->request.content_length > 64*1024) {
1184
			size_t disk_free = get_tempdirs_free_space(srv);
1185
			if (con->request.content_length > disk_free) {
1186
				con->http_status = 413;
1187
				con->keep_alive = 0;
1188

  
1189
				log_error_write(srv, __FILE__, __LINE__, "ssosos",
1190
                                        "not enough free space in tempdirs:",
1191
                                        "length =", (off_t) con->request.content_length,
1192
                                        "free =", (off_t) disk_free,
1193
                                        "-> 413");
1194
                                return 0;
1195
                        }
1196
                }
1197

  
1152 1198
		break;
1153 1199
	default:
1154 1200
		break;
1155
-