r14681: Get rid of hardcoded /tmp/add.ldif and /tmp/mod.ldif files. Is there a
authorJim McDonough <jmcd@samba.org>
Thu, 23 Mar 2006 16:39:37 +0000 (16:39 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 16:15:43 +0000 (11:15 -0500)
different directory the temp files should be in, or is /tmp ok?

Still have to get rid of the output file hardcoding, but that is to
come, because I need to cleanup stdout.
(This used to be commit 0d4bd93a5ca4025bbdeb507f4a2d6217cfb39c79)

source3/utils/net_rpc_samsync.c

index 9b001e02f322c084f6b559350902e6f82bfceb34..ae8d2cdc081f282413f9ef5122fec46ef8c15c6c 100644 (file)
@@ -1719,7 +1719,9 @@ static NTSTATUS fetch_database_to_ldif(struct rpc_pipe_client *pipe_hnd,
 {
        char *suffix;
        const char *builtin_sid = "S-1-5-32";
-       char *ldif_file;
+       char *ldif_file, *add_ldif, *mod_ldif;
+       const char *add_template = "/tmp/add.ldif.XXXXXX";
+       const char *mod_template = "/tmp/mod.ldif.XXXXXX";
        fstring sid, domainname;
        uint32 sync_context = 0;
        NTSTATUS ret = NT_STATUS_OK, result;
@@ -1728,7 +1730,6 @@ static NTSTATUS fetch_database_to_ldif(struct rpc_pipe_client *pipe_hnd,
        SAM_DELTA_HDR *hdr_deltas;
        SAM_DELTA_CTR *deltas;
        uint32 num_deltas;
-       const char *add_ldif = "/tmp/add.ldif", *mod_ldif = "/tmp/mod.ldif";
        FILE *add_fd = NULL, *mod_fd = NULL, *ldif_fd = NULL;
        char sys_cmd[1024];
        int num_alloced = 0, g_index = 0, a_index = 0, sys_cmd_result;
@@ -1751,18 +1752,20 @@ static NTSTATUS fetch_database_to_ldif(struct rpc_pipe_client *pipe_hnd,
        else
                ldif_file = talloc_strdup(mem_ctx, "/tmp/tmp.ldif");
        
-       if (ldif_file == NULL) {
+       add_ldif = talloc_strdup(mem_ctx, add_template);
+       mod_ldif = talloc_strdup(mem_ctx, mod_template);
+       if (!ldif_file || !add_ldif || !mod_ldif) {
                ret = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
        /* Open the add and mod ldif files */
-       if (!(add_fd = fopen(add_ldif, "a"))) {
+       if (!(add_fd = fdopen(smb_mkstemp(add_ldif),"w"))) {
                DEBUG(1, ("Could not open %s\n", add_ldif));
                ret = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
-       if (!(mod_fd = fopen(mod_ldif, "a"))) {
+       if (!(mod_fd = fdopen(smb_mkstemp(mod_ldif),"w"))) {
                DEBUG(1, ("Could not open %s\n", mod_ldif));
                ret = NT_STATUS_UNSUCCESSFUL;
                goto done;
@@ -1993,20 +1996,22 @@ static NTSTATUS fetch_database_to_ldif(struct rpc_pipe_client *pipe_hnd,
                goto done;
        }
 
-       /* Delete the temporary ldif files */
-       if (unlink(add_ldif))
-               d_fprintf(stderr, "unlink(%s) failed, error was (%s)\n",
-                         add_ldif, strerror(errno));
-       if (unlink(mod_ldif))
-               d_fprintf(stderr, "unlink(%s) failed, error was (%s)\n",
-                         mod_ldif, strerror(errno));
-
   done:
-       /* Close the ldif files */
+       /* Close and delete the ldif files */
        if (add_fd)
                fclose(add_fd);
+       if (strcmp(add_ldif, add_template) && (unlink(add_ldif))) {
+               DEBUG(1,("unlink(%s) failed, error was (%s)\n",
+                        add_ldif, strerror(errno)));
+       }
+
        if (mod_fd)
                fclose(mod_fd);
+       if (strcmp(mod_ldif, mod_template) && (unlink(mod_ldif))) {
+               DEBUG(1,("unlink(%s) failed, error was (%s)\n",
+                        mod_ldif, strerror(errno)));
+       }
+       
        if (ldif_fd)
                fclose(ldif_fd);