Welcome! Log In Create A New Profile

Advanced

Re: [PATCH 04 of 12] Win32: non-ASCII directory names support in ngx_create_dir()

Sergey Kandaurov
February 17, 2023 10:14AM
> On 13 Jan 2023, at 01:35, Maxim Dounin <mdounin@mdounin.ru> wrote:
>
> # HG changeset patch
> # User Maxim Dounin <mdounin@mdounin.ru>
> # Date 1673548958 -10800
> # Thu Jan 12 21:42:38 2023 +0300
> # Node ID 331e40f73047c03455e10212b2b8701ec5ec35d0
> # Parent 7cf820c46860796cff91f53a5d2db669bb5b5a6c
> 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

"it is/was improved" ?

> checking of just dirname, and now can be used to check paths when creating
> files or directories.
>
> diff -r 7cf820c46860 -r 331e40f73047 src/os/win32/ngx_files.c
> --- a/src/os/win32/ngx_files.c Thu Jan 12 21:41:56 2023 +0300
> +++ b/src/os/win32/ngx_files.c Thu Jan 12 21:42:38 2023 +0300
> @@ -11,8 +11,8 @@
>
> #define NGX_UTF16_BUFLEN 256
>
> -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,
> @@ -41,7 +41,7 @@ ngx_open_file(u_char *name, u_long mode,
> 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;
> }
> @@ -281,7 +281,7 @@ ngx_file_info(u_char *file, ngx_file_inf
>
> 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;
> }
>
> @@ -467,7 +467,7 @@ ngx_open_dir(ngx_str_t *name, ngx_dir_t
> 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;
> }
>
> @@ -566,6 +566,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)
> {
> u_char *p;
> @@ -779,11 +815,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,
> @@ -796,9 +831,14 @@ ngx_win32_check_filename(u_char *name, u
> /* 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) {
> @@ -812,6 +852,7 @@ ngx_win32_check_filename(u_char *name, u
>
> if (ch == '/' || ch == '\\') {
> state = sw_after_slash;
> + slash = p;
> }
>
> break;
> @@ -830,6 +871,7 @@ ngx_win32_check_filename(u_char *name, u
>
> if (ch == '/' || ch == '\\') {
> state = sw_after_slash;
> + slash = p;
> break;
> }
>
> @@ -857,6 +899,7 @@ ngx_win32_check_filename(u_char *name, u
>
> if (ch == '/' || ch == '\\') {
> state = sw_after_slash;
> + slash = p;
> break;
> }
>
> @@ -885,6 +928,12 @@ ngx_win32_check_filename(u_char *name, u
> goto invalid;
> }
>
> + if (dirname && slash) {
> + ch = *slash;
> + *slash = '\0';
> + len = slash - u + 1;
> + }
> +
> /* check if long name match */
>
> lu = malloc(len * 2);

two minor points, unnoticeable in practice:
- *slash is not restored on lu allocation error
- it can be "restored" on parsing errors from a wrong ch,
because it is still not remembered as shown above.

> @@ -895,6 +944,11 @@ ngx_win32_check_filename(u_char *name, u
> n = GetLongPathNameW(u, lu, len);
>
> if (n == 0) {
> +
> + if (dirname && slash && ngx_errno == NGX_ENOENT) {
> + ngx_set_errno(NGX_ENOPATH);
> + }
> +
> goto failed;
> }
>
> @@ -902,6 +956,10 @@ ngx_win32_check_filename(u_char *name, u
> goto invalid;
> }
>
> + if (dirname && slash) {
> + *slash = ch;
> + }
> +
> ngx_free(lu);
>
> return NGX_OK;
> @@ -912,6 +970,10 @@ invalid:
>
> failed:
>
> + if (dirname && slash) {
> + *slash = ch;
> + }
> +
> if (lu) {
> err = ngx_errno;
> ngx_free(lu);
> diff -r 7cf820c46860 -r 331e40f73047 src/os/win32/ngx_files.h
> --- a/src/os/win32/ngx_files.h Thu Jan 12 21:41:56 2023 +0300
> +++ b/src/os/win32/ngx_files.h Thu Jan 12 21:42:38 2023 +0300
> @@ -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()"
>
>

--
Sergey Kandaurov
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[PATCH 00 of 12] win32 non-ASCII names support fixes

Maxim Dounin 822 January 12, 2023 04:40PM

[PATCH 03 of 12] Win32: non-ASCII directory names support in ngx_getcwd()

Maxim Dounin 184 January 12, 2023 04:40PM

Re: [PATCH 03 of 12] Win32: non-ASCII directory names support in ngx_getcwd()

Sergey Kandaurov 118 February 17, 2023 10:04AM

Re: [PATCH 03 of 12] Win32: non-ASCII directory names support in ngx_getcwd()

Maxim Dounin 177 February 19, 2023 12:24PM

Re: [PATCH 03 of 12] Win32: non-ASCII directory names support in ngx_getcwd()

Sergey Kandaurov 112 February 22, 2023 11:02AM

[PATCH 01 of 12] Win32: non-ASCII names support in autoindex (ticket #458)

Maxim Dounin 138 January 12, 2023 04:40PM

Re: [PATCH 01 of 12] Win32: non-ASCII names support in autoindex (ticket #458)

Sergey Kandaurov 127 February 17, 2023 09:40AM

Re: [PATCH 01 of 12] Win32: non-ASCII names support in autoindex (ticket #458)

Maxim Dounin 111 February 19, 2023 12:18PM

Re: [PATCH 01 of 12] Win32: non-ASCII names support in autoindex (ticket #458)

Sergey Kandaurov 118 February 22, 2023 10:40AM

[PATCH 02 of 12] Win32: non-ASCII names support in "include" with wildcards

Maxim Dounin 145 January 12, 2023 04:40PM

Re: [PATCH 02 of 12] Win32: non-ASCII names support in "include" with wildcards

Sergey Kandaurov 141 February 17, 2023 09:54AM

Re: [PATCH 02 of 12] Win32: non-ASCII names support in "include" with wildcards

Maxim Dounin 162 February 19, 2023 12:20PM

Re: [PATCH 02 of 12] Win32: non-ASCII names support in "include" with wildcards

Sergey Kandaurov 120 February 22, 2023 10:50AM

[PATCH 04 of 12] Win32: non-ASCII directory names support in ngx_create_dir()

Maxim Dounin 150 January 12, 2023 04:40PM

Re: [PATCH 04 of 12] Win32: non-ASCII directory names support in ngx_create_dir()

Sergey Kandaurov 136 February 17, 2023 10:14AM

[PATCH 05 of 12] Win32: non-ASCII directory names support in ngx_delete_dir()

Maxim Dounin 127 January 12, 2023 04:40PM

Re: [PATCH 05 of 12] Win32: non-ASCII directory names support in ngx_delete_dir()

Sergey Kandaurov 126 February 17, 2023 10:14AM

[PATCH 06 of 12] Win32: reworked ngx_win32_rename_file() to check errors

Maxim Dounin 152 January 12, 2023 04:40PM

[PATCH 07 of 12] Win32: reworked ngx_win32_rename_file() to use nginx wrappers

Maxim Dounin 123 January 12, 2023 04:40PM

[PATCH 09 of 12] Win32: non-ASCII names support in ngx_rename_file()

Maxim Dounin 172 January 12, 2023 04:40PM

[PATCH 10 of 12] Win32: non-ASCII names support in ngx_open_tempfile()

Maxim Dounin 132 January 12, 2023 04:40PM

[PATCH 08 of 12] Win32: non-ASCII names support in ngx_delete_file()

Maxim Dounin 126 January 12, 2023 04:40PM

[PATCH 12 of 12] Win32: non-ASCII names in ngx_fs_bsize(), ngx_fs_available()

Maxim Dounin 147 January 12, 2023 04:40PM

[PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Maxim Dounin 150 January 12, 2023 04:40PM

Re: [PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Sergey Kandaurov 120 February 17, 2023 10:18AM

Re: [PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Maxim Dounin 125 February 19, 2023 12:24PM

Re: [PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Sergey Kandaurov 116 February 22, 2023 11:02AM

Re: [PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Maxim Dounin 119 February 23, 2023 01:48PM

Re: [PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Sergey Kandaurov 123 February 24, 2023 05:42AM

Re: [PATCH 11 of 12] Win32: fixed ngx_fs_bsize() for symlinks

Sergey Kandaurov 131 March 21, 2023 07:26AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 203
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready