| 1 | #include "addpass.h" |
| 2 | |
| 3 | void cleanup(gpgme_ctx_t ctx, gpgme_key_t key, gpgme_data_t in, gpgme_data_t out) { |
| 4 | gpgme_data_release(in); |
| 5 | gpgme_data_release(out); |
| 6 | gpgme_release(ctx); |
| 7 | gpgme_key_release(key); |
| 8 | } |
| 9 | |
| 10 | void getpasswd(char *prompt, char *pw, size_t pwlen) { |
| 11 | struct termios old, new; |
| 12 | printf("%s", prompt); |
| 13 | |
| 14 | |
| 15 | tcgetattr(fileno(stdin), &old); |
| 16 | new = old; |
| 17 | |
| 18 | |
| 19 | new.c_lflag &= ~ECHO; |
| 20 | tcsetattr(fileno(stdin), TCSANOW, &new); |
| 21 | |
| 22 | |
| 23 | if (fgets(pw, pwlen, stdin) != NULL) { |
| 24 | |
| 25 | size_t len = strlen(pw); |
| 26 | if (len > 0 && pw[len - 1] == '\n') { |
| 27 | pw[len - 1] = '\0'; |
| 28 | } else { |
| 29 | |
| 30 | int c; |
| 31 | while ((c = getchar()) != '\n' && c != EOF); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | tcsetattr(fileno(stdin), TCSANOW, &old); |
| 37 | } |
| 38 | |
| 39 | int addpass(char *file) { |
| 40 | char *lang = getlang(); |
| 41 | |
| 42 | |
| 43 | char *basedir = getbasedir(1); |
| 44 | char *ext = ".gpg"; |
| 45 | |
| 46 | char pass[256]; |
| 47 | char knin[256]; |
| 48 | |
| 49 | int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; |
| 50 | char *gpgpathchk = malloc(alllen); |
| 51 | if (gpgpathchk == NULL) { |
| 52 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 53 | "Failed to allocate memory" : "メモリを割当に失敗"); |
| 54 | fprintf(stderr, "%s\n", ero); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | snprintf(gpgpathchk, alllen, "%s%s%s", basedir, file, ext); |
| 60 | |
| 61 | if (access(gpgpathchk, F_OK) != -1) { |
| 62 | if (strncmp(lang, "en", 2) == 0) |
| 63 | fprintf( |
| 64 | stderr, |
| 65 | "Password already exist.\nTo edit, please run ' sp -e %s '.\n", |
| 66 | file |
| 67 | ); |
| 68 | else |
| 69 | fprintf( |
| 70 | stderr, |
| 71 | "%s\n変更するには、「 sp -e %s 」を実行して下さい。\n", |
| 72 | "パスワードが既に存在しています。", |
| 73 | file |
| 74 | ); |
| 75 | free(gpgpathchk); |
| 76 | return -1; |
| 77 | } |
| 78 | free(gpgpathchk); |
| 79 | |
| 80 | |
| 81 | if (strncmp(lang, "en", 2) == 0) |
| 82 | getpasswd("Password: ", pass, sizeof(pass)); |
| 83 | else getpasswd("パスワード: ", pass, sizeof(pass)); |
| 84 | puts(""); |
| 85 | if (strncmp(lang, "en", 2) == 0) |
| 86 | getpasswd("Password (for verification): ", knin, sizeof(knin)); |
| 87 | else getpasswd("パスワード(確認用): ", knin, sizeof(knin)); |
| 88 | puts(""); |
| 89 | |
| 90 | |
| 91 | if (strcmp(pass, knin) != 0) { |
| 92 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 93 | "Password does not match. Terminating..." : |
| 94 | "パスワードが一致していません。終了…"); |
| 95 | fprintf(stderr, "%s\n", ero); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | gpgme_ctx_t ctx; |
| 101 | gpgme_error_t err; |
| 102 | gpgme_key_t key[2] = {NULL, NULL}; |
| 103 | gpgme_data_t in, out; |
| 104 | FILE *gpgfile; |
| 105 | |
| 106 | |
| 107 | setlocale(LC_ALL, ""); |
| 108 | gpgme_check_version(NULL); |
| 109 | gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); |
| 110 | |
| 111 | |
| 112 | err = gpgme_new(&ctx); |
| 113 | if (err) { |
| 114 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 115 | "Failed to generate GPGME" : "GPGMEを創作に失敗"); |
| 116 | fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | err = gpgme_set_pinentry_mode(ctx, GPGME_PINENTRY_MODE_LOOPBACK); |
| 122 | if (err) { |
| 123 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 124 | "Failed to set pinentry mode" : "pinentryモードを設定に失敗"); |
| 125 | fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); |
| 126 | gpgme_release(ctx); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | err = gpgme_data_new_from_mem(&in, pass, strlen(pass), 0); |
| 132 | if (err) { |
| 133 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 134 | "Failed to make data object" : "データオブジェクトを創作に失敗"); |
| 135 | fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); |
| 136 | gpgme_release(ctx); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | gpgme_data_new(&out); |
| 141 | |
| 142 | |
| 143 | char keypath[256]; |
| 144 | snprintf(keypath, sizeof(keypath), "%s%s", basedir, ".gpg-id"); |
| 145 | |
| 146 | keypath[sizeof(keypath) - 1] = '\0'; |
| 147 | |
| 148 | FILE* keyfile = fopen(keypath, "rb"); |
| 149 | if (keyfile == NULL) { |
| 150 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 151 | "Failed to open .gpg-id file" : ".gpg-idファイルを開くに失敗"); |
| 152 | fprintf(stderr, "%s\n", ero); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | char *keyid = malloc(256); |
| 157 | if (!fgets(keyid, 256, keyfile)) { |
| 158 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 159 | "Failed to reading key ID" : "鍵IDを読込に失敗"); |
| 160 | fprintf(stderr, "%s\n", ero); |
| 161 | fclose(keyfile); |
| 162 | free(keyid); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | keyid[strcspn(keyid, "\n")] = 0; |
| 167 | fclose(keyfile); |
| 168 | |
| 169 | err = gpgme_get_key(ctx, keyid, &key[0], 0); |
| 170 | if (err) { |
| 171 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 172 | "Failed to get key" : "鍵Dを受取に失敗"); |
| 173 | fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); |
| 174 | free(keyid); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | if (key[0] == NULL) { |
| 179 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 180 | "Error: Key is NULL" : "エラー:鍵はNULLです"); |
| 181 | fprintf(stderr, "%s\n", ero); |
| 182 | free(keyid); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | free(keyid); |
| 187 | |
| 188 | |
| 189 | err = gpgme_op_encrypt(ctx, &key[0], GPGME_ENCRYPT_ALWAYS_TRUST, in, out); |
| 190 | if (err) { |
| 191 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 192 | "Failed to encrypt" : "暗号化に失敗"); |
| 193 | fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); |
| 194 | cleanup(ctx, key[0], in, out); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | char *gpgpath = malloc(alllen); |
| 200 | if (gpgpath == NULL) { |
| 201 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 202 | "Failed to allocate memory" : "メモリを割当に失敗"); |
| 203 | fprintf(stderr, "%s\n", ero); |
| 204 | cleanup(ctx, key[0], in, out); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | char dirpath[512]; |
| 210 | snprintf(dirpath, sizeof(dirpath), "%s%s", basedir, file); |
| 211 | |
| 212 | dirpath[sizeof(dirpath) - 1] = '\0'; |
| 213 | |
| 214 | char *lastsla = strrchr(dirpath, '/'); |
| 215 | if (lastsla != NULL) { |
| 216 | *lastsla = '\0'; |
| 217 | if (mkdir_r(dirpath, 0755) != 0) { |
| 218 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 219 | "Failed to create directory" : "ディレクトリを創作に失敗"); |
| 220 | fprintf(stderr, "%s\n", ero); |
| 221 | free(gpgpath); |
| 222 | cleanup(ctx, key[0], in, out); |
| 223 | return -1; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); |
| 228 | |
| 229 | struct stat statbuf; |
| 230 | if (stat(gpgpath, &statbuf) == 0) { |
| 231 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 232 | "Password already exists" : "パスワードは既に存在しています"); |
| 233 | fprintf(stderr, "%s\n", ero); |
| 234 | free(gpgpath); |
| 235 | cleanup(ctx, key[0], in, out); |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | gpgfile = fopen(gpgpath, "wb"); |
| 240 | if (gpgfile == NULL) { |
| 241 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 242 | "Failed to open file." : "ファイルを開くに失敗。"); |
| 243 | fprintf(stderr, "%s\n", ero); |
| 244 | free(gpgpath); |
| 245 | cleanup(ctx, key[0], in, out); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | ssize_t encrypted_data_size = gpgme_data_seek(out, 0, SEEK_END); |
| 251 | if (encrypted_data_size <= 0) { |
| 252 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 253 | "Failed to store the data" : "データを保存に失敗"); |
| 254 | fprintf(stderr, "%s\n", ero); |
| 255 | fclose(gpgfile); |
| 256 | free(gpgpath); |
| 257 | cleanup(ctx, key[0], in, out); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | gpgme_data_seek(out, 0, SEEK_SET); |
| 263 | |
| 264 | char buffer[512]; |
| 265 | ssize_t read_bytes; |
| 266 | |
| 267 | while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) { |
| 268 | if (fwrite(buffer, 1, (size_t)read_bytes, gpgfile) != (size_t)read_bytes) { |
| 269 | const char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 270 | "Failed to write password" : "パスワードを書き込みに失敗"); |
| 271 | fprintf(stderr, "%s\n", ero); |
| 272 | free(gpgpath); |
| 273 | cleanup(ctx, key[0], in, out); |
| 274 | return -1; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | fclose(gpgfile); |
| 280 | free(gpgpath); |
| 281 | cleanup(ctx, key[0], in, out); |
| 282 | |
| 283 | const char *msg = (strncmp(lang, "en", 2) == 0 ? |
| 284 | "The password got saved." : "パスワードを保存出来ました。"); |
| 285 | printf("%s\n", msg); |
| 286 | |
| 287 | return 0; |
| 288 | } |