]> Nishi Git Mirror - mandshurica.git/commitdiff
got auth working... kinda
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 20 Apr 2024 02:35:41 +0000 (02:35 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 20 Apr 2024 02:35:41 +0000 (02:35 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@50 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Mandshurica/util.c
Module/http.c

index 14754099231a9cb49ff6f4e5b9e8a36da48bd4de..8a00ff22c5d2fd6069d3a8c9743f2f9edd82f713 100644 (file)
@@ -138,7 +138,31 @@ char* mandshurica_parse_form(const char* form, const char* name) {
        int i;
        char cbuf[2];
        cbuf[1] = 0;
+       char* _form = mandshurica_strdup(form);
+       int start = 0;
        for(i = 0;; i++) {
+               if(_form[i] == 0 || _form[i] == '&') {
+                       char oldc = _form[i];
+                       _form[i] = 0;
+                       int j;
+                       bool found = false;
+                       char* _tform = _form + start;
+                       for(j = 0; _tform[j] != 0; j++) {
+                               if(_tform[j] == '=') {
+                                       _tform[j] = 0;
+                                       if(strcmp(_tform, name) == 0) found = true;
+                                       break;
+                               }
+                       }
+                       if(found) {
+                               char* val = mandshurica_strdup(_tform + j + 1);
+                               free(_form);
+                               return val;
+                       }
+                       start = i + 1;
+                       if(oldc == 0) break;
+               }
        }
+       free(_form);
        return NULL;
 }
index 3f3a774fb6f26958149cbd37749e64ce816f5340..cef0b0024209692d46aa675cc594b806adec8635 100644 (file)
@@ -196,7 +196,27 @@ void http_handler(int sock) {
                                                                                                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);
+                                                                                               if(usr != NULL && pwd != NULL) {
+                                                                                                       int res = mod_auth(usr, pwd);
+                                                                                                       if(res == MS_AUTH_OK) {
+                                                                                                       } 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) {
+
+                                                                                                               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 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;
                                                                                        }
                                                                                }