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