s3-registry: only include registry headers when really needed.
[nivanova/samba-autobuild/.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.  
20 */
21                                   
22 #include "includes.h"
23 #include "reg_objects.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 int opt_verbose = False;
31
32 /********************************************************************
33 ********************************************************************/
34
35 static void verbose_output(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
36 static void verbose_output(const char *format, ...)
37 {
38         va_list args;
39         char *var = NULL;
40
41         if (!opt_verbose) {
42                 return;
43         }
44
45         va_start(args, format);
46         if ((vasprintf(&var, format, args)) == -1) {
47                 va_end(args);
48                 return;
49         }
50
51         fprintf(stdout, "%s", var);
52         va_end(args);
53         SAFE_FREE(var);
54 }
55
56 /********************************************************************
57 ********************************************************************/
58
59 static bool swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
60 {
61         SEC_ACL *theacl;
62         int i;
63         bool update = False;
64
65         verbose_output("  Owner SID: %s\n", sid_string_tos(sd->owner_sid));
66         if ( sid_equal( sd->owner_sid, s1 ) ) {
67                 sid_copy( sd->owner_sid, s2 );
68                 update = True;
69                 verbose_output("  New Owner SID: %s\n", 
70                         sid_string_tos(sd->owner_sid));
71
72         }
73
74         verbose_output("  Group SID: %s\n", sid_string_tos(sd->group_sid));
75         if ( sid_equal( sd->group_sid, s1 ) ) {
76                 sid_copy( sd->group_sid, s2 );
77                 update = True;
78                 verbose_output("  New Group SID: %s\n", 
79                         sid_string_tos(sd->group_sid));
80         }
81
82         theacl = sd->dacl;
83         verbose_output("  DACL: %d entries:\n", theacl->num_aces);
84         for ( i=0; i<theacl->num_aces; i++ ) {
85                 verbose_output("    Trustee SID: %s\n", 
86                         sid_string_tos(&theacl->aces[i].trustee));
87                 if ( sid_equal( &theacl->aces[i].trustee, s1 ) ) {
88                         sid_copy( &theacl->aces[i].trustee, s2 );
89                         update = True;
90                         verbose_output("    New Trustee SID: %s\n", 
91                                 sid_string_tos(&theacl->aces[i].trustee));
92                 }
93         }
94
95 #if 0
96         theacl = sd->sacl;
97         verbose_output("  SACL: %d entries: \n", theacl->num_aces);
98         for ( i=0; i<theacl->num_aces; i++ ) {
99                 verbose_output("    Trustee SID: %s\n", 
100                         sid_string_tos(&theacl->aces[i].trustee));
101                 if ( sid_equal( &theacl->aces[i].trustee, s1 ) ) {
102                         sid_copy( &theacl->aces[i].trustee, s2 );
103                         update = True;
104                         verbose_output("    New Trustee SID: %s\n", 
105                                 sid_string_tos(&theacl->aces[i].trustee));
106                 }
107         }
108 #endif
109         return update;
110 }
111
112 /********************************************************************
113 ********************************************************************/
114
115 static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
116                                 REGF_NK_REC *parent, REGF_FILE *outfile,
117                                 const char *parentpath  )
118 {
119         REGF_NK_REC *key, *subkey;
120         SEC_DESC *new_sd;
121         struct regval_ctr *values;
122         struct regsubkey_ctr *subkeys;
123         int i;
124         char *path;
125         WERROR werr;
126
127         /* swap out the SIDs in the security descriptor */
128
129         if ( !(new_sd = dup_sec_desc( outfile->mem_ctx, nk->sec_desc->sec_desc )) ) {
130                 fprintf( stderr, "Failed to copy security descriptor!\n" );
131                 return False;
132         }
133
134         verbose_output("ACL for %s%s%s\n", parentpath, parent ? "\\" : "", nk->keyname);
135         swap_sid_in_acl( new_sd, &old_sid, &new_sid );
136
137         werr = regsubkey_ctr_init(NULL, &subkeys);
138         if (!W_ERROR_IS_OK(werr)) {
139                 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
140                 return False;
141         }
142
143         if ( !(values = TALLOC_ZERO_P( subkeys, struct regval_ctr )) ) {
144                 TALLOC_FREE( subkeys );
145                 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
146                 return False;
147         }
148
149         /* copy values into the struct regval_ctr */
150
151         for ( i=0; i<nk->num_values; i++ ) {
152                 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
153                         (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
154         }
155
156         /* copy subkeys into the struct regsubkey_ctr */
157
158         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
159                 regsubkey_ctr_addkey( subkeys, subkey->keyname );
160         }
161
162         key = regfio_write_key( outfile, nk->keyname, values, subkeys, new_sd, parent );
163
164         /* write each one of the subkeys out */
165
166         path = talloc_asprintf(subkeys, "%s%s%s",
167                         parentpath, parent ? "\\" : "",nk->keyname);
168         if (!path) {
169                 TALLOC_FREE( subkeys );
170                 return false;
171         }
172
173         nk->subkey_index = 0;
174         while ((subkey = regfio_fetch_subkey(infile, nk))) {
175                 if (!copy_registry_tree( infile, subkey, key, outfile, path)) {
176                         TALLOC_FREE(subkeys);
177                         return false;
178                 }
179         }
180
181         /* values is a talloc()'d child of subkeys here so just throw it all away */
182
183         TALLOC_FREE( subkeys );
184
185         verbose_output("[%s]\n", path);
186
187         return True;
188 }
189
190 /*********************************************************************
191 *********************************************************************/
192
193 int main( int argc, char *argv[] )
194 {
195         TALLOC_CTX *frame = talloc_stackframe();
196         int opt;
197         REGF_FILE *infile, *outfile;
198         REGF_NK_REC *nk;
199         char *orig_filename, *new_filename;
200         struct poptOption long_options[] = {
201                 POPT_AUTOHELP
202                 { "change-sid", 'c', POPT_ARG_STRING, NULL, 'c', "Provides SID to change" },
203                 { "new-sid", 'n', POPT_ARG_STRING, NULL, 'n', "Provides SID to change to" },
204                 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 'v', "Verbose output" },
205                 POPT_COMMON_SAMBA
206                 POPT_COMMON_VERSION
207                 POPT_TABLEEND
208         };
209         poptContext pc;
210
211         load_case_tables();
212
213         /* setup logging options */
214
215         setup_logging( "profiles", True );
216         dbf = x_stderr;
217         x_setbuf( x_stderr, NULL );
218
219         pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
220                 POPT_CONTEXT_KEEP_FIRST);
221
222         poptSetOtherOptionHelp(pc, "<profilefile>");
223
224         /* Now, process the arguments */
225
226         while ((opt = poptGetNextOpt(pc)) != -1) {
227                 switch (opt) {
228                 case 'c':
229                         change = 1;
230                         if (!string_to_sid(&old_sid, poptGetOptArg(pc))) {
231                                 fprintf(stderr, "Argument to -c should be a SID in form of S-1-5-...\n");
232                                 poptPrintUsage(pc, stderr, 0);
233                                 exit(254);
234                         }
235                         break;
236
237                 case 'n':
238                         new_val = 1;
239                         if (!string_to_sid(&new_sid, poptGetOptArg(pc))) {
240                                 fprintf(stderr, "Argument to -n should be a SID in form of S-1-5-...\n");
241                                 poptPrintUsage(pc, stderr, 0);
242                                 exit(253);
243                         }
244                         break;
245
246                 }
247         }
248
249         poptGetArg(pc);
250
251         if (!poptPeekArg(pc)) {
252                 poptPrintUsage(pc, stderr, 0);
253                 exit(1);
254         }
255
256         if ((!change && new_val) || (change && !new_val)) {
257                 fprintf(stderr, "You must specify both -c and -n if one or the other is set!\n");
258                 poptPrintUsage(pc, stderr, 0);
259                 exit(252);
260         }
261
262         orig_filename = talloc_strdup(frame, poptPeekArg(pc));
263         if (!orig_filename) {
264                 exit(ENOMEM);
265         }
266         new_filename = talloc_asprintf(frame,
267                                         "%s.new",
268                                         orig_filename);
269         if (!new_filename) {
270                 exit(ENOMEM);
271         }
272
273         if (!(infile = regfio_open( orig_filename, O_RDONLY, 0))) {
274                 fprintf( stderr, "Failed to open %s!\n", orig_filename );
275                 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
276                 exit (1);
277         }
278
279         if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
280                 fprintf( stderr, "Failed to open new file %s!\n", new_filename );
281                 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
282                 exit (1);
283         }
284
285         /* actually do the update now */
286
287         if ((nk = regfio_rootkey( infile )) == NULL) {
288                 fprintf(stderr, "Could not get rootkey\n");
289                 exit(3);
290         }
291
292         if (!copy_registry_tree( infile, nk, NULL, outfile, "")) {
293                 fprintf(stderr, "Failed to write updated registry file!\n");
294                 exit(2);
295         }
296
297         /* cleanup */
298
299         regfio_close(infile);
300         regfio_close(outfile);
301
302         poptFreeContext(pc);
303
304         TALLOC_FREE(frame);
305         return 0;
306 }