s3/profiles: improve copy_registry_tree() errors
[samba.git] / source3 / utils / profiles.c
index 0ac93dedeb8beb6a0c9caf49353489bcc91466e1..0f274ad604a2aa94cf2cbcc29ee2f138ea58d9ae 100644 (file)
@@ -1,30 +1,34 @@
-/* 
-   Samba Unix/Linux SMB client utility profiles.c 
-   
-   Copyright (C) Richard Sharpe, <rsharpe@richardsharpe.com>   2002 
-   Copyright (C) Jelmer Vernooij (conversion to popt)          2003 
-   Copyright (C) Gerald (Jerry) Carter                         2005 
+/*
+   Samba Unix/Linux SMB client utility profiles.c
+
+   Copyright (C) Richard Sharpe, <rsharpe@richardsharpe.com>   2002
+   Copyright (C) Jelmer Vernooij (conversion to popt)          2003
+   Copyright (C) Gerald (Jerry) Carter                         2005
 
    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 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, see <http://www.gnu.org/licenses/>.  
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-                                  
+
 #include "includes.h"
-#include "regfio.h"
+#include "system/filesys.h"
+#include "popt_common.h"
+#include "registry/reg_objects.h"
+#include "registry/regfio.h"
+#include "../libcli/security/security.h"
 
 /* GLOBAL VARIABLES */
 
-DOM_SID old_sid, new_sid;
+struct dom_sid old_sid, new_sid;
 int change = 0, new_val = 0;
 int opt_verbose = False;
 
@@ -55,38 +59,38 @@ static void verbose_output(const char *format, ...)
 /********************************************************************
 ********************************************************************/
 
-static bool swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
+static bool swap_sid_in_acl( struct security_descriptor *sd, struct dom_sid *s1, struct dom_sid *s2 )
 {
-       SEC_ACL *theacl;
+       struct security_acl *theacl;
        int i;
        bool update = False;
 
        verbose_output("  Owner SID: %s\n", sid_string_tos(sd->owner_sid));
-       if ( sid_equal( sd->owner_sid, s1 ) ) {
+       if ( dom_sid_equal( sd->owner_sid, s1 ) ) {
                sid_copy( sd->owner_sid, s2 );
                update = True;
-               verbose_output("  New Owner SID: %s\n", 
+               verbose_output("  New Owner SID: %s\n",
                        sid_string_tos(sd->owner_sid));
 
        }
 
        verbose_output("  Group SID: %s\n", sid_string_tos(sd->group_sid));
-       if ( sid_equal( sd->group_sid, s1 ) ) {
+       if ( dom_sid_equal( sd->group_sid, s1 ) ) {
                sid_copy( sd->group_sid, s2 );
                update = True;
-               verbose_output("  New Group SID: %s\n", 
+               verbose_output("  New Group SID: %s\n",
                        sid_string_tos(sd->group_sid));
        }
 
        theacl = sd->dacl;
        verbose_output("  DACL: %d entries:\n", theacl->num_aces);
        for ( i=0; i<theacl->num_aces; i++ ) {
-               verbose_output("    Trustee SID: %s\n", 
+               verbose_output("    Trustee SID: %s\n",
                        sid_string_tos(&theacl->aces[i].trustee));
-               if ( sid_equal( &theacl->aces[i].trustee, s1 ) ) {
+               if ( dom_sid_equal( &theacl->aces[i].trustee, s1 ) ) {
                        sid_copy( &theacl->aces[i].trustee, s2 );
                        update = True;
-                       verbose_output("    New Trustee SID: %s\n", 
+                       verbose_output("    New Trustee SID: %s\n",
                                sid_string_tos(&theacl->aces[i].trustee));
                }
        }
@@ -95,12 +99,12 @@ static bool swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
        theacl = sd->sacl;
        verbose_output("  SACL: %d entries: \n", theacl->num_aces);
        for ( i=0; i<theacl->num_aces; i++ ) {
-               verbose_output("    Trustee SID: %s\n", 
+               verbose_output("    Trustee SID: %s\n",
                        sid_string_tos(&theacl->aces[i].trustee));
-               if ( sid_equal( &theacl->aces[i].trustee, s1 ) ) {
+               if ( dom_sid_equal( &theacl->aces[i].trustee, s1 ) ) {
                        sid_copy( &theacl->aces[i].trustee, s2 );
                        update = True;
-                       verbose_output("    New Trustee SID: %s\n", 
+                       verbose_output("    New Trustee SID: %s\n",
                                sid_string_tos(&theacl->aces[i].trustee));
                }
        }
@@ -116,8 +120,8 @@ static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
                                 const char *parentpath  )
 {
        REGF_NK_REC *key, *subkey;
-       SEC_DESC *new_sd;
-       REGVAL_CTR *values;
+       struct security_descriptor *new_sd;
+       struct regval_ctr *values;
        struct regsubkey_ctr *subkeys;
        int i;
        char *path;
@@ -125,8 +129,15 @@ static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
 
        /* swap out the SIDs in the security descriptor */
 
-       if ( !(new_sd = dup_sec_desc( outfile->mem_ctx, nk->sec_desc->sec_desc )) ) {
-               fprintf( stderr, "Failed to copy security descriptor!\n" );
+       if (nk->sec_desc->sec_desc == NULL) {
+               fprintf(stderr, "Invalid (NULL) security descriptor!\n");
+               return false;
+       }
+
+       new_sd = security_descriptor_copy(outfile->mem_ctx,
+                                         nk->sec_desc->sec_desc);
+       if (new_sd == NULL) {
+               fprintf(stderr, "Failed to copy security descriptor!\n");
                return False;
        }
 
@@ -139,17 +150,18 @@ static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
                return False;
        }
 
-       if ( !(values = TALLOC_ZERO_P( subkeys, REGVAL_CTR )) ) {
+       werr = regval_ctr_init(subkeys, &values);
+       if (!W_ERROR_IS_OK(werr)) {
                TALLOC_FREE( subkeys );
                DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
                return False;
        }
 
-       /* copy values into the REGVAL_CTR */
+       /* copy values into the struct regval_ctr */
 
        for ( i=0; i<nk->num_values; i++ ) {
                regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
-                       (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
+                       nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
        }
 
        /* copy subkeys into the struct regsubkey_ctr */
@@ -189,7 +201,7 @@ static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
 /*********************************************************************
 *********************************************************************/
 
-int main( int argc, char *argv[] )
+int main( int argc, const char *argv[] )
 {
        TALLOC_CTX *frame = talloc_stackframe();
        int opt;
@@ -211,11 +223,9 @@ int main( int argc, char *argv[] )
 
        /* setup logging options */
 
-       setup_logging( "profiles", True );
-       dbf = x_stderr;
-       x_setbuf( x_stderr, NULL );
+       setup_logging( "profiles", DEBUG_STDERR);
 
-       pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
+       pc = poptGetContext("profiles", argc, argv, long_options,
                POPT_CONTEXT_KEEP_FIRST);
 
        poptSetOtherOptionHelp(pc, "<profilefile>");
@@ -275,7 +285,8 @@ int main( int argc, char *argv[] )
                exit (1);
        }
 
-       if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
+       if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC),
+                                     (S_IRUSR|S_IWUSR) )) ) {
                fprintf( stderr, "Failed to open new file %s!\n", new_filename );
                fprintf( stderr, "Error was (%s)\n", strerror(errno) );
                exit (1);