util.c 14.7 KB
Newer Older
1 2 3 4
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
5
#include <sys/types.h>
6 7
#include <stdio.h>
#include <string.h>
8
#include <stdlib.h>
Jack Moffitt's avatar
Jack Moffitt committed
9 10

#ifndef _WIN32
11 12
#include <sys/time.h>
#include <sys/socket.h>
Jack Moffitt's avatar
Jack Moffitt committed
13
#include <unistd.h>
14 15 16
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
17
#else
18
#include <winsock2.h>
19
#include <windows.h>
20 21 22 23
#include <stdio.h>
#define snprintf _snprintf
#define strcasecmp stricmp
#define strncasecmp strnicmp
Jack Moffitt's avatar
Jack Moffitt committed
24 25
#endif

Karl Heyes's avatar
Karl Heyes committed
26
#include "net/sock.h"
Jack Moffitt's avatar
Jack Moffitt committed
27

28
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
29
#include "util.h"
30
#include "os.h"
31 32 33 34 35 36 37
#include "refbuf.h"
#include "connection.h"
#include "client.h"

#define CATMODULE "util"

#include "logging.h"
Jack Moffitt's avatar
Jack Moffitt committed
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* Abstract out an interface to use either poll or select depending on which
 * is available (poll is preferred) to watch a single fd.
 *
 * timeout is in milliseconds.
 *
 * returns > 0 if activity on the fd occurs before the timeout.
 *           0 if no activity occurs
 *         < 0 for error.
 */
int util_timed_wait_for_fd(int fd, int timeout)
{
#ifdef HAVE_POLL
    struct pollfd ufds;

    ufds.fd = fd;
    ufds.events = POLLIN;
    ufds.revents = 0;

    return poll(&ufds, 1, timeout);
#else
    fd_set rfds;
60
    struct timeval tv, *p=NULL;
61 62 63 64

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

65 66 67 68 69 70
    if(timeout >= 0) {
        tv.tv_sec = timeout/1000;
        tv.tv_usec = (timeout % 1000)*1000;
        p = &tv;
    }
    return select(fd+1, &rfds, NULL, NULL, p);
71 72 73
#endif
}

Jack Moffitt's avatar
Jack Moffitt committed
74 75
int util_read_header(int sock, char *buff, unsigned long len)
{
76 77 78 79
    int read_bytes, ret;
    unsigned long pos;
    char c;
    ice_config_t *config;
Michael Smith's avatar
Michael Smith committed
80
    int header_timeout;
Jack Moffitt's avatar
Jack Moffitt committed
81

82
    config = config_get_config();
Michael Smith's avatar
Michael Smith committed
83 84
    header_timeout = config->header_timeout;
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
85

86 87 88
    read_bytes = 1;
    pos = 0;
    ret = 0;
Jack Moffitt's avatar
Jack Moffitt committed
89

90 91
    while ((read_bytes == 1) && (pos < (len - 1))) {
        read_bytes = 0;
Jack Moffitt's avatar
Jack Moffitt committed
92

Michael Smith's avatar
Michael Smith committed
93
        if (util_timed_wait_for_fd(sock, header_timeout*1000) > 0) {
Jack Moffitt's avatar
Jack Moffitt committed
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
            if ((read_bytes = recv(sock, &c, 1, 0))) {
                if (c != '\r') buff[pos++] = c;
                if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {
                    ret = 1;
                    break;
                }
            }
        } else {
            break;
        }
    }

    if (ret) buff[pos] = '\0';
    
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
110 111
}

112 113 114 115 116 117 118 119 120
char *util_get_extension(char *path) {
    char *ext = strrchr(path, '.');

    if(ext == NULL)
        return "";
    else
        return ext+1;
}

121
int util_check_valid_extension(char *uri) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    int    ret = 0;
    char    *p2;

    if (uri) {
        p2 = strrchr(uri, '.');
        if (p2) {
            p2++;
            if (strncmp(p2, "xsl", strlen("xsl")) == 0) {
                /* Build the full path for the request, concatenating the webroot from the config.
                ** Here would be also a good time to prevent accesses like '../../../../etc/passwd' or somesuch.
                */
                ret = XSLT_CONTENT;
            }
            if (strncmp(p2, "htm", strlen("htm")) == 0) {
                /* Build the full path for the request, concatenating the webroot from the config.
                ** Here would be also a good time to prevent accesses like '../../../../etc/passwd' or somesuch.
                */
                ret = HTML_CONTENT;
            }
            if (strncmp(p2, "html", strlen("html")) == 0) {
                /* Build the full path for the request, concatenating the webroot from the config.
                ** Here would be also a good time to prevent accesses like '../../../../etc/passwd' or somesuch.
                */
                ret = HTML_CONTENT;
            }

        }
    }
    return ret;
151
}
Jack Moffitt's avatar
Jack Moffitt committed
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static int hex(char c)
{
    if(c >= '0' && c <= '9')
        return c - '0';
    else if(c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    else if(c >= 'a' && c <= 'f')
        return c - 'a' + 10;
    else
        return -1;
}

static int verify_path(char *path) {
    int dir = 0, indotseq = 0;

    while(*path) {
        if(*path == '/' || *path == '\\') {
            if(indotseq)
                return 0;
            if(dir)
                return 0;
            dir = 1;
            path++;
            continue;
        }

        if(dir || indotseq) {
            if(*path == '.')
                indotseq = 1;
            else
                indotseq = 0;
        }
        
        dir = 0;
        path++;
    }

    return 1;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
char *util_get_path_from_uri(char *uri) {
    char *path = util_normalise_uri(uri);
    char *fullpath;

    if(!path)
        return NULL;
    else {
        fullpath = util_get_path_from_normalised_uri(path);
        free(path);
        return fullpath;
    }
}

char *util_get_path_from_normalised_uri(char *uri) {
    char *fullpath;
Michael Smith's avatar
Michael Smith committed
208 209
    char *webroot;
    ice_config_t *config = config_get_config();
210

Michael Smith's avatar
Michael Smith committed
211 212 213 214 215
    webroot = config->webroot_dir;
    config_release_config();

    fullpath = malloc(strlen(uri) + strlen(webroot) + 1);
    strcpy(fullpath, webroot);
216 217 218 219 220 221

    strcat(fullpath, uri);

    return fullpath;
}

222
static char hexchars[16] = {
Michael Smith's avatar
Michael Smith committed
223
    '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
};

static char safechars[256] = {
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0,
      0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
      1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,
      0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
      1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
};

245
char *util_url_escape(char *src)
246 247 248 249 250 251 252 253 254 255 256 257 258
{
    int len = strlen(src);
    /* Efficiency not a big concern here, keep the code simple/conservative */
    char *dst = calloc(1, len*3 + 1); 
    unsigned char *source = src;
    int i,j=0;

    for(i=0; i < len; i++) {
        if(safechars[source[i]]) {
            dst[j++] = source[i];
        }
        else {
            dst[j] = '%';
259 260
            dst[j+1] = hexchars[ (source[i] >> 4) & 0xf ];
            dst[j+2] = hexchars[ source[i] & 0xf ];
261 262 263 264 265 266 267 268 269
            j+= 3;
        }
    }

    dst[j] = 0;
    return dst;
}

char *util_url_unescape(char *src)
270 271 272
{
    int len = strlen(src);
    unsigned char *decoded;
273
    int i;
274
    char *dst;
275 276
    int done = 0;

277
    decoded = calloc(1, len + 1);
278

279
    dst = decoded;
280

281 282
    for(i=0; i < len; i++) {
        switch(src[i]) {
283
            case '%':
284 285
                if(i+2 >= len) {
                    free(decoded);
286 287
                    return NULL;
                }
288 289
                if(hex(src[i+1]) == -1 || hex(src[i+2]) == -1 ) {
                    free(decoded);
290 291 292
                    return NULL;
                }

293
                *dst++ = hex(src[i+1]) * 16  + hex(src[i+2]);
294 295 296 297 298 299
                i+= 2;
                break;
            case '#':
                done = 1;
                break;
            case 0:
300
                ERROR0("Fatal internal logic error in util_url_unescape()");
301
                free(decoded);
302 303 304
                return NULL;
                break;
            default:
305
                *dst++ = src[i];
306 307 308 309 310 311 312 313
                break;
        }
        if(done)
            break;
    }

    *dst = 0; /* null terminator */

314 315 316 317 318 319 320 321 322 323 324 325 326 327
    return decoded;
}

/* Get an absolute path (from the webroot dir) from a URI. Return NULL if the
 * path contains 'disallowed' sequences like foo/../ (which could be used to
 * escape from the webroot) or if it cannot be URI-decoded.
 * Caller should free the path.
 */
char *util_normalise_uri(char *uri) {
    char *path;

    if(uri[0] != '/')
        return NULL;

328
    path = util_url_unescape(uri);
329 330 331 332 333 334

    if(path == NULL) {
        WARN1("Error decoding URI: %s\n", uri);
        return NULL;
    }

335 336
    /* We now have a full URI-decoded path. Check it for allowability */
    if(verify_path(path))
337
        return path;
338 339 340 341 342 343
    else {
        WARN1("Rejecting invalid path \"%s\"", path);
        free(path);
        return NULL;
    }
}
Jack Moffitt's avatar
Jack Moffitt committed
344

345 346 347 348 349 350 351
static char base64table[64] = {
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
static signed char base64decode[256] = {
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -1, -2, -2,
     -2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
     -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};

Michael Smith's avatar
Michael Smith committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
char *util_bin_to_hex(unsigned char *data, int len)
{
    char *hex = malloc(len*2 + 1);
    int i;

    for(i = 0; i < len; i++) {
        hex[i*2] = hexchars[(data[i]&0xf0) >> 4];
        hex[i*2+1] = hexchars[data[i]&0x0f];
    }

    hex[len*2] = 0;

    return hex;
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/* This isn't efficient, but it doesn't need to be */
char *util_base64_encode(char *data)
{
    int len = strlen(data);
    char *out = malloc(len*4/3 + 4);
    char *result = out;
    int chunk;

    while(len > 0) {
        chunk = (len >3)?3:len;
        *out++ = base64table[(*data & 0xFC)>>2];
        *out++ = base64table[((*data & 0x03)<<4) | ((*(data+1) & 0xF0) >> 4)];
        switch(chunk) {
            case 3:
                *out++ = base64table[((*(data+1) & 0x0F)<<2) | ((*(data+2) & 0xC0)>>6)];
                *out++ = base64table[(*(data+2)) & 0x3F];
                break;
            case 2:
                *out++ = base64table[((*(data+1) & 0x0F)<<2)];
                *out++ = '=';
                break;
            case 1:
                *out++ = '=';
                *out++ = '=';
                break;
        }
        data += chunk;
        len -= chunk;
    }
415
    *out = 0;
416 417 418 419

    return result;
}

420
char *util_base64_decode(unsigned char *input)
421 422 423 424 425 426 427 428 429 430 431 432 433
{
    int len = strlen(input);
    char *out = malloc(len*3/4 + 5);
    char *result = out;
    signed char vals[4];

    while(len > 0) {
        if(len < 4)
        {
            free(result);
            return NULL; /* Invalid Base64 data */
        }

434 435 436 437
        vals[0] = base64decode[*input++];
        vals[1] = base64decode[*input++];
        vals[2] = base64decode[*input++];
        vals[3] = base64decode[*input++];
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

        if(vals[0] < 0 || vals[1] < 0 || vals[2] < -1 || vals[3] < -1) {
            continue;
        }

        *out++ = vals[0]<<2 | vals[1]>>4;
        if(vals[2] >= 0)
            *out++ = ((vals[1]&0x0F)<<4) | (vals[2]>>2);
        else
            *out++ = 0;

        if(vals[3] >= 0)
            *out++ = ((vals[2]&0x03)<<6) | (vals[3]);
        else
            *out++ = 0;

        len -= 4;
    }
    *out = 0;

    return result;
}

461 462
util_dict *util_dict_new(void)
{
463
    return (util_dict *)calloc(1, sizeof(util_dict));
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
}

void util_dict_free(util_dict *dict)
{
    util_dict *next;

    while (dict) {
        next = dict->next;

        if (dict->key)
            free (dict->key);
        if (dict->val)
            free (dict->val);
        free (dict);

        dict = next;
    }
}

const char *util_dict_get(util_dict *dict, const char *key)
{
485 486 487 488 489
    while (dict) {
        if (!strcmp(key, dict->key))
            return dict->val;
        dict = dict->next;
    }
Michael Smith's avatar
Michael Smith committed
490
    return NULL;
491 492 493 494
}

int util_dict_set(util_dict *dict, const char *key, const char *val)
{
495
    util_dict *prev;
496

497
    if (!dict || !key) {
498 499 500 501
        ERROR0("NULL values passed to util_dict_set()");
        return 0;
    }

502 503 504 505 506 507 508 509 510 511 512
    prev = NULL;
    while (dict) {
        if (!dict->key || !strcmp(dict->key, key))
            break;
        prev = dict;
        dict = dict->next;
    }

    if (!dict) {
        dict = util_dict_new();
        if (!dict) {
513
            ERROR0("unable to allocate new dictionary");
514
            return 0;
515
        }
516 517 518
        if (prev)
            prev->next = dict;
    }
519

520 521 522 523 524 525
    if (dict->key)
        free (dict->val);
    else if (!(dict->key = strdup(key))) {
        if (prev)
            prev->next = NULL;
        util_dict_free (dict);
526 527 528

        ERROR0("unable to allocate new dictionary key");
        return 0;
529
    }
530

531 532
    dict->val = strdup(val);
    if (!dict->val) {
533 534
        ERROR0("unable to allocate new dictionary value");
        return 0;
535
    }
536

537
    return 1;
538 539
}

540 541
/* given a dictionary, URL-encode each val and 
   stringify it in order as key=val&key=val... if val 
542 543 544 545
   is set, or just key&key if val is NULL.
  TODO: Memory management needs overhaul. */
char *util_dict_urlencode(util_dict *dict, char delim)
{
546 547 548 549 550 551 552 553 554
    char *res, *tmp;
    char *enc;
    int start = 1;

    for (res = NULL; dict; dict = dict->next) {
        /* encode key */
        if (!dict->key)
            continue;
        if (start) {
555
            if (!(res = malloc(strlen(dict->key) + 1))) {
556 557
                return NULL;
            }
558
            sprintf(res, "%s", dict->key);
559 560
            start = 0;
        } else {
561
            if (!(tmp = realloc(res, strlen(res) + strlen(dict->key) + 2))) {
562 563 564 565
                free(res);
                return NULL;
            } else
                res = tmp;
566
            sprintf(res + strlen(res), "%c%s", delim, dict->key);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
        }

        /* encode value */
        if (!dict->val)
            continue;
        if (!(enc = util_url_escape(dict->val))) {
            free(res);
            return NULL;
        }

        if (!(tmp = realloc(res, strlen(res) + strlen(enc) + 2))) {
            free(enc);
            free(res);
            return NULL;
        } else
            res = tmp;
        sprintf(res + strlen(res), "=%s", enc);
        free(enc);
    }

    return res;
588
}
Michael Smith's avatar
Michael Smith committed
589