2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) Gerald (Jerry) Carter 2005-2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "utils/net.h"
23 #include "reg_objects.h"
25 static bool reg_hive_key(const char *fullname, uint32 *reg_type,
26 const char **key_name)
31 sep = strchr_m(fullname, '\\');
38 len = strlen(fullname);
42 if (strnequal(fullname, "HKLM", len) ||
43 strnequal(fullname, "HKEY_LOCAL_MACHINE", len))
44 (*reg_type) = HKEY_LOCAL_MACHINE;
45 else if (strnequal(fullname, "HKCR", len) ||
46 strnequal(fullname, "HKEY_CLASSES_ROOT", len))
47 (*reg_type) = HKEY_CLASSES_ROOT;
48 else if (strnequal(fullname, "HKU", len) ||
49 strnequal(fullname, "HKEY_USERS", len))
50 (*reg_type) = HKEY_USERS;
51 else if (strnequal(fullname, "HKCU", len) ||
52 strnequal(fullname, "HKEY_CURRENT_USER", len))
53 (*reg_type) = HKEY_CURRENT_USER;
54 else if (strnequal(fullname, "HKPD", len) ||
55 strnequal(fullname, "HKEY_PERFORMANCE_DATA", len))
56 (*reg_type) = HKEY_PERFORMANCE_DATA;
58 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
66 static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
67 struct rpc_pipe_client *pipe_hnd,
68 const char *name, uint32 access_mask,
69 struct policy_handle *hive_hnd,
70 struct policy_handle *key_hnd)
74 struct winreg_String key;
78 if (!reg_hive_key(name, &hive, &key.name)) {
79 return NT_STATUS_INVALID_PARAMETER;
82 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive, access_mask,
84 if (!(NT_STATUS_IS_OK(status))) {
88 status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, hive_hnd, key, 0,
89 access_mask, key_hnd);
90 if (!(NT_STATUS_IS_OK(status))) {
91 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, hive_hnd);
98 static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
99 struct rpc_pipe_client *pipe_hnd,
100 struct policy_handle *key_hnd,
101 uint32 *pnum_keys, char ***pnames,
102 char ***pclasses, NTTIME ***pmodtimes)
106 uint32 num_subkeys, max_subkeylen, max_classlen;
107 uint32 num_values, max_valnamelen, max_valbufsize;
109 NTTIME last_changed_time;
111 struct winreg_String classname;
112 char **names, **classes;
115 if (!(mem_ctx = talloc_new(ctx))) {
116 return NT_STATUS_NO_MEMORY;
119 ZERO_STRUCT(classname);
120 status = rpccli_winreg_QueryInfoKey(
121 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
122 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
123 &max_valbufsize, &secdescsize, &last_changed_time );
125 if (!NT_STATUS_IS_OK(status)) {
129 if (num_subkeys == 0) {
131 TALLOC_FREE(mem_ctx);
135 if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
136 (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
137 (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
139 status = NT_STATUS_NO_MEMORY;
143 for (i=0; i<num_subkeys; i++) {
145 struct winreg_StringBuf class_buf;
146 struct winreg_StringBuf name_buf;
151 class_buf.size = max_classlen+2;
155 name_buf.size = max_subkeylen+2;
157 ZERO_STRUCT(modtime);
159 status = rpccli_winreg_EnumKey(pipe_hnd, mem_ctx, key_hnd,
160 i, &name_buf, &class_buf,
163 if (W_ERROR_EQUAL(ntstatus_to_werror(status),
164 WERR_NO_MORE_ITEMS) ) {
165 status = NT_STATUS_OK;
168 if (!NT_STATUS_IS_OK(status)) {
174 if (class_buf.name &&
175 (!(classes[i] = talloc_strdup(classes, class_buf.name)))) {
176 status = NT_STATUS_NO_MEMORY;
180 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
181 status = NT_STATUS_NO_MEMORY;
185 if ((!(modtimes[i] = (NTTIME *)talloc_memdup(
186 modtimes, &modtime, sizeof(modtime))))) {
187 status = NT_STATUS_NO_MEMORY;
192 *pnum_keys = num_subkeys;
195 *pnames = talloc_move(ctx, &names);
198 *pclasses = talloc_move(ctx, &classes);
201 *pmodtimes = talloc_move(ctx, &modtimes);
204 status = NT_STATUS_OK;
207 TALLOC_FREE(mem_ctx);
211 static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
212 struct rpc_pipe_client *pipe_hnd,
213 struct policy_handle *key_hnd,
214 uint32 *pnum_values, char ***pvalnames,
215 struct registry_value ***pvalues)
219 uint32 num_subkeys, max_subkeylen, max_classlen;
220 uint32 num_values, max_valnamelen, max_valbufsize;
222 NTTIME last_changed_time;
224 struct winreg_String classname;
225 struct registry_value **values;
228 if (!(mem_ctx = talloc_new(ctx))) {
229 return NT_STATUS_NO_MEMORY;
232 ZERO_STRUCT(classname);
233 status = rpccli_winreg_QueryInfoKey(
234 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
235 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
236 &max_valbufsize, &secdescsize, &last_changed_time );
238 if (!NT_STATUS_IS_OK(status)) {
242 if (num_values == 0) {
244 TALLOC_FREE(mem_ctx);
248 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
249 (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
251 status = NT_STATUS_NO_MEMORY;
255 for (i=0; i<num_values; i++) {
256 enum winreg_Type type = REG_NONE;
262 struct winreg_ValNameBuf name_buf;
267 name_buf.size = max_valnamelen + 2;
269 data_size = max_valbufsize;
270 data = (uint8 *)TALLOC(mem_ctx, data_size);
273 status = rpccli_winreg_EnumValue(pipe_hnd, mem_ctx, key_hnd,
278 if ( W_ERROR_EQUAL(ntstatus_to_werror(status),
279 WERR_NO_MORE_ITEMS) ) {
280 status = NT_STATUS_OK;
284 if (!(NT_STATUS_IS_OK(status))) {
288 if (name_buf.name == NULL) {
289 status = NT_STATUS_INVALID_PARAMETER;
293 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
294 status = NT_STATUS_NO_MEMORY;
298 err = registry_pull_value(values, &values[i], type, data,
299 data_size, value_length);
300 if (!W_ERROR_IS_OK(err)) {
301 status = werror_to_ntstatus(err);
306 *pnum_values = num_values;
309 *pvalnames = talloc_move(ctx, &names);
312 *pvalues = talloc_move(ctx, &values);
315 status = NT_STATUS_OK;
318 TALLOC_FREE(mem_ctx);
322 static NTSTATUS registry_getsd(TALLOC_CTX *mem_ctx,
323 struct rpc_pipe_client *pipe_hnd,
324 struct policy_handle *key_hnd,
326 struct KeySecurityData *sd)
328 return rpccli_winreg_GetKeySecurity(pipe_hnd, mem_ctx, key_hnd,
333 static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
334 struct rpc_pipe_client *pipe_hnd,
335 struct policy_handle *key_hnd,
337 const struct registry_value *value)
339 struct winreg_String name_string;
344 err = registry_push_value(mem_ctx, value, &blob);
345 if (!W_ERROR_IS_OK(err)) {
346 return werror_to_ntstatus(err);
349 ZERO_STRUCT(name_string);
351 name_string.name = name;
352 result = rpccli_winreg_SetValue(pipe_hnd, blob.data, key_hnd,
353 name_string, value->type,
354 blob.data, blob.length);
355 TALLOC_FREE(blob.data);
359 static NTSTATUS rpc_registry_setvalue_internal(const DOM_SID *domain_sid,
360 const char *domain_name,
361 struct cli_state *cli,
362 struct rpc_pipe_client *pipe_hnd,
367 struct policy_handle hive_hnd, key_hnd;
369 struct registry_value value;
371 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
372 SEC_RIGHTS_MAXIMUM_ALLOWED,
373 &hive_hnd, &key_hnd);
374 if (!NT_STATUS_IS_OK(status)) {
375 d_fprintf(stderr, "registry_openkey failed: %s\n",
380 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
381 d_fprintf(stderr, "Too many args for type %s\n", argv[2]);
382 return NT_STATUS_NOT_IMPLEMENTED;
385 if (strequal(argv[2], "dword")) {
386 value.type = REG_DWORD;
387 value.v.dword = strtoul(argv[3], NULL, 10);
389 else if (strequal(argv[2], "sz")) {
391 value.v.sz.len = strlen(argv[3])+1;
392 value.v.sz.str = CONST_DISCARD(char *, argv[3]);
395 d_fprintf(stderr, "type \"%s\" not implemented\n", argv[2]);
396 status = NT_STATUS_NOT_IMPLEMENTED;
400 status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
403 if (!NT_STATUS_IS_OK(status)) {
404 d_fprintf(stderr, "registry_setvalue failed: %s\n",
409 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd);
410 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd);
415 static int rpc_registry_setvalue( int argc, const char **argv )
418 d_fprintf(stderr, "usage: net rpc registry setvalue <key> "
419 "<valuename> <type> [<val>]+\n");
423 return run_rpc_command( NULL, PI_WINREG, 0,
424 rpc_registry_setvalue_internal, argc, argv );
427 static NTSTATUS rpc_registry_deletevalue_internal(const DOM_SID *domain_sid,
428 const char *domain_name,
429 struct cli_state *cli,
430 struct rpc_pipe_client *pipe_hnd,
435 struct policy_handle hive_hnd, key_hnd;
437 struct winreg_String valuename;
439 ZERO_STRUCT(valuename);
441 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
442 SEC_RIGHTS_MAXIMUM_ALLOWED,
443 &hive_hnd, &key_hnd);
444 if (!NT_STATUS_IS_OK(status)) {
445 d_fprintf(stderr, "registry_openkey failed: %s\n",
450 valuename.name = argv[1];
452 status = rpccli_winreg_DeleteValue(pipe_hnd, mem_ctx, &key_hnd,
455 if (!NT_STATUS_IS_OK(status)) {
456 d_fprintf(stderr, "registry_deletevalue failed: %s\n",
460 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd);
461 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd);
466 static int rpc_registry_deletevalue( int argc, const char **argv )
469 d_fprintf(stderr, "usage: net rpc registry deletevalue <key> "
474 return run_rpc_command( NULL, PI_WINREG, 0,
475 rpc_registry_deletevalue_internal, argc, argv );
478 static NTSTATUS rpc_registry_createkey_internal(const DOM_SID *domain_sid,
479 const char *domain_name,
480 struct cli_state *cli,
481 struct rpc_pipe_client *pipe_hnd,
487 struct policy_handle hive_hnd, key_hnd;
488 struct winreg_String key, keyclass;
489 enum winreg_CreateAction action;
493 ZERO_STRUCT(keyclass);
495 if (!reg_hive_key(argv[0], &hive, &key.name)) {
496 return NT_STATUS_INVALID_PARAMETER;
499 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive,
500 SEC_RIGHTS_MAXIMUM_ALLOWED,
502 if (!(NT_STATUS_IS_OK(status))) {
506 action = REG_ACTION_NONE;
509 status = rpccli_winreg_CreateKey(pipe_hnd, mem_ctx, &hive_hnd, key,
510 keyclass, 0, REG_KEY_READ, NULL,
512 if (!NT_STATUS_IS_OK(status)) {
513 d_fprintf(stderr, "createkey returned %s\n",
515 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd);
520 case REG_ACTION_NONE:
521 d_printf("createkey did nothing -- huh?\n");
523 case REG_CREATED_NEW_KEY:
524 d_printf("createkey created %s\n", argv[0]);
526 case REG_OPENED_EXISTING_KEY:
527 d_printf("createkey opened existing %s\n", argv[0]);
531 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd);
532 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd);
537 static int rpc_registry_createkey( int argc, const char **argv )
540 d_fprintf(stderr, "usage: net rpc registry createkey <key>\n");
544 return run_rpc_command( NULL, PI_WINREG, 0,
545 rpc_registry_createkey_internal, argc, argv );
548 static NTSTATUS rpc_registry_deletekey_internal(const DOM_SID *domain_sid,
549 const char *domain_name,
550 struct cli_state *cli,
551 struct rpc_pipe_client *pipe_hnd,
557 struct policy_handle hive_hnd;
558 struct winreg_String key;
563 if (!reg_hive_key(argv[0], &hive, &key.name)) {
564 return NT_STATUS_INVALID_PARAMETER;
567 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive,
568 SEC_RIGHTS_MAXIMUM_ALLOWED,
570 if (!(NT_STATUS_IS_OK(status))) {
574 status = rpccli_winreg_DeleteKey(pipe_hnd, mem_ctx, &hive_hnd, key);
575 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd);
577 if (!NT_STATUS_IS_OK(status)) {
578 d_fprintf(stderr, "deletekey returned %s\n",
585 static int rpc_registry_deletekey( int argc, const char **argv )
588 d_fprintf(stderr, "usage: net rpc registry deletekey <key>\n");
592 return run_rpc_command( NULL, PI_WINREG, 0,
593 rpc_registry_deletekey_internal, argc, argv );
596 /********************************************************************
597 ********************************************************************/
599 static NTSTATUS rpc_registry_enumerate_internal(const DOM_SID *domain_sid,
600 const char *domain_name,
601 struct cli_state *cli,
602 struct rpc_pipe_client *pipe_hnd,
607 POLICY_HND pol_hive, pol_key;
609 uint32 num_subkeys = 0;
610 uint32 num_values = 0;
611 char **names = NULL, **classes = NULL;
612 NTTIME **modtimes = NULL;
614 struct registry_value **values = NULL;
617 d_printf("Usage: net rpc registry enumerate <path> [recurse]\n");
618 d_printf("Example: net rpc registry enumerate 'HKLM\\Software\\Samba'\n");
622 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
623 &pol_hive, &pol_key);
624 if (!NT_STATUS_IS_OK(status)) {
625 d_fprintf(stderr, "registry_openkey failed: %s\n",
630 status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
631 &names, &classes, &modtimes);
632 if (!NT_STATUS_IS_OK(status)) {
633 d_fprintf(stderr, "enumerating keys failed: %s\n",
638 for (i=0; i<num_subkeys; i++) {
639 d_printf("Keyname = %s\n", names[i]);
640 d_printf("Modtime = %s\n", modtimes[i]
641 ? http_timestring(nt_time_to_unix(*modtimes[i]))
646 status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
648 if (!NT_STATUS_IS_OK(status)) {
649 d_fprintf(stderr, "enumerating values failed: %s\n",
654 for (i=0; i<num_values; i++) {
655 struct registry_value *v = values[i];
656 d_printf("Valuename = %s\n", names[i]);
657 d_printf("Type = %s\n",
658 reg_type_lookup(v->type));
661 d_printf("Value = %d\n", v->v.dword);
665 d_printf("Value = \"%s\"\n", v->v.sz.str);
669 for (j = 0; j < v->v.multi_sz.num_strings; j++) {
670 d_printf("Value[%3.3d] = \"%s\"\n", j,
671 v->v.multi_sz.strings[j]);
676 d_printf("Value = %d bytes\n",
677 (int)v->v.binary.length);
680 d_printf("Value = <unprintable>\n");
687 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key );
688 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive );
693 /********************************************************************
694 ********************************************************************/
696 static int rpc_registry_enumerate( int argc, const char **argv )
698 return run_rpc_command( NULL, PI_WINREG, 0,
699 rpc_registry_enumerate_internal, argc, argv );
702 /********************************************************************
703 ********************************************************************/
705 static NTSTATUS rpc_registry_save_internal(const DOM_SID *domain_sid,
706 const char *domain_name,
707 struct cli_state *cli,
708 struct rpc_pipe_client *pipe_hnd,
713 WERROR result = WERR_GENERAL_FAILURE;
714 POLICY_HND pol_hive, pol_key;
715 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
716 struct winreg_String filename;
719 d_printf("Usage: net rpc registry backup <path> <file> \n");
723 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_ALL,
724 &pol_hive, &pol_key);
725 if (!NT_STATUS_IS_OK(status)) {
726 d_fprintf(stderr, "registry_openkey failed: %s\n",
731 filename.name = argv[1];
732 status = rpccli_winreg_SaveKey( pipe_hnd, mem_ctx, &pol_key, &filename, NULL );
733 if ( !W_ERROR_IS_OK(result) ) {
734 d_fprintf(stderr, "Unable to save [%s] to %s:%s\n", argv[0], cli->desthost, argv[1]);
739 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key );
740 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive );
745 /********************************************************************
746 ********************************************************************/
748 static int rpc_registry_save( int argc, const char **argv )
750 return run_rpc_command( NULL, PI_WINREG, 0,
751 rpc_registry_save_internal, argc, argv );
755 /********************************************************************
756 ********************************************************************/
758 static void dump_values( REGF_NK_REC *nk )
762 uint32 data_size, data;
767 for ( i=0; i<nk->num_values; i++ ) {
768 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
769 d_printf( "(%s) ", reg_type_lookup( nk->values[i].type ) );
771 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
772 switch ( nk->values[i].type ) {
774 rpcstr_pull( data_str, nk->values[i].data, sizeof(data_str), -1, STR_TERMINATE );
775 d_printf( "%s", data_str );
779 for ( j=0; j<data_size; j++ ) {
780 d_printf( "%c", nk->values[i].data[j] );
784 data = IVAL( nk->values[i].data, 0 );
785 d_printf("0x%x", data );
788 for ( j=0; j<data_size; j++ ) {
789 d_printf( "%x", nk->values[i].data[j] );
802 /********************************************************************
803 ********************************************************************/
805 static bool dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
810 /* depth first dump of the registry tree */
812 while ( (key = regfio_fetch_subkey( file, nk )) ) {
813 pstr_sprintf( regpath, "%s\\%s", parent, key->keyname );
814 d_printf("[%s]\n", regpath );
817 dump_registry_tree( file, key, regpath );
823 /********************************************************************
824 ********************************************************************/
826 static bool write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
827 REGF_NK_REC *parent, REGF_FILE *outfile,
828 const char *parentpath )
830 REGF_NK_REC *key, *subkey;
832 REGSUBKEY_CTR *subkeys;
836 if ( !( subkeys = TALLOC_ZERO_P( infile->mem_ctx, REGSUBKEY_CTR )) ) {
837 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
841 if ( !(values = TALLOC_ZERO_P( subkeys, REGVAL_CTR )) ) {
842 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
846 /* copy values into the REGVAL_CTR */
848 for ( i=0; i<nk->num_values; i++ ) {
849 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
850 (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
853 /* copy subkeys into the REGSUBKEY_CTR */
855 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
856 regsubkey_ctr_addkey( subkeys, subkey->keyname );
859 key = regfio_write_key( outfile, nk->keyname, values, subkeys, nk->sec_desc->sec_desc, parent );
861 /* write each one of the subkeys out */
863 pstr_sprintf( path, "%s%s%s", parentpath, parent ? "\\" : "", nk->keyname );
864 nk->subkey_index = 0;
865 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
866 write_registry_tree( infile, subkey, key, outfile, path );
869 TALLOC_FREE( subkeys );
871 d_printf("[%s]\n", path );
876 /********************************************************************
877 ********************************************************************/
879 static int rpc_registry_dump( int argc, const char **argv )
885 d_printf("Usage: net rpc registry dump <file> \n");
889 d_printf("Opening %s....", argv[0]);
890 if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
891 d_fprintf(stderr, "Failed to open %s for reading\n", argv[0]);
896 /* get the root of the registry file */
898 if ((nk = regfio_rootkey( registry )) == NULL) {
899 d_fprintf(stderr, "Could not get rootkey\n");
900 regfio_close( registry );
903 d_printf("[%s]\n", nk->keyname);
907 dump_registry_tree( registry, nk, nk->keyname );
910 talloc_report_full( registry->mem_ctx, stderr );
912 d_printf("Closing registry...");
913 regfio_close( registry );
919 /********************************************************************
920 ********************************************************************/
922 static int rpc_registry_copy( int argc, const char **argv )
924 REGF_FILE *infile = NULL, *outfile = NULL;
929 d_printf("Usage: net rpc registry copy <srcfile> <newfile>\n");
933 d_printf("Opening %s....", argv[0]);
934 if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
935 d_fprintf(stderr, "Failed to open %s for reading\n", argv[0]);
940 d_printf("Opening %s....", argv[1]);
941 if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
942 d_fprintf(stderr, "Failed to open %s for writing\n", argv[1]);
947 /* get the root of the registry file */
949 if ((nk = regfio_rootkey( infile )) == NULL) {
950 d_fprintf(stderr, "Could not get rootkey\n");
953 d_printf("RootKey: [%s]\n", nk->keyname);
955 write_registry_tree( infile, nk, NULL, outfile, "" );
961 d_printf("Closing %s...", argv[1]);
963 regfio_close( outfile );
967 d_printf("Closing %s...", argv[0]);
969 regfio_close( infile );
976 /********************************************************************
977 ********************************************************************/
979 static NTSTATUS rpc_registry_getsd_internal(const DOM_SID *domain_sid,
980 const char *domain_name,
981 struct cli_state *cli,
982 struct rpc_pipe_client *pipe_hnd,
987 POLICY_HND pol_hive, pol_key;
989 struct KeySecurityData *sd = NULL;
992 struct security_descriptor sec_desc;
994 if (argc <1 || argc > 2) {
995 d_printf("Usage: net rpc registry getsd <path> <secinfo>\n");
996 d_printf("Example: net rpc registry getsd 'HKLM\\Software\\Samba'\n");
1000 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1001 &pol_hive, &pol_key);
1002 if (!NT_STATUS_IS_OK(status)) {
1003 d_fprintf(stderr, "registry_openkey failed: %s\n",
1008 sd = TALLOC_ZERO_P(mem_ctx, struct KeySecurityData);
1010 status = NT_STATUS_NO_MEMORY;
1017 sscanf(argv[1], "%x", &sec_info);
1019 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
1022 status = registry_getsd(mem_ctx, pipe_hnd, &pol_key, sec_info, sd);
1023 if (!NT_STATUS_IS_OK(status)) {
1024 d_fprintf(stderr, "getting sd failed: %s\n",
1029 blob.data = sd->data;
1030 blob.length = sd->size;
1032 status = ndr_pull_struct_blob(&blob, mem_ctx, &sec_desc,
1033 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
1034 if (!NT_STATUS_IS_OK(status)) {
1038 display_sec_desc(&sec_desc);
1041 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key);
1042 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive);
1048 static int rpc_registry_getsd(int argc, const char **argv)
1050 return run_rpc_command(NULL, PI_WINREG, 0,
1051 rpc_registry_getsd_internal, argc, argv);
1054 /********************************************************************
1055 ********************************************************************/
1057 int net_rpc_registry(int argc, const char **argv)
1059 struct functable2 func[] = {
1060 { "enumerate", rpc_registry_enumerate,
1061 "Enumerate registry keys and values" },
1062 { "createkey", rpc_registry_createkey,
1063 "Create a new registry key" },
1064 { "deletekey", rpc_registry_deletekey,
1065 "Delete a registry key" },
1066 { "setvalue", rpc_registry_setvalue,
1067 "Set a new registry value" },
1068 { "deletevalue", rpc_registry_deletevalue,
1069 "Delete a registry value" },
1070 { "save", rpc_registry_save,
1071 "Save a registry file" },
1072 { "dump", rpc_registry_dump,
1073 "Dump a registry file" },
1074 { "copy", rpc_registry_copy,
1075 "Copy a registry file" },
1076 { "getsd", rpc_registry_getsd,
1077 "Get security descriptor" },
1081 return net_run_function2(argc, argv, "net rpc registry", func);