gssapi: check for gss_acquire_cred_from
[Samba.git] / source3 / lib / filename_util.c
blob3983aaa0eca857e928d452e4e10b6c0be5873e26
1 /*
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/>.
19 #include "includes.h"
21 /**
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,
29 char **full_name)
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);
37 } else {
38 *full_name = talloc_strdup(ctx, smb_fname->base_name);
41 if (!*full_name) {
42 return NT_STATUS_NO_MEMORY;
45 return NT_STATUS_OK;
48 /**
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,
57 uint32_t flags)
59 struct smb_filename smb_fname_loc = { 0, };
61 /* Setup the base_name/stream_name. */
62 smb_fname_loc.base_name = discard_const_p(char, base_name);
63 smb_fname_loc.stream_name = discard_const_p(char, stream_name);
64 smb_fname_loc.flags = flags;
66 /* Copy the psbuf if one was given. */
67 if (psbuf)
68 smb_fname_loc.st = *psbuf;
70 /* Let cp_smb_filename() do the heavy lifting. */
71 return cp_smb_filename(mem_ctx, &smb_fname_loc);
74 /**
75 * There are a few legitimate users of this.
77 struct smb_filename *synthetic_smb_fname_split(TALLOC_CTX *ctx,
78 const char *fname,
79 bool posix_path)
81 char *stream_name = NULL;
82 char *base_name = NULL;
83 struct smb_filename *ret;
84 bool ok;
86 if (posix_path) {
87 /* No stream name looked for. */
88 return synthetic_smb_fname(ctx,
89 fname,
90 NULL,
91 NULL,
92 SMB_FILENAME_POSIX_PATH);
95 ok = split_stream_filename(ctx,
96 fname,
97 &base_name,
98 &stream_name);
99 if (!ok) {
100 return NULL;
103 ret = synthetic_smb_fname(ctx, base_name, stream_name, NULL, 0);
104 TALLOC_FREE(base_name);
105 TALLOC_FREE(stream_name);
106 return ret;
110 * Return a string using the talloc_tos()
112 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
114 char *fname = NULL;
115 NTSTATUS status;
117 if (smb_fname == NULL) {
118 return "";
120 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
121 if (!NT_STATUS_IS_OK(status)) {
122 return "";
124 return fname;
128 * Return a debug string of the path name of an fsp using the talloc_tos().
130 const char *fsp_str_dbg(const struct files_struct *fsp)
132 return smb_fname_str_dbg(fsp->fsp_name);
136 * Create a debug string for the fnum of an fsp.
138 * This is allocated to talloc_tos() or a string constant
139 * in certain corner cases. The returned string should
140 * hence not be free'd directly but only via the talloc stack.
142 const char *fsp_fnum_dbg(const struct files_struct *fsp)
144 char *str;
146 if (fsp == NULL) {
147 return "fnum [fsp is NULL]";
150 if (fsp->fnum == FNUM_FIELD_INVALID) {
151 return "fnum [invalid value]";
154 str = talloc_asprintf(talloc_tos(), "fnum %llu",
155 (unsigned long long)fsp->fnum);
156 if (str == NULL) {
157 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
158 return "fnum [talloc failed!]";
161 return str;
164 struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
165 const struct smb_filename *in)
167 struct smb_filename *out;
168 size_t base_len = 0;
169 size_t stream_len = 0;
170 size_t lcomp_len = 0;
171 int num = 0;
173 /* stream_name must always be NULL if there is no stream. */
174 if (in->stream_name) {
175 SMB_ASSERT(in->stream_name[0] != '\0');
178 if (in->base_name != NULL) {
179 base_len = strlen(in->base_name) + 1;
180 num += 1;
182 if (in->stream_name != NULL) {
183 stream_len = strlen(in->stream_name) + 1;
184 num += 1;
186 if (in->original_lcomp != NULL) {
187 lcomp_len = strlen(in->original_lcomp) + 1;
188 num += 1;
191 out = talloc_pooled_object(mem_ctx, struct smb_filename,
192 num, stream_len + base_len + lcomp_len);
193 if (out == NULL) {
194 return NULL;
196 ZERO_STRUCTP(out);
199 * The following allocations cannot fail as we
200 * pre-allocated space for them in the out pooled
201 * object.
203 if (in->base_name != NULL) {
204 out->base_name = talloc_memdup(
205 out, in->base_name, base_len);
206 talloc_set_name_const(out->base_name,
207 out->base_name);
209 if (in->stream_name != NULL) {
210 out->stream_name = talloc_memdup(
211 out, in->stream_name, stream_len);
212 talloc_set_name_const(out->stream_name,
213 out->stream_name);
215 if (in->original_lcomp != NULL) {
216 out->original_lcomp = talloc_memdup(
217 out, in->original_lcomp, lcomp_len);
218 talloc_set_name_const(out->original_lcomp,
219 out->original_lcomp);
221 out->flags = in->flags;
222 out->st = in->st;
223 return out;
226 /****************************************************************************
227 Simple check to determine if the filename is a stream.
228 ***************************************************************************/
229 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
231 /* stream_name must always be NULL if there is no stream. */
232 if (smb_fname->stream_name) {
233 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
236 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
237 return false;
240 return smb_fname->stream_name != NULL;
243 /****************************************************************************
244 Returns true if the filename's stream == "::$DATA"
245 ***************************************************************************/
246 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
248 if (!is_ntfs_stream_smb_fname(smb_fname)) {
249 return false;
252 return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;
255 /****************************************************************************
256 Filter out Windows invalid EA names (list probed from Windows 2012).
257 ****************************************************************************/
259 static char bad_ea_name_chars[] = "\"*+,/:;<=>?[\\]|";
261 bool is_invalid_windows_ea_name(const char *name)
263 int i;
264 /* EA name is pulled as ascii so we can examine
265 individual bytes here. */
266 for (i = 0; name[i] != 0; i++) {
267 int val = (name[i] & 0xff);
268 if (val < ' ' || strchr(bad_ea_name_chars, val)) {
269 return true;
272 return false;
275 bool ea_list_has_invalid_name(struct ea_list *ea_list)
277 for (;ea_list; ea_list = ea_list->next) {
278 if (is_invalid_windows_ea_name(ea_list->ea.name)) {
279 return true;
282 return false;
285 /****************************************************************************
286 Split an incoming name into tallocd filename and stream components.
287 Returns true on success, false on out of memory.
288 ****************************************************************************/
290 bool split_stream_filename(TALLOC_CTX *ctx,
291 const char *filename_in,
292 char **filename_out,
293 char **streamname_out)
295 const char *stream_name = NULL;
296 char *stream_out = NULL;
297 char *file_out = NULL;
299 stream_name = strchr_m(filename_in, ':');
301 if (stream_name) {
302 stream_out = talloc_strdup(ctx, stream_name);
303 if (stream_out == NULL) {
304 return false;
306 file_out = talloc_strndup(ctx,
307 filename_in,
308 PTR_DIFF(stream_name, filename_in));
309 } else {
310 file_out = talloc_strdup(ctx, filename_in);
313 if (file_out == NULL) {
314 TALLOC_FREE(stream_out);
315 return false;
318 if (filename_out) {
319 *filename_out = file_out;
321 if (streamname_out) {
322 *streamname_out = stream_out;
324 return true;