From: 諏訪子 Date: Tue, 21 May 2024 14:49:32 +0000 (+0900) Subject: ソースコードは綺麗に、OTPのバグの修正、表示のバグの修正 X-Git-Tag: sp-1.3.0~17 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=0668578cbdbd6798af6f0c92bbbfeb575143099d;p=sp.git ソースコードは綺麗に、OTPのバグの修正、表示のバグの修正 --- diff --git a/.gitignore b/.gitignore index 8564edf..429bcfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ sp -dist -release .ccls-cache *.o -*.tar.gz +release +*.core diff --git a/Makefile b/Makefile index a9db0b7..eef7981 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,31 @@ -UNAME_S!=uname -s -UNAME_M!=uname -m +UNAME_S != uname -s +UNAME_M != uname -m -NAME!=cat main.c | grep "const char\* sofname" | awk '{print $$5}' | \ +NAME != cat main.c | grep "const char \*sofname" | awk '{print $$5}' | \ sed "s/\"//g" | sed "s/;//" -VERSION!=cat main.c | grep "const char\* version" | awk '{print $$5}' | \ +VERSION != cat main.c | grep "const char \*version" | awk '{print $$5}' | \ sed "s/\"//g" | sed "s/;//" -PREFIX=/usr/local +PREFIX = /usr/local + +MANPREFIX = ${PREFIX}/man .if ${UNAME_S} == "FreeBSD" -MANPREFIX=${PREFIX}/share/man +MANPREFIX = ${PREFIX}/share/man .elif ${UNAME_S} == "Linux" -PREFIX=/usr -MANPREFIX=${PREFIX}/share/man +PREFIX = /usr +MANPREFIX = ${PREFIX}/share/man .elif ${UNAME_S} == "NetBSD" -PREFIX=/usr/pkg -MANPREFIX=${PREFIX}/share/man +PREFIX = /usr/pkg +MANPREFIX = ${PREFIX}/share/man .endif -CC=cc -FILES=main.c src/*.c -CFLAGS=-Wall -Wextra -O3 -I${PREFIX}/include -L${PREFIX}/lib +CC = cc +FILES = main.c src/*.c +CFLAGS = -Wall -Wextra -O3 -I${PREFIX}/include -L${PREFIX}/lib .if ${UNAME_S} == "NetBSD" -CFLAGS+=-I/usr/local/include -L/usr/local/lib -I/usr/include -L/usr/lib +CFLAGS += -I/usr/local/include -L/usr/local/lib -I/usr/include -L/usr/lib .endif -LDFLAGS=-lgpgme -lcrypto +LDFLAGS = -lgpgme -lcrypto all: ${CC} ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} diff --git a/main.c b/main.c index cd82f21..bd5aab9 100644 --- a/main.c +++ b/main.c @@ -1,10 +1,4 @@ -#include -#include -#include -#include - -#include - +#include "src/common.h" #include "src/initpass.h" #include "src/showpass.h" #include "src/yankpass.h" @@ -14,8 +8,8 @@ #include "src/genpass.h" #include "src/otppass.h" -const char* sofname = "sp"; -const char* version = "1.3.0"; +const char *sofname = "sp"; +const char *version = "1.3.0"; void helpme() { printf("076 %s %s - シンプルなパスワードマネージャー\n", sofname, version); @@ -98,8 +92,47 @@ void helpme_en() { printf("%s -v : Show version\n", sofname); } -int main (int argc, char* argv[]) { - char *lang = getenv("SP_LANG"); +char *getfullpath(char *arg) { + char *lang = getlang(); + + char *homedir = getenv("HOME"); + if (homedir == NULL) { + if (strncmp(lang, "en", 2) == 0) + perror("Failed to getting home directory"); + else perror("ホームディレクトリを受取に失敗"); + return NULL; + } + + char *basedir = "/.local/share/sp/"; + size_t fullPathLen; + char *fullPath; + if (arg != NULL) { + fullPathLen = strlen(homedir) + strlen(basedir) + strlen(arg) + 5; + } else { + fullPathLen = strlen(homedir) + strlen(basedir); + } + + fullPath = (char *)malloc(fullPathLen); + if (fullPath == NULL) { + if (strncmp(lang, "en", 2) == 0) + perror("Failed to allocating memory"); + else perror("メモリの役割に失敗"); + if (fullPath) free(fullPath); + if (homedir) free(homedir); + return NULL; + } + + if (arg != NULL) { + snprintf(fullPath, fullPathLen, "%s%s%s.gpg", homedir, basedir, arg); + } else { + snprintf(fullPath, fullPathLen, "%s%s", homedir, basedir); + } + + return fullPath; +} + +int main(int argc, char *argv[]) { + char *lang = getlang(); if (argc < 2) { if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); @@ -107,61 +140,59 @@ int main (int argc, char* argv[]) { return 0; } - if (argc == 3 && strcmp(argv[1], "-i") == 0) initpass(argv[2]); - else if (argc == 3 && strcmp(argv[1], "-s") == 0) showpass(argv[2]); - else if (argc == 3 && strcmp(argv[1], "-y") == 0) yankpass(argv[2]); - else if (argc == 2 && strcmp(argv[1], "-l") == 0) { - char basePath[512]; - char* homedir = getenv("HOME"); - if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) - perror("Failed to getting home directory"); - else perror("ホームディレクトリを受取に失敗"); - return -1; + if (strcmp(argv[1], "-g") == 0) { + if (argc != 3 && argc != 4) { + if (strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + return 1; } - char* basedir = "/.local/share/sp/"; - snprintf(basePath, sizeof(basePath), "%s%s", homedir, basedir); - - listpass(basePath, 0); - } - else if (argc == 3 && strcmp(argv[1], "-a") == 0) addpass(argv[2]); - else if (argc == 3 && strcmp(argv[1], "-d") == 0) delpass(argv[2], 0); - else if (argc == 3 && strcmp(argv[1], "-e") == 0) { - delpass(argv[2], 1); - addpass(argv[2]); - } - else if (strcmp(argv[1], "-g") == 0) { - if (argc == 3) genpass(atoi(argv[2]), true); + if (argc == 3) genpass(atoi(argv[2]), true); else if (argc == 4 && strcmp(argv[3], "risk") == 0) genpass(atoi(argv[2]), false); else if (argc == 4 && strcmp(argv[3], "secure") == 0) genpass(atoi(argv[2]), true); - else { + + return 0; + } + + if (argc == 3) { + if (strcmp(argv[1], "-i") == 0) initpass(argv[2]); + else if (strcmp(argv[1], "-s") == 0) printf("%s\n", showpass(argv[2])); + else if (strcmp(argv[1], "-y") == 0) yankpass(argv[2]); + else if (strcmp(argv[1], "-a") == 0) addpass(argv[2]); + else if (strcmp(argv[1], "-d") == 0) delpass(argv[2], 0); + else if (strcmp(argv[1], "-e") == 0) { + delpass(argv[2], 1); + addpass(argv[2]); + } + else if (strcmp(argv[1], "-o") == 0) { + char *fullPath = getfullpath(argv[2]); + if (fullPath == NULL) return -1; + otppass(fullPath); + if (fullPath) free(fullPath); + } else { if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); else helpme(); + return 1; } - } - else if (argc == 3 && strcmp(argv[1], "-o") == 0) { - char fullPath[512]; - char* homedir = getenv("HOME"); - if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) - perror("Failed to getting home directory"); - else perror("ホームディレクトリを受取に失敗"); - return -1; - } - - char* basedir = "/.local/share/sp/"; - snprintf(fullPath, sizeof(fullPath), "%s%s%s.gpg", homedir, basedir, argv[2]); + } else if (argc == 2) { + char *basePath = getfullpath(NULL); + if (basePath == NULL) return -1; - otppass(fullPath); - } - else if (argc == 2 && strcmp(argv[1], "-v") == 0) - printf("%s-%s\n", sofname, version); - else { - if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); - else helpme(); + if (strcmp(argv[1], "-l") == 0) listpass(basePath, 0); + else if (strcmp(argv[1], "-v") == 0) printf("%s-%s\n", sofname, version); + else { + if (strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + if (basePath) free(basePath); + return 1; + } + if (basePath) free(basePath); + } else { + if (strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + return 1; } return 0; diff --git a/src/addpass.c b/src/addpass.c index 9798b56..507e31f 100644 --- a/src/addpass.c +++ b/src/addpass.c @@ -1,14 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include - -#include -#include - #include "addpass.h" void cleanup(gpgme_ctx_t ctx, gpgme_key_t key, gpgme_data_t in, gpgme_data_t out) { @@ -18,35 +7,7 @@ void cleanup(gpgme_ctx_t ctx, gpgme_key_t key, gpgme_data_t in, gpgme_data_t out gpgme_key_release(key); } -int mkdir_r(const char *path, mode_t mode) { - char tmp[256]; - char *p = NULL; - size_t len; - - snprintf(tmp, sizeof(tmp), "%s", path); - len = strlen(tmp); - if (tmp[len - 1] == '/') { - tmp[len - 1] = 0; // 最後の「/」文字を取り消す - } - - for (p = tmp + 1; *p; p++) { - if (*p == '/') { - // 最後の「/」文字を取り消す - *p = 0; - if (mkdir(tmp, mode) != 0 && errno != EEXIST) return -1; - // また追加 - *p = '/'; - } - } - - if (mkdir(tmp, mode) != 0 && errno != EEXIST) { - return -1; - } - - return 0; -} - -void getpasswd(char* prompt, char*pw, size_t pwlen) { +void getpasswd(char *prompt, char *pw, size_t pwlen) { struct termios old, new; printf("%s", prompt); @@ -75,28 +36,28 @@ void getpasswd(char* prompt, char*pw, size_t pwlen) { tcsetattr(fileno(stdin), TCSANOW, &old); } -void addpass(char* file) { - char *lang = getenv("SP_LANG"); +void addpass(char *file) { + char *lang = getlang(); // パスを準備 - char* homedir = getenv("HOME"); + char *homedir = getenv("HOME"); if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to retrieving home directory"); else perror("ホームディレクトリを受取に失敗"); return; } - char* basedir = "/.local/share/sp/"; - char* ext = ".gpg"; + char *basedir = "/.local/share/sp/"; + char *ext = ".gpg"; char pass[256]; char knin[256]; int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; - char* gpgpathchk = malloc(alllen); + char *gpgpathchk = malloc(alllen); if (gpgpathchk == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); else perror("メモリを割当に失敗"); return; @@ -104,8 +65,9 @@ void addpass(char* file) { // ファイルが既に存在するかどうか確認 snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext); + if (access(gpgpathchk, F_OK) != -1) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf( stderr, "Password already exist.\nFor edit, please run ' sp -e %s '.\n", @@ -124,18 +86,18 @@ void addpass(char* file) { free(gpgpathchk); // パスワードを受け取る - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) getpasswd("Password: ", pass, sizeof(pass)); else getpasswd("パスワード: ", pass, sizeof(pass)); puts(""); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) getpasswd("Password (for verification): ", knin, sizeof(knin)); else getpasswd("パスワード(確認用): ", knin, sizeof(knin)); puts(""); // パスワードが一致するかどうか確認 if (strcmp(pass, knin) != 0) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Password does not match. Ending..."); else perror("パスワードが一致していません。終了…"); return; @@ -156,7 +118,7 @@ void addpass(char* file) { // GPGMEを創作 err = gpgme_new(&ctx); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err)); else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); return; @@ -165,7 +127,7 @@ void addpass(char* file) { // GPGMEは非対話的モードに設定 err = gpgme_set_pinentry_mode(ctx, GPGME_PINENTRY_MODE_LOOPBACK); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to setting pinentry mode: %s\n", gpgme_strerror(err)); else fprintf(stderr, "pinentryモードを設定に失敗: %s\n", gpgme_strerror(err)); gpgme_release(ctx); @@ -175,7 +137,7 @@ void addpass(char* file) { // パスワードからデータオブジェクトを創作 err = gpgme_data_new_from_mem(&in, pass, strlen(pass), 0); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to making data object: %s\n", gpgme_strerror(err)); else fprintf(stderr, "データオブジェクトを創作に失敗: %s\n", gpgme_strerror(err)); @@ -188,11 +150,12 @@ void addpass(char* file) { // 鍵を受け取る char keypath[256]; snprintf(keypath, sizeof(keypath), "%s%s%s", homedir, basedir, ".gpg-id"); + keypath[sizeof(keypath) - 1] = '\0'; FILE* keyfile = fopen(keypath, "rb"); if (keyfile == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) { + if (strncmp(lang, "en", 2) == 0) { perror("Failed to opening .gpg-id file"); fprintf(stderr, "Failed path: %s\n", keypath); } else { @@ -202,9 +165,9 @@ void addpass(char* file) { return; } - char* keyid = malloc(256); + char *keyid = malloc(256); if (!fgets(keyid, 256, keyfile)) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to reading key ID"); else perror("鍵IDを読込に失敗"); fclose(keyfile); @@ -217,7 +180,7 @@ void addpass(char* file) { err = gpgme_get_key(ctx, keyid, &key[0], 0); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to getting key: %s\n", gpgme_strerror(err)); else fprintf(stderr, "鍵を受取に失敗: %s\n", gpgme_strerror(err)); free(keyid); @@ -225,7 +188,7 @@ void addpass(char* file) { } if (key[0] == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Error: Key is NULL"); + if (strncmp(lang, "en", 2) == 0) perror("Error: Key is NULL"); else perror("エラー:鍵はNULLです"); free(keyid); return; @@ -236,7 +199,7 @@ void addpass(char* file) { // 暗号化 err = gpgme_op_encrypt(ctx, &key[0], GPGME_ENCRYPT_ALWAYS_TRUST, in, out); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to encrypt: %s\n", gpgme_strerror(err)); else fprintf(stderr, "暗号化に失敗: %s\n", gpgme_strerror(err)); cleanup(ctx, key[0], in, out); @@ -244,10 +207,10 @@ void addpass(char* file) { } // 暗号化したタイルを開く - char* gpgpath = malloc(alllen); + char *gpgpath = malloc(alllen); if (gpgpath == NULL) { cleanup(ctx, key[0], in, out); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); else perror("メモリを割当に失敗"); return; @@ -256,15 +219,16 @@ void addpass(char* file) { // ディレクトリを創作 char dirpath[512]; snprintf(dirpath, sizeof(dirpath), "%s%s%s", homedir, basedir, file); + dirpath[sizeof(dirpath) - 1] = '\0'; - char* lastsla = strrchr(dirpath, '/'); + char *lastsla = strrchr(dirpath, '/'); if (lastsla != NULL) { *lastsla = '\0'; if (mkdir_r(dirpath, 0755) != 0) { free(gpgpath); cleanup(ctx, key[0], in, out); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to constructing directory"); else perror("ディレクトリを創作に失敗"); return; @@ -272,11 +236,12 @@ void addpass(char* file) { } snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); + struct stat statbuf; if (stat(gpgpath, &statbuf) == 0) { free(gpgpath); cleanup(ctx, key[0], in, out); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Password is already exist"); else perror("パスワードは既に存在しています"); return; @@ -284,7 +249,7 @@ void addpass(char* file) { gpgfile = fopen(gpgpath, "wb"); if (gpgfile == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) { + if (strncmp(lang, "en", 2) == 0) { perror("Failed to opening file."); fprintf(stderr, "Failed path: %s\n", gpgpath); } else { @@ -299,7 +264,7 @@ void addpass(char* file) { // データが保存したかどうか確認 ssize_t encrypted_data_size = gpgme_data_seek(out, 0, SEEK_END); if (encrypted_data_size <= 0) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to saving the data"); else perror("データを保存に失敗"); fclose(gpgfile); @@ -316,7 +281,7 @@ void addpass(char* file) { while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) { if (fwrite(buffer, 1, (size_t)read_bytes, gpgfile) != (size_t)read_bytes) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to writing password"); else perror("パスワードを書き込みに失敗"); free(gpgpath); @@ -330,7 +295,7 @@ void addpass(char* file) { free(gpgpath); cleanup(ctx, key[0], in, out); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) puts("I could save the password"); else puts("パスワードを保存出来ました"); } diff --git a/src/addpass.h b/src/addpass.h index 8aca69d..23918ad 100644 --- a/src/addpass.h +++ b/src/addpass.h @@ -1,9 +1,13 @@ #ifndef ADDPASS_H #define ADDPASS_H -#include +#include +#include -int mkdir_r(const char *path, mode_t mode); -void addpass(char* file); +#include + +#include "common.h" + +void addpass(char *file); #endif diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..193dcff --- /dev/null +++ b/src/common.c @@ -0,0 +1,37 @@ +#include "common.h" + +char *getlang() { + char *lang = NULL; + + lang = getenv("SP_LANG"); + if (lang == NULL) lang = "ja"; + + return lang; +} + +int mkdir_r(const char *path, mode_t mode) { + char tmp[256]; + char *p = NULL; + size_t len; + + snprintf(tmp, sizeof(tmp), "%s", path); + + len = strlen(tmp); + if (tmp[len - 1] == '/') { + tmp[len - 1] = 0; // 最後の「/」文字を取り消す + } + + for (p = tmp + 1; *p; p++) { + if (*p == '/') { + *p = 0; // 最後の「/」文字を取り消す + if (mkdir(tmp, mode) != 0 && errno != EEXIST) return -1; + *p = '/'; // また追加 + } + } + + if (mkdir(tmp, mode) != 0 && errno != EEXIST) { + return -1; + } + + return 0; +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..25f8f28 --- /dev/null +++ b/src/common.h @@ -0,0 +1,16 @@ +#ifndef COMMON_H +#define COMMON_H + +#include +#include +#include +#include +#include +#include + +#include + +char *getlang(); +int mkdir_r(const char *path, mode_t mode); + +#endif diff --git a/src/delpass.c b/src/delpass.c index 9b85ddd..5ef52c1 100644 --- a/src/delpass.c +++ b/src/delpass.c @@ -1,30 +1,28 @@ #include -#include -#include -#include +#include "common.h" #include "delpass.h" -int delpass(char* file, int force) { - char *lang = getenv("SP_LANG"); +int delpass(char *file, int force) { + char *lang = getlang(); // パスを準備 char pwfile[512]; - char* homedir = getenv("HOME"); + char *homedir = getenv("HOME"); if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); else perror("ホームディレクトリを受取に失敗"); return -1; } - char* basedir = "/.local/share/sp/"; - char* ext = ".gpg"; + char *basedir = "/.local/share/sp/"; + char *ext = ".gpg"; int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; - char* gpgpathchk = malloc(alllen); + char *gpgpathchk = malloc(alllen); if (gpgpathchk == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); else perror("メモリを割当に失敗"); return -1; @@ -32,8 +30,9 @@ int delpass(char* file, int force) { // ファイルが既に存在するかどうか確認 snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext); + if (access(gpgpathchk, F_OK) != 0) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Password does not exist"); else perror("パスワードが存在しません"); free(gpgpathchk); @@ -51,20 +50,20 @@ int delpass(char* file, int force) { ext ); if (needed >= (int)sizeof(pwfile)) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Error: Path is too long"); else perror("エラー:パスが長すぎる"); return -1; } // 削除を確認する - if (force == 0) { // パスワードの変更のばあい、確認は不要 - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (force == 0) { // パスワードの変更の場合、確認は不要 + if (strncmp(lang, "en", 2) == 0) printf("Is it really good if I delete the password '%s'? (y/N): ", file); printf("パスワード「%s」を本当に削除する事が宜しいでしょうか? (y/N): ", file); int confirm = getchar(); if (confirm != 'y' && confirm != 'Y') { - if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Not deleted"); + if (strncmp(lang, "en", 2) == 0) puts("Not deleted"); else puts("削除しませんでした"); return -1; } @@ -74,7 +73,7 @@ int delpass(char* file, int force) { } if (unlink(pwfile) == -1) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Password cannot be delete"); else perror("パスワードを削除出来ませんですた"); return -1; @@ -82,7 +81,8 @@ int delpass(char* file, int force) { if (force == 1) return 0; - if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Deleted password"); + if (strncmp(lang, "en", 2) == 0) puts("Deleted password"); else puts("パスワードを削除しました"); + return 0; } diff --git a/src/delpass.h b/src/delpass.h index 90e12de..8189816 100644 --- a/src/delpass.h +++ b/src/delpass.h @@ -1,6 +1,6 @@ #ifndef DELPASS_H #define DELPASS_H -int delpass(char* file, int force); +int delpass(char *file, int force); #endif diff --git a/src/genpass.c b/src/genpass.c index ab6f320..b2f5071 100644 --- a/src/genpass.c +++ b/src/genpass.c @@ -1,21 +1,18 @@ -#include -#include -#include - +#include "common.h" #include "genpass.h" void genpass(int count, bool issecure) { - char *lang = getenv("SP_LANG"); + char *lang = getlang(); - const char* charset_risky = + const char *charset_risky = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - const char* charset_secure = + const char *charset_secure = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?"; - const char* charset = issecure ? charset_secure : charset_risky; + const char *charset = issecure ? charset_secure : charset_risky; FILE *fp = fopen("/dev/random", "rb"); if (fp == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Could not opening /dev/random"); else perror("/dev/randomを開けられませんでした"); exit(EXIT_FAILURE); diff --git a/src/initpass.c b/src/initpass.c index 374f22b..615d99b 100644 --- a/src/initpass.c +++ b/src/initpass.c @@ -1,29 +1,23 @@ -#include -#include -#include -#include -#include - +#include "common.h" #include "initpass.h" -#include "addpass.h" -void initpass(char* gpgid) { - char *lang = getenv("SP_LANG"); +void initpass(char *gpgid) { + char *lang = getlang(); - char* homedir = getenv("HOME"); + char *homedir = getenv("HOME"); if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory."); else perror("ホームディレクトリを受取に失敗。"); return; } - char* basedir = "/.local/share/sp/"; + char *basedir = "/.local/share/sp/"; char dirpath[256]; snprintf(dirpath, sizeof(dirpath), "%s%s", homedir, basedir); if (mkdir_r(dirpath, 0755) != 0 && errno != EEXIST) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to creating directory."); else perror("ディレクトリを作成に失敗。"); return; @@ -34,15 +28,15 @@ void initpass(char* gpgid) { struct stat statbuf; if (stat(gpgidpath, &statbuf) == 0) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror(".gpg-id file is already exist."); else perror(".gpg-idファイルは既に存在します。"); return; } - FILE* gpgidfile = fopen(gpgidpath, "w"); + FILE *gpgidfile = fopen(gpgidpath, "w"); if (gpgidfile == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to writing .gpg-id file."); else perror(".gpg-idファイルを書き込めません。"); fclose(gpgidfile); @@ -50,7 +44,7 @@ void initpass(char* gpgid) { } if (fputs(gpgid, gpgidfile) == EOF) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to writing .gpg-id file."); else perror(".gpg-idファイルへの書き込みに失敗しました。"); fclose(gpgidfile); @@ -58,7 +52,7 @@ void initpass(char* gpgid) { } fclose(gpgidfile); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) puts("First time setup was complete."); else puts("初期設定に完了しました。"); } diff --git a/src/listpass.c b/src/listpass.c index 38df395..3308c24 100644 --- a/src/listpass.c +++ b/src/listpass.c @@ -1,18 +1,15 @@ -#include #include -#include -#include -#include +#include "common.h" #include "listpass.h" -void listpass(char* basePath, int level) { - char *lang = getenv("SP_LANG"); +void listpass(char *basePath, int level) { + char *lang = getlang(); - struct dirent* entry; + struct dirent *entry; DIR* dir = opendir(basePath); if (!dir) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Could not opening directory"); else perror("ディレクトリを開けられません"); return; @@ -26,7 +23,7 @@ void listpass(char* basePath, int level) { char path[1000]; int needed = snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name); if (needed >= (int)sizeof(path) || needed < 0) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Error: Path is too long, or failed to getting lenth"); else perror("エラー:パスが長すぎる、又は長さを受取に失敗"); continue; @@ -34,7 +31,7 @@ void listpass(char* basePath, int level) { struct stat statbuf; if (stat(path, &statbuf) == -1) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to reading file status"); else perror("ファイル状況を読込に失敗"); continue; @@ -48,8 +45,8 @@ void listpass(char* basePath, int level) { printf("|-- %s\n", entry->d_name); listpass(path, level + 1); } else if (S_ISREG(statbuf.st_mode)) { - char* filename = entry->d_name; - char* ext = strstr(filename, ".gpg"); + char *filename = entry->d_name; + char *ext = strstr(filename, ".gpg"); if (ext) *ext = '\0'; printf("|-- %s\n", filename); } diff --git a/src/listpass.h b/src/listpass.h index b13c294..8215396 100644 --- a/src/listpass.h +++ b/src/listpass.h @@ -1,6 +1,6 @@ #ifndef LISTPASS_H #define LISTPASS_H -void listpass(char* basePath, int level); +void listpass(char *basePath, int level); #endif diff --git a/src/otppass.c b/src/otppass.c index 5f32449..4df0d73 100644 --- a/src/otppass.c +++ b/src/otppass.c @@ -1,25 +1,23 @@ -#include -#include #include #include -#include #include "base32.h" +#include "common.h" #include "otppass.h" -unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { - char *lang = getenv("SP_LANG"); +unsigned char *extract_secret(const char *otpauth_url, size_t *decoded_len) { + char *lang = getlang(); - const char* secret_start = strstr(otpauth_url, "secret="); + const char *secret_start = strstr(otpauth_url, "secret="); if (!secret_start) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("In the middle of the OTPAuth URL, could not found secret"); else perror("OTPAuth URLの中に、シークレットを見つけられませんでした"); return NULL; } secret_start += 7; - const char* secret_end = strchr(secret_start, '&'); + const char *secret_end = strchr(secret_start, '&'); if (!secret_end) { if (secret_start[0] != '\0') { secret_end = secret_start + strlen(secret_start); @@ -29,17 +27,17 @@ unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { } if (secret_end < secret_start) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Illegal secret range"); else perror("不正なシークレットの距離"); return NULL; } size_t secret_len = secret_end - secret_start; - char* secret_encoded = (char*)malloc(secret_len + 1); + char *secret_encoded = (char *)malloc(secret_len + 1); if (secret_encoded == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); else perror("メモリの役割に失敗"); return NULL; @@ -48,11 +46,11 @@ unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { strncpy(secret_encoded, secret_start, secret_len); secret_encoded[secret_len] = '\0'; - unsigned char* secret_decoded = base32_decode(secret_encoded, decoded_len); + unsigned char *secret_decoded = base32_decode(secret_encoded, decoded_len); free(secret_encoded); if (!secret_decoded) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to decrypting of the BASE32"); else perror("BASE32の復号化に失敗"); return NULL; @@ -85,8 +83,8 @@ uint32_t generate_totp(const char *secret, uint64_t counter) { return truncated_hash % 1000000; } -void otppass(char* file) { - char *lang = getenv("SP_LANG"); +void otppass(char *file) { + char *lang = getlang(); gpgme_ctx_t ctx; gpgme_error_t err; @@ -96,7 +94,7 @@ void otppass(char* file) { gpgme_check_version(NULL); err = gpgme_new(&ctx); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPG"); else perror("GPGMEを創作に失敗"); exit(1); @@ -104,7 +102,7 @@ void otppass(char* file) { err = gpgme_data_new_from_file(&in, file, 1); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to reading the GPG file"); else perror("GPGファイルを読込に失敗"); exit(1); @@ -112,7 +110,7 @@ void otppass(char* file) { err = gpgme_data_new(&out); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to reading the GPG data"); else perror("GPGデータを読込に失敗"); exit(1); @@ -120,37 +118,42 @@ void otppass(char* file) { err = gpgme_op_decrypt(ctx, in, out); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to decrypting the GPG"); else perror("GPGを復号化に失敗"); exit(1); } - char* secret = gpgme_data_release_and_get_mem(out, &secret_len); + char *secret = gpgme_data_release_and_get_mem(out, &secret_len); if (!secret) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to getting the GPG"); else perror("GPGを受取に失敗"); exit(1); } + secret[secret_len] = '\0'; + size_t decoded_len; - unsigned char* secret_decoded = extract_secret(secret, &decoded_len); + unsigned char *secret_decoded = extract_secret(secret, &decoded_len); if (!secret_decoded) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to decoding or exporting secret"); else perror("シークレットの抽出又はデコードに失敗しました"); free(secret); exit(1); } + secret_decoded[decoded_len] = '\0'; + time_t current_time = time(NULL); uint64_t counter = current_time / 30; - uint32_t otp = generate_totp((const char*)secret_decoded, counter); - printf("%06d\n", otp); + uint32_t otp = generate_totp((const char *)secret_decoded, counter); gpgme_data_release(in); gpgme_release(ctx); - free(secret); + gpgme_free(secret); free(secret_decoded); + + printf("%06d\n", otp); } diff --git a/src/showpass.c b/src/showpass.c index c88c47b..9d422d0 100644 --- a/src/showpass.c +++ b/src/showpass.c @@ -1,19 +1,14 @@ -#include -#include -#include #include -#include -#include - +#include "common.h" #include "showpass.h" void clean_up( gpgme_ctx_t ctx, gpgme_data_t in, gpgme_data_t out, - FILE* gpgfile, - char* gpgpath + FILE *gpgfile, + char *gpgpath ) { if (gpgfile) fclose(gpgfile); if (gpgpath) free(gpgpath); @@ -22,8 +17,8 @@ void clean_up( gpgme_release(ctx); } -void showpass(char* file) { - char *lang = getenv("SP_LANG"); +const char *showpass(char *file) { + char *lang = getlang(); gpgme_ctx_t ctx; gpgme_error_t err; @@ -38,39 +33,40 @@ void showpass(char* file) { // GPGMEを創作 err = gpgme_new(&ctx); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err)); else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); - return; + return NULL; } // OpenPGPプロトコールを設定 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); // 暗号化したタイルを開く - char* homedir = getenv("HOME"); + char *homedir = getenv("HOME"); if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); else perror("ホームディレクトリを受取に失敗"); - return; + return NULL; } - char* basedir = "/.local/share/sp/"; - char* ext = ".gpg"; + char *basedir = "/.local/share/sp/"; + char *ext = ".gpg"; int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; - char* gpgpath = malloc(alllen); + char *gpgpath = malloc(alllen); if (gpgpath == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memeory"); else perror("メモリを割当に失敗"); - return; + return NULL; } snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); + gpgfile = fopen(gpgpath, "rb"); if (gpgfile == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) { + if (strncmp(lang, "en", 2) == 0) { perror("Failed to opening file"); fprintf(stderr, "Failing path: %s\n", gpgpath); } else { @@ -78,25 +74,25 @@ void showpass(char* file) { fprintf(stderr, "失敗したパス: %s\n", gpgpath); } free(gpgpath); - return; + return NULL; } // ファイルからinデータオブジェクトを創作 if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object"); else perror("GPGMEデータオブジェクトを創作に失敗"); clean_up(ctx, in, out, gpgfile, gpgpath); - return; + return NULL; } // outデータオブジェクトを創作 if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object"); else perror("GPGMEデータオブジェクトを創作に失敗"); clean_up(ctx, in, out, gpgfile, gpgpath); - return; + return NULL; } // データオブジェクトを創作 @@ -105,32 +101,39 @@ void showpass(char* file) { // 復号化して err = gpgme_op_decrypt(ctx, in, out); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to decrypting: %s\n", gpgme_strerror(err)); else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); // 掃除 clean_up(ctx, in, out, gpgfile, gpgpath); - return; + return NULL; } // 復号化したパスワードを表示する gpgme_data_seek(out, 0, SEEK_SET); char buffer[512]; + char *res = malloc(512 * sizeof(char)); + if (res == NULL) { + if (strncmp(lang, "en", 2) == 0) + perror("Failed to allocating memory"); + else perror("メモリを役割に失敗"); + clean_up(ctx, in, out, gpgfile, gpgpath); + return NULL; + } + ssize_t read_bytes; - bool islastnl = false; + int i = 0; while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) { - fwrite(buffer, 1, read_bytes, stdout); - if (buffer[read_bytes - 1] == '\n') { - islastnl = true; - } + memcpy(res + i, buffer, read_bytes); + i += read_bytes; } - if (!islastnl) { - putchar('\n'); - } + res[i] = '\0'; + if (res[i-1] == '\n') res[i-1] = '\0'; // 掃除 clean_up(ctx, in, out, gpgfile, gpgpath); + return res; } diff --git a/src/showpass.h b/src/showpass.h index 3c47d0f..40f317f 100644 --- a/src/showpass.h +++ b/src/showpass.h @@ -1,6 +1,6 @@ #ifndef SHOWPASS_H #define SHOWPASS_H -void showpass(char* file); +const char *showpass(char *file); #endif diff --git a/src/yankpass.c b/src/yankpass.c index 72367b5..de583f5 100644 --- a/src/yankpass.c +++ b/src/yankpass.c @@ -1,20 +1,16 @@ -#include -#include #include #include -#include - -#include +#include "common.h" #include "yankpass.h" #include "showpass.h" -void yankpass(char* file) { - char *lang = getenv("SP_LANG"); +void yankpass(char *file) { + char *lang = getlang(); // Xセッションではない場合(例えば、SSH、TTY、Gayland等)、showpass()を実行して if (getenv("DISPLAY") == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) puts("There is no X session, so executing 'sp -s'."); else puts("Xセッションではありませんので、「sp -s」を実行します。"); showpass(file); @@ -34,7 +30,7 @@ void yankpass(char* file) { // GPGMEを創作 err = gpgme_new(&ctx); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err)); else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); return; @@ -46,7 +42,7 @@ void yankpass(char* file) { // 暗号化したタイルを開く char* homedir = getenv("HOME"); if (homedir == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); else perror("ホームディレクトリを受取に失敗"); return; @@ -57,7 +53,7 @@ void yankpass(char* file) { int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; char* gpgpath = malloc(alllen); if (gpgpath == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); else perror("メモリを割当に失敗"); return; @@ -66,7 +62,7 @@ void yankpass(char* file) { snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); gpgfile = fopen(gpgpath, "rb"); if (gpgfile == NULL) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) { + if (strncmp(lang, "en", 2) == 0) { perror("Failed to opening the file"); fprintf(stderr, "Failed path: %s\n", gpgpath); } else { @@ -84,7 +80,7 @@ void yankpass(char* file) { // 復号化して err = gpgme_op_decrypt(ctx, in, out); if (err) { - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to decryption: %s\n", gpgme_strerror(err)); else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); @@ -107,7 +103,7 @@ void yankpass(char* file) { gpgme_data_release(in); gpgme_data_release(out); gpgme_release(ctx); - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) perror("Could not found a clipboard"); else perror("クリップボードを見つけられませんでした"); return; @@ -129,17 +125,17 @@ void yankpass(char* file) { pclose(pipe); // 45秒後、クリップボードから削除する - if (lang != NULL && strncmp(lang, "en", 2) == 0) + if (strncmp(lang, "en", 2) == 0) printf( - "%s\n%s", + "%s\n%s\n", "Added password to the clipboard.", - "I will take it away from the clipboard after 45 second" + "I will take it away from the clipboard after 45 second." ); else printf( - "%s\n%s", + "%s\n%s\n", "パスワードをクリップボードに追加しました。", - "45秒後はクリップボードから取り消されます" + "45秒後はクリップボードから取り消されます。" ); sleep(45); system("echo -n | xclip -selection clipboard"); diff --git a/src/yankpass.h b/src/yankpass.h index 7219787..e97d95c 100644 --- a/src/yankpass.h +++ b/src/yankpass.h @@ -1,6 +1,6 @@ #ifndef YANKPASS_H #define YANKPASS_H -void yankpass(char* file); +void yankpass(char *file); #endif