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
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"
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
,
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
)
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
);
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
;
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
,
121 struct sip_dialog
*dialog
= ft_private
->dialog
;
122 sip_transport_request(ft_private
->sipe_private
,
126 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
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
);
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
);
159 gchar
*b64_encryption_key
;
162 b64_encryption_key
= g_base64_encode(ft_private
->encryption_key
,
164 b64_hash_key
= g_base64_encode(ft_private
->hash_key
,
167 g_string_append_printf(body
,
168 "Encryption-Key: %s\r\n"
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
,
184 "AuthCookie: %u\r\n",
185 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
),
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
);
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
);
211 send_ft_accept(SIPE_FILE_TRANSFER_PRIVATE
, FALSE
, TRUE
, FALSE
);
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
);
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
,
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
,
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"
263 "Encryption: R\r\n", // TODO: non encrypted file transfer support
264 ft_private
->invitation_cookie
,
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
);
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
;
289 void sipe_ft_incoming_transfer(struct sipe_core_private
*sipe_private
,
290 struct sip_dialog
*dialog
,
293 struct sipe_file_transfer_private
*ft_private
;
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"),
310 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC
,
311 SIPE_FILE_TRANSFER_PUBLIC
,
313 sipe_utils_nameval_find(body
, "Application-File"),
316 if (ft_private
->public.backend_private
!= NULL
) {
317 ft_private
->dialog
->filetransfers
= g_slist_append(ft_private
->dialog
->filetransfers
, ft_private
);
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
))
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
);
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");
348 ft_private
->auth_cookie
= g_ascii_strtoull(auth_cookie
,
352 guchar
*enc_key
= g_base64_decode(enc_key_b64
,
354 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
355 memcpy(ft_private
->encryption_key
,
356 enc_key
, SIPE_FT_KEY_LENGTH
);
358 sipe_ft_raise_error_and_cancel(ft_private
,
359 _("Received encryption key has wrong size."));
367 guchar
*hash_key
= g_base64_decode(hash_key_b64
,
369 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
370 memcpy(ft_private
->hash_key
,
371 hash_key
, SIPE_FT_KEY_LENGTH
);
373 sipe_ft_raise_error_and_cancel(ft_private
,
374 _("Received hash key has wrong size."));
382 if (ip
&& port_str
) {
383 sipe_backend_ft_start(SIPE_FILE_TRANSFER_PUBLIC
, NULL
, ip
,
384 g_ascii_strtoull(port_str
, NULL
, 10));
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
,
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
);
405 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC
);
408 GSList
*sipe_ft_parse_msg_body(const gchar
*body
)
411 gchar
**lines
= g_strsplit(body
, "\r\n", 0);
412 if (sipe_utils_parse_lines(&list
, lines
, ":") == FALSE
) {
413 sipe_utils_nameval_free(list
);