2 Unix SMB/CIFS implementation.
3 Filename utility functions.
4 Copyright (C) Tim Prouty 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * XXX: This is temporary and there should be no callers of this outside of
23 * this file once smb_filename is plumbed through all path based operations.
24 * The one legitimate caller currently is smb_fname_str_dbg(), which this
25 * could be made static for.
27 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx,
28 const struct smb_filename *smb_fname,
31 if (smb_fname->stream_name) {
32 /* stream_name must always be NULL if there is no stream. */
33 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
35 *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
36 smb_fname->stream_name);
38 *full_name = talloc_strdup(ctx, smb_fname->base_name);
42 return NT_STATUS_NO_MEMORY;
49 * There are actually legitimate callers of this such as functions that
50 * enumerate streams using the vfs_streaminfo interface and then want to
51 * operate on each stream.
53 struct smb_filename *synthetic_smb_fname(TALLOC_CTX *mem_ctx,
54 const char *base_name,
55 const char *stream_name,
56 const SMB_STRUCT_STAT *psbuf,
60 struct smb_filename smb_fname_loc = { 0, };
62 /* Setup the base_name/stream_name. */
63 smb_fname_loc.base_name = discard_const_p(char, base_name);
64 smb_fname_loc.stream_name = discard_const_p(char, stream_name);
65 smb_fname_loc.flags = flags;
66 smb_fname_loc.twrp = twrp;
68 /* Copy the psbuf if one was given. */
70 smb_fname_loc.st = *psbuf;
72 /* Let cp_smb_filename() do the heavy lifting. */
73 return cp_smb_filename(mem_ctx, &smb_fname_loc);
77 * Utility function used by VFS calls that must *NOT* operate
78 * on a stream filename, only the base_name.
80 struct smb_filename *cp_smb_filename_nostream(TALLOC_CTX *mem_ctx,
81 const struct smb_filename *smb_fname_in)
83 struct smb_filename *smb_fname = cp_smb_filename(mem_ctx,
85 if (smb_fname == NULL) {
88 TALLOC_FREE(smb_fname->stream_name);
93 * There are a few legitimate users of this.
95 struct smb_filename *synthetic_smb_fname_split(TALLOC_CTX *ctx,
99 char *stream_name = NULL;
100 char *base_name = NULL;
101 struct smb_filename *ret;
105 /* No stream name looked for. */
106 return synthetic_smb_fname(ctx,
111 SMB_FILENAME_POSIX_PATH);
114 ok = split_stream_filename(ctx,
122 ret = synthetic_smb_fname(ctx,
128 TALLOC_FREE(base_name);
129 TALLOC_FREE(stream_name);
134 * Return a string using the talloc_tos()
136 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
141 if (smb_fname == NULL) {
144 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
145 if (!NT_STATUS_IS_OK(status)) {
152 * Return a debug string of the path name of an fsp using the talloc_tos().
154 const char *fsp_str_dbg(const struct files_struct *fsp)
156 return smb_fname_str_dbg(fsp->fsp_name);
160 * Create a debug string for the fnum of an fsp.
162 * This is allocated to talloc_tos() or a string constant
163 * in certain corner cases. The returned string should
164 * hence not be free'd directly but only via the talloc stack.
166 const char *fsp_fnum_dbg(const struct files_struct *fsp)
171 return "fnum [fsp is NULL]";
174 if (fsp->fnum == FNUM_FIELD_INVALID) {
175 return "fnum [invalid value]";
178 str = talloc_asprintf(talloc_tos(), "fnum %llu",
179 (unsigned long long)fsp->fnum);
181 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
182 return "fnum [talloc failed!]";
188 struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
189 const struct smb_filename *in)
191 struct smb_filename *out;
193 size_t stream_len = 0;
196 /* stream_name must always be NULL if there is no stream. */
197 if (in->stream_name) {
198 SMB_ASSERT(in->stream_name[0] != '\0');
201 if (in->base_name != NULL) {
202 base_len = strlen(in->base_name) + 1;
205 if (in->stream_name != NULL) {
206 stream_len = strlen(in->stream_name) + 1;
210 out = talloc_pooled_object(mem_ctx, struct smb_filename,
211 num, stream_len + base_len);
218 * The following allocations cannot fail as we
219 * pre-allocated space for them in the out pooled
222 if (in->base_name != NULL) {
223 out->base_name = talloc_memdup(
224 out, in->base_name, base_len);
225 talloc_set_name_const(out->base_name,
228 if (in->stream_name != NULL) {
229 out->stream_name = talloc_memdup(
230 out, in->stream_name, stream_len);
231 talloc_set_name_const(out->stream_name,
234 out->flags = in->flags;
236 out->twrp = in->twrp;
241 * Return allocated parent directory and basename of path
243 * Note: if requesting name, it is returned as talloc child of the
244 * parent. Freeing the parent is thus sufficient to free both.
246 bool parent_smb_fname(TALLOC_CTX *mem_ctx,
247 const struct smb_filename *path,
248 struct smb_filename **_parent,
249 struct smb_filename **_name)
251 TALLOC_CTX *frame = talloc_stackframe();
252 struct smb_filename *parent = NULL;
253 struct smb_filename *name = NULL;
256 parent = cp_smb_filename(frame, path);
257 if (parent == NULL) {
261 TALLOC_FREE(parent->stream_name);
262 SET_STAT_INVALID(parent->st);
264 p = strrchr_m(parent->base_name, '/'); /* Find final '/', if any */
266 TALLOC_FREE(parent->base_name);
267 parent->base_name = talloc_strdup(parent, ".");
268 if (parent->base_name == NULL) {
279 *_parent = talloc_move(mem_ctx, &parent);
284 name = cp_smb_filename(frame, path);
289 TALLOC_FREE(name->base_name);
291 name->base_name = talloc_strdup(mem_ctx, p);
297 *_parent = talloc_move(mem_ctx, &parent);
298 *_name = talloc_move(*_parent, &name);
303 static void assert_valid_stream_smb_fname(const struct smb_filename *smb_fname)
305 /* stream_name must always be NULL if there is no stream. */
306 if (smb_fname->stream_name) {
307 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
310 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
311 SMB_ASSERT(smb_fname->stream_name == NULL);
315 /****************************************************************************
316 Simple check to determine if a smb_fname is a real named stream or the
318 ***************************************************************************/
320 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
322 assert_valid_stream_smb_fname(smb_fname);
324 if (smb_fname->stream_name == NULL) {
331 /****************************************************************************
332 Simple check to determine if a smb_fname is pointing to a normal file or
333 a named stream that is not the default stream "::$DATA".
338 foo:bar:$DATA -> true
340 ***************************************************************************/
342 bool is_named_stream(const struct smb_filename *smb_fname)
344 assert_valid_stream_smb_fname(smb_fname);
346 if (smb_fname->stream_name == NULL) {
350 if (strequal_m(smb_fname->stream_name, "::$DATA")) {
357 /****************************************************************************
358 Returns true if the filename's stream == "::$DATA"
359 ***************************************************************************/
360 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
362 assert_valid_stream_smb_fname(smb_fname);
364 if (smb_fname->stream_name == NULL) {
368 return strequal_m(smb_fname->stream_name, "::$DATA");
371 /****************************************************************************
372 Filter out Windows invalid EA names (list probed from Windows 2012).
373 ****************************************************************************/
375 static char bad_ea_name_chars[] = "\"*+,/:;<=>?[\\]|";
377 bool is_invalid_windows_ea_name(const char *name)
380 /* EA name is pulled as ascii so we can examine
381 individual bytes here. */
382 for (i = 0; name[i] != 0; i++) {
383 int val = (name[i] & 0xff);
384 if (val < ' ' || strchr(bad_ea_name_chars, val)) {
391 bool ea_list_has_invalid_name(struct ea_list *ea_list)
393 for (;ea_list; ea_list = ea_list->next) {
394 if (is_invalid_windows_ea_name(ea_list->ea.name)) {
401 /****************************************************************************
402 Split an incoming name into tallocd filename and stream components.
403 Returns true on success, false on out of memory.
404 ****************************************************************************/
406 bool split_stream_filename(TALLOC_CTX *ctx,
407 const char *filename_in,
409 char **streamname_out)
411 const char *stream_name = NULL;
412 char *stream_out = NULL;
413 char *file_out = NULL;
415 stream_name = strchr_m(filename_in, ':');
418 stream_out = talloc_strdup(ctx, stream_name);
419 if (stream_out == NULL) {
422 file_out = talloc_strndup(ctx,
424 PTR_DIFF(stream_name, filename_in));
426 file_out = talloc_strdup(ctx, filename_in);
429 if (file_out == NULL) {
430 TALLOC_FREE(stream_out);
435 *filename_out = file_out;
437 if (streamname_out) {
438 *streamname_out = stream_out;