filetransfer: use 'request_denied' callback in sipe_file_transfer
[siplcs.git] / src / core / sipe-ft.c
blob0cbc76a44173688da1831ebb0113643409ba1cf0
1 /**
2 * @file sipe-ft.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2015 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
8 * Copyright (C) 2010 Tomáš Hrabčík <tomas.hrabcik@tieto.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <stdlib.h>
30 #include <string.h>
32 #include <glib.h>
34 #include "sipmsg.h"
35 #include "sip-transport.h"
36 #include "sipe-backend.h"
37 #include "sipe-common.h"
38 #include "sipe-core.h"
39 #include "sipe-core-private.h"
40 #include "sipe-crypt.h"
41 #include "sipe-dialog.h"
42 #include "sipe-digest.h"
43 #include "sipe-ft.h"
44 #include "sipe-im.h"
45 #include "sipe-nls.h"
46 #include "sipe-session.h"
47 #include "sipe-utils.h"
50 * DO NOT CHANGE THE FOLLOWING CONSTANTS!!!
52 * It seems that Microsoft Office Communicator client will accept
53 * file transfer invitations *only* within this port range!
55 * If a firewall is active on your system you need to open these ports if
56 * you want to *send* files to other users. Receiving files uses an outgoing
57 * connection and should therefore automatically penetrate your firewall.
59 #define SIPE_FT_TCP_PORT_MIN 6891
60 #define SIPE_FT_TCP_PORT_MAX 6901
62 static void
63 ft_outgoing_init(struct sipe_file_transfer *ft, const gchar *filename,
64 gsize size, const gchar *who);
66 void sipe_ft_raise_error_and_cancel(struct sipe_file_transfer_private *ft_private,
67 const gchar *errmsg)
69 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC, errmsg);
70 sipe_backend_ft_cancel_local(SIPE_FILE_TRANSFER_PUBLIC);
73 static void generate_key(guchar *buffer, gsize size)
75 gsize i = 0;
76 while (i < size) buffer[i++] = rand();
79 struct sipe_file_transfer *sipe_core_ft_allocate(struct sipe_core_public *sipe_public)
81 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
82 struct sipe_file_transfer_private *ft_private =
83 g_new0(struct sipe_file_transfer_private, 1);
85 ft_private->sipe_private = sipe_private;
87 ft_private->public.init = ft_outgoing_init;
89 ft_private->invitation_cookie = g_strdup_printf("%u", rand() % 1000000000);
91 return(SIPE_FILE_TRANSFER_PUBLIC);
94 static void sipe_ft_deallocate(struct sipe_file_transfer *ft)
96 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
98 if (ft->backend_private)
99 sipe_backend_ft_deallocate(ft);
101 if (ft_private->listendata)
102 sipe_backend_network_listen_cancel(ft_private->listendata);
104 if (ft_private->cipher_context)
105 sipe_crypt_ft_destroy(ft_private->cipher_context);
107 if (ft_private->hmac_context)
108 sipe_digest_ft_destroy(ft_private->hmac_context);
110 g_free(ft_private->invitation_cookie);
111 g_free(ft_private->encrypted_outbuf);
112 g_free(ft_private);
115 void sipe_core_ft_deallocate(struct sipe_file_transfer *ft)
117 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
118 struct sip_dialog *dialog = ft_private->dialog;
120 if (dialog)
121 dialog->filetransfers = g_slist_remove(dialog->filetransfers, ft_private);
123 sipe_ft_deallocate(ft);
126 static void sipe_ft_request(struct sipe_file_transfer_private *ft_private,
127 const gchar *body)
129 struct sip_dialog *dialog = ft_private->dialog;
130 sip_transport_request(ft_private->sipe_private,
131 "MESSAGE",
132 dialog->with,
133 dialog->with,
134 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
135 body,
136 dialog,
137 NULL);
140 static void
141 ft_request_denied(struct sipe_file_transfer *ft)
143 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
145 gchar *body = g_strdup_printf("Invitation-Command: CANCEL\r\n"
146 "Invitation-Cookie: %s\r\n"
147 "Cancel-Code: REJECT\r\n",
148 ft_private->invitation_cookie);
149 sipe_ft_request(ft_private, body);
150 g_free(body);
153 static void
154 send_ft_accept(struct sipe_file_transfer_private *ft_private,
155 gboolean send_enc_key,
156 gboolean send_connect_data,
157 gboolean sender_connect)
159 GString *body = g_string_new("");
161 g_string_append_printf(body,
162 "Invitation-Command: ACCEPT\r\n"
163 "Request-Data: IP-Address:\r\n"
164 "Invitation-Cookie: %s\r\n",
165 ft_private->invitation_cookie);
167 if (send_enc_key) {
168 gchar *b64_encryption_key;
169 gchar *b64_hash_key;
171 b64_encryption_key = g_base64_encode(ft_private->encryption_key,
172 SIPE_FT_KEY_LENGTH);
173 b64_hash_key = g_base64_encode(ft_private->hash_key,
174 SIPE_FT_KEY_LENGTH);
176 g_string_append_printf(body,
177 "Encryption-Key: %s\r\n"
178 "Hash-Key: %s\r\n",
179 b64_encryption_key,
180 b64_hash_key);
182 g_free(b64_hash_key);
183 g_free(b64_encryption_key);
186 if (send_connect_data) {
187 struct sipe_core_private *sipe_private = ft_private->sipe_private;
189 g_string_append_printf(body,
190 "IP-Address: %s\r\n"
191 "Port: %d\r\n"
192 "PortX: 11178\r\n"
193 "AuthCookie: %u\r\n",
194 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC),
195 ft_private->port,
196 ft_private->auth_cookie);
199 if (sender_connect) {
200 g_string_append(body,
201 "Sender-Connect: TRUE\r\n");
204 sipe_ft_request(ft_private, body->str);
206 g_string_free(body, TRUE);
209 static void
210 listen_socket_created_cb(unsigned short port, gpointer data)
212 struct sipe_file_transfer *ft = data;
214 SIPE_FILE_TRANSFER_PRIVATE->port = port;
215 SIPE_FILE_TRANSFER_PRIVATE->auth_cookie = rand() % 1000000000;
217 if (sipe_backend_ft_is_incoming(ft))
218 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE, TRUE, TRUE, TRUE);
219 else
220 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE, FALSE, TRUE, FALSE);
223 static void
224 client_connected_cb(struct sipe_backend_fd *fd, gpointer data)
226 struct sipe_file_transfer *ft = data;
228 SIPE_FILE_TRANSFER_PRIVATE->listendata = NULL;
230 if (!sipe_backend_fd_is_valid(fd)) {
231 sipe_backend_ft_error(ft, _("Socket read failed"));
232 sipe_backend_ft_cancel_local(ft);
233 } else {
234 sipe_backend_ft_start(ft, fd, NULL, 0);
237 sipe_backend_fd_free(fd);
240 static void
241 ft_incoming_init(struct sipe_file_transfer *ft,
242 SIPE_UNUSED_PARAMETER const gchar *filename,
243 SIPE_UNUSED_PARAMETER gsize size,
244 SIPE_UNUSED_PARAMETER const gchar *who)
246 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
248 if (ft_private->peer_using_nat) {
249 ft_private->listendata =
250 sipe_backend_network_listen_range(SIPE_FT_TCP_PORT_MIN,
251 SIPE_FT_TCP_PORT_MAX,
252 listen_socket_created_cb,
253 client_connected_cb,
254 ft);
255 } else {
256 send_ft_accept(ft_private, TRUE, FALSE, FALSE);
260 static void
261 ft_outgoing_init(struct sipe_file_transfer *ft, const gchar *filename,
262 gsize size, const gchar *who)
264 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
265 struct sipe_core_private *sipe_private = ft_private->sipe_private;
266 struct sip_dialog *dialog;
268 const gchar *ip = sipe_backend_network_ip_address(SIPE_CORE_PUBLIC);
269 gchar *body = g_strdup_printf("Application-Name: File Transfer\r\n"
270 "Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n"
271 "Invitation-Command: INVITE\r\n"
272 "Invitation-Cookie: %s\r\n"
273 "Application-File: %s\r\n"
274 "Application-FileSize: %" G_GSIZE_FORMAT "\r\n"
275 "%s"
276 "Encryption: R\r\n", // TODO: non encrypted file transfer support
277 ft_private->invitation_cookie,
278 filename,
279 size,
280 sipe_utils_ip_is_private(ip) ? "Connectivity: N\r\n" : "");
282 struct sip_session *session = sipe_session_find_or_add_im(sipe_private, who);
284 // Queue the message
285 sipe_session_enqueue_message(session, body, "text/x-msmsgsinvite");
287 dialog = sipe_dialog_find(session, who);
288 if (dialog && !dialog->outgoing_invite) {
289 sipe_im_process_queue(sipe_private, session);
290 } else if (!dialog || !dialog->outgoing_invite) {
291 // Need to send the INVITE to get the outgoing dialog setup
292 sipe_im_invite(sipe_private, session, who, body, "text/x-msmsgsinvite", NULL, FALSE);
293 dialog = sipe_dialog_find(session, who);
296 dialog->filetransfers = g_slist_append(dialog->filetransfers, ft_private);
297 ft_private->dialog = dialog;
299 g_free(body);
302 void sipe_ft_incoming_transfer(struct sipe_core_private *sipe_private,
303 struct sip_dialog *dialog,
304 const GSList *body)
306 struct sipe_file_transfer_private *ft_private;
307 gsize file_size;
309 ft_private = g_new0(struct sipe_file_transfer_private, 1);
310 ft_private->sipe_private = sipe_private;
312 ft_private->public.init = ft_incoming_init;
313 ft_private->public.request_denied = ft_request_denied;
315 generate_key(ft_private->encryption_key, SIPE_FT_KEY_LENGTH);
316 generate_key(ft_private->hash_key, SIPE_FT_KEY_LENGTH);
318 ft_private->invitation_cookie = g_strdup(sipe_utils_nameval_find(body, "Invitation-Cookie"));
319 ft_private->peer_using_nat = sipe_strequal(sipe_utils_nameval_find(body, "Connectivity"), "N");
321 ft_private->dialog = dialog;
323 file_size = g_ascii_strtoull(sipe_utils_nameval_find(body,
324 "Application-FileSize"),
325 NULL, 10);
326 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC,
327 SIPE_FILE_TRANSFER_PUBLIC,
328 dialog->with,
329 sipe_utils_nameval_find(body, "Application-File"),
330 file_size);
332 if (ft_private->public.backend_private != NULL) {
333 ft_private->dialog->filetransfers = g_slist_append(ft_private->dialog->filetransfers, ft_private);
334 } else {
335 sipe_ft_deallocate(SIPE_FILE_TRANSFER_PUBLIC);
339 static struct sipe_file_transfer_private *
340 sipe_find_ft(const struct sip_dialog *dialog, const gchar *inv_cookie)
342 GSList *ftlist = dialog->filetransfers;
343 for (; ftlist != NULL; ftlist = ftlist->next) {
344 struct sipe_file_transfer_private *ft_private = ftlist->data;
345 if (sipe_strequal(ft_private->invitation_cookie, inv_cookie))
346 return ft_private;
348 return NULL;
351 void sipe_ft_incoming_accept(struct sip_dialog *dialog, const GSList *body)
353 const gchar *inv_cookie = sipe_utils_nameval_find(body, "Invitation-Cookie");
354 struct sipe_file_transfer_private *ft_private = sipe_find_ft(dialog, inv_cookie);
356 if (ft_private) {
357 const gchar *ip = sipe_utils_nameval_find(body, "IP-Address");
358 const gchar *port_str = sipe_utils_nameval_find(body, "Port");
359 const gchar *auth_cookie = sipe_utils_nameval_find(body, "AuthCookie");
360 const gchar *enc_key_b64 = sipe_utils_nameval_find(body, "Encryption-Key");
361 const gchar *hash_key_b64 = sipe_utils_nameval_find(body, "Hash-Key");
363 if (auth_cookie)
364 ft_private->auth_cookie = g_ascii_strtoull(auth_cookie,
365 NULL, 10);
366 if (enc_key_b64) {
367 gsize ret_len;
368 guchar *enc_key = g_base64_decode(enc_key_b64,
369 &ret_len);
370 if (ret_len == SIPE_FT_KEY_LENGTH) {
371 memcpy(ft_private->encryption_key,
372 enc_key, SIPE_FT_KEY_LENGTH);
373 } else {
374 sipe_ft_raise_error_and_cancel(ft_private,
375 _("Received encryption key has wrong size."));
376 g_free(enc_key);
377 return;
379 g_free(enc_key);
381 if (hash_key_b64) {
382 gsize ret_len;
383 guchar *hash_key = g_base64_decode(hash_key_b64,
384 &ret_len);
385 if (ret_len == SIPE_FT_KEY_LENGTH) {
386 memcpy(ft_private->hash_key,
387 hash_key, SIPE_FT_KEY_LENGTH);
388 } else {
389 sipe_ft_raise_error_and_cancel(ft_private,
390 _("Received hash key has wrong size."));
391 g_free(hash_key);
392 return;
394 g_free(hash_key);
398 if (ip && port_str) {
399 sipe_backend_ft_start(SIPE_FILE_TRANSFER_PUBLIC, NULL, ip,
400 g_ascii_strtoull(port_str, NULL, 10));
401 } else {
402 ft_private->listendata =
403 sipe_backend_network_listen_range(SIPE_FT_TCP_PORT_MIN,
404 SIPE_FT_TCP_PORT_MAX,
405 listen_socket_created_cb,
406 client_connected_cb,
407 ft_private);
408 if (!ft_private->listendata)
409 sipe_ft_raise_error_and_cancel(ft_private,
410 _("Could not create listen socket"));
415 void sipe_ft_incoming_cancel(struct sip_dialog *dialog, const GSList *body)
417 const gchar *inv_cookie = sipe_utils_nameval_find(body, "Invitation-Cookie");
418 struct sipe_file_transfer_private *ft_private = sipe_find_ft(dialog, inv_cookie);
420 if (ft_private)
421 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC);
424 GSList *sipe_ft_parse_msg_body(const gchar *body)
426 GSList *list = NULL;
427 gchar **lines = g_strsplit(body, "\r\n", 0);
428 if (sipe_utils_parse_lines(&list, lines, ":") == FALSE) {
429 sipe_utils_nameval_free(list);
430 list = NULL;
432 g_strfreev(lines);
433 return list;
437 Local Variables:
438 mode: c
439 c-file-style: "bsd"
440 indent-tabs-mode: t
441 tab-width: 8
442 End: