packaging(RHEL-CTDB): Build winbind-32bit package in the 64bit build
[Samba/fernandojvsilva.git] / source4 / libgpo / gpo_filesync.c
blob3f6f5f796032ffea23d79b3afc5d8833dcf081ed
1 /*
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/>.
22 #include "includes.h"
23 #include "libcli/libcli.h"
24 #include "system/filesys.h"
27 struct sync_context {
28 TALLOC_CTX *mem_ctx;
29 struct smbcli_state *cli;
30 char *remote_path;
31 char *local_path;
32 char *mask;
33 uint16_t attribute;
36 static void gpo_sync_func(struct clilist_file_info *info,
37 const char *mask,
38 void *state);
40 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
41 struct smbcli_state *cli,
42 const char *nt_path,
43 const char *unix_path)
45 NTSTATUS result;
46 int fnum;
47 int fd = 0;
48 char *data = NULL;
49 static int io_bufsize = 64512;
50 int read_size = io_bufsize;
51 off_t nread = 0;
53 if ((fnum = smbcli_open(cli->tree, nt_path, O_RDONLY, DENY_NONE)) == -1) {
54 result = NT_STATUS_NO_SUCH_FILE;
55 goto out;
58 if ((fd = open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
59 result = map_nt_error_from_unix(errno);
60 goto out;
63 if ((data = talloc_size(mem_ctx, read_size)) == NULL) {
64 result = NT_STATUS_NO_MEMORY;
65 goto out;
68 while (1) {
70 int n = smbcli_read(cli->tree, fnum, data, nread, read_size);
72 if (n <= 0)
73 break;
75 if (write(fd, data, n) != n) {
76 break;
79 nread += n;
82 result = NT_STATUS_OK;
84 out:
85 SAFE_FREE(data);
86 if (fnum) {
87 smbcli_close(cli->tree, fnum);
89 if (fd) {
90 close(fd);
93 return result;
96 /****************************************************************
97 copy dir
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;
106 return NT_STATUS_OK;
109 /****************************************************************
110 sync files
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,
118 ctx->mask,
119 ctx->attribute,
120 gpo_sync_func,
121 ctx) == -1) {
122 DEBUG(1,("listing [%s] failed with error: %s\n",
123 ctx->mask, smbcli_errstr(ctx->cli->tree)));
124 return false;
127 return true;
130 /****************************************************************
131 syncronisation call back
132 ****************************************************************/
134 static void gpo_sync_func(struct clilist_file_info *info,
135 const char *mask,
136 void *state)
138 NTSTATUS result;
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, "..")) {
147 return;
150 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
151 mask, info->name));
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",
158 ctx->remote_path,
159 info->name);
161 unix_dir = talloc_asprintf(ctx->mem_ctx, "%s/%s",
162 ctx->local_path,
163 info->name);
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",
168 nt_errstr(result)));
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,
178 "%s\\*",
179 nt_dir);
180 if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
181 DEBUG(0,("gpo_sync_func: ENOMEM\n"));
182 return;
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;
190 return;
193 DEBUG(3,("got file: [%s]\n", info->name));
195 nt_filename = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
196 ctx->remote_path,
197 info->name);
199 unix_filename = talloc_asprintf(ctx->mem_ctx, "%s/%s",
200 ctx->local_path,
201 info->name);
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",
207 nt_errstr(result)));
212 /****************************************************************
213 list a remote directory and download recursivly
214 ****************************************************************/
216 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
217 struct smbcli_state *cli,
218 const char *nt_path,
219 const char *local_path)
221 struct sync_context ctx;
223 ctx.mem_ctx = mem_ctx;
224 ctx.cli = cli;
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,
230 "%s\\*",
231 nt_path);
232 if (!ctx.mask) {
233 return NT_STATUS_NO_MEMORY;
236 if (!gpo_sync_files(&ctx)) {
237 return NT_STATUS_NO_SUCH_FILE;
240 return NT_STATUS_OK;