From e06f9fcb689878878469e2396617786ed525ca87 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Tue, 23 Mar 2021 17:32:33 +0000 Subject: refactor; set correct headers guard --- src/constants.h | 2 +- src/easydir.h | 6 +- src/exec-cmd.c | 2 +- src/implementation.c | 217 --------------------------------------------------- src/implementation.h | 34 -------- src/r-gpgme.c | 11 ++- src/routines.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++++ src/routines.h | 34 ++++++++ src/xstd.h | 2 +- 9 files changed, 256 insertions(+), 263 deletions(-) delete mode 100644 src/implementation.c delete mode 100644 src/implementation.h create mode 100644 src/routines.c create mode 100644 src/routines.h diff --git a/src/constants.h b/src/constants.h index cc15a86..0843b5b 100644 --- a/src/constants.h +++ b/src/constants.h @@ -51,7 +51,7 @@ #endif enum { - maxlen_texteditor = 16, + maxlen_fingerprint = 256, minlen_pass = 1, maxlen_pass = 128, stdlen_pass = 14 diff --git a/src/easydir.h b/src/easydir.h index 292002e..c9cab57 100644 --- a/src/easydir.h +++ b/src/easydir.h @@ -1,5 +1,5 @@ -#ifndef EASYDIR_H -#define EASYDIR_H +#ifndef LPASS_EASYDIR_H +#define LPASS_EASYDIR_H /*** This file is part of LockPassword @@ -28,4 +28,4 @@ enum status_file { int file_exist(const char *path); int count_dir_entries(const char *path); -#endif \ No newline at end of file +#endif /* LPASS_EASYDIR_H */ \ No newline at end of file diff --git a/src/exec-cmd.c b/src/exec-cmd.c index 6aced93..4ec1c10 100644 --- a/src/exec-cmd.c +++ b/src/exec-cmd.c @@ -29,7 +29,7 @@ #include "exec-cmd.h" #include "constants.h" #include "easydir.h" -#include "implementation.h" +#include "routines.h" #include "exec-cmd.h" #include "tree.h" diff --git a/src/implementation.c b/src/implementation.c deleted file mode 100644 index af59b21..0000000 --- a/src/implementation.c +++ /dev/null @@ -1,217 +0,0 @@ -/*** - This file is part of LockPassword - Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -***/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "implementation.h" -#include "constants.h" -#include "easydir.h" -#include "r-gpgme.h" -#if defined(DISPLAY) - #include "r-x11.h" -#endif - -/* define in implementation.h */ -// GPG_PUBLICKEY_MAXLENGTH NNNN - -int copy_outside(char *password) -{ - #if defined(DISPLAY) - int pid; - pid = fork(); - if(pid == -1) - errprint_r(1, "X11 fork() failed\n"); - if(pid == 0) /* new process */ - exit(run_clipboard(password)); - return 0; - #endif - - if(getenv("WAYLAND_DISPLAY") != NULL) { - char * const wl_copy[] = {"wl-copy", password, NULL}; - int pid; - pid = fork(); - if(pid == -1) - errprint_r(1, "Wayland fork() failed\n"); - if(pid == 0) { /* new process */ - execvp("wl-copy", wl_copy); - perror("wl-copy"); - exit(1); - } - - return 0; - } - - errprint_r(1, "You didn't have x11 or wayland when app builded\n"); -} - -/* check two dot in path */ -int check_sneaky_paths(const char *path) -{ - int length = strlen(path), i; - for(i = 1; i < length; i++) - { - if(path[i-1] == '.' && path[i] == '.') - return 1; - } - return 0; -} - -char *get_pubkey() -{ - int size_key = sizeof(char) * GPG_PUBLICKEY_MAXLENGTH; - char *pubkey = malloc(size_key + sizeof(char)); - - FILE *fileGPG = fopen(".gpg-key", "r"); - if(fileGPG == NULL) { - free(pubkey); - if(errno == ENOENT) - errprint_r(NULL, "No GPG key exists. Use \"lpass init\"."); - perror(".gpg-key"); - return NULL; - } - - if(!fgets(pubkey, size_key, fileGPG)) { - free(pubkey); - pubkey = NULL; - } - fclose(fileGPG); - - return pubkey; -} - -char *get_password(const char *path) -{ - return decrypt_data(path); -} - -void visible_enter(int status) -{ - struct termios term_settings; - tcgetattr(0, &term_settings); // get current settings - if(status == 1) - term_settings.c_lflag |= ECHO; - else - term_settings.c_lflag &= ~ECHO; - tcsetattr(0, TCSANOW, &term_settings); -} - -static void mkdir_recursive(const char *path) -{ - char *subpath, *fullpath; - - fullpath = strdup(path); - subpath = dirname(fullpath); - if(subpath[0] != '.') - mkdir_recursive(subpath); - - mkdir(path, S_IRWXU); - free(fullpath); -} - -int insert_pass(const char *path, const char *password) -{ - char *public_gpgkey = get_pubkey(); - if(public_gpgkey == NULL) - return 1; - - // create directories - char *tmp, *dirs_path; - tmp = strdup(path); - dirs_path = dirname(tmp); - if(dirs_path[0] != '.') - mkdir_recursive(dirs_path); - free(tmp); - - int ret = ecnrypt_data(path, password, public_gpgkey); - free(public_gpgkey); - return ret; -} - -char *get_input(int minlen, int maxlen) -{ - size_t size = sizeof(char) * maxlen; - char *pass = malloc(size + sizeof(char)); // +1 for '\0' - int len; - - if(fgets(pass, size, stdin) == NULL) { - free(pass); - return NULL; - } - - len = strlen(pass); - if(len < minlen) { - free(pass); - return NULL; - } - - if(pass[len-1] == '\n') - pass[len-1] = '\0'; - - return pass; -} - -char *gen_password(int length) -{ - int i, min = 33, max = 126; - char *password = malloc(sizeof(char) * (length + 1)); - - srand(time(NULL)); - for(i = 0; i < length; i++) - password[i] = min + rand() % (max-min); - - return password; -} - -static void clear_stdin() -{ - int garbage; - while( (garbage = fgetc(stdin)) != '\n' && garbage != EOF ) - ; -} - -int overwrite_answer(const char *path) -{ - int answer; - printf("Password for \"%s\" exists. Overwrite? (Y/N)\n", path); - while((answer = fgetc(stdin))) - { - clear_stdin(); - switch(answer) - { - case 'Y': - case 'y': return 'y'; - case 'N': - case 'n': - case EOF: return 'n'; - default: { - printf("Overwrite? (Y/N) "); - break; - } - } - } - - return 'n'; -} diff --git a/src/implementation.h b/src/implementation.h deleted file mode 100644 index 467da6f..0000000 --- a/src/implementation.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef IMPLEMENTATION_H -#define IMPLEMENTATION_H - -/*** - This file is part of LockPassword - Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -***/ - -#define GPG_PUBLICKEY_MAXLENGTH 1024 - -int copy_outside(char *password); -int check_sneaky_paths(const char *path); -char *get_pubkey(); -char *get_password(const char *path); -void visible_enter(int status); -int insert_pass(const char *path, const char *password); -char *get_input(int minlen, int maxlen); -char *gen_password(int length); -int overwrite_answer(const char *path); - -#endif \ No newline at end of file diff --git a/src/r-gpgme.c b/src/r-gpgme.c index a13a62a..586bd55 100644 --- a/src/r-gpgme.c +++ b/src/r-gpgme.c @@ -23,10 +23,9 @@ #include #include +#include "constants.h" #include "r-gpgme.h" -#define BUF_SIZE 128 - #ifdef DEBUG #define ret_if_err(ret, err) \ do { \ @@ -79,7 +78,7 @@ static int init_ctx(gpgme_ctx_t ctx, gpgme_protocol_t protocol) static int loop_read(const char *path, gpgme_data_t dh) { - char buf[BUF_SIZE]; + char buf[maxlen_pass]; int ret; FILE *f = fopen(path, "w"); @@ -91,7 +90,7 @@ static int loop_read(const char *path, gpgme_data_t dh) fclose(f); ret_if_err(1, gpgme_err_code_from_errno(errno)); } - while((ret = gpgme_data_read(dh, buf, BUF_SIZE)) > 0) + while((ret = gpgme_data_read(dh, buf, maxlen_pass)) > 0) fwrite(buf, ret, 1, f); if(ret < 0) { fclose(f); @@ -165,8 +164,8 @@ char *decrypt_data(const char *path) if(ret) ret_if_err(NULL, gpgme_err_code_from_errno(errno)); - char *data = malloc(sizeof(char) * (BUF_SIZE + 1)); - gpgme_data_read(plain, data, BUF_SIZE); + char *data = malloc(sizeof(char) * (maxlen_pass + 1)); + gpgme_data_read(plain, data, maxlen_pass); gpgme_data_release(plain); gpgme_data_release(cipher); diff --git a/src/routines.c b/src/routines.c new file mode 100644 index 0000000..f6b6bcc --- /dev/null +++ b/src/routines.c @@ -0,0 +1,211 @@ +/*** + This file is part of LockPassword + Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +***/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "routines.h" +#include "constants.h" +#include "easydir.h" +#include "r-gpgme.h" +#if defined(DISPLAY) + #include "r-x11.h" +#endif + +int copy_outside(char *password) +{ + #if defined(DISPLAY) + int pid; + pid = fork(); + if(pid == -1) + errprint_r(1, "X11 fork() failed\n"); + if(pid == 0) /* new process */ + exit(run_clipboard(password)); + return 0; + #endif + + if(getenv("WAYLAND_DISPLAY") != NULL) { + char * const wl_copy[] = {"wl-copy", password, NULL}; + int pid; + pid = fork(); + if(pid == -1) + errprint_r(1, "Wayland fork() failed\n"); + if(pid == 0) { /* new process */ + execvp("wl-copy", wl_copy); + perror("wl-copy"); + exit(1); + } + + return 0; + } + + errprint_r(1, "You didn't have x11 or wayland when app builded\n"); +} + +/* check two dot in path */ +int check_sneaky_paths(const char *path) +{ + int length = strlen(path), i; + for(i = 1; i < length; i++) + { + if(path[i-1] == '.' && path[i] == '.') + return 1; + } + return 0; +} + +char *get_pubkey() +{ + char *pubkey; + + FILE *fileGPG = fopen(".gpg-key", "r"); + if(fileGPG == NULL) { + if(errno == ENOENT) + errprint_r(NULL, "No GPG key exists. Use \"lpass init\"\n"); + errprint_r(NULL, "%s\n", strerror(errno)); + } + + pubkey = malloc(sizeof(char) * (maxlen_fingerprint + 1)); + if(fgets(pubkey, maxlen_fingerprint + 1, fileGPG) == NULL) { + free(pubkey); + errprint_ptr(&pubkey, NULL, "%s\n", strerror(errno)); + } + + fclose(fileGPG); + return pubkey; +} + +char *get_password(const char *path) +{ + return decrypt_data(path); +} + +void visible_enter(int status) +{ + struct termios term_settings; + tcgetattr(0, &term_settings); // get current settings + if(status == 1) + term_settings.c_lflag |= ECHO; + else + term_settings.c_lflag &= ~ECHO; + tcsetattr(0, TCSANOW, &term_settings); +} + +static void mkdir_recursive(const char *path) +{ + char *subpath, *fullpath; + + fullpath = strdup(path); + subpath = dirname(fullpath); + if(subpath[0] != '.') + mkdir_recursive(subpath); + + mkdir(path, S_IRWXU); + free(fullpath); +} + +int insert_pass(const char *path, const char *password) +{ + char *public_gpgkey = get_pubkey(); + if(public_gpgkey == NULL) + return 1; + + // create directories + char *tmp, *dirs_path; + tmp = strdup(path); + dirs_path = dirname(tmp); + if(dirs_path[0] != '.') + mkdir_recursive(dirs_path); + free(tmp); + + int ret = ecnrypt_data(path, password, public_gpgkey); + free(public_gpgkey); + return ret; +} + +char *get_input(int minlen, int maxlen) +{ + char *pass = malloc(sizeof(char) * (maxlen + 1)); + int len; + + if(fgets(pass, maxlen + 1, stdin) == NULL) { + free(pass); + errprint_ptr(&pass, NULL, "%s\n", strerror(errno)); + } + + len = strlen(pass); + if(len < minlen) { + free(pass); + return NULL; + } + + if(pass[len-1] == '\n') + pass[len-1] = '\0'; + + return pass; +} + +char *gen_password(int length) +{ + int i, min = 33, max = 126; + char *password = malloc(sizeof(char) * (length + 1)); + + srand(time(NULL)); + for(i = 0; i < length; i++) + password[i] = min + rand() % (max-min); + + return password; +} + +static void clear_stdin() +{ + int garbage; + while( (garbage = fgetc(stdin)) != '\n' && garbage != EOF ) + ; +} + +int overwrite_answer(const char *path) +{ + int answer; + printf("Password for \"%s\" exists. Overwrite? (Y/N)\n", path); + while((answer = fgetc(stdin))) + { + clear_stdin(); + switch(answer) + { + case 'Y': + case 'y': return 'y'; + case 'N': + case 'n': + case EOF: return 'n'; + default: { + printf("Overwrite? (Y/N) "); + break; + } + } + } + + return 'n'; +} diff --git a/src/routines.h b/src/routines.h new file mode 100644 index 0000000..90d6fb3 --- /dev/null +++ b/src/routines.h @@ -0,0 +1,34 @@ +#ifndef LPASS_ROUTINES_H +#define LPASS_ROUTINES_H + +/*** + This file is part of LockPassword + Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +***/ + +#define GPG_PUBLICKEY_MAXLENGTH 1024 + +int copy_outside(char *password); +int check_sneaky_paths(const char *path); +char *get_pubkey(); +char *get_password(const char *path); +void visible_enter(int status); +int insert_pass(const char *path, const char *password); +char *get_input(int minlen, int maxlen); +char *gen_password(int length); +int overwrite_answer(const char *path); + +#endif /* LPASS_ROUTINES_H */ \ No newline at end of file diff --git a/src/xstd.h b/src/xstd.h index 351069d..4683c3b 100644 --- a/src/xstd.h +++ b/src/xstd.h @@ -22,4 +22,4 @@ char *xstrcat(const char *first, const char *second, const char *delimiter); -#endif \ No newline at end of file +#endif /* LPASS_XSTD_H */ -- cgit v1.2.3-18-g5258