1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* MSN module - Miscellaneous utilities */ |
---|
8 | |
---|
9 | /* |
---|
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. |
---|
14 | |
---|
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. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "nogaim.h" |
---|
27 | #include "msn.h" |
---|
28 | #include "md5.h" |
---|
29 | #include <ctype.h> |
---|
30 | |
---|
31 | int msn_write( struct im_connection *ic, char *s, int len ) |
---|
32 | { |
---|
33 | struct msn_data *md = ic->proto_data; |
---|
34 | int st; |
---|
35 | |
---|
36 | st = write( md->fd, s, len ); |
---|
37 | if( st != len ) |
---|
38 | { |
---|
39 | imcb_error( ic, "Short write() to main server" ); |
---|
40 | imc_logout( ic, TRUE ); |
---|
41 | return( 0 ); |
---|
42 | } |
---|
43 | |
---|
44 | return( 1 ); |
---|
45 | } |
---|
46 | |
---|
47 | int msn_logged_in( struct im_connection *ic ) |
---|
48 | { |
---|
49 | imcb_connected( ic ); |
---|
50 | |
---|
51 | return( 0 ); |
---|
52 | } |
---|
53 | |
---|
54 | int msn_buddy_list_add( struct im_connection *ic, char *list, char *who, char *realname_ ) |
---|
55 | { |
---|
56 | struct msn_data *md = ic->proto_data; |
---|
57 | char buf[1024], *realname; |
---|
58 | |
---|
59 | realname = msn_http_encode( realname_ ); |
---|
60 | |
---|
61 | g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s\r\n", ++md->trId, list, who, realname ); |
---|
62 | if( msn_write( ic, buf, strlen( buf ) ) ) |
---|
63 | { |
---|
64 | g_free( realname ); |
---|
65 | |
---|
66 | return( 1 ); |
---|
67 | } |
---|
68 | |
---|
69 | g_free( realname ); |
---|
70 | |
---|
71 | return( 0 ); |
---|
72 | } |
---|
73 | |
---|
74 | int msn_buddy_list_remove( struct im_connection *ic, char *list, char *who ) |
---|
75 | { |
---|
76 | struct msn_data *md = ic->proto_data; |
---|
77 | char buf[1024]; |
---|
78 | |
---|
79 | g_snprintf( buf, sizeof( buf ), "REM %d %s %s\r\n", ++md->trId, list, who ); |
---|
80 | if( msn_write( ic, buf, strlen( buf ) ) ) |
---|
81 | return( 1 ); |
---|
82 | |
---|
83 | return( 0 ); |
---|
84 | } |
---|
85 | |
---|
86 | struct msn_buddy_ask_data |
---|
87 | { |
---|
88 | struct im_connection *ic; |
---|
89 | char *handle; |
---|
90 | char *realname; |
---|
91 | }; |
---|
92 | |
---|
93 | static void msn_buddy_ask_yes( void *data ) |
---|
94 | { |
---|
95 | struct msn_buddy_ask_data *bla = data; |
---|
96 | |
---|
97 | msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname ); |
---|
98 | |
---|
99 | if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) |
---|
100 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
101 | |
---|
102 | g_free( bla->handle ); |
---|
103 | g_free( bla->realname ); |
---|
104 | g_free( bla ); |
---|
105 | } |
---|
106 | |
---|
107 | static void msn_buddy_ask_no( void *data ) |
---|
108 | { |
---|
109 | struct msn_buddy_ask_data *bla = data; |
---|
110 | |
---|
111 | msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname ); |
---|
112 | |
---|
113 | g_free( bla->handle ); |
---|
114 | g_free( bla->realname ); |
---|
115 | g_free( bla ); |
---|
116 | } |
---|
117 | |
---|
118 | void msn_buddy_ask( struct im_connection *ic, char *handle, char *realname ) |
---|
119 | { |
---|
120 | struct msn_buddy_ask_data *bla = g_new0( struct msn_buddy_ask_data, 1 ); |
---|
121 | char buf[1024]; |
---|
122 | |
---|
123 | bla->ic = ic; |
---|
124 | bla->handle = g_strdup( handle ); |
---|
125 | bla->realname = g_strdup( realname ); |
---|
126 | |
---|
127 | g_snprintf( buf, sizeof( buf ), |
---|
128 | "The user %s (%s) wants to add you to his/her buddy list.", |
---|
129 | handle, realname ); |
---|
130 | imcb_ask( ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no ); |
---|
131 | } |
---|
132 | |
---|
133 | char *msn_findheader( char *text, char *header, int len ) |
---|
134 | { |
---|
135 | int hlen = strlen( header ), i; |
---|
136 | char *ret; |
---|
137 | |
---|
138 | if( len == 0 ) |
---|
139 | len = strlen( text ); |
---|
140 | |
---|
141 | i = 0; |
---|
142 | while( ( i + hlen ) < len ) |
---|
143 | { |
---|
144 | /* Maybe this is a bit over-commented, but I just hate this part... */ |
---|
145 | if( g_strncasecmp( text + i, header, hlen ) == 0 ) |
---|
146 | { |
---|
147 | /* Skip to the (probable) end of the header */ |
---|
148 | i += hlen; |
---|
149 | |
---|
150 | /* Find the first non-[: \t] character */ |
---|
151 | while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++; |
---|
152 | |
---|
153 | /* Make sure we're still inside the string */ |
---|
154 | if( i >= len ) return( NULL ); |
---|
155 | |
---|
156 | /* Save the position */ |
---|
157 | ret = text + i; |
---|
158 | |
---|
159 | /* Search for the end of this line */ |
---|
160 | while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++; |
---|
161 | |
---|
162 | /* Make sure we're still inside the string */ |
---|
163 | if( i >= len ) return( NULL ); |
---|
164 | |
---|
165 | /* Copy the found data */ |
---|
166 | return( g_strndup( ret, text + i - ret ) ); |
---|
167 | } |
---|
168 | |
---|
169 | /* This wasn't the header we were looking for, skip to the next line. */ |
---|
170 | while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++; |
---|
171 | while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++; |
---|
172 | |
---|
173 | /* End of headers? */ |
---|
174 | if( ( i >= 4 && strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ) || |
---|
175 | ( i >= 2 && ( strncmp( text + i - 2, "\n\n", 2 ) == 0 || |
---|
176 | strncmp( text + i - 2, "\r\r", 2 ) == 0 ) ) ) |
---|
177 | { |
---|
178 | break; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | return( NULL ); |
---|
183 | } |
---|
184 | |
---|
185 | /* *NOT* thread-safe, but that's not a problem for now... */ |
---|
186 | char **msn_linesplit( char *line ) |
---|
187 | { |
---|
188 | static char **ret = NULL; |
---|
189 | static int size = 3; |
---|
190 | int i, n = 0; |
---|
191 | |
---|
192 | if( ret == NULL ) |
---|
193 | ret = g_new0( char*, size ); |
---|
194 | |
---|
195 | for( i = 0; line[i] && line[i] == ' '; i ++ ); |
---|
196 | if( line[i] ) |
---|
197 | { |
---|
198 | ret[n++] = line + i; |
---|
199 | for( i ++; line[i]; i ++ ) |
---|
200 | { |
---|
201 | if( line[i] == ' ' ) |
---|
202 | line[i] = 0; |
---|
203 | else if( line[i] != ' ' && !line[i-1] ) |
---|
204 | ret[n++] = line + i; |
---|
205 | |
---|
206 | if( n >= size ) |
---|
207 | ret = g_renew( char*, ret, size += 2 ); |
---|
208 | } |
---|
209 | } |
---|
210 | ret[n] = NULL; |
---|
211 | |
---|
212 | return( ret ); |
---|
213 | } |
---|
214 | |
---|
215 | /* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give |
---|
216 | commands, but sometimes they give additional data (payload). This function tries to handle |
---|
217 | this all in a nice way and send all data to the right places. */ |
---|
218 | |
---|
219 | /* Return values: -1: Read error, abort connection. |
---|
220 | 0: Command reported error; Abort *immediately*. (The connection does not exist anymore) |
---|
221 | 1: OK */ |
---|
222 | |
---|
223 | int msn_handler( struct msn_handler_data *h ) |
---|
224 | { |
---|
225 | int st; |
---|
226 | |
---|
227 | h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 ); |
---|
228 | st = read( h->fd, h->rxq + h->rxlen, 1024 ); |
---|
229 | h->rxlen += st; |
---|
230 | |
---|
231 | if( st <= 0 ) |
---|
232 | return( -1 ); |
---|
233 | |
---|
234 | while( st ) |
---|
235 | { |
---|
236 | int i; |
---|
237 | |
---|
238 | if( h->msglen == 0 ) |
---|
239 | { |
---|
240 | for( i = 0; i < h->rxlen; i ++ ) |
---|
241 | { |
---|
242 | if( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) |
---|
243 | { |
---|
244 | char *cmd_text, **cmd; |
---|
245 | int count; |
---|
246 | |
---|
247 | cmd_text = g_strndup( h->rxq, i ); |
---|
248 | cmd = msn_linesplit( cmd_text ); |
---|
249 | for( count = 0; cmd[count]; count ++ ); |
---|
250 | st = h->exec_command( h->data, cmd, count ); |
---|
251 | g_free( cmd_text ); |
---|
252 | |
---|
253 | /* If the connection broke, don't continue. We don't even exist anymore. */ |
---|
254 | if( !st ) |
---|
255 | return( 0 ); |
---|
256 | |
---|
257 | if( h->msglen ) |
---|
258 | h->cmd_text = g_strndup( h->rxq, i ); |
---|
259 | |
---|
260 | /* Skip to the next non-emptyline */ |
---|
261 | while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++; |
---|
262 | |
---|
263 | break; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /* If we reached the end of the buffer, there's still an incomplete command there. |
---|
268 | Return and wait for more data. */ |
---|
269 | if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' ) |
---|
270 | break; |
---|
271 | } |
---|
272 | else |
---|
273 | { |
---|
274 | char *msg, **cmd; |
---|
275 | int count; |
---|
276 | |
---|
277 | /* Do we have the complete message already? */ |
---|
278 | if( h->msglen > h->rxlen ) |
---|
279 | break; |
---|
280 | |
---|
281 | msg = g_strndup( h->rxq, h->msglen ); |
---|
282 | cmd = msn_linesplit( h->cmd_text ); |
---|
283 | for( count = 0; cmd[count]; count ++ ); |
---|
284 | |
---|
285 | st = h->exec_message( h->data, msg, h->msglen, cmd, count ); |
---|
286 | g_free( msg ); |
---|
287 | g_free( h->cmd_text ); |
---|
288 | h->cmd_text = NULL; |
---|
289 | |
---|
290 | if( !st ) |
---|
291 | return( 0 ); |
---|
292 | |
---|
293 | i = h->msglen; |
---|
294 | h->msglen = 0; |
---|
295 | } |
---|
296 | |
---|
297 | /* More data after this block? */ |
---|
298 | if( i < h->rxlen ) |
---|
299 | { |
---|
300 | char *tmp; |
---|
301 | |
---|
302 | tmp = g_memdup( h->rxq + i, h->rxlen - i ); |
---|
303 | g_free( h->rxq ); |
---|
304 | h->rxq = tmp; |
---|
305 | h->rxlen -= i; |
---|
306 | i = 0; |
---|
307 | } |
---|
308 | else |
---|
309 | /* If not, reset the rx queue and get lost. */ |
---|
310 | { |
---|
311 | g_free( h->rxq ); |
---|
312 | h->rxq = g_new0( char, 1 ); |
---|
313 | h->rxlen = 0; |
---|
314 | return( 1 ); |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | return( 1 ); |
---|
319 | } |
---|
320 | |
---|
321 | /* The difference between this function and the normal http_encode() function |
---|
322 | is that this one escapes every 7-bit ASCII character because this is said |
---|
323 | to avoid some lame server-side checks when setting a real-name. Also, |
---|
324 | non-ASCII characters are not escaped because MSN servers don't seem to |
---|
325 | appreciate that! */ |
---|
326 | char *msn_http_encode( const char *input ) |
---|
327 | { |
---|
328 | char *ret, *s; |
---|
329 | int i; |
---|
330 | |
---|
331 | ret = s = g_new0( char, strlen( input ) * 3 + 1 ); |
---|
332 | for( i = 0; input[i]; i ++ ) |
---|
333 | if( input[i] & 128 ) |
---|
334 | { |
---|
335 | *s = input[i]; |
---|
336 | s ++; |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | g_snprintf( s, 4, "%%%02X", input[i] ); |
---|
341 | s += 3; |
---|
342 | } |
---|
343 | |
---|
344 | return ret; |
---|
345 | } |
---|
346 | |
---|
347 | void msn_msgq_purge( struct im_connection *ic, GSList **list ) |
---|
348 | { |
---|
349 | struct msn_message *m; |
---|
350 | GString *ret; |
---|
351 | GSList *l; |
---|
352 | |
---|
353 | l = *list; |
---|
354 | if( l == NULL ) |
---|
355 | return; |
---|
356 | |
---|
357 | m = l->data; |
---|
358 | ret = g_string_sized_new( 1024 ); |
---|
359 | g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent " |
---|
360 | "messages to %s:", m->who ? m->who : "unknown recipient" ); |
---|
361 | |
---|
362 | while( l ) |
---|
363 | { |
---|
364 | m = l->data; |
---|
365 | |
---|
366 | g_string_append_printf( ret, "\n%s", m->text ); |
---|
367 | |
---|
368 | g_free( m->who ); |
---|
369 | g_free( m->text ); |
---|
370 | g_free( m ); |
---|
371 | |
---|
372 | l = l->next; |
---|
373 | } |
---|
374 | g_slist_free( *list ); |
---|
375 | *list = NULL; |
---|
376 | |
---|
377 | imcb_log( ic, "%s", ret->str ); |
---|
378 | g_string_free( ret, TRUE ); |
---|
379 | } |
---|
380 | |
---|
381 | unsigned int little_endian( unsigned int dw ) |
---|
382 | { |
---|
383 | #if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN |
---|
384 | return dw; |
---|
385 | #else |
---|
386 | /* We're still not sure if this machine is big endian since the |
---|
387 | constants above are not that portable. Don't swap bytes, just |
---|
388 | force-compose a 32-bit little endian integer. */ |
---|
389 | unsigned int ret = 0, i; |
---|
390 | char *dst = (char*) (&ret + 1); |
---|
391 | |
---|
392 | for (i = 0; i < 4; i ++) |
---|
393 | { |
---|
394 | *(--dst) = dw >> 24; |
---|
395 | dw <<= 8; |
---|
396 | } |
---|
397 | |
---|
398 | return ret; |
---|
399 | #endif |
---|
400 | } |
---|
401 | |
---|
402 | /* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */ |
---|
403 | |
---|
404 | char *msn_p11_challenge( char *challenge ) |
---|
405 | { |
---|
406 | char *output, buf[256]; |
---|
407 | md5_state_t md5c; |
---|
408 | unsigned char md5Hash[16], *newHash; |
---|
409 | unsigned int *md5Parts, *chlStringParts, newHashParts[5]; |
---|
410 | long long nHigh = 0, nLow = 0; |
---|
411 | int i, n; |
---|
412 | |
---|
413 | /* Create the MD5 hash */ |
---|
414 | md5_init(&md5c); |
---|
415 | md5_append(&md5c, (unsigned char*) challenge, strlen(challenge)); |
---|
416 | md5_append(&md5c, (unsigned char*) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY)); |
---|
417 | md5_finish(&md5c, md5Hash); |
---|
418 | |
---|
419 | /* Split it into four integers */ |
---|
420 | md5Parts = (unsigned int *)md5Hash; |
---|
421 | for (i = 0; i < 4; i ++) |
---|
422 | { |
---|
423 | md5Parts[i] = little_endian(md5Parts[i]); |
---|
424 | |
---|
425 | /* & each integer with 0x7FFFFFFF */ |
---|
426 | /* and save one unmodified array for later */ |
---|
427 | newHashParts[i] = md5Parts[i]; |
---|
428 | md5Parts[i] &= 0x7FFFFFFF; |
---|
429 | } |
---|
430 | |
---|
431 | /* make a new string and pad with '0' */ |
---|
432 | n = g_snprintf(buf, sizeof(buf)-5, "%s%s00000000", challenge, MSNP11_PROD_ID); |
---|
433 | /* truncate at an 8-byte boundary */ |
---|
434 | buf[n&=~7] = '\0'; |
---|
435 | |
---|
436 | /* split into integers */ |
---|
437 | chlStringParts = (unsigned int *)buf; |
---|
438 | |
---|
439 | /* this is magic */ |
---|
440 | for (i = 0; i < (n / 4) - 1; i += 2) |
---|
441 | { |
---|
442 | long long temp; |
---|
443 | |
---|
444 | chlStringParts[i] = little_endian(chlStringParts[i]); |
---|
445 | chlStringParts[i+1] = little_endian(chlStringParts[i+1]); |
---|
446 | |
---|
447 | temp = (md5Parts[0] * (((0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF)+nHigh) + md5Parts[1])%0x7FFFFFFF; |
---|
448 | nHigh = (md5Parts[2] * (((long long)chlStringParts[i+1]+temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF; |
---|
449 | nLow = nLow + nHigh + temp; |
---|
450 | } |
---|
451 | nHigh = (nHigh+md5Parts[1]) % 0x7FFFFFFF; |
---|
452 | nLow = (nLow+md5Parts[3]) % 0x7FFFFFFF; |
---|
453 | |
---|
454 | newHashParts[0] ^= nHigh; |
---|
455 | newHashParts[1] ^= nLow; |
---|
456 | newHashParts[2] ^= nHigh; |
---|
457 | newHashParts[3] ^= nLow; |
---|
458 | |
---|
459 | /* swap more bytes if big endian */ |
---|
460 | for (i = 0; i < 4; i ++) |
---|
461 | newHashParts[i] = little_endian(newHashParts[i]); |
---|
462 | |
---|
463 | /* make a string of the parts */ |
---|
464 | newHash = (unsigned char *)newHashParts; |
---|
465 | |
---|
466 | /* convert to hexadecimal */ |
---|
467 | output = g_new(char, 33); |
---|
468 | for (i = 0; i < 16; i ++) |
---|
469 | sprintf(output + i * 2, "%02x", newHash[i]); |
---|
470 | |
---|
471 | return output; |
---|
472 | } |
---|