fix Ń•ome == to correct shell test in commented stuff
[Samba.git] / source3 / utils / profiles.c
blob53862f441fc39af93642237cd6075cfe89c4d65c
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 "popt_common.h"
24 #include "registry/reg_objects.h"
25 #include "registry/regfio.h"
26 #include "../libcli/security/security.h"
28 /* GLOBAL VARIABLES */
30 struct dom_sid old_sid, new_sid;
31 int change = 0, new_val = 0;
32 int opt_verbose = False;
34 /********************************************************************
35 ********************************************************************/
37 static void verbose_output(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
38 static void verbose_output(const char *format, ...)
40 va_list args;
41 char *var = NULL;
43 if (!opt_verbose) {
44 return;
47 va_start(args, format);
48 if ((vasprintf(&var, format, args)) == -1) {
49 va_end(args);
50 return;
53 fprintf(stdout, "%s", var);
54 va_end(args);
55 SAFE_FREE(var);
58 /********************************************************************
59 ********************************************************************/
61 static bool swap_sid_in_acl( struct security_descriptor *sd, struct dom_sid *s1, struct dom_sid *s2 )
63 struct security_acl *theacl;
64 int i;
65 bool update = False;
67 verbose_output(" Owner SID: %s\n", sid_string_tos(sd->owner_sid));
68 if ( dom_sid_equal( sd->owner_sid, s1 ) ) {
69 sid_copy( sd->owner_sid, s2 );
70 update = True;
71 verbose_output(" New Owner SID: %s\n",
72 sid_string_tos(sd->owner_sid));
76 verbose_output(" Group SID: %s\n", sid_string_tos(sd->group_sid));
77 if ( dom_sid_equal( sd->group_sid, s1 ) ) {
78 sid_copy( sd->group_sid, s2 );
79 update = True;
80 verbose_output(" New Group SID: %s\n",
81 sid_string_tos(sd->group_sid));
84 theacl = sd->dacl;
85 verbose_output(" DACL: %d entries:\n", theacl->num_aces);
86 for ( i=0; i<theacl->num_aces; i++ ) {
87 verbose_output(" Trustee SID: %s\n",
88 sid_string_tos(&theacl->aces[i].trustee));
89 if ( dom_sid_equal( &theacl->aces[i].trustee, s1 ) ) {
90 sid_copy( &theacl->aces[i].trustee, s2 );
91 update = True;
92 verbose_output(" New Trustee SID: %s\n",
93 sid_string_tos(&theacl->aces[i].trustee));
97 #if 0
98 theacl = sd->sacl;
99 verbose_output(" SACL: %d entries: \n", theacl->num_aces);
100 for ( i=0; i<theacl->num_aces; i++ ) {
101 verbose_output(" Trustee SID: %s\n",
102 sid_string_tos(&theacl->aces[i].trustee));
103 if ( dom_sid_equal( &theacl->aces[i].trustee, s1 ) ) {
104 sid_copy( &theacl->aces[i].trustee, s2 );
105 update = True;
106 verbose_output(" New Trustee SID: %s\n",
107 sid_string_tos(&theacl->aces[i].trustee));
110 #endif
111 return update;
114 /********************************************************************
115 ********************************************************************/
117 static bool copy_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
118 REGF_NK_REC *parent, REGF_FILE *outfile,
119 const char *parentpath )
121 REGF_NK_REC *key, *subkey;
122 struct security_descriptor *new_sd;
123 struct regval_ctr *values;
124 struct regsubkey_ctr *subkeys;
125 int i;
126 char *path;
127 WERROR werr;
129 /* swap out the SIDs in the security descriptor */
131 if ( !(new_sd = dup_sec_desc( outfile->mem_ctx, nk->sec_desc->sec_desc )) ) {
132 fprintf( stderr, "Failed to copy security descriptor!\n" );
133 return False;
136 verbose_output("ACL for %s%s%s\n", parentpath, parent ? "\\" : "", nk->keyname);
137 swap_sid_in_acl( new_sd, &old_sid, &new_sid );
139 werr = regsubkey_ctr_init(NULL, &subkeys);
140 if (!W_ERROR_IS_OK(werr)) {
141 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
142 return False;
145 werr = regval_ctr_init(subkeys, &values);
146 if (!W_ERROR_IS_OK(werr)) {
147 TALLOC_FREE( subkeys );
148 DEBUG(0,("copy_registry_tree: talloc() failure!\n"));
149 return False;
152 /* copy values into the struct regval_ctr */
154 for ( i=0; i<nk->num_values; i++ ) {
155 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
156 nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
159 /* copy subkeys into the struct regsubkey_ctr */
161 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
162 regsubkey_ctr_addkey( subkeys, subkey->keyname );
165 key = regfio_write_key( outfile, nk->keyname, values, subkeys, new_sd, parent );
167 /* write each one of the subkeys out */
169 path = talloc_asprintf(subkeys, "%s%s%s",
170 parentpath, parent ? "\\" : "",nk->keyname);
171 if (!path) {
172 TALLOC_FREE( subkeys );
173 return false;
176 nk->subkey_index = 0;
177 while ((subkey = regfio_fetch_subkey(infile, nk))) {
178 if (!copy_registry_tree( infile, subkey, key, outfile, path)) {
179 TALLOC_FREE(subkeys);
180 return false;
184 /* values is a talloc()'d child of subkeys here so just throw it all away */
186 TALLOC_FREE( subkeys );
188 verbose_output("[%s]\n", path);
190 return True;
193 /*********************************************************************
194 *********************************************************************/
196 int main( int argc, char *argv[] )
198 TALLOC_CTX *frame = talloc_stackframe();
199 int opt;
200 REGF_FILE *infile, *outfile;
201 REGF_NK_REC *nk;
202 char *orig_filename, *new_filename;
203 struct poptOption long_options[] = {
204 POPT_AUTOHELP
205 { "change-sid", 'c', POPT_ARG_STRING, NULL, 'c', "Provides SID to change" },
206 { "new-sid", 'n', POPT_ARG_STRING, NULL, 'n', "Provides SID to change to" },
207 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 'v', "Verbose output" },
208 POPT_COMMON_SAMBA
209 POPT_COMMON_VERSION
210 POPT_TABLEEND
212 poptContext pc;
214 load_case_tables();
216 /* setup logging options */
218 setup_logging( "profiles", DEBUG_STDERR);
220 pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
221 POPT_CONTEXT_KEEP_FIRST);
223 poptSetOtherOptionHelp(pc, "<profilefile>");
225 /* Now, process the arguments */
227 while ((opt = poptGetNextOpt(pc)) != -1) {
228 switch (opt) {
229 case 'c':
230 change = 1;
231 if (!string_to_sid(&old_sid, poptGetOptArg(pc))) {
232 fprintf(stderr, "Argument to -c should be a SID in form of S-1-5-...\n");
233 poptPrintUsage(pc, stderr, 0);
234 exit(254);
236 break;
238 case 'n':
239 new_val = 1;
240 if (!string_to_sid(&new_sid, poptGetOptArg(pc))) {
241 fprintf(stderr, "Argument to -n should be a SID in form of S-1-5-...\n");
242 poptPrintUsage(pc, stderr, 0);
243 exit(253);
245 break;
250 poptGetArg(pc);
252 if (!poptPeekArg(pc)) {
253 poptPrintUsage(pc, stderr, 0);
254 exit(1);
257 if ((!change && new_val) || (change && !new_val)) {
258 fprintf(stderr, "You must specify both -c and -n if one or the other is set!\n");
259 poptPrintUsage(pc, stderr, 0);
260 exit(252);
263 orig_filename = talloc_strdup(frame, poptPeekArg(pc));
264 if (!orig_filename) {
265 exit(ENOMEM);
267 new_filename = talloc_asprintf(frame,
268 "%s.new",
269 orig_filename);
270 if (!new_filename) {
271 exit(ENOMEM);
274 if (!(infile = regfio_open( orig_filename, O_RDONLY, 0))) {
275 fprintf( stderr, "Failed to open %s!\n", orig_filename );
276 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
277 exit (1);
280 if ( !(outfile = regfio_open( new_filename, (O_RDWR|O_CREAT|O_TRUNC),
281 (S_IRUSR|S_IWUSR) )) ) {
282 fprintf( stderr, "Failed to open new file %s!\n", new_filename );
283 fprintf( stderr, "Error was (%s)\n", strerror(errno) );
284 exit (1);
287 /* actually do the update now */
289 if ((nk = regfio_rootkey( infile )) == NULL) {
290 fprintf(stderr, "Could not get rootkey\n");
291 exit(3);
294 if (!copy_registry_tree( infile, nk, NULL, outfile, "")) {
295 fprintf(stderr, "Failed to write updated registry file!\n");
296 exit(2);
299 /* cleanup */
301 regfio_close(infile);
302 regfio_close(outfile);
304 poptFreeContext(pc);
306 TALLOC_FREE(frame);
307 return 0;