From 74fe98805051c9b9271bad2426abddcb33a69960 Mon Sep 17 00:00:00 2001 From: nishi Date: Fri, 7 Jun 2024 03:24:43 +0000 Subject: [PATCH] add stuff git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@167 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Server/hayes.c | 40 +++++++++-------- Server/server.c | 114 ++++++++++++++++++++++++++++++++++++++---------- Server/tcpip.c | 29 +++++++----- 3 files changed, 130 insertions(+), 53 deletions(-) diff --git a/Server/hayes.c b/Server/hayes.c index 4dcfb5f..200b127 100644 --- a/Server/hayes.c +++ b/Server/hayes.c @@ -31,18 +31,20 @@ #include #include +#include #include #include #include #include -#include extern int argc; extern char** argv; +extern bool auth; +extern char* authfile; #include -#include #include +#include 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; } diff --git a/Server/server.c b/Server/server.c index 35c2160..3acae32 100644 --- a/Server/server.c +++ b/Server/server.c @@ -31,14 +31,22 @@ #include #include +#include #include #include #include #include -#include 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; } diff --git a/Server/tcpip.c b/Server/tcpip.c index 923ad0c..c11ef0b 100644 --- a/Server/tcpip.c +++ b/Server/tcpip.c @@ -38,6 +38,8 @@ extern int argc; extern char** argv; +extern bool auth; +extern char* authfile; #include #include @@ -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"); -- 2.43.0