-
Notifications
You must be signed in to change notification settings - Fork 14
/
otr_key.c
302 lines (249 loc) · 6.66 KB
/
otr_key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Off-the-Record Messaging (OTR) module for the irssi IRC client
* Copyright (C) 2008 Uli Meis <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/
#define _GNU_SOURCE
#include "otr.h"
#include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <signal.h>
extern OtrlUserState otr_state;
typedef enum { KEYGEN_NO, KEYGEN_RUNNING } keygen_status_t;
struct {
keygen_status_t status;
char *accountname;
char *protocol;
time_t started;
GIOChannel *ch[2];
guint cpid,cwid;
pid_t pid;
} kg_st = {.status = KEYGEN_NO };
void keygen_childwatch(GPid pid,gint status, gpointer data) {
struct pollfd pfd = {
.fd = g_io_channel_unix_get_fd(kg_st.ch[0]),
.events = POLLIN };
int ret;
/* nothing to do if keygen_complete has already been called */
if (data)
return;
kg_st.pid = 0;
ret = poll(&pfd,1,0);
/* data is there, let's wait for keygen_complete to be called */
if (ret == 1)
return;
/* no data, report error and reset kg_st */
if (ret==0) {
if (WIFSIGNALED(status)) {
char sigstr[16];
sprintf(sigstr,
#ifndef HAVE_STRSIGNAL
"%d",WTERMSIG(status));
#else
"%s",strsignal(WTERMSIG(status)));
#endif
otr_noticest(TXT_KG_EXITSIG,
kg_st.accountname,
sigstr);
}
else
otr_noticest(TXT_KG_EXITED,kg_st.accountname);
} else if (ret==-1)
otr_noticest(TXT_KG_POLLERR,kg_st.accountname,strerror(errno));
keygen_abort(FALSE);
}
/*
* Installed as g_io_watch and called when the key generation
* process finishs.
*/
gboolean keygen_complete(GIOChannel *source, GIOCondition condition,
gpointer data)
{
gcry_error_t err;
const char *irssidir = get_irssi_dir();
char *filename = g_strconcat(irssidir,KEYFILE,NULL);
char *tmpfilename = g_strconcat(irssidir,TMPKEYFILE,NULL);
read(g_io_channel_unix_get_fd(kg_st.ch[0]),&err,sizeof(err));
g_io_channel_shutdown(kg_st.ch[0],FALSE,NULL);
g_io_channel_shutdown(kg_st.ch[1],FALSE,NULL);
g_io_channel_unref(kg_st.ch[0]);
g_io_channel_unref(kg_st.ch[1]);
if (err)
otr_noticest(TXT_KG_FAILED,
kg_st.accountname,
gcry_strerror(err),
gcry_strsource(err));
else {
/* reload keys */
otr_noticest(TXT_KG_COMPLETED,
kg_st.accountname,
time(NULL)-kg_st.started);
rename(tmpfilename,filename);
//otrl_privkey_forget_all(otr_state); <-- done by lib
key_load();
}
g_source_remove(kg_st.cwid);
kg_st.cwid = g_child_watch_add(kg_st.pid,keygen_childwatch,(void*)1);
kg_st.status = KEYGEN_NO;
g_free(kg_st.accountname);
g_free(filename);
g_free(tmpfilename);
return FALSE;
}
/*
* Run key generation in a seperate process (takes ages).
* The other process will rewrite the key file, we shouldn't
* change anything till it's done and we've reloaded the keys.
*/
void keygen_run(const char *accname)
{
gcry_error_t err;
int ret;
int fds[2];
char *filename = g_strconcat(get_irssi_dir(),TMPKEYFILE,NULL);
char *dir = dirname(g_strdup(filename));
if (kg_st.status!=KEYGEN_NO) {
if (strcmp(accname,kg_st.accountname)!=0)
otr_noticest(TXT_KG_ABORTED_DUP,
accname,kg_st.accountname);
return;
}
if (!g_file_test(dir, G_FILE_TEST_EXISTS)) {
if (g_mkdir(dir,S_IRWXU)) {
otr_noticest(TXT_KG_ABORTED_DIR,
accname,dir,strerror(errno));
g_free(dir);
g_free(filename);
return;
} else
otr_noticest(TXT_KG_MKDIR,dir);
}
g_free(dir);
if (pipe(fds) != 0) {
otr_noticest(TXT_KG_PIPE,
accname,strerror(errno));
g_free(filename);
return;
}
kg_st.ch[0] = g_io_channel_unix_new(fds[0]);
kg_st.ch[1] = g_io_channel_unix_new(fds[1]);
kg_st.accountname = g_strdup(accname);
kg_st.protocol = PROTOCOLID;
kg_st.started = time(NULL);
if ((ret = fork())) {
g_free(filename);
if (ret==-1) {
otr_noticest(TXT_KG_FORK,
accname,strerror(errno));
return;
}
kg_st.status = KEYGEN_RUNNING;
kg_st.pid = ret;
otr_noticest(TXT_KG_INITIATED,
accname);
kg_st.cpid = g_io_add_watch(kg_st.ch[0], G_IO_IN,
(GIOFunc) keygen_complete, NULL);
kg_st.cwid = g_child_watch_add(kg_st.pid,keygen_childwatch,NULL);
kg_st.started = time(NULL);
return;
}
/* child */
err = otrl_privkey_generate(otr_state,filename,accname,PROTOCOLID);
write(fds[1],&err,sizeof(err));
//g_free(filename);
_exit(0);
}
/*
* Abort ongoing key generation.
*/
void keygen_abort(int ignoreidle)
{
if (kg_st.status!=KEYGEN_RUNNING) {
if (!ignoreidle)
otr_noticest(TXT_KG_NOABORT);
return;
}
otr_noticest(TXT_KG_ABORT,kg_st.accountname);
g_source_remove(kg_st.cpid);
g_source_remove(kg_st.cwid);
g_free(kg_st.accountname);
if (kg_st.pid!=0) {
kill(kg_st.pid,SIGTERM);
g_child_watch_add(kg_st.pid,keygen_childwatch,(void*)1);
}
kg_st.status = KEYGEN_NO;
}
/*
* Write fingerprints to file.
*/
void otr_writefps()
{
gcry_error_t err;
char *filename = g_strconcat(get_irssi_dir(),FPSFILE,NULL);
err = otrl_privkey_write_fingerprints(otr_state,filename);
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_FP_SAVED);
} else {
otr_noticest(TXT_FP_SAVE_ERROR,
gcry_strerror(err),
gcry_strsource(err));
}
g_free(filename);
}
/*
* Load private keys.
*/
void key_load()
{
gcry_error_t err;
char *filename = g_strconcat(get_irssi_dir(),KEYFILE,NULL);
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
otr_noticest(TXT_KEY_NOT_FOUND);
return;
}
err = otrl_privkey_read(otr_state, filename);
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_KEY_LOADED);
} else {
otr_noticest(TXT_KEY_LOAD_ERROR,
gcry_strerror(err),
gcry_strsource(err));
}
g_free(filename);
}
/*
* Load fingerprints.
*/
void fps_load()
{
gcry_error_t err;
char *filename = g_strconcat(get_irssi_dir(),FPSFILE,NULL);
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
otr_noticest(TXT_FP_NOT_FOUND);
return;
}
err = otrl_privkey_read_fingerprints(otr_state,filename,NULL,NULL);
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_FP_LOADED);
} else {
otr_noticest(TXT_FP_LOAD_ERROR,
gcry_strerror(err),
gcry_strsource(err));
}
g_free(filename);
}