webticket: implement caching
[siplcs.git] / src / core / sipe-ft.c
blob6d7eeb973fc760f5dcf6bed54559887dbf045ea2
1 /**
2 * @file sipe-ft.c
4 * pidgin-sipe
6 * Copyright (C) 2010-12 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-core.h"
38 #include "sipe-core-private.h"
39 #include "sipe-crypt.h"
40 #include "sipe-dialog.h"
41 #include "sipe-digest.h"
42 #include "sipe-ft.h"
43 #include "sipe-im.h"
44 #include "sipe-nls.h"
45 #include "sipe-session.h"
46 #include "sipe-utils.h"
49 * DO NOT CHANGE THE FOLLOWING CONSTANTS!!!
51 * It seems that Microsoft Office Communicator client will accept
52 * file transfer invitations *only* within this port range!
54 * If a firewall is active on your system you need to open these ports if
55 * you want to *send* files to other users. Receiving files uses an outgoing
56 * connection and should therefore automatically penetrate your firewall.
58 #define SIPE_FT_TCP_PORT_MIN 6891
59 #define SIPE_FT_TCP_PORT_MAX 6901
61 void sipe_ft_raise_error_and_cancel(struct sipe_file_transfer_private *ft_private,
62 const gchar *errmsg)
64 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC, errmsg);
65 sipe_backend_ft_cancel_local(SIPE_FILE_TRANSFER_PUBLIC);
68 static void generate_key(guchar *buffer, gsize size)
70 gsize i = 0;
71 while (i < size) buffer[i++] = rand();
74 struct sipe_file_transfer *sipe_core_ft_allocate(struct sipe_core_public *sipe_public)
76 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
77 struct sipe_file_transfer_private *ft_private =
78 g_new0(struct sipe_file_transfer_private, 1);
80 ft_private->sipe_private = sipe_private;
81 ft_private->invitation_cookie = g_strdup_printf("%u", rand() % 1000000000);
83 return(SIPE_FILE_TRANSFER_PUBLIC);
86 static void sipe_ft_deallocate(struct sipe_file_transfer *ft)
88 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
90 if (ft->backend_private)
91 sipe_backend_ft_deallocate(ft);
93 if (ft_private->listendata)
94 sipe_backend_network_listen_cancel(ft_private->listendata);
96 if (ft_private->cipher_context)
97 sipe_crypt_ft_destroy(ft_private->cipher_context);
99 if (ft_private->hmac_context)
100 sipe_digest_ft_destroy(ft_private->hmac_context);
102 g_free(ft_private->invitation_cookie);
103 g_free(ft_private->encrypted_outbuf);
104 g_free(ft_private);
107 void sipe_core_ft_deallocate(struct sipe_file_transfer *ft)
109 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
110 struct sip_dialog *dialog = ft_private->dialog;
112 if (dialog)
113 dialog->filetransfers = g_slist_remove(dialog->filetransfers, ft_private);
115 sipe_ft_deallocate(ft);
118 static void sipe_ft_request(struct sipe_file_transfer_private *ft_private,
119 const gchar *body)
121 struct sip_dialog *dialog = ft_private->dialog;
122 sip_transport_request(ft_private->sipe_private,
123 "MESSAGE",
124 dialog->with,
125 dialog->with,
126 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
127 body,
128 dialog,
129 NULL);
132 void sipe_core_ft_cancel(struct sipe_file_transfer *ft)
134 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
136 gchar *body = g_strdup_printf("Invitation-Command: CANCEL\r\n"
137 "Invitation-Cookie: %s\r\n"
138 "Cancel-Code: REJECT\r\n",
139 ft_private->invitation_cookie);
140 sipe_ft_request(ft_private, body);
141 g_free(body);
144 static void
145 send_ft_accept(struct sipe_file_transfer_private *ft_private,
146 gboolean send_enc_key,
147 gboolean send_connect_data,
148 gboolean sender_connect)
150 GString *body = g_string_new("");
152 g_string_append_printf(body,
153 "Invitation-Command: ACCEPT\r\n"
154 "Request-Data: IP-Address:\r\n"
155 "Invitation-Cookie: %s\r\n",
156 ft_private->invitation_cookie);
158 if (send_enc_key) {
159 gchar *b64_encryption_key;
160 gchar *b64_hash_key;
162 b64_encryption_key = g_base64_encode(ft_private->encryption_key,
163 SIPE_FT_KEY_LENGTH);
164 b64_hash_key = g_base64_encode(ft_private->hash_key,
165 SIPE_FT_KEY_LENGTH);
167 g_string_append_printf(body,
168 "Encryption-Key: %s\r\n"
169 "Hash-Key: %s\r\n",
170 b64_encryption_key,
171 b64_hash_key);
173 g_free(b64_hash_key);
174 g_free(b64_encryption_key);
177 if (send_connect_data) {
178 struct sipe_core_private *sipe_private = ft_private->sipe_private;
180 g_string_append_printf(body,
181 "IP-Address: %s\r\n"
182 "Port: %d\r\n"
183 "PortX: 11178\r\n"
184 "AuthCookie: %u\r\n",
185 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC),
186 ft_private->port,
187 ft_private->auth_cookie);
190 if (sender_connect) {
191 g_string_append(body,
192 "Sender-Connect: TRUE\r\n");
195 sipe_ft_request(ft_private, body->str);
197 g_string_free(body, TRUE);
200 static void
201 listen_socket_created_cb(unsigned short port, gpointer data)
203 struct sipe_file_transfer *ft = data;
205 SIPE_FILE_TRANSFER_PRIVATE->port = port;
206 SIPE_FILE_TRANSFER_PRIVATE->auth_cookie = rand() % 1000000000;
208 if (sipe_backend_ft_is_incoming(ft))
209 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE, TRUE, TRUE, TRUE);
210 else
211 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE, FALSE, TRUE, FALSE);
214 static void
215 client_connected_cb(struct sipe_backend_fd *fd, gpointer data)
217 struct sipe_file_transfer *ft = data;
219 SIPE_FILE_TRANSFER_PRIVATE->listendata = NULL;
221 if (!sipe_backend_fd_is_valid(fd)) {
222 sipe_backend_ft_error(ft, _("Socket read failed"));
223 sipe_backend_ft_cancel_local(ft);
224 } else {
225 sipe_backend_ft_start(ft, fd, NULL, 0);
228 sipe_backend_fd_free(fd);
231 void sipe_core_ft_incoming_init(struct sipe_file_transfer *ft)
233 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
235 if (ft_private->peer_using_nat) {
236 ft_private->listendata =
237 sipe_backend_network_listen_range(SIPE_FT_TCP_PORT_MIN,
238 SIPE_FT_TCP_PORT_MAX,
239 listen_socket_created_cb,
240 client_connected_cb,
241 ft);
242 } else {
243 send_ft_accept(ft_private, TRUE, FALSE, FALSE);
247 void sipe_core_ft_outgoing_init(struct sipe_file_transfer *ft,
248 const gchar *filename, gsize size,
249 const gchar *who)
251 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
252 struct sipe_core_private *sipe_private = ft_private->sipe_private;
253 struct sip_dialog *dialog;
255 const gchar *ip = sipe_backend_network_ip_address(SIPE_CORE_PUBLIC);
256 gchar *body = g_strdup_printf("Application-Name: File Transfer\r\n"
257 "Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n"
258 "Invitation-Command: INVITE\r\n"
259 "Invitation-Cookie: %s\r\n"
260 "Application-File: %s\r\n"
261 "Application-FileSize: %" G_GSIZE_FORMAT "\r\n"
262 "%s"
263 "Encryption: R\r\n", // TODO: non encrypted file transfer support
264 ft_private->invitation_cookie,
265 filename,
266 size,
267 sipe_utils_ip_is_private(ip) ? "Connectivity: N\r\n" : "");
269 struct sip_session *session = sipe_session_find_or_add_im(sipe_private, who);
271 // Queue the message
272 sipe_session_enqueue_message(session, body, "text/x-msmsgsinvite");
274 dialog = sipe_dialog_find(session, who);
275 if (dialog && !dialog->outgoing_invite) {
276 sipe_im_process_queue(sipe_private, session);
277 } else if (!dialog || !dialog->outgoing_invite) {
278 // Need to send the INVITE to get the outgoing dialog setup
279 sipe_im_invite(sipe_private, session, who, body, "text/x-msmsgsinvite", NULL, FALSE);
280 dialog = sipe_dialog_find(session, who);
283 dialog->filetransfers = g_slist_append(dialog->filetransfers, ft_private);
284 ft_private->dialog = dialog;
286 g_free(body);
289 void sipe_ft_incoming_transfer(struct sipe_core_private *sipe_private,
290 struct sip_dialog *dialog,
291 const GSList *body)
293 struct sipe_file_transfer_private *ft_private;
294 gsize file_size;
296 ft_private = g_new0(struct sipe_file_transfer_private, 1);
297 ft_private->sipe_private = sipe_private;
299 generate_key(ft_private->encryption_key, SIPE_FT_KEY_LENGTH);
300 generate_key(ft_private->hash_key, SIPE_FT_KEY_LENGTH);
302 ft_private->invitation_cookie = g_strdup(sipe_utils_nameval_find(body, "Invitation-Cookie"));
303 ft_private->peer_using_nat = sipe_strequal(sipe_utils_nameval_find(body, "Connectivity"), "N");
305 ft_private->dialog = dialog;
307 file_size = g_ascii_strtoull(sipe_utils_nameval_find(body,
308 "Application-FileSize"),
309 NULL, 10);
310 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC,
311 SIPE_FILE_TRANSFER_PUBLIC,
312 dialog->with,
313 sipe_utils_nameval_find(body, "Application-File"),
314 file_size);
316 if (ft_private->public.backend_private != NULL) {
317 ft_private->dialog->filetransfers = g_slist_append(ft_private->dialog->filetransfers, ft_private);
318 } else {
319 sipe_ft_deallocate(SIPE_FILE_TRANSFER_PUBLIC);
323 static struct sipe_file_transfer_private *
324 sipe_find_ft(const struct sip_dialog *dialog, const gchar *inv_cookie)
326 GSList *ftlist = dialog->filetransfers;
327 for (; ftlist != NULL; ftlist = ftlist->next) {
328 struct sipe_file_transfer_private *ft_private = ftlist->data;
329 if (sipe_strequal(ft_private->invitation_cookie, inv_cookie))
330 return ft_private;
332 return NULL;
335 void sipe_ft_incoming_accept(struct sip_dialog *dialog, const GSList *body)
337 const gchar *inv_cookie = sipe_utils_nameval_find(body, "Invitation-Cookie");
338 struct sipe_file_transfer_private *ft_private = sipe_find_ft(dialog, inv_cookie);
340 if (ft_private) {
341 const gchar *ip = sipe_utils_nameval_find(body, "IP-Address");
342 const gchar *port_str = sipe_utils_nameval_find(body, "Port");
343 const gchar *auth_cookie = sipe_utils_nameval_find(body, "AuthCookie");
344 const gchar *enc_key_b64 = sipe_utils_nameval_find(body, "Encryption-Key");
345 const gchar *hash_key_b64 = sipe_utils_nameval_find(body, "Hash-Key");
347 if (auth_cookie)
348 ft_private->auth_cookie = g_ascii_strtoull(auth_cookie,
349 NULL, 10);
350 if (enc_key_b64) {
351 gsize ret_len;
352 guchar *enc_key = g_base64_decode(enc_key_b64,
353 &ret_len);
354 if (ret_len == SIPE_FT_KEY_LENGTH) {
355 memcpy(ft_private->encryption_key,
356 enc_key, SIPE_FT_KEY_LENGTH);
357 } else {
358 sipe_ft_raise_error_and_cancel(ft_private,
359 _("Received encryption key has wrong size."));
360 g_free(enc_key);
361 return;
363 g_free(enc_key);
365 if (hash_key_b64) {
366 gsize ret_len;
367 guchar *hash_key = g_base64_decode(hash_key_b64,
368 &ret_len);
369 if (ret_len == SIPE_FT_KEY_LENGTH) {
370 memcpy(ft_private->hash_key,
371 hash_key, SIPE_FT_KEY_LENGTH);
372 } else {
373 sipe_ft_raise_error_and_cancel(ft_private,
374 _("Received hash key has wrong size."));
375 g_free(hash_key);
376 return;
378 g_free(hash_key);
382 if (ip && port_str) {
383 sipe_backend_ft_start(SIPE_FILE_TRANSFER_PUBLIC, NULL, ip,
384 g_ascii_strtoull(port_str, NULL, 10));
385 } else {
386 ft_private->listendata =
387 sipe_backend_network_listen_range(SIPE_FT_TCP_PORT_MIN,
388 SIPE_FT_TCP_PORT_MAX,
389 listen_socket_created_cb,
390 client_connected_cb,
391 ft_private);
392 if (!ft_private->listendata)
393 sipe_ft_raise_error_and_cancel(ft_private,
394 _("Could not create listen socket"));
399 void sipe_ft_incoming_cancel(struct sip_dialog *dialog, const GSList *body)
401 const gchar *inv_cookie = sipe_utils_nameval_find(body, "Invitation-Cookie");
402 struct sipe_file_transfer_private *ft_private = sipe_find_ft(dialog, inv_cookie);
404 if (ft_private)
405 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC);
408 GSList *sipe_ft_parse_msg_body(const gchar *body)
410 GSList *list = NULL;
411 gchar **lines = g_strsplit(body, "\r\n", 0);
412 if (sipe_utils_parse_lines(&list, lines, ":") == FALSE) {
413 sipe_utils_nameval_free(list);
414 list = NULL;
416 g_strfreev(lines);
417 return list;
421 Local Variables:
422 mode: c
423 c-file-style: "bsd"
424 indent-tabs-mode: t
425 tab-width: 8
426 End: