Remove extraneous comment from 0a33d8bd312cc4497d08bbe0f4dd2abcce67bd0b
[samba.git] / source3 / utils / profiles.c
index 9629dffaea2fa496cb4da58aba24b0e8375a7cc7..0b8a0d4278386e968bdbfc2590e251cb532eb864 100644 (file)
@@ -7,7 +7,7 @@
 
    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,
@@ -16,8 +16,7 @@
    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"
 
 DOM_SID old_sid, new_sid;
 int change = 0, new_val = 0;
+int opt_verbose = False;
 
+/********************************************************************
+********************************************************************/
+
+static void verbose_output(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
+static void verbose_output(const char *format, ...)
+{
+       va_list args;
+       char *var = NULL;
+
+       if (!opt_verbose) {
+               return;
+       }
+
+       va_start(args, format);
+       if ((vasprintf(&var, format, args)) == -1) {
+               va_end(args);
+               return;
+       }
+
+       fprintf(stdout, var);
+       va_end(args);
+       SAFE_FREE(var);
+}
 
 /********************************************************************
 ********************************************************************/
 
-static BOOL swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
+static bool swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
 {
-       SEC_ACL *acl = sd->dacl;
+       SEC_ACL *acl;
        int i;
-       BOOL update = False;
+       bool update = False;
 
+       verbose_output("  Owner SID: %s\n", sid_string_tos(sd->owner_sid));
        if ( sid_equal( sd->owner_sid, s1 ) ) {
                sid_copy( sd->owner_sid, s2 );
                update = True;
+               verbose_output("  New Owner SID: %s\n", 
+                       sid_string_tos(sd->owner_sid));
+
        }
 
-       if ( sid_equal( sd->grp_sid, s1 ) ) {
-               sid_copy( sd->grp_sid, s2 );
+       verbose_output("  Group SID: %s\n", sid_string_tos(sd->group_sid));
+       if ( sid_equal( sd->group_sid, s1 ) ) {
+               sid_copy( sd->group_sid, s2 );
                update = True;
+               verbose_output("  New Group SID: %s\n", 
+                       sid_string_tos(sd->group_sid));
        }
 
+       acl = sd->dacl;
+       verbose_output("  DACL: %d entries:\n", acl->num_aces);
        for ( i=0; i<acl->num_aces; i++ ) {
-               if ( sid_equal( &acl->ace[i].trustee, s1 ) ) {
-                       sid_copy( &acl->ace[i].trustee, s2 );
+               verbose_output("    Trustee SID: %s\n", 
+                       sid_string_tos(&acl->aces[i].trustee));
+               if ( sid_equal( &acl->aces[i].trustee, s1 ) ) {
+                       sid_copy( &acl->aces[i].trustee, s2 );
                        update = True;
+                       verbose_output("    New Trustee SID: %s\n", 
+                               sid_string_tos(&acl->aces[i].trustee));
                }
        }
 
+#if 0
+       acl = sd->sacl;
+       verbose_output("  SACL: %d entries: \n", acl->num_aces);
+       for ( i=0; i<acl->num_aces; i++ ) {
+               verbose_output("    Trustee SID: %s\n", 
+                       sid_string_tos(&acl->aces[i].trustee));
+               if ( sid_equal( &acl->aces[i].trustee, s1 ) ) {
+                       sid_copy( &acl->aces[i].trustee, s2 );
+                       update = True;
+                       verbose_output("    New Trustee SID: %s\n", 
+                               sid_string_tos(&acl->aces[i].trustee));
+               }
+       }
+#endif
        return update;
 }
 
 /********************************************************************
 ********************************************************************/
 
-static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
+static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
                                 REGF_NK_REC *parent, REGF_FILE *outfile,
                                 const char *parentpath  )
 {
@@ -70,7 +120,7 @@ static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
        REGVAL_CTR *values;
        REGSUBKEY_CTR *subkeys;
        int i;
-       pstring path;
+       char *path;
 
        /* swap out the SIDs in the security descriptor */
 
@@ -79,8 +129,8 @@ static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
                return False;
        }
 
-       if ( swap_sid_in_acl( new_sd, &old_sid, &new_sid ) )
-               DEBUG(2,("Updating ACL for %s\n", nk->keyname ));
+       verbose_output("ACL for %s%s%s\n", parentpath, parent ? "\\" : "", nk->keyname);
+       swap_sid_in_acl( new_sd, &old_sid, &new_sid );
 
        if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
                DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
@@ -88,6 +138,7 @@ static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
        }
 
        if ( !(values = TALLOC_ZERO_P( subkeys, REGVAL_CTR )) ) {
+               TALLOC_FREE( subkeys );
                DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
                return False;
        }
@@ -109,19 +160,26 @@ static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
 
        /* write each one of the subkeys out */
 
-       pstr_sprintf( path, "%s%s%s", parentpath, parent ? "\\" : "", nk->keyname );
-       
+       path = talloc_asprintf(subkeys, "%s%s%s",
+                       parentpath, parent ? "\\" : "",nk->keyname);
+       if (!path) {
+               TALLOC_FREE( subkeys );
+               return false;
+       }
+
        nk->subkey_index = 0;
-       while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
-               if ( !copy_registry_tree( infile, subkey, key, outfile, path ) )
-                       return False;
+       while ((subkey = regfio_fetch_subkey(infile, nk))) {
+               if (!copy_registry_tree( infile, subkey, key, outfile, path)) {
+                       TALLOC_FREE(subkeys);
+                       return false;
+               }
        }
 
        /* values is a talloc()'d child of subkeys here so just throw it all away */
 
        TALLOC_FREE( subkeys );
 
-       DEBUG(1,("[%s]\n", path));
+       verbose_output("[%s]\n", path);
 
        return True;
 }
@@ -131,14 +189,16 @@ static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
 
 int main( int argc, char *argv[] )
 {
+       TALLOC_CTX *frame = talloc_stackframe();
        int opt;
        REGF_FILE *infile, *outfile;
        REGF_NK_REC *nk;
-       pstring orig_filename, new_filename;
+       char *orig_filename, *new_filename;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
                { "change-sid", 'c', POPT_ARG_STRING, NULL, 'c', "Provides SID to change" },
                { "new-sid", 'n', POPT_ARG_STRING, NULL, 'n', "Provides SID to change to" },
+               { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 'v', "Verbose output" },
                POPT_COMMON_SAMBA
                POPT_COMMON_VERSION
                POPT_TABLEEND
@@ -153,7 +213,7 @@ int main( int argc, char *argv[] )
        dbf = x_stderr;
        x_setbuf( x_stderr, NULL );
 
-       pc = poptGetContext("profiles", argc, (const char **)argv, long_options, 
+       pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
                POPT_CONTEXT_KEEP_FIRST);
 
        poptSetOtherOptionHelp(pc, "<profilefile>");
@@ -183,7 +243,7 @@ int main( int argc, char *argv[] )
                }
        }
 
-       poptGetArg(pc); 
+       poptGetArg(pc);
 
        if (!poptPeekArg(pc)) {
                poptPrintUsage(pc, stderr, 0);
@@ -196,36 +256,48 @@ int main( int argc, char *argv[] )
                exit(252);
        }
 
-       pstrcpy( orig_filename, poptPeekArg(pc) );
-       pstr_sprintf( new_filename, "%s.new", orig_filename );
-       
-       if ( !(infile = regfio_open( orig_filename, O_RDONLY, 0 )) ) {
+       orig_filename = talloc_strdup(frame, poptPeekArg(pc));
+       if (!orig_filename) {
+               exit(ENOMEM);
+       }
+       new_filename = talloc_asprintf(frame,
+                                       "%s.new",
+                                       orig_filename);
+       if (!new_filename) {
+               exit(ENOMEM);
+       }
+
+       if (!(infile = regfio_open( orig_filename, O_RDONLY, 0))) {
                fprintf( stderr, "Failed to open %s!\n", orig_filename );
                fprintf( stderr, "Error was (%s)\n", strerror(errno) );
                exit (1);
        }
-       
+
        if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
                fprintf( stderr, "Failed to open new file %s!\n", new_filename );
                fprintf( stderr, "Error was (%s)\n", strerror(errno) );
                exit (1);
        }
-       
+
        /* actually do the update now */
-       
-       nk = regfio_rootkey( infile );
-       
-       if ( !copy_registry_tree( infile, nk, NULL, outfile, "" ) ) {
+
+       if ((nk = regfio_rootkey( infile )) == NULL) {
+               fprintf(stderr, "Could not get rootkey\n");
+               exit(3);
+       }
+
+       if (!copy_registry_tree( infile, nk, NULL, outfile, "")) {
                fprintf(stderr, "Failed to write updated registry file!\n");
                exit(2);
        }
-       
+
        /* cleanup */
-       
-       regfio_close( infile );
-       regfio_close( outfile );
+
+       regfio_close(infile);
+       regfio_close(outfile);
 
        poptFreeContext(pc);
 
-       return( 0 );
+       TALLOC_FREE(frame);
+       return 0;
 }