r9658: cleanup a few debug messages
[Samba/gbeck.git] / source / utils / profiles.c
blob284e15230a7f93dfbf58c103ed407c94e06a7e6b
1 /*
2 Samba Unix/Linux SMB client utility profiles.c
4 Copyright (C) Richard Sharpe, <rsharpe@richardsharpe.com> 2002
5 Copyright (C) Jelmer Vernooij (conversion to popt) 2003
6 Copyright (C) Gerald (Jerry) Carter 2005
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "regfio.h"
26 /* GLOBAL VARIABLES */
28 DOM_SID old_sid, new_sid;
29 int change = 0, new_val = 0;
32 /********************************************************************
33 ********************************************************************/
35 static BOOL swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
37 SEC_ACL *acl = sd->dacl;
38 int i;
39 BOOL update = False;
41 if ( sid_equal( sd->owner_sid, s1 ) ) {
42 sid_copy( sd->owner_sid, s2 );
43 update = True;
46 if ( sid_equal( sd->grp_sid, s1 ) ) {
47 sid_copy( sd->grp_sid, s2 );
48 update = True;
51 for ( i=0; i<acl->num_aces; i++ ) {
52 if ( sid_equal( &acl->ace[i].trustee, s1 ) ) {
53 sid_copy( &acl->ace[i].trustee, s2 );
54 update = True;
58 return update;
61 /********************************************************************
62 ********************************************************************/
64 static BOOL copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
65 REGF_NK_REC *parent, REGF_FILE *outfile,
66 const char *parentpath )
68 REGF_NK_REC *key, *subkey;
69 SEC_DESC *new_sd;
70 REGVAL_CTR values;
71 REGSUBKEY_CTR subkeys;
72 int i;
73 pstring path;
75 /* swap out the SIDs in the security descriptor */
77 if ( !(new_sd = dup_sec_desc( outfile->mem_ctx, nk->sec_desc->sec_desc )) ) {
78 fprintf( stderr, "Failed to copy security descriptor!\n" );
79 return False;
82 if ( swap_sid_in_acl( new_sd, &old_sid, &new_sid ) )
83 DEBUG(2,("Updating ACL for %s\n", nk->keyname ));
85 regsubkey_ctr_init( &subkeys );
86 regval_ctr_init( &values );
88 /* copy values into the REGVAL_CTR */
90 for ( i=0; i<nk->num_values; i++ ) {
91 regval_ctr_addvalue( &values, nk->values[i].valuename, nk->values[i].type,
92 nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
95 /* copy subkeys into the REGSUBKEY_CTR */
97 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
98 regsubkey_ctr_addkey( &subkeys, subkey->keyname );
101 key = regfio_write_key( outfile, nk->keyname, &values, &subkeys, new_sd, parent );
103 /* write each one of the subkeys out */
105 pstr_sprintf( path, "%s%s%s", parentpath, parent ? "\\" : "", nk->keyname );
107 nk->subkey_index = 0;
108 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
109 if ( !copy_registry_tree( infile, subkey, key, outfile, path ) )
110 return False;
113 regval_ctr_destroy( &values );
114 regsubkey_ctr_destroy( &subkeys );
116 DEBUG(1,("[%s]\n", path));
118 return True;
121 /*********************************************************************
122 *********************************************************************/
124 int main( int argc, char *argv[] )
126 int opt;
127 REGF_FILE *infile, *outfile;
128 REGF_NK_REC *nk;
129 pstring orig_filename, new_filename;
130 struct poptOption long_options[] = {
131 POPT_AUTOHELP
132 { "change-sid", 'c', POPT_ARG_STRING, NULL, 'c', "Provides SID to change" },
133 { "new-sid", 'n', POPT_ARG_STRING, NULL, 'n', "Provides SID to change to" },
134 POPT_COMMON_SAMBA
135 POPT_COMMON_VERSION
136 POPT_TABLEEND
138 poptContext pc;
140 /* setup logging options */
142 setup_logging( "profiles", True );
143 dbf = x_stderr;
144 x_setbuf( x_stderr, NULL );
146 pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
147 POPT_CONTEXT_KEEP_FIRST);
149 poptSetOtherOptionHelp(pc, "<profilefile>");
151 /* Now, process the arguments */
153 while ((opt = poptGetNextOpt(pc)) != -1) {
154 switch (opt) {
155 case 'c':
156 change = 1;
157 if (!string_to_sid(&old_sid, poptGetOptArg(pc))) {
158 fprintf(stderr, "Argument to -c should be a SID in form of S-1-5-...\n");
159 poptPrintUsage(pc, stderr, 0);
160 exit(254);
162 break;
164 case 'n':
165 new_val = 1;
166 if (!string_to_sid(&new_sid, poptGetOptArg(pc))) {
167 fprintf(stderr, "Argument to -n should be a SID in form of S-1-5-...\n");
168 poptPrintUsage(pc, stderr, 0);
169 exit(253);
171 break;
176 poptGetArg(pc);
178 if (!poptPeekArg(pc)) {
179 poptPrintUsage(pc, stderr, 0);
180 exit(1);
183 if ((!change & new_val) || (change & !new_val)) {
184 fprintf(stderr, "You must specify both -c and -n if one or the other is set!\n");
185 poptPrintUsage(pc, stderr, 0);
186 exit(252);
189 pstrcpy( orig_filename, poptPeekArg(pc) );
190 pstr_sprintf( new_filename, "%s.new", orig_filename );
192 if ( !(infile = regfio_open( orig_filename, O_RDONLY, 0 )) ) {
193 fprintf( stderr, "Failed to open %s!\n", orig_filename );
194 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
195 exit (1);
198 if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
199 fprintf( stderr, "Failed to open new file %s!\n", new_filename );
200 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
201 exit (1);
204 /* actually do the update now */
206 nk = regfio_rootkey( infile );
208 if ( !copy_registry_tree( infile, nk, NULL, outfile, "" ) ) {
209 fprintf(stderr, "Failed to write updated registry file!\n");
210 exit(2);
213 /* cleanup */
215 regfio_close( infile );
216 regfio_close( outfile );
218 poptFreeContext(pc);
220 exit( 0 );