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
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"
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
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
,
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
)
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
);
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
;
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
,
129 struct sip_dialog
*dialog
= ft_private
->dialog
;
130 sip_transport_request(ft_private
->sipe_private
,
134 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
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
);
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
);
168 gchar
*b64_encryption_key
;
171 b64_encryption_key
= g_base64_encode(ft_private
->encryption_key
,
173 b64_hash_key
= g_base64_encode(ft_private
->hash_key
,
176 g_string_append_printf(body
,
177 "Encryption-Key: %s\r\n"
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
,
193 "AuthCookie: %u\r\n",
194 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
),
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
);
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
);
220 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE
, FALSE
, TRUE
, FALSE
);
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
);
234 sipe_backend_ft_start(ft
, fd
, NULL
, 0);
237 sipe_backend_fd_free(fd
);
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
,
256 send_ft_accept(ft_private
, TRUE
, FALSE
, FALSE
);
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"
276 "Encryption: R\r\n", // TODO: non encrypted file transfer support
277 ft_private
->invitation_cookie
,
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
);
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
;
302 void sipe_ft_incoming_transfer(struct sipe_core_private
*sipe_private
,
303 struct sip_dialog
*dialog
,
306 struct sipe_file_transfer_private
*ft_private
;
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"),
326 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC
,
327 SIPE_FILE_TRANSFER_PUBLIC
,
329 sipe_utils_nameval_find(body
, "Application-File"),
332 if (ft_private
->public.backend_private
!= NULL
) {
333 ft_private
->dialog
->filetransfers
= g_slist_append(ft_private
->dialog
->filetransfers
, ft_private
);
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
))
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
);
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");
364 ft_private
->auth_cookie
= g_ascii_strtoull(auth_cookie
,
368 guchar
*enc_key
= g_base64_decode(enc_key_b64
,
370 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
371 memcpy(ft_private
->encryption_key
,
372 enc_key
, SIPE_FT_KEY_LENGTH
);
374 sipe_ft_raise_error_and_cancel(ft_private
,
375 _("Received encryption key has wrong size."));
383 guchar
*hash_key
= g_base64_decode(hash_key_b64
,
385 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
386 memcpy(ft_private
->hash_key
,
387 hash_key
, SIPE_FT_KEY_LENGTH
);
389 sipe_ft_raise_error_and_cancel(ft_private
,
390 _("Received hash key has wrong size."));
398 if (ip
&& port_str
) {
399 sipe_backend_ft_start(SIPE_FILE_TRANSFER_PUBLIC
, NULL
, ip
,
400 g_ascii_strtoull(port_str
, NULL
, 10));
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
,
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
);
421 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC
);
424 GSList
*sipe_ft_parse_msg_body(const gchar
*body
)
427 gchar
**lines
= g_strsplit(body
, "\r\n", 0);
428 if (sipe_utils_parse_lines(&list
, lines
, ":") == FALSE
) {
429 sipe_utils_nameval_free(list
);