1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/kernel.h>
8 #include <linux/xattr.h>
12 #include "smb_common.h"
13 #include "connection.h"
16 #include "mgmt/share_config.h"
19 * match_pattern() - compare a string with a pattern which might include
20 * wildcard '*' and '?'
21 * TODO : implement consideration about DOS_DOT, DOS_QM and DOS_STAR
23 * @str: string to compare with a pattern
25 * @pattern: pattern string which might include wildcard '*' and '?'
27 * Return: 0 if pattern matched with the string, otherwise non zero value
29 int match_pattern(const char *str, size_t len, const char *pattern)
32 const char *p = pattern;
50 if (tolower(*s) == tolower(*p)) {
71 * is_char_allowed() - check for valid character
72 * @ch: input character to be checked
74 * Return: 1 if char is allowed, otherwise 0
76 static inline int is_char_allowed(char ch)
78 /* check for control chars, wildcards etc. */
81 ch == '?' || ch == '"' || ch == '<' ||
82 ch == '>' || ch == '|' || ch == '*'))
88 int ksmbd_validate_filename(char *filename)
94 if (!is_char_allowed(c)) {
95 ksmbd_debug(VFS, "File name validation failed: 0x%x\n", c);
103 static int ksmbd_validate_stream_name(char *stream_name)
105 while (*stream_name) {
106 char c = *stream_name;
109 if (c == '/' || c == ':' || c == '\\') {
110 pr_err("Stream name validation failed: %c\n", c);
118 int parse_stream_name(char *filename, char **stream_name, int *s_type)
125 filename = strsep(&s_name, ":");
126 ksmbd_debug(SMB, "filename : %s, streams : %s\n", filename, s_name);
127 if (strchr(s_name, ':')) {
128 stream_type = s_name;
129 s_name = strsep(&stream_type, ":");
131 rc = ksmbd_validate_stream_name(s_name);
137 ksmbd_debug(SMB, "stream name : %s, stream type : %s\n", s_name,
139 if (!strncasecmp("$data", stream_type, 5))
140 *s_type = DATA_STREAM;
141 else if (!strncasecmp("$index_allocation", stream_type, 17))
142 *s_type = DIR_STREAM;
147 *stream_name = s_name;
153 * convert_to_nt_pathname() - extract and return windows path string
154 * whose share directory prefix was removed from file path
155 * @share: ksmbd_share_config pointer
156 * @path: path to report
158 * Return : windows path string or error
161 char *convert_to_nt_pathname(struct ksmbd_share_config *share,
164 char *pathname, *ab_pathname, *nt_pathname;
165 int share_path_len = share->path_sz;
167 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
169 return ERR_PTR(-EACCES);
171 ab_pathname = d_path(path, pathname, PATH_MAX);
172 if (IS_ERR(ab_pathname)) {
173 nt_pathname = ERR_PTR(-EACCES);
177 if (strncmp(ab_pathname, share->path, share_path_len)) {
178 nt_pathname = ERR_PTR(-EACCES);
182 nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2, GFP_KERNEL);
184 nt_pathname = ERR_PTR(-ENOMEM);
187 if (ab_pathname[share_path_len] == '\0')
188 strcpy(nt_pathname, "/");
189 strcat(nt_pathname, &ab_pathname[share_path_len]);
191 ksmbd_conv_path_to_windows(nt_pathname);
198 int get_nlink(struct kstat *st)
203 if (S_ISDIR(st->mode))
209 void ksmbd_conv_path_to_unix(char *path)
211 strreplace(path, '\\', '/');
214 void ksmbd_strip_last_slash(char *path)
216 int len = strlen(path);
218 while (len && path[len - 1] == '/') {
219 path[len - 1] = '\0';
224 void ksmbd_conv_path_to_windows(char *path)
226 strreplace(path, '/', '\\');
230 * ksmbd_extract_sharename() - get share name from tree connect request
231 * @treename: buffer containing tree name and share name
233 * Return: share name on success, otherwise error
235 char *ksmbd_extract_sharename(char *treename)
237 char *name = treename;
239 char *pos = strrchr(name, '\\');
244 /* caller has to free the memory */
245 dst = kstrdup(name, GFP_KERNEL);
247 return ERR_PTR(-ENOMEM);
252 * convert_to_unix_name() - convert windows name to unix format
253 * @share: ksmbd_share_config pointer
254 * @name: file name that is relative to share
256 * Return: converted name on success, otherwise NULL
258 char *convert_to_unix_name(struct ksmbd_share_config *share, const char *name)
260 int no_slash = 0, name_len, path_len;
266 path_len = share->path_sz;
267 name_len = strlen(name);
268 new_name = kmalloc(path_len + name_len + 2, GFP_KERNEL);
272 memcpy(new_name, share->path, path_len);
273 if (new_name[path_len - 1] != '/') {
274 new_name[path_len] = '/';
278 memcpy(new_name + path_len + no_slash, name, name_len);
279 path_len += name_len + no_slash;
280 new_name[path_len] = 0x00;
284 char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info,
285 const struct nls_table *local_nls,
289 int sz = min(4 * d_info->name_len, PATH_MAX);
294 conv = kmalloc(sz, GFP_KERNEL);
299 *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name,
300 d_info->name_len, local_nls, 0);
303 /* We allocate buffer twice bigger than needed. */
304 conv[*conv_len] = 0x00;
305 conv[*conv_len + 1] = 0x00;
310 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
311 * into Unix UTC (based 1970-01-01, in seconds).
313 struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc)
315 struct timespec64 ts;
317 /* Subtract the NTFS time offset, then convert to 1s intervals. */
318 s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
322 * Unfortunately can not use normal 64 bit division on 32 bit arch, but
323 * the alternative, do_div, does not work with negative numbers so have
324 * to special case them
328 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
329 ts.tv_nsec = -ts.tv_nsec;
333 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
340 /* Convert the Unix UTC into NT UTC. */
341 inline u64 ksmbd_UnixTimeToNT(struct timespec64 t)
343 /* Convert to 100ns intervals and then add the NTFS time offset. */
344 return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET;
347 inline long long ksmbd_systime(void)
349 struct timespec64 ts;
351 ktime_get_real_ts64(&ts);
352 return ksmbd_UnixTimeToNT(ts);