|
Packit |
284210 |
/*
|
|
Packit |
284210 |
* ModSecurity for Apache 2.x, http://www.modsecurity.org/
|
|
Packit |
284210 |
* Copyright (c) 2004-2013 Trustwave Holdings, Inc. (http://www.trustwave.com/)
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* You may not use this file except in compliance with
|
|
Packit |
284210 |
* the License. You may obtain a copy of the License at
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* If any of the files related to licensing are missing or if you have any
|
|
Packit |
284210 |
* other questions related to licensing please contact Trustwave Holdings, Inc.
|
|
Packit |
284210 |
* directly using the email address security@modsecurity.org.
|
|
Packit |
284210 |
*/
|
|
Packit |
284210 |
|
|
Packit |
284210 |
#include "msc_unicode.h"
|
|
Packit |
284210 |
|
|
Packit |
284210 |
#define CODEPAGE_SEPARATORS " \t\n\r"
|
|
Packit |
284210 |
|
|
Packit |
284210 |
/** \brief Load Unicode file
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* \param dcfg Pointer to directory configuration
|
|
Packit |
284210 |
* \param error_msg Error message
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* \retval 1 On Success
|
|
Packit |
284210 |
* \retval 0 On Fail
|
|
Packit |
284210 |
*/
|
|
Packit |
284210 |
static int unicode_map_create(directory_config *dcfg, char **error_msg)
|
|
Packit |
284210 |
{
|
|
Packit |
284210 |
char errstr[1024];
|
|
Packit |
284210 |
apr_pool_t *mp = dcfg->mp;
|
|
Packit |
284210 |
unicode_map *u_map = dcfg->u_map;
|
|
Packit |
284210 |
apr_int32_t wanted = APR_FINFO_SIZE;
|
|
Packit |
284210 |
apr_finfo_t finfo;
|
|
Packit |
284210 |
apr_status_t rc;
|
|
Packit |
284210 |
apr_size_t nbytes;
|
|
Packit |
284210 |
unsigned int codepage = 0;
|
|
Packit |
284210 |
char *buf = NULL, *p = NULL, *savedptr = NULL;
|
|
Packit |
284210 |
char *ucode = NULL, *hmap = NULL;
|
|
Packit |
284210 |
int found = 0, processing = 0;
|
|
Packit |
284210 |
int Code = 0, Map = 0;
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if(unicode_map_table != NULL) {
|
|
Packit |
284210 |
free(unicode_map_table);
|
|
Packit |
284210 |
unicode_map_table = NULL;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if ((rc = apr_file_open(&u_map->map, u_map->mapfn, APR_READ, APR_OS_DEFAULT, mp)) != APR_SUCCESS) {
|
|
Packit |
284210 |
*error_msg = apr_psprintf(mp, "Could not open unicode map file \"%s\": %s", u_map->mapfn, apr_strerror(rc, errstr, 1024));
|
|
Packit |
284210 |
return 0;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if ((rc = apr_file_info_get(&finfo, wanted, u_map->map)) != APR_SUCCESS) {
|
|
Packit |
284210 |
*error_msg = apr_psprintf(mp, "Could not cannot get unicode map file information \"%s\": %s", u_map->mapfn, apr_strerror(rc, errstr, 1024));
|
|
Packit |
284210 |
apr_file_close(u_map->map);
|
|
Packit |
284210 |
return 0;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
buf = (char *)malloc(finfo.size+1);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if (buf == NULL) {
|
|
Packit |
284210 |
*error_msg = apr_psprintf(mp, "Could not alloc memory for unicode map");
|
|
Packit |
284210 |
apr_file_close(u_map->map);
|
|
Packit |
284210 |
return 0;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
rc = apr_file_read_full(u_map->map, buf, finfo.size, &nbytes);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if (unicode_map_table != NULL) {
|
|
Packit |
284210 |
memset(unicode_map_table, -1, (sizeof(int)*65536));
|
|
Packit |
284210 |
} else {
|
|
Packit |
284210 |
unicode_map_table = (int *)malloc(sizeof(int) * 65536);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if(unicode_map_table == NULL) {
|
|
Packit |
284210 |
*error_msg = apr_psprintf(mp, "Could not alloc memory for unicode map");
|
|
Packit |
284210 |
free(buf);
|
|
Packit |
284210 |
buf = NULL;
|
|
Packit |
284210 |
apr_file_close(u_map->map);
|
|
Packit |
284210 |
return 0;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
memset(unicode_map_table, -1, (sizeof(int)*65536));
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
/* Setting some unicode values - http://tools.ietf.org/html/rfc3490#section-3.1 */
|
|
Packit |
284210 |
|
|
Packit |
284210 |
/* Set 0x3002 -> 0x2e */
|
|
Packit |
284210 |
unicode_map_table[0x3002] = 0x2e;
|
|
Packit |
284210 |
/* Set 0xFF61 -> 0x2e */
|
|
Packit |
284210 |
unicode_map_table[0xff61] = 0x2e;
|
|
Packit |
284210 |
/* Set 0xFF0E -> 0x2e */
|
|
Packit |
284210 |
unicode_map_table[0xff0e] = 0x2e;
|
|
Packit |
284210 |
/* Set 0x002E -> 0x2e */
|
|
Packit |
284210 |
unicode_map_table[0x002e] = 0x2e;
|
|
Packit |
284210 |
|
|
Packit |
284210 |
p = apr_strtok(buf,CODEPAGE_SEPARATORS,&savedptr);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
while (p != NULL) {
|
|
Packit |
284210 |
|
|
Packit |
284210 |
codepage = atol(p);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if (codepage == unicode_codepage) {
|
|
Packit |
284210 |
found = 1;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if (found == 1 && (strchr(p,':') != NULL)) {
|
|
Packit |
284210 |
char *mapping = strdup(p);
|
|
Packit |
284210 |
processing = 1;
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if(mapping != NULL) {
|
|
Packit |
284210 |
ucode = apr_strtok(mapping,":", &hmap);
|
|
Packit |
284210 |
sscanf(ucode,"%x",&Code);
|
|
Packit |
284210 |
sscanf(hmap,"%x",&Map);
|
|
Packit |
284210 |
if(Code >= 0 && Code <= 65535) {
|
|
Packit |
284210 |
unicode_map_table[Code] = Map;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
free(mapping);
|
|
Packit |
284210 |
mapping = NULL;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if (processing == 1 && (strchr(p,':') == NULL)) {
|
|
Packit |
284210 |
free(buf);
|
|
Packit |
284210 |
buf = NULL;
|
|
Packit |
284210 |
break;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
p = apr_strtok(NULL,CODEPAGE_SEPARATORS,&savedptr);
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
apr_file_close(u_map->map);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if(buf) {
|
|
Packit |
284210 |
free(buf);
|
|
Packit |
284210 |
buf = NULL;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
return 1;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
|
|
Packit |
284210 |
/** \brief Init unicode map
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* \param dcfg Pointer to directory configuration
|
|
Packit |
284210 |
* \param mapfn Unicode map filename
|
|
Packit |
284210 |
* \param error_msg Error message
|
|
Packit |
284210 |
*
|
|
Packit |
284210 |
* \retval unicode_map_create On Success
|
|
Packit |
284210 |
* \retval -1 On Fail
|
|
Packit |
284210 |
*/
|
|
Packit |
284210 |
int unicode_map_init(directory_config *dcfg, const char *mapfn, char **error_msg)
|
|
Packit |
284210 |
{
|
|
Packit |
284210 |
|
|
Packit |
284210 |
*error_msg = NULL;
|
|
Packit |
284210 |
|
|
Packit |
284210 |
if ((dcfg->u_map == NULL) || (dcfg->u_map == NOT_SET_P)) {
|
|
Packit |
284210 |
dcfg->u_map = apr_pcalloc(dcfg->mp, sizeof(unicode_map));
|
|
Packit |
284210 |
if (dcfg->u_map == NULL) {
|
|
Packit |
284210 |
return -1;
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|
|
Packit |
284210 |
dcfg->u_map->map = NULL;
|
|
Packit |
284210 |
dcfg->u_map->mapfn = apr_pstrdup(dcfg->mp, mapfn);
|
|
Packit |
284210 |
|
|
Packit |
284210 |
return unicode_map_create(dcfg, error_msg);
|
|
Packit |
284210 |
}
|
|
Packit |
284210 |
|