[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 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) |
---|
[0e788f5] | 13 | * Copyright 2002-2012 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; |
---|
[6f10697] | 29 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 30 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 31 | */ |
---|
| 32 | |
---|
| 33 | #define BITLBEE_CORE |
---|
| 34 | #include "nogaim.h" |
---|
[4e8db1c] | 35 | #include "base64.h" |
---|
[c6ca3ee] | 36 | #include "md5.h" |
---|
[b7d3cc34] | 37 | #include <stdio.h> |
---|
| 38 | #include <stdlib.h> |
---|
| 39 | #include <string.h> |
---|
[dd8d4c5] | 40 | #include <ctype.h> |
---|
[b7d3cc34] | 41 | #include <glib.h> |
---|
| 42 | #include <time.h> |
---|
| 43 | |
---|
[36e9f62] | 44 | #ifdef HAVE_RESOLV_A |
---|
| 45 | #include <arpa/nameser.h> |
---|
| 46 | #include <resolv.h> |
---|
| 47 | #endif |
---|
| 48 | |
---|
[4cf80bb] | 49 | #include "md5.h" |
---|
[d52111a] | 50 | #include "ssl_client.h" |
---|
| 51 | |
---|
[b7d3cc34] | 52 | void strip_linefeed(gchar *text) |
---|
| 53 | { |
---|
| 54 | int i, j; |
---|
| 55 | gchar *text2 = g_malloc(strlen(text) + 1); |
---|
| 56 | |
---|
[5ebff60] | 57 | for (i = 0, j = 0; text[i]; i++) { |
---|
| 58 | if (text[i] != '\r') { |
---|
[b7d3cc34] | 59 | text2[j++] = text[i]; |
---|
[5ebff60] | 60 | } |
---|
| 61 | } |
---|
[b7d3cc34] | 62 | text2[j] = '\0'; |
---|
| 63 | |
---|
| 64 | strcpy(text, text2); |
---|
| 65 | g_free(text2); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | time_t get_time(int year, int month, int day, int hour, int min, int sec) |
---|
| 69 | { |
---|
| 70 | struct tm tm; |
---|
| 71 | |
---|
[613cc55] | 72 | memset(&tm, 0, sizeof(struct tm)); |
---|
[b7d3cc34] | 73 | tm.tm_year = year - 1900; |
---|
| 74 | tm.tm_mon = month - 1; |
---|
| 75 | tm.tm_mday = day; |
---|
| 76 | tm.tm_hour = hour; |
---|
| 77 | tm.tm_min = min; |
---|
| 78 | tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; |
---|
[5ebff60] | 79 | |
---|
[b7d3cc34] | 80 | return mktime(&tm); |
---|
| 81 | } |
---|
| 82 | |
---|
[5ebff60] | 83 | time_t mktime_utc(struct tm *tp) |
---|
[2e3a857] | 84 | { |
---|
| 85 | struct tm utc; |
---|
| 86 | time_t res, tres; |
---|
[5ebff60] | 87 | |
---|
[2e3a857] | 88 | tp->tm_isdst = -1; |
---|
[5ebff60] | 89 | res = mktime(tp); |
---|
[2e3a857] | 90 | /* Problem is, mktime() just gave us the GMT timestamp for the |
---|
| 91 | given local time... While the given time WAS NOT local. So |
---|
| 92 | we should fix this now. |
---|
[5ebff60] | 93 | |
---|
[2e3a857] | 94 | Now I could choose between messing with environment variables |
---|
| 95 | (kludgy) or using timegm() (not portable)... Or doing the |
---|
| 96 | following, which I actually prefer... |
---|
[5ebff60] | 97 | |
---|
[2e3a857] | 98 | tzset() may also work but in other places I actually want to |
---|
| 99 | use local time. |
---|
[5ebff60] | 100 | |
---|
[2e3a857] | 101 | FFFFFFFFFFFFFFFFFFFFFUUUUUUUUUUUUUUUUUUUU!! */ |
---|
[5ebff60] | 102 | gmtime_r(&res, &utc); |
---|
[2e3a857] | 103 | utc.tm_isdst = -1; |
---|
[5ebff60] | 104 | if (utc.tm_hour == tp->tm_hour && utc.tm_min == tp->tm_min) { |
---|
[2e3a857] | 105 | /* Sweet! We're in UTC right now... */ |
---|
| 106 | return res; |
---|
[5ebff60] | 107 | } |
---|
| 108 | |
---|
| 109 | tres = mktime(&utc); |
---|
[2e3a857] | 110 | res += res - tres; |
---|
[5ebff60] | 111 | |
---|
[2e3a857] | 112 | /* Yes, this is a hack. And it will go wrong around DST changes. |
---|
| 113 | BUT this is more likely to be threadsafe than messing with |
---|
| 114 | environment variables, and possibly more portable... */ |
---|
[5ebff60] | 115 | |
---|
[2e3a857] | 116 | return res; |
---|
| 117 | } |
---|
| 118 | |
---|
[5ebff60] | 119 | typedef struct htmlentity { |
---|
[51fdc45] | 120 | char code[7]; |
---|
| 121 | char is[3]; |
---|
[b7d3cc34] | 122 | } htmlentity_t; |
---|
| 123 | |
---|
[39cc341] | 124 | static const htmlentity_t ent[] = |
---|
[b7d3cc34] | 125 | { |
---|
[39cc341] | 126 | { "lt", "<" }, |
---|
| 127 | { "gt", ">" }, |
---|
| 128 | { "amp", "&" }, |
---|
[b52e478] | 129 | { "apos", "'" }, |
---|
[39cc341] | 130 | { "quot", "\"" }, |
---|
| 131 | { "aacute", "á" }, |
---|
| 132 | { "eacute", "é" }, |
---|
| 133 | { "iacute", "é" }, |
---|
| 134 | { "oacute", "ó" }, |
---|
| 135 | { "uacute", "ú" }, |
---|
| 136 | { "agrave", "à" }, |
---|
| 137 | { "egrave", "è" }, |
---|
| 138 | { "igrave", "ì" }, |
---|
| 139 | { "ograve", "ò" }, |
---|
| 140 | { "ugrave", "ù" }, |
---|
| 141 | { "acirc", "â" }, |
---|
| 142 | { "ecirc", "ê" }, |
---|
| 143 | { "icirc", "î" }, |
---|
| 144 | { "ocirc", "ô" }, |
---|
| 145 | { "ucirc", "û" }, |
---|
| 146 | { "auml", "ä" }, |
---|
| 147 | { "euml", "ë" }, |
---|
| 148 | { "iuml", "ï" }, |
---|
| 149 | { "ouml", "ö" }, |
---|
| 150 | { "uuml", "ü" }, |
---|
| 151 | { "nbsp", " " }, |
---|
| 152 | { "", "" } |
---|
[b7d3cc34] | 153 | }; |
---|
| 154 | |
---|
[5ebff60] | 155 | void strip_html(char *in) |
---|
[b7d3cc34] | 156 | { |
---|
| 157 | char *start = in; |
---|
[5ebff60] | 158 | char out[strlen(in) + 1]; |
---|
[b7d3cc34] | 159 | char *s = out, *cs; |
---|
| 160 | int i, matched; |
---|
[5a71d9c] | 161 | int taglen; |
---|
[5ebff60] | 162 | |
---|
| 163 | memset(out, 0, sizeof(out)); |
---|
| 164 | |
---|
| 165 | while (*in) { |
---|
| 166 | if (*in == '<' && (g_ascii_isalpha(*(in + 1)) || *(in + 1) == '/')) { |
---|
[b7d3cc34] | 167 | /* If in points at a < and in+1 points at a letter or a slash, this is probably |
---|
| 168 | a HTML-tag. Try to find a closing > and continue there. If the > can't be |
---|
| 169 | found, assume that it wasn't a HTML-tag after all. */ |
---|
[5ebff60] | 170 | |
---|
[b7d3cc34] | 171 | cs = in; |
---|
[5ebff60] | 172 | |
---|
| 173 | while (*in && *in != '>') { |
---|
| 174 | in++; |
---|
| 175 | } |
---|
| 176 | |
---|
[be999a5] | 177 | taglen = in - cs - 1; /* not <0 because the above loop runs at least once */ |
---|
[5ebff60] | 178 | if (*in) { |
---|
| 179 | if (g_strncasecmp(cs + 1, "b", taglen) == 0) { |
---|
[5a71d9c] | 180 | *(s++) = '\x02'; |
---|
[5ebff60] | 181 | } else if (g_strncasecmp(cs + 1, "/b", taglen) == 0) { |
---|
[5a71d9c] | 182 | *(s++) = '\x02'; |
---|
[5ebff60] | 183 | } else if (g_strncasecmp(cs + 1, "i", taglen) == 0) { |
---|
[5a71d9c] | 184 | *(s++) = '\x1f'; |
---|
[5ebff60] | 185 | } else if (g_strncasecmp(cs + 1, "/i", taglen) == 0) { |
---|
[5a71d9c] | 186 | *(s++) = '\x1f'; |
---|
[5ebff60] | 187 | } else if (g_strncasecmp(cs + 1, "br", taglen) == 0) { |
---|
[b7d3cc34] | 188 | *(s++) = '\n'; |
---|
[5ebff60] | 189 | } |
---|
| 190 | in++; |
---|
| 191 | } else { |
---|
[b7d3cc34] | 192 | in = cs; |
---|
| 193 | *(s++) = *(in++); |
---|
| 194 | } |
---|
[5ebff60] | 195 | } else if (*in == '&') { |
---|
[b7d3cc34] | 196 | cs = ++in; |
---|
[5ebff60] | 197 | while (*in && g_ascii_isalpha(*in)) { |
---|
| 198 | in++; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | if (*in == ';') { |
---|
| 202 | in++; |
---|
| 203 | } |
---|
[b7d3cc34] | 204 | matched = 0; |
---|
[5ebff60] | 205 | |
---|
| 206 | for (i = 0; *ent[i].code; i++) { |
---|
| 207 | if (g_strncasecmp(ent[i].code, cs, strlen(ent[i].code)) == 0) { |
---|
[39cc341] | 208 | int j; |
---|
[5ebff60] | 209 | |
---|
| 210 | for (j = 0; ent[i].is[j]; j++) { |
---|
[39cc341] | 211 | *(s++) = ent[i].is[j]; |
---|
[5ebff60] | 212 | } |
---|
| 213 | |
---|
[b7d3cc34] | 214 | matched = 1; |
---|
| 215 | break; |
---|
| 216 | } |
---|
[5ebff60] | 217 | } |
---|
[b7d3cc34] | 218 | |
---|
| 219 | /* None of the entities were matched, so return the string */ |
---|
[5ebff60] | 220 | if (!matched) { |
---|
[b7d3cc34] | 221 | in = cs - 1; |
---|
| 222 | *(s++) = *(in++); |
---|
| 223 | } |
---|
[5ebff60] | 224 | } else { |
---|
[b7d3cc34] | 225 | *(s++) = *(in++); |
---|
| 226 | } |
---|
| 227 | } |
---|
[5ebff60] | 228 | |
---|
| 229 | strcpy(start, out); |
---|
[b7d3cc34] | 230 | } |
---|
| 231 | |
---|
[5ebff60] | 232 | char *escape_html(const char *html) |
---|
[b7d3cc34] | 233 | { |
---|
| 234 | const char *c = html; |
---|
| 235 | GString *ret; |
---|
| 236 | char *str; |
---|
[5ebff60] | 237 | |
---|
| 238 | if (html == NULL) { |
---|
| 239 | return(NULL); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | ret = g_string_new(""); |
---|
| 243 | |
---|
| 244 | while (*c) { |
---|
| 245 | switch (*c) { |
---|
| 246 | case '&': |
---|
| 247 | ret = g_string_append(ret, "&"); |
---|
| 248 | break; |
---|
| 249 | case '<': |
---|
| 250 | ret = g_string_append(ret, "<"); |
---|
| 251 | break; |
---|
| 252 | case '>': |
---|
| 253 | ret = g_string_append(ret, ">"); |
---|
| 254 | break; |
---|
| 255 | case '"': |
---|
| 256 | ret = g_string_append(ret, """); |
---|
| 257 | break; |
---|
| 258 | default: |
---|
| 259 | ret = g_string_append_c(ret, *c); |
---|
[b7d3cc34] | 260 | } |
---|
[5ebff60] | 261 | c++; |
---|
[b7d3cc34] | 262 | } |
---|
[5ebff60] | 263 | |
---|
[b7d3cc34] | 264 | str = ret->str; |
---|
[5ebff60] | 265 | g_string_free(ret, FALSE); |
---|
| 266 | return(str); |
---|
[b7d3cc34] | 267 | } |
---|
| 268 | |
---|
[c88999c] | 269 | /* Decode%20a%20file%20name */ |
---|
[5ebff60] | 270 | void http_decode(char *s) |
---|
[c88999c] | 271 | { |
---|
| 272 | char *t; |
---|
| 273 | int i, j, k; |
---|
[5ebff60] | 274 | |
---|
| 275 | t = g_new(char, strlen(s) + 1); |
---|
| 276 | |
---|
| 277 | for (i = j = 0; s[i]; i++, j++) { |
---|
| 278 | if (s[i] == '%') { |
---|
| 279 | if (sscanf(s + i + 1, "%2x", &k)) { |
---|
[c88999c] | 280 | t[j] = k; |
---|
| 281 | i += 2; |
---|
[5ebff60] | 282 | } else { |
---|
[c88999c] | 283 | *t = 0; |
---|
| 284 | break; |
---|
| 285 | } |
---|
[5ebff60] | 286 | } else { |
---|
[c88999c] | 287 | t[j] = s[i]; |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | t[j] = 0; |
---|
[5ebff60] | 291 | |
---|
| 292 | strcpy(s, t); |
---|
| 293 | g_free(t); |
---|
[c88999c] | 294 | } |
---|
| 295 | |
---|
| 296 | /* Warning: This one explodes the string. Worst-cases can make the string 3x its original size! */ |
---|
| 297 | /* This fuction is safe, but make sure you call it safely as well! */ |
---|
[5ebff60] | 298 | void http_encode(char *s) |
---|
[c88999c] | 299 | { |
---|
[5ebff60] | 300 | char t[strlen(s) + 1]; |
---|
[c88999c] | 301 | int i, j; |
---|
[5ebff60] | 302 | |
---|
| 303 | strcpy(t, s); |
---|
| 304 | for (i = j = 0; t[i]; i++, j++) { |
---|
[6b13103] | 305 | /* Warning: g_ascii_isalnum() is locale-aware, so don't use it here! */ |
---|
[5ebff60] | 306 | if ((t[i] >= 'A' && t[i] <= 'Z') || |
---|
| 307 | (t[i] >= 'a' && t[i] <= 'z') || |
---|
| 308 | (t[i] >= '0' && t[i] <= '9') || |
---|
| 309 | strchr("._-~", t[i])) { |
---|
[b40e60d] | 310 | s[j] = t[i]; |
---|
[5ebff60] | 311 | } else { |
---|
| 312 | sprintf(s + j, "%%%02X", ((unsigned char *) t)[i]); |
---|
[b40e60d] | 313 | j += 2; |
---|
[c88999c] | 314 | } |
---|
| 315 | } |
---|
| 316 | s[j] = 0; |
---|
| 317 | } |
---|
| 318 | |
---|
[5ebff60] | 319 | /* Strip newlines from a string. Modifies the string passed to it. */ |
---|
| 320 | char *strip_newlines(char *source) |
---|
[c88999c] | 321 | { |
---|
[5ebff60] | 322 | int i; |
---|
[c88999c] | 323 | |
---|
[5ebff60] | 324 | for (i = 0; source[i] != '\0'; i++) { |
---|
| 325 | if (source[i] == '\n' || source[i] == '\r') { |
---|
[c88999c] | 326 | source[i] = ' '; |
---|
[5ebff60] | 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
[c88999c] | 330 | return source; |
---|
| 331 | } |
---|
[2a6ca4f] | 332 | |
---|
| 333 | /* Wrap an IPv4 address into IPv6 space. Not thread-safe... */ |
---|
[5ebff60] | 334 | char *ipv6_wrap(char *src) |
---|
[2a6ca4f] | 335 | { |
---|
| 336 | static char dst[64]; |
---|
| 337 | int i; |
---|
[5ebff60] | 338 | |
---|
| 339 | for (i = 0; src[i]; i++) { |
---|
| 340 | if ((src[i] < '0' || src[i] > '9') && src[i] != '.') { |
---|
[2a6ca4f] | 341 | break; |
---|
[5ebff60] | 342 | } |
---|
| 343 | } |
---|
| 344 | |
---|
[2a6ca4f] | 345 | /* Hmm, it's not even an IP... */ |
---|
[5ebff60] | 346 | if (src[i]) { |
---|
[2a6ca4f] | 347 | return src; |
---|
[5ebff60] | 348 | } |
---|
| 349 | |
---|
| 350 | g_snprintf(dst, sizeof(dst), "::ffff:%s", src); |
---|
| 351 | |
---|
[2a6ca4f] | 352 | return dst; |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | /* Unwrap an IPv4 address into IPv6 space. Thread-safe, because it's very simple. :-) */ |
---|
[5ebff60] | 356 | char *ipv6_unwrap(char *src) |
---|
[2a6ca4f] | 357 | { |
---|
| 358 | int i; |
---|
[5ebff60] | 359 | |
---|
| 360 | if (g_strncasecmp(src, "::ffff:", 7) != 0) { |
---|
[2a6ca4f] | 361 | return src; |
---|
[5ebff60] | 362 | } |
---|
| 363 | |
---|
| 364 | for (i = 7; src[i]; i++) { |
---|
| 365 | if ((src[i] < '0' || src[i] > '9') && src[i] != '.') { |
---|
[2a6ca4f] | 366 | break; |
---|
[5ebff60] | 367 | } |
---|
| 368 | } |
---|
| 369 | |
---|
[2a6ca4f] | 370 | /* Hmm, it's not even an IP... */ |
---|
[5ebff60] | 371 | if (src[i]) { |
---|
[2a6ca4f] | 372 | return src; |
---|
[5ebff60] | 373 | } |
---|
| 374 | |
---|
| 375 | return (src + 7); |
---|
[2a6ca4f] | 376 | } |
---|
[e27661d] | 377 | |
---|
| 378 | /* Convert from one charset to another. |
---|
[5ebff60] | 379 | |
---|
[e27661d] | 380 | from_cs, to_cs: Source and destination charsets |
---|
| 381 | src, dst: Source and destination strings |
---|
| 382 | size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. |
---|
| 383 | maxbuf: Maximum number of bytes to write to dst |
---|
[5ebff60] | 384 | |
---|
[e27661d] | 385 | Returns the number of bytes written to maxbuf or -1 on an error. |
---|
| 386 | */ |
---|
[5ebff60] | 387 | signed int do_iconv(char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf) |
---|
[e27661d] | 388 | { |
---|
[574af7e] | 389 | GIConv cd; |
---|
[e27661d] | 390 | size_t res; |
---|
| 391 | size_t inbytesleft, outbytesleft; |
---|
| 392 | char *inbuf = src; |
---|
| 393 | char *outbuf = dst; |
---|
[5ebff60] | 394 | |
---|
| 395 | cd = g_iconv_open(to_cs, from_cs); |
---|
| 396 | if (cd == (GIConv) - 1) { |
---|
[f5da476] | 397 | return -1; |
---|
[5ebff60] | 398 | } |
---|
| 399 | |
---|
| 400 | inbytesleft = size ? size : strlen(src); |
---|
[e27661d] | 401 | outbytesleft = maxbuf - 1; |
---|
[5ebff60] | 402 | res = g_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
---|
[e27661d] | 403 | *outbuf = '\0'; |
---|
[5ebff60] | 404 | g_iconv_close(cd); |
---|
| 405 | |
---|
| 406 | if (res != 0) { |
---|
[f5da476] | 407 | return -1; |
---|
[5ebff60] | 408 | } else { |
---|
[f5da476] | 409 | return outbuf - dst; |
---|
[5ebff60] | 410 | } |
---|
[e27661d] | 411 | } |
---|
| 412 | |
---|
[5eab298f] | 413 | /* A wrapper for /dev/urandom. |
---|
| 414 | * If /dev/urandom is not present or not usable, it calls abort() |
---|
| 415 | * to prevent bitlbee from working without a decent entropy source */ |
---|
[5ebff60] | 416 | void random_bytes(unsigned char *buf, int count) |
---|
[7f49a86] | 417 | { |
---|
[5eab298f] | 418 | int fd; |
---|
[5ebff60] | 419 | |
---|
| 420 | if (((fd = open("/dev/urandom", O_RDONLY)) == -1) || |
---|
| 421 | (read(fd, buf, count) == -1)) { |
---|
| 422 | log_message(LOGLVL_ERROR, "/dev/urandom not present - aborting"); |
---|
[5eab298f] | 423 | abort(); |
---|
[7f49a86] | 424 | } |
---|
[5eab298f] | 425 | |
---|
[5ebff60] | 426 | close(fd); |
---|
[7f49a86] | 427 | } |
---|
| 428 | |
---|
[578e5b0] | 429 | int is_bool(const char *value) |
---|
[5100caa] | 430 | { |
---|
[5ebff60] | 431 | if (*value == 0) { |
---|
[5100caa] | 432 | return 0; |
---|
[5ebff60] | 433 | } |
---|
| 434 | |
---|
| 435 | if ((g_strcasecmp(value, |
---|
| 436 | "true") == 0) || (g_strcasecmp(value, "yes") == 0) || (g_strcasecmp(value, "on") == 0)) { |
---|
[5100caa] | 437 | return 1; |
---|
[5ebff60] | 438 | } |
---|
| 439 | if ((g_strcasecmp(value, |
---|
| 440 | "false") == 0) || (g_strcasecmp(value, "no") == 0) || (g_strcasecmp(value, "off") == 0)) { |
---|
[5100caa] | 441 | return 1; |
---|
[5ebff60] | 442 | } |
---|
| 443 | |
---|
| 444 | while (*value) { |
---|
| 445 | if (!g_ascii_isdigit(*value)) { |
---|
[5100caa] | 446 | return 0; |
---|
[5ebff60] | 447 | } else { |
---|
| 448 | value++; |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
[5100caa] | 452 | return 1; |
---|
| 453 | } |
---|
| 454 | |
---|
[578e5b0] | 455 | int bool2int(const char *value) |
---|
[5100caa] | 456 | { |
---|
| 457 | int i; |
---|
[5ebff60] | 458 | |
---|
| 459 | if ((g_strcasecmp(value, |
---|
| 460 | "true") == 0) || (g_strcasecmp(value, "yes") == 0) || (g_strcasecmp(value, "on") == 0)) { |
---|
[5100caa] | 461 | return 1; |
---|
[5ebff60] | 462 | } |
---|
| 463 | if ((g_strcasecmp(value, |
---|
| 464 | "false") == 0) || (g_strcasecmp(value, "no") == 0) || (g_strcasecmp(value, "off") == 0)) { |
---|
[5100caa] | 465 | return 0; |
---|
[5ebff60] | 466 | } |
---|
| 467 | |
---|
| 468 | if (sscanf(value, "%d", &i) == 1) { |
---|
[5100caa] | 469 | return i; |
---|
[5ebff60] | 470 | } |
---|
| 471 | |
---|
[5100caa] | 472 | return 0; |
---|
| 473 | } |
---|
[36e9f62] | 474 | |
---|
[5ebff60] | 475 | struct ns_srv_reply **srv_lookup(char *service, char *protocol, char *domain) |
---|
| 476 | { |
---|
[ffdf2e7] | 477 | struct ns_srv_reply **replies = NULL; |
---|
[5ebff60] | 478 | |
---|
[36e9f62] | 479 | #ifdef HAVE_RESOLV_A |
---|
[ffdf2e7] | 480 | struct ns_srv_reply *reply = NULL; |
---|
[36e9f62] | 481 | char name[1024]; |
---|
| 482 | unsigned char querybuf[1024]; |
---|
| 483 | const unsigned char *buf; |
---|
| 484 | ns_msg nsh; |
---|
| 485 | ns_rr rr; |
---|
[632627e] | 486 | int n, len, size; |
---|
[5ebff60] | 487 | |
---|
| 488 | g_snprintf(name, sizeof(name), "_%s._%s.%s", service, protocol, domain); |
---|
| 489 | |
---|
| 490 | if ((size = res_query(name, ns_c_in, ns_t_srv, querybuf, sizeof(querybuf))) <= 0) { |
---|
[36e9f62] | 491 | return NULL; |
---|
[5ebff60] | 492 | } |
---|
| 493 | |
---|
| 494 | if (ns_initparse(querybuf, size, &nsh) != 0) { |
---|
[36e9f62] | 495 | return NULL; |
---|
[5ebff60] | 496 | } |
---|
| 497 | |
---|
[ffdf2e7] | 498 | n = 0; |
---|
[5ebff60] | 499 | while (ns_parserr(&nsh, ns_s_an, n, &rr) == 0) { |
---|
[632627e] | 500 | char name[NS_MAXDNAME]; |
---|
| 501 | |
---|
[5ebff60] | 502 | if (ns_rr_rdlen(rr) < 7) { |
---|
| 503 | break; |
---|
| 504 | } |
---|
[632627e] | 505 | |
---|
[5ebff60] | 506 | buf = ns_rr_rdata(rr); |
---|
| 507 | |
---|
| 508 | if (dn_expand(querybuf, querybuf + size, &buf[6], name, NS_MAXDNAME) == -1) { |
---|
[ffdf2e7] | 509 | break; |
---|
[5ebff60] | 510 | } |
---|
[632627e] | 511 | |
---|
| 512 | len = strlen(name) + 1; |
---|
[5ebff60] | 513 | |
---|
| 514 | reply = g_malloc(sizeof(struct ns_srv_reply) + len); |
---|
| 515 | memcpy(reply->name, name, len); |
---|
| 516 | |
---|
| 517 | reply->prio = (buf[0] << 8) | buf[1]; |
---|
| 518 | reply->weight = (buf[2] << 8) | buf[3]; |
---|
| 519 | reply->port = (buf[4] << 8) | buf[5]; |
---|
| 520 | |
---|
| 521 | n++; |
---|
| 522 | replies = g_renew(struct ns_srv_reply *, replies, n + 1); |
---|
| 523 | replies[n - 1] = reply; |
---|
| 524 | } |
---|
| 525 | if (replies) { |
---|
[ffdf2e7] | 526 | replies[n] = NULL; |
---|
[5ebff60] | 527 | } |
---|
[36e9f62] | 528 | #endif |
---|
[5ebff60] | 529 | |
---|
[ffdf2e7] | 530 | return replies; |
---|
| 531 | } |
---|
| 532 | |
---|
[5ebff60] | 533 | void srv_free(struct ns_srv_reply **srv) |
---|
[ffdf2e7] | 534 | { |
---|
| 535 | int i; |
---|
[5ebff60] | 536 | |
---|
| 537 | if (srv == NULL) { |
---|
[ffdf2e7] | 538 | return; |
---|
[5ebff60] | 539 | } |
---|
| 540 | |
---|
| 541 | for (i = 0; srv[i]; i++) { |
---|
| 542 | g_free(srv[i]); |
---|
| 543 | } |
---|
| 544 | g_free(srv); |
---|
[36e9f62] | 545 | } |
---|
[d444c09] | 546 | |
---|
| 547 | /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ |
---|
[5ebff60] | 548 | char *word_wrap(const char *msg, int line_len) |
---|
[d444c09] | 549 | { |
---|
[5ebff60] | 550 | GString *ret = g_string_sized_new(strlen(msg) + 16); |
---|
| 551 | |
---|
| 552 | while (strlen(msg) > line_len) { |
---|
[d444c09] | 553 | int i; |
---|
[5ebff60] | 554 | |
---|
[d444c09] | 555 | /* First try to find out if there's a newline already. Don't |
---|
| 556 | want to add more splits than necessary. */ |
---|
[5ebff60] | 557 | for (i = line_len; i > 0 && msg[i] != '\n'; i--) { |
---|
| 558 | ; |
---|
| 559 | } |
---|
| 560 | if (msg[i] == '\n') { |
---|
| 561 | g_string_append_len(ret, msg, i + 1); |
---|
[d444c09] | 562 | msg += i + 1; |
---|
| 563 | continue; |
---|
| 564 | } |
---|
[5ebff60] | 565 | |
---|
| 566 | for (i = line_len; i > 0; i--) { |
---|
| 567 | if (msg[i] == '-') { |
---|
| 568 | g_string_append_len(ret, msg, i + 1); |
---|
| 569 | g_string_append_c(ret, '\n'); |
---|
[d444c09] | 570 | msg += i + 1; |
---|
| 571 | break; |
---|
[5ebff60] | 572 | } else if (msg[i] == ' ') { |
---|
| 573 | g_string_append_len(ret, msg, i); |
---|
| 574 | g_string_append_c(ret, '\n'); |
---|
[d444c09] | 575 | msg += i + 1; |
---|
| 576 | break; |
---|
| 577 | } |
---|
| 578 | } |
---|
[5ebff60] | 579 | if (i == 0) { |
---|
| 580 | g_string_append_len(ret, msg, line_len); |
---|
| 581 | g_string_append_c(ret, '\n'); |
---|
[d444c09] | 582 | msg += line_len; |
---|
| 583 | } |
---|
| 584 | } |
---|
[5ebff60] | 585 | g_string_append(ret, msg); |
---|
| 586 | |
---|
| 587 | return g_string_free(ret, FALSE); |
---|
[d444c09] | 588 | } |
---|
[d52111a] | 589 | |
---|
[5ebff60] | 590 | gboolean ssl_sockerr_again(void *ssl) |
---|
[d52111a] | 591 | { |
---|
[5ebff60] | 592 | if (ssl) { |
---|
[d52111a] | 593 | return ssl_errno == SSL_AGAIN; |
---|
[5ebff60] | 594 | } else { |
---|
[d52111a] | 595 | return sockerr_again(); |
---|
[5ebff60] | 596 | } |
---|
[d52111a] | 597 | } |
---|
[4e8db1c] | 598 | |
---|
| 599 | /* Returns values: -1 == Failure (base64-decoded to something unexpected) |
---|
| 600 | 0 == Okay |
---|
| 601 | 1 == Password doesn't match the hash. */ |
---|
[5ebff60] | 602 | int md5_verify_password(char *password, char *hash) |
---|
[4e8db1c] | 603 | { |
---|
| 604 | md5_byte_t *pass_dec = NULL; |
---|
| 605 | md5_byte_t pass_md5[16]; |
---|
| 606 | md5_state_t md5_state; |
---|
[6a78c0e] | 607 | int ret = -1, i; |
---|
[5ebff60] | 608 | |
---|
| 609 | if (base64_decode(hash, &pass_dec) == 21) { |
---|
| 610 | md5_init(&md5_state); |
---|
| 611 | md5_append(&md5_state, (md5_byte_t *) password, strlen(password)); |
---|
| 612 | md5_append(&md5_state, (md5_byte_t *) pass_dec + 16, 5); /* Hmmm, salt! */ |
---|
| 613 | md5_finish(&md5_state, pass_md5); |
---|
| 614 | |
---|
| 615 | for (i = 0; i < 16; i++) { |
---|
| 616 | if (pass_dec[i] != pass_md5[i]) { |
---|
[4e8db1c] | 617 | ret = 1; |
---|
| 618 | break; |
---|
| 619 | } |
---|
| 620 | } |
---|
[5ebff60] | 621 | |
---|
[4e8db1c] | 622 | /* If we reached the end of the loop, it was a match! */ |
---|
[5ebff60] | 623 | if (i == 16) { |
---|
[4e8db1c] | 624 | ret = 0; |
---|
[5ebff60] | 625 | } |
---|
[4e8db1c] | 626 | } |
---|
[5ebff60] | 627 | |
---|
| 628 | g_free(pass_dec); |
---|
[4e8db1c] | 629 | |
---|
| 630 | return ret; |
---|
| 631 | } |
---|
[24b8bbb] | 632 | |
---|
[7b87539] | 633 | /* Split commands (root-style, *not* IRC-style). Handles "quoting of" |
---|
| 634 | white\ space in 'various ways'. Returns a NULL-terminated static |
---|
| 635 | char** so watch out with nested use! Definitely not thread-safe. */ |
---|
[5ebff60] | 636 | char **split_command_parts(char *command, int limit) |
---|
[24b8bbb] | 637 | { |
---|
[5ebff60] | 638 | static char *cmd[IRC_MAX_ARGS + 1]; |
---|
[24b8bbb] | 639 | char *s, q = 0; |
---|
| 640 | int k; |
---|
[5ebff60] | 641 | |
---|
| 642 | memset(cmd, 0, sizeof(cmd)); |
---|
[24b8bbb] | 643 | cmd[0] = command; |
---|
| 644 | k = 1; |
---|
[5ebff60] | 645 | for (s = command; *s && k < IRC_MAX_ARGS; s++) { |
---|
| 646 | if (*s == ' ' && !q) { |
---|
[24b8bbb] | 647 | *s = 0; |
---|
[5ebff60] | 648 | while (*++s == ' ') { |
---|
| 649 | ; |
---|
| 650 | } |
---|
| 651 | if (k != limit && (*s == '"' || *s == '\'')) { |
---|
[24b8bbb] | 652 | q = *s; |
---|
[5ebff60] | 653 | s++; |
---|
[24b8bbb] | 654 | } |
---|
[5ebff60] | 655 | if (*s) { |
---|
[24b8bbb] | 656 | cmd[k++] = s; |
---|
[269580c] | 657 | if (limit && k > limit) { |
---|
| 658 | break; |
---|
| 659 | } |
---|
[5ebff60] | 660 | s--; |
---|
| 661 | } else { |
---|
[24b8bbb] | 662 | break; |
---|
| 663 | } |
---|
[5ebff60] | 664 | } else if (*s == '\\' && ((!q && s[1]) || (q && q == s[1]))) { |
---|
[24b8bbb] | 665 | char *cpy; |
---|
[5ebff60] | 666 | |
---|
| 667 | for (cpy = s; *cpy; cpy++) { |
---|
[24b8bbb] | 668 | cpy[0] = cpy[1]; |
---|
[5ebff60] | 669 | } |
---|
| 670 | } else if (*s == q) { |
---|
[24b8bbb] | 671 | q = *s = 0; |
---|
| 672 | } |
---|
[269580c] | 673 | } |
---|
[5ebff60] | 674 | |
---|
[89c11e7] | 675 | /* Full zero-padding for easier argc checking. */ |
---|
[5ebff60] | 676 | while (k <= IRC_MAX_ARGS) { |
---|
[89c11e7] | 677 | cmd[k++] = NULL; |
---|
[5ebff60] | 678 | } |
---|
| 679 | |
---|
[24b8bbb] | 680 | return cmd; |
---|
| 681 | } |
---|
[9b0ad7e] | 682 | |
---|
[5ebff60] | 683 | char *get_rfc822_header(const char *text, const char *header, int len) |
---|
[9b0ad7e] | 684 | { |
---|
[5ebff60] | 685 | int hlen = strlen(header), i; |
---|
[55ccc9a0] | 686 | const char *ret; |
---|
[5ebff60] | 687 | |
---|
| 688 | if (text == NULL) { |
---|
[f9789d4] | 689 | return NULL; |
---|
[5ebff60] | 690 | } |
---|
| 691 | |
---|
| 692 | if (len == 0) { |
---|
| 693 | len = strlen(text); |
---|
| 694 | } |
---|
| 695 | |
---|
[9b0ad7e] | 696 | i = 0; |
---|
[5ebff60] | 697 | while ((i + hlen) < len) { |
---|
[9b0ad7e] | 698 | /* Maybe this is a bit over-commented, but I just hate this part... */ |
---|
[5ebff60] | 699 | if (g_strncasecmp(text + i, header, hlen) == 0) { |
---|
[9b0ad7e] | 700 | /* Skip to the (probable) end of the header */ |
---|
| 701 | i += hlen; |
---|
[5ebff60] | 702 | |
---|
[9b0ad7e] | 703 | /* Find the first non-[: \t] character */ |
---|
[5ebff60] | 704 | while (i < len && (text[i] == ':' || text[i] == ' ' || text[i] == '\t')) { |
---|
| 705 | i++; |
---|
| 706 | } |
---|
| 707 | |
---|
[9b0ad7e] | 708 | /* Make sure we're still inside the string */ |
---|
[5ebff60] | 709 | if (i >= len) { |
---|
| 710 | return(NULL); |
---|
| 711 | } |
---|
| 712 | |
---|
[9b0ad7e] | 713 | /* Save the position */ |
---|
| 714 | ret = text + i; |
---|
[5ebff60] | 715 | |
---|
[9b0ad7e] | 716 | /* Search for the end of this line */ |
---|
[5ebff60] | 717 | while (i < len && text[i] != '\r' && text[i] != '\n') { |
---|
| 718 | i++; |
---|
| 719 | } |
---|
| 720 | |
---|
[9b0ad7e] | 721 | /* Copy the found data */ |
---|
[5ebff60] | 722 | return(g_strndup(ret, text + i - ret)); |
---|
[9b0ad7e] | 723 | } |
---|
[5ebff60] | 724 | |
---|
[9b0ad7e] | 725 | /* This wasn't the header we were looking for, skip to the next line. */ |
---|
[5ebff60] | 726 | while (i < len && (text[i] != '\r' && text[i] != '\n')) { |
---|
| 727 | i++; |
---|
| 728 | } |
---|
| 729 | while (i < len && (text[i] == '\r' || text[i] == '\n')) { |
---|
| 730 | i++; |
---|
| 731 | } |
---|
| 732 | |
---|
[9b0ad7e] | 733 | /* End of headers? */ |
---|
[5ebff60] | 734 | if ((i >= 4 && strncmp(text + i - 4, "\r\n\r\n", 4) == 0) || |
---|
| 735 | (i >= 2 && (strncmp(text + i - 2, "\n\n", 2) == 0 || |
---|
| 736 | strncmp(text + i - 2, "\r\r", 2) == 0))) { |
---|
[9b0ad7e] | 737 | break; |
---|
| 738 | } |
---|
| 739 | } |
---|
[5ebff60] | 740 | |
---|
[f9789d4] | 741 | return NULL; |
---|
[9b0ad7e] | 742 | } |
---|
[fed4f76] | 743 | |
---|
| 744 | /* Takes a string, truncates it where it's safe, returns the new length */ |
---|
[5ebff60] | 745 | int truncate_utf8(char *string, int maxlen) |
---|
[fed4f76] | 746 | { |
---|
| 747 | char *end; |
---|
[5ebff60] | 748 | |
---|
| 749 | g_utf8_validate((const gchar *) string, maxlen, (const gchar **) &end); |
---|
[fed4f76] | 750 | *end = '\0'; |
---|
| 751 | return end - string; |
---|
| 752 | } |
---|