[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[1719464] | 4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* |
---|
[c88999c] | 8 | * Various utility functions. Some are copied from Gaim to support the |
---|
| 9 | * IM-modules, most are from BitlBee. |
---|
[b7d3cc34] | 10 | * |
---|
| 11 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
| 12 | * (and possibly other members of the Gaim team) |
---|
[1719464] | 13 | * Copyright 2002-2006 Wilmer van der Gaast <wilmer@gaast.net> |
---|
[b7d3cc34] | 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" |
---|
[4e8db1c] | 35 | #include "base64.h" |
---|
[b7d3cc34] | 36 | #include <stdio.h> |
---|
| 37 | #include <stdlib.h> |
---|
| 38 | #include <string.h> |
---|
[dd8d4c5] | 39 | #include <ctype.h> |
---|
[b7d3cc34] | 40 | #include <glib.h> |
---|
| 41 | #include <time.h> |
---|
| 42 | |
---|
[36e9f62] | 43 | #ifdef HAVE_RESOLV_A |
---|
| 44 | #include <arpa/nameser.h> |
---|
| 45 | #include <resolv.h> |
---|
| 46 | #endif |
---|
| 47 | |
---|
[d52111a] | 48 | #include "ssl_client.h" |
---|
| 49 | |
---|
[b7d3cc34] | 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 | |
---|
[613cc55] | 93 | memset(&tm, 0, sizeof(struct tm)); |
---|
[b7d3cc34] | 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; |
---|
[613cc55] | 100 | |
---|
[b7d3cc34] | 101 | return mktime(&tm); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | typedef struct htmlentity |
---|
| 105 | { |
---|
[51fdc45] | 106 | char code[7]; |
---|
| 107 | char is[3]; |
---|
[b7d3cc34] | 108 | } htmlentity_t; |
---|
| 109 | |
---|
[39cc341] | 110 | static const htmlentity_t ent[] = |
---|
[b7d3cc34] | 111 | { |
---|
[39cc341] | 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 | { "", "" } |
---|
[b7d3cc34] | 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 | { |
---|
[39cc341] | 186 | int j; |
---|
| 187 | |
---|
| 188 | for( j = 0; ent[i].is[j]; j ++ ) |
---|
| 189 | *(s++) = ent[i].is[j]; |
---|
| 190 | |
---|
[b7d3cc34] | 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 | |
---|
[c88999c] | 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 | { |
---|
[dd8d4c5] | 295 | /* if( t[i] <= ' ' || ((unsigned char *)t)[i] >= 128 || t[i] == '%' ) */ |
---|
| 296 | if( !isalnum( t[i] ) ) |
---|
[c88999c] | 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 | } |
---|
[2a6ca4f] | 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 | } |
---|
[e27661d] | 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 | { |
---|
[574af7e] | 372 | GIConv cd; |
---|
[e27661d] | 373 | size_t res; |
---|
| 374 | size_t inbytesleft, outbytesleft; |
---|
| 375 | char *inbuf = src; |
---|
| 376 | char *outbuf = dst; |
---|
| 377 | |
---|
[574af7e] | 378 | cd = g_iconv_open( to_cs, from_cs ); |
---|
| 379 | if( cd == (GIConv) -1 ) |
---|
[e27661d] | 380 | return( -1 ); |
---|
| 381 | |
---|
| 382 | inbytesleft = size ? size : strlen( src ); |
---|
| 383 | outbytesleft = maxbuf - 1; |
---|
[574af7e] | 384 | res = g_iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); |
---|
[e27661d] | 385 | *outbuf = '\0'; |
---|
[574af7e] | 386 | g_iconv_close( cd ); |
---|
[e27661d] | 387 | |
---|
| 388 | if( res == (size_t) -1 ) |
---|
| 389 | return( -1 ); |
---|
| 390 | else |
---|
| 391 | return( outbuf - dst ); |
---|
| 392 | } |
---|
| 393 | |
---|
[5100caa] | 394 | int is_bool( char *value ) |
---|
| 395 | { |
---|
| 396 | if( *value == 0 ) |
---|
| 397 | return 0; |
---|
| 398 | |
---|
| 399 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 400 | return 1; |
---|
| 401 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 402 | return 1; |
---|
| 403 | |
---|
| 404 | while( *value ) |
---|
| 405 | if( !isdigit( *value ) ) |
---|
| 406 | return 0; |
---|
| 407 | else |
---|
| 408 | value ++; |
---|
| 409 | |
---|
| 410 | return 1; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | int bool2int( char *value ) |
---|
| 414 | { |
---|
| 415 | int i; |
---|
| 416 | |
---|
| 417 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 418 | return 1; |
---|
| 419 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 420 | return 0; |
---|
| 421 | |
---|
| 422 | if( sscanf( value, "%d", &i ) == 1 ) |
---|
| 423 | return i; |
---|
| 424 | |
---|
| 425 | return 0; |
---|
| 426 | } |
---|
[36e9f62] | 427 | |
---|
| 428 | struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain ) |
---|
| 429 | { |
---|
| 430 | struct ns_srv_reply *reply = NULL; |
---|
| 431 | #ifdef HAVE_RESOLV_A |
---|
| 432 | char name[1024]; |
---|
| 433 | unsigned char querybuf[1024]; |
---|
| 434 | const unsigned char *buf; |
---|
| 435 | ns_msg nsh; |
---|
| 436 | ns_rr rr; |
---|
| 437 | int i, len, size; |
---|
| 438 | |
---|
| 439 | g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain ); |
---|
| 440 | |
---|
| 441 | if( ( size = res_query( name, ns_c_in, ns_t_srv, querybuf, sizeof( querybuf ) ) ) <= 0 ) |
---|
| 442 | return NULL; |
---|
| 443 | |
---|
| 444 | if( ns_initparse( querybuf, size, &nsh ) != 0 ) |
---|
| 445 | return NULL; |
---|
| 446 | |
---|
| 447 | if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 ) |
---|
| 448 | return NULL; |
---|
| 449 | |
---|
| 450 | size = ns_rr_rdlen( rr ); |
---|
| 451 | buf = ns_rr_rdata( rr ); |
---|
| 452 | |
---|
| 453 | len = 0; |
---|
| 454 | for( i = 6; i < size && buf[i]; i += buf[i] + 1 ) |
---|
| 455 | len += buf[i] + 1; |
---|
| 456 | |
---|
| 457 | if( i > size ) |
---|
| 458 | return NULL; |
---|
| 459 | |
---|
| 460 | reply = g_malloc( sizeof( struct ns_srv_reply ) + len ); |
---|
| 461 | memcpy( reply->name, buf + 7, len ); |
---|
| 462 | |
---|
| 463 | for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 ) |
---|
| 464 | reply->name[i] = '.'; |
---|
| 465 | |
---|
| 466 | if( i > len ) |
---|
| 467 | { |
---|
| 468 | g_free( reply ); |
---|
| 469 | return NULL; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | reply->prio = ( buf[0] << 8 ) | buf[1]; |
---|
| 473 | reply->weight = ( buf[2] << 8 ) | buf[3]; |
---|
| 474 | reply->port = ( buf[4] << 8 ) | buf[5]; |
---|
| 475 | #endif |
---|
| 476 | |
---|
| 477 | return reply; |
---|
| 478 | } |
---|
[d444c09] | 479 | |
---|
| 480 | /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ |
---|
| 481 | char *word_wrap( char *msg, int line_len ) |
---|
| 482 | { |
---|
| 483 | GString *ret = g_string_sized_new( strlen( msg ) + 16 ); |
---|
| 484 | |
---|
| 485 | while( strlen( msg ) > line_len ) |
---|
| 486 | { |
---|
| 487 | int i; |
---|
| 488 | |
---|
| 489 | /* First try to find out if there's a newline already. Don't |
---|
| 490 | want to add more splits than necessary. */ |
---|
| 491 | for( i = line_len; i > 0 && msg[i] != '\n'; i -- ); |
---|
| 492 | if( msg[i] == '\n' ) |
---|
| 493 | { |
---|
| 494 | g_string_append_len( ret, msg, i + 1 ); |
---|
| 495 | msg += i + 1; |
---|
| 496 | continue; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | for( i = line_len; i > 0; i -- ) |
---|
| 500 | { |
---|
| 501 | if( msg[i] == '-' ) |
---|
| 502 | { |
---|
| 503 | g_string_append_len( ret, msg, i + 1 ); |
---|
| 504 | g_string_append_c( ret, '\n' ); |
---|
| 505 | msg += i + 1; |
---|
| 506 | break; |
---|
| 507 | } |
---|
| 508 | else if( msg[i] == ' ' ) |
---|
| 509 | { |
---|
| 510 | g_string_append_len( ret, msg, i ); |
---|
| 511 | g_string_append_c( ret, '\n' ); |
---|
| 512 | msg += i + 1; |
---|
| 513 | break; |
---|
| 514 | } |
---|
| 515 | } |
---|
| 516 | if( i == 0 ) |
---|
| 517 | { |
---|
| 518 | g_string_append_len( ret, msg, line_len ); |
---|
| 519 | g_string_append_c( ret, '\n' ); |
---|
| 520 | msg += line_len; |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | g_string_append( ret, msg ); |
---|
| 524 | |
---|
| 525 | return g_string_free( ret, FALSE ); |
---|
| 526 | } |
---|
[d52111a] | 527 | |
---|
| 528 | gboolean ssl_sockerr_again( void *ssl ) |
---|
| 529 | { |
---|
| 530 | if( ssl ) |
---|
| 531 | return ssl_errno == SSL_AGAIN; |
---|
| 532 | else |
---|
| 533 | return sockerr_again(); |
---|
| 534 | } |
---|
[4e8db1c] | 535 | |
---|
| 536 | /* Returns values: -1 == Failure (base64-decoded to something unexpected) |
---|
| 537 | 0 == Okay |
---|
| 538 | 1 == Password doesn't match the hash. */ |
---|
| 539 | int md5_verify_password( char *password, char *hash ) |
---|
| 540 | { |
---|
| 541 | md5_byte_t *pass_dec = NULL; |
---|
| 542 | md5_byte_t pass_md5[16]; |
---|
| 543 | md5_state_t md5_state; |
---|
| 544 | int ret, i; |
---|
| 545 | |
---|
| 546 | if( base64_decode( hash, &pass_dec ) != 21 ) |
---|
| 547 | { |
---|
| 548 | ret = -1; |
---|
| 549 | } |
---|
| 550 | else |
---|
| 551 | { |
---|
| 552 | md5_init( &md5_state ); |
---|
| 553 | md5_append( &md5_state, (md5_byte_t*) password, strlen( password ) ); |
---|
| 554 | md5_append( &md5_state, (md5_byte_t*) pass_dec + 16, 5 ); /* Hmmm, salt! */ |
---|
| 555 | md5_finish( &md5_state, pass_md5 ); |
---|
| 556 | |
---|
| 557 | for( i = 0; i < 16; i ++ ) |
---|
| 558 | { |
---|
| 559 | if( pass_dec[i] != pass_md5[i] ) |
---|
| 560 | { |
---|
| 561 | ret = 1; |
---|
| 562 | break; |
---|
| 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | /* If we reached the end of the loop, it was a match! */ |
---|
| 567 | if( i == 16 ) |
---|
| 568 | ret = 0; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | g_free( pass_dec ); |
---|
| 572 | |
---|
| 573 | return ret; |
---|
| 574 | } |
---|