aboutsummaryrefslogtreecommitdiff
path: root/lib/network/http_file.cpp
blob: 6c6abc83a076d0cba3f604f3d0d3ceb48fa4d010 (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
#include <lib/network/http_file.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <shadow.h>
#include <pwd.h>

DEFINE_REF(eHTTPFile);

eHTTPFile::eHTTPFile(eHTTPConnection *c, int _fd, int method, const char *mime): eHTTPDataSource(c), method(method)
{
	fd=_fd;
	if (method == methodGET)
	{
		c->local_header["Content-Type"]=std::string(mime);
		size=lseek(fd, 0, SEEK_END);
		char asize[10];
		snprintf(asize, 10, "%d", size);
		lseek(fd, 0, SEEK_SET);
		c->local_header["Content-Length"]=std::string(asize);
	}
	connection->code_descr="OK";
	connection->code=200;
}

int eHTTPFile::doWrite(int bytes)
{
	if (method == methodGET)
	{
		char buff[bytes];
		if (!size)
			return -1;
		int len=bytes;
		if (len>size)
			len=size;
		len=read(fd, buff, len);
		if (len<=0)
			return -1;
		size-=connection->writeBlock(buff, len);
		if (!size)
			return -1;
		else
			return 1;
	} else
		return -1;
}

void eHTTPFile::haveData(void *data, int len)
{
	if (method != methodPUT)
		return;
	::write(fd, data, len);
}

eHTTPFile::~eHTTPFile()
{
	close(fd);
}

eHTTPFilePathResolver::eHTTPFilePathResolver()
{
}


static char _base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

static int unbase64(std::string &dst, const std::string string)
{
	dst="";
	char c[4];
	int pos=0;
	unsigned int i=0;
	
	while (1)
	{
		if (i == string.size())
			break;
		char *ch=strchr(_base64, string[i++]);
		if (!ch)
		{
			i++;
			continue;
		}
		c[pos++]=ch-_base64;
		if (pos == 4)
		{
			char d[3];
			d[0]=c[0]<<2;
			d[0]|=c[1]>>4;
			d[1]=c[1]<<4;
			d[1]|=c[2]>>2;
			d[2]=c[2]<<6;
			d[2]|=c[3];
			
			dst+=d[0];
			if (c[2] != 64)
				dst+=d[1];
			if (c[3] != 64)
				dst+=d[2];
			pos=0;
		}
	}
	return pos;
}

int CheckUnixPassword(const char *user, const char *pass)
{
	passwd *pwd=getpwnam(user);
	if (!pwd)
		return -1;
	char *cpwd=pwd->pw_passwd;
	if (pwd && (!strcmp(pwd->pw_passwd, "x")))
	{
		spwd *sp=getspnam(user);
		if (!sp)						// no shadow password defined.
			return -1;
		cpwd=sp->sp_pwdp;
	}
	if (!cpwd)
		return -1;
	if ((*cpwd=='!')||(*cpwd=='*'))		 // disabled user
		return -2;
	char *cres=crypt(pass, cpwd);
	return !!strcmp(cres, cpwd);
}

static int checkAuth(const std::string cauth)
{
	std::string auth;
	if (cauth.substr(0, 6) != "Basic ")
		return -1;
	if (unbase64(auth, cauth.substr(6)))
		return -1;
	std::string username=auth.substr(0, auth.find(":"));
	std::string password=auth.substr(auth.find(":")+1);
	if (CheckUnixPassword(username.c_str(), password.c_str()))
		return -1;
	return 0;
}

DEFINE_REF(eHTTPFilePathResolver);

RESULT eHTTPFilePathResolver::getDataSource(eHTTPDataSourcePtr &ptr, std::string request, std::string path, eHTTPConnection *conn)
{
	int method;
	eDebug("request = %s, path = %s", request.c_str(), path.c_str());
	if (request == "GET")
		method=eHTTPFile::methodGET;
	else if (request == "PUT")
		method=eHTTPFile::methodPUT;
	else
	{
		ptr = new eHTTPError(conn, 405); // method not allowed
		return 0;
	}
	if (path.find("../")!=std::string::npos)		// evil hax0r
	{
		ptr = new eHTTPError(conn, 403);
		return 0;
	}
	if (path[0] != '/')		// prepend '/'
		path.insert(0,"/");
	if (path[path.length()-1]=='/')
		path+="index.html";
	
	eHTTPDataSource *data=0;
	for (ePtrList<eHTTPFilePath>::iterator i(translate); i != translate.end(); ++i)
	{
		if (i->root==path.substr(0, i->root.length()))
		{
			std::string newpath=i->path+path.substr(i->root.length());
			if (newpath.find('?'))
				newpath=newpath.substr(0, newpath.find('?'));
			eDebug("translated %s to %s", path.c_str(), newpath.c_str());

			if (i->authorized & ((method==eHTTPFile::methodGET)?1:2))
			{
				std::map<std::string, std::string>::iterator i=conn->remote_header.find("Authorization");
				if ((i == conn->remote_header.end()) || checkAuth(i->second))
				{
					conn->local_header["WWW-Authenticate"]="Basic realm=\"dreambox\"";
					ptr = new eHTTPError(conn, 401); // auth req'ed
					return 0;
				}
			}

			int fd=open(newpath.c_str(), (method==eHTTPFile::methodGET)?O_RDONLY:(O_WRONLY|O_CREAT|O_TRUNC), 0644);

			if (fd==-1)
			{
				switch (errno)
				{
				case ENOENT:
					data=new eHTTPError(conn, 404);
					break;
				case EACCES:
					data=new eHTTPError(conn, 403);
					break;
				default:
					data=new eHTTPError(conn, 403); // k.a.
					break;
				}
				break;
			}
			
			std::string ext=path.substr(path.rfind('.'));
			const char *mime="text/unknown";
			if ((ext==".html") || (ext==".htm"))
				mime="text/html";
			else if ((ext==".jpeg") || (ext==".jpg"))
				mime="image/jpeg";
			else if (ext==".gif")
				mime="image/gif";
			else if (ext==".css")
				mime="text/css";
			else if (ext==".png")
				mime="image/png";
			else if (ext==".xml")
				mime="text/xml";
			else if (ext==".xsl")
				mime="text/xsl";

			data=new eHTTPFile(conn, fd, method, mime);
			break;
		}
	}
	if (!data)
		return -1;
	ptr = data;
	return 0;
}

void eHTTPFilePathResolver::addTranslation(std::string path, std::string root, int authorized)
{
	if (path[path.length()-1]!='/')
		path+='/';
	if (root[root.length()-1]!='/')
		root+='/';
	translate.push_back(new eHTTPFilePath(path, root, authorized));
}