1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
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 * @string: 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 * @filename : unix filename
156 * @sharepath: share path string
158 * Return : windows path string or error
161 char *convert_to_nt_pathname(char *filename)
165 if (strlen(filename) == 0)
168 ab_pathname = kstrdup(filename, GFP_KERNEL);
172 ksmbd_conv_path_to_windows(ab_pathname);
176 int get_nlink(struct kstat *st)
181 if (S_ISDIR(st->mode))
187 void ksmbd_conv_path_to_unix(char *path)
189 strreplace(path, '\\', '/');
192 void ksmbd_strip_last_slash(char *path)
194 int len = strlen(path);
196 while (len && path[len - 1] == '/') {
197 path[len - 1] = '\0';
202 void ksmbd_conv_path_to_windows(char *path)
204 strreplace(path, '/', '\\');
208 * ksmbd_extract_sharename() - get share name from tree connect request
209 * @treename: buffer containing tree name and share name
211 * Return: share name on success, otherwise error
213 char *ksmbd_extract_sharename(char *treename)
215 char *name = treename;
217 char *pos = strrchr(name, '\\');
222 /* caller has to free the memory */
223 dst = kstrdup(name, GFP_KERNEL);
225 return ERR_PTR(-ENOMEM);
230 * convert_to_unix_name() - convert windows name to unix format
231 * @path: name to be converted
232 * @tid: tree id of mathing share
234 * Return: converted name on success, otherwise NULL
236 char *convert_to_unix_name(struct ksmbd_share_config *share, const char *name)
238 int no_slash = 0, name_len, path_len;
244 path_len = share->path_sz;
245 name_len = strlen(name);
246 new_name = kmalloc(path_len + name_len + 2, GFP_KERNEL);
250 memcpy(new_name, share->path, path_len);
251 if (new_name[path_len - 1] != '/') {
252 new_name[path_len] = '/';
256 memcpy(new_name + path_len + no_slash, name, name_len);
257 path_len += name_len + no_slash;
258 new_name[path_len] = 0x00;
262 char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info,
263 const struct nls_table *local_nls,
267 int sz = min(4 * d_info->name_len, PATH_MAX);
272 conv = kmalloc(sz, GFP_KERNEL);
277 *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name,
278 d_info->name_len, local_nls, 0);
281 /* We allocate buffer twice bigger than needed. */
282 conv[*conv_len] = 0x00;
283 conv[*conv_len + 1] = 0x00;
288 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
289 * into Unix UTC (based 1970-01-01, in seconds).
291 struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc)
293 struct timespec64 ts;
295 /* Subtract the NTFS time offset, then convert to 1s intervals. */
296 s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
300 * Unfortunately can not use normal 64 bit division on 32 bit arch, but
301 * the alternative, do_div, does not work with negative numbers so have
302 * to special case them
306 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
307 ts.tv_nsec = -ts.tv_nsec;
311 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
318 /* Convert the Unix UTC into NT UTC. */
319 inline u64 ksmbd_UnixTimeToNT(struct timespec64 t)
321 /* Convert to 100ns intervals and then add the NTFS time offset. */
322 return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET;
325 inline long long ksmbd_systime(void)
327 struct timespec64 ts;
329 ktime_get_real_ts64(&ts);
330 return ksmbd_UnixTimeToNT(ts);