]> Nishi Git Mirror - dataworks.git/commitdiff
add stuff
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Fri, 7 Jun 2024 03:24:43 +0000 (03:24 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Fri, 7 Jun 2024 03:24:43 +0000 (03:24 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@167 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Server/hayes.c
Server/server.c
Server/tcpip.c

index 4dcfb5f2abb4b60caa982355036599bf6cd970f2..200b127006c51331f95e7d03dfd617591ce21141 100644 (file)
 #include <dw_database.h>
 #include <dw_util.h>
 
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <signal.h>
 
 extern int argc;
 extern char** argv;
+extern bool auth;
+extern char* authfile;
 
 #include <bios.h>
-#include <i86.h>
 #include <conio.h>
+#include <i86.h>
 
 void protocol_init(int sock);
 void protocol_loop(int sock);
@@ -53,17 +55,17 @@ bool option(const char* str, const char* shortopt, const char* longopt);
 
 int port = -1;
 
-int get_ioport(){
+int get_ioport() {
 #ifdef PC98
        if(port == 0) return 0x30;
 #else
-       if(port == 0){
+       if(port == 0) {
                return 0x3f8;
-       }else if(port == 1){
+       } else if(port == 1) {
                return 0x2f8;
-       }else if(port == 2){
+       } else if(port == 2) {
                return 0x3e8;
-       }else if(port == 3){
+       } else if(port == 3) {
                return 0x2e8;
        }
 #endif
@@ -105,9 +107,9 @@ char* modem_response(void) {
                        }
                }
                printf("\r");
-               if(signals > 0){
+               if(signals > 0) {
                        free(buf);
-                       if(connected){
+                       if(connected) {
                                writeline(0, "QUIT:Bye");
                                disconnect(0);
                        }
@@ -122,11 +124,11 @@ int server_init(void) {
        connected = false;
        int i;
        for(i = 1; i < argc; i++) {
-               if(argv[i][0] == '/' || argv[i][0] == '-'){
-                       if(option(argv[i], "p", "port")){
+               if(argv[i][0] == '/' || argv[i][0] == '-') {
+                       if(option(argv[i], "p", "port")) {
                                i++;
                                if(__dw_strcaseequ(argv[i], "COM1")) {
-                                       port = 0;       
+                                       port = 0;
 #ifndef PC98
                                } else if(__dw_strcaseequ(argv[i], "COM2")) {
                                        port = 1;
@@ -139,11 +141,15 @@ int server_init(void) {
                                        fprintf(stderr, "Invalid port: %s\n", argv[i]);
                                        return 1;
                                }
-                       }else{
+                       } else if(option(argv[i], "l", "login")) {
+                               i++;
+                               authfile = argv[i];
+                               auth = true;
+                       } else {
                                fprintf(stderr, "Invalid option: %s\n", argv[i]);
                                return 1;
                        }
-               }else{
+               } else {
                        db = dataworks_database_open(argv[i]);
                }
        }
@@ -210,9 +216,7 @@ void server_loop(void) {
        }
 }
 
-char* readline_sock(int sock){
-       return connected ? modem_response() : NULL;
-}
+char* readline_sock(int sock) { return connected ? modem_response() : NULL; }
 
 void writeline(int sock, const char* str) {
        char* snd = __dw_strcat(str, "\r\n");
@@ -220,7 +224,7 @@ void writeline(int sock, const char* str) {
        free(snd);
 }
 
-void disconnect(int sock){
+void disconnect(int sock) {
        while(inp(get_ioport() + 6) & (1 << 7)) outp(get_ioport() + 4, 0);
        connected = false;
 }
index 35c2160c7605b12547ecb040880b90ae5082f5cf..3acae32a0fef2291368f0aca3236ebf819c87f14 100644 (file)
 #include <dw_database.h>
 #include <dw_util.h>
 
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <signal.h>
 
 int argc;
 char** argv;
+bool auth = false;
+char* authfile = NULL;
+FILE* authdb;
+struct auth_entry {
+       char* user;
+       char* pass;
+};
+struct auth_entry** entries;
 #ifdef __DOS__
 sig_atomic_t signals = 0;
 #endif
@@ -64,10 +72,17 @@ void disconnect(int sock);
 
 void protocol_init(int sock) {
        /* sock is ignored on hayes */
-       writeline(sock, "READY");
        char* tmp;
        char* verstr;
-       verstr = __dw_strcat("ATTR:AUTH:VER=", dataworks_get_version());
+       verstr = __dw_strcat("ATTR", auth ? ":AUTH" : "");
+
+       tmp = verstr;
+       verstr = __dw_strcat(tmp, ":VER=");
+       free(tmp);
+
+       tmp = verstr;
+       verstr = __dw_strcat(tmp, dataworks_get_version());
+       free(tmp);
 
        tmp = verstr;
        verstr = __dw_strcat(tmp, ":PLATFORM=");
@@ -79,75 +94,126 @@ void protocol_init(int sock) {
 
        writeline(sock, verstr);
        free(verstr);
+
+       writeline(sock, "READY");
 }
 
-void protocol_loop(int sock){
-       while(1){
+void protocol_loop(int sock) {
+       while(1) {
                char* buf = readline_sock(sock);
-               if(buf == NULL){
+               if(buf == NULL) {
                        break;
                }
                int i;
                bool has_arg = false;
-               for(i = 0; buf[i] != 0; i++){
-                       if(buf[i] == ':'){
+               for(i = 0; buf[i] != 0; i++) {
+                       if(buf[i] == ':') {
                                buf[i] = 0;
                                has_arg = true;
                                break;
                        }
                }
-               if(__dw_strcaseequ(buf, "ECHO")){
+               if(__dw_strcaseequ(buf, "ECHO")) {
                        int start;
-                       if(has_arg){
+                       if(has_arg) {
                                i++;
                                start = i;
-                               for(;; i++){
-                                       if(buf[i] == ':' || buf[i] == 0){
+                               for(;; i++) {
+                                       if(buf[i] == ':' || buf[i] == 0) {
                                                char oldc = buf[i];
                                                buf[i] = 0;
                                                char* echo = __dw_strcat("ECHO:", buf + start);
                                                start = i + 1;
-       
+
                                                writeline(sock, echo);
                                                free(echo);
                                                if(oldc == 0) break;
                                        }
                                }
-                       }else{
+                       } else {
                                writeline(sock, "ECHO");
                        }
-               }else if(__dw_strcaseequ(buf, "BYE") || __dw_strcaseequ(buf, "QUIT")){
+               } else if(__dw_strcaseequ(buf, "BYE") || __dw_strcaseequ(buf, "QUIT")) {
                        writeline(sock, "QUIT:Bye");
                        disconnect(sock);
-               }else{
+               } else if(__dw_strcaseequ(buf, "USER")) {
+                       if(auth) {
+                       } else {
+                               writeline(sock, "ERROR:NO_AUTH");
+                       }
+               } else if(__dw_strcaseequ(buf, "PASS")) {
+                       if(auth) {
+                       } else {
+                               writeline(sock, "ERROR:NO_AUTH");
+                       }
+               } else {
                        writeline(sock, "ERROR:UNKNOWN_CMD");
                }
                free(buf);
        }
 }
 
+void exitnow(int sig) {
 #ifdef __DOS__
-void exitnow(int sig){
-       signals++;
-}
+       if(sig != -1) signals++;
+#else
+       if(sig != -1) exit(0);
 #endif
+}
 
 int main(int _argc, char** _argv) {
        argc = _argc;
        argv = _argv;
-#ifdef __DOS__
+       entries = malloc(sizeof(*entries));
+       entries[0] = NULL;
        signal(SIGINT, exitnow);
-#endif
+       signal(SIGTERM, exitnow);
        printf("DataWorks Server  version %s  %s %s\n", dataworks_get_version(), dataworks_get_compile_date(), dataworks_get_platform());
        int st;
        if((st = server_init()) != 0) return st;
-       if(db != NULL){
-               if(db->error){
+       if(auth) {
+               authdb = fopen(authfile, "r");
+               if(authdb == NULL) {
+                       fprintf(stderr, "Failed to open authfile\n");
+                       return 1;
+               }
+               char cbuf[2];
+               cbuf[1] = 0;
+               char* str = malloc(1);
+               str[0] = 0;
+               while(true) {
+                       if(fread(cbuf, 1, 1, authdb) <= 0) break;
+                       if(cbuf[0] == '\n') {
+                               int i;
+                               bool has_pass = false;
+                               for(i = 0; str[i] != 0; i++) {
+                                       if(str[i] == ':') {
+                                               str[i] = 0;
+                                               has_pass = true;
+                                               break;
+                                       }
+                               }
+                               if(has_pass) {
+                                       printf("User %s is allowed to access the database now\n", str);
+                               }
+                               free(str);
+                               str = malloc(1);
+                               str[0] = 0;
+                       } else if(cbuf[0] != '\r') {
+                               char* tmp = str;
+                               str = __dw_strcat(tmp, cbuf);
+                               free(tmp);
+                       }
+               }
+               free(str);
+       }
+       if(db != NULL) {
+               if(db->error) {
                        dataworks_database_close(db);
                        db = NULL;
                }
        }
-       if(db == NULL){
+       if(db == NULL) {
                fprintf(stderr, "Failed to open databse\n");
                return 1;
        }
index 923ad0c7969563dcbd5b148367b46cb7e7411147..c11ef0b8bd90308f30494c527258f5175f6b49db 100644 (file)
@@ -38,6 +38,8 @@
 
 extern int argc;
 extern char** argv;
+extern bool auth;
+extern char* authfile;
 
 #include <arpa/inet.h>
 #include <netinet/in.h>
@@ -61,12 +63,19 @@ int server_init(void) {
        printf("Using BSD TCP/IP\n");
        int i;
        for(i = 1; i < argc; i++) {
-               if(argv[i][0] == '/' || argv[i][0] == '-'){
-                       if(option(argv[i], "p", "port")){
+               if(argv[i][0] == '/' || argv[i][0] == '-') {
+                       if(option(argv[i], "p", "port")) {
                                i++;
                                port = atoi(argv[i]);
+                       } else if(option(argv[i], "l", "login")) {
+                               i++;
+                               authfile = argv[i];
+                               auth = true;
+                       } else {
+                               fprintf(stderr, "Invalid option: %s\n", argv[i]);
+                               return 1;
                        }
-               }else{
+               } else {
                        db = dataworks_database_open(argv[i]);
                }
        }
@@ -126,19 +135,19 @@ void server_loop(void) {
        }
 }
 
-char* readline_sock(int sock){
+char* readline_sock(int sock) {
        char cbuf[2];
        cbuf[1] = 0;
        char* buf = malloc(1);
        buf[0] = 0;
-       while(1){
+       while(1) {
                int l = recv(sock, cbuf, 1, 0);
-               if(l <= 0){
+               if(l <= 0) {
                        free(buf);
                        return NULL;
-               }else if(cbuf[0] == '\n'){
+               } else if(cbuf[0] == '\n') {
                        break;
-               }else if(cbuf[0] != '\r'){
+               } else if(cbuf[0] != '\r') {
                        char* tmp = buf;
                        buf = __dw_strcat(tmp, cbuf);
                        free(tmp);
@@ -147,9 +156,7 @@ char* readline_sock(int sock){
        return buf;
 }
 
-void disconnect(int sock){
-       close(sock);
-}
+void disconnect(int sock) { close(sock); }
 
 void writeline(int sock, const char* str) {
        char* snd = __dw_strcat(str, "\r\n");