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

Maxim Dounin mdounin at mdounin.ru
Thu Jan 12 21:35:27 UTC 2023


# HG changeset patch
# User Maxim Dounin <mdounin at 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
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);
@@ -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()"
 
 


More information about the nginx-devel mailing list