#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;
}
char* basedir = "/.local/share/sp/";
+ char* ext = ".gpg";
// 鍵を受け取る
char keypath[256];
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;
}
*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;
}
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;
}
// 掃除
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");
}
#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;
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);
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);
}