Update homepages for talloc, tdb and ldb subprojects
[Samba.git] / source3 / utils / profiles.c
blob0b8a0d4278386e968bdbfc2590e251cb532eb864
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "regfio.h"
25 /* GLOBAL VARIABLES */
27 DOM_SID old_sid, new_sid;
28 int change = 0, new_val = 0;
29 int opt_verbose = False;
31 /********************************************************************
32 ********************************************************************/
34 static void verbose_output(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
35 static void verbose_output(const char *format, ...)
37 va_list args;
38 char *var = NULL;
40 if (!opt_verbose) {
41 return;
44 va_start(args, format);
45 if ((vasprintf(&var, format, args)) == -1) {
46 va_end(args);
47 return;
50 fprintf(stdout, var);
51 va_end(args);
52 SAFE_FREE(var);
55 /********************************************************************
56 ********************************************************************/
58 static bool swap_sid_in_acl( SEC_DESC *sd, DOM_SID *s1, DOM_SID *s2 )
60 SEC_ACL *acl;
61 int i;
62 bool update = False;
64 verbose_output(" Owner SID: %s\n", sid_string_tos(sd->owner_sid));
65 if ( sid_equal( sd->owner_sid, s1 ) ) {
66 sid_copy( sd->owner_sid, s2 );
67 update = True;
68 verbose_output(" New Owner SID: %s\n",
69 sid_string_tos(sd->owner_sid));
73 verbose_output(" Group SID: %s\n", sid_string_tos(sd->group_sid));
74 if ( sid_equal( sd->group_sid, s1 ) ) {
75 sid_copy( sd->group_sid, s2 );
76 update = True;
77 verbose_output(" New Group SID: %s\n",
78 sid_string_tos(sd->group_sid));
81 acl = sd->dacl;
82 verbose_output(" DACL: %d entries:\n", acl->num_aces);
83 for ( i=0; i<acl->num_aces; i++ ) {
84 verbose_output(" Trustee SID: %s\n",
85 sid_string_tos(&acl->aces[i].trustee));
86 if ( sid_equal( &acl->aces[i].trustee, s1 ) ) {
87 sid_copy( &acl->aces[i].trustee, s2 );
88 update = True;
89 verbose_output(" New Trustee SID: %s\n",
90 sid_string_tos(&acl->aces[i].trustee));
94 #if 0
95 acl = sd->sacl;
96 verbose_output(" SACL: %d entries: \n", acl->num_aces);
97 for ( i=0; i<acl->num_aces; i++ ) {
98 verbose_output(" Trustee SID: %s\n",
99 sid_string_tos(&acl->aces[i].trustee));
100 if ( sid_equal( &acl->aces[i].trustee, s1 ) ) {
101 sid_copy( &acl->aces[i].trustee, s2 );
102 update = True;
103 verbose_output(" New Trustee SID: %s\n",
104 sid_string_tos(&acl->aces[i].trustee));
107 #endif
108 return update;
111 /********************************************************************
112 ********************************************************************/
114 static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
115 REGF_NK_REC *parent, REGF_FILE *outfile,
116 const char *parentpath )
118 REGF_NK_REC *key, *subkey;
119 SEC_DESC *new_sd;
120 REGVAL_CTR *values;
121 REGSUBKEY_CTR *subkeys;
122 int i;
123 char *path;
125 /* swap out the SIDs in the security descriptor */
127 if ( !(new_sd = dup_sec_desc( outfile->mem_ctx, nk->sec_desc->sec_desc )) ) {
128 fprintf( stderr, "Failed to copy security descriptor!\n" );
129 return False;
132 verbose_output("ACL for %s%s%s\n", parentpath, parent ? "\\" : "", nk->keyname);
133 swap_sid_in_acl( new_sd, &old_sid, &new_sid );
135 if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
136 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
137 return False;
140 if ( !(values = TALLOC_ZERO_P( subkeys, REGVAL_CTR )) ) {
141 TALLOC_FREE( subkeys );
142 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
143 return False;
146 /* copy values into the REGVAL_CTR */
148 for ( i=0; i<nk->num_values; i++ ) {
149 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
150 (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
153 /* copy subkeys into the REGSUBKEY_CTR */
155 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
156 regsubkey_ctr_addkey( subkeys, subkey->keyname );
159 key = regfio_write_key( outfile, nk->keyname, values, subkeys, new_sd, parent );
161 /* write each one of the subkeys out */
163 path = talloc_asprintf(subkeys, "%s%s%s",
164 parentpath, parent ? "\\" : "",nk->keyname);
165 if (!path) {
166 TALLOC_FREE( subkeys );
167 return false;
170 nk->subkey_index = 0;
171 while ((subkey = regfio_fetch_subkey(infile, nk))) {
172 if (!copy_registry_tree( infile, subkey, key, outfile, path)) {
173 TALLOC_FREE(subkeys);
174 return false;
178 /* values is a talloc()'d child of subkeys here so just throw it all away */
180 TALLOC_FREE( subkeys );
182 verbose_output("[%s]\n", path);
184 return True;
187 /*********************************************************************
188 *********************************************************************/
190 int main( int argc, char *argv[] )
192 TALLOC_CTX *frame = talloc_stackframe();
193 int opt;
194 REGF_FILE *infile, *outfile;
195 REGF_NK_REC *nk;
196 char *orig_filename, *new_filename;
197 struct poptOption long_options[] = {
198 POPT_AUTOHELP
199 { "change-sid", 'c', POPT_ARG_STRING, NULL, 'c', "Provides SID to change" },
200 { "new-sid", 'n', POPT_ARG_STRING, NULL, 'n', "Provides SID to change to" },
201 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 'v', "Verbose output" },
202 POPT_COMMON_SAMBA
203 POPT_COMMON_VERSION
204 POPT_TABLEEND
206 poptContext pc;
208 load_case_tables();
210 /* setup logging options */
212 setup_logging( "profiles", True );
213 dbf = x_stderr;
214 x_setbuf( x_stderr, NULL );
216 pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
217 POPT_CONTEXT_KEEP_FIRST);
219 poptSetOtherOptionHelp(pc, "<profilefile>");
221 /* Now, process the arguments */
223 while ((opt = poptGetNextOpt(pc)) != -1) {
224 switch (opt) {
225 case 'c':
226 change = 1;
227 if (!string_to_sid(&old_sid, poptGetOptArg(pc))) {
228 fprintf(stderr, "Argument to -c should be a SID in form of S-1-5-...\n");
229 poptPrintUsage(pc, stderr, 0);
230 exit(254);
232 break;
234 case 'n':
235 new_val = 1;
236 if (!string_to_sid(&new_sid, poptGetOptArg(pc))) {
237 fprintf(stderr, "Argument to -n should be a SID in form of S-1-5-...\n");
238 poptPrintUsage(pc, stderr, 0);
239 exit(253);
241 break;
246 poptGetArg(pc);
248 if (!poptPeekArg(pc)) {
249 poptPrintUsage(pc, stderr, 0);
250 exit(1);
253 if ((!change && new_val) || (change && !new_val)) {
254 fprintf(stderr, "You must specify both -c and -n if one or the other is set!\n");
255 poptPrintUsage(pc, stderr, 0);
256 exit(252);
259 orig_filename = talloc_strdup(frame, poptPeekArg(pc));
260 if (!orig_filename) {
261 exit(ENOMEM);
263 new_filename = talloc_asprintf(frame,
264 "%s.new",
265 orig_filename);
266 if (!new_filename) {
267 exit(ENOMEM);
270 if (!(infile = regfio_open( orig_filename, O_RDONLY, 0))) {
271 fprintf( stderr, "Failed to open %s!\n", orig_filename );
272 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
273 exit (1);
276 if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
277 fprintf( stderr, "Failed to open new file %s!\n", new_filename );
278 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
279 exit (1);
282 /* actually do the update now */
284 if ((nk = regfio_rootkey( infile )) == NULL) {
285 fprintf(stderr, "Could not get rootkey\n");
286 exit(3);
289 if (!copy_registry_tree( infile, nk, NULL, outfile, "")) {
290 fprintf(stderr, "Failed to write updated registry file!\n");
291 exit(2);
294 /* cleanup */
296 regfio_close(infile);
297 regfio_close(outfile);
299 poptFreeContext(pc);
301 TALLOC_FREE(frame);
302 return 0;