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