aboutsummaryrefslogtreecommitdiffstats
path: root/src/implementation.c
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-02-25 13:06:35 +0000
committerJoursoir <chat@joursoir.net>2021-02-25 13:06:35 +0000
commit9824208d05a2ba476cbb3f583fa2df3080a00967 (patch)
tree1518af407599d7287f9a03ef79675250b1cf35d4 /src/implementation.c
parent7cd03c4bbaba01e6b3c46ef5eb61825b34e60643 (diff)
downloadlock-password-9824208d05a2ba476cbb3f583fa2df3080a00967.tar.gz
lock-password-9824208d05a2ba476cbb3f583fa2df3080a00967.tar.bz2
lock-password-9824208d05a2ba476cbb3f583fa2df3080a00967.zip
add recursive mkdir, improve error handling
Diffstat (limited to 'src/implementation.c')
-rw-r--r--src/implementation.c150
1 files changed, 72 insertions, 78 deletions
diff --git a/src/implementation.c b/src/implementation.c
index 17b2c9a..334b7e1 100644
--- a/src/implementation.c
+++ b/src/implementation.c
@@ -1,10 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
+#include <libgen.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <time.h>
#include <errno.h>
+#include <sys/stat.h>
#include "handerror.h"
#include "easydir.h"
@@ -50,38 +52,46 @@ static void copyText(char *password)
#endif
}
-void checkForbiddenPaths(char *path) // check two dot in path
+/* check two dot in path */
+int checkForbiddenPaths(char *path)
{
+ int i, length;
int firstdot = 0;
- for(int i=0; i < strlen(path); i++)
+ for(i = 0, length = strlen(path); i < length; i++)
{
- if(path[i] == '.')
- firstdot ? printError("Error: please, don't use forbidden paths\n") : firstdot++;
+ if(path[i] == '.') {
+ if(firstdot)
+ return 1;
+ firstdot++;
+ }
else firstdot = 0;
}
+ return 0;
}
-char *getGPGKey(char *dest, size_t size)
+char *getGPGKey()
{
+ int size_gpgkey = sizeof(char) * GPG_PUBLICKEY_MAXLENGTH;
+ char *pub_gpgkey = malloc(size_gpgkey + sizeof(char));
+
FILE *fileGPG = fopen(".gpg-key", "r");
if(fileGPG == NULL) {
- if(errno == ENOENT) printError("error: No GPG key exists. Use \"lpass init\".");
+ if(errno == ENOENT)
+ printError("error: No GPG key exists. Use \"lpass init\".");
callError(121);
}
- if(!fgets(dest, size, fileGPG)) {
+ if(!fgets(pub_gpgkey, size_gpgkey, fileGPG)) {
callError(122);
}
fclose(fileGPG);
- return dest;
+ return pub_gpgkey;
}
char* getPassword(char *path_pass, char *password, size_t size, int flag_copy)
{
- int size_gpgkey = sizeof(char) * GPG_PUBLICKEY_MAXLENGTH;
- char *public_gpgkey = (char *) malloc(size_gpgkey);
- getGPGKey(public_gpgkey, size_gpgkey);
+ char *public_gpgkey = getGPGKey();
char *arguments[] = {"gpg", "-d", "--quiet", "-r", public_gpgkey, "-o", path_pass, gPath_pass, NULL};
easyFork("gpg", arguments);
@@ -112,90 +122,74 @@ void nonvisibleEnter(int status)
tcsetattr(0, TCSANOW, &term_settings);
}
-void insertPass(char *add_path, char *password, int flag_copy)
+static void mkdir_recursive(const char *path)
{
- /* add_path = banks/france/[number]
- gPath_pass = banks/france/[number].gpg
- gPath_subdir = banks/france */
+ char *subpath, *fullpath;
- int size_gpgkey = sizeof(char) * GPG_PUBLICKEY_MAXLENGTH;
- char *public_gpgkey = (char *) malloc(size_gpgkey);
- getGPGKey(public_gpgkey, size_gpgkey);
+ fullpath = strdup(path);
+ subpath = dirname(fullpath);
+ if(subpath[0] != '.')
+ mkdir_recursive(subpath);
- char *mkdir_arg[] = {"mkdir", "-p", gPath_subdir, NULL};
- easyFork("mkdir", mkdir_arg);
+ mkdir(path, S_IRWXU);
+ free(fullpath);
+}
+
+int insertPass(char *path, char *password, int flag_copy)
+{
+ char *public_gpgkey = getGPGKey();
+
+ // create directories
+ char *tmp, *dirs_path;
+ tmp = strdup(path);
+ dirs_path = dirname(tmp);
+ if(dirs_path[0] != '.')
+ mkdir_recursive(dirs_path);
+ free(tmp);
// create file, copy password there
- FILE *filePass;
- filePass = fopen(add_path, "w");
- if(filePass == NULL) {
- callError(108);
+ FILE *file_pass;
+ file_pass = fopen(path, "w");
+ if(file_pass == NULL) {
+ free(public_gpgkey);
+ return 1;
}
- fputs(password, filePass);
- fclose(filePass);
+ fputs(password, file_pass);
+ fclose(file_pass);
- if(flag_copy) copyText(password);
+ if(flag_copy)
+ copyText(password);
// encryption
- char *encrypt_arg[] = {"gpg", "--quiet", "--yes", "-r", public_gpgkey, "-e", add_path, NULL};
+ char *encrypt_arg[] = {"gpg", "--quiet", "--yes", "-r", public_gpgkey, "-e", path, NULL};
easyFork("gpg", encrypt_arg);
- remove(add_path);
+ remove(path);
free(public_gpgkey);
+ return 0;
}
-char *typePass(char *text, char *dest, int minlen, int maxlen)
+char *getInput(int minlen, int maxlen)
{
- printf("%s", text);
- if(fgets(dest, sizeof(char)*maxlen, stdin) == NULL) {
- nonvisibleEnter(0);
- printError("lpass: Unexpected end of file\n");
- }
+ size_t size = sizeof(char) * maxlen;
+ char *pass = malloc(size + sizeof(char)); // +1 for '\0'
+ int len;
- int len = strlen(dest);
- if(len < minlen || len > maxlen) {
- nonvisibleEnter(0);
- printError("lpass: incorrect password\n");
- }
-
- if(dest[len-1] == '\n') {
- dest[len-1] = '\0';
+ if(fgets(pass, size, stdin) == NULL) {
+ free(pass);
+ return NULL;
}
- #if defined(DEBUG)
- printf("%s", dest);
- #endif
-
- printf("\n"); // for new line
- return dest;
-}
-
-int userEnterPassword(int minlen, int maxlen, char *path_insert, int flag_echo, int flag_copy)
-{
- char *pass_one = malloc(sizeof(char) * maxlen);
- int rvalue = 0;
- if(!flag_echo) {
- char *pass_two = malloc(sizeof(char) * maxlen);
-
- nonvisibleEnter(1); // change terminal work
- typePass("Type your password: ", pass_one, minlen, maxlen);
- typePass("Type your password again: ", pass_two, minlen, maxlen);
- nonvisibleEnter(0);
-
- if(strcmp(pass_one, pass_two) == 0) {
- insertPass(path_insert, pass_one, flag_copy);
- rvalue = 1;
- }
- free(pass_two);
- }
- else {
- typePass("Type your password: ", pass_one, minlen, maxlen);
- insertPass(path_insert, pass_one, flag_copy);
- rvalue = 1;
+ len = strlen(pass);
+ if(len < minlen) {
+ free(pass);
+ return NULL;
}
- free(pass_one);
- return rvalue;
+ if(pass[len-1] == '\n')
+ pass[len-1] = '\0';
+
+ return pass;
}
char *generatePassword(char *dest, int amount)
@@ -230,13 +224,13 @@ int getOverwriteAnswer(char *path)
switch(answer)
{
case 'Y':
- case 'y': return 1;
+ case 'y': return OW_YES;
case 'N':
- case 'n': return 0;
+ case 'n': return OW_NO;
case EOF: printError("Error: Unexpected end of file\n");
default: { printf("Overwrite? (Y/N) "); break; }
}
}
- return -1;
+ return 2;
}