2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
4 * Copyright (C) Guenther Deschner 2006
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 struct cli_state *cli;
32 static void gpo_sync_func(const char *mnt,
37 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
38 struct cli_state *cli,
40 const char *unix_path)
45 static int io_bufsize = 64512;
46 int read_size = io_bufsize;
50 if ((fnum = cli_open(cli, nt_path, O_RDONLY, DENY_NONE)) == -1) {
51 result = NT_STATUS_NO_SUCH_FILE;
55 if ((fd = sys_open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
56 result = map_nt_error_from_unix(errno);
60 if ((data = (char *)SMB_MALLOC(read_size)) == NULL) {
61 result = NT_STATUS_NO_MEMORY;
67 int n = cli_read(cli, fnum, data, nread + start, read_size);
72 if (write(fd, data, n) != n) {
79 result = NT_STATUS_OK;
93 /****************************************************************
95 ****************************************************************/
97 static NTSTATUS gpo_copy_dir(const char *unix_path)
99 if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
100 return NT_STATUS_ACCESS_DENIED;
106 /****************************************************************
108 ****************************************************************/
110 static BOOL gpo_sync_files(struct sync_context *ctx)
112 DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
114 if (cli_list(ctx->cli, ctx->mask, ctx->attribute, gpo_sync_func, ctx) == -1) {
115 DEBUG(1,("listing [%s] failed with error: %s\n",
116 ctx->mask, cli_errstr(ctx->cli)));
123 /****************************************************************
124 syncronisation call back
125 ****************************************************************/
127 static void gpo_sync_func(const char *mnt,
133 struct sync_context *ctx;
134 fstring nt_filename, unix_filename;
135 fstring nt_dir, unix_dir;
136 char *old_nt_dir, *old_unix_dir;
138 ctx = (struct sync_context *)state;
140 if (strequal(info->name, ".") || strequal(info->name, "..")) {
144 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
147 if (info->mode & aDIR) {
149 DEBUG(3,("got dir: [%s]\n", info->name));
151 fstrcpy(nt_dir, ctx->remote_path);
152 fstrcat(nt_dir, "\\");
153 fstrcat(nt_dir, info->name);
155 fstrcpy(unix_dir, ctx->local_path);
156 fstrcat(unix_dir, "/");
157 fstrcat(unix_dir, info->name);
159 result = gpo_copy_dir(unix_dir);
160 if (!NT_STATUS_IS_OK(result)) {
161 DEBUG(1,("failed to copy dir: %s\n", nt_errstr(result)));
164 old_nt_dir = ctx->remote_path;
165 ctx->remote_path = nt_dir;
167 old_unix_dir = ctx->local_path;
168 ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
170 pstrcpy(ctx->mask, nt_dir);
171 pstrcat(ctx->mask, "\\*");
173 if (!gpo_sync_files(ctx)) {
174 DEBUG(0,("could not sync files\n"));
177 ctx->remote_path = old_nt_dir;
178 ctx->local_path = old_unix_dir;
182 DEBUG(3,("got file: [%s]\n", info->name));
184 fstrcpy(nt_filename, ctx->remote_path);
185 fstrcat(nt_filename, "\\");
186 fstrcat(nt_filename, info->name);
188 fstrcpy(unix_filename, ctx->local_path);
189 fstrcat(unix_filename, "/");
190 fstrcat(unix_filename, info->name);
192 result = gpo_copy_file(ctx->mem_ctx, ctx->cli, nt_filename, unix_filename);
193 if (!NT_STATUS_IS_OK(result)) {
194 DEBUG(1,("failed to copy file: %s\n", nt_errstr(result)));
199 /****************************************************************
200 list a remote directory and download recursivly
201 ****************************************************************/
203 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
204 struct cli_state *cli,
206 const char *local_path)
208 struct sync_context ctx;
210 ctx.mem_ctx = mem_ctx;
212 ctx.remote_path = CONST_DISCARD(char *, nt_path);
213 ctx.local_path = CONST_DISCARD(char *, local_path);
214 ctx.attribute = (aSYSTEM | aHIDDEN | aDIR);
216 pstrcpy(ctx.mask, nt_path);
217 pstrcat(ctx.mask, "\\*");
219 if (!gpo_sync_files(&ctx)) {
220 return NT_STATUS_NO_SUCH_FILE;