Win32: non-ASCII directory names support in ngx_create_dir().

This makes it possible to create directories under prefix with non-ASCII
characters, as well as makes it possible to create directories with non-ASCII
characters when using the dav module (ticket #1433).

To ensure that the dav module operations are restricted similarly to
other file operations (in particular, short names are not allowed), the
ngx_win32_check_filename() function is used.  It improved to support
checking of just dirname, and now can be used to check paths when creating
files or directories.
This commit is contained in:
Maxim Dounin 2023-02-23 20:49:45 +03:00
parent 4408a67ee7
commit c77dd27fb0
2 changed files with 72 additions and 10 deletions

View File

@ -12,8 +12,8 @@
#define NGX_UTF16_BUFLEN 256
#define NGX_UTF8_BUFLEN 512
static ngx_int_t ngx_win32_check_filename(u_char *name, u_short *u,
size_t len);
static ngx_int_t ngx_win32_check_filename(u_short *u, size_t len,
ngx_uint_t dirname);
static u_short *ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t *len,
size_t reserved);
static u_char *ngx_utf16_to_utf8(u_char *utf8, u_short *utf16, size_t *len,
@ -42,7 +42,7 @@ ngx_open_file(u_char *name, u_long mode, u_long create, u_long access)
fd = INVALID_HANDLE_VALUE;
if (create == NGX_FILE_OPEN
&& ngx_win32_check_filename(name, u, len) != NGX_OK)
&& ngx_win32_check_filename(u, len, 0) != NGX_OK)
{
goto failed;
}
@ -282,7 +282,7 @@ ngx_file_info(u_char *file, ngx_file_info_t *sb)
rc = NGX_FILE_ERROR;
if (ngx_win32_check_filename(file, u, len) != NGX_OK) {
if (ngx_win32_check_filename(u, len, 0) != NGX_OK) {
goto failed;
}
@ -478,7 +478,7 @@ ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir)
return NGX_ERROR;
}
if (ngx_win32_check_filename(name->data, u, len) != NGX_OK) {
if (ngx_win32_check_filename(u, len, 0) != NGX_OK) {
goto failed;
}
@ -577,6 +577,42 @@ ngx_close_dir(ngx_dir_t *dir)
}
ngx_int_t
ngx_create_dir(u_char *name, ngx_uint_t access)
{
long rc;
size_t len;
u_short *u;
ngx_err_t err;
u_short utf16[NGX_UTF16_BUFLEN];
len = NGX_UTF16_BUFLEN;
u = ngx_utf8_to_utf16(utf16, name, &len, 0);
if (u == NULL) {
return NGX_FILE_ERROR;
}
rc = NGX_FILE_ERROR;
if (ngx_win32_check_filename(u, len, 1) != NGX_OK) {
goto failed;
}
rc = CreateDirectoryW(u, NULL);
failed:
if (u != utf16) {
err = ngx_errno;
ngx_free(u);
ngx_set_errno(err);
}
return rc;
}
ngx_int_t
ngx_open_glob(ngx_glob_t *gl)
{
@ -791,11 +827,10 @@ ngx_fs_available(u_char *name)
static ngx_int_t
ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
ngx_win32_check_filename(u_short *u, size_t len, ngx_uint_t dirname)
{
u_char *p, ch;
u_long n;
u_short *lu;
u_short *lu, *p, *slash, ch;
ngx_err_t err;
enum {
sw_start = 0,
@ -808,9 +843,14 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
/* check for NTFS streams (":"), trailing dots and spaces */
lu = NULL;
slash = NULL;
state = sw_start;
for (p = name; *p; p++) {
#if (NGX_SUPPRESS_WARN)
ch = 0;
#endif
for (p = u; *p; p++) {
ch = *p;
switch (state) {
@ -824,6 +864,7 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
if (ch == '/' || ch == '\\') {
state = sw_after_slash;
slash = p;
}
break;
@ -842,6 +883,7 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
if (ch == '/' || ch == '\\') {
state = sw_after_slash;
slash = p;
break;
}
@ -869,6 +911,7 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
if (ch == '/' || ch == '\\') {
state = sw_after_slash;
slash = p;
break;
}
@ -897,6 +940,12 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
goto invalid;
}
if (dirname && slash) {
ch = *slash;
*slash = '\0';
len = slash - u + 1;
}
/* check if long name match */
lu = malloc(len * 2);
@ -907,6 +956,11 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
n = GetLongPathNameW(u, lu, len);
if (n == 0) {
if (dirname && slash && ngx_errno == NGX_ENOENT) {
ngx_set_errno(NGX_ENOPATH);
}
goto failed;
}
@ -914,6 +968,10 @@ ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
goto invalid;
}
if (dirname && slash) {
*slash = ch;
}
ngx_free(lu);
return NGX_OK;
@ -924,6 +982,10 @@ invalid:
failed:
if (dirname && slash) {
*slash = ch;
}
if (lu) {
err = ngx_errno;
ngx_free(lu);

View File

@ -202,7 +202,7 @@ ngx_int_t ngx_close_dir(ngx_dir_t *dir);
#define ngx_close_dir_n "FindClose()"
#define ngx_create_dir(name, access) CreateDirectory((const char *) name, NULL)
ngx_int_t ngx_create_dir(u_char *name, ngx_uint_t access);
#define ngx_create_dir_n "CreateDirectory()"