2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
5 * Copyright (C) Guenther Deschner 2006
6 * Copyright (C) Wilco Baan Hofman 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/libcli.h"
24 #include "system/filesys.h"
29 struct smbcli_state *cli;
36 static void gpo_sync_func(struct clilist_file_info *info,
40 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
41 struct smbcli_state *cli,
43 const char *unix_path)
49 static int io_bufsize = 64512;
50 int read_size = io_bufsize;
53 if ((fnum = smbcli_open(cli->tree, nt_path, O_RDONLY, DENY_NONE)) == -1) {
54 result = NT_STATUS_NO_SUCH_FILE;
58 if ((fd = open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
59 result = map_nt_error_from_unix(errno);
63 if ((data = talloc_size(mem_ctx, read_size)) == NULL) {
64 result = NT_STATUS_NO_MEMORY;
70 int n = smbcli_read(cli->tree, fnum, data, nread, read_size);
75 if (write(fd, data, n) != n) {
82 result = NT_STATUS_OK;
87 smbcli_close(cli->tree, fnum);
96 /****************************************************************
98 ****************************************************************/
100 static NTSTATUS gpo_copy_dir(const char *nt_dir, const char *unix_path)
102 if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
103 return NT_STATUS_ACCESS_DENIED;
109 /****************************************************************
111 ****************************************************************/
113 static bool gpo_sync_files(struct sync_context *ctx)
115 DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
117 if (smbcli_list(ctx->cli->tree,
122 DEBUG(1,("listing [%s] failed with error: %s\n",
123 ctx->mask, smbcli_errstr(ctx->cli->tree)));
130 /****************************************************************
131 syncronisation call back
132 ****************************************************************/
134 static void gpo_sync_func(struct clilist_file_info *info,
139 struct sync_context *ctx;
140 char *nt_filename, *unix_filename;
141 char *nt_dir, *unix_dir;
142 char *old_nt_dir, *old_unix_dir;
144 ctx = (struct sync_context *)state;
146 if (strequal(info->name, ".") || strequal(info->name, "..")) {
150 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
153 if (info->attrib & FILE_ATTRIBUTE_DIRECTORY) {
155 DEBUG(3,("got dir: [%s]\n", info->name));
157 nt_dir = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
161 unix_dir = talloc_asprintf(ctx->mem_ctx, "%s/%s",
165 result = gpo_copy_dir(nt_dir, unix_dir);
166 if (!NT_STATUS_IS_OK(result)) {
167 DEBUG(1,("failed to copy dir: %s\n",
171 old_nt_dir = ctx->remote_path;
172 ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
174 old_unix_dir = ctx->local_path;
175 ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
177 ctx->mask = talloc_asprintf(ctx->mem_ctx,
180 if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
181 DEBUG(0,("gpo_sync_func: ENOMEM\n"));
184 if (!gpo_sync_files(ctx)) {
185 DEBUG(0,("could not sync files\n"));
188 ctx->remote_path = old_nt_dir;
189 ctx->local_path = old_unix_dir;
193 DEBUG(3,("got file: [%s]\n", info->name));
195 nt_filename = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
199 unix_filename = talloc_asprintf(ctx->mem_ctx, "%s/%s",
203 result = gpo_copy_file(ctx->mem_ctx, ctx->cli,
204 nt_filename, unix_filename);
205 if (!NT_STATUS_IS_OK(result)) {
206 DEBUG(1,("failed to copy file: %s\n",
212 /****************************************************************
213 list a remote directory and download recursivly
214 ****************************************************************/
216 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
217 struct smbcli_state *cli,
219 const char *local_path)
221 struct sync_context ctx;
223 ctx.mem_ctx = mem_ctx;
225 ctx.remote_path = discard_const_p(char, nt_path);
226 ctx.local_path = discard_const_p(char, local_path);
227 ctx.attribute = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
229 ctx.mask = talloc_asprintf(mem_ctx,
233 return NT_STATUS_NO_MEMORY;
236 if (!gpo_sync_files(&ctx)) {
237 return NT_STATUS_NO_SUCH_FILE;