File: 0070-realpath_wrapper.patch

package info (click to toggle)
vsftpd 3.0.5-0.2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 2,636 kB
  • sloc: ansic: 16,810; sh: 251; makefile: 51; python: 18
file content (85 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Description: realpath wrapper to match not yet existing files in deny_file and others
Author: Thomas B. Preußer <thomas.preusser@utexas.edu>
Last-Update: 2015-12-23
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
===================================================================
Index:  ls.c
    sysutil.h
    sysutil.c

--- vsftpd.orig/ls.c
+++ vsftpd/ls.c
@@ -255,7 +255,7 @@
 
   /* normalize filepath */
   path = str_strdup(p_filename_str);
-  normname = realpath(path, NULL);
+  normname = vsf_sysutil_realpath(path, 1);
   if (normname == NULL)
      goto out;
   str_alloc_text(&normalize_filename_str, normname);
--- vsftpd.orig/sysutil.c
+++ vsftpd/sysutil.c
@@ -988,6 +988,51 @@
   return rename(p_from, p_to);
 }
 
+char*
+vsf_sysutil_realpath(char const *path, int may_be_fresh)
+{
+  { /* existing paths must resolve right away */
+    char *const  resolved = realpath(path, NULL);
+    if ((resolved != NULL) || (errno != ENOENT) || !may_be_fresh)
+    {
+      return  resolved;
+    }
+  }
+
+  { /* try to resolve directory part */
+    char const *filename = strrchr(path, '/');
+    char const *resolved_dir;
+    if(filename == NULL)
+    {
+      filename     = path;
+      resolved_dir = realpath(".", NULL);
+    }
+    else
+    {
+      char const *original_dir;
+      filename++;
+      original_dir = strndup(path, filename-path);
+      resolved_dir = realpath(original_dir, NULL);
+      free((void*)original_dir);
+    }
+    if(resolved_dir == NULL)  return  NULL;
+
+    /* compose path from resolved directory and filename */
+    size_t  dir_len = strlen(resolved_dir);
+    char *resolved;
+
+    /* empty root as slash is added anyways */
+    if (dir_len == 1)  dir_len == 0;
+
+    resolved = (char*)malloc(dir_len+strlen(filename)+2);
+    strcpy(resolved, resolved_dir);
+    free((void*)resolved_dir);
+    resolved[dir_len] = '/';
+    strcpy(resolved+dir_len+1, filename);
+    return  resolved;
+  }
+}
+
 struct vsf_sysutil_dir*
 vsf_sysutil_opendir(const char* p_dirname)
 {
--- vsftpd.orig/sysutil.h
+++ vsftpd/sysutil.h
@@ -66,6 +66,7 @@
 int vsf_sysutil_rmdir(const char* p_dirname);
 int vsf_sysutil_chdir(const char* p_dirname);
 int vsf_sysutil_rename(const char* p_from, const char* p_to);
+char* vsf_sysutil_realpath(char const *path, int may_be_fresh);
 
 struct vsf_sysutil_dir;
 struct vsf_sysutil_dir* vsf_sysutil_opendir(const char* p_dirname);