forked from namjaejeon/ksmbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
411 lines (350 loc) · 8.97 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2016 Namjae Jeon <[email protected]>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/xattr.h>
#include <linux/fs.h>
#include "misc.h"
#include "smb_common.h"
#include "transport_tcp.h"
#include "vfs.h"
#include "mgmt/share_config.h"
/* @FIXME rework this code */
/**
* dump_smb_msg() - print smb packet for debugging
* @buf: smb packet
* @smb_buf_length: packet print length
*
*/
void dump_smb_msg(void *buf, int smb_buf_length)
{
int i, j;
char debug_line[33];
unsigned char *buffer = buf;
if (likely(cifsd_debugging != 2))
return;
for (i = 0, j = 0; i < smb_buf_length; i++, j++) {
if (i % 16 == 0) {
/* have reached the beginning of line */
pr_err("%04x ", i);
pr_cont("| ");
j = 0;
}
pr_cont("%02x ", buffer[i]);
debug_line[2 * j] = ' ';
if (isprint(buffer[i]))
debug_line[1 + (2 * j)] = buffer[i];
else
debug_line[1 + (2 * j)] = '_';
if (i % 16 == 15) {
/* reached end of line, time to print ascii */
debug_line[32] = 0;
pr_cont(" | %s\n", debug_line);
}
}
for (; j < 16; j++) {
pr_cont(" ");
debug_line[2 * j] = ' ';
debug_line[1 + (2 * j)] = ' ';
}
pr_cont(" | %s\n", debug_line);
return;
}
/**
* pattern_cmp() - compare a string with a pattern which might include
* wildcard '*' and '?'
* TODO : implement consideration about DOS_DOT, DOS_QM and DOS_STAR
*
* @string: string to compare with a pattern
* @pattern: pattern string which might include wildcard '*' and '?'
*
* Return: 0 if pattern matched with the string, otherwise non zero value
*/
int pattern_cmp(const char *string, const char *pattern)
{
const char *cp = NULL;
const char *mp = NULL;
int diff;
/* handle plain characters and '?' */
while ((*string) && (*pattern != '*')) {
diff = strncasecmp(pattern, string, 1);
if (diff && (*pattern != '?'))
return diff;
pattern++;
string++;
}
/* handle '*' wildcard */
while (*string) {
if (*pattern == '*') {
/*
* if the last char of a pattern is '*',
* any string matches with the pattern
*/
if (!*++pattern)
return 0;
mp = pattern;
cp = string + 1;
} else if (!strncasecmp(pattern, string, 1)
|| (*pattern == '?')) {
/* ? is matched with any "one" char */
pattern++;
string++;
} else {
pattern = mp;
string = cp++;
}
}
/* handle remaining '*' */
while (*pattern == '*')
pattern++;
return *pattern;
}
/**
* is_matched() - compare a file name with an expression which might
* include wildcards
*
* @fname: file name to compare with an expression
* @exp: an expression which might include wildcard '*' and '?'
*
* Return: true if fname and exp are matched, otherwise false
*/
bool is_matched(const char *fname, const char *exp)
{
/* optimization to avoid pattern compare */
if (!*fname && *exp)
return false;
else if (*fname && !*exp)
return false;
else if (!*fname && !*exp)
return true;
else if (*exp == '*' && strlen(exp) == 1)
return true;
if (pattern_cmp(fname, exp))
return false;
else
return true;
}
/*
* is_char_allowed() - check for valid character
* @ch: input character to be checked
*
* Return: 1 if char is allowed, otherwise 0
*/
static inline int is_char_allowed(char *ch)
{
/* check for control chars, wildcards etc. */
if (!(*ch & 0x80) &&
(*ch <= 0x1f ||
*ch == '?' || *ch == '"' || *ch == '<' ||
*ch == '>' || *ch == '|' || *ch == '*'))
return 0;
return 1;
}
int check_invalid_char(char *filename)
{
int len, i, rc = 0;
len = strlen(filename);
/* Check invalid character in stream name */
for (i = 0; i < len; i++) {
if (!is_char_allowed(&filename[i])) {
cifsd_err("found invalid character : 0x%x\n",
filename[i]);
rc = -ENOENT;
break;
}
}
return rc;
}
int check_invalid_char_stream(char *stream_name)
{
int len, i, rc = 0;
len = strlen(stream_name);
/* Check invalid character in stream name */
for (i = 0; i < len; i++) {
if (stream_name[i] == '/' || stream_name[i] == ':' ||
stream_name[i] == '\\') {
cifsd_err("found invalid character : %c\n",
stream_name[i]);
rc = -ENOENT;
break;
}
}
return rc;
}
int parse_stream_name(char *filename, char **stream_name, int *s_type)
{
char *stream_type;
char *s_name;
int rc = 0;
s_name = filename;
filename = strsep(&s_name, ":");
cifsd_debug("filename : %s, streams : %s\n", filename, s_name);
if (strchr(s_name, ':')) {
stream_type = s_name;
s_name = strsep(&stream_type, ":");
rc = check_invalid_char_stream(s_name);
if (rc < 0) {
rc = -ENOENT;
goto out;
}
cifsd_debug("stream name : %s, stream type : %s\n", s_name,
stream_type);
if (!strncasecmp("$data", stream_type, 5))
*s_type = DATA_STREAM;
else if (!strncasecmp("$index_allocation", stream_type, 17))
*s_type = DIR_STREAM;
else
rc = -ENOENT;
}
*stream_name = s_name;
out:
return rc;
}
/**
* convert_to_nt_pathname() - extract and return windows path string
* whose share directory prefix was removed from file path
* @filename : unix filename
* @sharepath: share path string
*
* Return : windows path string or error
*/
char *convert_to_nt_pathname(char *filename, char *sharepath)
{
char *ab_pathname;
int len;
ab_pathname = kmalloc(strlen(filename), GFP_KERNEL);
if (!ab_pathname)
return NULL;
ab_pathname[0] = '\\';
ab_pathname[1] = '\0';
len = strlen(sharepath);
if (!strncmp(filename, sharepath, len) && strlen(filename) != len) {
strcpy(ab_pathname, &filename[len]);
convert_delimiter(ab_pathname, 1);
}
return ab_pathname;
}
int get_nlink(struct kstat *st)
{
int nlink;
nlink = st->nlink;
if (S_ISDIR(st->mode))
nlink--;
return nlink;
}
/**
* convert_delimiter() - convert windows path to unix format or unix format
* to windos path
* @path: path to be converted
* @flags: 1 is to convert windows, 2 is to convert unix
*
*/
void convert_delimiter(char *path, int flags)
{
char *pos = path;
if (flags == 1)
while ((pos = strchr(pos, '/')))
*pos = '\\';
else
while ((pos = strchr(pos, '\\')))
*pos = '/';
}
/**
* extract_sharename() - get share name from tree connect request
* @treename: buffer containing tree name and share name
*
* Return: share name on success, otherwise error
*/
char *extract_sharename(char *treename)
{
int len;
char *dst;
/* skip double chars at the beginning */
while (strchr(treename, '\\'))
strsep(&treename, "\\");
len = strlen(treename);
/* caller has to free the memory */
dst = kstrndup(treename, len, GFP_KERNEL);
if (!dst)
return ERR_PTR(-ENOMEM);
return dst;
}
/**
* convert_to_unix_name() - convert windows name to unix format
* @path: name to be converted
* @tid: tree id of mathing share
*
* Return: converted name on success, otherwise NULL
*/
char *convert_to_unix_name(struct cifsd_share_config *share, char *name)
{
int len;
char *new_name;
len = strlen(share->path);
len += strlen(name);
/* for '/' needed for smb2
* as '/' is not present in beginning of name*/
if (name[0] != '/')
len++;
/* 1 extra for NULL byte */
cifsd_debug("new_name len = %d\n", len);
new_name = kmalloc(len + 1, GFP_KERNEL);
if (new_name == NULL) {
cifsd_debug("Failed to allocate memory\n");
return new_name;
}
memcpy(new_name, share->path, strlen(share->path));
if (name[0] != '/') {
memset(new_name + strlen(share->path), '/', 1);
memcpy(new_name + strlen(share->path) + 1, name, strlen(name));
} else
memcpy(new_name + strlen(share->path), name, strlen(name));
*(new_name + len) = '\0';
return new_name;
}
/**
* convname_updatenextoffset() - convert name to UTF, update next_entry_offset
* @namestr: source filename buffer
* @len: source buffer length
* @size: used buffer size
* @local_nls code page table
* @name_len: file name length after conversion
* @next_entry_offset: offset of dentry
* @buf_len: response buffer length
* @data_count: used response buffer size
* @no_namelen_field: flag which shows if a namelen field flag exist
*
* Return: return error if next entry could not fit in current response
* buffer, otherwise return encode buffer.
*/
char *convname_updatenextoffset(char *namestr, int len, int size,
const struct nls_table *local_nls, int *name_len,
int *next_entry_offset, int *buf_len, int *data_count,
int alignment, bool no_namelen_field)
{
char *enc_buf;
enc_buf = kmalloc(PATH_MAX, GFP_KERNEL);
if (!enc_buf)
return NULL;
*name_len = smbConvertToUTF16((__le16 *)enc_buf,
namestr, len, local_nls, 0);
*name_len *= 2;
if (no_namelen_field) {
enc_buf[*name_len] = '\0';
enc_buf[*name_len+1] = '\0';
*name_len += 2;
}
*next_entry_offset = (size - 1 + *name_len + alignment) & ~alignment;
if (*next_entry_offset > *buf_len) {
cifsd_debug("buf_len : %d next_entry_offset : %d"
" data_count : %d\n", *buf_len,
*next_entry_offset, *data_count);
*buf_len = -1;
kfree(enc_buf);
return NULL;
}
return enc_buf;
}