1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * Various utility functions. Some are copied from Gaim to support the |
---|
9 | * IM-modules, most are from BitlBee. |
---|
10 | * |
---|
11 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
12 | * (and possibly other members of the Gaim team) |
---|
13 | * Copyright 2002-2006 Wilmer van der Gaast <wilmer@gaast.net> |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | This program is free software; you can redistribute it and/or modify |
---|
18 | it under the terms of the GNU General Public License as published by |
---|
19 | the Free Software Foundation; either version 2 of the License, or |
---|
20 | (at your option) any later version. |
---|
21 | |
---|
22 | This program is distributed in the hope that it will be useful, |
---|
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
25 | GNU General Public License for more details. |
---|
26 | |
---|
27 | You should have received a copy of the GNU General Public License with |
---|
28 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
29 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
30 | Suite 330, Boston, MA 02111-1307 USA |
---|
31 | */ |
---|
32 | |
---|
33 | #define BITLBEE_CORE |
---|
34 | #include "nogaim.h" |
---|
35 | #include "base64.h" |
---|
36 | #include <stdio.h> |
---|
37 | #include <stdlib.h> |
---|
38 | #include <string.h> |
---|
39 | #include <ctype.h> |
---|
40 | #include <glib.h> |
---|
41 | #include <time.h> |
---|
42 | |
---|
43 | #ifdef HAVE_RESOLV_A |
---|
44 | #include <arpa/nameser.h> |
---|
45 | #include <resolv.h> |
---|
46 | #endif |
---|
47 | |
---|
48 | #include "ssl_client.h" |
---|
49 | |
---|
50 | void strip_linefeed(gchar *text) |
---|
51 | { |
---|
52 | int i, j; |
---|
53 | gchar *text2 = g_malloc(strlen(text) + 1); |
---|
54 | |
---|
55 | for (i = 0, j = 0; text[i]; i++) |
---|
56 | if (text[i] != '\r') |
---|
57 | text2[j++] = text[i]; |
---|
58 | text2[j] = '\0'; |
---|
59 | |
---|
60 | strcpy(text, text2); |
---|
61 | g_free(text2); |
---|
62 | } |
---|
63 | |
---|
64 | char *normalize(const char *s) |
---|
65 | { |
---|
66 | static char buf[BUF_LEN]; |
---|
67 | char *t, *u; |
---|
68 | int x = 0; |
---|
69 | |
---|
70 | g_return_val_if_fail((s != NULL), NULL); |
---|
71 | |
---|
72 | u = t = g_strdup(s); |
---|
73 | |
---|
74 | strcpy(t, s); |
---|
75 | g_strdown(t); |
---|
76 | |
---|
77 | while (*t && (x < BUF_LEN - 1)) { |
---|
78 | if (*t != ' ') { |
---|
79 | buf[x] = *t; |
---|
80 | x++; |
---|
81 | } |
---|
82 | t++; |
---|
83 | } |
---|
84 | buf[x] = '\0'; |
---|
85 | g_free(u); |
---|
86 | return buf; |
---|
87 | } |
---|
88 | |
---|
89 | time_t get_time(int year, int month, int day, int hour, int min, int sec) |
---|
90 | { |
---|
91 | struct tm tm; |
---|
92 | |
---|
93 | memset(&tm, 0, sizeof(struct tm)); |
---|
94 | tm.tm_year = year - 1900; |
---|
95 | tm.tm_mon = month - 1; |
---|
96 | tm.tm_mday = day; |
---|
97 | tm.tm_hour = hour; |
---|
98 | tm.tm_min = min; |
---|
99 | tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; |
---|
100 | |
---|
101 | return mktime(&tm); |
---|
102 | } |
---|
103 | |
---|
104 | typedef struct htmlentity |
---|
105 | { |
---|
106 | char code[7]; |
---|
107 | char is[3]; |
---|
108 | } htmlentity_t; |
---|
109 | |
---|
110 | static const htmlentity_t ent[] = |
---|
111 | { |
---|
112 | { "lt", "<" }, |
---|
113 | { "gt", ">" }, |
---|
114 | { "amp", "&" }, |
---|
115 | { "quot", "\"" }, |
---|
116 | { "aacute", "á" }, |
---|
117 | { "eacute", "é" }, |
---|
118 | { "iacute", "é" }, |
---|
119 | { "oacute", "ó" }, |
---|
120 | { "uacute", "ú" }, |
---|
121 | { "agrave", "à" }, |
---|
122 | { "egrave", "è" }, |
---|
123 | { "igrave", "ì" }, |
---|
124 | { "ograve", "ò" }, |
---|
125 | { "ugrave", "ù" }, |
---|
126 | { "acirc", "â" }, |
---|
127 | { "ecirc", "ê" }, |
---|
128 | { "icirc", "î" }, |
---|
129 | { "ocirc", "ô" }, |
---|
130 | { "ucirc", "û" }, |
---|
131 | { "auml", "ä" }, |
---|
132 | { "euml", "ë" }, |
---|
133 | { "iuml", "ï" }, |
---|
134 | { "ouml", "ö" }, |
---|
135 | { "uuml", "ü" }, |
---|
136 | { "nbsp", " " }, |
---|
137 | { "", "" } |
---|
138 | }; |
---|
139 | |
---|
140 | void strip_html( char *in ) |
---|
141 | { |
---|
142 | char *start = in; |
---|
143 | char *out = g_malloc( strlen( in ) + 1 ); |
---|
144 | char *s = out, *cs; |
---|
145 | int i, matched; |
---|
146 | |
---|
147 | memset( out, 0, strlen( in ) + 1 ); |
---|
148 | |
---|
149 | while( *in ) |
---|
150 | { |
---|
151 | if( *in == '<' && ( isalpha( *(in+1) ) || *(in+1) == '/' ) ) |
---|
152 | { |
---|
153 | /* If in points at a < and in+1 points at a letter or a slash, this is probably |
---|
154 | a HTML-tag. Try to find a closing > and continue there. If the > can't be |
---|
155 | found, assume that it wasn't a HTML-tag after all. */ |
---|
156 | |
---|
157 | cs = in; |
---|
158 | |
---|
159 | while( *in && *in != '>' ) |
---|
160 | in ++; |
---|
161 | |
---|
162 | if( *in ) |
---|
163 | { |
---|
164 | if( g_strncasecmp( cs+1, "br", 2) == 0 ) |
---|
165 | *(s++) = '\n'; |
---|
166 | in ++; |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | in = cs; |
---|
171 | *(s++) = *(in++); |
---|
172 | } |
---|
173 | } |
---|
174 | else if( *in == '&' ) |
---|
175 | { |
---|
176 | cs = ++in; |
---|
177 | while( *in && isalpha( *in ) ) |
---|
178 | in ++; |
---|
179 | |
---|
180 | if( *in == ';' ) in ++; |
---|
181 | matched = 0; |
---|
182 | |
---|
183 | for( i = 0; *ent[i].code; i ++ ) |
---|
184 | if( g_strncasecmp( ent[i].code, cs, strlen( ent[i].code ) ) == 0 ) |
---|
185 | { |
---|
186 | int j; |
---|
187 | |
---|
188 | for( j = 0; ent[i].is[j]; j ++ ) |
---|
189 | *(s++) = ent[i].is[j]; |
---|
190 | |
---|
191 | matched = 1; |
---|
192 | break; |
---|
193 | } |
---|
194 | |
---|
195 | /* None of the entities were matched, so return the string */ |
---|
196 | if( !matched ) |
---|
197 | { |
---|
198 | in = cs - 1; |
---|
199 | *(s++) = *(in++); |
---|
200 | } |
---|
201 | } |
---|
202 | else |
---|
203 | { |
---|
204 | *(s++) = *(in++); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | strcpy( start, out ); |
---|
209 | g_free( out ); |
---|
210 | } |
---|
211 | |
---|
212 | char *escape_html( const char *html ) |
---|
213 | { |
---|
214 | const char *c = html; |
---|
215 | GString *ret; |
---|
216 | char *str; |
---|
217 | |
---|
218 | if( html == NULL ) |
---|
219 | return( NULL ); |
---|
220 | |
---|
221 | ret = g_string_new( "" ); |
---|
222 | |
---|
223 | while( *c ) |
---|
224 | { |
---|
225 | switch( *c ) |
---|
226 | { |
---|
227 | case '&': |
---|
228 | ret = g_string_append( ret, "&" ); |
---|
229 | break; |
---|
230 | case '<': |
---|
231 | ret = g_string_append( ret, "<" ); |
---|
232 | break; |
---|
233 | case '>': |
---|
234 | ret = g_string_append( ret, ">" ); |
---|
235 | break; |
---|
236 | case '"': |
---|
237 | ret = g_string_append( ret, """ ); |
---|
238 | break; |
---|
239 | default: |
---|
240 | ret = g_string_append_c( ret, *c ); |
---|
241 | } |
---|
242 | c ++; |
---|
243 | } |
---|
244 | |
---|
245 | str = ret->str; |
---|
246 | g_string_free( ret, FALSE ); |
---|
247 | return( str ); |
---|
248 | } |
---|
249 | |
---|
250 | /* Decode%20a%20file%20name */ |
---|
251 | void http_decode( char *s ) |
---|
252 | { |
---|
253 | char *t; |
---|
254 | int i, j, k; |
---|
255 | |
---|
256 | t = g_new( char, strlen( s ) + 1 ); |
---|
257 | |
---|
258 | for( i = j = 0; s[i]; i ++, j ++ ) |
---|
259 | { |
---|
260 | if( s[i] == '%' ) |
---|
261 | { |
---|
262 | if( sscanf( s + i + 1, "%2x", &k ) ) |
---|
263 | { |
---|
264 | t[j] = k; |
---|
265 | i += 2; |
---|
266 | } |
---|
267 | else |
---|
268 | { |
---|
269 | *t = 0; |
---|
270 | break; |
---|
271 | } |
---|
272 | } |
---|
273 | else |
---|
274 | { |
---|
275 | t[j] = s[i]; |
---|
276 | } |
---|
277 | } |
---|
278 | t[j] = 0; |
---|
279 | |
---|
280 | strcpy( s, t ); |
---|
281 | g_free( t ); |
---|
282 | } |
---|
283 | |
---|
284 | /* Warning: This one explodes the string. Worst-cases can make the string 3x its original size! */ |
---|
285 | /* This fuction is safe, but make sure you call it safely as well! */ |
---|
286 | void http_encode( char *s ) |
---|
287 | { |
---|
288 | char *t; |
---|
289 | int i, j; |
---|
290 | |
---|
291 | t = g_strdup( s ); |
---|
292 | |
---|
293 | for( i = j = 0; t[i]; i ++, j ++ ) |
---|
294 | { |
---|
295 | /* if( t[i] <= ' ' || ((unsigned char *)t)[i] >= 128 || t[i] == '%' ) */ |
---|
296 | if( !isalnum( t[i] ) ) |
---|
297 | { |
---|
298 | sprintf( s + j, "%%%02X", ((unsigned char*)t)[i] ); |
---|
299 | j += 2; |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | s[j] = t[i]; |
---|
304 | } |
---|
305 | } |
---|
306 | s[j] = 0; |
---|
307 | |
---|
308 | g_free( t ); |
---|
309 | } |
---|
310 | |
---|
311 | /* Strip newlines from a string. Modifies the string passed to it. */ |
---|
312 | char *strip_newlines( char *source ) |
---|
313 | { |
---|
314 | int i; |
---|
315 | |
---|
316 | for( i = 0; source[i] != '\0'; i ++ ) |
---|
317 | if( source[i] == '\n' || source[i] == '\r' ) |
---|
318 | source[i] = ' '; |
---|
319 | |
---|
320 | return source; |
---|
321 | } |
---|
322 | |
---|
323 | /* Wrap an IPv4 address into IPv6 space. Not thread-safe... */ |
---|
324 | char *ipv6_wrap( char *src ) |
---|
325 | { |
---|
326 | static char dst[64]; |
---|
327 | int i; |
---|
328 | |
---|
329 | for( i = 0; src[i]; i ++ ) |
---|
330 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
331 | break; |
---|
332 | |
---|
333 | /* Hmm, it's not even an IP... */ |
---|
334 | if( src[i] ) |
---|
335 | return src; |
---|
336 | |
---|
337 | g_snprintf( dst, sizeof( dst ), "::ffff:%s", src ); |
---|
338 | |
---|
339 | return dst; |
---|
340 | } |
---|
341 | |
---|
342 | /* Unwrap an IPv4 address into IPv6 space. Thread-safe, because it's very simple. :-) */ |
---|
343 | char *ipv6_unwrap( char *src ) |
---|
344 | { |
---|
345 | int i; |
---|
346 | |
---|
347 | if( g_strncasecmp( src, "::ffff:", 7 ) != 0 ) |
---|
348 | return src; |
---|
349 | |
---|
350 | for( i = 7; src[i]; i ++ ) |
---|
351 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
352 | break; |
---|
353 | |
---|
354 | /* Hmm, it's not even an IP... */ |
---|
355 | if( src[i] ) |
---|
356 | return src; |
---|
357 | |
---|
358 | return ( src + 7 ); |
---|
359 | } |
---|
360 | |
---|
361 | /* Convert from one charset to another. |
---|
362 | |
---|
363 | from_cs, to_cs: Source and destination charsets |
---|
364 | src, dst: Source and destination strings |
---|
365 | size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. |
---|
366 | maxbuf: Maximum number of bytes to write to dst |
---|
367 | |
---|
368 | Returns the number of bytes written to maxbuf or -1 on an error. |
---|
369 | */ |
---|
370 | signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ) |
---|
371 | { |
---|
372 | GIConv cd; |
---|
373 | size_t res; |
---|
374 | size_t inbytesleft, outbytesleft; |
---|
375 | char *inbuf = src; |
---|
376 | char *outbuf = dst; |
---|
377 | |
---|
378 | cd = g_iconv_open( to_cs, from_cs ); |
---|
379 | if( cd == (GIConv) -1 ) |
---|
380 | return( -1 ); |
---|
381 | |
---|
382 | inbytesleft = size ? size : strlen( src ); |
---|
383 | outbytesleft = maxbuf - 1; |
---|
384 | res = g_iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); |
---|
385 | *outbuf = '\0'; |
---|
386 | g_iconv_close( cd ); |
---|
387 | |
---|
388 | if( res == (size_t) -1 ) |
---|
389 | return( -1 ); |
---|
390 | else |
---|
391 | return( outbuf - dst ); |
---|
392 | } |
---|
393 | |
---|
394 | /* A pretty reliable random number generator. Tries to use the /dev/random |
---|
395 | devices first, and falls back to the random number generator from libc |
---|
396 | when it fails. Opens randomizer devices with O_NONBLOCK to make sure a |
---|
397 | lack of entropy won't halt BitlBee. */ |
---|
398 | void random_bytes( unsigned char *buf, int count ) |
---|
399 | { |
---|
400 | #ifndef _WIN32 |
---|
401 | static int use_dev = -1; |
---|
402 | |
---|
403 | /* Actually this probing code isn't really necessary, is it? */ |
---|
404 | if( use_dev == -1 ) |
---|
405 | { |
---|
406 | if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) |
---|
407 | use_dev = 1; |
---|
408 | else |
---|
409 | { |
---|
410 | use_dev = 0; |
---|
411 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | if( use_dev ) |
---|
416 | { |
---|
417 | int fd; |
---|
418 | |
---|
419 | /* At least on Linux, /dev/random can block if there's not |
---|
420 | enough entropy. We really don't want that, so if it can't |
---|
421 | give anything, use /dev/urandom instead. */ |
---|
422 | if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
423 | if( read( fd, buf, count ) == count ) |
---|
424 | { |
---|
425 | close( fd ); |
---|
426 | return; |
---|
427 | } |
---|
428 | close( fd ); |
---|
429 | |
---|
430 | /* urandom isn't supposed to block at all, but just to be |
---|
431 | sure. If it blocks, we'll disable use_dev and use the libc |
---|
432 | randomizer instead. */ |
---|
433 | if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
434 | if( read( fd, buf, count ) == count ) |
---|
435 | { |
---|
436 | close( fd ); |
---|
437 | return; |
---|
438 | } |
---|
439 | close( fd ); |
---|
440 | |
---|
441 | /* If /dev/random blocks once, we'll still try to use it |
---|
442 | again next time. If /dev/urandom also fails for some |
---|
443 | reason, stick with libc during this session. */ |
---|
444 | |
---|
445 | use_dev = 0; |
---|
446 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
447 | } |
---|
448 | |
---|
449 | if( !use_dev ) |
---|
450 | #endif |
---|
451 | { |
---|
452 | int i; |
---|
453 | |
---|
454 | /* Possibly the LSB of rand() isn't very random on some |
---|
455 | platforms. Seems okay on at least Linux and OSX though. */ |
---|
456 | for( i = 0; i < count; i ++ ) |
---|
457 | buf[i] = rand() & 0xff; |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | int is_bool( char *value ) |
---|
462 | { |
---|
463 | if( *value == 0 ) |
---|
464 | return 0; |
---|
465 | |
---|
466 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
467 | return 1; |
---|
468 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
469 | return 1; |
---|
470 | |
---|
471 | while( *value ) |
---|
472 | if( !isdigit( *value ) ) |
---|
473 | return 0; |
---|
474 | else |
---|
475 | value ++; |
---|
476 | |
---|
477 | return 1; |
---|
478 | } |
---|
479 | |
---|
480 | int bool2int( char *value ) |
---|
481 | { |
---|
482 | int i; |
---|
483 | |
---|
484 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
485 | return 1; |
---|
486 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
487 | return 0; |
---|
488 | |
---|
489 | if( sscanf( value, "%d", &i ) == 1 ) |
---|
490 | return i; |
---|
491 | |
---|
492 | return 0; |
---|
493 | } |
---|
494 | |
---|
495 | struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain ) |
---|
496 | { |
---|
497 | struct ns_srv_reply *reply = NULL; |
---|
498 | #ifdef HAVE_RESOLV_A |
---|
499 | char name[1024]; |
---|
500 | unsigned char querybuf[1024]; |
---|
501 | const unsigned char *buf; |
---|
502 | ns_msg nsh; |
---|
503 | ns_rr rr; |
---|
504 | int i, len, size; |
---|
505 | |
---|
506 | g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain ); |
---|
507 | |
---|
508 | if( ( size = res_query( name, ns_c_in, ns_t_srv, querybuf, sizeof( querybuf ) ) ) <= 0 ) |
---|
509 | return NULL; |
---|
510 | |
---|
511 | if( ns_initparse( querybuf, size, &nsh ) != 0 ) |
---|
512 | return NULL; |
---|
513 | |
---|
514 | if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 ) |
---|
515 | return NULL; |
---|
516 | |
---|
517 | size = ns_rr_rdlen( rr ); |
---|
518 | buf = ns_rr_rdata( rr ); |
---|
519 | |
---|
520 | len = 0; |
---|
521 | for( i = 6; i < size && buf[i]; i += buf[i] + 1 ) |
---|
522 | len += buf[i] + 1; |
---|
523 | |
---|
524 | if( i > size ) |
---|
525 | return NULL; |
---|
526 | |
---|
527 | reply = g_malloc( sizeof( struct ns_srv_reply ) + len ); |
---|
528 | memcpy( reply->name, buf + 7, len ); |
---|
529 | |
---|
530 | for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 ) |
---|
531 | reply->name[i] = '.'; |
---|
532 | |
---|
533 | if( i > len ) |
---|
534 | { |
---|
535 | g_free( reply ); |
---|
536 | return NULL; |
---|
537 | } |
---|
538 | |
---|
539 | reply->prio = ( buf[0] << 8 ) | buf[1]; |
---|
540 | reply->weight = ( buf[2] << 8 ) | buf[3]; |
---|
541 | reply->port = ( buf[4] << 8 ) | buf[5]; |
---|
542 | #endif |
---|
543 | |
---|
544 | return reply; |
---|
545 | } |
---|
546 | |
---|
547 | /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ |
---|
548 | char *word_wrap( char *msg, int line_len ) |
---|
549 | { |
---|
550 | GString *ret = g_string_sized_new( strlen( msg ) + 16 ); |
---|
551 | |
---|
552 | while( strlen( msg ) > line_len ) |
---|
553 | { |
---|
554 | int i; |
---|
555 | |
---|
556 | /* First try to find out if there's a newline already. Don't |
---|
557 | want to add more splits than necessary. */ |
---|
558 | for( i = line_len; i > 0 && msg[i] != '\n'; i -- ); |
---|
559 | if( msg[i] == '\n' ) |
---|
560 | { |
---|
561 | g_string_append_len( ret, msg, i + 1 ); |
---|
562 | msg += i + 1; |
---|
563 | continue; |
---|
564 | } |
---|
565 | |
---|
566 | for( i = line_len; i > 0; i -- ) |
---|
567 | { |
---|
568 | if( msg[i] == '-' ) |
---|
569 | { |
---|
570 | g_string_append_len( ret, msg, i + 1 ); |
---|
571 | g_string_append_c( ret, '\n' ); |
---|
572 | msg += i + 1; |
---|
573 | break; |
---|
574 | } |
---|
575 | else if( msg[i] == ' ' ) |
---|
576 | { |
---|
577 | g_string_append_len( ret, msg, i ); |
---|
578 | g_string_append_c( ret, '\n' ); |
---|
579 | msg += i + 1; |
---|
580 | break; |
---|
581 | } |
---|
582 | } |
---|
583 | if( i == 0 ) |
---|
584 | { |
---|
585 | g_string_append_len( ret, msg, line_len ); |
---|
586 | g_string_append_c( ret, '\n' ); |
---|
587 | msg += line_len; |
---|
588 | } |
---|
589 | } |
---|
590 | g_string_append( ret, msg ); |
---|
591 | |
---|
592 | return g_string_free( ret, FALSE ); |
---|
593 | } |
---|
594 | |
---|
595 | gboolean ssl_sockerr_again( void *ssl ) |
---|
596 | { |
---|
597 | if( ssl ) |
---|
598 | return ssl_errno == SSL_AGAIN; |
---|
599 | else |
---|
600 | return sockerr_again(); |
---|
601 | } |
---|
602 | |
---|
603 | /* Returns values: -1 == Failure (base64-decoded to something unexpected) |
---|
604 | 0 == Okay |
---|
605 | 1 == Password doesn't match the hash. */ |
---|
606 | int md5_verify_password( char *password, char *hash ) |
---|
607 | { |
---|
608 | md5_byte_t *pass_dec = NULL; |
---|
609 | md5_byte_t pass_md5[16]; |
---|
610 | md5_state_t md5_state; |
---|
611 | int ret, i; |
---|
612 | |
---|
613 | if( base64_decode( hash, &pass_dec ) != 21 ) |
---|
614 | { |
---|
615 | ret = -1; |
---|
616 | } |
---|
617 | else |
---|
618 | { |
---|
619 | md5_init( &md5_state ); |
---|
620 | md5_append( &md5_state, (md5_byte_t*) password, strlen( password ) ); |
---|
621 | md5_append( &md5_state, (md5_byte_t*) pass_dec + 16, 5 ); /* Hmmm, salt! */ |
---|
622 | md5_finish( &md5_state, pass_md5 ); |
---|
623 | |
---|
624 | for( i = 0; i < 16; i ++ ) |
---|
625 | { |
---|
626 | if( pass_dec[i] != pass_md5[i] ) |
---|
627 | { |
---|
628 | ret = 1; |
---|
629 | break; |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | /* If we reached the end of the loop, it was a match! */ |
---|
634 | if( i == 16 ) |
---|
635 | ret = 0; |
---|
636 | } |
---|
637 | |
---|
638 | g_free( pass_dec ); |
---|
639 | |
---|
640 | return ret; |
---|
641 | } |
---|