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"
44 #include "sipe-ft-tftp.h"
47 #include "sipe-session.h"
48 #include "sipe-utils.h"
51 * DO NOT CHANGE THE FOLLOWING CONSTANTS!!!
53 * It seems that Microsoft Office Communicator client will accept
54 * file transfer invitations *only* within this port range!
56 * If a firewall is active on your system you need to open these ports if
57 * you want to *send* files to other users. Receiving files uses an outgoing
58 * connection and should therefore automatically penetrate your firewall.
60 #define SIPE_FT_TCP_PORT_MIN 6891
61 #define SIPE_FT_TCP_PORT_MAX 6901
64 ft_outgoing_init(struct sipe_file_transfer
*ft
, const gchar
*filename
,
65 gsize size
, const gchar
*who
);
67 void sipe_ft_raise_error_and_cancel(struct sipe_file_transfer_private
*ft_private
,
70 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
, errmsg
);
71 sipe_backend_ft_cancel_local(SIPE_FILE_TRANSFER_PUBLIC
);
74 static void generate_key(guchar
*buffer
, gsize size
)
77 while (i
< size
) buffer
[i
++] = rand();
80 struct sipe_file_transfer
*
81 sipe_core_ft_create_outgoing(struct sipe_core_public
*sipe_public
)
83 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
84 struct sipe_file_transfer_private
*ft_private
=
85 g_new0(struct sipe_file_transfer_private
, 1);
87 ft_private
->sipe_private
= sipe_private
;
89 ft_private
->public.ft_init
= ft_outgoing_init
;
90 ft_private
->public.ft_start
= sipe_ft_tftp_start_sending
;
91 ft_private
->public.ft_write
= sipe_ft_tftp_write
;
92 ft_private
->public.ft_end
= sipe_ft_tftp_stop_sending
;
93 ft_private
->public.ft_deallocate
= sipe_ft_free
;
95 ft_private
->invitation_cookie
= g_strdup_printf("%u", rand() % 1000000000);
97 return(SIPE_FILE_TRANSFER_PUBLIC
);
101 sipe_ft_free(struct sipe_file_transfer
*ft
)
103 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
104 struct sip_dialog
*dialog
= ft_private
->dialog
;
107 dialog
->filetransfers
=
108 g_slist_remove(dialog
->filetransfers
, ft_private
);
110 if (ft
->backend_private
)
111 sipe_backend_ft_deallocate(ft
);
113 if (ft_private
->listendata
)
114 sipe_backend_network_listen_cancel(ft_private
->listendata
);
116 if (ft_private
->cipher_context
)
117 sipe_crypt_ft_destroy(ft_private
->cipher_context
);
119 if (ft_private
->hmac_context
)
120 sipe_digest_ft_destroy(ft_private
->hmac_context
);
122 g_free(ft_private
->invitation_cookie
);
123 g_free(ft_private
->encrypted_outbuf
);
127 static void sipe_ft_request(struct sipe_file_transfer_private
*ft_private
,
130 struct sip_dialog
*dialog
= ft_private
->dialog
;
131 sip_transport_request(ft_private
->sipe_private
,
135 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
142 ft_request_denied(struct sipe_file_transfer
*ft
)
144 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
146 gchar
*body
= g_strdup_printf("Invitation-Command: CANCEL\r\n"
147 "Invitation-Cookie: %s\r\n"
148 "Cancel-Code: REJECT\r\n",
149 ft_private
->invitation_cookie
);
150 sipe_ft_request(ft_private
, body
);
155 send_ft_accept(struct sipe_file_transfer_private
*ft_private
,
156 gboolean send_enc_key
,
157 gboolean send_connect_data
,
158 gboolean sender_connect
)
160 GString
*body
= g_string_new("");
162 g_string_append_printf(body
,
163 "Invitation-Command: ACCEPT\r\n"
164 "Request-Data: IP-Address:\r\n"
165 "Invitation-Cookie: %s\r\n",
166 ft_private
->invitation_cookie
);
169 gchar
*b64_encryption_key
;
172 b64_encryption_key
= g_base64_encode(ft_private
->encryption_key
,
174 b64_hash_key
= g_base64_encode(ft_private
->hash_key
,
177 g_string_append_printf(body
,
178 "Encryption-Key: %s\r\n"
183 g_free(b64_hash_key
);
184 g_free(b64_encryption_key
);
187 if (send_connect_data
) {
188 struct sipe_core_private
*sipe_private
= ft_private
->sipe_private
;
190 g_string_append_printf(body
,
194 "AuthCookie: %u\r\n",
195 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
),
197 ft_private
->auth_cookie
);
200 if (sender_connect
) {
201 g_string_append(body
,
202 "Sender-Connect: TRUE\r\n");
205 sipe_ft_request(ft_private
, body
->str
);
207 g_string_free(body
, TRUE
);
211 listen_socket_created_cb(unsigned short port
, gpointer data
)
213 struct sipe_file_transfer
*ft
= data
;
215 SIPE_FILE_TRANSFER_PRIVATE
->port
= port
;
216 SIPE_FILE_TRANSFER_PRIVATE
->auth_cookie
= rand() % 1000000000;
218 if (sipe_backend_ft_is_incoming(ft
))
219 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE
, TRUE
, TRUE
, TRUE
);
221 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE
, FALSE
, TRUE
, FALSE
);
225 client_connected_cb(struct sipe_backend_fd
*fd
, gpointer data
)
227 struct sipe_file_transfer
*ft
= data
;
229 SIPE_FILE_TRANSFER_PRIVATE
->listendata
= NULL
;
231 if (!sipe_backend_fd_is_valid(fd
)) {
232 sipe_backend_ft_error(ft
, _("Socket read failed"));
233 sipe_backend_ft_cancel_local(ft
);
235 sipe_backend_ft_start(ft
, fd
, NULL
, 0);
238 sipe_backend_fd_free(fd
);
242 ft_incoming_init(struct sipe_file_transfer
*ft
,
243 SIPE_UNUSED_PARAMETER
const gchar
*filename
,
244 SIPE_UNUSED_PARAMETER gsize size
,
245 SIPE_UNUSED_PARAMETER
const gchar
*who
)
247 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
249 if (ft_private
->peer_using_nat
) {
250 ft_private
->listendata
=
251 sipe_backend_network_listen_range(SIPE_FT_TCP_PORT_MIN
,
252 SIPE_FT_TCP_PORT_MAX
,
253 listen_socket_created_cb
,
257 send_ft_accept(ft_private
, TRUE
, FALSE
, FALSE
);
262 ft_outgoing_init(struct sipe_file_transfer
*ft
, const gchar
*filename
,
263 gsize size
, const gchar
*who
)
265 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
266 struct sipe_core_private
*sipe_private
= ft_private
->sipe_private
;
267 struct sip_dialog
*dialog
;
269 const gchar
*ip
= sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
);
270 gchar
*body
= g_strdup_printf("Application-Name: File Transfer\r\n"
271 "Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n"
272 "Invitation-Command: INVITE\r\n"
273 "Invitation-Cookie: %s\r\n"
274 "Application-File: %s\r\n"
275 "Application-FileSize: %" G_GSIZE_FORMAT
"\r\n"
277 "Encryption: R\r\n", // TODO: non encrypted file transfer support
278 ft_private
->invitation_cookie
,
281 sipe_utils_ip_is_private(ip
) ? "Connectivity: N\r\n" : "");
283 struct sip_session
*session
= sipe_session_find_or_add_im(sipe_private
, who
);
286 sipe_session_enqueue_message(session
, body
, "text/x-msmsgsinvite");
288 dialog
= sipe_dialog_find(session
, who
);
289 if (dialog
&& !dialog
->outgoing_invite
) {
290 sipe_im_process_queue(sipe_private
, session
);
291 } else if (!dialog
|| !dialog
->outgoing_invite
) {
292 // Need to send the INVITE to get the outgoing dialog setup
293 sipe_im_invite(sipe_private
, session
, who
, body
, "text/x-msmsgsinvite", NULL
, FALSE
);
294 dialog
= sipe_dialog_find(session
, who
);
297 dialog
->filetransfers
= g_slist_append(dialog
->filetransfers
, ft_private
);
298 ft_private
->dialog
= dialog
;
303 void sipe_ft_incoming_transfer(struct sipe_core_private
*sipe_private
,
304 struct sip_dialog
*dialog
,
307 struct sipe_file_transfer_private
*ft_private
;
310 ft_private
= g_new0(struct sipe_file_transfer_private
, 1);
311 ft_private
->sipe_private
= sipe_private
;
313 ft_private
->public.ft_init
= ft_incoming_init
;
314 ft_private
->public.ft_start
= sipe_ft_tftp_start_receiving
;
315 ft_private
->public.ft_read
= sipe_ft_tftp_read
;
316 ft_private
->public.ft_end
= sipe_ft_tftp_stop_receiving
;
317 ft_private
->public.ft_request_denied
= ft_request_denied
;
318 ft_private
->public.ft_deallocate
= sipe_ft_free
;
320 generate_key(ft_private
->encryption_key
, SIPE_FT_KEY_LENGTH
);
321 generate_key(ft_private
->hash_key
, SIPE_FT_KEY_LENGTH
);
323 ft_private
->invitation_cookie
= g_strdup(sipe_utils_nameval_find(body
, "Invitation-Cookie"));
324 ft_private
->peer_using_nat
= sipe_strequal(sipe_utils_nameval_find(body
, "Connectivity"), "N");
326 ft_private
->dialog
= dialog
;
328 file_size
= g_ascii_strtoull(sipe_utils_nameval_find(body
,
329 "Application-FileSize"),
331 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC
,
332 SIPE_FILE_TRANSFER_PUBLIC
,
334 sipe_utils_nameval_find(body
, "Application-File"),
337 if (ft_private
->public.backend_private
!= NULL
) {
338 ft_private
->dialog
->filetransfers
= g_slist_append(ft_private
->dialog
->filetransfers
, ft_private
);
340 sipe_ft_free(SIPE_FILE_TRANSFER_PUBLIC
);
344 static struct sipe_file_transfer_private
*
345 sipe_find_ft(const struct sip_dialog
*dialog
, const gchar
*inv_cookie
)
347 GSList
*ftlist
= dialog
->filetransfers
;
348 for (; ftlist
!= NULL
; ftlist
= ftlist
->next
) {
349 struct sipe_file_transfer_private
*ft_private
= ftlist
->data
;
350 if (sipe_strequal(ft_private
->invitation_cookie
, inv_cookie
))
356 void sipe_ft_incoming_accept(struct sip_dialog
*dialog
, const GSList
*body
)
358 const gchar
*inv_cookie
= sipe_utils_nameval_find(body
, "Invitation-Cookie");
359 struct sipe_file_transfer_private
*ft_private
= sipe_find_ft(dialog
, inv_cookie
);
362 const gchar
*ip
= sipe_utils_nameval_find(body
, "IP-Address");
363 const gchar
*port_str
= sipe_utils_nameval_find(body
, "Port");
364 const gchar
*auth_cookie
= sipe_utils_nameval_find(body
, "AuthCookie");
365 const gchar
*enc_key_b64
= sipe_utils_nameval_find(body
, "Encryption-Key");
366 const gchar
*hash_key_b64
= sipe_utils_nameval_find(body
, "Hash-Key");
369 ft_private
->auth_cookie
= g_ascii_strtoull(auth_cookie
,
373 guchar
*enc_key
= g_base64_decode(enc_key_b64
,
375 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
376 memcpy(ft_private
->encryption_key
,
377 enc_key
, SIPE_FT_KEY_LENGTH
);
379 sipe_ft_raise_error_and_cancel(ft_private
,
380 _("Received encryption key has wrong size."));
388 guchar
*hash_key
= g_base64_decode(hash_key_b64
,
390 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
391 memcpy(ft_private
->hash_key
,
392 hash_key
, SIPE_FT_KEY_LENGTH
);
394 sipe_ft_raise_error_and_cancel(ft_private
,
395 _("Received hash key has wrong size."));
403 if (ip
&& port_str
) {
404 sipe_backend_ft_start(SIPE_FILE_TRANSFER_PUBLIC
, NULL
, ip
,
405 g_ascii_strtoull(port_str
, NULL
, 10));
407 ft_private
->listendata
=
408 sipe_backend_network_listen_range(SIPE_FT_TCP_PORT_MIN
,
409 SIPE_FT_TCP_PORT_MAX
,
410 listen_socket_created_cb
,
413 if (!ft_private
->listendata
)
414 sipe_ft_raise_error_and_cancel(ft_private
,
415 _("Could not create listen socket"));
420 void sipe_ft_incoming_cancel(struct sip_dialog
*dialog
, const GSList
*body
)
422 const gchar
*inv_cookie
= sipe_utils_nameval_find(body
, "Invitation-Cookie");
423 struct sipe_file_transfer_private
*ft_private
= sipe_find_ft(dialog
, inv_cookie
);
426 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC
);
429 GSList
*sipe_ft_parse_msg_body(const gchar
*body
)
432 gchar
**lines
= g_strsplit(body
, "\r\n", 0);
433 if (sipe_utils_parse_lines(&list
, lines
, ":") == FALSE
) {
434 sipe_utils_nameval_free(list
);