summaryrefslogtreecommitdiff
path: root/request.c
blob: 609f3685eeeb7ac318a2ac6c07916e911fab47de (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
/*
 * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <ctype.h>
#include <limits.h>
#include <string.h>

#include "log.h"
#include "twind.h"

char hex_to_int(char);
char* uridecode(const char *);

/*
 * The following two functions are from https://geekhideout.com/urlcode.shtml
 * and provided without license restrictions
 */
char hex_to_int(char ch) {
	return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}

char *
uridecode(const char *request)
{
	char *temp = xmalloc(strlen(request) + 1);
	const char *p;
	char *pt;

	memset(temp, 0, strlen(request)+1);

	p = request;
	pt = temp;

	while (*p) {
		if (*p == '%') {
			if (p[1] && p[2]) {
				*pt++ = hex_to_int(p[1]) << 4 | hex_to_int(p[2]);
				p += 2;
			}
		} else if (*p == '+') {
			*pt++ = ' ';
		} else {
			*pt++ = *p;
		}
		p++;
	}
	*pt = '\0';

	return temp;
}

int
get_path_from_request(char *request, char *finalpath)
{
	char hostname[MAXREQLEN];
	char localpath[MAXREQLEN];
	char temp[MAXREQLEN];
	char *p, *decoded_request;
	int pos = 0, ret;

	memset(hostname, 0, sizeof(hostname));
	memset(localpath, 0, sizeof(localpath));
	memset(temp, 0, sizeof(temp));

	p = request;

	if ((p = strchr(request, '\r')) == NULL) {
		log_info("\\r missing from request, abort processing");
		return -1;
	}

	*p = '\0';	/* Strip \r\n */
	p = request;

	if (strncmp(p, "gemini://", 9) != 0) {
		log_info("Gemini scheme missing, abort processing");
		return -1;
	}
	memmove(request, p + 9, strlen(request) + 1 - 9);

	decoded_request = uridecode(request);

	/* save hostname */
	if ((p = strchr(decoded_request, '/')) != NULL)
		snprintf(hostname, strlen(decoded_request) - strlen(p)+1, "%s",
			decoded_request);
	else
		snprintf(hostname, strlen(decoded_request)+1, "%s", decoded_request);

	/* Strip possible port (e.g. :1965) from hostname */
	if ((p = strrchr(hostname, ':')) != NULL) {
		pos = strlen(hostname) - strlen(p);
		if (pos < 0 || pos > _POSIX_HOST_NAME_MAX)
			fatalx("pos while shorten hostname out of range");
		hostname[pos] = '\0';
	}

	/* Remove ../ for security reasons */
	while ((p = strstr(decoded_request, "/..")) != NULL) {
		memmove(decoded_request, p + 3, strlen(p) + 1 - 3);
	}

	if ((p = strchr(decoded_request, '/')) != NULL) {
		/* Save all after the first / in localpath */
		snprintf(localpath, strlen(decoded_request), "%s", p+1);
		if (strlen(localpath) == 0) {
			/*
			 * If the request is 'example.com/', localpart will be empty. In this case
			 * write the default to it.
			 */
			sprintf(localpath, "index.gmi");
		}
	} else {
		/* There is no slash in the request, so assume index.gmi */
		sprintf(localpath, "index.gmi");
	}

	/*
	 *We do not need to take the base dir aka /var/db/gemini into account
	 * since we already chroot() to _PATH_TWIND_CHROOT .
	 *
	 * Here, a string truncation could happen.  This can be implemented
	 * better! XXX FIXME
	 */
	snprintf(finalpath, MAXREQLEN, "%s/%s", hostname, localpath);

	/* Check if the wanted path exists and if it's a directory */
	ret = check_gemini_file(finalpath);
	if (ret < 0) {
		log_debug("%s not found", finalpath);
		free(decoded_request);
		return -2;
	} else if (ret == 1) {
		log_debug("%s is a directory", finalpath);
		/* Auto append index.gmi if destination is a directory */
		snprintf(temp, MAXREQLEN, "%s", finalpath);
		snprintf(finalpath, MAXREQLEN, "%s/index.gmi", temp);
	}

	log_debug("Got request for %s on server %s -> %s",
		localpath, hostname, finalpath);

	/* decoded_request is no longer used, so it can be freed */
	free(decoded_request);

	return 0;
}

generated by cgit on OpenBSD