]> Nishi Git Mirror - mandshurica.git/commitdiff
trying to get cookie auth works
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 20 Apr 2024 02:24:31 +0000 (02:24 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 20 Apr 2024 02:24:31 +0000 (02:24 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@49 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Mandshurica/config.c
Mandshurica/mandshurica.h
Mandshurica/ms_util.h
Mandshurica/util.c
Module/Makefile
Module/cookie.c
Module/http.c

index 093b44d5039668eb152f031f8885a8fc056aba97..743247add46a9e7cc23ed4074dfb4d4a5c4c9b40 100644 (file)
@@ -32,6 +32,7 @@
 #include "ms_config.h"
 
 #include "mandshurica.h"
+#include "ms_crypto.h"
 #include "ms_file.h"
 #include "ms_log.h"
 #include "ms_template.h"
@@ -88,7 +89,7 @@ char* mandshurica_get_param(const char* param) {
        return NULL;
 }
 
-struct mandshurica_config config = {.mandshurica_log = mandshurica_log, .mandshurica_get_param = mandshurica_get_param, .mandshurica_parse_template = mandshurica_parse_template, .mandshurica_load = mandshurica_load};
+struct mandshurica_config config = {.mandshurica_log = mandshurica_log, .mandshurica_get_param = mandshurica_get_param, .mandshurica_parse_template = mandshurica_parse_template, .mandshurica_load = mandshurica_load, .mandshurica_sha512 = mandshurica_sha512};
 
 int mandshurica_load_config(const char* path) {
        if(server_root == NULL) {
index 9db8df488e6c95d33d38bdeef802b7e5e0c96979..b1206caf991192060e30f1bf76528330100b423d 100644 (file)
 #define MS_MOD_SRV "SRV"
 #define MS_MOD_AUTH "AUTH"
 
+#define MS_AUTH_ERROR -1
+#define MS_AUTH_OK 0
+#define MS_AUTH_FAIL 1
+
 struct mandshurica_config {
        void (*mandshurica_log)(const char*, const char*);
        char* (*mandshurica_get_param)(const char* param);
        char* (*mandshurica_parse_template)(const char* data);
        char* (*mandshurica_load)(const char* path, char** type, char** lastmod, uint64_t* len);
+       char* (*mandshurica_sha512)(const char* pwd);
        struct mandshurica_mod*** libs;
 };
 
index d06c7373a77cda96b78cb903930d21b64321385d..6afd962bcfdc031b6594d874067fac6ae7dc32b6 100644 (file)
@@ -39,5 +39,6 @@ char* mandshurica_strdup(const char* str);
 char* mandshurica_path(const char* path);
 bool mandshurica_strequ(const char* str1, const char* str2);
 bool mandshurica_strcaseequ(const char* str1, const char* str2);
+char* mandshurica_parse_form(const char* form, const char* name);
 
 #endif
index 36213bbff4aac9657815ea4a2d35625a99d7c222..14754099231a9cb49ff6f4e5b9e8a36da48bd4de 100644 (file)
@@ -133,3 +133,12 @@ bool mandshurica_strcaseequ(const char* str1, const char* str2) {
        }
        return true;
 }
+
+char* mandshurica_parse_form(const char* form, const char* name) {
+       int i;
+       char cbuf[2];
+       cbuf[1] = 0;
+       for(i = 0;; i++) {
+       }
+       return NULL;
+}
index b536a03d2c23bc26b48a9646de676831be435bf3..d73b1e5eb2863b1a9e670fa8b47660f7dcee87fe 100644 (file)
@@ -12,12 +12,18 @@ endif
 
 all: ./subversion.so ./syslog.so ./http.so ./cookie.so
        
-./%.so: ./%.o ./util.o
+./%.so: ./%.o ./util.o ./db.o ./crypto.o
        $(CC) $(LDFLAGS) $(EXTRA_LDFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS)
 
 ./util.o: ../Mandshurica/util.c
        $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
 
+./db.o: ../Mandshurica/db.c
+       $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+
+./crypto.o: ../Mandshurica/crypto.c
+       $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+
 ./%.o: ./%.c ../Mandshurica/mandshurica.h
        $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
 
index a07312bde846441c6c0a0be00271af4724bfbb2d..3aca1390a2ab5cb7aa9b8702d3e199ff1ef72dbf 100644 (file)
@@ -29,6 +29,7 @@
 /* --- END LICENSE --- */
 
 #include "../Mandshurica/mandshurica.h"
+#include "../Mandshurica/ms_db.h"
 #include "../Mandshurica/ms_log.h"
 #include "../Mandshurica/ms_util.h"
 
@@ -60,3 +61,24 @@ int mod_init(struct mandshurica_config* _config) {
 
        return 0;
 }
+
+int mod_auth(const char* username, const char* password) {
+       struct ms_db* db = mandshurica_db_open(config->mandshurica_get_param("CookieDB"));
+       if(db != NULL) {
+               char* pwd = mandshurica_db_get(db, username);
+               if(pwd == NULL) {
+                       mandshurica_db_close(db);
+                       return MS_AUTH_ERROR;
+               } else {
+                       char* sh = config->mandshurica_sha512(password);
+                       if(strcmp(sh, pwd) == 0) {
+                               free(sh);
+                               return MS_AUTH_OK;
+                       }
+                       free(sh);
+                       return MS_AUTH_FAIL;
+               }
+               mandshurica_db_close(db);
+       }
+       return MS_AUTH_ERROR;
+}
index 28aa4165af9e1d32fc555a719d8e40943049bf16..3f3a774fb6f26958149cbd37749e64ce816f5340 100644 (file)
@@ -154,16 +154,73 @@ void http_handler(int sock) {
                        headers = NULL;
                }
                unsigned long long conlen = 0;
+               unsigned long long conincr = 0;
+               char* postbuf = NULL;
                while(true) {
                        int len = recv(sock, buf, BUFFER_SIZE, 0);
                        if(len <= 0) goto goquit;
                        int i;
                        for(i = 0; i < len; i++) {
                                cbuf[0] = buf[i];
-                               if(conlen > 0){
+                               if(conlen > 0) {
                                        conlen--;
-                                       printf("%c", cbuf[0]);
-                                       fflush(stdout);
+                                       postbuf[conincr] = cbuf[0];
+                                       conincr++;
+                                       if(conlen == 0) {
+                                               if(headers != NULL) {
+                                                       int j;
+                                                       bool hastype = false;
+                                                       bool form = false;
+                                                       for(j = 0; headers[j] != NULL; j += 2) {
+                                                               if(mandshurica_strcaseequ(headers[j], "Content-Type")) {
+                                                                       if(mandshurica_strcaseequ(headers[j + 1], "application/x-www-form-urlencoded")) {
+                                                                               form = true;
+                                                                               hastype = true;
+                                                                               break;
+                                                                       }
+                                                               }
+                                                       }
+                                                       if(!hastype) {
+                                                               send(sock, "HTTP/1.1 415 Unsupported Media Type\r\n", 28 + 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(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*) = (int (*)(const char*, const char*))dlsym((*config->libs)[j]->lib, "mod_auth");
+                                                                                               char* usr = mandshurica_parse_form(postbuf, "username");
+                                                                                               char* pwd = mandshurica_parse_form(postbuf, "password");
+                                                                                               printf("%s, %s\n", usr == NULL ? "(null)" : usr, pwd == NULL ? "(null)" : pwd);
+                                                                                               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);
+                                                               }
+                                                       } else {
+                                                               send(sock, "HTTP/1.1 500 Internal Server Error\r\n", 15 + 9 + 12, 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);
+                                                       }
+                                               }
+
+                                               fflush(stdout);
+                                               free(postbuf);
+                                               state = 0;
+                                               goto reset;
+                                       }
                                        continue;
                                }
                                if(state == 0) {
@@ -193,29 +250,35 @@ void http_handler(int sock) {
                                                count++;
                                                if(count == 2) {
                                                        if(strcmp(method, "POST") == 0) {
-                                                               if(headers != NULL){
+                                                               if(headers != NULL) {
                                                                        int j;
                                                                        bool haslen = false;
-                                                                       for(j = 0; headers[j] != NULL; j += 2){
-                                                                               if(mandshurica_strcaseequ(headers[j], "Content-Length")){
+                                                                       for(j = 0; headers[j] != NULL; j += 2) {
+                                                                               if(mandshurica_strcaseequ(headers[j], "Content-Length")) {
                                                                                        haslen = true;
                                                                                        conlen = atoll(headers[j + 1]);
+                                                                                       conincr = 0;
+                                                                                       postbuf = malloc(conlen + 1);
+                                                                                       postbuf[conlen] = 0;
+                                                                                       count = 0;
                                                                                        break;
                                                                                }
                                                                        }
-                                                                       if(!haslen){
+                                                                       if(!haslen) {
                                                                                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 {
+                                                                               continue;
                                                                        }
-                                                               }else{
+                                                               } 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 if(strcmp(method, "GET") == 0) {
+                                                       } else if(strcmp(method, "GET") == 0) {
                                                                char* chpath = mandshurica_strcat(webroot, path);
                                                                struct stat st2;
                                                                if(stat(chpath, &st2) != 0) {