From: 諏訪子 Date: Wed, 29 Nov 2023 15:10:25 +0000 (+0900) Subject: ちんちん X-Git-Tag: sp-1.0.0~3 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=40c014dcb01253dc1e5a1bdcfd24da320f515dc7;p=sp.git ちんちん --- diff --git a/addpass.c b/addpass.c index 8a207af..3ac8e01 100644 --- a/addpass.c +++ b/addpass.c @@ -11,6 +11,13 @@ #include "addpass.h" +void cleanup(gpgme_ctx_t ctx, gpgme_key_t key, gpgme_data_t in, gpgme_data_t out) { + gpgme_data_release(in); + gpgme_data_release(out); + gpgme_release(ctx); + gpgme_key_release(key); +} + int mkdir_r(const char *path, mode_t mode) { char tmp[256]; char *p = NULL; @@ -120,6 +127,7 @@ void addpass(char* file) { } char* basedir = "/.local/share/sp/"; + char* ext = ".gpg"; // 鍵を受け取る char keypath[256]; @@ -163,22 +171,15 @@ 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)); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); - gpgme_key_release(key[0]); + cleanup(ctx, key[0], in, out); return; } // 暗号化したタイルを開く - char* ext = ".gpg"; int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; char* gpgpath = malloc(alllen); if (gpgpath == NULL) { - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); - gpgme_key_release(key[0]); + cleanup(ctx, key[0], in, out); perror("メモリを割当に失敗。"); return; } @@ -193,25 +194,19 @@ void addpass(char* file) { *lastsla = '\0'; if (mkdir_r(dirpath, 0755) != 0) { free(gpgpath); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); - gpgme_key_release(key[0]); + cleanup(ctx, key[0], in, out); perror("ディレクトリを創作に失敗。"); return; } } - sprintf(gpgpath, "%s%s%s%s", homedir, basedir, file, ext); + snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); gpgfile = fopen(gpgpath, "wb"); if (gpgfile == NULL) { perror("ファイルを開くに失敗。"); printf("失敗したパス: %s\n", gpgpath); free(gpgpath); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); - gpgme_key_release(key[0]); + cleanup(ctx, key[0], in, out); return; } @@ -221,10 +216,7 @@ void addpass(char* file) { fprintf(stderr, "データを保存に失敗。\n"); fclose(gpgfile); free(gpgpath); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); - gpgme_key_release(key[0]); + cleanup(ctx, key[0], in, out); return; } @@ -240,10 +232,7 @@ void addpass(char* file) { // 掃除 fclose(gpgfile); free(gpgpath); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); - gpgme_key_release(key[0]); + cleanup(ctx, key[0], in, out); printf("パスワードを保存出来ました。\n"); } diff --git a/main.c b/main.c index a1ccf4e..d2f8682 100644 --- a/main.c +++ b/main.c @@ -35,7 +35,7 @@ void helpme() { int main (int argc, char* argv[]) { if (argc < 2) { helpme(); - return 1; + return 0; } if (argc == 3 && strcmp(argv[1], "-i") == 0) printf("TODO: 初期設定\n"); diff --git a/showpass.c b/showpass.c index 8cb8dc8..e9a01d4 100644 --- a/showpass.c +++ b/showpass.c @@ -6,6 +6,14 @@ #include "showpass.h" +void clean_up(gpgme_ctx_t ctx, gpgme_data_t in, gpgme_data_t out, FILE* gpgfile, char* gpgpath) { + if (gpgfile) fclose(gpgfile); + if (gpgpath) free(gpgpath); + gpgme_data_release(in); + gpgme_data_release(out); + gpgme_release(ctx); +} + void showpass(char* file) { gpgme_ctx_t ctx; gpgme_error_t err; @@ -51,7 +59,12 @@ void showpass(char* file) { free(gpgpath); return; } - gpgme_data_new_from_stream(&in, gpgfile); + + if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR || gpgme_data_new(&out) != GPG_ERR_NO_ERROR) { + fprintf(stderr, "GPGMEデータオブジェクトを創作に失敗。\n"); + clean_up(ctx, in, out, gpgfile, gpgpath); + return; + } // データオブジェクトを創作 gpgme_data_new(&out); @@ -62,29 +75,20 @@ void showpass(char* file) { fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); // 掃除 - fclose(gpgfile); - free(gpgpath); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); + clean_up(ctx, in, out, gpgfile, gpgpath); return; } // 復号化したパスワードを表示する gpgme_data_seek(out, 0, SEEK_SET); - char buffer[512]; ssize_t read_bytes; - while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) { + while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) { buffer[read_bytes] = '\0'; printf("%s", buffer); } puts(""); // 掃除 - fclose(gpgfile); - free(gpgpath); - gpgme_data_release(in); - gpgme_data_release(out); - gpgme_release(ctx); + clean_up(ctx, in, out, gpgfile, gpgpath); }