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