util.c 5.79 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1
#include <sys/types.h>
2 3
#include <stdio.h>
#include <string.h>
4
#include <stdlib.h>
Jack Moffitt's avatar
Jack Moffitt committed
5 6

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

#include "sock.h"

#include "config.h"
#include "util.h"
26
#include "os.h"
27 28 29 30 31 32 33 34
#include "refbuf.h"
#include "connection.h"
#include "client.h"

#define CATMODULE "util"

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

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/* 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;
    struct timeval tv;

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

    tv.tv_sec = timeout/1000;
    tv.tv_usec = (timeout - tv.tv_sec)*1000;

    return select(fd+1, &rfds, NULL, NULL, &tv);
#endif
}

Jack Moffitt's avatar
Jack Moffitt committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
int util_read_header(int sock, char *buff, unsigned long len)
{
	int read_bytes, ret;
	unsigned long pos;
	char c;
	ice_config_t *config;

	config = config_get_config();

	read_bytes = 1;
	pos = 0;
	ret = 0;

	while ((read_bytes == 1) && (pos < (len - 1))) {
		read_bytes = 0;

85
        if (util_timed_wait_for_fd(sock, config->header_timeout*1000) > 0) {
Jack Moffitt's avatar
Jack Moffitt committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

			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;
}

104 105 106 107 108 109 110 111 112
char *util_get_extension(char *path) {
    char *ext = strrchr(path, '.');

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

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
int util_check_valid_extension(char *uri) {
	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;
}
Jack Moffitt's avatar
Jack Moffitt committed
144

145 146 147 148 149 150 151 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
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;
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
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;

    fullpath = malloc(strlen(uri) + strlen(config_get_config()->webroot_dir) + 1);
    strcpy(fullpath, config_get_config()->webroot_dir);

    strcat(fullpath, uri);

    return fullpath;
}


    

212 213 214 215 216
/* 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.
 */
217
char *util_normalise_uri(char *uri) {
218 219 220 221 222 223 224 225 226
    int urilen = strlen(uri);
    unsigned char *path;
    char *dst;
    int i;
    int done = 0;

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

227
    path = calloc(1, urilen + 1);
228

229
    dst = path;
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

    for(i=0; i < urilen; i++) {
        switch(uri[i]) {
            case '%':
                if(i+2 >= urilen) {
                    free(path);
                    return NULL;
                }
                if(hex(uri[i+1]) == -1 || hex(uri[i+2]) == -1 ) {
                    free(path);
                    return NULL;
                }

                *dst++ = hex(uri[i+1]) * 16  + hex(uri[i+2]);
                i+= 2;
                break;
            case '#':
                done = 1;
                break;
            case 0:
                ERROR0("Fatal internal logic error in util_get_path_from_uri()");
                free(path);
                return NULL;
                break;
            default:
                *dst++ = uri[i];
                break;
        }
        if(done)
            break;
    }

    *dst = 0; /* null terminator */

    /* We now have a full URI-decoded path. Check it for allowability */
    if(verify_path(path))
        return (char *)path;
    else {
        WARN1("Rejecting invalid path \"%s\"", path);
        free(path);
        return NULL;
    }
}
Jack Moffitt's avatar
Jack Moffitt committed
273