#include "../Mandshurica/mandshurica.h"
+#include "../Mandshurica/ms_db.h"
#include "../Mandshurica/ms_log.h"
#include "../Mandshurica/ms_util.h"
struct mandshurica_config* config;
+char* get_cookie(const char* cookie, const char* key){
+ char* str = mandshurica_strdup(cookie);
+ int i;
+ int start = 0;
+ for(i = 0; str[i] != 0; i++){
+ if(str[i] == '='){
+ char* k = malloc(i - start + 1);
+ k[i] = 0;
+ memcpy(k, str + start, i - start);
+ if(strcmp(key, k) == 0){
+ int epos = i + 1;
+ for(; str[i] != 0 && str[i] != ';'; i++);
+ char* v = malloc(i - epos + 1);
+ v[i] = 0;
+ memcpy(v, str + epos, i - epos);
+ for(; str[i] != 0 && str[i] != ' ' && str[i] != '\t'; i++);
+ start = i + 1;
+ free(k);
+ free(str);
+ return v;
+ }else{
+ for(; str[i] != 0 && str[i] != ';'; i++);
+ for(; str[i] != 0 && str[i] != ' ' && str[i] != '\t'; i++);
+ start = i + 1;
+ }
+ free(k);
+ }
+ }
+ free(str);
+ return NULL;
+}
+
int mod_init(struct mandshurica_config* _config) {
config = _config;
config->mandshurica_log(MS_INFO, "HTTP Module init");
struct ms_param param;
param.login = false;
cbuf[1] = 0;
+ char* cookie = NULL;
while(true) {
int state;
int count;
send(sock, "Content-Length: 0\r\n", 19, 0);
send(sock, "\r\n", 2, 0);
} else if(form) {
- bool sent = false;
- for(j = 0; (*config->libs)[j] != NULL; j++) {
- const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
- if(strcmp(type, MS_MOD_AUTH) == 0) {
- const char* httppath = (const char*)dlsym((*config->libs)[j]->lib, "mod_http_path");
- if(httppath != NULL) {
- if(strcmp(httppath, path) == 0) {
- int (*mod_auth)(const char*, const char*, char**) = (int (*)(const char*, const char*, char**))dlsym((*config->libs)[j]->lib, "mod_auth");
- char* usr = mandshurica_parse_form(postbuf, "username");
- char* pwd = mandshurica_parse_form(postbuf, "password");
- if(usr != NULL && pwd != NULL) {
- char* token;
- int res = mod_auth(usr, pwd, &token);
- if(res == MS_AUTH_OK) {
- char* type;
- uint64_t len;
- char* data = NULL;
- char* s;
-
- struct stat st;
- char* lastmod = NULL;
- const char* pth = mandshurica_strcat(webroot, "/login-success/");
- if(stat(pth, &st) == 0) {
- if(S_ISDIR(st.st_mode)) {
- if(data == NULL) {
- s = mandshurica_strcat(pth, "/index.html.tmpl");
- data = config->mandshurica_load(s, &type, &lastmod, &len, param);
- free(s);
+ if(strcmp(path, "/cookie-logout") == 0){
+ bool did_logout = false;
+ if(cookie != NULL){
+ char* token = get_cookie(cookie, "TOKEN");
+ if(token != NULL){
+ struct ms_db* db = mandshurica_db_open(config->mandshurica_get_param("LoginDB"));
+ char* usr = mandshurica_db_get(db, token);
+ if(usr != NULL){
+ free(usr);
+ did_logout = true;
+ }
+ mandshurica_db_close(db);
+ }
+ free(token);
+ }
+ if(did_logout){
+ char* type;
+ uint64_t len;
+ char* data = NULL;
+ char* s;
+
+ struct stat st;
+ char* lastmod = NULL;
+ const char* pth = mandshurica_strcat(webroot, "/logout-success/");
+ if(stat(pth, &st) == 0) {
+ if(S_ISDIR(st.st_mode)) {
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html.tmpl");
+ data = config->mandshurica_load(s, &type, &lastmod, &len, param);
+ free(s);
+ }
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html");
+ data = config->mandshurica_load(s, &type, &lastmod, &len, param);
+ free(s);
+ }
+ } else {
+ data = config->mandshurica_load(pth, &type, &lastmod, &len, param);
+ }
+ }
+ if(data != NULL) {
+ send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Type: ", 14, 0);
+ send(sock, type, strlen(type), 0);
+ send(sock, "\r\n", 2, 0);
+ if(lastmod != NULL) {
+ send(sock, "Last-Modified: ", 15, 0);
+ send(sock, lastmod, strlen(lastmod), 0);
+ send(sock, "\r\n", 2, 0);
+ free(lastmod);
+ }
+ send(sock, "Content-Length: ", 16, 0);
+
+ char* lenstr = malloc(513);
+ sprintf(lenstr, "%llu", len);
+ send(sock, lenstr, strlen(lenstr), 0);
+ free(lenstr);
+
+ send(sock, "\r\n", 2, 0);
+ send(sock, "\r\n", 2, 0);
+ send(sock, data, len, 0);
+ free(type);
+ } else {
+ send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "\r\n", 2, 0);
+ send(sock, "\r\n", 2, 0);
+ }
+ }
+ }else{
+ bool sent = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* httppath = (const char*)dlsym((*config->libs)[j]->lib, "mod_http_path");
+ run_again:;
+ if(httppath != NULL) {
+ if(strcmp(httppath, path) == 0) {
+ int (*mod_auth)(const char*, const char*, char**) = (int (*)(const char*, const char*, char**))dlsym((*config->libs)[j]->lib, "mod_auth");
+ char* usr = mandshurica_parse_form(postbuf, "username");
+ char* pwd = mandshurica_parse_form(postbuf, "password");
+ if(usr != NULL && pwd != NULL) {
+ char* token = mandshurica_strdup(httppath);
+ int res = mod_auth(usr, pwd, &token);
+ if(res == MS_AUTH_OK) {
+ char* type;
+ uint64_t len;
+ char* data = NULL;
+ char* s;
+
+ struct stat st;
+ char* lastmod = NULL;
+ const char* pth = mandshurica_strcat(webroot, "/login-success/");
+ if(stat(pth, &st) == 0) {
+ if(S_ISDIR(st.st_mode)) {
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html.tmpl");
+ data = config->mandshurica_load(s, &type, &lastmod, &len, param);
+ free(s);
+ }
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html");
+ data = config->mandshurica_load(s, &type, &lastmod, &len, param);
+ free(s);
+ }
+ } else {
+ data = config->mandshurica_load(pth, &type, &lastmod, &len, param);
}
- if(data == NULL) {
- s = mandshurica_strcat(pth, "/index.html");
- data = config->mandshurica_load(s, &type, &lastmod, &len, param);
- free(s);
+ }
+ if(data != NULL) {
+ send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Type: ", 14, 0);
+ send(sock, type, strlen(type), 0);
+ send(sock, "\r\n", 2, 0);
+ if(lastmod != NULL) {
+ send(sock, "Last-Modified: ", 15, 0);
+ send(sock, lastmod, strlen(lastmod), 0);
+ send(sock, "\r\n", 2, 0);
+ free(lastmod);
}
+ send(sock, "Content-Length: ", 16, 0);
+
+ char* lenstr = malloc(513);
+ sprintf(lenstr, "%llu", len);
+ send(sock, lenstr, strlen(lenstr), 0);
+ free(lenstr);
+
+ send(sock, "\r\n", 2, 0);
+ send(sock, "Set-Cookie: ", 11, 0);
+ send(sock, "TOKEN=", 6, 0);
+ send(sock, token, strlen(token), 0);
+ send(sock, "; HttpOnly", 10, 0);
+ send(sock, "\r\n", 2, 0);
+ send(sock, "\r\n", 2, 0);
+ send(sock, data, len, 0);
+ free(type);
} else {
- data = config->mandshurica_load(pth, &type, &lastmod, &len, param);
- }
- }
- if(data != NULL) {
- send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
- send(sock, "Connection: keep-alive\r\n", 24, 0);
- send(sock, "Content-Type: ", 14, 0);
- send(sock, type, strlen(type), 0);
- send(sock, "\r\n", 2, 0);
- if(lastmod != NULL) {
- send(sock, "Last-Modified: ", 15, 0);
- send(sock, lastmod, strlen(lastmod), 0);
+ send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "Set-Cookie: ", 11, 0);
+ send(sock, "TOKEN=", 6, 0);
+ send(sock, token, strlen(token), 0);
+ send(sock, "; HttpOnly", 10, 0);
+ send(sock, "\r\n", 2, 0);
send(sock, "\r\n", 2, 0);
- free(lastmod);
}
- send(sock, "Content-Length: ", 16, 0);
-
- char* lenstr = malloc(513);
- sprintf(lenstr, "%llu", len);
- send(sock, lenstr, strlen(lenstr), 0);
- free(lenstr);
-
- send(sock, "\r\n", 2, 0);
- send(sock, "Set-Cookie: ", 11, 0);
- send(sock, "TOKEN=", 6, 0);
- send(sock, token, strlen(token), 0);
- send(sock, "; HttpOnly", 10, 0);
- send(sock, "\r\n", 2, 0);
- send(sock, "\r\n", 2, 0);
- send(sock, data, len, 0);
- free(type);
- } else {
- send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
+ free(token);
+ } else if(res == MS_AUTH_ERROR) {
+ send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0);
send(sock, "Connection: keep-alive\r\n", 24, 0);
send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "Set-Cookie: ", 11, 0);
- send(sock, "TOKEN=", 6, 0);
- send(sock, token, strlen(token), 0);
- send(sock, "; HttpOnly", 10, 0);
- send(sock, "\r\n", 2, 0);
send(sock, "\r\n", 2, 0);
- }
- free(token);
- } else if(res == MS_AUTH_ERROR) {
- send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0);
- send(sock, "Connection: keep-alive\r\n", 24, 0);
- send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "\r\n", 2, 0);
- } else if(res == MS_AUTH_FAIL) {
- char* type;
- uint64_t len;
- char* data = NULL;
- char* s;
-
- struct stat st;
- char* lastmod = NULL;
- const char* pth = mandshurica_strcat(webroot, "/login-fail/");
- if(stat(pth, &st) == 0) {
- if(S_ISDIR(st.st_mode)) {
- if(data == NULL) {
- s = mandshurica_strcat(pth, "/index.html.tmpl");
- data = config->mandshurica_load(s, &type, &lastmod, &len, param);
- free(s);
+ } else if(res == MS_AUTH_FAIL) {
+ char* type;
+ uint64_t len;
+ char* data = NULL;
+ char* s;
+
+ struct stat st;
+ char* lastmod = NULL;
+ const char* pth = mandshurica_strcat(webroot, "/login-fail/");
+ if(stat(pth, &st) == 0) {
+ if(S_ISDIR(st.st_mode)) {
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html.tmpl");
+ data = config->mandshurica_load(s, &type, &lastmod, &len, param);
+ free(s);
+ }
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html");
+ data = config->mandshurica_load(s, &type, &lastmod, &len, param);
+ free(s);
+ }
+ } else {
+ data = config->mandshurica_load(pth, &type, &lastmod, &len, param);
}
- if(data == NULL) {
- s = mandshurica_strcat(pth, "/index.html");
- data = config->mandshurica_load(s, &type, &lastmod, &len, param);
- free(s);
+ }
+ if(data != NULL) {
+ send(sock, "HTTP/1.1 403 Forbidden\r\n", 24, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Type: ", 14, 0);
+ send(sock, type, strlen(type), 0);
+ send(sock, "\r\n", 2, 0);
+ if(lastmod != NULL) {
+ send(sock, "Last-Modified: ", 15, 0);
+ send(sock, lastmod, strlen(lastmod), 0);
+ send(sock, "\r\n", 2, 0);
+ free(lastmod);
}
+ send(sock, "Content-Length: ", 16, 0);
+
+ char* lenstr = malloc(513);
+ sprintf(lenstr, "%llu", len);
+ send(sock, lenstr, strlen(lenstr), 0);
+ free(lenstr);
+
+ send(sock, "\r\n", 2, 0);
+ send(sock, "\r\n", 2, 0);
+ send(sock, data, len, 0);
+ free(type);
} else {
- data = config->mandshurica_load(pth, &type, &lastmod, &len, param);
- }
- }
- if(data != NULL) {
- send(sock, "HTTP/1.1 403 Forbidden\r\n", 24, 0);
- send(sock, "Connection: keep-alive\r\n", 24, 0);
- send(sock, "Content-Type: ", 14, 0);
- send(sock, type, strlen(type), 0);
- send(sock, "\r\n", 2, 0);
- if(lastmod != NULL) {
- send(sock, "Last-Modified: ", 15, 0);
- send(sock, lastmod, strlen(lastmod), 0);
+ send(sock, "HTTP/1.1 403 Forbidden\r\n", 24, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
send(sock, "\r\n", 2, 0);
- free(lastmod);
}
- send(sock, "Content-Length: ", 16, 0);
-
- char* lenstr = malloc(513);
- sprintf(lenstr, "%llu", len);
- send(sock, lenstr, strlen(lenstr), 0);
- free(lenstr);
-
- send(sock, "\r\n", 2, 0);
- send(sock, "\r\n", 2, 0);
- send(sock, data, len, 0);
- free(type);
- } else {
- send(sock, "HTTP/1.1 403 Forbidden\r\n", 24, 0);
- send(sock, "Connection: keep-alive\r\n", 24, 0);
- send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "\r\n", 2, 0);
}
+ } else {
+ send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "\r\n", 2, 0);
}
- } else {
- send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0);
- send(sock, "Connection: keep-alive\r\n", 24, 0);
- send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "\r\n", 2, 0);
+ sent = true;
}
- sent = true;
}
}
}
- }
- if(!sent) {
- send(sock, "HTTP/1.1 403 Forbidden\r\n", 15 + 9, 0);
- send(sock, "Connection: keep-alive\r\n", 24, 0);
- send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "\r\n", 2, 0);
+ if(!sent) {
+ send(sock, "HTTP/1.1 403 Forbidden\r\n", 15 + 9, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "\r\n", 2, 0);
+ }
}
} else {
send(sock, "HTTP/1.1 500 Internal Server Error\r\n", 15 + 9 + 12, 0);
} else if(buf[i] == '\n') {
count++;
if(count == 2) {
+ if(headers != NULL){
+ int j;
+ for(j = 0; headers[j] != NULL; j += 2) {
+ if(mandshurica_strcaseequ(headers[j], "Cookie")){
+ cookie = mandshurica_strdup(headers[j + 1]);
+ char* v = get_cookie(headers[j + 1], "TOKEN");
+ if(v != NULL){
+ struct ms_db* db = mandshurica_db_open(config->mandshurica_get_param("LoginDB"));
+ char* username = mandshurica_db_get(db, v);
+ if(username != NULL){
+ param.login = true;
+ free(username);
+ }
+ mandshurica_db_close(db);
+ free(v);
+ }
+ }
+ }
+ }
if(strcmp(method, "POST") == 0) {
if(headers != NULL) {
int j;