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

Sergey Kandaurov pluknet at nginx.com
Fri Feb 24 10:32:56 UTC 2023


details:   https://hg.nginx.org/nginx/rev/e0f385521c79
branches:  
changeset: 8133:e0f385521c79
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Feb 23 20:49:45 2023 +0300
description:
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.

diffstat:

 src/os/win32/ngx_files.c |  80 ++++++++++++++++++++++++++++++++++++++++++-----
 src/os/win32/ngx_files.h |   2 +-
 2 files changed, 72 insertions(+), 10 deletions(-)

diffs (197 lines):

diff -r 8ea2e052feb4 -r e0f385521c79 src/os/win32/ngx_files.c
--- a/src/os/win32/ngx_files.c	Thu Feb 23 20:49:44 2023 +0300
+++ b/src/os/win32/ngx_files.c	Thu Feb 23 20:49:45 2023 +0300
@@ -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,
     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_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;
     }
 
@@ -478,7 +478,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;
     }
 
@@ -578,6 +578,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;
@@ -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
     /* 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
 
             if (ch == '/' || ch == '\\') {
                 state = sw_after_slash;
+                slash = p;
             }
 
             break;
@@ -842,6 +883,7 @@ ngx_win32_check_filename(u_char *name, u
 
             if (ch == '/' || ch == '\\') {
                 state = sw_after_slash;
+                slash = p;
                 break;
             }
 
@@ -869,6 +911,7 @@ ngx_win32_check_filename(u_char *name, u
 
             if (ch == '/' || ch == '\\') {
                 state = sw_after_slash;
+                slash = p;
                 break;
             }
 
@@ -897,6 +940,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);
@@ -907,6 +956,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;
     }
 
@@ -914,6 +968,10 @@ ngx_win32_check_filename(u_char *name, u
         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);
diff -r 8ea2e052feb4 -r e0f385521c79 src/os/win32/ngx_files.h
--- a/src/os/win32/ngx_files.h	Thu Feb 23 20:49:44 2023 +0300
+++ b/src/os/win32/ngx_files.h	Thu Feb 23 20:49:45 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()"
 
 


More information about the nginx-devel mailing list