s3:lib: Rewrite synthetic_smb_fname_split() to use split_stream_filename().
[amitay/samba.git] / source3 / lib / filename_util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Filename utility functions.
4    Copyright (C) Tim Prouty 2009
5
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.
10
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.
15
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/>.
18 */
19 #include "includes.h"
20
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.
26  */
27 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx,
28                                const struct smb_filename *smb_fname,
29                                char **full_name)
30 {
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');
34
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);
39         }
40
41         if (!*full_name) {
42                 return NT_STATUS_NO_MEMORY;
43         }
44
45         return NT_STATUS_OK;
46 }
47
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.
52  */
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 {
58         struct smb_filename smb_fname_loc = { 0, };
59
60         /* Setup the base_name/stream_name. */
61         smb_fname_loc.base_name = discard_const_p(char, base_name);
62         smb_fname_loc.stream_name = discard_const_p(char, stream_name);
63
64         /* Copy the psbuf if one was given. */
65         if (psbuf)
66                 smb_fname_loc.st = *psbuf;
67
68         /* Let cp_smb_filename() do the heavy lifting. */
69         return cp_smb_filename(mem_ctx, &smb_fname_loc);
70 }
71
72 /**
73  * There are a few legitimate users of this.
74  */
75 struct smb_filename *synthetic_smb_fname_split(TALLOC_CTX *ctx,
76                                                const char *fname,
77                                                const SMB_STRUCT_STAT *psbuf)
78 {
79         char *stream_name = NULL;
80         char *base_name = NULL;
81         struct smb_filename *ret;
82         bool ok;
83
84         if (lp_posix_pathnames()) {
85                 /* No stream name looked for. */
86                 return synthetic_smb_fname(ctx, fname, NULL, psbuf);
87         }
88
89         ok = split_stream_filename(ctx,
90                                 fname,
91                                 &base_name,
92                                 &stream_name);
93         if (!ok) {
94                 return NULL;
95         }
96
97         ret = synthetic_smb_fname(ctx, base_name, stream_name, psbuf);
98         TALLOC_FREE(base_name);
99         TALLOC_FREE(stream_name);
100         return ret;
101 }
102
103 /**
104  * Return a string using the talloc_tos()
105  */
106 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
107 {
108         char *fname = NULL;
109         NTSTATUS status;
110
111         if (smb_fname == NULL) {
112                 return "";
113         }
114         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
115         if (!NT_STATUS_IS_OK(status)) {
116                 return "";
117         }
118         return fname;
119 }
120
121 /**
122  * Return a debug string of the path name of an fsp using the talloc_tos().
123  */
124 const char *fsp_str_dbg(const struct files_struct *fsp)
125 {
126         return smb_fname_str_dbg(fsp->fsp_name);
127 }
128
129 /**
130  * Create a debug string for the fnum of an fsp.
131  *
132  * This is allocated to talloc_tos() or a string constant
133  * in certain corner cases. The returned string should
134  * hence not be free'd directly but only via the talloc stack.
135  */
136 const char *fsp_fnum_dbg(const struct files_struct *fsp)
137 {
138         char *str;
139
140         if (fsp == NULL) {
141                 return "fnum [fsp is NULL]";
142         }
143
144         if (fsp->fnum == FNUM_FIELD_INVALID) {
145                 return "fnum [invalid value]";
146         }
147
148         str = talloc_asprintf(talloc_tos(), "fnum %llu",
149                               (unsigned long long)fsp->fnum);
150         if (str == NULL) {
151                 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
152                 return "fnum [talloc failed!]";
153         }
154
155         return str;
156 }
157
158 struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
159                                      const struct smb_filename *in)
160 {
161         struct smb_filename *out;
162         size_t base_len = 0;
163         size_t stream_len = 0;
164         size_t lcomp_len = 0;
165         int num = 0;
166
167         /* stream_name must always be NULL if there is no stream. */
168         if (in->stream_name) {
169                 SMB_ASSERT(in->stream_name[0] != '\0');
170         }
171
172         if (in->base_name != NULL) {
173                 base_len = strlen(in->base_name) + 1;
174                 num += 1;
175         }
176         if (in->stream_name != NULL) {
177                 stream_len = strlen(in->stream_name) + 1;
178                 num += 1;
179         }
180         if (in->original_lcomp != NULL) {
181                 lcomp_len = strlen(in->original_lcomp) + 1;
182                 num += 1;
183         }
184
185         out = talloc_pooled_object(mem_ctx, struct smb_filename,
186                                 num, stream_len + base_len + lcomp_len);
187         if (out == NULL) {
188                 return NULL;
189         }
190         ZERO_STRUCTP(out);
191
192         /*
193          * The following allocations cannot fail as we
194          * pre-allocated space for them in the out pooled
195          * object.
196          */
197         if (in->base_name != NULL) {
198                 out->base_name = talloc_memdup(
199                                 out, in->base_name, base_len);
200                 talloc_set_name_const(out->base_name,
201                                       out->base_name);
202         }
203         if (in->stream_name != NULL) {
204                 out->stream_name = talloc_memdup(
205                                 out, in->stream_name, stream_len);
206                 talloc_set_name_const(out->stream_name,
207                                       out->stream_name);
208         }
209         if (in->original_lcomp != NULL) {
210                 out->original_lcomp = talloc_memdup(
211                                 out, in->original_lcomp, lcomp_len);
212                 talloc_set_name_const(out->original_lcomp,
213                                       out->original_lcomp);
214         }
215         out->st = in->st;
216         return out;
217 }
218
219 /****************************************************************************
220  Simple check to determine if the filename is a stream.
221  ***************************************************************************/
222 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
223 {
224         /* stream_name must always be NULL if there is no stream. */
225         if (smb_fname->stream_name) {
226                 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
227         }
228
229         if (lp_posix_pathnames()) {
230                 return false;
231         }
232
233         return smb_fname->stream_name != NULL;
234 }
235
236 /****************************************************************************
237  Returns true if the filename's stream == "::$DATA"
238  ***************************************************************************/
239 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
240 {
241         if (!is_ntfs_stream_smb_fname(smb_fname)) {
242                 return false;
243         }
244
245         return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;
246 }
247
248 /****************************************************************************
249  Filter out Windows invalid EA names (list probed from Windows 2012).
250 ****************************************************************************/
251
252 static char bad_ea_name_chars[] = "\"*+,/:;<=>?[\\]|";
253
254 bool is_invalid_windows_ea_name(const char *name)
255 {
256         int i;
257         /* EA name is pulled as ascii so we can examine
258            individual bytes here. */
259         for (i = 0; name[i] != 0; i++) {
260                 int val = (name[i] & 0xff);
261                 if (val < ' ' || strchr(bad_ea_name_chars, val)) {
262                         return true;
263                 }
264         }
265         return false;
266 }
267
268 bool ea_list_has_invalid_name(struct ea_list *ea_list)
269 {
270         if (lp_posix_pathnames()) {
271                 return false;
272         }
273
274         for (;ea_list; ea_list = ea_list->next) {
275                 if (is_invalid_windows_ea_name(ea_list->ea.name)) {
276                         return true;
277                 }
278         }
279         return false;
280 }
281
282 /****************************************************************************
283  Split an incoming name into tallocd filename and stream components.
284  Returns true on success, false on out of memory.
285 ****************************************************************************/
286
287 bool split_stream_filename(TALLOC_CTX *ctx,
288                                 const char *filename_in,
289                                 char **filename_out,
290                                 char **streamname_out)
291 {
292         const char *stream_name = NULL;
293         char *stream_out = NULL;
294         char *file_out = NULL;
295
296         stream_name = strchr_m(filename_in, ':');
297
298         if (stream_name) {
299                 stream_out = talloc_strdup(ctx, stream_name);
300                 if (stream_out == NULL) {
301                         return false;
302                 }
303                 file_out = talloc_strndup(ctx,
304                                         filename_in,
305                                         PTR_DIFF(stream_name, filename_in));
306         } else {
307                 file_out = talloc_strdup(ctx, filename_in);
308         }
309
310         if (file_out == NULL) {
311                 TALLOC_FREE(stream_out);
312                 return false;
313         }
314
315         if (filename_out) {
316                 *filename_out = file_out;
317         }
318         if (streamname_out) {
319                 *streamname_out = stream_out;
320         }
321         return true;
322 }