| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * Group Policy Object Support
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (C) Guenther Deschner 2006
|
|---|
| 6 | * Copyright (C) Wilco Baan Hofman 2008
|
|---|
| 7 | *
|
|---|
| 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.
|
|---|
| 12 | *
|
|---|
| 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.
|
|---|
| 17 | *
|
|---|
| 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/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 | #include "libcli/libcli.h"
|
|---|
| 24 | #include "system/filesys.h"
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 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;
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | static void gpo_sync_func(struct clilist_file_info *info,
|
|---|
| 37 | const char *mask,
|
|---|
| 38 | void *state);
|
|---|
| 39 |
|
|---|
| 40 | NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
|
|---|
| 41 | struct smbcli_state *cli,
|
|---|
| 42 | const char *nt_path,
|
|---|
| 43 | const char *unix_path)
|
|---|
| 44 | {
|
|---|
| 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;
|
|---|
| 52 |
|
|---|
| 53 | if ((fnum = smbcli_open(cli->tree, nt_path, O_RDONLY, DENY_NONE)) == -1) {
|
|---|
| 54 | result = NT_STATUS_NO_SUCH_FILE;
|
|---|
| 55 | goto out;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 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;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | if ((data = talloc_size(mem_ctx, read_size)) == NULL) {
|
|---|
| 64 | result = NT_STATUS_NO_MEMORY;
|
|---|
| 65 | goto out;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | while (1) {
|
|---|
| 69 |
|
|---|
| 70 | int n = smbcli_read(cli->tree, 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 | smbcli_close(cli->tree, fnum);
|
|---|
| 88 | }
|
|---|
| 89 | if (fd) {
|
|---|
| 90 | close(fd);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | return result;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /****************************************************************
|
|---|
| 97 | copy dir
|
|---|
| 98 | ****************************************************************/
|
|---|
| 99 |
|
|---|
| 100 | static NTSTATUS gpo_copy_dir(const char *nt_dir, const char *unix_path)
|
|---|
| 101 | {
|
|---|
| 102 | if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
|
|---|
| 103 | return NT_STATUS_ACCESS_DENIED;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | return NT_STATUS_OK;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /****************************************************************
|
|---|
| 110 | sync files
|
|---|
| 111 | ****************************************************************/
|
|---|
| 112 |
|
|---|
| 113 | static bool gpo_sync_files(struct sync_context *ctx)
|
|---|
| 114 | {
|
|---|
| 115 | DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
|
|---|
| 116 |
|
|---|
| 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;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | return true;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /****************************************************************
|
|---|
| 131 | syncronisation call back
|
|---|
| 132 | ****************************************************************/
|
|---|
| 133 |
|
|---|
| 134 | static void gpo_sync_func(struct clilist_file_info *info,
|
|---|
| 135 | const char *mask,
|
|---|
| 136 | void *state)
|
|---|
| 137 | {
|
|---|
| 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;
|
|---|
| 143 |
|
|---|
| 144 | ctx = (struct sync_context *)state;
|
|---|
| 145 |
|
|---|
| 146 | if (strequal(info->name, ".") || strequal(info->name, "..")) {
|
|---|
| 147 | return;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
|
|---|
| 151 | mask, info->name));
|
|---|
| 152 |
|
|---|
| 153 | if (info->attrib & FILE_ATTRIBUTE_DIRECTORY) {
|
|---|
| 154 |
|
|---|
| 155 | DEBUG(3,("got dir: [%s]\n", info->name));
|
|---|
| 156 |
|
|---|
| 157 | nt_dir = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
|
|---|
| 158 | ctx->remote_path,
|
|---|
| 159 | info->name);
|
|---|
| 160 |
|
|---|
| 161 | unix_dir = talloc_asprintf(ctx->mem_ctx, "%s/%s",
|
|---|
| 162 | ctx->local_path,
|
|---|
| 163 | info->name);
|
|---|
| 164 |
|
|---|
| 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)));
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | old_nt_dir = ctx->remote_path;
|
|---|
| 172 | ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
|
|---|
| 173 |
|
|---|
| 174 | old_unix_dir = ctx->local_path;
|
|---|
| 175 | ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
|
|---|
| 176 |
|
|---|
| 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;
|
|---|
| 183 | }
|
|---|
| 184 | if (!gpo_sync_files(ctx)) {
|
|---|
| 185 | DEBUG(0,("could not sync files\n"));
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | ctx->remote_path = old_nt_dir;
|
|---|
| 189 | ctx->local_path = old_unix_dir;
|
|---|
| 190 | return;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | DEBUG(3,("got file: [%s]\n", info->name));
|
|---|
| 194 |
|
|---|
| 195 | nt_filename = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
|
|---|
| 196 | ctx->remote_path,
|
|---|
| 197 | info->name);
|
|---|
| 198 |
|
|---|
| 199 | unix_filename = talloc_asprintf(ctx->mem_ctx, "%s/%s",
|
|---|
| 200 | ctx->local_path,
|
|---|
| 201 | info->name);
|
|---|
| 202 |
|
|---|
| 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)));
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | /****************************************************************
|
|---|
| 213 | list a remote directory and download recursivly
|
|---|
| 214 | ****************************************************************/
|
|---|
| 215 |
|
|---|
| 216 | NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
|
|---|
| 217 | struct smbcli_state *cli,
|
|---|
| 218 | const char *nt_path,
|
|---|
| 219 | const char *local_path)
|
|---|
| 220 | {
|
|---|
| 221 | struct sync_context ctx;
|
|---|
| 222 |
|
|---|
| 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);
|
|---|
| 228 |
|
|---|
| 229 | ctx.mask = talloc_asprintf(mem_ctx,
|
|---|
| 230 | "%s\\*",
|
|---|
| 231 | nt_path);
|
|---|
| 232 | if (!ctx.mask) {
|
|---|
| 233 | return NT_STATUS_NO_MEMORY;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | if (!gpo_sync_files(&ctx)) {
|
|---|
| 237 | return NT_STATUS_NO_SUCH_FILE;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | return NT_STATUS_OK;
|
|---|
| 241 | }
|
|---|