s3-libgpo: move group policy protos to where they belong.
[sfrench/samba-autobuild/.git] / source3 / libgpo / gpo_filesync.c
index 36c538dc51225029ea0ccc6c3bebddeb0887e7fd..a3002fe860667b3c8c7468a5700d1c438006b47d 100644 (file)
@@ -1,38 +1,39 @@
-/* 
+/*
  *  Unix SMB/CIFS implementation.
  *  Group Policy Object Support
  *  Copyright (C) Guenther Deschner 2006
- *  
+ *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
- *  
+ *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *  
+ *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
+#include "../libgpo/gpo.h"
+#include "libgpo/gpo_proto.h"
 
 struct sync_context {
        TALLOC_CTX *mem_ctx;
        struct cli_state *cli;
        char *remote_path;
        char *local_path;
-       pstring mask;
-       uint16 attribute;
+       char *mask;
+       uint16_t attribute;
 };
 
 static void gpo_sync_func(const char *mnt,
-                          file_info *info,
-                          const char *mask,
-                          void *state);
+                         file_info *info,
+                         const char *mask,
+                         void *state);
 
 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
                       struct cli_state *cli,
@@ -40,15 +41,15 @@ NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
                       const char *unix_path)
 {
        NTSTATUS result;
-       int fnum, fd;
+       uint16_t fnum;
+       int fd = -1;
        char *data = NULL;
        static int io_bufsize = 64512;
        int read_size = io_bufsize;
-       off_t start = 0;
        off_t nread = 0;
 
-       if ((fnum = cli_open(cli, nt_path, O_RDONLY, DENY_NONE)) == -1) {
-               result = NT_STATUS_NO_SUCH_FILE;
+       result = cli_open(cli, nt_path, O_RDONLY, DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(result)) {
                goto out;
        }
 
@@ -56,7 +57,7 @@ NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
                result = map_nt_error_from_unix(errno);
                goto out;
        }
-        
+
        if ((data = (char *)SMB_MALLOC(read_size)) == NULL) {
                result = NT_STATUS_NO_MEMORY;
                goto out;
@@ -64,7 +65,7 @@ NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
 
        while (1) {
 
-               int n = cli_read(cli, fnum, data, nread + start, read_size);
+               int n = cli_read(cli, fnum, data, nread, read_size);
 
                if (n <= 0)
                        break;
@@ -83,7 +84,7 @@ NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
        if (fnum) {
                cli_close(cli, fnum);
        }
-       if (fd) {
+       if (fd != -1) {
                close(fd);
        }
 
@@ -107,17 +108,21 @@ static NTSTATUS gpo_copy_dir(const char *unix_path)
  sync files
 ****************************************************************/
 
-static BOOL gpo_sync_files(struct sync_context *ctx)
+static bool gpo_sync_files(struct sync_context *ctx)
 {
        DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
 
-       if (cli_list(ctx->cli, ctx->mask, ctx->attribute, gpo_sync_func, ctx) == -1) {
-               DEBUG(1,("listing [%s] failed with error: %s\n", 
+       if (cli_list(ctx->cli,
+                    ctx->mask,
+                    ctx->attribute,
+                    gpo_sync_func,
+                    ctx) == -1) {
+               DEBUG(1,("listing [%s] failed with error: %s\n",
                        ctx->mask, cli_errstr(ctx->cli)));
-               return False;
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 /****************************************************************
@@ -141,7 +146,7 @@ static void gpo_sync_func(const char *mnt,
                return;
        }
 
-       DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n", 
+       DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
                mask, info->name));
 
        if (info->mode & aDIR) {
@@ -158,18 +163,23 @@ static void gpo_sync_func(const char *mnt,
 
                result = gpo_copy_dir(unix_dir);
                if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(1,("failed to copy dir: %s\n", nt_errstr(result)));
+                       DEBUG(1,("failed to copy dir: %s\n",
+                               nt_errstr(result)));
                }
 
                old_nt_dir = ctx->remote_path;
-               ctx->remote_path = nt_dir;
-               
+               ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
+
                old_unix_dir = ctx->local_path;
                ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
 
-               pstrcpy(ctx->mask, nt_dir);
-               pstrcat(ctx->mask, "\\*");
-
+               ctx->mask = talloc_asprintf(ctx->mem_ctx,
+                                       "%s\\*",
+                                       nt_dir);
+               if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
+                       DEBUG(0,("gpo_sync_func: ENOMEM\n"));
+                       return;
+               }
                if (!gpo_sync_files(ctx)) {
                        DEBUG(0,("could not sync files\n"));
                }
@@ -189,9 +199,11 @@ static void gpo_sync_func(const char *mnt,
        fstrcat(unix_filename, "/");
        fstrcat(unix_filename, info->name);
 
-       result = gpo_copy_file(ctx->mem_ctx, ctx->cli, nt_filename, unix_filename);
+       result = gpo_copy_file(ctx->mem_ctx, ctx->cli,
+                              nt_filename, unix_filename);
        if (!NT_STATUS_IS_OK(result)) {
-               DEBUG(1,("failed to copy file: %s\n", nt_errstr(result)));
+               DEBUG(1,("failed to copy file: %s\n",
+                       nt_errstr(result)));
        }
 }
 
@@ -200,9 +212,9 @@ static void gpo_sync_func(const char *mnt,
  list a remote directory and download recursivly
 ****************************************************************/
 
-NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx, 
-                             struct cli_state *cli, 
-                             const char *nt_path, 
+NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
+                             struct cli_state *cli,
+                             const char *nt_path,
                              const char *local_path)
 {
        struct sync_context ctx;
@@ -213,8 +225,12 @@ NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
        ctx.local_path  = CONST_DISCARD(char *, local_path);
        ctx.attribute   = (aSYSTEM | aHIDDEN | aDIR);
 
-       pstrcpy(ctx.mask, nt_path);
-       pstrcat(ctx.mask, "\\*");
+       ctx.mask = talloc_asprintf(mem_ctx,
+                               "%s\\*",
+                               nt_path);
+       if (!ctx.mask) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        if (!gpo_sync_files(&ctx)) {
                return NT_STATUS_NO_SUCH_FILE;