suwako / sp アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
sp / src / yankpass.c
4.1 KB、154行 生 Blame 履歴
1#include <locale.h>
2#include <unistd.h>
3
4#include "common.h"
5#include "yankpass.h"
6#include "showpass.h"
7
8void yankpass(char *file, int copyTimeout) {
9 if (copyTimeout > 300) copyTimeout = 300;
10 char *lang = getlang();
11
12 int isGay = (getenv("WAYLAND_DISPLAY") != NULL);
13
14 // Xセッションではない場合(例えば、SSH、TTY等)、showpass()を実行して
15 if (getenv("DISPLAY") == NULL && getenv("WAYLAND_DISPLAY") == NULL) {
16 if (strncmp(lang, "en", 2) == 0)
17 puts("There is no X or Wayland session, so running 'sp -s'.");
18 else puts("X又はWaylandセッションではありませんので、「sp -s」を実行します。");
19 showpass(file);
20 return;
21 }
22
23 gpgme_ctx_t ctx;
24 gpgme_error_t err;
25 gpgme_data_t in, out;
26 FILE *gpgfile;
27
28 // GPGMEライブラリを設置
29 setlocale(LC_ALL, "");
30 gpgme_check_version(NULL);
31 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
32
33 // GPGMEを創作
34 err = gpgme_new(&ctx);
35 if (err) {
36 if (strncmp(lang, "en", 2) == 0)
37 fprintf(stderr, "Failed to generate GPGME: %s\n", gpgme_strerror(err));
38 else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err));
39 return;
40 }
41
42 // CTRL + Cを押す場合
43 signal(SIGINT, handle_sigint);
44
45 // OpenPGPプロトコールを設定
46 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
47
48 // 暗号化したタイルを開く
49 char *basedir = getbasedir(1);
50 char* ext = ".gpg";
51 int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1;
52 char* gpgpath = malloc(alllen);
53 if (gpgpath == NULL) {
54 if (strncmp(lang, "en", 2) == 0)
55 perror("Failed to allocate memory");
56 else perror("メモリを割当に失敗");
57 return;
58 }
59
60 snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext);
61 gpgfile = fopen(gpgpath, "rb");
62 if (gpgfile == NULL) {
63 if (strncmp(lang, "en", 2) == 0) {
64 perror("Failed to open the file");
65 fprintf(stderr, "Failed path: %s\n", gpgpath);
66 } else {
67 perror("ファイルを開くに失敗");
68 fprintf(stderr, "失敗したパス: %s\n", gpgpath);
69 }
70 free(gpgpath);
71 return;
72 }
73 gpgme_data_new_from_stream(&in, gpgfile);
74
75 // データオブジェクトを創作
76 gpgme_data_new(&out);
77
78 // 復号化して
79 err = gpgme_op_decrypt(ctx, in, out);
80 if (err) {
81 if (strncmp(lang, "en", 2) == 0)
82 fprintf(stderr, "Failed to decrypt: %s\n", gpgme_strerror(err));
83 else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err));
84
85 // 掃除
86 fclose(gpgfile);
87 free(gpgpath);
88 gpgme_data_release(in);
89 gpgme_data_release(out);
90 gpgme_release(ctx);
91 return;
92 }
93
94 // xclip又はwl-copyを準備して
95 char *cmd;
96 if (isGay) cmd = "wl-copy";
97 else cmd = "xclip -selection clipboard";
98
99 FILE *pipe = popen(cmd, "w");
100 if (pipe == NULL) {
101 // 掃除
102 fclose(gpgfile);
103 free(gpgpath);
104 gpgme_data_release(in);
105 gpgme_data_release(out);
106 gpgme_release(ctx);
107 if (strncmp(lang, "en", 2) == 0)
108 perror("Could not find a clipboard");
109 else perror("クリップボードを見つけられませんでした");
110 return;
111 }
112
113 // 復号化したパスワードを表示する
114 gpgme_data_seek(out, 0, SEEK_SET);
115
116 char buffer[512];
117 ssize_t read_bytes;
118
119 while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) {
120 if (buffer[read_bytes - 1] == '\n') {
121 read_bytes--;
122 }
123 fwrite(buffer, 1, read_bytes, pipe);
124 }
125
126 pclose(pipe);
127
128 // 何(デフォルトは45)秒後、クリップボードから削除する
129 if (strncmp(lang, "en", 2) == 0)
130 printf(
131 "%s\n%s%d%s\n",
132 "Added password to the clipboard.",
133 "After ",
134 copyTimeout,
135 " seconds it'll be deleted from the clipboard."
136 );
137 else
138 printf(
139 "%s\n%d%s\n",
140 "パスワードをクリップボードに追加しました。",
141 copyTimeout,
142 "秒後はクリップボードから取り消されます。"
143 );
144 sleep(copyTimeout);
145 if (isGay) system("echo -n \"\" | wl-copy");
146 else system("echo -n \"\" | xclip -selection clipboard");
147
148 // 掃除
149 fclose(gpgfile);
150 free(gpgpath);
151 gpgme_data_release(in);
152 gpgme_data_release(out);
153 gpgme_release(ctx);
154}