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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "libsmb/libsmb.h"
23 #include "../libgpo/gpo.h"
24 #include "libgpo/gpo_proto.h"
25 #include "lib/util/string_wrappers.h"
29 struct cli_state
*cli
;
36 static NTSTATUS
gpo_sync_func(struct file_info
*info
,
40 NTSTATUS
gpo_copy_file(TALLOC_CTX
*mem_ctx
,
41 struct cli_state
*cli
,
43 const char *unix_path
)
49 static int io_bufsize
= 64512;
50 int read_size
= io_bufsize
;
53 result
= cli_open(cli
, nt_path
, O_RDONLY
, DENY_NONE
, &fnum
);
54 if (!NT_STATUS_IS_OK(result
)) {
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
= (char *)SMB_MALLOC(read_size
)) == NULL
) {
64 result
= NT_STATUS_NO_MEMORY
;
71 result
= cli_read(cli
, fnum
, data
, nread
, read_size
, &n
);
72 if (!NT_STATUS_IS_OK(result
)) {
79 if (write(fd
, data
, n
) != n
) {
86 result
= NT_STATUS_OK
;
100 /****************************************************************
102 ****************************************************************/
104 static NTSTATUS
gpo_copy_dir(const char *unix_path
)
106 if ((mkdir(unix_path
, 0644)) < 0 && errno
!= EEXIST
) {
107 return map_nt_error_from_unix(errno
);
113 /****************************************************************
115 ****************************************************************/
117 static NTSTATUS
gpo_sync_files(struct sync_context
*ctx
)
121 DEBUG(3,("calling cli_list with mask: %s\n", ctx
->mask
));
123 status
= cli_list(ctx
->cli
, ctx
->mask
, ctx
->attribute
, gpo_sync_func
,
125 if (!NT_STATUS_IS_OK(status
)) {
126 DEBUG(1, ("listing [%s] failed with error: %s\n",
127 ctx
->mask
, nt_errstr(status
)));
134 /****************************************************************
135 syncronisation call back
136 ****************************************************************/
138 static NTSTATUS
gpo_sync_func(struct file_info
*info
,
143 struct sync_context
*ctx
;
144 fstring nt_filename
, unix_filename
;
145 fstring nt_dir
, unix_dir
;
146 char *old_nt_dir
, *old_unix_dir
;
148 ctx
= (struct sync_context
*)state
;
150 if (strequal(info
->name
, ".") || strequal(info
->name
, "..")) {
154 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
157 if (info
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
159 DEBUG(3,("got dir: [%s]\n", info
->name
));
161 fstrcpy(nt_dir
, ctx
->remote_path
);
162 fstrcat(nt_dir
, "\\");
163 fstrcat(nt_dir
, info
->name
);
165 fstrcpy(unix_dir
, ctx
->local_path
);
166 fstrcat(unix_dir
, "/");
167 fstrcat(unix_dir
, info
->name
);
169 result
= gpo_copy_dir(unix_dir
);
170 if (!NT_STATUS_IS_OK(result
)) {
171 DEBUG(1,("failed to copy dir: %s\n",
176 old_nt_dir
= ctx
->remote_path
;
177 ctx
->remote_path
= talloc_strdup(ctx
->mem_ctx
, nt_dir
);
179 old_unix_dir
= ctx
->local_path
;
180 ctx
->local_path
= talloc_strdup(ctx
->mem_ctx
, unix_dir
);
182 ctx
->mask
= talloc_asprintf(ctx
->mem_ctx
,
185 if (!ctx
->local_path
|| !ctx
->mask
|| !ctx
->remote_path
) {
186 DEBUG(0,("gpo_sync_func: ENOMEM\n"));
187 return NT_STATUS_NO_MEMORY
;
189 result
= gpo_sync_files(ctx
);
190 if (!NT_STATUS_IS_OK(result
)) {
191 DEBUG(0,("could not sync files\n"));
195 ctx
->remote_path
= old_nt_dir
;
196 ctx
->local_path
= old_unix_dir
;
200 DEBUG(3,("got file: [%s]\n", info
->name
));
202 fstrcpy(nt_filename
, ctx
->remote_path
);
203 fstrcat(nt_filename
, "\\");
204 fstrcat(nt_filename
, info
->name
);
206 fstrcpy(unix_filename
, ctx
->local_path
);
207 fstrcat(unix_filename
, "/");
208 fstrcat(unix_filename
, info
->name
);
210 result
= gpo_copy_file(ctx
->mem_ctx
, ctx
->cli
,
211 nt_filename
, unix_filename
);
212 if (!NT_STATUS_IS_OK(result
)) {
213 DEBUG(1,("failed to copy file: %s\n",
220 /****************************************************************
221 list a remote directory and download recursively
222 ****************************************************************/
224 NTSTATUS
gpo_sync_directories(TALLOC_CTX
*mem_ctx
,
225 struct cli_state
*cli
,
227 const char *local_path
)
229 struct sync_context ctx
;
231 ctx
.mem_ctx
= mem_ctx
;
233 ctx
.remote_path
= discard_const_p(char, nt_path
);
234 ctx
.local_path
= discard_const_p(char, local_path
);
235 ctx
.attribute
= (FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
237 ctx
.mask
= talloc_asprintf(mem_ctx
,
241 return NT_STATUS_NO_MEMORY
;
244 return gpo_sync_files(&ctx
);