]> Git Repo - J-linux.git/blob - fs/smb/server/misc.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / fs / smb / server / misc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <[email protected]>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/xattr.h>
9 #include <linux/fs.h>
10 #include <linux/unicode.h>
11
12 #include "misc.h"
13 #include "smb_common.h"
14 #include "connection.h"
15 #include "vfs.h"
16
17 #include "mgmt/share_config.h"
18
19 /**
20  * match_pattern() - compare a string with a pattern which might include
21  * wildcard '*' and '?'
22  * TODO : implement consideration about DOS_DOT, DOS_QM and DOS_STAR
23  *
24  * @str:        string to compare with a pattern
25  * @len:        string length
26  * @pattern:    pattern string which might include wildcard '*' and '?'
27  *
28  * Return:      0 if pattern matched with the string, otherwise non zero value
29  */
30 int match_pattern(const char *str, size_t len, const char *pattern)
31 {
32         const char *s = str;
33         const char *p = pattern;
34         bool star = false;
35
36         while (*s && len) {
37                 switch (*p) {
38                 case '?':
39                         s++;
40                         len--;
41                         p++;
42                         break;
43                 case '*':
44                         star = true;
45                         str = s;
46                         if (!*++p)
47                                 return true;
48                         pattern = p;
49                         break;
50                 default:
51                         if (tolower(*s) == tolower(*p)) {
52                                 s++;
53                                 len--;
54                                 p++;
55                         } else {
56                                 if (!star)
57                                         return false;
58                                 str++;
59                                 s = str;
60                                 p = pattern;
61                         }
62                         break;
63                 }
64         }
65
66         if (*p == '*')
67                 ++p;
68         return !*p;
69 }
70
71 /*
72  * is_char_allowed() - check for valid character
73  * @ch:         input character to be checked
74  *
75  * Return:      1 if char is allowed, otherwise 0
76  */
77 static inline int is_char_allowed(char ch)
78 {
79         /* check for control chars, wildcards etc. */
80         if (!(ch & 0x80) &&
81             (ch <= 0x1f ||
82              ch == '?' || ch == '"' || ch == '<' ||
83              ch == '>' || ch == '|' || ch == '*'))
84                 return 0;
85
86         return 1;
87 }
88
89 int ksmbd_validate_filename(char *filename)
90 {
91         while (*filename) {
92                 char c = *filename;
93
94                 filename++;
95                 if (!is_char_allowed(c)) {
96                         ksmbd_debug(VFS, "File name validation failed: 0x%x\n", c);
97                         return -ENOENT;
98                 }
99         }
100
101         return 0;
102 }
103
104 static int ksmbd_validate_stream_name(char *stream_name)
105 {
106         while (*stream_name) {
107                 char c = *stream_name;
108
109                 stream_name++;
110                 if (c == '/' || c == ':' || c == '\\') {
111                         pr_err("Stream name validation failed: %c\n", c);
112                         return -ENOENT;
113                 }
114         }
115
116         return 0;
117 }
118
119 int parse_stream_name(char *filename, char **stream_name, int *s_type)
120 {
121         char *stream_type;
122         char *s_name;
123         int rc = 0;
124
125         s_name = filename;
126         filename = strsep(&s_name, ":");
127         ksmbd_debug(SMB, "filename : %s, streams : %s\n", filename, s_name);
128         if (strchr(s_name, ':')) {
129                 stream_type = s_name;
130                 s_name = strsep(&stream_type, ":");
131
132                 rc = ksmbd_validate_stream_name(s_name);
133                 if (rc < 0) {
134                         rc = -ENOENT;
135                         goto out;
136                 }
137
138                 ksmbd_debug(SMB, "stream name : %s, stream type : %s\n", s_name,
139                             stream_type);
140                 if (!strncasecmp("$data", stream_type, 5))
141                         *s_type = DATA_STREAM;
142                 else if (!strncasecmp("$index_allocation", stream_type, 17))
143                         *s_type = DIR_STREAM;
144                 else
145                         rc = -ENOENT;
146         }
147
148         *stream_name = s_name;
149 out:
150         return rc;
151 }
152
153 /**
154  * convert_to_nt_pathname() - extract and return windows path string
155  *      whose share directory prefix was removed from file path
156  * @share: ksmbd_share_config pointer
157  * @path: path to report
158  *
159  * Return : windows path string or error
160  */
161
162 char *convert_to_nt_pathname(struct ksmbd_share_config *share,
163                              const struct path *path)
164 {
165         char *pathname, *ab_pathname, *nt_pathname;
166         int share_path_len = share->path_sz;
167
168         pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
169         if (!pathname)
170                 return ERR_PTR(-EACCES);
171
172         ab_pathname = d_path(path, pathname, PATH_MAX);
173         if (IS_ERR(ab_pathname)) {
174                 nt_pathname = ERR_PTR(-EACCES);
175                 goto free_pathname;
176         }
177
178         if (strncmp(ab_pathname, share->path, share_path_len)) {
179                 nt_pathname = ERR_PTR(-EACCES);
180                 goto free_pathname;
181         }
182
183         nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2,
184                               KSMBD_DEFAULT_GFP);
185         if (!nt_pathname) {
186                 nt_pathname = ERR_PTR(-ENOMEM);
187                 goto free_pathname;
188         }
189         if (ab_pathname[share_path_len] == '\0')
190                 strcpy(nt_pathname, "/");
191         strcat(nt_pathname, &ab_pathname[share_path_len]);
192
193         ksmbd_conv_path_to_windows(nt_pathname);
194
195 free_pathname:
196         kfree(pathname);
197         return nt_pathname;
198 }
199
200 int get_nlink(struct kstat *st)
201 {
202         int nlink;
203
204         nlink = st->nlink;
205         if (S_ISDIR(st->mode))
206                 nlink--;
207
208         return nlink;
209 }
210
211 void ksmbd_conv_path_to_unix(char *path)
212 {
213         strreplace(path, '\\', '/');
214 }
215
216 void ksmbd_strip_last_slash(char *path)
217 {
218         int len = strlen(path);
219
220         while (len && path[len - 1] == '/') {
221                 path[len - 1] = '\0';
222                 len--;
223         }
224 }
225
226 void ksmbd_conv_path_to_windows(char *path)
227 {
228         strreplace(path, '/', '\\');
229 }
230
231 char *ksmbd_casefold_sharename(struct unicode_map *um, const char *name)
232 {
233         char *cf_name;
234         int cf_len;
235
236         cf_name = kzalloc(KSMBD_REQ_MAX_SHARE_NAME, KSMBD_DEFAULT_GFP);
237         if (!cf_name)
238                 return ERR_PTR(-ENOMEM);
239
240         if (IS_ENABLED(CONFIG_UNICODE) && um) {
241                 const struct qstr q_name = {.name = name, .len = strlen(name)};
242
243                 cf_len = utf8_casefold(um, &q_name, cf_name,
244                                        KSMBD_REQ_MAX_SHARE_NAME);
245                 if (cf_len < 0)
246                         goto out_ascii;
247
248                 return cf_name;
249         }
250
251 out_ascii:
252         cf_len = strscpy(cf_name, name, KSMBD_REQ_MAX_SHARE_NAME);
253         if (cf_len < 0) {
254                 kfree(cf_name);
255                 return ERR_PTR(-E2BIG);
256         }
257
258         for (; *cf_name; ++cf_name)
259                 *cf_name = isascii(*cf_name) ? tolower(*cf_name) : *cf_name;
260         return cf_name - cf_len;
261 }
262
263 /**
264  * ksmbd_extract_sharename() - get share name from tree connect request
265  * @um: pointer to a unicode_map structure for character encoding handling
266  * @treename:   buffer containing tree name and share name
267  *
268  * Return:      share name on success, otherwise error
269  */
270 char *ksmbd_extract_sharename(struct unicode_map *um, const char *treename)
271 {
272         const char *name = treename, *pos = strrchr(name, '\\');
273
274         if (pos)
275                 name = (pos + 1);
276
277         /* caller has to free the memory */
278         return ksmbd_casefold_sharename(um, name);
279 }
280
281 /**
282  * convert_to_unix_name() - convert windows name to unix format
283  * @share:      ksmbd_share_config pointer
284  * @name:       file name that is relative to share
285  *
286  * Return:      converted name on success, otherwise NULL
287  */
288 char *convert_to_unix_name(struct ksmbd_share_config *share, const char *name)
289 {
290         int no_slash = 0, name_len, path_len;
291         char *new_name;
292
293         if (name[0] == '/')
294                 name++;
295
296         path_len = share->path_sz;
297         name_len = strlen(name);
298         new_name = kmalloc(path_len + name_len + 2, KSMBD_DEFAULT_GFP);
299         if (!new_name)
300                 return new_name;
301
302         memcpy(new_name, share->path, path_len);
303         if (new_name[path_len - 1] != '/') {
304                 new_name[path_len] = '/';
305                 no_slash = 1;
306         }
307
308         memcpy(new_name + path_len + no_slash, name, name_len);
309         path_len += name_len + no_slash;
310         new_name[path_len] = 0x00;
311         return new_name;
312 }
313
314 char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info,
315                                   const struct nls_table *local_nls,
316                                   int *conv_len)
317 {
318         char *conv;
319         int  sz = min(4 * d_info->name_len, PATH_MAX);
320
321         if (!sz)
322                 return NULL;
323
324         conv = kmalloc(sz, KSMBD_DEFAULT_GFP);
325         if (!conv)
326                 return NULL;
327
328         /* XXX */
329         *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name,
330                                       d_info->name_len, local_nls, 0);
331         *conv_len *= 2;
332
333         /* We allocate buffer twice bigger than needed. */
334         conv[*conv_len] = 0x00;
335         conv[*conv_len + 1] = 0x00;
336         return conv;
337 }
338
339 /*
340  * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
341  * into Unix UTC (based 1970-01-01, in seconds).
342  */
343 struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc)
344 {
345         struct timespec64 ts;
346
347         /* Subtract the NTFS time offset, then convert to 1s intervals. */
348         s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
349         u64 abs_t;
350
351         /*
352          * Unfortunately can not use normal 64 bit division on 32 bit arch, but
353          * the alternative, do_div, does not work with negative numbers so have
354          * to special case them
355          */
356         if (t < 0) {
357                 abs_t = -t;
358                 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
359                 ts.tv_nsec = -ts.tv_nsec;
360                 ts.tv_sec = -abs_t;
361         } else {
362                 abs_t = t;
363                 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
364                 ts.tv_sec = abs_t;
365         }
366
367         return ts;
368 }
369
370 /* Convert the Unix UTC into NT UTC. */
371 inline u64 ksmbd_UnixTimeToNT(struct timespec64 t)
372 {
373         /* Convert to 100ns intervals and then add the NTFS time offset. */
374         return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET;
375 }
376
377 inline long long ksmbd_systime(void)
378 {
379         struct timespec64       ts;
380
381         ktime_get_real_ts64(&ts);
382         return ksmbd_UnixTimeToNT(ts);
383 }
This page took 0.053273 seconds and 4 git commands to generate.