summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
Diffstat (limited to 'mysys')
-rw-r--r--mysys/Makefile.am3
-rw-r--r--mysys/charset-def.c6
-rw-r--r--mysys/charset.c75
-rw-r--r--mysys/default.c130
-rw-r--r--mysys/my_mmap.c7
-rw-r--r--mysys/my_windac.c224
6 files changed, 402 insertions, 43 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am
index 89d65624976..db8aa10bf1a 100644
--- a/mysys/Makefile.am
+++ b/mysys/Makefile.am
@@ -52,7 +52,8 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \
my_net.c my_semaphore.c my_port.c my_sleep.c \
charset.c charset-def.c my_bitmap.c my_bit.c md5.c \
my_gethostbyname.c rijndael.c my_aes.c sha1.c \
- my_handler.c my_netware.c my_largepage.c
+ my_handler.c my_netware.c my_largepage.c \
+ my_windac.c
EXTRA_DIST = thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \
thr_mutex.c thr_rwlock.c
libmysys_a_LIBADD = @THREAD_LOBJECTS@
diff --git a/mysys/charset-def.c b/mysys/charset-def.c
index f168556e3f7..92453aa0f59 100644
--- a/mysys/charset-def.c
+++ b/mysys/charset-def.c
@@ -62,6 +62,9 @@ extern CHARSET_INFO my_charset_utf8_slovak_uca_ci;
extern CHARSET_INFO my_charset_utf8_spanish2_uca_ci;
extern CHARSET_INFO my_charset_utf8_roman_uca_ci;
extern CHARSET_INFO my_charset_utf8_persian_uca_ci;
+#ifdef HAVE_CYBOZU_COLLATION
+extern CHARSET_INFO my_charset_utf8_general_cs;
+#endif
#endif
#endif /* HAVE_UCA_COLLATIONS */
@@ -156,6 +159,9 @@ my_bool init_compiled_charsets(myf flags __attribute__((unused)))
#ifdef HAVE_CHARSET_utf8
add_compiled_collation(&my_charset_utf8_general_ci);
add_compiled_collation(&my_charset_utf8_bin);
+#ifdef HAVE_CYBOZU_COLLATION
+ add_compiled_collation(&my_charset_utf8_general_cs);
+#endif
#ifdef HAVE_UCA_COLLATIONS
add_compiled_collation(&my_charset_utf8_general_uca_ci);
add_compiled_collation(&my_charset_utf8_icelandic_uca_ci);
diff --git a/mysys/charset.c b/mysys/charset.c
index 4b7ad3e59f4..a4a8205a3f9 100644
--- a/mysys/charset.c
+++ b/mysys/charset.c
@@ -547,10 +547,10 @@ CHARSET_INFO *get_charset_by_csname(const char *cs_name,
DBUG_PRINT("enter",("name: '%s'", cs_name));
(void) init_available_charsets(MYF(0)); /* If it isn't initialized */
-
+
cs_number= get_charset_number(cs_name, cs_flags);
cs= cs_number ? get_internal_charset(cs_number, flags) : NULL;
-
+
if (!cs && (flags & MY_WME))
{
char index_file[FN_REFLEN];
@@ -561,21 +561,34 @@ CHARSET_INFO *get_charset_by_csname(const char *cs_name,
DBUG_RETURN(cs);
}
-
-ulong escape_string_for_mysql(CHARSET_INFO *charset_info, char *to,
+/*
+ NOTE
+ to keep old C API, to_length may be 0 to mean "big enough"
+ RETURN
+ the length of the escaped string or ~0 if it did not fit.
+*/
+ulong escape_string_for_mysql(CHARSET_INFO *charset_info,
+ char *to, ulong to_length,
const char *from, ulong length)
{
const char *to_start= to;
- const char *end;
+ const char *end, *to_end=to_start + (to_length ? to_length-1 : 2*length);
+ my_bool overflow=0;
#ifdef USE_MB
my_bool use_mb_flag= use_mb(charset_info);
#endif
- for (end= from + length; from != end; from++)
+ for (end= from + length; from < end; from++)
{
+ char escape=0;
#ifdef USE_MB
int l;
if (use_mb_flag && (l= my_ismbchar(charset_info, from, end)))
{
+ if (to + l >= to_end)
+ {
+ overflow=1;
+ break;
+ }
while (l--)
*to++= *from++;
from--;
@@ -593,45 +606,53 @@ ulong escape_string_for_mysql(CHARSET_INFO *charset_info, char *to,
a valid GBK character, but 0xbf5c is. (0x27 = ', 0x5c = \)
*/
if (use_mb_flag && (l= my_mbcharlen(charset_info, *from)) > 1)
- {
- *to++= '\\';
- *to++= *from;
- continue;
- }
+ escape= *from;
+ else
#endif
switch (*from) {
case 0: /* Must be escaped for 'mysql' */
- *to++= '\\';
- *to++= '0';
+ escape= '0';
break;
case '\n': /* Must be escaped for logs */
- *to++= '\\';
- *to++= 'n';
+ escape= 'n';
break;
case '\r':
- *to++= '\\';
- *to++= 'r';
+ escape= 'r';
break;
case '\\':
- *to++= '\\';
- *to++= '\\';
+ escape= '\\';
break;
case '\'':
- *to++= '\\';
- *to++= '\'';
+ escape= '\'';
break;
case '"': /* Better safe than sorry */
- *to++= '\\';
- *to++= '"';
+ escape= '"';
break;
case '\032': /* This gives problems on Win32 */
- *to++= '\\';
- *to++= 'Z';
+ escape= 'Z';
break;
- default:
+ }
+ if (escape)
+ {
+ if (to + 2 >= to_end)
+ {
+ overflow=1;
+ break;
+ }
+ *to++= '\\';
+ *to++= escape;
+ }
+ else
+ {
+ if (to + 1 >= to_end)
+ {
+ overflow=1;
+ break;
+ }
*to++= *from;
}
}
*to= 0;
- return (ulong) (to - to_start);
+ return overflow ? (ulong)~0 : (ulong) (to - to_start);
}
+
diff --git a/mysys/default.c b/mysys/default.c
index 1c06feec634..ddff4e26be5 100644
--- a/mysys/default.c
+++ b/mysys/default.c
@@ -73,7 +73,7 @@ static int search_default_file(Process_option_func func, void *func_ctx,
static int search_default_file_with_ext(Process_option_func func,
void *func_ctx,
const char *dir, const char *ext,
- const char *config_file);
+ const char *config_file, int recursion_level);
static void init_default_directories();
static char *remove_end_comment(char *ptr);
@@ -124,7 +124,7 @@ int my_search_option_files(const char *conf_file, int *argc, char ***argv,
if (forced_default_file)
{
if ((error= search_default_file_with_ext(func, func_ctx, "", "",
- forced_default_file)) < 0)
+ forced_default_file, 0)) < 0)
goto err;
if (error > 0)
{
@@ -328,7 +328,7 @@ int load_defaults(const char *conf_file, const char **groups,
ctx.alloc= &alloc;
ctx.args= &args;
ctx.group= &group;
-
+
error= my_search_option_files(conf_file, argc, argv, &args_used,
handle_default_option, (void *) &ctx);
/*
@@ -402,7 +402,7 @@ static int search_default_file(Process_option_func opt_handler,
int error;
if ((error= search_default_file_with_ext(opt_handler, handler_ctx,
dir, *ext,
- config_file)) < 0)
+ config_file, 0)) < 0)
return error;
}
return 0;
@@ -411,7 +411,7 @@ static int search_default_file(Process_option_func opt_handler,
/*
Open a configuration file (if exists) and read given options from it
-
+
SYNOPSIS
search_default_file_with_ext()
opt_handler Option handler function. It is used to process
@@ -420,8 +420,10 @@ static int search_default_file(Process_option_func opt_handler,
parameters of the function.
dir directory to read
ext Extension for configuration file
- config_file Name of configuration file
+ config_file Name of configuration file
group groups to read
+ recursion_level the level of recursion, got while processing
+ "!include" or "!includedir"
RETURN
0 Success
@@ -433,13 +435,20 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
void *handler_ctx,
const char *dir,
const char *ext,
- const char *config_file)
+ const char *config_file,
+ int recursion_level)
{
- char name[FN_REFLEN+10], buff[4096], curr_gr[4096], *ptr, *end;
- char *value, option[4096];
+ char name[FN_REFLEN + 10], buff[4096], curr_gr[4096], *ptr, *end, **tmp_ext;
+ char *value, option[4096], tmp[FN_REFLEN];
+ static const char includedir_keyword[]= "includedir";
+ static const char include_keyword[]= "include";
+ const int max_recursion_level= 10;
FILE *fp;
uint line=0;
my_bool found_group=0;
+ uint i;
+ MY_DIR *search_dir;
+ FILEINFO *search_file;
if ((dir ? strlen(dir) : 0 )+strlen(config_file) >= FN_REFLEN-3)
return 0; /* Ignore wrong paths */
@@ -468,22 +477,117 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
if ((stat_info.st_mode & S_IWOTH) &&
(stat_info.st_mode & S_IFMT) == S_IFREG)
{
- fprintf(stderr, "warning: World-writeable config file %s is ignored\n",
+ fprintf(stderr, "warning: World-writable config file %s is ignored\n",
name);
return 0;
}
}
#endif
- if (!(fp = my_fopen(fn_format(name,name,"","",4),O_RDONLY,MYF(0))))
+ if (!(fp= my_fopen(fn_format(name, name, "", "", 4), O_RDONLY, MYF(0))))
return 0; /* Ignore wrong files */
- while (fgets(buff,sizeof(buff)-1,fp))
+ while (fgets(buff, sizeof(buff) - 1, fp))
{
line++;
/* Ignore comment and empty lines */
- for (ptr=buff ; my_isspace(&my_charset_latin1,*ptr) ; ptr++ ) ;
+ for (ptr= buff; my_isspace(&my_charset_latin1, *ptr); ptr++)
+ {}
+
if (*ptr == '#' || *ptr == ';' || !*ptr)
continue;
+
+ /* Configuration File Directives */
+ if ((*ptr == '!') && (recursion_level < max_recursion_level))
+ {
+ /* skip over `!' and following whitespace */
+ for (++ptr; my_isspace(&my_charset_latin1, ptr[0]); ptr++)
+ {}
+
+ if ((!strncmp(ptr, includedir_keyword, sizeof(includedir_keyword) - 1))
+ && my_isspace(&my_charset_latin1, ptr[sizeof(includedir_keyword) - 1]))
+ {
+ /* skip over "includedir" and following whitespace */
+ for (ptr+= sizeof(includedir_keyword) - 1;
+ my_isspace(&my_charset_latin1, ptr[0]); ptr++)
+ {}
+
+ /* trim trailing whitespace from directory name */
+ end= ptr + strlen(ptr) - 1;
+ /*
+ This would work fine even if no whitespaces are met
+ since fgets() stores the newline character in the buffer
+ */
+ for (; my_isspace(&my_charset_latin1, *(end - 1)); end--)
+ {}
+ end[0]= 0;
+
+ /* print error msg if there is nothing after !inludedir directive */
+ if (end == ptr)
+ {
+ fprintf(stderr,
+ "error: Wrong !includedir directive in config "
+ "file: %s at line %d\n",
+ name,line);
+ goto err;
+ }
+
+ if (!(search_dir= my_dir(ptr, MYF(MY_WME))))
+ goto err;
+
+ for (i= 0; i < (uint) search_dir->number_off_files; i++)
+ {
+ search_file= search_dir->dir_entry + i;
+ ext= fn_ext(search_file->name);
+
+ /* check extenstion */
+ for (tmp_ext= (char**) f_extensions; *tmp_ext; *tmp_ext++)
+ {
+ if (!strcmp(ext, *tmp_ext))
+ break;
+ }
+
+ if (*tmp_ext)
+ {
+ fn_format(tmp, search_file->name, ptr, "",
+ MY_UNPACK_FILENAME | MY_SAFE_PATH);
+
+ search_default_file_with_ext(opt_handler, handler_ctx, "", "", tmp,
+ recursion_level + 1);
+ }
+ }
+
+ my_dirend(search_dir);
+ }
+ else if ((!strncmp(ptr, include_keyword, sizeof(include_keyword) - 1))
+ && my_isspace(&my_charset_latin1, ptr[sizeof(include_keyword) - 1]))
+ {
+ /* skip over `include' and following whitespace */
+ for (ptr+= sizeof(include_keyword) - 1;
+ my_isspace(&my_charset_latin1, ptr[0]); ptr++)
+ {}
+
+ /* trim trailing whitespace from filename */
+ end= ptr + strlen(ptr) - 1;
+ for (; my_isspace(&my_charset_latin1, *(end - 1)) ; end--)
+ {}
+ end[0]= 0;
+
+ if (end == ptr)
+ {
+ fprintf(stderr,
+ "error: Wrong !include directive in config "
+ "file: %s at line %d\n",
+ name,line);
+ goto err;
+ }
+
+ search_default_file_with_ext(opt_handler, handler_ctx, "", "", ptr,
+ recursion_level + 1);
+ }
+
+ continue;
+ }
+
if (*ptr == '[') /* Group name */
{
found_group=1;
diff --git a/mysys/my_mmap.c b/mysys/my_mmap.c
index a111c3dc571..cd84630a761 100644
--- a/mysys/my_mmap.c
+++ b/mysys/my_mmap.c
@@ -46,11 +46,14 @@ void *my_mmap(void *addr, size_t len, int prot,
DWORD flProtect=0;
HANDLE hFileMap;
LPVOID ptr;
+ HANDLE hFile= (HANDLE)_get_osfhandle(fd);
+ if (hFile == INVALID_HANDLE_VALUE)
+ return MAP_FAILED;
flProtect|=SEC_COMMIT;
- hFileMap=CreateFileMapping(fd, NULL, &mmap_security_attributes,
- PAGE_READWRITE, 0, len, 0);
+ hFileMap=CreateFileMapping(hFile, &mmap_security_attributes,
+ PAGE_READWRITE, 0, len, NULL);
if (hFileMap == 0)
return MAP_FAILED;
diff --git a/mysys/my_windac.c b/mysys/my_windac.c
new file mode 100644
index 00000000000..2c1027e4aa6
--- /dev/null
+++ b/mysys/my_windac.c
@@ -0,0 +1,224 @@
+/* Copyright (C) 2000-2005 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#include "mysys_priv.h"
+#include "m_string.h"
+#ifdef __WIN__
+
+/* Windows NT/2000 discretionary access control utility functions. */
+
+/*
+ Check if the operating system is built on NT technology.
+
+ RETURN
+ 0 Windows 95/98/Me
+ 1 otherwise
+*/
+
+static my_bool is_nt()
+{
+ return GetVersion() < 0x80000000;
+}
+
+/*
+ Auxilary structure to store pointers to the data which we need to keep
+ around while SECURITY_ATTRIBUTES is in use.
+*/
+
+typedef struct st_my_security_attr
+{
+ PSID everyone_sid;
+ PACL dacl;
+} My_security_attr;
+
+
+/*
+ Allocate and initialize SECURITY_ATTRIBUTES setting up access
+ rights for the owner and group `Everybody'.
+
+ SYNOPSIS
+ my_security_attr_create()
+ psa [OUT] pointer to store the pointer to SA in
+ perror [OUT] pointer to store error message if there was an
+ error
+ owner_rights [IN] access rights for the owner
+ everyone_rights [IN] access rights for group Everybody
+
+ DESCRIPTION
+ Set up the security attributes to provide clients with sufficient
+ access rights to a kernel object. We need this function
+ because if we simply grant all access to everybody (by installing
+ a NULL DACL) a mailicious user can attempt a denial of service
+ attack by taking ownership over the kernel object. Upon successful
+ return `psa' contains a pointer to SECUIRITY_ATTRIBUTES that can be used
+ to create kernel objects with proper access rights.
+
+ RETURN
+ 0 success, psa is 0 or points to a valid SA structure,
+ perror is left intact
+ !0 error, SA is set to 0, error message is stored in perror
+*/
+
+int my_security_attr_create(SECURITY_ATTRIBUTES **psa, const char **perror,
+ DWORD owner_rights, DWORD everyone_rights)
+{
+ /* Top-level SID authority */
+ SID_IDENTIFIER_AUTHORITY world_auth= SECURITY_WORLD_SID_AUTHORITY;
+ PSID everyone_sid= 0;
+ HANDLE htoken= 0;
+ SECURITY_ATTRIBUTES *sa= 0;
+ PACL dacl= 0;
+ DWORD owner_token_length, dacl_length;
+ SECURITY_DESCRIPTOR *sd;
+ PTOKEN_USER owner_token;
+ PSID owner_sid;
+ My_security_attr *attr;
+
+ if (! is_nt())
+ {
+ *psa= 0;
+ return 0;
+ }
+
+ /*
+ Get SID of Everyone group. Easier to retrieve all SIDs each time
+ this function is called than worry about thread safety.
+ */
+ if (! AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
+ 0, 0, 0, 0, 0, 0, 0, &everyone_sid))
+ {
+ *perror= "Failed to retrieve the SID of Everyone group";
+ goto error;
+ }
+
+ /*
+ Get SID of the owner. Using GetSecurityInfo this task can be done
+ in just one call instead of five, but GetSecurityInfo declared in
+ aclapi.h, so I hesitate to use it.
+ SIC: OpenThreadToken works only if there is an active impersonation
+ token, hence OpenProcessToken is used.
+ */
+ if (! OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &htoken))
+ {
+ *perror= "Failed to retrieve thread access token";
+ goto error;
+ }
+ GetTokenInformation(htoken, TokenUser, 0, 0, &owner_token_length);
+
+ if (! my_multi_malloc(MYF(MY_WME),
+ &sa, ALIGN_SIZE(sizeof(SECURITY_ATTRIBUTES)) +
+ sizeof(My_security_attr),
+ &sd, sizeof(SECURITY_DESCRIPTOR),
+ &owner_token, owner_token_length,
+ 0))
+ {
+ *perror= "Failed to allocate memory for SECURITY_ATTRIBUTES";
+ goto error;
+ }
+ bzero(owner_token, owner_token_length);
+ if (! GetTokenInformation(htoken, TokenUser, owner_token,
+ owner_token_length, &owner_token_length))
+ {
+ *perror= "GetTokenInformation failed";
+ goto error;
+ }
+ owner_sid= owner_token->User.Sid;
+
+ if (! IsValidSid(owner_sid))
+ {
+ *perror= "IsValidSid failed";
+ goto error;
+ }
+
+ /* Calculate the amount of memory that must be allocated for the DACL */
+ dacl_length= sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD)) * 2 +
+ GetLengthSid(everyone_sid) + GetLengthSid(owner_sid);
+
+ /* Create an ACL */
+ if (! (dacl= (PACL) my_malloc(dacl_length, MYF(MY_ZEROFILL|MY_WME))))
+ {
+ *perror= "Failed to allocate memory for DACL";
+ goto error;
+ }
+ if (! InitializeAcl(dacl, dacl_length, ACL_REVISION))
+ {
+ *perror= "Failed to initialize DACL";
+ goto error;
+ }
+ if (! AddAccessAllowedAce(dacl, ACL_REVISION, everyone_rights, everyone_sid))
+ {
+ *perror= "Failed to set up DACL";
+ goto error;
+ }
+ if (! AddAccessAllowedAce(dacl, ACL_REVISION, owner_rights, owner_sid))
+ {
+ *perror= "Failed to set up DACL";
+ goto error;
+ }
+ if (! InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION))
+ {
+ *perror= "Could not initialize security descriptor";
+ goto error;
+ }
+ if (! SetSecurityDescriptorDacl(sd, TRUE, dacl, FALSE))
+ {
+ *perror= "Failed to install DACL";
+ goto error;
+ }
+
+ sa->nLength= sizeof(*sa);
+ sa->bInheritHandle= TRUE;
+ sa->lpSecurityDescriptor= sd;
+ /* Save pointers to everyone_sid and dacl to be able to clean them up */
+ attr= (My_security_attr*) (((char*) sa) + ALIGN_SIZE(sizeof(*sa)));
+ attr->everyone_sid= everyone_sid;
+ attr->dacl= dacl;
+ *psa= sa;
+
+ CloseHandle(htoken);
+ return 0;
+error:
+ if (everyone_sid)
+ FreeSid(everyone_sid);
+ if (htoken)
+ CloseHandle(htoken);
+ my_free((gptr) sa, MYF(MY_ALLOW_ZERO_PTR));
+ my_free((gptr) dacl, MYF(MY_ALLOW_ZERO_PTR));
+ *psa= 0;
+ return 1;
+}
+
+/*
+ Cleanup security attributes freeing used memory.
+
+ SYNOPSIS
+ my_security_attr_free()
+ sa security attributes
+*/
+
+void my_security_attr_free(SECURITY_ATTRIBUTES *sa)
+{
+ if (sa)
+ {
+ My_security_attr *attr= (My_security_attr*)
+ (((char*)sa) + ALIGN_SIZE(sizeof(*sa)));
+ FreeSid(attr->everyone_sid);
+ my_free((gptr) attr->dacl, MYF(0));
+ my_free((gptr) sa, MYF(0));
+ }
+}
+
+#endif /* __WIN__ */