| 1 | #include <locale.h> |
| 2 | |
| 3 | #include "common.h" |
| 4 | #include "showpass.h" |
| 5 | |
| 6 | void clean_up( |
| 7 | gpgme_ctx_t ctx, |
| 8 | gpgme_data_t in, |
| 9 | gpgme_data_t out, |
| 10 | FILE *gpgfile, |
| 11 | char *gpgpath |
| 12 | ) { |
| 13 | if (gpgfile) fclose(gpgfile); |
| 14 | if (gpgpath) free(gpgpath); |
| 15 | gpgme_data_release(in); |
| 16 | gpgme_data_release(out); |
| 17 | gpgme_release(ctx); |
| 18 | } |
| 19 | |
| 20 | const char *showpass(char *file) { |
| 21 | char *lang = getlang(); |
| 22 | |
| 23 | gpgme_ctx_t ctx; |
| 24 | gpgme_error_t err; |
| 25 | gpgme_data_t in = NULL, out = NULL; |
| 26 | char *gpgpath = NULL; |
| 27 | FILE *gpgfile = NULL; |
| 28 | |
| 29 | |
| 30 | setlocale(LC_ALL, ""); |
| 31 | gpgme_check_version(NULL); |
| 32 | gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); |
| 33 | |
| 34 | |
| 35 | err = gpgme_new(&ctx); |
| 36 | if (err) { |
| 37 | if (strncmp(lang, "en", 2) == 0) |
| 38 | fprintf(stderr, "Failed to generate GPGME: %s\n", gpgme_strerror(err)); |
| 39 | else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); |
| 45 | if (err) { |
| 46 | if (strncmp(lang, "en", 2) == 0) |
| 47 | fprintf(stderr, "Failed to set OpenPGP protocol: %s\n", gpgme_strerror(err)); |
| 48 | else fprintf(stderr, "OpenPGP付トロコールの設置に失敗:%s\n", gpgme_strerror(err)); |
| 49 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | char *basedir = getbasedir(1); |
| 55 | char *ext = ".gpg"; |
| 56 | int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; |
| 57 | gpgpath = malloc(alllen); |
| 58 | if (gpgpath == NULL) { |
| 59 | if (strncmp(lang, "en", 2) == 0) |
| 60 | perror("Failed to allocate memeory"); |
| 61 | else perror("メモリを割当に失敗"); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); |
| 66 | |
| 67 | gpgfile = fopen(gpgpath, "rb"); |
| 68 | if (gpgfile == NULL) { |
| 69 | if (strncmp(lang, "en", 2) == 0) { |
| 70 | perror("Failed to open file"); |
| 71 | } else { |
| 72 | perror("ファイルを開くに失敗"); |
| 73 | } |
| 74 | free(gpgpath); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR) { |
| 80 | if (strncmp(lang, "en", 2) == 0) |
| 81 | perror("Failed to generate the GPGME data object"); |
| 82 | else perror("GPGMEデータオブジェクトを創作に失敗"); |
| 83 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) { |
| 89 | if (strncmp(lang, "en", 2) == 0) |
| 90 | perror("Failed to generate the GPGME data object"); |
| 91 | else perror("GPGMEデータオブジェクトを創作に失敗"); |
| 92 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | gpgme_data_new(&out); |
| 98 | |
| 99 | |
| 100 | err = gpgme_op_decrypt(ctx, in, out); |
| 101 | if (err) { |
| 102 | if (strncmp(lang, "en", 2) == 0) |
| 103 | fprintf(stderr, "Failed to decrypt: %s (source: %s)\n", |
| 104 | gpgme_strerror(err), gpgme_strsource(err)); |
| 105 | else fprintf(stderr, "復号化に失敗: %s (元: %s)\n", |
| 106 | gpgme_strerror(err), gpgme_strsource(err)); |
| 107 | |
| 108 | |
| 109 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | gpgme_data_seek(out, 0, SEEK_SET); |
| 115 | size_t bufsize = 512; |
| 116 | size_t totsize = 0; |
| 117 | char buffer[512]; |
| 118 | char *res = malloc(bufsize); |
| 119 | if (res == NULL) { |
| 120 | if (strncmp(lang, "en", 2) == 0) |
| 121 | perror("Failed to allocate memory"); |
| 122 | else perror("メモリを役割に失敗"); |
| 123 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | ssize_t read_bytes; |
| 128 | |
| 129 | while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) { |
| 130 | if (totsize + read_bytes >= bufsize) { |
| 131 | bufsize *= 2; |
| 132 | res = realloc(res, bufsize); |
| 133 | if (res == NULL) { |
| 134 | perror("Failed to reallocate memory"); |
| 135 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 136 | return NULL; |
| 137 | } |
| 138 | } |
| 139 | memcpy(res + totsize, buffer, read_bytes); |
| 140 | totsize += read_bytes; |
| 141 | } |
| 142 | |
| 143 | res[totsize] = '\0'; |
| 144 | |
| 145 | |
| 146 | clean_up(ctx, in, out, gpgfile, gpgpath); |
| 147 | return res; |
| 148 | } |