55180cb78ed03ce7c436a58d5a1c158bb67509b9
[kai/samba.git] / source4 / lib / policy / gp_filesys.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Wilco Baan Hofman 2008-2010
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 #include "lib/policy/policy.h"
21 #include "libcli/raw/smb.h"
22 #include "libcli/libcli.h"
23 #include "param/param.h"
24 #include "libcli/resolve/resolve.h"
25 #include "libcli/raw/libcliraw.h"
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <dirent.h>
30 #include <errno.h>
31
32 #define GP_MAX_DEPTH 25
33
34 struct gp_file_entry {
35         bool is_directory;
36         const char *rel_path;
37 };
38 struct gp_file_list {
39         uint32_t num_files;
40         struct gp_file_entry *files;
41 };
42 struct gp_list_state {
43         struct smbcli_tree *tree;
44         uint8_t depth;
45         const char *cur_rel_path;
46         const char *share_path;
47
48         struct gp_file_list list;
49 };
50
51 static NTSTATUS gp_do_list(const char *, struct gp_list_state *);
52
53 /* Create a temporary policy directory */
54 static const char *gp_tmpdir(TALLOC_CTX *mem_ctx)
55 {
56         char *gp_dir = talloc_asprintf(mem_ctx, "%s/policy", tmpdir());
57         struct stat st;
58         int rv;
59
60         if (gp_dir == NULL) return NULL;
61
62         if (stat(gp_dir, &st) != 0) {
63                 rv = mkdir(gp_dir, 0755);
64                 if (rv < 0) {
65                         DEBUG(0, ("Failed to create directory %s: %s\n",
66                                         gp_dir, strerror(errno)));
67                         talloc_free(gp_dir);
68                         return NULL;
69                 }
70         }
71
72         return gp_dir;
73 }
74
75 /* This function is called by the smbcli_list function */
76 static void gp_list_helper (struct clilist_file_info *info, const char *mask,
77                             void *list_state_ptr)
78 {
79         struct gp_list_state *state = list_state_ptr;
80         const char *rel_path;
81
82         /* Ignore . and .. directory entries */
83         if (strcmp(info->name, ".") == 0 || strcmp(info->name, "..") == 0) {
84                 return;
85         }
86
87         /* Safety check against ../.. in filenames which may occur on non-POSIX
88          * platforms */
89         if (strstr(info->name, "../")) {
90                 return;
91         }
92
93         rel_path = talloc_asprintf(state, "%s\\%s", state->cur_rel_path, info->name);
94         if (rel_path == NULL) return;
95
96         /* Append entry to file list */
97         state->list.files = talloc_realloc(state, state->list.files,
98                         struct gp_file_entry,
99                         state->list.num_files + 1);
100         if (state->list.files == NULL) return;
101
102         state->list.files[state->list.num_files].rel_path = rel_path;
103
104         /* Directory */
105         if (info->attrib & FILE_ATTRIBUTE_DIRECTORY) {
106                 state->list.files[state->list.num_files].is_directory = true;
107                 state->list.num_files++;
108
109                 /* Recurse into this directory if the depth is below the maximum */
110                 if (state->depth < GP_MAX_DEPTH) {
111                         gp_do_list(rel_path, state);
112                 }
113
114                 return;
115         }
116
117         state->list.files[state->list.num_files].is_directory = false;
118         state->list.num_files++;
119
120         return;
121 }
122
123 static NTSTATUS gp_do_list (const char *rel_path, struct gp_list_state *state)
124 {
125         uint16_t attributes;
126         int rv;
127         char *mask;
128         const char *old_rel_path;
129
130         attributes = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN |
131                      FILE_ATTRIBUTE_DIRECTORY;
132
133         /* Update the relative paths, while buffering the parent */
134         old_rel_path = state->cur_rel_path;
135         state->cur_rel_path = rel_path;
136         state->depth++;
137
138         /* Get the current mask */
139         mask = talloc_asprintf(state, "%s%s\\*", state->share_path, rel_path);
140         NT_STATUS_HAVE_NO_MEMORY(mask);
141         rv = smbcli_list(state->tree, mask, attributes, gp_list_helper, state);
142         talloc_free(mask);
143
144         /* Go back to the state of the parent */
145         state->cur_rel_path = old_rel_path;
146         state->depth--;
147
148         if (rv == -1)
149                 return NT_STATUS_UNSUCCESSFUL;
150
151         return NT_STATUS_OK;
152 }
153
154 static NTSTATUS gp_cli_connect(struct gp_context *gp_ctx)
155 {
156         struct smbcli_options options;
157         struct smbcli_session_options session_options;
158
159         if (gp_ctx->cli != NULL)
160                 return NT_STATUS_OK;
161
162         gp_ctx->cli = smbcli_state_init(gp_ctx);
163
164         lpcfg_smbcli_options(gp_ctx->lp_ctx, &options);
165         lpcfg_smbcli_session_options(gp_ctx->lp_ctx, &session_options);
166
167         return smbcli_full_connection(gp_ctx,
168                         &gp_ctx->cli,
169                         gp_ctx->active_dc->name,
170                         lpcfg_smb_ports(gp_ctx->lp_ctx),
171                         "sysvol",
172                         NULL,
173                         lpcfg_socket_options(gp_ctx->lp_ctx),
174                         gp_ctx->credentials,
175                         lpcfg_resolve_context(gp_ctx->lp_ctx),
176                         gp_ctx->ev_ctx,
177                         &options,
178                         &session_options,
179                         lpcfg_gensec_settings(gp_ctx, gp_ctx->lp_ctx));
180 }
181
182 static char * gp_get_share_path(TALLOC_CTX *mem_ctx, const char *file_sys_path)
183 {
184         unsigned int i, bkslash_cnt;
185
186         /* Get the path from the share down (\\..\..\(this\stuff) */
187         for (i = 0, bkslash_cnt = 0; file_sys_path[i] != '\0'; i++) {
188                 if (file_sys_path[i] == '\\')
189                         bkslash_cnt++;
190
191                 if (bkslash_cnt == 4) {
192                         return talloc_strdup(mem_ctx, &file_sys_path[i]);
193                 }
194         }
195
196         return NULL;
197 }
198
199 static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
200                              const char *local_dst)
201 {
202         int fh_remote, fh_local;
203         uint8_t *buf;
204         size_t nread = 0;
205         size_t buf_size = 1024;
206         size_t file_size;
207         uint16_t attr;
208
209         /* Open the remote file */
210         fh_remote = smbcli_open(tree, remote_src, O_RDONLY, DENY_NONE);
211         if (fh_remote == -1) {
212                 DEBUG(0, ("Failed to open remote file: %s\n", remote_src));
213                 return NT_STATUS_UNSUCCESSFUL;
214         }
215
216         /* Open the local file */
217         fh_local = open(local_dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
218         if (fh_local == -1) {
219                 DEBUG(0, ("Failed to open local file: %s\n", local_dst));
220                 return NT_STATUS_UNSUCCESSFUL;
221         }
222
223         /* Get the remote file size for error checking */
224         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(tree, fh_remote,
225                                 &attr, &file_size, NULL, NULL, NULL, NULL, NULL)) &&
226                         NT_STATUS_IS_ERR(smbcli_getattrE(tree, fh_remote,
227                                 &attr, &file_size, NULL, NULL, NULL))) {
228                 DEBUG(0, ("Failed to get remote file size: %s\n", smbcli_errstr(tree)));
229                 return NT_STATUS_UNSUCCESSFUL;
230         }
231
232         buf = talloc_zero_array(tree, uint8_t, buf_size);
233         NT_STATUS_HAVE_NO_MEMORY(buf);
234
235         /* Copy the contents of the file */
236         while (1) {
237                 int n = smbcli_read(tree, fh_remote, buf, nread, buf_size);
238
239                 if (n <= 0) {
240                         break;
241                 }
242
243                 if (write(fh_local, buf, n) != n) {
244                         DEBUG(0, ("Short write while copying file.\n"));
245                         talloc_free(buf);
246                         return NT_STATUS_UNSUCCESSFUL;
247                 }
248                 nread += n;
249         }
250
251         /* Bytes read should match the file size, or the copy was incomplete */
252         if (nread != file_size) {
253                 DEBUG(0, ("Remote/local file size mismatch after copying file: "
254                           "%s (remote %zu, local %zu).\n",
255                           remote_src, file_size, nread));
256                 talloc_free(buf);
257                 return NT_STATUS_UNSUCCESSFUL;
258         }
259
260         /* Close the files */
261         smbcli_close(tree, fh_remote);
262         close(fh_local);
263
264         talloc_free(buf);
265         return NT_STATUS_OK;
266 }
267
268 static NTSTATUS gp_get_files(struct smbcli_tree *tree, const char *share_path,
269                              const char *local_path, struct gp_file_list *list)
270 {
271         uint32_t i;
272         int rv;
273         char *local_rel_path, *full_local_path, *full_remote_path;
274         TALLOC_CTX *mem_ctx;
275         NTSTATUS status;
276
277         mem_ctx = talloc_new(tree);
278         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
279
280         for (i = 0; i < list->num_files; i++) {
281
282                 /* Get local path by replacing backslashes with slashes */
283                 local_rel_path = talloc_strdup(mem_ctx, list->files[i].rel_path);
284                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(local_rel_path, mem_ctx);
285                 string_replace(local_rel_path, '\\', '/');
286
287                 full_local_path = talloc_asprintf(mem_ctx, "%s%s", local_path,
288                                 local_rel_path);
289                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(full_local_path, mem_ctx);
290
291                 /* If the entry is a directory, create it. */
292                 if (list->files[i].is_directory == true) {
293                         rv = mkdir(full_local_path, 0755);
294                         if (rv < 0) {
295                                 DEBUG(0, ("Failed to create directory %s: %s\n",
296                                                 full_local_path, strerror(errno)));
297                                 talloc_free(mem_ctx);
298                                 return NT_STATUS_UNSUCCESSFUL;
299                         }
300                         continue;
301                 }
302
303                 full_remote_path = talloc_asprintf(mem_ctx, "%s%s", share_path,
304                                 list->files[i].rel_path);
305                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(full_remote_path, mem_ctx);
306
307                 /* Get the file */
308                 status = gp_get_file(tree, full_remote_path, full_local_path);
309                 if (!NT_STATUS_IS_OK(status)) {
310                         DEBUG(0, ("Error getting file.\n"));
311                         talloc_free(mem_ctx);
312                         return status;
313                 }
314         }
315
316         return NT_STATUS_OK;
317 }
318
319 NTSTATUS gp_fetch_gpt (struct gp_context *gp_ctx, struct gp_object *gpo,
320                        const char **ret_local_path)
321 {
322         TALLOC_CTX *mem_ctx;
323         struct gp_list_state *state;
324         NTSTATUS status;
325         struct stat st;
326         int rv;
327         const char *local_path, *share_path;
328
329         /* Create a forked memory context, as a base for everything here */
330         mem_ctx = talloc_new(gp_ctx);
331         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
332
333         if (gp_ctx->cli == NULL) {
334                 status = gp_cli_connect(gp_ctx);
335                 if (!NT_STATUS_IS_OK(status)) {
336                         DEBUG(0, ("Failed to create cli connection to DC\n"));
337                         talloc_free(mem_ctx);
338                         return status;
339                 }
340         }
341
342         /* Get the remote path to copy from */
343         share_path = gp_get_share_path(mem_ctx, gpo->file_sys_path);
344         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(share_path, mem_ctx);
345
346         /* Get the local path to copy to */
347         local_path = talloc_asprintf(gp_ctx, "%s/%s", gp_tmpdir(mem_ctx), gpo->name);
348         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(local_path, mem_ctx);
349
350         /* Prepare the state structure */
351         state = talloc_zero(mem_ctx, struct gp_list_state);
352         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(state, mem_ctx);
353
354         state->tree = gp_ctx->cli->tree;
355         state->share_path = share_path;
356
357         /* Create the GPO dir if it does not exist */
358         if (stat(local_path, &st) != 0) {
359                 rv = mkdir(local_path, 0755);
360                 if (rv < 0) {
361                         DEBUG(0, ("Could not create local path\n"));
362                         talloc_free(mem_ctx);
363                         return NT_STATUS_UNSUCCESSFUL;
364                 }
365         }
366
367         /* Get the file list */
368         status = gp_do_list("", state);
369         if (!NT_STATUS_IS_OK(status)) {
370                 DEBUG(0, ("Could not list GPO files on remote server\n"));
371                 talloc_free(mem_ctx);
372                 return status;
373         }
374
375         /* If the list has no entries there is a problem. */
376         if (state->list.num_files == 0) {
377                 DEBUG(0, ("File list is has no entries. Is the GPT directory empty?\n"));
378                 talloc_free(mem_ctx);
379                 return NT_STATUS_UNSUCCESSFUL;
380         }
381
382         /* Fetch the files */
383         status = gp_get_files(gp_ctx->cli->tree, share_path, local_path, &state->list);
384
385         /* Return the local path to the gpo */
386         *ret_local_path = local_path;
387
388         talloc_free(mem_ctx);
389         return NT_STATUS_OK;
390 }
391
392 static NTSTATUS push_recursive (struct gp_context *gp_ctx, const char *local_path,
393                                 const char *remote_path, int depth)
394 {
395         DIR *dir;
396         struct dirent *dirent;
397         char *entry_local_path;
398         char *entry_remote_path;
399         int local_fd, remote_fd;
400         int buf[1024];
401         int nread, total_read;
402         struct stat s;
403
404         dir = opendir(local_path);
405         while ((dirent = readdir(dir)) != NULL) {
406                 if (strcmp(dirent->d_name, ".") == 0 ||
407                                 strcmp(dirent->d_name, "..") == 0) {
408                         continue;
409                 }
410
411                 entry_local_path = talloc_asprintf(gp_ctx, "%s/%s", local_path,
412                                                    dirent->d_name);
413                 NT_STATUS_HAVE_NO_MEMORY(entry_local_path);
414
415                 entry_remote_path = talloc_asprintf(gp_ctx, "%s\\%s",
416                                                     remote_path, dirent->d_name);
417                 NT_STATUS_HAVE_NO_MEMORY(entry_remote_path);
418
419                 if (stat(entry_local_path, &s) != 0) {
420                         return NT_STATUS_UNSUCCESSFUL;
421                 }
422                 if (s.st_mode & S_IFDIR) {
423                         DEBUG(6, ("Pushing directory %s to %s on sysvol\n",
424                                   entry_local_path, entry_remote_path));
425                         smbcli_mkdir(gp_ctx->cli->tree, entry_remote_path);
426                         if (depth < GP_MAX_DEPTH) {
427                                 push_recursive(gp_ctx, entry_local_path,
428                                                entry_remote_path, depth+1);
429                         }
430                 } else {
431                         DEBUG(6, ("Pushing file %s to %s on sysvol\n",
432                                   entry_local_path, entry_remote_path));
433                         remote_fd = smbcli_open(gp_ctx->cli->tree,
434                                                 entry_remote_path,
435                                                 O_WRONLY | O_CREAT,
436                                                 0);
437                         if (remote_fd < 0) {
438                                 talloc_free(entry_local_path);
439                                 talloc_free(entry_remote_path);
440                                 DEBUG(0, ("Failed to create remote file: %s\n",
441                                           entry_remote_path));
442                                 return NT_STATUS_UNSUCCESSFUL;
443                         }
444                         local_fd = open(entry_local_path, O_RDONLY);
445                         if (local_fd < 0) {
446                                 talloc_free(entry_local_path);
447                                 talloc_free(entry_remote_path);
448                                 DEBUG(0, ("Failed to open local file: %s\n",
449                                           entry_local_path));
450                                 return NT_STATUS_UNSUCCESSFUL;
451                         }
452                         total_read = 0;
453                         while ((nread = read(local_fd, &buf, sizeof(buf)))) {
454                                 smbcli_write(gp_ctx->cli->tree, remote_fd, 0,
455                                                 &buf, total_read, nread);
456                                 total_read += nread;
457                         }
458
459                         close(local_fd);
460                         smbcli_close(gp_ctx->cli->tree, remote_fd);
461                 }
462                 talloc_free(entry_local_path);
463                 talloc_free(entry_remote_path);
464         }
465         closedir(dir);
466
467         return NT_STATUS_OK;
468 }
469
470
471
472 NTSTATUS gp_push_gpt(struct gp_context *gp_ctx, const char *local_path,
473                      const char *file_sys_path)
474 {
475         NTSTATUS status;
476         char *share_path;
477
478         if (gp_ctx->cli == NULL) {
479                 status = gp_cli_connect(gp_ctx);
480                 if (!NT_STATUS_IS_OK(status)) {
481                         DEBUG(0, ("Failed to create cli connection to DC\n"));
482                         return status;
483                 }
484         }
485         share_path = gp_get_share_path(gp_ctx, file_sys_path);
486
487         DEBUG(5, ("Copying %s to %s on sysvol\n", local_path, share_path));
488
489         smbcli_mkdir(gp_ctx->cli->tree, share_path);
490
491         status = push_recursive(gp_ctx, local_path, share_path, 0);
492
493         talloc_free(share_path);
494         return status;
495 }
496
497 NTSTATUS gp_create_gpt(struct gp_context *gp_ctx, const char *name,
498                        const char *file_sys_path)
499 {
500         TALLOC_CTX *mem_ctx;
501         const char *tmp_dir, *policy_dir, *tmp_str;
502         int rv;
503         int fd;
504         NTSTATUS status;
505         const char *file_content = "[General]\r\nVersion=0\r\n";
506
507         /* Create a forked memory context, as a base for everything here */
508         mem_ctx = talloc_new(gp_ctx);
509         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
510
511         tmp_dir = gp_tmpdir(mem_ctx);
512         NT_STATUS_HAVE_NO_MEMORY(tmp_dir);
513         policy_dir = talloc_asprintf(mem_ctx, "%s/%s", tmp_dir, name);
514         NT_STATUS_HAVE_NO_MEMORY(policy_dir);
515
516         /* Create the directories */
517
518         rv = mkdir(policy_dir, 0755);
519         if (rv < 0) {
520                 DEBUG(0, ("Could not create the policy dir: %s\n", policy_dir));
521                 talloc_free(mem_ctx);
522                 return NT_STATUS_UNSUCCESSFUL;
523         }
524
525         tmp_str = talloc_asprintf(mem_ctx, "%s/User", policy_dir);
526         NT_STATUS_HAVE_NO_MEMORY(tmp_str);
527         rv = mkdir(tmp_str, 0755);
528         if (rv < 0) {
529                 DEBUG(0, ("Could not create the User dir: %s\n", tmp_str));
530                 talloc_free(mem_ctx);
531                 return NT_STATUS_UNSUCCESSFUL;
532         }
533
534         tmp_str = talloc_asprintf(mem_ctx, "%s/Machine", policy_dir);
535         NT_STATUS_HAVE_NO_MEMORY(tmp_str);
536         rv = mkdir(tmp_str, 0755);
537         if (rv < 0) {
538                 DEBUG(0, ("Could not create the Machine dir: %s\n", tmp_str));
539                 talloc_free(mem_ctx);
540                 return NT_STATUS_UNSUCCESSFUL;
541         }
542
543         /* Create a GPT.INI with version 0 */
544
545         tmp_str = talloc_asprintf(mem_ctx, "%s/GPT.INI", policy_dir);
546         NT_STATUS_HAVE_NO_MEMORY(tmp_str);
547         fd = open(tmp_str, O_CREAT | O_WRONLY, 0644);
548         if (fd < 0) {
549                 DEBUG(0, ("Could not create the GPT.INI: %s\n", tmp_str));
550                 talloc_free(mem_ctx);
551                 return NT_STATUS_UNSUCCESSFUL;
552         }
553
554         rv = write(fd, file_content, strlen(file_content));
555         if (rv != strlen(file_content)) {
556                 DEBUG(0, ("Short write in GPT.INI\n"));
557                 talloc_free(mem_ctx);
558                 return NT_STATUS_UNSUCCESSFUL;
559         }
560
561         close(fd);
562
563         /* Upload the GPT to the sysvol share on a DC */
564         status = gp_push_gpt(gp_ctx, policy_dir, file_sys_path);
565         if (!NT_STATUS_IS_OK(status)) {
566                 talloc_free(mem_ctx);
567                 return status;
568         }
569
570         talloc_free(mem_ctx);
571         return NT_STATUS_OK;
572 }
573
574 NTSTATUS gp_set_gpt_security_descriptor(struct gp_context *gp_ctx,
575                                         struct gp_object *gpo,
576                                         struct security_descriptor *sd)
577 {
578         TALLOC_CTX *mem_ctx;
579         NTSTATUS status;
580         union smb_setfileinfo fileinfo;
581         union smb_open io;
582         union smb_close io_close;
583
584         /* Create a connection to sysvol if it is not already there */
585         if (gp_ctx->cli == NULL) {
586                 status = gp_cli_connect(gp_ctx);
587                 if (!NT_STATUS_IS_OK(status)) {
588                         DEBUG(0, ("Failed to create cli connection to DC\n"));
589                         return status;
590                 }
591         }
592
593         /* Create a forked memory context which can be freed easily */
594         mem_ctx = talloc_new(gp_ctx);
595         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
596
597         /* Open the directory with NTCreate AndX call */
598         io.generic.level = RAW_OPEN_NTCREATEX;
599         io.ntcreatex.in.root_fid.fnum = 0;
600         io.ntcreatex.in.flags = 0;
601         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
602         io.ntcreatex.in.create_options = 0;
603         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
604         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
605                                        NTCREATEX_SHARE_ACCESS_WRITE;
606         io.ntcreatex.in.alloc_size = 0;
607         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
608         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
609         io.ntcreatex.in.security_flags = 0;
610         io.ntcreatex.in.fname = gp_get_share_path(mem_ctx, gpo->file_sys_path);
611         status = smb_raw_open(gp_ctx->cli->tree, mem_ctx, &io);
612         if (!NT_STATUS_IS_OK(status)) {
613                 DEBUG(0, ("Can't open GPT directory\n"));
614                 talloc_free(mem_ctx);
615                 return status;
616         }
617
618         /* Set the security descriptor on the directory */
619         fileinfo.generic.level = RAW_FILEINFO_SEC_DESC;
620         fileinfo.set_secdesc.in.file.fnum = io.ntcreatex.out.file.fnum;
621         fileinfo.set_secdesc.in.secinfo_flags = SECINFO_PROTECTED_DACL |
622                                                 SECINFO_OWNER |
623                                                 SECINFO_GROUP |
624                                                 SECINFO_DACL;
625         fileinfo.set_secdesc.in.sd = sd;
626         status = smb_raw_setfileinfo(gp_ctx->cli->tree, &fileinfo);
627         if (!NT_STATUS_IS_OK(status)) {
628                 DEBUG(0, ("Failed to set security descriptor on the GPT\n"));
629                 talloc_free(mem_ctx);
630                 return status;
631         }
632
633         /* Close the directory */
634         io_close.close.level = RAW_CLOSE_CLOSE;
635         io_close.close.in.file.fnum = io.ntcreatex.out.file.fnum;
636         io_close.close.in.write_time = 0;
637         status = smb_raw_close(gp_ctx->cli->tree, &io_close);
638         if (!NT_STATUS_IS_OK(status)) {
639                 DEBUG(0, ("Failed to close directory\n"));
640                 talloc_free(mem_ctx);
641                 return status;
642         }
643
644         talloc_free(mem_ctx);
645         return NT_STATUS_OK;
646 }