2503e77764c23fd13cad757bc413050cd2e990a6
[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 static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
22                                   const struct smb_filename *smb_fname_in,
23                                   struct smb_filename **smb_fname_out);
24
25 /**
26  * XXX: This is temporary and there should be no callers of this outside of
27  * this file once smb_filename is plumbed through all path based operations.
28  * The one legitimate caller currently is smb_fname_str_dbg(), which this
29  * could be made static for.
30  */
31 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx,
32                                const struct smb_filename *smb_fname,
33                                char **full_name)
34 {
35         if (smb_fname->stream_name) {
36                 /* stream_name must always be NULL if there is no stream. */
37                 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
38
39                 *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
40                                              smb_fname->stream_name);
41         } else {
42                 *full_name = talloc_strdup(ctx, smb_fname->base_name);
43         }
44
45         if (!*full_name) {
46                 return NT_STATUS_NO_MEMORY;
47         }
48
49         return NT_STATUS_OK;
50 }
51
52 /**
53  * There are actually legitimate callers of this such as functions that
54  * enumerate streams using the vfs_streaminfo interface and then want to
55  * operate on each stream.
56  */
57 NTSTATUS create_synthetic_smb_fname(TALLOC_CTX *ctx, const char *base_name,
58                                     const char *stream_name,
59                                     const SMB_STRUCT_STAT *psbuf,
60                                     struct smb_filename **smb_fname_out)
61 {
62         struct smb_filename smb_fname_loc;
63
64         ZERO_STRUCT(smb_fname_loc);
65
66         /* Setup the base_name/stream_name. */
67         smb_fname_loc.base_name = discard_const_p(char, base_name);
68         smb_fname_loc.stream_name = discard_const_p(char, stream_name);
69
70         /* Copy the psbuf if one was given. */
71         if (psbuf)
72                 smb_fname_loc.st = *psbuf;
73
74         /* Let copy_smb_filename() do the heavy lifting. */
75         return copy_smb_filename(ctx, &smb_fname_loc, smb_fname_out);
76 }
77
78 struct smb_filename *synthetic_smb_fname(TALLOC_CTX *mem_ctx,
79                                          const char *base_name,
80                                          const char *stream_name,
81                                          const SMB_STRUCT_STAT *psbuf)
82 {
83         struct smb_filename smb_fname_loc = { 0, };
84
85         /* Setup the base_name/stream_name. */
86         smb_fname_loc.base_name = discard_const_p(char, base_name);
87         smb_fname_loc.stream_name = discard_const_p(char, stream_name);
88
89         /* Copy the psbuf if one was given. */
90         if (psbuf)
91                 smb_fname_loc.st = *psbuf;
92
93         /* Let copy_smb_filename() do the heavy lifting. */
94         return cp_smb_filename(mem_ctx, &smb_fname_loc);
95 }
96
97 /**
98  * XXX: This is temporary and there should be no callers of this once
99  * smb_filename is plumbed through all path based operations.
100  */
101 NTSTATUS create_synthetic_smb_fname_split(TALLOC_CTX *ctx,
102                                           const char *fname,
103                                           const SMB_STRUCT_STAT *psbuf,
104                                           struct smb_filename **smb_fname_out)
105 {
106         NTSTATUS status;
107         const char *stream_name = NULL;
108         char *base_name = NULL;
109
110         if (!lp_posix_pathnames()) {
111                 stream_name = strchr_m(fname, ':');
112         }
113
114         /* Setup the base_name/stream_name. */
115         if (stream_name) {
116                 base_name = talloc_strndup(ctx, fname,
117                                            PTR_DIFF(stream_name, fname));
118         } else {
119                 base_name = talloc_strdup(ctx, fname);
120         }
121
122         if (!base_name) {
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         status = create_synthetic_smb_fname(ctx, base_name, stream_name, psbuf,
127                                             smb_fname_out);
128         TALLOC_FREE(base_name);
129         return status;
130 }
131
132 /**
133  * Return a string using the talloc_tos()
134  */
135 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
136 {
137         char *fname = NULL;
138         NTSTATUS status;
139
140         if (smb_fname == NULL) {
141                 return "";
142         }
143         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
144         if (!NT_STATUS_IS_OK(status)) {
145                 return "";
146         }
147         return fname;
148 }
149
150 /**
151  * Return a debug string of the path name of an fsp using the talloc_tos().
152  */
153 const char *fsp_str_dbg(const struct files_struct *fsp)
154 {
155         return smb_fname_str_dbg(fsp->fsp_name);
156 }
157
158 /**
159  * Create a debug string for the fnum of an fsp.
160  *
161  * This is allocated to talloc_tos() or a string constant
162  * in certain corner cases. The returned string should
163  * hence not be free'd directly but only via the talloc stack.
164  */
165 const char *fsp_fnum_dbg(const struct files_struct *fsp)
166 {
167         char *str;
168
169         if (fsp == NULL) {
170                 return "fnum [fsp is NULL]";
171         }
172
173         if (fsp->fnum == FNUM_FIELD_INVALID) {
174                 return "fnum [invalid value]";
175         }
176
177         str = talloc_asprintf(talloc_tos(), "fnum %llu",
178                               (unsigned long long)fsp->fnum);
179         if (str == NULL) {
180                 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
181                 return "fnum [talloc failed!]";
182         }
183
184         return str;
185 }
186
187 struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
188                                      const struct smb_filename *in)
189 {
190         struct smb_filename *out;
191
192         /* stream_name must always be NULL if there is no stream. */
193         if (in->stream_name) {
194                 SMB_ASSERT(in->stream_name[0] != '\0');
195         }
196
197         out = talloc_zero(mem_ctx, struct smb_filename);
198         if (out == NULL) {
199                 return NULL;
200         }
201         if (in->base_name != NULL) {
202                 out->base_name = talloc_strdup(out, in->base_name);
203                 if (out->base_name == NULL) {
204                         goto fail;
205                 }
206         }
207         if (in->stream_name != NULL) {
208                 out->stream_name = talloc_strdup(out, in->stream_name);
209                 if (out->stream_name == NULL) {
210                         goto fail;
211                 }
212         }
213         if (in->original_lcomp != NULL) {
214                 out->original_lcomp = talloc_strdup(out, in->original_lcomp);
215                 if (out->original_lcomp == NULL) {
216                         goto fail;
217                 }
218         }
219         out->st = in->st;
220         return out;
221 fail:
222         TALLOC_FREE(out);
223         return NULL;
224 }
225
226 static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
227                                   const struct smb_filename *smb_fname_in,
228                                   struct smb_filename **smb_fname_out)
229 {
230         *smb_fname_out = cp_smb_filename(ctx, smb_fname_in);
231         if (*smb_fname_out == NULL) {
232                 return NT_STATUS_NO_MEMORY;
233         }
234         return NT_STATUS_OK;
235 }
236
237 /****************************************************************************
238  Simple check to determine if the filename is a stream.
239  ***************************************************************************/
240 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
241 {
242         /* stream_name must always be NULL if there is no stream. */
243         if (smb_fname->stream_name) {
244                 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
245         }
246
247         if (lp_posix_pathnames()) {
248                 return false;
249         }
250
251         return smb_fname->stream_name != NULL;
252 }
253
254 /****************************************************************************
255  Returns true if the filename's stream == "::$DATA"
256  ***************************************************************************/
257 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
258 {
259         if (!is_ntfs_stream_smb_fname(smb_fname)) {
260                 return false;
261         }
262
263         return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;
264 }