From 7cd03c4bbaba01e6b3c46ef5eb61825b34e60643 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 24 Feb 2021 18:11:03 +0000 Subject: refactor code --- src/easydir.c | 41 ++---- src/easydir.h | 8 +- src/implementation.c | 34 ++--- src/main.c | 347 ++++++++++++++++++++++++++------------------------- 4 files changed, 205 insertions(+), 225 deletions(-) (limited to 'src') diff --git a/src/easydir.c b/src/easydir.c index 162bae7..6de77bc 100644 --- a/src/easydir.c +++ b/src/easydir.c @@ -5,43 +5,16 @@ #include #include +#include "easydir.h" #include "handerror.h" -int deleteFile(char *file_path) +int checkFileExist(char *source) { - char *arguments[] = {"rm", file_path, NULL}; - easyFork("rm", arguments); - - return 1; -} - -int deleteEmptyDir(char *dir_path) -{ - #if defined(DEBUG) - char *arguments[] = {"rmdir", "-p", dir_path, NULL}; - #else - char *arguments[] = {"rmdir", "-p", "--ignore-fail-on-non-empty", dir_path, NULL}; - #endif - easyFork("rmdir", arguments); - - return 1; -} - -int checkFileExist(char *path_to_file) -{ - FILE *pFile; - - pFile = fopen(path_to_file, "r+"); // r+ so that errno can equal EISDIR - if(pFile == NULL) { - if(errno == ENOENT) // file doesn't exist - return 0; - if(errno == EISDIR) // it's directory - return 2; - else callError(120); - } - fclose(pFile); - - return 1; + FILE *file = fopen(source, "r+"); // r+ so that errno can equal EISDIR + if(!file) + return errno == EISDIR ? F_ISDIR : F_NOEXIST; + fclose(file); + return F_NOEXIST; } char *fileCropLineFeed(char *path, char *text, int maxlen) diff --git a/src/easydir.h b/src/easydir.h index 4680709..0ba5677 100644 --- a/src/easydir.h +++ b/src/easydir.h @@ -1,8 +1,12 @@ #ifndef EASYDIR_H #define EASYDIR_H -int deleteFile(char *file_path); -int deleteEmptyDir(char *dir_path); +enum status_file { + F_SUCCESS, + F_NOEXIST, + F_ISDIR +}; + int checkFileExist(char *path_to_file); char *fileCropLineFeed(char *path, char *text, int maxlen); diff --git a/src/implementation.c b/src/implementation.c index 9b93704..17b2c9a 100644 --- a/src/implementation.c +++ b/src/implementation.c @@ -14,15 +14,13 @@ // GPG_PUBLICKEY_MAXLENGTH NNNN // == global var == -extern char *gPath_rootdir; // /home/[username]/.lockpassword/ extern char *gPath_subdir; // example: programming/github.com extern char *gPath_pass; // example: programming/github.com/joursoir.gpg static void copyText(char *password) { - size_t size = (strlen(gPath_rootdir) + 5 + 1) * sizeof(char); - char *simple_path = malloc(size); - snprintf(simple_path, size, "%s%s", gPath_rootdir, ".pass"); + char *simple_path = malloc(sizeof(char) * (5 + 1)); + strcpy(simple_path, ".pass"); if(getenv("DISPLAY") != NULL) { @@ -67,7 +65,7 @@ char *getGPGKey(char *dest, size_t size) { FILE *fileGPG = fopen(".gpg-key", "r"); if(fileGPG == NULL) { - if(errno == ENOENT) printError("error: No GPG key exists\n"); + if(errno == ENOENT) printError("error: No GPG key exists. Use \"lpass init\"."); callError(121); } @@ -107,19 +105,16 @@ void nonvisibleEnter(int status) { struct termios term_settings; tcgetattr(0, &term_settings); // get current settings - if(status == 1) { + if(status == 1) term_settings.c_lflag &= ~ECHO; // flag reset - } - else { + else term_settings.c_lflag |= ECHO; - } tcsetattr(0, TCSANOW, &term_settings); } void insertPass(char *add_path, char *password, int flag_copy) { - /* gPath_rootdir = /home/[username]/.lock-password/ - add_path = banks/france/[number] + /* add_path = banks/france/[number] gPath_pass = banks/france/[number].gpg gPath_subdir = banks/france */ @@ -177,10 +172,10 @@ char *typePass(char *text, char *dest, int minlen, int maxlen) int userEnterPassword(int minlen, int maxlen, char *path_insert, int flag_echo, int flag_copy) { - char *pass_one = (char *) malloc(sizeof(char) * maxlen); + char *pass_one = malloc(sizeof(char) * maxlen); int rvalue = 0; if(!flag_echo) { - char *pass_two = (char *) malloc(sizeof(char) * maxlen); + char *pass_two = malloc(sizeof(char) * maxlen); nonvisibleEnter(1); // change terminal work typePass("Type your password: ", pass_one, minlen, maxlen); @@ -227,26 +222,21 @@ static void clearStdinBuff() int getOverwriteAnswer(char *path) { - int buffSize = (strlen("Password for '' exists. Overwrite? (Y/N)") + strlen(path) + 1)* sizeof(char); - char *text = malloc(buffSize); - snprintf(text, buffSize, "Password for '%s' exists. Overwrite? (Y/N)", path); - printf("%s ", text); - int answer; + printf("Password for \"%s\" exists. Overwrite? (Y/N)\n", path); while((answer = fgetc(stdin))) { clearStdinBuff(); switch(answer) { case 'Y': - case 'y': { free(text); return 1; } + case 'y': return 1; case 'N': - case 'n': { free(text); return 0; } + case 'n': return 0; case EOF: printError("Error: Unexpected end of file\n"); - default: { printf("%s ", text); break; } + default: { printf("Overwrite? (Y/N) "); break; } } } - free(text); return -1; } diff --git a/src/main.c b/src/main.c index 595aa80..7cffde2 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include "easydir.h" #include "handerror.h" @@ -26,8 +28,8 @@ #define MINLEN_PASSWORD 1 #define MAXLEN_PASSWORD 128 #define STANDARD_AMOUNT_GENERATE_SYMBOLS 14 -#define LOCKPASS_DIR "/.lock-password/" -#define GPGKEY_FILE "/.gpg-key" +#define LOCKPASS_DIR ".lock-password/" +#define GPGKEY_FILE ".gpg-key" #define TREE_OUTPUT_FILE ".tree" #define TEXTEDITOR_FILE ".text-editor" @@ -66,7 +68,6 @@ static struct cmd_struct commands[] = { }; // == global var == -char *gPath_rootdir = NULL; // /home/[username]/.lockpassword/ char *gPath_subdir = NULL; // example: programming/github.com char *gPath_pass = NULL; // example: programming/github.com/joursoir.gpg @@ -92,26 +93,14 @@ int cmd_init(int argc, char *argv[]) char *gpg_key = argv[2]; if(gpg_key == NULL) usageprint("%s", description); - // create main directory: - int len_init_storage = strlen(gPath_rootdir) + strlen(GPGKEY_FILE) + 1; // +1 for '\0' - char *path_init_storage = (char *) malloc(sizeof(char) * len_init_storage); - strcpy(path_init_storage, gPath_rootdir); - - char *arguments[] = {"mkdir", "-vp", path_init_storage, NULL}; - easyFork("mkdir", arguments); - - strcat(path_init_storage, GPGKEY_FILE); - // create .gpg-key in storage - FILE *filekey; - filekey = fopen(path_init_storage, "w"); - if(filekey == NULL) { - callError(122); - } + FILE *filekey = fopen(GPGKEY_FILE, "w"); + if(!filekey) + errprint("fopen() failed"); + fputs(gpg_key, filekey); fclose(filekey); - free(path_init_storage); printf("LockPassword initialized for %s\n", gpg_key); return 0; } @@ -120,52 +109,57 @@ int cmd_edit(int argc, char *argv[]) { const char description[] = "edit [-t=text-editor] passname\n"; const struct option long_options[] = { - {"text-editor", required_argument, NULL, 't'}, - {NULL, 0, NULL, 0} - }; - - int result; - while((result = getopt_long(argc, argv, "t:", long_options, NULL)) != -1) { - switch(result) { - case 't': - { - // create file, copy name text editor there + {"text-editor", required_argument, NULL, 't'}, + {NULL, 0, NULL, 0} + }; + + int result; + while((result = getopt_long(argc, argv, "t:", long_options, NULL)) != -1) { + switch(result) { + case 't': + { + // create file, copy name text editor there FILE *f_texteditor = fopen(TEXTEDITOR_FILE, "w"); - if(f_texteditor == NULL) callError(108); + if(!f_texteditor) + errprint("fopen() failed"); fputs(optarg, f_texteditor); fclose(f_texteditor); printf("You changed text editor to %s\n", optarg); - break; - } - default: usageprint("%s", description); - } - } - - if(optind < argc) optind++; // for skip "edit" - dbgprint("passname: %s\n", argv[optind]); - - char *path_to_password; - if(argv[optind] == NULL) usageprint("%s", description); - else path_to_password = argv[optind]; + break; + } + default: usageprint("%s", description); + } + } + + if(optind < argc) optind++; // for skip "edit" + dbgprint("passname: %s\n", argv[optind]); + + if(argv[optind] == NULL) + usageprint("%s", description); + char *path_to_password = argv[optind]; checkForbiddenPaths(path_to_password); globalSplitPath(path_to_password); - if(checkFileExist(gPath_pass) != 1) + result = checkFileExist(gPath_pass); + if(result != F_SUCCESS) { + if(result == F_ISDIR) errprint("It is a directory\n"); errprint("No such file exists\n"); + } // configure text editor file char text_editor[MAXLEN_TEXTEDITOR]; FILE *f_texteditor = fopen(TEXTEDITOR_FILE, "r"); if(f_texteditor == NULL) { f_texteditor = fopen(TEXTEDITOR_FILE, "w"); - if(f_texteditor == NULL) callError(108); + if(f_texteditor == NULL) + errprint("fopen() failed"); fputs(STANDARD_TEXTEDITOR, f_texteditor); // in file strcpy(text_editor, STANDARD_TEXTEDITOR); // in variable } else { if(!fgets(text_editor, sizeof(char)*MAXLEN_TEXTEDITOR, f_texteditor)) - callError(122); + errprint("fgets() failed"); } fclose(f_texteditor); @@ -207,40 +201,46 @@ int cmd_move(int argc, char *argv[]) /* we have a two situation: 1) mv file file 2) mv file directory */ - + const char description[] = "mv [-f] old-path new-path\n"; const struct option long_options[] = { - {"force", no_argument, NULL, 'f'}, - {NULL, 0, NULL, 0} - }; - - int result, flag_force = 0; - while((result = getopt_long(argc, argv, "f", long_options, NULL)) != -1) { - switch(result) { - case 'f': { flag_force = 1; break; } - default: usageprint("%s", description); - } - } - - if(optind < argc) optind++; // for skip "move" - if(!argv[optind] || !argv[optind+1]) - usageprint("%s", description); - dbgprint("old-path: %s\n", argv[optind]); - dbgprint("new-path: %s\n", argv[optind+1]); + {"force", no_argument, NULL, 'f'}, + {NULL, 0, NULL, 0} + }; + + int result, flag_force = 0; + while((result = getopt_long(argc, argv, "f", long_options, NULL)) != -1) { + switch(result) { + case 'f': { flag_force = 1; break; } + default: usageprint("%s", description); + } + } + + if(optind < argc) optind++; // for skip "move" + if(!argv[optind] || !argv[optind+1]) + usageprint("%s", description); + dbgprint("old-path: %s\n", argv[optind]); + dbgprint("new-path: %s\n", argv[optind+1]); char *old_path = argv[optind]; - checkForbiddenPaths(old_path); globalSplitPath(old_path); - if(checkFileExist(gPath_pass) != 1) errprint("No such old-path exists\n"); + checkForbiddenPaths(old_path); + globalSplitPath(old_path); + result = checkFileExist(gPath_pass); + if(result != F_SUCCESS) { + if(result == F_ISDIR) errprint("It is a directory\n"); + errprint("No such file exists\n"); + } char *old_path_gpg = gPath_pass; char *old_path_subdir = gPath_subdir; char *new_path = argv[optind+1]; - checkForbiddenPaths(new_path); globalSplitPath(new_path); + checkForbiddenPaths(new_path); + globalSplitPath(new_path); - if(checkFileExist(new_path) == 2) // if new-path = dir + if(checkFileExist(new_path) == F_ISDIR) ; - else if(checkFileExist(gPath_pass) == 1) { // if new-path = file + else if(checkFileExist(gPath_pass) == F_SUCCESS) { if(!flag_force) { if(getOverwriteAnswer(new_path) != 1) return 1; @@ -252,8 +252,9 @@ int cmd_move(int argc, char *argv[]) char *arguments[] = {"mv", "-f", old_path_gpg, new_path, NULL}; easyFork("mv", arguments); - deleteEmptyDir(old_path_subdir); - free(old_path_subdir); free(old_path_gpg); + rmdir(old_path_subdir); + free(old_path_subdir); + free(old_path_gpg); return 0; } @@ -263,36 +264,36 @@ int cmd_generate(int argc, char *argv[]) int pass_length = STANDARD_AMOUNT_GENERATE_SYMBOLS; int flag_force = 0, flag_copy = 0, result; const struct option long_options[] = { - {"length", required_argument, NULL, 'l'}, - {"force", no_argument, NULL, 'f'}, - {"copy", no_argument, NULL, 'c'}, - {NULL, 0, NULL, 0} - }; - - while((result = getopt_long(argc, argv, "l:fc", long_options, NULL)) != -1) { - switch(result) { - // if optarg - incorrect number, atoi return 0 - case 'l': { pass_length = atoi(optarg); break; } - case 'f': { flag_force = 1; break; } - case 'c': { flag_copy = 1; break; } - default: usageprint("%s", description); - } - } - - if(optind < argc) optind++; // for skip "generate" - dbgprint("passname: %s\n", argv[optind]); - - char *path_to_password; - if(argv[optind] == NULL) usageprint("%s", description); - else path_to_password = argv[optind]; - - if(pass_length < MINLEN_PASSWORD || pass_length > MAXLEN_PASSWORD) + {"length", required_argument, NULL, 'l'}, + {"force", no_argument, NULL, 'f'}, + {"copy", no_argument, NULL, 'c'}, + {NULL, 0, NULL, 0} + }; + + while((result = getopt_long(argc, argv, "l:fc", long_options, NULL)) != -1) { + switch(result) { + // if optarg - incorrect number, atoi return 0 + case 'l': { pass_length = atoi(optarg); break; } + case 'f': { flag_force = 1; break; } + case 'c': { flag_copy = 1; break; } + default: usageprint("%s", description); + } + } + + if(optind < argc) optind++; // for skip "generate" + dbgprint("passname: %s\n", argv[optind]); + + if(argv[optind] == NULL) + usageprint("%s", description); + char *path_to_password = argv[optind]; + + if(pass_length < MINLEN_PASSWORD || pass_length > MAXLEN_PASSWORD) errprint("You typed an incorrect number\n"); checkForbiddenPaths(path_to_password); globalSplitPath(path_to_password); - if(checkFileExist(gPath_pass) == 1) { + if(checkFileExist(gPath_pass) == F_SUCCESS) { if(!flag_force) { if(getOverwriteAnswer(path_to_password) != 1) return 1; @@ -314,39 +315,40 @@ int cmd_insert(int argc, char *argv[]) const char description[] = "insert [-ecf] passname\n"; int flag_echo = 0, flag_force = 0, flag_copy = 0, result; const struct option long_options[] = { - {"echo", no_argument, NULL, 'e'}, - {"force", no_argument, NULL, 'f'}, - {"copy", no_argument, NULL, 'c'}, - {NULL, 0, NULL, 0} - }; - - while((result = getopt_long(argc, argv, "efc", long_options, NULL)) != -1) { - switch(result) { - case 'e': { flag_echo = 1; break; } - case 'f': { flag_force = 1; break; } - case 'c': { flag_copy = 1; break; } - default: usageprint("%s", description); - } - } - - if(optind < argc) optind++; // for skip "insert" - dbgprint("passname: %s\n", argv[optind]); - - char *path_to_password; - if(argv[optind] == NULL) usageprint("%s", description); - else path_to_password = argv[optind]; + {"echo", no_argument, NULL, 'e'}, + {"force", no_argument, NULL, 'f'}, + {"copy", no_argument, NULL, 'c'}, + {NULL, 0, NULL, 0} + }; + + while((result = getopt_long(argc, argv, "efc", long_options, NULL)) != -1) { + switch(result) { + case 'e': { flag_echo = 1; break; } + case 'f': { flag_force = 1; break; } + case 'c': { flag_copy = 1; break; } + default: usageprint("%s", description); + } + } + + if(optind < argc) optind++; // for skip "insert" + dbgprint("passname: %s\n", argv[optind]); + + if(argv[optind] == NULL) + usageprint("%s", description); + char *path_to_password = argv[optind]; checkForbiddenPaths(path_to_password); globalSplitPath(path_to_password); - if(checkFileExist(gPath_pass) == 1) { + if(checkFileExist(gPath_pass) == F_SUCCESS) { if(!flag_force) { if(getOverwriteAnswer(path_to_password) != 1) return 1; } } - if(userEnterPassword(MINLEN_PASSWORD, MAXLEN_PASSWORD, path_to_password, flag_echo, flag_copy) == 1) { + if(userEnterPassword(MINLEN_PASSWORD, MAXLEN_PASSWORD, + path_to_password, flag_echo, flag_copy) == 1) { printf("Password added successfully for %s\n", path_to_password); } else @@ -357,18 +359,22 @@ int cmd_insert(int argc, char *argv[]) int cmd_remove(int argc, char *argv[]) { const char description[] = "rm passname\n"; - char *path_to_password = argv[2]; - if(path_to_password == NULL) + int res; + char *path = argv[2]; + if(!path) usageprint("%s", description); - checkForbiddenPaths(path_to_password); - globalSplitPath(path_to_password); + checkForbiddenPaths(path); + globalSplitPath(path); - if(checkFileExist(gPath_pass) != 1) + res = checkFileExist(gPath_pass); + if(res != F_SUCCESS) { + if(res == F_ISDIR) errprint("It is a directory\n"); errprint("No such file exists\n"); + } - if(deleteFile(gPath_pass)) - deleteEmptyDir(gPath_subdir); + if(unlink(gPath_pass) == 0) + rmdir(gPath_subdir); return 0; } @@ -378,27 +384,26 @@ int cmd_showtree(int argc, char *argv[]) int flag_copy = 0, result; char *path; const struct option long_options[] = { - {"copy", no_argument, NULL, 'c'}, - {NULL, 0, NULL, 0} - }; - - while((result = getopt_long(argc, argv, "c", long_options, NULL)) != -1) { - switch(result) { - case 'c': { flag_copy = 1; break; } - default: usageprint("%s", description); - } - } - - if(!argv[optind]) { - if(flag_copy) - usageprint("%s", description); - else { - path = (char *) malloc(sizeof(char) * 2); - strcpy(path, "."); - } - } - else path = argv[optind]; - checkForbiddenPaths(path); + {"copy", no_argument, NULL, 'c'}, + {NULL, 0, NULL, 0} + }; + + while((result = getopt_long(argc, argv, "c", long_options, NULL)) != -1) { + switch(result) { + case 'c': { flag_copy = 1; break; } + default: usageprint("%s", description); + } + } + + if(argv[optind]) { + path = malloc(sizeof(char) * (strlen(argv[optind]) + 1)); + strcpy(path, argv[optind]); + checkForbiddenPaths(path); + } + else { + path = malloc(sizeof(char) * 2); + strcpy(path, "."); + } if(opendir(path)) // if it's directory { @@ -415,7 +420,7 @@ int cmd_showtree(int argc, char *argv[]) else printf("Password Manager/%s\n", path); char *arg3[] = {"tail", "-n", "+2", TREE_OUTPUT_FILE, NULL}; - easyFork("tail", arg3); // remove working directory + easyFork("tail", arg3); // remove working directory from output remove(TREE_OUTPUT_FILE); } @@ -423,7 +428,7 @@ int cmd_showtree(int argc, char *argv[]) { globalSplitPath(path); - if(checkFileExist(gPath_pass) == 1) // exist + if(checkFileExist(gPath_pass) == F_SUCCESS) { char password[MAXLEN_PASSWORD]; getPassword(path, password, sizeof(char)*MAXLEN_PASSWORD, flag_copy); @@ -432,7 +437,7 @@ int cmd_showtree(int argc, char *argv[]) else errprint("%s is not in the password storage\n", path); } - if(argv[1] == NULL) free(path); + free(path); return 0; } @@ -483,40 +488,48 @@ static struct cmd_struct *get_cmd(const char *name) return NULL; } +static int gotoMainDir() +{ + char *env_home = getenv("HOME"); + int len_rootdir = strlen(env_home) + 1 + strlen(LOCKPASS_DIR) + 1; + char *rootdir = malloc(sizeof(char) * len_rootdir); + sprintf(rootdir, "%s/%s", env_home, LOCKPASS_DIR); + + if(chdir(rootdir)) // failed + { + // create main directory: + if(mkdir(rootdir, S_IRWXU)) { + if(errno != EEXIST) + errprint("mkdir() failed\n"); + } + + // try again: + return chdir(rootdir); + } + + return 0; +} + int main(int argc, char *argv[]) { if(!isatty(STDIN_FILENO)) errprint("Please, use a terminal to run this application\n"); - /* init global path to root directory */ - int len_rootdir = strlen(getenv("HOME")) + strlen(LOCKPASS_DIR) + 1; // +1 for '\0' - - gPath_rootdir = (char *) malloc(sizeof(char) * len_rootdir); - strcpy(gPath_rootdir, getenv("HOME")); - strcat(gPath_rootdir, LOCKPASS_DIR); - /* end init */ + if(gotoMainDir()) + errprint("chdir() failed\n"); + int ret = 0; char *cmd = (argv[1] != NULL) ? argv[1] : ""; struct cmd_struct *ptr; - - int dir = chdir(gPath_rootdir); - - int ret = 0; if((ptr = get_cmd(cmd))) ret = ptr->func(argc, argv); - else { - if(dir != 0) { - errprint("Before starting work, you must initialize LockPassword\n"); - return 1; - } + else ret = cmd_showtree(argc, argv); - } if(gPath_subdir != NULL) { free(gPath_subdir); free(gPath_pass); } - free(gPath_rootdir); return ret; } \ No newline at end of file -- cgit v1.2.3-18-g5258