#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);
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
}
}
printf("\r");
- if(signals > 0){
+ if(signals > 0) {
free(buf);
- if(connected){
+ if(connected) {
writeline(0, "QUIT:Bye");
disconnect(0);
}
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;
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]);
}
}
}
}
-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");
free(snd);
}
-void disconnect(int sock){
+void disconnect(int sock) {
while(inp(get_ioport() + 6) & (1 << 7)) outp(get_ioport() + 4, 0);
connected = false;
}
#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
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=");
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;
}
extern int argc;
extern char** argv;
+extern bool auth;
+extern char* authfile;
#include <arpa/inet.h>
#include <netinet/in.h>
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]);
}
}
}
}
-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);
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");