summaryrefslogtreecommitdiff
path: root/source3/lib/filename_util.c
blob: 66c07001eba273b0424ad03f31cc8312040b0ff6 (plain)
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
/*
   Unix SMB/CIFS implementation.
   Filename utility functions.
   Copyright (C) Tim Prouty 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"

/**
 * XXX: This is temporary and there should be no callers of this outside of
 * this file once smb_filename is plumbed through all path based operations.
 * The one legitimate caller currently is smb_fname_str_dbg(), which this
 * could be made static for.
 */
NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx,
			       const struct smb_filename *smb_fname,
			       char **full_name)
{
	if (smb_fname->stream_name) {
		/* stream_name must always be NULL if there is no stream. */
		SMB_ASSERT(smb_fname->stream_name[0] != '\0');

		*full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
					     smb_fname->stream_name);
	} else {
		*full_name = talloc_strdup(ctx, smb_fname->base_name);
	}

	if (!*full_name) {
		return NT_STATUS_NO_MEMORY;
	}

	return NT_STATUS_OK;
}

/**
 * There are actually legitimate callers of this such as functions that
 * enumerate streams using the vfs_streaminfo interface and then want to
 * operate on each stream.
 */
struct smb_filename *synthetic_smb_fname(TALLOC_CTX *mem_ctx,
					 const char *base_name,
					 const char *stream_name,
					 const SMB_STRUCT_STAT *psbuf,
					 uint32_t flags)
{
	struct smb_filename smb_fname_loc = { 0, };

	/* Setup the base_name/stream_name. */
	smb_fname_loc.base_name = discard_const_p(char, base_name);
	smb_fname_loc.stream_name = discard_const_p(char, stream_name);
	smb_fname_loc.flags = flags;

	/* Copy the psbuf if one was given. */
	if (psbuf)
		smb_fname_loc.st = *psbuf;

	/* Let cp_smb_filename() do the heavy lifting. */
	return cp_smb_filename(mem_ctx, &smb_fname_loc);
}

/**
 * Utility function used by VFS calls that must *NOT* operate
 * on a stream filename, only the base_name.
 */
struct smb_filename *cp_smb_filename_nostream(TALLOC_CTX *mem_ctx,
					const struct smb_filename *smb_fname_in)
{
	struct smb_filename *smb_fname = cp_smb_filename(mem_ctx,
							smb_fname_in);
	if (smb_fname == NULL) {
		return NULL;
	}
	TALLOC_FREE(smb_fname->stream_name);
	return smb_fname;
}

/**
 * There are a few legitimate users of this.
 */
struct smb_filename *synthetic_smb_fname_split(TALLOC_CTX *ctx,
						const char *fname,
						bool posix_path)
{
	char *stream_name = NULL;
	char *base_name = NULL;
	struct smb_filename *ret;
	bool ok;

	if (posix_path) {
		/* No stream name looked for. */
		return synthetic_smb_fname(ctx,
				fname,
				NULL,
				NULL,
				SMB_FILENAME_POSIX_PATH);
	}

	ok = split_stream_filename(ctx,
				fname,
				&base_name,
				&stream_name);
	if (!ok) {
		return NULL;
	}

	ret = synthetic_smb_fname(ctx, base_name, stream_name, NULL, 0);
	TALLOC_FREE(base_name);
	TALLOC_FREE(stream_name);
	return ret;
}

/**
 * Return a string using the talloc_tos()
 */
const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
{
	char *fname = NULL;
	NTSTATUS status;

	if (smb_fname == NULL) {
		return "";
	}
	status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
	if (!NT_STATUS_IS_OK(status)) {
		return "";
	}
	return fname;
}

/**
 * Return a debug string of the path name of an fsp using the talloc_tos().
 */
const char *fsp_str_dbg(const struct files_struct *fsp)
{
	return smb_fname_str_dbg(fsp->fsp_name);
}

/**
 * Create a debug string for the fnum of an fsp.
 *
 * This is allocated to talloc_tos() or a string constant
 * in certain corner cases. The returned string should
 * hence not be free'd directly but only via the talloc stack.
 */
const char *fsp_fnum_dbg(const struct files_struct *fsp)
{
	char *str;

	if (fsp == NULL) {
		return "fnum [fsp is NULL]";
	}

	if (fsp->fnum == FNUM_FIELD_INVALID) {
		return "fnum [invalid value]";
	}

	str = talloc_asprintf(talloc_tos(), "fnum %llu",
			      (unsigned long long)fsp->fnum);
	if (str == NULL) {
		DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
		return "fnum [talloc failed!]";
	}

	return str;
}

struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
				     const struct smb_filename *in)
{
	struct smb_filename *out;
	size_t base_len = 0;
	size_t stream_len = 0;
	size_t lcomp_len = 0;
	int num = 0;

	/* stream_name must always be NULL if there is no stream. */
	if (in->stream_name) {
		SMB_ASSERT(in->stream_name[0] != '\0');
	}

	if (in->base_name != NULL) {
		base_len = strlen(in->base_name) + 1;
		num += 1;
	}
	if (in->stream_name != NULL) {
		stream_len = strlen(in->stream_name) + 1;
		num += 1;
	}
	if (in->original_lcomp != NULL) {
		lcomp_len = strlen(in->original_lcomp) + 1;
		num += 1;
	}

	out = talloc_pooled_object(mem_ctx, struct smb_filename,
				num, stream_len + base_len + lcomp_len);
	if (out == NULL) {
		return NULL;
	}
	ZERO_STRUCTP(out);

	/*
	 * The following allocations cannot fail as we
	 * pre-allocated space for them in the out pooled
	 * object.
	 */
	if (in->base_name != NULL) {
		out->base_name = talloc_memdup(
				out, in->base_name, base_len);
		talloc_set_name_const(out->base_name,
				      out->base_name);
	}
	if (in->stream_name != NULL) {
		out->stream_name = talloc_memdup(
				out, in->stream_name, stream_len);
		talloc_set_name_const(out->stream_name,
				      out->stream_name);
	}
	if (in->original_lcomp != NULL) {
		out->original_lcomp = talloc_memdup(
				out, in->original_lcomp, lcomp_len);
		talloc_set_name_const(out->original_lcomp,
				      out->original_lcomp);
	}
	out->flags = in->flags;
	out->st = in->st;
	return out;
}

static void assert_valid_stream_smb_fname(const struct smb_filename *smb_fname)
{
	/* stream_name must always be NULL if there is no stream. */
	if (smb_fname->stream_name) {
		SMB_ASSERT(smb_fname->stream_name[0] != '\0');
	}

	if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
		SMB_ASSERT(smb_fname->stream_name == NULL);
	}
}

/****************************************************************************
 Simple check to determine if a smb_fname is a real named stream or the
 default stream.
 ***************************************************************************/

bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
{
	assert_valid_stream_smb_fname(smb_fname);

	if (smb_fname->stream_name == NULL) {
		return false;
	}

	return true;
}

/****************************************************************************
 Simple check to determine if a smb_fname is pointing to a normal file or
 a named stream that is not the default stream "::$DATA".

  foo           -> false
  foo::$DATA    -> false
  foo:bar       -> true
  foo:bar:$DATA -> true

 ***************************************************************************/

bool is_named_stream(const struct smb_filename *smb_fname)
{
	assert_valid_stream_smb_fname(smb_fname);

	if (smb_fname->stream_name == NULL) {
		return false;
	}

	if (strequal_m(smb_fname->stream_name, "::$DATA")) {
		return false;
	}

	return true;
}

/****************************************************************************
 Returns true if the filename's stream == "::$DATA"
 ***************************************************************************/
bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
{
	assert_valid_stream_smb_fname(smb_fname);

	if (smb_fname->stream_name == NULL) {
		return false;
	}

	return strequal_m(smb_fname->stream_name, "::$DATA");
}

/****************************************************************************
 Filter out Windows invalid EA names (list probed from Windows 2012).
****************************************************************************/

static char bad_ea_name_chars[] = "\"*+,/:;<=>?[\\]|";

bool is_invalid_windows_ea_name(const char *name)
{
	int i;
	/* EA name is pulled as ascii so we can examine
	   individual bytes here. */
	for (i = 0; name[i] != 0; i++) {
		int val = (name[i] & 0xff);
		if (val < ' ' || strchr(bad_ea_name_chars, val)) {
			return true;
		}
	}
	return false;
}

bool ea_list_has_invalid_name(struct ea_list *ea_list)
{
	for (;ea_list; ea_list = ea_list->next) {
		if (is_invalid_windows_ea_name(ea_list->ea.name)) {
			return true;
		}
	}
	return false;
}

/****************************************************************************
 Split an incoming name into tallocd filename and stream components.
 Returns true on success, false on out of memory.
****************************************************************************/

bool split_stream_filename(TALLOC_CTX *ctx,
				const char *filename_in,
				char **filename_out,
				char **streamname_out)
{
	const char *stream_name = NULL;
	char *stream_out = NULL;
	char *file_out = NULL;

	stream_name = strchr_m(filename_in, ':');

	if (stream_name) {
		stream_out = talloc_strdup(ctx, stream_name);
		if (stream_out == NULL) {
			return false;
		}
		file_out = talloc_strndup(ctx,
					filename_in,
					PTR_DIFF(stream_name, filename_in));
	} else {
		file_out = talloc_strdup(ctx, filename_in);
	}

	if (file_out == NULL) {
		TALLOC_FREE(stream_out);
		return false;
	}

	if (filename_out) {
		*filename_out = file_out;
	}
	if (streamname_out) {
		*streamname_out = stream_out;
	}
	return true;
}