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>
10 #include <linux/unicode.h>
13 #include "smb_common.h"
14 #include "connection.h"
17 #include "mgmt/share_config.h"
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
24 * @str: string to compare with a pattern
26 * @pattern: pattern string which might include wildcard '*' and '?'
28 * Return: 0 if pattern matched with the string, otherwise non zero value
30 int match_pattern(const char *str, size_t len, const char *pattern)
33 const char *p = pattern;
51 if (tolower(*s) == tolower(*p)) {
72 * is_char_allowed() - check for valid character
73 * @ch: input character to be checked
75 * Return: 1 if char is allowed, otherwise 0
77 static inline int is_char_allowed(char ch)
79 /* check for control chars, wildcards etc. */
82 ch == '?' || ch == '"' || ch == '<' ||
83 ch == '>' || ch == '|' || ch == '*'))
89 int ksmbd_validate_filename(char *filename)
95 if (!is_char_allowed(c)) {
96 ksmbd_debug(VFS, "File name validation failed: 0x%x\n", c);
104 static int ksmbd_validate_stream_name(char *stream_name)
106 while (*stream_name) {
107 char c = *stream_name;
110 if (c == '/' || c == ':' || c == '\\') {
111 pr_err("Stream name validation failed: %c\n", c);
119 int parse_stream_name(char *filename, char **stream_name, int *s_type)
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, ":");
132 rc = ksmbd_validate_stream_name(s_name);
138 ksmbd_debug(SMB, "stream name : %s, stream type : %s\n", s_name,
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;
148 *stream_name = s_name;
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
159 * Return : windows path string or error
162 char *convert_to_nt_pathname(struct ksmbd_share_config *share,
163 const struct path *path)
165 char *pathname, *ab_pathname, *nt_pathname;
166 int share_path_len = share->path_sz;
168 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
170 return ERR_PTR(-EACCES);
172 ab_pathname = d_path(path, pathname, PATH_MAX);
173 if (IS_ERR(ab_pathname)) {
174 nt_pathname = ERR_PTR(-EACCES);
178 if (strncmp(ab_pathname, share->path, share_path_len)) {
179 nt_pathname = ERR_PTR(-EACCES);
183 nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2, GFP_KERNEL);
185 nt_pathname = ERR_PTR(-ENOMEM);
188 if (ab_pathname[share_path_len] == '\0')
189 strcpy(nt_pathname, "/");
190 strcat(nt_pathname, &ab_pathname[share_path_len]);
192 ksmbd_conv_path_to_windows(nt_pathname);
199 int get_nlink(struct kstat *st)
204 if (S_ISDIR(st->mode))
210 void ksmbd_conv_path_to_unix(char *path)
212 strreplace(path, '\\', '/');
215 void ksmbd_strip_last_slash(char *path)
217 int len = strlen(path);
219 while (len && path[len - 1] == '/') {
220 path[len - 1] = '\0';
225 void ksmbd_conv_path_to_windows(char *path)
227 strreplace(path, '/', '\\');
230 char *ksmbd_casefold_sharename(struct unicode_map *um, const char *name)
235 cf_name = kzalloc(KSMBD_REQ_MAX_SHARE_NAME, GFP_KERNEL);
237 return ERR_PTR(-ENOMEM);
239 if (IS_ENABLED(CONFIG_UNICODE) && um) {
240 const struct qstr q_name = {.name = name, .len = strlen(name)};
242 cf_len = utf8_casefold(um, &q_name, cf_name,
243 KSMBD_REQ_MAX_SHARE_NAME);
251 cf_len = strscpy(cf_name, name, KSMBD_REQ_MAX_SHARE_NAME);
254 return ERR_PTR(-E2BIG);
257 for (; *cf_name; ++cf_name)
258 *cf_name = isascii(*cf_name) ? tolower(*cf_name) : *cf_name;
259 return cf_name - cf_len;
263 * ksmbd_extract_sharename() - get share name from tree connect request
264 * @treename: buffer containing tree name and share name
266 * Return: share name on success, otherwise error
268 char *ksmbd_extract_sharename(struct unicode_map *um, const char *treename)
270 const char *name = treename, *pos = strrchr(name, '\\');
275 /* caller has to free the memory */
276 return ksmbd_casefold_sharename(um, name);
280 * convert_to_unix_name() - convert windows name to unix format
281 * @share: ksmbd_share_config pointer
282 * @name: file name that is relative to share
284 * Return: converted name on success, otherwise NULL
286 char *convert_to_unix_name(struct ksmbd_share_config *share, const char *name)
288 int no_slash = 0, name_len, path_len;
294 path_len = share->path_sz;
295 name_len = strlen(name);
296 new_name = kmalloc(path_len + name_len + 2, GFP_KERNEL);
300 memcpy(new_name, share->path, path_len);
301 if (new_name[path_len - 1] != '/') {
302 new_name[path_len] = '/';
306 memcpy(new_name + path_len + no_slash, name, name_len);
307 path_len += name_len + no_slash;
308 new_name[path_len] = 0x00;
312 char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info,
313 const struct nls_table *local_nls,
317 int sz = min(4 * d_info->name_len, PATH_MAX);
322 conv = kmalloc(sz, GFP_KERNEL);
327 *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name,
328 d_info->name_len, local_nls, 0);
331 /* We allocate buffer twice bigger than needed. */
332 conv[*conv_len] = 0x00;
333 conv[*conv_len + 1] = 0x00;
338 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
339 * into Unix UTC (based 1970-01-01, in seconds).
341 struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc)
343 struct timespec64 ts;
345 /* Subtract the NTFS time offset, then convert to 1s intervals. */
346 s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
350 * Unfortunately can not use normal 64 bit division on 32 bit arch, but
351 * the alternative, do_div, does not work with negative numbers so have
352 * to special case them
356 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
357 ts.tv_nsec = -ts.tv_nsec;
361 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
368 /* Convert the Unix UTC into NT UTC. */
369 inline u64 ksmbd_UnixTimeToNT(struct timespec64 t)
371 /* Convert to 100ns intervals and then add the NTFS time offset. */
372 return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET;
375 inline long long ksmbd_systime(void)
377 struct timespec64 ts;
379 ktime_get_real_ts64(&ts);
380 return ksmbd_UnixTimeToNT(ts);