From 461b952da7a4ae014850936d06bf4b2c5f043b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Fri, 2 Feb 2024 12:15:58 +0900 Subject: [PATCH] =?UTF-8?q?=E8=8B=B1=E8=A8=B3=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++ addpass.c | 77 ++++++++++++++++++++++++++++++++++++---------------- base32.c | 2 ++ base32.h | 1 - delpass.c | 25 ++++++++++++----- genpass.c | 5 +++- initpass.c | 20 ++++++++++---- listpass.c | 11 ++++++-- main.c | 44 ++++++++++++++++++++++++------ otppass.c | 35 +++++++++++++++++------- showpass.c | 30 ++++++++++++++------ yankpass.c | 35 +++++++++++++++++------- 12 files changed, 211 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c8d8e7..3339f36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.3.0 +* 英訳の追加 + # 1.2.0 * やっとTOTP機能性を修正した * makeを実行したら、バイナリがもっと小さくなる diff --git a/addpass.c b/addpass.c index b25f1ee..cd5c72a 100644 --- a/addpass.c +++ b/addpass.c @@ -74,10 +74,13 @@ void getpasswd(char* prompt, char*pw, size_t pwlen) { } void addpass(char* file) { + char *lang = getenv("SP_LANG"); + // パスを準備 char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to retrieving home directory"); + else perror("ホームディレクトリを受取に失敗"); return; } @@ -90,28 +93,33 @@ void addpass(char* file) { int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; char* gpgpathchk = malloc(alllen); if (gpgpathchk == NULL) { - perror("メモリを割当に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリを割当に失敗"); return; } // ファイルが既に存在するかどうか確認 snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext); if (access(gpgpathchk, F_OK) != -1) { - fprintf(stderr, "パスワードが既に存在しています。\n変更するには、「 sp -e %s 」を実行して下さい。\n", file); + if (lang != NULL && strncmp(lang, "en", 2) == 0) fprintf(stderr, "Password already exist.\nFor edit, please run ' sp -e %s '.\n", file); + else fprintf(stderr, "パスワードが既に存在しています。\n変更するには、「 sp -e %s 」を実行して下さい。\n", file); free(gpgpathchk); return; } free(gpgpathchk); // パスワードを受け取る - getpasswd("パスワード: ", pass, sizeof(pass)); + if (lang != NULL && strncmp(lang, "en", 2) == 0) getpasswd("Password: ", pass, sizeof(pass)); + else getpasswd("パスワード: ", pass, sizeof(pass)); puts(""); - getpasswd("パスワード(確認用): ", knin, sizeof(knin)); + if (lang != NULL && strncmp(lang, "en", 2) == 0) getpasswd("Password (for verification): ", knin, sizeof(knin)); + else getpasswd("パスワード(確認用): ", knin, sizeof(knin)); puts(""); // パスワードが一致するかどうか確認 if (strcmp(pass, knin) != 0) { - fprintf(stderr, "パスワードが一致していません。終了…\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Password does not match. Ending..."); + else perror("パスワードが一致していません。終了…"); return; } @@ -130,14 +138,16 @@ void addpass(char* file) { // GPGMEを創作 err = gpgme_new(&ctx); if (err) { - fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); + if (lang != NULL && 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; } // GPGMEは非対話的モードに設定 err = gpgme_set_pinentry_mode(ctx, GPGME_PINENTRY_MODE_LOOPBACK); if (err) { - fprintf(stderr, "pinentryモードを設定に失敗: %s\n", gpgme_strerror(err)); + if (lang != NULL && 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); return; } @@ -145,7 +155,8 @@ void addpass(char* file) { // パスワードからデータオブジェクトを創作 err = gpgme_data_new_from_mem(&in, pass, strlen(pass), 0); if (err) { - fprintf(stderr, "データオブジェクトを創作に失敗: %s\n", gpgme_strerror(err)); + if (lang != NULL && 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)); gpgme_release(ctx); return; } @@ -159,14 +170,20 @@ void addpass(char* file) { FILE* keyfile = fopen(keypath, "rb"); if (keyfile == NULL) { - perror(".gpg-idファイルを開くに失敗。"); - printf("失敗したパス: %s\n", keypath); + if (lang != NULL && strncmp(lang, "en", 2) == 0) { + perror("Failed to opening .gpg-id file"); + fprintf(stderr, "Failed path: %s\n", keypath); + } else { + perror(".gpg-idファイルを開くに失敗"); + fprintf(stderr, "失敗したパス: %s\n", keypath); + } return; } char* keyid = malloc(256); if (!fgets(keyid, 256, keyfile)) { - perror("鍵IDを読込に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to reading key ID"); + else perror("鍵IDを読込に失敗"); fclose(keyfile); free(keyid); return; @@ -177,13 +194,15 @@ void addpass(char* file) { err = gpgme_get_key(ctx, keyid, &key[0], 0); if (err) { - fprintf(stderr, "鍵を受取に失敗: %s\n", gpgme_strerror(err)); + if (lang != NULL && 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); return; } if (key[0] == NULL) { - fprintf(stderr, "エラー:鍵はNULLです。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Error: Key is NULL"); + else perror("エラー:鍵はNULLです"); free(keyid); return; } @@ -193,7 +212,8 @@ void addpass(char* file) { // 暗号化 err = gpgme_op_encrypt(ctx, &key[0], GPGME_ENCRYPT_ALWAYS_TRUST, in, out); if (err) { - fprintf(stderr, "暗号化に失敗: %s\n", gpgme_strerror(err)); + if (lang != NULL && 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); return; } @@ -202,7 +222,8 @@ void addpass(char* file) { char* gpgpath = malloc(alllen); if (gpgpath == NULL) { cleanup(ctx, key[0], in, out); - perror("メモリを割当に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリを割当に失敗"); return; } @@ -217,7 +238,8 @@ void addpass(char* file) { if (mkdir_r(dirpath, 0755) != 0) { free(gpgpath); cleanup(ctx, key[0], in, out); - perror("ディレクトリを創作に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to constructing directory"); + else perror("ディレクトリを創作に失敗"); return; } } @@ -227,14 +249,20 @@ void addpass(char* file) { if (stat(gpgpath, &statbuf) == 0) { free(gpgpath); cleanup(ctx, key[0], in, out); - fprintf(stderr, "パスワードは既に存在しています。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Password is already exist"); + else perror("パスワードは既に存在しています"); return; } gpgfile = fopen(gpgpath, "wb"); if (gpgfile == NULL) { - perror("ファイルを開くに失敗。"); - printf("失敗したパス: %s\n", gpgpath); + if (lang != NULL && strncmp(lang, "en", 2) == 0) { + perror("Failed to opening file."); + fprintf(stderr, "Failed path: %s\n", gpgpath); + } else { + perror("ファイルを開くに失敗。"); + fprintf(stderr, "失敗したパス: %s\n", gpgpath); + } free(gpgpath); cleanup(ctx, key[0], in, out); return; @@ -243,7 +271,8 @@ void addpass(char* file) { // データが保存したかどうか確認 ssize_t encrypted_data_size = gpgme_data_seek(out, 0, SEEK_END); if (encrypted_data_size <= 0) { - fprintf(stderr, "データを保存に失敗。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to saving the data"); + else perror("データを保存に失敗"); fclose(gpgfile); free(gpgpath); cleanup(ctx, key[0], in, out); @@ -258,7 +287,8 @@ 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) { - perror("パスワードを書き込みに失敗"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to writing password"); + else perror("パスワードを書き込みに失敗"); free(gpgpath); cleanup(ctx, key[0], in, out); return; @@ -270,5 +300,6 @@ void addpass(char* file) { free(gpgpath); cleanup(ctx, key[0], in, out); - printf("パスワードを保存出来ました。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("I could save the password"); + else puts("パスワードを保存出来ました"); } diff --git a/base32.c b/base32.c index 6a8ce6d..ee36ab6 100644 --- a/base32.c +++ b/base32.c @@ -1,3 +1,5 @@ +#include + #include "base32.h" int char_to_val(char c) { diff --git a/base32.h b/base32.h index 6e2561f..4023054 100644 --- a/base32.h +++ b/base32.h @@ -2,7 +2,6 @@ #define BASE32_H #include -#include unsigned char *base32_decode(const char *encoded, size_t *out_len); diff --git a/delpass.c b/delpass.c index f64d0bc..2f20ed0 100644 --- a/delpass.c +++ b/delpass.c @@ -1,15 +1,19 @@ #include #include #include +#include #include "delpass.h" int delpass(char* file, int force) { + char *lang = getenv("SP_LANG"); + // パスを準備 char pwfile[512]; char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); + else perror("ホームディレクトリを受取に失敗"); return -1; } @@ -19,14 +23,16 @@ int delpass(char* file, int force) { int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; char* gpgpathchk = malloc(alllen); if (gpgpathchk == NULL) { - perror("メモリを割当に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリを割当に失敗"); return -1; } // ファイルが既に存在するかどうか確認 snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext); if (access(gpgpathchk, F_OK) != 0) { - fprintf(stderr, "パスワードが存在しません。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Password does not exist"); + else perror("パスワードが存在しません"); free(gpgpathchk); return -1; } @@ -34,16 +40,19 @@ int delpass(char* file, int force) { int needed = snprintf(pwfile, sizeof(pwfile), "%s%s%s%s", homedir, basedir, file, ext); if (needed >= (int)sizeof(pwfile)) { - fprintf(stderr, "エラー:パスが長すぎる。\n"); + if (lang != NULL && 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) 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') { - printf("削除しませんでした。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Not deleted"); + else puts("削除しませんでした"); return -1; } @@ -52,12 +61,14 @@ int delpass(char* file, int force) { } if (unlink(pwfile) == -1) { - perror("パスワードを削除出来ませんですた。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Password cannot be delete"); + else perror("パスワードを削除出来ませんですた"); return -1; } if (force == 1) return 0; - printf("パスワードを削除しました。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Deleted password"); + else puts("パスワードを削除しました"); return 0; } diff --git a/genpass.c b/genpass.c index 3d862b6..ac7617b 100644 --- a/genpass.c +++ b/genpass.c @@ -5,13 +5,16 @@ #include "genpass.h" void genpass(int count, bool issecure) { + char *lang = getenv("SP_LANG"); + const char* charset_risky = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; const char* charset_secure = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?"; const char* charset = issecure ? charset_secure : charset_risky; FILE *fp = fopen("/dev/random", "rb"); if (fp == NULL) { - perror("/dev/randomを開けられませんでした。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Could not opening /dev/random"); + else perror("/dev/randomを開けられませんでした"); exit(EXIT_FAILURE); } diff --git a/initpass.c b/initpass.c index 6a5679d..b073559 100644 --- a/initpass.c +++ b/initpass.c @@ -8,9 +8,12 @@ #include "addpass.h" void initpass(char* gpgid) { + char *lang = getenv("SP_LANG"); + char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory."); + else perror("ホームディレクトリを受取に失敗。"); return; } @@ -19,7 +22,8 @@ void initpass(char* gpgid) { snprintf(dirpath, sizeof(dirpath), "%s%s", homedir, basedir); if (mkdir_r(dirpath, 0755) != 0 && errno != EEXIST) { - perror("ディレクトリを作成に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to creating directory."); + else perror("ディレクトリを作成に失敗。"); return; } @@ -28,23 +32,27 @@ void initpass(char* gpgid) { struct stat statbuf; if (stat(gpgidpath, &statbuf) == 0) { - fprintf(stderr, ".gpg-idファイルは既に存在します。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror(".gpg-id file is already exist."); + else perror(".gpg-idファイルは既に存在します。"); return; } FILE* gpgidfile = fopen(gpgidpath, "w"); if (gpgidfile == NULL) { - perror(".gpg-idファイルを書き込めません。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to writing .gpg-id file."); + else perror(".gpg-idファイルを書き込めません。"); fclose(gpgidfile); return; } if (fputs(gpgid, gpgidfile) == EOF) { - fprintf(stderr, ".gpg-idファイルへの書き込みに失敗しました。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to writing .gpg-id file."); + else perror(".gpg-idファイルへの書き込みに失敗しました。"); fclose(gpgidfile); return; } fclose(gpgidfile); - printf("初期設定に完了しました。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("First time setup was complete."); + else puts("初期設定に完了しました。"); } diff --git a/listpass.c b/listpass.c index a5e5932..93429e8 100644 --- a/listpass.c +++ b/listpass.c @@ -7,10 +7,13 @@ #include "listpass.h" void listpass(char* basePath, int level) { + char *lang = getenv("SP_LANG"); + struct dirent* entry; DIR* dir = opendir(basePath); if (!dir) { - perror("ディレクトリを開けられません。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Could not opening directory"); + else perror("ディレクトリを開けられません"); return; } @@ -19,13 +22,15 @@ 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) { - fprintf(stderr, "エラー:パスが長すぎる、又は長さを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Error: Path is too long, or failed to getting lenth"); + else perror("エラー:パスが長すぎる、又は長さを受取に失敗"); continue; } struct stat statbuf; if (stat(path, &statbuf) == -1) { - perror("ファイル状況を読込に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to reading file status"); + else perror("ファイル状況を読込に失敗"); continue; } diff --git a/main.c b/main.c index 42b0bd5..5ade13f 100644 --- a/main.c +++ b/main.c @@ -13,7 +13,6 @@ #include "delpass.h" #include "genpass.h" #include "otppass.h" -void helpme(); const char* sofname = "sp"; const char* version = "1.2.0"; @@ -21,7 +20,7 @@ const char* version = "1.2.0"; void helpme() { printf("076 %s %s - シンプルなパスワードマネージャー\n", sofname, version); printf("https://076.moe/ | https://gitler.moe/suwako/%s\n\n", sofname); - puts ("使い方:\n"); + puts ("使い方:"); printf("%s -i :GPGと使ってパスワードストレージを初期設定\n", sofname); printf("%s -s <パスワード名> :パスワードを表示\n", sofname); printf("%s -y <パスワード名> :パスワードを表示せずクリップボードにコピーする\n", sofname); @@ -30,14 +29,35 @@ void helpme() { printf("%s -d <パスワード名> :パスワードを削除\n", sofname); printf("%s -e <パスワード名> :パスワードを変更\n", sofname); printf("%s -g <文字数> [risk|secure] :希望文字数でパスワードをランダムに作成する。risk=英数字のみ(不安)、secure=英数字+特別文字(デフォルト)を使用\n", sofname); - printf("%s -o <パスワード名>\n :ワンタイムパスワード(TOTP)を表示。存在しなければ、創作する\n", sofname); + printf("%s -o <パスワード名> :ワンタイムパスワード(TOTP)を表示。存在しなければ、創作する\n", sofname); printf("%s -h :ヘルプを表示\n", sofname); printf("%s -v :バージョンを表示\n", sofname); } +void helpme_en() { + printf("076 %s %s - Simple Password Manager\n", sofname, version); + printf("https://076.moe/ | https://gitler.moe/suwako/%s\n", sofname); + puts ("When reporting issues, please report in Japanese.\n"); + puts ("Usage:"); + printf("%s -i : First setting for using GPG and password storage\n", sofname); + printf("%s -s : Show password\n", sofname); + printf("%s -y : Copy password to clipboard without show\n", sofname); + printf("%s -l : Show me list of password\n", sofname); + printf("%s -a : Add password\n", sofname); + printf("%s -d : Delete password\n", sofname); + printf("%s -e : Edit password\n", sofname); + printf("%s -g [risk|secure] : Randomly make password with hoped amount. Using risk = only english letter and number (abnoxious), secure = english letter and digit + special character (default)\n", sofname); + printf("%s -o : Show one time password. If not exist, construct\n", sofname); + printf("%s -h : Show help\n", sofname); + printf("%s -v : Show version\n", sofname); +} + int main (int argc, char* argv[]) { + char *lang = getenv("SP_LANG"); + if (argc < 2) { - helpme(); + if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); return 0; } @@ -48,7 +68,8 @@ int main (int argc, char* argv[]) { char basePath[512]; char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); + else perror("ホームディレクトリを受取に失敗"); return -1; } @@ -67,13 +88,17 @@ int main (int argc, char* argv[]) { 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 helpme(); + else { + if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + } } else if (argc == 3 && strcmp(argv[1], "-o") == 0) { char fullPath[512]; char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); + else perror("ホームディレクトリを受取に失敗"); return -1; } @@ -83,7 +108,10 @@ int main (int argc, char* argv[]) { otppass(fullPath); } else if (argc == 2 && strcmp(argv[1], "-v") == 0) printf("%s-%s\n", sofname, version); - else helpme(); + else { + if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + } return 0; } diff --git a/otppass.c b/otppass.c index 1630f47..a6a3148 100644 --- a/otppass.c +++ b/otppass.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,9 +8,12 @@ #include "otppass.h" unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { + char *lang = getenv("SP_LANG"); + const char* secret_start = strstr(otpauth_url, "secret="); if (!secret_start) { - fprintf(stderr, "OTPAuth URLの中に、シークレットを見つけられませんでした。\n"); + if (lang != NULL && 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; @@ -24,7 +28,8 @@ unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { } if (secret_end < secret_start) { - fprintf(stderr, "不正なシークレットの距離。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Illegal secret range"); + else perror("不正なシークレットの距離"); return NULL; } @@ -32,7 +37,8 @@ unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { char* secret_encoded = (char*)malloc(secret_len + 1); if (secret_encoded == NULL) { - fprintf(stderr, "メモリの役割に失敗。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリの役割に失敗"); return NULL; } @@ -43,7 +49,8 @@ unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) { free(secret_encoded); if (!secret_decoded) { - fprintf(stderr, "BASE32の復号化に失敗。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to decrypting of the BASE32"); + else perror("BASE32の復号化に失敗"); return NULL; } @@ -67,6 +74,8 @@ uint32_t generate_totp(const char *secret, uint64_t counter) { } void otppass(char* file) { + char *lang = getenv("SP_LANG"); + gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; @@ -75,38 +84,44 @@ void otppass(char* file) { gpgme_check_version(NULL); err = gpgme_new(&ctx); if (err) { - fprintf(stderr, "GPGMEを創作に失敗\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPG"); + else perror("GPGMEを創作に失敗"); exit(1); } err = gpgme_data_new_from_file(&in, file, 1); if (err) { - fprintf(stderr, "GPGファイルを読込に失敗\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to reading the GPG file"); + else perror("GPGファイルを読込に失敗"); exit(1); } err = gpgme_data_new(&out); if (err) { - fprintf(stderr, "GPGデータを読込に失敗\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to reading the GPG data"); + else perror("GPGデータを読込に失敗"); exit(1); } err = gpgme_op_decrypt(ctx, in, out); if (err) { - fprintf(stderr, "GPGを復号化に失敗\n"); + if (lang != NULL && 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); if (!secret) { - fprintf(stderr, "GPGを受取に失敗\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting the GPG"); + else perror("GPGを受取に失敗"); exit(1); } size_t decoded_len; unsigned char* secret_decoded = extract_secret(secret, &decoded_len); if (!secret_decoded) { - fprintf(stderr, "シークレットの抽出またはデコードに失敗しました\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to decoding or exporting secret"); + else perror("シークレットの抽出又はデコードに失敗しました"); free(secret); exit(1); } diff --git a/showpass.c b/showpass.c index 9355521..1262775 100644 --- a/showpass.c +++ b/showpass.c @@ -4,6 +4,7 @@ #include #include +#include #include "showpass.h" @@ -16,6 +17,8 @@ void clean_up(gpgme_ctx_t ctx, gpgme_data_t in, gpgme_data_t out, FILE* gpgfile, } void showpass(char* file) { + char *lang = getenv("SP_LANG"); + gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in = NULL, out = NULL; @@ -29,7 +32,8 @@ void showpass(char* file) { // GPGMEを創作 err = gpgme_new(&ctx); if (err) { - fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); + if (lang != NULL && 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; } @@ -39,7 +43,8 @@ void showpass(char* file) { // 暗号化したタイルを開く char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); + else perror("ホームディレクトリを受取に失敗"); return; } @@ -48,29 +53,37 @@ void showpass(char* file) { int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; char* gpgpath = malloc(alllen); if (gpgpath == NULL) { - perror("メモリを割当に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memeory"); + else perror("メモリを割当に失敗"); return; } snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); gpgfile = fopen(gpgpath, "rb"); if (gpgfile == NULL) { - perror("ファイルを開くに失敗。"); - printf("失敗したパス: %s\n", gpgpath); + if (lang != NULL && strncmp(lang, "en", 2) == 0) { + perror("Failed to opening file"); + fprintf(stderr, "Failing path: %s\n", gpgpath); + } else { + perror("ファイルを開くに失敗"); + fprintf(stderr, "失敗したパス: %s\n", gpgpath); + } free(gpgpath); return; } // ファイルからinデータオブジェクトを創作 if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR) { - fprintf(stderr, "GPGMEデータオブジェクトを創作に失敗。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object"); + else perror("GPGMEデータオブジェクトを創作に失敗"); clean_up(ctx, in, out, gpgfile, gpgpath); return; } // outデータオブジェクトを創作 if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) { - fprintf(stderr, "GPGMEデータオブジェクトを創作に失敗。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object"); + else perror("GPGMEデータオブジェクトを創作に失敗"); clean_up(ctx, in, out, gpgfile, gpgpath); return; } @@ -81,7 +94,8 @@ void showpass(char* file) { // 復号化して err = gpgme_op_decrypt(ctx, in, out); if (err) { - fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); + if (lang != NULL && 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); diff --git a/yankpass.c b/yankpass.c index cf0af8c..6f67aba 100644 --- a/yankpass.c +++ b/yankpass.c @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -9,9 +10,12 @@ #include "showpass.h" void yankpass(char* file) { + char *lang = getenv("SP_LANG"); + // Xセッションではない場合(例えば、SSH、TTY、Gayland等)、showpass()を実行して - if (getenv("DISPLAY") == NULL) { - printf("Xセッションではありませんので、「sp -s」を実行します。\n"); + if (getenv("DISPLAY") == NULL) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("There is no X session, so executing 'sp -s'."); + else puts("Xセッションではありませんので、「sp -s」を実行します。"); showpass(file); return; } @@ -29,7 +33,8 @@ void yankpass(char* file) { // GPGMEを創作 err = gpgme_new(&ctx); if (err) { - fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); + if (lang != NULL && 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; } @@ -39,7 +44,8 @@ void yankpass(char* file) { // 暗号化したタイルを開く char* homedir = getenv("HOME"); if (homedir == NULL) { - perror("ホームディレクトリを受取に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); + else perror("ホームディレクトリを受取に失敗"); return; } @@ -48,15 +54,21 @@ 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) { - perror("メモリを割当に失敗。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリを割当に失敗"); return; } snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); gpgfile = fopen(gpgpath, "rb"); if (gpgfile == NULL) { - perror("ファイルを開くに失敗。"); - printf("失敗したパス: %s\n", gpgpath); + if (lang != NULL && strncmp(lang, "en", 2) == 0) { + perror("Failed to opening the file"); + fprintf(stderr, "Failed path: %s\n", gpgpath); + } else { + perror("ファイルを開くに失敗"); + fprintf(stderr, "失敗したパス: %s\n", gpgpath); + } free(gpgpath); return; } @@ -68,7 +80,8 @@ void yankpass(char* file) { // 復号化して err = gpgme_op_decrypt(ctx, in, out); if (err) { - fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); + if (lang != NULL && strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to decryption: %s\n", gpgme_strerror(err)); + else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); // 掃除 fclose(gpgfile); @@ -89,7 +102,8 @@ void yankpass(char* file) { gpgme_data_release(in); gpgme_data_release(out); gpgme_release(ctx); - perror("クリップボードを見つけられませんでした。"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Could not found a clipboard"); + else perror("クリップボードを見つけられませんでした"); return; } @@ -109,7 +123,8 @@ void yankpass(char* file) { pclose(pipe); // 45秒後、クリップボードから削除する - printf("パスワードをクリップボードに追加しました。\n45秒後はクリップボードから取り消されます。\n"); + if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Added password to the clipboard.\nI will take it away from the clipboard after 45 second"); + else puts("パスワードをクリップボードに追加しました。\n45秒後はクリップボードから取り消されます"); sleep(45); system("echo -n | xclip -selection clipboard");