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