More const fixes for compiler warnings from the waf build.
[ira/wip.git] / source3 / libgpo / gpo_filesync.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Guenther Deschner 2006
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
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
24
25 struct sync_context {
26         TALLOC_CTX *mem_ctx;
27         struct cli_state *cli;
28         char *remote_path;
29         char *local_path;
30         char *mask;
31         uint16_t attribute;
32 };
33
34 static NTSTATUS gpo_sync_func(const char *mnt,
35                           struct file_info *info,
36                           const char *mask,
37                           void *state);
38
39 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
40                        struct cli_state *cli,
41                        const char *nt_path,
42                        const char *unix_path)
43 {
44         NTSTATUS result;
45         uint16_t fnum;
46         int fd = -1;
47         char *data = NULL;
48         static int io_bufsize = 64512;
49         int read_size = io_bufsize;
50         off_t nread = 0;
51
52         result = cli_open(cli, nt_path, O_RDONLY, DENY_NONE, &fnum);
53         if (!NT_STATUS_IS_OK(result)) {
54                 goto out;
55         }
56
57         if ((fd = sys_open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
58                 result = map_nt_error_from_unix(errno);
59                 goto out;
60         }
61
62         if ((data = (char *)SMB_MALLOC(read_size)) == NULL) {
63                 result = NT_STATUS_NO_MEMORY;
64                 goto out;
65         }
66
67         while (1) {
68
69                 int n = cli_read(cli, fnum, data, nread, read_size);
70
71                 if (n <= 0)
72                         break;
73
74                 if (write(fd, data, n) != n) {
75                         break;
76                 }
77
78                 nread += n;
79         }
80
81         result = NT_STATUS_OK;
82
83  out:
84         SAFE_FREE(data);
85         if (fnum) {
86                 cli_close(cli, fnum);
87         }
88         if (fd != -1) {
89                 close(fd);
90         }
91
92         return result;
93 }
94
95 /****************************************************************
96  copy dir
97 ****************************************************************/
98
99 static NTSTATUS gpo_copy_dir(const char *unix_path)
100 {
101         if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
102                 return map_nt_error_from_unix(errno);
103         }
104
105         return NT_STATUS_OK;
106 }
107
108 /****************************************************************
109  sync files
110 ****************************************************************/
111
112 static NTSTATUS gpo_sync_files(struct sync_context *ctx)
113 {
114         NTSTATUS status;
115
116         DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
117
118         status = cli_list(ctx->cli, ctx->mask, ctx->attribute, gpo_sync_func,
119                           ctx);
120         if (!NT_STATUS_IS_OK(status)) {
121                 DEBUG(1, ("listing [%s] failed with error: %s\n",
122                           ctx->mask, nt_errstr(status)));
123                 return status;
124         }
125
126         return status;
127 }
128
129 /****************************************************************
130  syncronisation call back
131 ****************************************************************/
132
133 static NTSTATUS gpo_sync_func(const char *mnt,
134                           struct file_info *info,
135                           const char *mask,
136                           void *state)
137 {
138         NTSTATUS result;
139         struct sync_context *ctx;
140         fstring nt_filename, unix_filename;
141         fstring nt_dir, unix_dir;
142         char *old_nt_dir, *old_unix_dir;
143
144         ctx = (struct sync_context *)state;
145
146         if (strequal(info->name, ".") || strequal(info->name, "..")) {
147                 return NT_STATUS_OK;
148         }
149
150         DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
151                 mask, info->name));
152
153         if (info->mode & FILE_ATTRIBUTE_DIRECTORY) {
154
155                 DEBUG(3,("got dir: [%s]\n", info->name));
156
157                 fstrcpy(nt_dir, ctx->remote_path);
158                 fstrcat(nt_dir, "\\");
159                 fstrcat(nt_dir, info->name);
160
161                 fstrcpy(unix_dir, ctx->local_path);
162                 fstrcat(unix_dir, "/");
163                 fstrcat(unix_dir, info->name);
164
165                 result = gpo_copy_dir(unix_dir);
166                 if (!NT_STATUS_IS_OK(result)) {
167                         DEBUG(1,("failed to copy dir: %s\n",
168                                 nt_errstr(result)));
169                         return result;
170                 }
171
172                 old_nt_dir = ctx->remote_path;
173                 ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
174
175                 old_unix_dir = ctx->local_path;
176                 ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
177
178                 ctx->mask = talloc_asprintf(ctx->mem_ctx,
179                                         "%s\\*",
180                                         nt_dir);
181                 if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
182                         DEBUG(0,("gpo_sync_func: ENOMEM\n"));
183                         return NT_STATUS_NO_MEMORY;
184                 }
185                 result = gpo_sync_files(ctx);
186                 if (!NT_STATUS_IS_OK(result)) {
187                         DEBUG(0,("could not sync files\n"));
188                         return result;
189                 }
190
191                 ctx->remote_path = old_nt_dir;
192                 ctx->local_path = old_unix_dir;
193                 return NT_STATUS_OK;
194         }
195
196         DEBUG(3,("got file: [%s]\n", info->name));
197
198         fstrcpy(nt_filename, ctx->remote_path);
199         fstrcat(nt_filename, "\\");
200         fstrcat(nt_filename, info->name);
201
202         fstrcpy(unix_filename, ctx->local_path);
203         fstrcat(unix_filename, "/");
204         fstrcat(unix_filename, info->name);
205
206         result = gpo_copy_file(ctx->mem_ctx, ctx->cli,
207                                nt_filename, unix_filename);
208         if (!NT_STATUS_IS_OK(result)) {
209                 DEBUG(1,("failed to copy file: %s\n",
210                         nt_errstr(result)));
211         }
212         return result;
213 }
214
215
216 /****************************************************************
217  list a remote directory and download recursivly
218 ****************************************************************/
219
220 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
221                               struct cli_state *cli,
222                               const char *nt_path,
223                               const char *local_path)
224 {
225         struct sync_context ctx;
226
227         ctx.mem_ctx     = mem_ctx;
228         ctx.cli         = cli;
229         ctx.remote_path = discard_const_p(char, nt_path);
230         ctx.local_path  = discard_const_p(char, local_path);
231         ctx.attribute   = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
232
233         ctx.mask = talloc_asprintf(mem_ctx,
234                                 "%s\\*",
235                                 nt_path);
236         if (!ctx.mask) {
237                 return NT_STATUS_NO_MEMORY;
238         }
239
240         return gpo_sync_files(&ctx);
241 }