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 * @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) {
166 ab_pathname = kmalloc(2, GFP_KERNEL);
167 ab_pathname[0] = '\\';
168 ab_pathname[1] = '\0';
170 ab_pathname = kstrdup(filename, GFP_KERNEL);
174 ksmbd_conv_path_to_windows(ab_pathname);
179 int get_nlink(struct kstat *st)
184 if (S_ISDIR(st->mode))
190 void ksmbd_conv_path_to_unix(char *path)
192 strreplace(path, '\\', '/');
195 void ksmbd_strip_last_slash(char *path)
197 int len = strlen(path);
199 while (len && path[len - 1] == '/') {
200 path[len - 1] = '\0';
205 void ksmbd_conv_path_to_windows(char *path)
207 strreplace(path, '/', '\\');
211 * ksmbd_extract_sharename() - get share name from tree connect request
212 * @treename: buffer containing tree name and share name
214 * Return: share name on success, otherwise error
216 char *ksmbd_extract_sharename(char *treename)
218 char *name = treename;
220 char *pos = strrchr(name, '\\');
225 /* caller has to free the memory */
226 dst = kstrdup(name, GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
233 * convert_to_unix_name() - convert windows name to unix format
234 * @path: name to be converted
235 * @tid: tree id of mathing share
237 * Return: converted name on success, otherwise NULL
239 char *convert_to_unix_name(struct ksmbd_share_config *share, const char *name)
241 int no_slash = 0, name_len, path_len;
247 path_len = share->path_sz;
248 name_len = strlen(name);
249 new_name = kmalloc(path_len + name_len + 2, GFP_KERNEL);
253 memcpy(new_name, share->path, path_len);
254 if (new_name[path_len - 1] != '/') {
255 new_name[path_len] = '/';
259 memcpy(new_name + path_len + no_slash, name, name_len);
260 path_len += name_len + no_slash;
261 new_name[path_len] = 0x00;
265 char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info,
266 const struct nls_table *local_nls,
270 int sz = min(4 * d_info->name_len, PATH_MAX);
275 conv = kmalloc(sz, GFP_KERNEL);
280 *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name,
281 d_info->name_len, local_nls, 0);
284 /* We allocate buffer twice bigger than needed. */
285 conv[*conv_len] = 0x00;
286 conv[*conv_len + 1] = 0x00;
291 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
292 * into Unix UTC (based 1970-01-01, in seconds).
294 struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc)
296 struct timespec64 ts;
298 /* Subtract the NTFS time offset, then convert to 1s intervals. */
299 s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
303 * Unfortunately can not use normal 64 bit division on 32 bit arch, but
304 * the alternative, do_div, does not work with negative numbers so have
305 * to special case them
309 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
310 ts.tv_nsec = -ts.tv_nsec;
314 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
321 /* Convert the Unix UTC into NT UTC. */
322 inline u64 ksmbd_UnixTimeToNT(struct timespec64 t)
324 /* Convert to 100ns intervals and then add the NTFS time offset. */
325 return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET;
328 inline long long ksmbd_systime(void)
330 struct timespec64 ts;
332 ktime_get_real_ts64(&ts);
333 return ksmbd_UnixTimeToNT(ts);