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