author | Mikael Berthe <mikael@lilotux.net> |
Sat, 27 Oct 2007 13:01:03 +0200 | |
changeset 1335 | ab1b7f7a682e |
parent 1320 | cb400799db8f |
child 1343 | 8a05feb23c5a |
permissions | -rw-r--r-- |
1299 | 1 |
/* |
1303
b6fdbfa6b219
Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents:
1301
diff
changeset
|
2 |
* otr.c -- Off-The-Record Messaging for mcabber |
1299 | 3 |
* |
4 |
* Copyright (C) 2007 Frank Zschockelt <mcabber_otr@freakysoft.de> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
||
22 |
#include <config.h> |
|
23 |
||
24 |
#ifdef HAVE_LIBOTR |
|
25 |
||
26 |
#include "otr.h" |
|
27 |
#include "logprint.h" |
|
28 |
#include "hbuf.h" |
|
29 |
#include "jab_priv.h" |
|
30 |
#include "roster.h" |
|
31 |
#include "utils.h" |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
32 |
#include "screen.h" |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
33 |
#include "settings.h" |
1299 | 34 |
|
35 |
||
36 |
static OtrlUserState userstate = NULL; |
|
37 |
static char * account = NULL; |
|
38 |
static char * keyfile = NULL; |
|
39 |
static char * fprfile = NULL; |
|
40 |
||
41 |
||
42 |
static OtrlPolicy cb_policy (void *opdata, ConnContext *ctx); |
|
43 |
static void cb_create_privkey (void *opdata, |
|
44 |
const char *accountname, |
|
45 |
const char *protocol); |
|
46 |
static int cb_is_logged_in (void *opdata, |
|
47 |
const char *accountname, |
|
48 |
const char *protocol, |
|
49 |
const char *recipient); |
|
50 |
static void cb_inject_message (void *opdata, |
|
51 |
const char *accountname, |
|
52 |
const char *protocol, |
|
53 |
const char *recipient, |
|
54 |
const char *message); |
|
55 |
static void cb_notify (void *opdata, |
|
56 |
OtrlNotifyLevel level, |
|
57 |
const char *accountname, |
|
58 |
const char *protocol, |
|
59 |
const char *username, |
|
60 |
const char *title, |
|
61 |
const char *primary, |
|
62 |
const char *secondary); |
|
63 |
static int cb_display_otr_message(void *opdata, |
|
64 |
const char *accountname, |
|
65 |
const char *protocol, |
|
66 |
const char *username, |
|
67 |
const char *msg); |
|
68 |
static void cb_update_context_list(void *opdata); |
|
69 |
static const char *cb_protocol_name (void *opdata, const char *protocol); |
|
70 |
static void cb_protocol_name_free (void *opdata, |
|
71 |
const char *protocol_name); |
|
72 |
static void cb_new_fingerprint (void *opdata, OtrlUserState us, |
|
73 |
const char *accountname, |
|
74 |
const char *protocol, |
|
75 |
const char *username, |
|
76 |
unsigned char fingerprint[20]); |
|
77 |
static void cb_write_fingerprints (void *opdata); |
|
78 |
static void cb_gone_secure (void *opdata, ConnContext *context); |
|
79 |
static void cb_gone_insecure (void *opdata, ConnContext *context); |
|
80 |
static void cb_still_secure (void *opdata, ConnContext *context, |
|
81 |
int is_reply); |
|
82 |
static void cb_log_message (void *opdata, const char *message); |
|
83 |
static int cb_max_message_size (void *opdata, ConnContext *context); |
|
84 |
||
85 |
static OtrlMessageAppOps ops = |
|
86 |
{ |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
87 |
cb_policy, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
88 |
cb_create_privkey, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
89 |
cb_is_logged_in, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
90 |
cb_inject_message, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
91 |
cb_notify, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
92 |
cb_display_otr_message, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
93 |
cb_update_context_list, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
94 |
cb_protocol_name, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
95 |
cb_protocol_name_free, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
96 |
cb_new_fingerprint, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
97 |
cb_write_fingerprints, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
98 |
cb_gone_secure, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
99 |
cb_gone_insecure, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
100 |
cb_still_secure, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
101 |
cb_log_message, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
102 |
cb_max_message_size, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
103 |
NULL, /*account_name*/ |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
104 |
NULL /*account_name_free*/ |
1299 | 105 |
}; |
106 |
||
107 |
static void otr_message_disconnect(ConnContext *ctx); |
|
108 |
static ConnContext * otr_get_context(const char *buddy); |
|
109 |
static void otr_startstop(const char * buddy, int start); |
|
110 |
static void otr_handle_smp_tlvs(OtrlTLV * tlvs, ConnContext * ctx); |
|
111 |
||
1320 | 112 |
static char * otr_get_dir(void); |
1299 | 113 |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
114 |
void otr_init(const char *jid) |
1299 | 115 |
{ |
1320 | 116 |
char * root = otr_get_dir(); |
1299 | 117 |
account = jidtodisp(jid); |
118 |
keyfile = g_strdup_printf("%s%s.key", root, account); |
|
119 |
fprfile = g_strdup_printf("%s%s.fpr", root, account); |
|
120 |
g_free(root); |
|
121 |
||
122 |
OTRL_INIT; |
|
123 |
||
124 |
userstate = otrl_userstate_create (); |
|
125 |
if (otrl_privkey_read(userstate, keyfile)){ |
|
126 |
scr_LogPrint(LPRINT_LOGNORM, "Could not read OTR key from %s", keyfile); |
|
127 |
cb_create_privkey(NULL, account, "jabber"); |
|
128 |
} |
|
129 |
if (otrl_privkey_read_fingerprints(userstate, fprfile, NULL, NULL)){ |
|
130 |
scr_LogPrint(LPRINT_LOGNORM, "Could not read OTR fingerprints from %s", |
|
131 |
fprfile); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
void otr_terminate(void) |
|
136 |
{ |
|
137 |
ConnContext * ctx; |
|
138 |
||
1320 | 139 |
for (ctx = userstate->context_root; ctx; ctx = ctx->next) |
1299 | 140 |
if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) |
141 |
otr_message_disconnect(ctx); |
|
142 |
||
143 |
g_free(account); |
|
144 |
account = NULL; |
|
1307
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
145 |
|
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
146 |
/* XXX This #ifdef is a quick workaround: when mcabber |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
147 |
* is linked to both gnutls and libotr, libgcrypt will |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
148 |
* segfault when we call otrl_userstate_free(). |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
149 |
* This is reported to be a bug in libgcrypt :-/ |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
150 |
* Mikael |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
151 |
*/ |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
152 |
#if defined(HAVE_GNUTLS) && !defined(HAVE_OPENSSL) |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
153 |
if (!settings_opt_get_int("ssl")) |
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
154 |
#endif |
1299 | 155 |
otrl_userstate_free(userstate); |
1307
6c116207ab2e
Work around segfault in otrl_userstate_free()
Mikael Berthe <mikael@lilotux.net>
parents:
1306
diff
changeset
|
156 |
|
1299 | 157 |
userstate = NULL; |
158 |
g_free(keyfile); |
|
159 |
keyfile = NULL; |
|
160 |
} |
|
161 |
||
1320 | 162 |
static char * otr_get_dir(void) |
163 |
{ |
|
164 |
char * configured_dir = (char *)settings_opt_get("otr_dir"); |
|
165 |
||
1335
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
166 |
if (configured_dir && *configured_dir) { |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
167 |
char *xp_conf_dir; |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
168 |
int l; |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
169 |
xp_conf_dir = expand_filename(configured_dir); |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
170 |
// The path must be slash-terminated |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
171 |
l = strlen(xp_conf_dir); |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
172 |
if (xp_conf_dir[l-1] != '/') { |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
173 |
char *xp_conf_dir_tmp = xp_conf_dir; |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
174 |
xp_conf_dir = g_strdup_printf("%s/", xp_conf_dir_tmp); |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
175 |
g_free(xp_conf_dir_tmp); |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
176 |
} |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
177 |
return xp_conf_dir; |
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
178 |
} else { |
1320 | 179 |
return expand_filename("~/.mcabber/otr/"); |
1335
ab1b7f7a682e
Make sure the otr_dir is always slash-terminated
Mikael Berthe <mikael@lilotux.net>
parents:
1320
diff
changeset
|
180 |
} |
1320 | 181 |
} |
182 |
||
1299 | 183 |
static ConnContext * otr_get_context(const char *buddy) |
184 |
{ |
|
185 |
int null = 0; |
|
186 |
return otrl_context_find(userstate, buddy, account, "jabber", 1, &null, |
|
187 |
NULL, NULL); |
|
188 |
} |
|
189 |
||
190 |
static void otr_message_disconnect(ConnContext *ctx) |
|
191 |
{ |
|
192 |
if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) |
|
193 |
cb_gone_insecure(NULL, ctx); |
|
194 |
otrl_message_disconnect(userstate, &ops, NULL, ctx->accountname, |
|
195 |
ctx->protocol, ctx->username); |
|
196 |
} |
|
197 |
||
198 |
static void otr_startstop(const char * buddy, int start) |
|
199 |
{ |
|
200 |
char * msg = NULL; |
|
201 |
ConnContext *ctx = otr_get_context(buddy); |
|
202 |
||
203 |
if (!userstate || !ctx) |
|
204 |
return; |
|
205 |
||
206 |
if (start && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) |
|
207 |
otr_message_disconnect(ctx); |
|
208 |
||
209 |
if (start) { |
|
210 |
OtrlPolicy policy = cb_policy(NULL, ctx); |
|
1308
b17754d9221b
Show a warning for /otr start|stop, if policy == plain
franky@veqlargh.fs
parents:
1307
diff
changeset
|
211 |
if (policy == plain) { |
b17754d9221b
Show a warning for /otr start|stop, if policy == plain
franky@veqlargh.fs
parents:
1307
diff
changeset
|
212 |
scr_LogPrint(LPRINT_LOGNORM, "The OTR policy for this user is set to" |
b17754d9221b
Show a warning for /otr start|stop, if policy == plain
franky@veqlargh.fs
parents:
1307
diff
changeset
|
213 |
" plain. You have to change it first."); |
b17754d9221b
Show a warning for /otr start|stop, if policy == plain
franky@veqlargh.fs
parents:
1307
diff
changeset
|
214 |
return; |
b17754d9221b
Show a warning for /otr start|stop, if policy == plain
franky@veqlargh.fs
parents:
1307
diff
changeset
|
215 |
} |
1299 | 216 |
msg = otrl_proto_default_query_msg(ctx->accountname, policy); |
217 |
cb_inject_message(NULL, ctx->accountname, ctx->protocol, ctx->username, |
|
218 |
msg); |
|
219 |
free (msg); |
|
220 |
} |
|
221 |
else |
|
222 |
otr_message_disconnect(ctx); |
|
223 |
} |
|
224 |
||
225 |
void otr_establish(const char *buddy) |
|
226 |
{ |
|
227 |
otr_startstop(buddy, 1); |
|
228 |
} |
|
229 |
||
230 |
void otr_disconnect(const char * buddy) |
|
231 |
{ |
|
232 |
otr_startstop(buddy, 0); |
|
233 |
} |
|
234 |
||
235 |
void otr_fingerprint(const char * buddy, const char * trust) |
|
236 |
{ |
|
237 |
char fpr[45], *tr; |
|
238 |
ConnContext *ctx = otr_get_context(buddy); |
|
239 |
if (!userstate || !ctx) |
|
240 |
return; |
|
241 |
||
242 |
if (!ctx->active_fingerprint || !ctx->active_fingerprint->fingerprint) { |
|
243 |
scr_LogPrint(LPRINT_LOGNORM, |
|
244 |
"No active fingerprint - start OTR for this buddy first."); |
|
245 |
return; |
|
246 |
} |
|
247 |
||
248 |
otrl_privkey_hash_to_human(fpr, ctx->active_fingerprint->fingerprint); |
|
249 |
if (trust) { |
|
250 |
if (strcmp(fpr, trust) == 0) |
|
251 |
otrl_context_set_trust(ctx->active_fingerprint, "trust"); |
|
252 |
else |
|
253 |
otrl_context_set_trust(ctx->active_fingerprint, NULL); |
|
254 |
} |
|
255 |
||
256 |
tr = ctx->active_fingerprint->trust; |
|
257 |
scr_LogPrint(LPRINT_LOGNORM, "%s [%44s]: %s", ctx->username, fpr, |
|
258 |
tr && *tr ? "trusted" : "untrusted"); |
|
259 |
cb_write_fingerprints(NULL); |
|
260 |
} |
|
261 |
||
262 |
static void otr_handle_smp_tlvs(OtrlTLV * tlvs, ConnContext * ctx) |
|
263 |
{ |
|
264 |
OtrlTLV *tlv = NULL; |
|
265 |
char *sbuf = NULL; |
|
266 |
NextExpectedSMP nextMsg = ctx->smstate->nextExpected; |
|
267 |
||
268 |
tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1); |
|
269 |
if (tlv) { |
|
270 |
if (nextMsg != OTRL_SMP_EXPECT1) |
|
271 |
otr_smp_abort(ctx->username); |
|
272 |
else { |
|
273 |
sbuf = g_strdup_printf("OTR: Received SMP Initiation. " |
|
274 |
"Answer with /otr smpr %s $secret", |
|
275 |
ctx->username); |
|
276 |
} |
|
277 |
} |
|
278 |
tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP2); |
|
279 |
if (tlv) { |
|
280 |
if (nextMsg != OTRL_SMP_EXPECT2) |
|
281 |
otr_smp_abort(ctx->username); |
|
282 |
else { |
|
283 |
sbuf = g_strdup("OTR: Received SMP Response."); |
|
284 |
/* If we received TLV2, we will send TLV3 and expect TLV4 */ |
|
285 |
ctx->smstate->nextExpected = OTRL_SMP_EXPECT4; |
|
286 |
} |
|
287 |
} |
|
288 |
tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP3); |
|
289 |
if (tlv) { |
|
290 |
if (nextMsg != OTRL_SMP_EXPECT3) |
|
291 |
otr_smp_abort(ctx->username); |
|
292 |
else { |
|
293 |
/* If we received TLV3, we will send TLV4 |
|
294 |
* We will not expect more messages, so prepare for next SMP */ |
|
295 |
ctx->smstate->nextExpected = OTRL_SMP_EXPECT1; |
|
296 |
/* Report result to user */ |
|
297 |
if (ctx->active_fingerprint && ctx->active_fingerprint->trust && |
|
298 |
*ctx->active_fingerprint->trust != '\0') |
|
299 |
sbuf = g_strdup("OTR: SMP succeeded"); |
|
300 |
else |
|
301 |
sbuf = g_strdup("OTR: SMP failed"); |
|
302 |
} |
|
303 |
} |
|
304 |
tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP4); |
|
305 |
if (tlv) { |
|
306 |
if (nextMsg != OTRL_SMP_EXPECT4) |
|
307 |
otr_smp_abort(ctx->username); |
|
308 |
else { |
|
309 |
/* We will not expect more messages, so prepare for next SMP */ |
|
310 |
ctx->smstate->nextExpected = OTRL_SMP_EXPECT1; |
|
311 |
/* Report result to user */ |
|
312 |
if (ctx->active_fingerprint && ctx->active_fingerprint->trust && |
|
313 |
*ctx->active_fingerprint->trust != '\0') |
|
314 |
sbuf = g_strdup("OTR: SMP succeeded"); |
|
315 |
else |
|
316 |
sbuf = g_strdup("OTR: SMP failed"); |
|
317 |
} |
|
318 |
} |
|
319 |
tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP_ABORT); |
|
320 |
if (tlv) { |
|
321 |
/* The message we are waiting for will not arrive, so reset |
|
322 |
* and prepare for the next SMP */ |
|
323 |
sbuf = g_strdup("OTR: SMP aborted by your buddy"); |
|
324 |
ctx->smstate->nextExpected = OTRL_SMP_EXPECT1; |
|
325 |
} |
|
326 |
||
327 |
if (sbuf) { |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
328 |
scr_WriteIncomingMessage(ctx->username, sbuf, 0, HBB_PREFIX_INFO, 0); |
1299 | 329 |
g_free(sbuf); |
330 |
} |
|
331 |
} |
|
332 |
||
333 |
/* |
|
334 |
* returns whether a otr_message was received |
|
335 |
* sets *otr_data to NULL, when it was an internal otr message |
|
336 |
*/ |
|
337 |
int otr_receive(char **otr_data, const char * buddy, int * free_msg) |
|
338 |
{ |
|
339 |
int ignore_message; |
|
340 |
char *newmessage = NULL; |
|
341 |
OtrlTLV *tlvs = NULL; |
|
342 |
OtrlTLV *tlv = NULL; |
|
343 |
ConnContext * ctx; |
|
344 |
||
345 |
*free_msg = 0; |
|
346 |
ignore_message = otrl_message_receiving(userstate, &ops, NULL, account, |
|
347 |
"jabber", buddy, *otr_data, &newmessage, &tlvs, NULL, NULL); |
|
348 |
||
349 |
ctx = otr_get_context(buddy); |
|
350 |
||
351 |
tlv = otrl_tlv_find(tlvs, OTRL_TLV_DISCONNECTED); |
|
352 |
if (tlv) { |
|
353 |
/* Notify the user that the other side disconnected. */ |
|
354 |
if (ctx) { |
|
355 |
cb_gone_insecure(NULL, ctx); |
|
356 |
otr_disconnect(buddy); |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
otr_handle_smp_tlvs(tlvs, ctx); |
|
361 |
||
362 |
if (tlvs != NULL) |
|
363 |
otrl_tlv_free(tlvs); |
|
364 |
||
365 |
if (ignore_message) |
|
366 |
*otr_data = NULL; |
|
367 |
||
368 |
if (!ignore_message && newmessage) { |
|
369 |
*free_msg = 1; |
|
370 |
*otr_data = g_strdup(newmessage); |
|
371 |
otrl_message_free(newmessage); |
|
372 |
if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) |
|
373 |
return 1; |
|
374 |
} |
|
375 |
return 0; |
|
376 |
} |
|
377 |
||
378 |
int otr_send(char **msg, const char *buddy) |
|
379 |
{ |
|
380 |
gcry_error_t err; |
|
381 |
char *newmessage = NULL; |
|
382 |
ConnContext * ctx = otr_get_context(buddy); |
|
383 |
||
384 |
err = otrl_message_sending(userstate, &ops, NULL, account, "jabber", buddy, |
|
385 |
*msg, NULL, &newmessage, NULL, NULL); |
|
386 |
||
387 |
if (err) |
|
388 |
*msg = NULL; /*something went wrong, don't send the plain-message! */ |
|
389 |
||
390 |
if (!err && newmessage) { |
|
391 |
*msg = g_strdup(newmessage); |
|
392 |
otrl_message_free(newmessage); |
|
393 |
if (cb_policy(NULL, ctx) & OTRL_POLICY_REQUIRE_ENCRYPTION || |
|
394 |
ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) |
|
395 |
return 1; |
|
396 |
} |
|
397 |
return 0; |
|
398 |
} |
|
399 |
||
400 |
/* Prints OTR connection state */ |
|
401 |
void otr_print_info(const char * buddy) |
|
402 |
{ |
|
403 |
const char *state, *auth, *policy; |
|
404 |
ConnContext * ctx = otr_get_context(buddy); |
|
405 |
OtrlPolicy p = cb_policy (ctx->app_data, ctx); |
|
406 |
||
407 |
if (!userstate || !ctx) |
|
408 |
return; |
|
409 |
||
410 |
switch (ctx->msgstate) { |
|
411 |
case OTRL_MSGSTATE_PLAINTEXT: state = "plaintext"; break; |
|
412 |
case OTRL_MSGSTATE_ENCRYPTED: |
|
413 |
switch (ctx->protocol_version) { |
|
414 |
case 1: state = "encrypted V1"; break; |
|
415 |
case 2: state = "encrypted V2"; break; |
|
416 |
default:state = "encrypted"; |
|
417 |
}; |
|
418 |
break; |
|
419 |
case OTRL_MSGSTATE_FINISHED: state = "finished"; break; |
|
420 |
default: state = "unknown state"; |
|
421 |
} |
|
422 |
switch (ctx->auth.authstate) { |
|
423 |
case OTRL_AUTHSTATE_NONE: |
|
424 |
switch (ctx->otr_offer) { |
|
425 |
case OFFER_NOT: auth = "no offer sent"; break; |
|
426 |
case OFFER_SENT: auth = "offer sent"; break; |
|
427 |
case OFFER_ACCEPTED: auth = "offer accepted"; break; |
|
428 |
case OFFER_REJECTED: auth = "offer rejected"; break; |
|
429 |
default: auth = "unknown auth"; |
|
430 |
} |
|
431 |
break; |
|
432 |
case OTRL_AUTHSTATE_AWAITING_DHKEY: |
|
433 |
auth = "awaiting D-H key"; break; |
|
434 |
case OTRL_AUTHSTATE_AWAITING_REVEALSIG: |
|
435 |
auth = "awaiting reveal signature"; break; |
|
436 |
case OTRL_AUTHSTATE_AWAITING_SIG: |
|
437 |
auth = "awaiting signature"; break; |
|
438 |
case OTRL_AUTHSTATE_V1_SETUP: |
|
439 |
auth = "v1 setup"; break; |
|
440 |
default: |
|
441 |
auth = "unknown auth"; |
|
442 |
} |
|
443 |
if (p == OTRL_POLICY_NEVER) |
|
444 |
policy = "plain"; |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
445 |
else if (p == (OTRL_POLICY_OPPORTUNISTIC & ~OTRL_POLICY_ALLOW_V1)) |
1299 | 446 |
policy = "opportunistic"; |
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
447 |
else if (p == (OTRL_POLICY_MANUAL & ~OTRL_POLICY_ALLOW_V1)) |
1299 | 448 |
policy = "manual"; |
449 |
else if (p == (OTRL_POLICY_ALWAYS & ~OTRL_POLICY_ALLOW_V1)) |
|
450 |
policy = "always"; |
|
451 |
else |
|
452 |
policy = "unknown"; |
|
453 |
||
454 |
scr_LogPrint(LPRINT_LOGNORM, "%s: %s (%s) [%s]", |
|
455 |
ctx->username, state, auth, policy); |
|
456 |
} |
|
457 |
||
458 |
static ConnContext * otr_context_encrypted(const char * buddy) |
|
459 |
{ |
|
460 |
ConnContext * ctx = otr_get_context(buddy); |
|
461 |
||
462 |
if (!userstate || !ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED){ |
|
463 |
scr_LogPrint(LPRINT_LOGNORM, |
|
464 |
"You have to start an OTR channel with %s before you can " |
|
465 |
"use SMP.", buddy); |
|
466 |
return NULL; |
|
467 |
} |
|
468 |
||
469 |
return ctx; |
|
470 |
} |
|
471 |
||
472 |
void otr_smp_query(const char * buddy, const char * secret) |
|
473 |
{ |
|
474 |
ConnContext * ctx = otr_context_encrypted(buddy); |
|
475 |
||
476 |
if (!secret) { |
|
477 |
scr_LogPrint(LPRINT_LOGNORM, |
|
478 |
"Using SMP without a secret isn't a good idea."); |
|
479 |
return; |
|
480 |
} |
|
481 |
||
482 |
if (ctx) { |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
483 |
otrl_message_initiate_smp(userstate, &ops, NULL, ctx, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
484 |
(const unsigned char *)secret, |
1299 | 485 |
strlen(secret)); |
486 |
scr_WriteIncomingMessage(ctx->username, |
|
487 |
"OTR: Socialist Millionaires' Protocol " |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
488 |
"initiated.", 0, HBB_PREFIX_INFO, 0); |
1299 | 489 |
} |
490 |
} |
|
491 |
||
492 |
void otr_smp_respond(const char * buddy, const char * secret) |
|
493 |
{ |
|
494 |
ConnContext * ctx = otr_context_encrypted(buddy); |
|
495 |
||
496 |
if (!secret) { |
|
497 |
scr_LogPrint(LPRINT_LOGNORM, |
|
498 |
"Using SMP without a secret isn't a good idea."); |
|
499 |
return; |
|
500 |
} |
|
501 |
||
502 |
if (ctx) { |
|
503 |
if (!ctx->smstate->secret) { |
|
504 |
scr_LogPrint(LPRINT_LOGNORM, |
|
505 |
"Don't call smpr before you haven't received an SMP " |
|
506 |
"Initiation!"); |
|
507 |
return; |
|
508 |
} |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
509 |
otrl_message_respond_smp(userstate, &ops, NULL, ctx, |
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
510 |
(const unsigned char *)secret, |
1299 | 511 |
strlen(secret)); |
512 |
scr_WriteIncomingMessage(ctx->username, |
|
513 |
"OTR: Socialist Millionaires' Protocol: " |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
514 |
"response sent", 0, HBB_PREFIX_INFO, 0); |
1299 | 515 |
} |
516 |
} |
|
517 |
||
518 |
void otr_smp_abort(const char * buddy) |
|
519 |
{ |
|
520 |
ConnContext * ctx = otr_context_encrypted(buddy); |
|
521 |
||
522 |
if (ctx) { |
|
523 |
otrl_message_abort_smp(userstate, &ops, NULL, ctx); |
|
524 |
scr_WriteIncomingMessage(ctx->username, |
|
525 |
"OTR: Socialist Millionaires' Protocol aborted.", |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
526 |
0, HBB_PREFIX_INFO, 0); |
1299 | 527 |
} |
528 |
} |
|
529 |
||
530 |
void otr_key(void) |
|
531 |
{ |
|
532 |
OtrlPrivKey * key; |
|
533 |
char readable[45] = ""; |
|
534 |
||
535 |
if(!userstate) |
|
536 |
return; |
|
537 |
for (key = userstate->privkey_root; key; key = key->next) { |
|
538 |
otrl_privkey_fingerprint(userstate, readable, key->accountname, |
|
539 |
key->protocol); |
|
540 |
scr_LogPrint(LPRINT_LOGNORM, "%s: %s", key->accountname, readable); |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
/* Return the OTR policy for the given context. */ |
|
545 |
static OtrlPolicy cb_policy(void *opdata, ConnContext *ctx) |
|
546 |
{ |
|
547 |
enum otr_policy p = settings_otr_getpolicy(NULL); |
|
548 |
||
549 |
if(ctx) |
|
550 |
if(settings_otr_getpolicy(ctx->username)) |
|
551 |
p = settings_otr_getpolicy(ctx->username); |
|
552 |
||
553 |
switch (p) { |
|
554 |
case plain: |
|
555 |
return OTRL_POLICY_NEVER; |
|
556 |
case opportunistic: |
|
557 |
return OTRL_POLICY_OPPORTUNISTIC & ~OTRL_POLICY_ALLOW_V1; |
|
558 |
case manual: |
|
559 |
return OTRL_POLICY_MANUAL & ~OTRL_POLICY_ALLOW_V1; |
|
560 |
case always: |
|
561 |
return OTRL_POLICY_ALWAYS & ~OTRL_POLICY_ALLOW_V1; |
|
562 |
} |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
563 |
|
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
564 |
return OTRL_POLICY_MANUAL & ~OTRL_POLICY_ALLOW_V1; |
1299 | 565 |
} |
566 |
||
567 |
/* Create a private key for the given accountname/protocol if |
|
568 |
* desired. */ |
|
569 |
static void cb_create_privkey(void *opdata, const char *accountname, |
|
570 |
const char *protocol) |
|
571 |
{ |
|
572 |
gcry_error_t e; |
|
1320 | 573 |
char * root; |
1299 | 574 |
|
575 |
scr_LogPrint(LPRINT_LOGNORM, |
|
576 |
"Generating new OTR key for %s. This may take a while...", |
|
577 |
accountname); |
|
578 |
scr_DoUpdate(); |
|
579 |
||
580 |
e = otrl_privkey_generate(userstate, keyfile, accountname, protocol); |
|
581 |
||
1320 | 582 |
if (e) { |
583 |
root = otr_get_dir(); |
|
584 |
scr_LogPrint(LPRINT_LOGNORM, "OTR key generation failed! Please mkdir " |
|
585 |
"%s if you want to use otr encryption.", root); |
|
586 |
g_free(root); |
|
587 |
} |
|
1299 | 588 |
else |
589 |
scr_LogPrint(LPRINT_LOGNORM, "OTR key generated."); |
|
590 |
} |
|
591 |
||
592 |
/* Report whether you think the given user is online. Return 1 if |
|
593 |
* you think he is, 0 if you think he isn't, -1 if you're not sure. |
|
594 |
* If you return 1, messages such as heartbeats or other |
|
595 |
* notifications may be sent to the user, which could result in "not |
|
596 |
* logged in" errors if you're wrong. */ |
|
597 |
static int cb_is_logged_in(void *opdata, const char *accountname, |
|
598 |
const char *protocol, const char *recipient) |
|
599 |
{ |
|
600 |
int ret = (roster_getstatus((char *)recipient, NULL) != offline); |
|
601 |
return ret; |
|
602 |
} |
|
603 |
||
604 |
/* Send the given IM to the given recipient from the given |
|
605 |
* accountname/protocol. */ |
|
606 |
static void cb_inject_message(void *opdata, const char *accountname, |
|
607 |
const char *protocol, const char *recipient, |
|
608 |
const char *message) |
|
609 |
{ |
|
610 |
char * id = g_strdup("otrinject"); |
|
1306
a7d21918234c
Fix jb_send_msg() call in OTR
Mikael Berthe <mikael@lilotux.net>
parents:
1303
diff
changeset
|
611 |
jb_send_msg(recipient, message, ROSTER_TYPE_USER, "", id, NULL, NULL); |
1299 | 612 |
g_free(id); |
613 |
} |
|
614 |
||
615 |
/* Display a notification message for a particular |
|
616 |
* accountname / protocol / username conversation. */ |
|
617 |
static void cb_notify(void *opdata, OtrlNotifyLevel level, |
|
618 |
const char *accountname, const char *protocol, |
|
619 |
const char *username, const char *title, |
|
620 |
const char *primary, const char *secondary) |
|
621 |
{ |
|
622 |
char * type; |
|
623 |
char *sbuf = NULL; |
|
624 |
switch (level) { |
|
625 |
case OTRL_NOTIFY_ERROR: type = "error"; break; |
|
626 |
case OTRL_NOTIFY_WARNING: type = "warning"; break; |
|
627 |
case OTRL_NOTIFY_INFO: type = "info"; break; |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
628 |
default: type = "unknown"; |
1299 | 629 |
} |
630 |
sbuf = g_strdup_printf("OTR %s:%s\n%s\n%s",type,title, primary, secondary); |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
631 |
scr_WriteIncomingMessage(username, sbuf, 0, HBB_PREFIX_INFO, 0); |
1299 | 632 |
g_free(sbuf); |
633 |
} |
|
634 |
||
635 |
/* Display an OTR control message for a particular |
|
636 |
* accountname / protocol / username conversation. Return 0 if you are able |
|
637 |
* to successfully display it. If you return non-0 (or if this |
|
638 |
* function is NULL), the control message will be displayed inline, |
|
639 |
* as a received message, or else by using the above notify() |
|
640 |
* callback. */ |
|
641 |
static int cb_display_otr_message(void *opdata, const char *accountname, |
|
642 |
const char *protocol, const char *username, |
|
643 |
const char *msg) |
|
644 |
{ |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
645 |
scr_WriteIncomingMessage(username, msg, 0, HBB_PREFIX_INFO, 0); |
1299 | 646 |
return 0; |
647 |
} |
|
648 |
||
649 |
/* When the list of ConnContexts changes (including a change in |
|
650 |
* state), this is called so the UI can be updated. */ |
|
651 |
static void cb_update_context_list(void *opdata) |
|
652 |
{ |
|
653 |
/*maybe introduce new status characters for mcabber, |
|
654 |
* then use this function (?!)*/ |
|
655 |
} |
|
656 |
||
657 |
/* Return a newly allocated string containing a human-friendly name |
|
658 |
* for the given protocol id */ |
|
659 |
static const char *cb_protocol_name(void *opdata, const char *protocol) |
|
660 |
{ |
|
661 |
return protocol; |
|
662 |
} |
|
663 |
||
664 |
/* Deallocate a string allocated by protocol_name */ |
|
665 |
static void cb_protocol_name_free (void *opdata, const char *protocol_name) |
|
666 |
{ |
|
667 |
/* We didn't allocated memory, so we don't have to free anything :p */ |
|
668 |
} |
|
669 |
||
670 |
/* A new fingerprint for the given user has been received. */ |
|
671 |
static void cb_new_fingerprint(void *opdata, OtrlUserState us, |
|
672 |
const char *accountname, const char *protocol, |
|
673 |
const char *username, |
|
674 |
unsigned char fingerprint[20]) |
|
675 |
{ |
|
676 |
char *sbuf = NULL; |
|
677 |
char readable[45]; |
|
678 |
||
679 |
otrl_privkey_hash_to_human(readable, fingerprint); |
|
680 |
sbuf = g_strdup_printf("OTR: new fingerprint: %s", readable); |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
681 |
scr_WriteIncomingMessage(username, sbuf, 0, HBB_PREFIX_INFO, 0); |
1299 | 682 |
g_free(sbuf); |
683 |
} |
|
684 |
||
685 |
/* The list of known fingerprints has changed. Write them to disk. */ |
|
686 |
static void cb_write_fingerprints(void *opdata) |
|
687 |
{ |
|
688 |
otrl_privkey_write_fingerprints(userstate, fprfile); |
|
689 |
} |
|
690 |
||
691 |
/* A ConnContext has entered a secure state. */ |
|
692 |
static void cb_gone_secure(void *opdata, ConnContext *context) |
|
693 |
{ |
|
694 |
scr_WriteIncomingMessage(context->username, "OTR: channel established", 0, |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
695 |
HBB_PREFIX_INFO, 0); |
1299 | 696 |
} |
697 |
||
698 |
/* A ConnContext has left a secure state. */ |
|
699 |
static void cb_gone_insecure(void *opdata, ConnContext *context) |
|
700 |
{ |
|
701 |
scr_WriteIncomingMessage(context->username, "OTR: channel closed", 0, |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
702 |
HBB_PREFIX_INFO, 0); |
1299 | 703 |
} |
704 |
||
705 |
/* We have completed an authentication, using the D-H keys we |
|
706 |
* already knew. is_reply indicates whether we initiated the AKE. */ |
|
707 |
static void cb_still_secure(void *opdata, ConnContext *context, int is_reply) |
|
708 |
{ |
|
709 |
scr_WriteIncomingMessage(context->username, "OTR: channel reestablished", 0, |
|
1301
37b41ed9ed35
Fixed compiler warnings
Frank Zschockelt <mcabber_otr[at]freakysoft.de>
parents:
1299
diff
changeset
|
710 |
HBB_PREFIX_INFO, 0); |
1299 | 711 |
} |
712 |
||
713 |
/* Log a message. The passed message will end in "\n". */ |
|
714 |
static void cb_log_message(void *opdata, const char *message) |
|
715 |
{ |
|
716 |
scr_LogPrint(LPRINT_DEBUG, "OTR: %s", message); |
|
717 |
} |
|
718 |
||
719 |
/* Find the maximum message size supported by this protocol. */ |
|
720 |
static int cb_max_message_size(void *opdata, ConnContext *context) |
|
721 |
{ |
|
722 |
return 8192; |
|
723 |
} |
|
724 |
||
725 |
#endif /* HAVE_LIBOTR */ |
|
726 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |