From: nishi Date: Thu, 13 Jun 2024 00:47:01 +0000 (+0000) Subject: add LOGIN X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=4ef8032ed411a67ca788f9dcbbdf0fa723c17237;p=dataworks.git add LOGIN git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@281 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Server/server.c b/Server/server.c index f0a4e53..6c40e8f 100644 --- a/Server/server.c +++ b/Server/server.c @@ -141,35 +141,75 @@ void protocol_loop(int sock) { break; } else if(__dw_strcaseequ(buf, "USER")) { if(auth) { - if(login){ + if(login) { writeline(sock, "ERROR:ALREADY_IN"); - }else{ - if(has_arg){ - writeline(sock, "REQPASS"); - if(user != NULL){ - free(user); + } else { + if(has_arg) { + int j; + bool found_user = false; + for(j = 0; entries[j] != NULL; j++) { + if(strcmp(entries[j]->user, buf + i + 1) == 0) { + writeline(sock, "REQPASS"); + if(user != NULL) { + free(user); + } + user = __dw_strdup(buf + i + 1); + found_user = true; + break; + } + } + if(!found_user) { + writeline(sock, "ERROR:NO_SUCH_USER"); } - user = __dw_strdup(buf + i + 1); - }else{ + } else { writeline(sock, "ERROR:NEED_MORE_ARGS"); } } } else { writeline(sock, "ERROR:NO_AUTH"); } + } else if(__dw_strcaseequ(buf, "LOGOUT")) { + if(auth) { + if(login) { + login = false; + free(user); + user = NULL; + writeline(sock, "LOGOUT"); + } else { + writeline(sock, "ERROR:NOT_IN"); + } + } else { + writeline(sock, "ERROR:NO_AUTH"); + } } else if(__dw_strcaseequ(buf, "PASS")) { if(auth) { - if(login){ + if(login) { writeline(sock, "ERROR:ALREADY_IN"); - }else if(user == NULL){ + } else if(user == NULL) { writeline(sock, "ERROR:NEED_USERNAME"); - }else{ - if(has_arg){ + } else { + if(has_arg) { int j; - for(j = 0; entries[j] != NULL; j++){ - printf("%s\n", entries[j]->pass); + bool found_user = false; + for(j = 0; entries[j] != NULL; j++) { + if(strcmp(entries[j]->user, user) == 0) { + found_user = true; + if(strcmp(entries[j]->pass, buf + i + 1) == 0) { + writeline(sock, "LOGIN"); + login = true; + break; + } else { + writeline(sock, "ERROR:INVALID_PASSWORD"); + free(user); + user = NULL; + break; + } + } + } + if(!found_user) { + writeline(sock, "ERROR:NO_SUCH_USER"); } - }else{ + } else { writeline(sock, "ERROR:NEED_MORE_ARGS"); } }