2023-11-29 21:19:53 +09:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2023-12-01 02:25:27 +09:00
|
|
|
|
#include <stdbool.h>
|
2023-11-29 21:19:53 +09:00
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
|
|
#include <gpgme.h>
|
2024-02-02 12:15:58 +09:00
|
|
|
|
#include <string.h>
|
2023-11-29 21:19:53 +09:00
|
|
|
|
|
|
|
|
|
#include "showpass.h"
|
|
|
|
|
|
2023-11-30 00:10:25 +09:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 21:19:53 +09:00
|
|
|
|
void showpass(char* file) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
char *lang = getenv("SP_LANG");
|
|
|
|
|
|
2023-11-29 21:19:53 +09:00
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
|
gpgme_error_t err;
|
2023-12-01 13:26:30 +09:00
|
|
|
|
gpgme_data_t in = NULL, out = NULL;
|
2023-11-29 21:19:53 +09:00
|
|
|
|
FILE *gpgfile;
|
|
|
|
|
|
|
|
|
|
// GPGMEライブラリを設置
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
gpgme_check_version(NULL);
|
|
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
|
|
|
|
|
|
|
|
|
// GPGMEを創作
|
|
|
|
|
err = gpgme_new(&ctx);
|
|
|
|
|
if (err) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
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));
|
2023-11-29 21:19:53 +09:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OpenPGPプロトコールを設定
|
|
|
|
|
gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
|
|
|
|
|
|
|
|
|
|
// 暗号化したタイルを開く
|
|
|
|
|
char* homedir = getenv("HOME");
|
|
|
|
|
if (homedir == NULL) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory");
|
|
|
|
|
else perror("ホームディレクトリを受取に失敗");
|
2023-11-29 21:19:53 +09:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
if (gpgpath == NULL) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memeory");
|
|
|
|
|
else perror("メモリを割当に失敗");
|
2023-11-29 21:19:53 +09:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 13:26:30 +09:00
|
|
|
|
snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext);
|
2023-11-29 21:19:53 +09:00
|
|
|
|
gpgfile = fopen(gpgpath, "rb");
|
|
|
|
|
if (gpgfile == NULL) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
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);
|
|
|
|
|
}
|
2023-11-29 21:19:53 +09:00
|
|
|
|
free(gpgpath);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-30 00:10:25 +09:00
|
|
|
|
|
2023-12-01 13:26:30 +09:00
|
|
|
|
// ファイルからinデータオブジェクトを創作
|
|
|
|
|
if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object");
|
|
|
|
|
else perror("GPGMEデータオブジェクトを創作に失敗");
|
2023-12-01 13:26:30 +09:00
|
|
|
|
clean_up(ctx, in, out, gpgfile, gpgpath);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// outデータオブジェクトを創作
|
|
|
|
|
if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object");
|
|
|
|
|
else perror("GPGMEデータオブジェクトを創作に失敗");
|
2023-11-30 00:10:25 +09:00
|
|
|
|
clean_up(ctx, in, out, gpgfile, gpgpath);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-29 21:19:53 +09:00
|
|
|
|
|
|
|
|
|
// データオブジェクトを創作
|
|
|
|
|
gpgme_data_new(&out);
|
|
|
|
|
|
|
|
|
|
// 復号化して
|
|
|
|
|
err = gpgme_op_decrypt(ctx, in, out);
|
|
|
|
|
if (err) {
|
2024-02-02 12:15:58 +09:00
|
|
|
|
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));
|
2023-11-29 21:19:53 +09:00
|
|
|
|
|
|
|
|
|
// 掃除
|
2023-11-30 00:10:25 +09:00
|
|
|
|
clean_up(ctx, in, out, gpgfile, gpgpath);
|
2023-11-29 21:19:53 +09:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 復号化したパスワードを表示する
|
|
|
|
|
gpgme_data_seek(out, 0, SEEK_SET);
|
|
|
|
|
char buffer[512];
|
|
|
|
|
ssize_t read_bytes;
|
2023-12-01 02:25:27 +09:00
|
|
|
|
bool islastnl = false;
|
|
|
|
|
|
2023-11-30 00:10:25 +09:00
|
|
|
|
while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) {
|
2023-12-01 02:25:27 +09:00
|
|
|
|
fwrite(buffer, 1, read_bytes, stdout);
|
|
|
|
|
if (buffer[read_bytes - 1] == '\n') {
|
|
|
|
|
islastnl = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!islastnl) {
|
|
|
|
|
putchar('\n');
|
2023-11-29 21:19:53 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 掃除
|
2023-11-30 00:10:25 +09:00
|
|
|
|
clean_up(ctx, in, out, gpgfile, gpgpath);
|
2023-11-29 21:19:53 +09:00
|
|
|
|
}
|