aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2020-10-26 19:11:16 +0000
committerJoursoir <chat@joursoir.net>2020-10-26 19:11:16 +0000
commit101dd376b32c2cd12640e81b93c2f30975baac7f (patch)
tree8f571472aefb58274755e3dd6778497564554edc
parent5f3bbf1279d39554cd2d185170ae4e5119cf3b61 (diff)
downloadlock-password-101dd376b32c2cd12640e81b93c2f30975baac7f.tar.gz
lock-password-101dd376b32c2cd12640e81b93c2f30975baac7f.tar.bz2
lock-password-101dd376b32c2cd12640e81b93c2f30975baac7f.zip
feature: edit command
-rw-r--r--easydir.c34
-rw-r--r--easydir.h1
-rw-r--r--implementation.c17
-rw-r--r--implementation.h5
-rw-r--r--main.c101
5 files changed, 142 insertions, 16 deletions
diff --git a/easydir.c b/easydir.c
index 08cac0f..9337fe1 100644
--- a/easydir.c
+++ b/easydir.c
@@ -42,3 +42,37 @@ int checkFileExist(char *path_to_file)
return 1;
}
+
+char *fileCropLineFeed(char *path, char *text, int maxlen)
+{
+ FILE *file = fopen(path, "r+");
+ if(file == NULL) callError(130);
+
+ int symbol;
+ int pos = 0;
+ char *str = (char *) malloc(sizeof(char) * maxlen);
+ while((symbol = fgetc(file)))
+ {
+ switch(symbol)
+ {
+ case '\n':
+ case EOF: {
+ str[pos] = '\0';
+ pos = -1; // for break while
+ break;
+ }
+ default: {
+ str[pos] = symbol;
+ pos++;
+ break;
+ }
+ }
+ if(pos == -1) break;
+ if(pos > maxlen-1) { str[pos-1] = '\0'; break; }
+ }
+ fclose(file);
+
+ strcpy(text, str);
+ free(str);
+ return text;
+}
diff --git a/easydir.h b/easydir.h
index 2191d6a..4680709 100644
--- a/easydir.h
+++ b/easydir.h
@@ -4,5 +4,6 @@
int deleteFile(char *file_path);
int deleteEmptyDir(char *dir_path);
int checkFileExist(char *path_to_file);
+char *fileCropLineFeed(char *path, char *text, int maxlen);
#endif \ No newline at end of file
diff --git a/implementation.c b/implementation.c
index 0a011ce..04af102 100644
--- a/implementation.c
+++ b/implementation.c
@@ -8,9 +8,10 @@
#include "handerror.h"
#include "easydir.h"
+#include "implementation.h"
-#define GPG_OUTPUT_FILE ".gap"
-#define GPG_PUBLICKEY_MAXLENGTH 1025 // +1 for '\0'
+/* define in implementation.h */
+// GPG_PUBLICKEY_MAXLENGTH 1025
// == global var ==
extern char *gPath_rootdir; // /home/[username]/.lockpassword/
@@ -28,7 +29,7 @@ void checkForbiddenPaths(char *path) // check two dot in path
}
}
-static char *getGPGKey(char *dest, size_t size)
+char *getGPGKey(char *dest, size_t size)
{
FILE *fileGPG = fopen(".gpg-key", "r");
if(fileGPG == NULL) {
@@ -44,16 +45,16 @@ static char *getGPGKey(char *dest, size_t size)
return dest;
}
-char* getPassword(char *path_pass, char *password, size_t size) // path_pass is not used
+char* getPassword(char *path_pass, char *password, size_t size)
{
int size_gpgkey = sizeof(char) * GPG_PUBLICKEY_MAXLENGTH;
char *secret_gpgkey = (char *) malloc(size_gpgkey);
getGPGKey(secret_gpgkey, size_gpgkey);
- char *arguments[] = {"gpg", "-d", "--quiet", "-r", secret_gpgkey, "-o", GPG_OUTPUT_FILE, gPath_pass, NULL};
+ char *arguments[] = {"gpg", "-d", "--quiet", "-r", secret_gpgkey, "-o", path_pass, gPath_pass, NULL};
easyFork("gpg", arguments);
- FILE *filePass = fopen(GPG_OUTPUT_FILE, "r");
+ FILE *filePass = fopen(path_pass, "r");
if(filePass == NULL) callError(127);
if(!fgets(password, size, filePass)) {
@@ -61,8 +62,8 @@ char* getPassword(char *path_pass, char *password, size_t size) // path_pass is
}
fclose(filePass);
+ remove(path_pass);
free(secret_gpgkey);
- remove(GPG_OUTPUT_FILE);
return password;
}
@@ -218,7 +219,7 @@ int getOverwriteAnswer(char *path)
snprintf(text, buffSize, "Password for '%s' exists. Overwrite? (Y/N)", path);
printf("%s ", text);
- char answer;
+ int answer;
while((answer = fgetc(stdin)))
{
clearStdinBuff();
diff --git a/implementation.h b/implementation.h
index c5d31e8..7ede6be 100644
--- a/implementation.h
+++ b/implementation.h
@@ -1,8 +1,11 @@
#ifndef IMPLEMENTATION_H
#define IMPLEMENTATION_H
+#define GPG_PUBLICKEY_MAXLENGTH 1025 // +1 for '\0'
+
void checkForbiddenPaths(char *path);
-char* getPassword(char *path, char *password, int maxlen);
+char *getGPGKey(char *dest, size_t size);
+char* getPassword(char *path_pass, char *password, size_t size);
void nonvisibleEnter(int status);
void insertPass(char *add_path, char *password);
char *typePass(char *text, char *dest, int minlen, int maxlen);
diff --git a/main.c b/main.c
index 07b8670..1381e5a 100644
--- a/main.c
+++ b/main.c
@@ -20,7 +20,9 @@
#define VERSION "1.0"
#define DATE_RELEASE "21 October, 2020"
-#define DEBUG
+//#define DEBUG
+#define STANDARD_TEXTEDITOR "vim"
+#define MAXLEN_TEXTEDITOR 16
#define MINLEN_PASSWORD 1
#define MAXLEN_PASSWORD 128
#define STANDARD_AMOUNT_GENERATE_SYMBOLS 14
@@ -28,6 +30,7 @@
#define GPGKEY_FILE "/.gpg-key"
#define TREE_OUTPUT_FILE ".tree"
+#define TEXTEDITOR_FILE ".text-editor"
#define HASH_INIT 6385337657
#define HASH_HELP 6385292014
@@ -43,10 +46,10 @@
#define HASH_DELETE 6953426453624
#define WITHOUT_ARGUMENTS 1
-#define STR_SHOWTREEUSE "Use: lpass [-c=passname] [passname]"
+#define STR_SHOWTREEUSE "Use: lpass [-c=passname] [passname]\n"
#define STR_INITUSE "Use: lpass init gpg-key\n"
#define STR_INSERTUSE "Use: lpass insert [-ef] passname\n"
-//#define STR_EDITUSE "Use: lpass edit [-e] passname\n"
+#define STR_EDITUSE "Use: lpass edit [-t=text-editor] passname\n"
#define STR_GENERATEUSE "Use: lpass generate [-l=pass-length] [-f] passname\n"
#define STR_REMOVEUSE "Use: lpass remove/rm/delete passname\n"
@@ -108,7 +111,91 @@ static void cmd_copy(int argc, char *argv[])
static void cmd_edit(int argc, char *argv[])
{
- printf("Coming soon...\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
+ FILE *f_texteditor = fopen(TEXTEDITOR_FILE, "w");
+ if(f_texteditor == NULL) callError(108);
+ fputs(optarg, f_texteditor);
+ fclose(f_texteditor);
+ printf("You changed text editor to %s\n", optarg);
+ break;
+ }
+ default: printError(STR_EDITUSE);
+ }
+ }
+
+ if(optind < argc) optind++; // for skip "edit"
+ #if defined(DEBUG)
+ for(int i=0; i < argc; i++) printf("arg: %s\n", argv[i]);
+ printf("passname: %s\n", argv[optind]);
+ #endif
+
+ char *path_to_password;
+ if(argv[optind] == NULL) printError(STR_EDITUSE);
+ else path_to_password = argv[optind];
+
+ checkForbiddenPaths(path_to_password);
+ globalSplitPath(path_to_password);
+
+ if(checkFileExist(gPath_pass) != 1)
+ printError("Error: 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);
+ 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);
+ }
+ fclose(f_texteditor);
+
+ #if defined(DEBUG)
+ printf("using text editor: %s\n", text_editor);
+ #endif
+ // end configure
+
+ // decryption
+ int size_gpgkey = sizeof(char) * GPG_PUBLICKEY_MAXLENGTH;
+ char *secret_gpgkey = (char *) malloc(size_gpgkey);
+ getGPGKey(secret_gpgkey, size_gpgkey);
+
+ char *decryp_arg[] = {"gpg", "-d", "--quiet", "-r", secret_gpgkey, "-o", path_to_password, gPath_pass, NULL};
+ easyFork("gpg", decryp_arg);
+
+ // start vim/etc for edit passowrd
+ char *texte_arg[] = {text_editor, path_to_password, NULL};
+ easyFork(text_editor, texte_arg);
+
+ // delete '\n' and paste good pass
+ char password[MAXLEN_PASSWORD];
+ fileCropLineFeed(path_to_password, password, MAXLEN_PASSWORD);
+
+ FILE *file = fopen(path_to_password, "w");
+ if(file == NULL) callError(108);
+ fputs(password, file);
+ fclose(file);
+
+ // encryption
+ char *encryp_arg[] = {"gpg", "--quiet", "--yes", "-r", secret_gpgkey, "-e", path_to_password, NULL};
+ easyFork("gpg", encryp_arg);
+
+ remove(path_to_password);
+ free(secret_gpgkey);
}
static void cmd_move(int argc, char *argv[])
@@ -159,7 +246,7 @@ static void cmd_generate(int argc, char *argv[])
if(checkFileExist(gPath_pass) == 1) {
if(!flag_force) {
if(getOverwriteAnswer(path_to_password) != 1)
- exit(EXIT_SUCCESS);
+ return;
}
}
@@ -205,7 +292,7 @@ static void cmd_insert(int argc, char *argv[])
if(checkFileExist(gPath_pass) == 1) {
if(!flag_force) {
if(getOverwriteAnswer(path_to_password) != 1)
- exit(EXIT_SUCCESS);
+ return;
}
}
@@ -224,7 +311,7 @@ static void cmd_remove(int argc, char *argv[])
checkForbiddenPaths(path_to_password);
globalSplitPath(path_to_password);
- if(checkFileExist(gPath_pass) == 0)
+ if(checkFileExist(gPath_pass) != 1)
printError("Error: No such file exists\n");
if(deleteFile(gPath_pass))