Initial import
[samba] / source / utils / profiles.c
1 /* 
2    Samba Unix/Linux SMB client utility profiles.c 
3    
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 
7
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.
12    
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.
17    
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.  
21 */
22                                   
23 #include "includes.h"
24 #include "regfio.h"
25
26 /* GLOBAL VARIABLES */
27
28 DOM_SID old_sid, new_sid;
29 int change = 0, new_val = 0;
30
31
32 /********************************************************************
33 ********************************************************************/
34
35 static BOOL swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
36 {
37         SEC_ACL *acl = sd->dacl;
38         int i;
39         BOOL update = False;
40
41         if ( sid_equal( sd->owner_sid, s1 ) ) {
42                 sid_copy( sd->owner_sid, s2 );
43                 update = True;
44         }
45
46         if ( sid_equal( sd->grp_sid, s1 ) ) {
47                 sid_copy( sd->grp_sid, s2 );
48                 update = True;
49         }
50
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;
55                 }
56         }
57
58         return update;
59 }
60
61 /********************************************************************
62 ********************************************************************/
63
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  )
67 {
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;
74
75         /* swap out the SIDs in the security descriptor */
76
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;
80         }
81
82         if ( swap_sid_in_acl( new_sd, &old_sid, &new_sid ) )
83                 DEBUG(2,("Updating ACL for %s\n", nk->keyname ));
84
85         if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
86                 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
87                 return False;
88         }
89
90         if ( !(values = TALLOC_ZERO_P( subkeys, REGVAL_CTR )) ) {
91                 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
92                 return False;
93         }
94
95         /* copy values into the REGVAL_CTR */
96
97         for ( i=0; i<nk->num_values; i++ ) {
98                 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
99                         (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
100         }
101
102         /* copy subkeys into the REGSUBKEY_CTR */
103
104         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
105                 regsubkey_ctr_addkey( subkeys, subkey->keyname );
106         }
107
108         key = regfio_write_key( outfile, nk->keyname, values, subkeys, new_sd, parent );
109
110         /* write each one of the subkeys out */
111
112         pstr_sprintf( path, "%s%s%s", parentpath, parent ? "\\" : "", nk->keyname );
113         
114         nk->subkey_index = 0;
115         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
116                 if ( !copy_registry_tree( infile, subkey, key, outfile, path ) )
117                         return False;
118         }
119
120         /* values is a talloc()'d child of subkeys here so just throw it all away */
121
122         TALLOC_FREE( subkeys );
123
124         DEBUG(1,("[%s]\n", path));
125
126         return True;
127 }
128
129 /*********************************************************************
130 *********************************************************************/
131
132 int main( int argc, char *argv[] )
133 {
134         int opt;
135         REGF_FILE *infile, *outfile;
136         REGF_NK_REC *nk;
137         pstring orig_filename, new_filename;
138         struct poptOption long_options[] = {
139                 POPT_AUTOHELP
140                 { "change-sid", 'c', POPT_ARG_STRING, NULL, 'c', "Provides SID to change" },
141                 { "new-sid", 'n', POPT_ARG_STRING, NULL, 'n', "Provides SID to change to" },
142                 POPT_COMMON_SAMBA
143                 POPT_COMMON_VERSION
144                 POPT_TABLEEND
145         };
146         poptContext pc;
147
148         load_case_tables();
149
150         /* setup logging options */
151
152         setup_logging( "profiles", True );
153         dbf = x_stderr;
154         x_setbuf( x_stderr, NULL );
155
156         pc = poptGetContext("profiles", argc, (const char **)argv, long_options, 
157                 POPT_CONTEXT_KEEP_FIRST);
158
159         poptSetOtherOptionHelp(pc, "<profilefile>");
160
161         /* Now, process the arguments */
162
163         while ((opt = poptGetNextOpt(pc)) != -1) {
164                 switch (opt) {
165                 case 'c':
166                         change = 1;
167                         if (!string_to_sid(&old_sid, poptGetOptArg(pc))) {
168                                 fprintf(stderr, "Argument to -c should be a SID in form of S-1-5-...\n");
169                                 poptPrintUsage(pc, stderr, 0);
170                                 exit(254);
171                         }
172                         break;
173
174                 case 'n':
175                         new_val = 1;
176                         if (!string_to_sid(&new_sid, poptGetOptArg(pc))) {
177                                 fprintf(stderr, "Argument to -n should be a SID in form of S-1-5-...\n");
178                                 poptPrintUsage(pc, stderr, 0);
179                                 exit(253);
180                         }
181                         break;
182
183                 }
184         }
185
186         poptGetArg(pc); 
187
188         if (!poptPeekArg(pc)) {
189                 poptPrintUsage(pc, stderr, 0);
190                 exit(1);
191         }
192
193         if ((!change && new_val) || (change && !new_val)) {
194                 fprintf(stderr, "You must specify both -c and -n if one or the other is set!\n");
195                 poptPrintUsage(pc, stderr, 0);
196                 exit(252);
197         }
198
199         pstrcpy( orig_filename, poptPeekArg(pc) );
200         pstr_sprintf( new_filename, "%s.new", orig_filename );
201         
202         if ( !(infile = regfio_open( orig_filename, O_RDONLY, 0 )) ) {
203                 fprintf( stderr, "Failed to open %s!\n", orig_filename );
204                 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
205                 exit (1);
206         }
207         
208         if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
209                 fprintf( stderr, "Failed to open new file %s!\n", new_filename );
210                 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
211                 exit (1);
212         }
213         
214         /* actually do the update now */
215         
216         nk = regfio_rootkey( infile );
217         
218         if ( !copy_registry_tree( infile, nk, NULL, outfile, "" ) ) {
219                 fprintf(stderr, "Failed to write updated registry file!\n");
220                 exit(2);
221         }
222         
223         /* cleanup */
224         
225         regfio_close( infile );
226         regfio_close( outfile );
227
228         poptFreeContext(pc);
229
230         return( 0 );
231 }