/samba-3.5.6/source4/libgpo/gpo_filesync.c

https://github.com/theuni/XBMC-deps · C · 241 lines · 161 code · 48 blank · 32 comment · 28 complexity · 58d942c5553bc2466f6cb643fe8b4bfb MD5 · raw file

  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. #include "includes.h"
  22. #include "libcli/libcli.h"
  23. #include "system/filesys.h"
  24. struct sync_context {
  25. TALLOC_CTX *mem_ctx;
  26. struct smbcli_state *cli;
  27. char *remote_path;
  28. char *local_path;
  29. char *mask;
  30. uint16_t attribute;
  31. };
  32. static void gpo_sync_func(struct clilist_file_info *info,
  33. const char *mask,
  34. void *state);
  35. NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
  36. struct smbcli_state *cli,
  37. const char *nt_path,
  38. const char *unix_path)
  39. {
  40. NTSTATUS result;
  41. int fnum;
  42. int fd = 0;
  43. char *data = NULL;
  44. static int io_bufsize = 64512;
  45. int read_size = io_bufsize;
  46. off_t nread = 0;
  47. if ((fnum = smbcli_open(cli->tree, nt_path, O_RDONLY, DENY_NONE)) == -1) {
  48. result = NT_STATUS_NO_SUCH_FILE;
  49. goto out;
  50. }
  51. if ((fd = open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
  52. result = map_nt_error_from_unix(errno);
  53. goto out;
  54. }
  55. if ((data = talloc_size(mem_ctx, read_size)) == NULL) {
  56. result = NT_STATUS_NO_MEMORY;
  57. goto out;
  58. }
  59. while (1) {
  60. int n = smbcli_read(cli->tree, fnum, data, nread, read_size);
  61. if (n <= 0)
  62. break;
  63. if (write(fd, data, n) != n) {
  64. break;
  65. }
  66. nread += n;
  67. }
  68. result = NT_STATUS_OK;
  69. out:
  70. SAFE_FREE(data);
  71. if (fnum) {
  72. smbcli_close(cli->tree, fnum);
  73. }
  74. if (fd) {
  75. close(fd);
  76. }
  77. return result;
  78. }
  79. /****************************************************************
  80. copy dir
  81. ****************************************************************/
  82. static NTSTATUS gpo_copy_dir(const char *nt_dir, const char *unix_path)
  83. {
  84. if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
  85. return NT_STATUS_ACCESS_DENIED;
  86. }
  87. return NT_STATUS_OK;
  88. }
  89. /****************************************************************
  90. sync files
  91. ****************************************************************/
  92. static bool gpo_sync_files(struct sync_context *ctx)
  93. {
  94. DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
  95. if (smbcli_list(ctx->cli->tree,
  96. ctx->mask,
  97. ctx->attribute,
  98. gpo_sync_func,
  99. ctx) == -1) {
  100. DEBUG(1,("listing [%s] failed with error: %s\n",
  101. ctx->mask, smbcli_errstr(ctx->cli->tree)));
  102. return false;
  103. }
  104. return true;
  105. }
  106. /****************************************************************
  107. syncronisation call back
  108. ****************************************************************/
  109. static void gpo_sync_func(struct clilist_file_info *info,
  110. const char *mask,
  111. void *state)
  112. {
  113. NTSTATUS result;
  114. struct sync_context *ctx;
  115. char *nt_filename, *unix_filename;
  116. char *nt_dir, *unix_dir;
  117. char *old_nt_dir, *old_unix_dir;
  118. ctx = (struct sync_context *)state;
  119. if (strequal(info->name, ".") || strequal(info->name, "..")) {
  120. return;
  121. }
  122. DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
  123. mask, info->name));
  124. if (info->attrib & FILE_ATTRIBUTE_DIRECTORY) {
  125. DEBUG(3,("got dir: [%s]\n", info->name));
  126. nt_dir = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
  127. ctx->remote_path,
  128. info->name);
  129. unix_dir = talloc_asprintf(ctx->mem_ctx, "%s/%s",
  130. ctx->local_path,
  131. info->name);
  132. result = gpo_copy_dir(nt_dir, unix_dir);
  133. if (!NT_STATUS_IS_OK(result)) {
  134. DEBUG(1,("failed to copy dir: %s\n",
  135. nt_errstr(result)));
  136. }
  137. old_nt_dir = ctx->remote_path;
  138. ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
  139. old_unix_dir = ctx->local_path;
  140. ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
  141. ctx->mask = talloc_asprintf(ctx->mem_ctx,
  142. "%s\\*",
  143. nt_dir);
  144. if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
  145. DEBUG(0,("gpo_sync_func: ENOMEM\n"));
  146. return;
  147. }
  148. if (!gpo_sync_files(ctx)) {
  149. DEBUG(0,("could not sync files\n"));
  150. }
  151. ctx->remote_path = old_nt_dir;
  152. ctx->local_path = old_unix_dir;
  153. return;
  154. }
  155. DEBUG(3,("got file: [%s]\n", info->name));
  156. nt_filename = talloc_asprintf(ctx->mem_ctx, "%s\\%s",
  157. ctx->remote_path,
  158. info->name);
  159. unix_filename = talloc_asprintf(ctx->mem_ctx, "%s/%s",
  160. ctx->local_path,
  161. info->name);
  162. result = gpo_copy_file(ctx->mem_ctx, ctx->cli,
  163. nt_filename, unix_filename);
  164. if (!NT_STATUS_IS_OK(result)) {
  165. DEBUG(1,("failed to copy file: %s\n",
  166. nt_errstr(result)));
  167. }
  168. }
  169. /****************************************************************
  170. list a remote directory and download recursivly
  171. ****************************************************************/
  172. NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
  173. struct smbcli_state *cli,
  174. const char *nt_path,
  175. const char *local_path)
  176. {
  177. struct sync_context ctx;
  178. ctx.mem_ctx = mem_ctx;
  179. ctx.cli = cli;
  180. ctx.remote_path = discard_const_p(char, nt_path);
  181. ctx.local_path = discard_const_p(char, local_path);
  182. ctx.attribute = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
  183. ctx.mask = talloc_asprintf(mem_ctx,
  184. "%s\\*",
  185. nt_path);
  186. if (!ctx.mask) {
  187. return NT_STATUS_NO_MEMORY;
  188. }
  189. if (!gpo_sync_files(&ctx)) {
  190. return NT_STATUS_NO_SUCH_FILE;
  191. }
  192. return NT_STATUS_OK;
  193. }