| 1 | /*
|
|---|
| 2 | Samba Unix/Linux SMB client library
|
|---|
| 3 | Distributed SMB/CIFS Server Management Utility
|
|---|
| 4 |
|
|---|
| 5 | Copyright (C) Gerald (Jerry) Carter 2005-2006
|
|---|
| 6 |
|
|---|
| 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.
|
|---|
| 11 |
|
|---|
| 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.
|
|---|
| 16 |
|
|---|
| 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/>. */
|
|---|
| 19 |
|
|---|
| 20 | #include "includes.h"
|
|---|
| 21 | #include "utils/net.h"
|
|---|
| 22 | #include "utils/net_registry_util.h"
|
|---|
| 23 | #include "regfio.h"
|
|---|
| 24 | #include "reg_objects.h"
|
|---|
| 25 |
|
|---|
| 26 | static bool reg_hive_key(TALLOC_CTX *ctx, const char *fullname,
|
|---|
| 27 | uint32 *reg_type, const char **key_name)
|
|---|
| 28 | {
|
|---|
| 29 | WERROR werr;
|
|---|
| 30 | char *hivename = NULL;
|
|---|
| 31 | char *tmp_keyname = NULL;
|
|---|
| 32 | bool ret = false;
|
|---|
| 33 | TALLOC_CTX *tmp_ctx = talloc_stackframe();
|
|---|
| 34 |
|
|---|
| 35 | werr = split_hive_key(tmp_ctx, fullname, &hivename, &tmp_keyname);
|
|---|
| 36 | if (!W_ERROR_IS_OK(werr)) {
|
|---|
| 37 | goto done;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | *key_name = talloc_strdup(ctx, tmp_keyname);
|
|---|
| 41 | if (*key_name == NULL) {
|
|---|
| 42 | goto done;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if (strequal(hivename, "HKLM") ||
|
|---|
| 46 | strequal(hivename, "HKEY_LOCAL_MACHINE"))
|
|---|
| 47 | {
|
|---|
| 48 | (*reg_type) = HKEY_LOCAL_MACHINE;
|
|---|
| 49 | } else if (strequal(hivename, "HKCR") ||
|
|---|
| 50 | strequal(hivename, "HKEY_CLASSES_ROOT"))
|
|---|
| 51 | {
|
|---|
| 52 | (*reg_type) = HKEY_CLASSES_ROOT;
|
|---|
| 53 | } else if (strequal(hivename, "HKU") ||
|
|---|
| 54 | strequal(hivename, "HKEY_USERS"))
|
|---|
| 55 | {
|
|---|
| 56 | (*reg_type) = HKEY_USERS;
|
|---|
| 57 | } else if (strequal(hivename, "HKCU") ||
|
|---|
| 58 | strequal(hivename, "HKEY_CURRENT_USER"))
|
|---|
| 59 | {
|
|---|
| 60 | (*reg_type) = HKEY_CURRENT_USER;
|
|---|
| 61 | } else if (strequal(hivename, "HKPD") ||
|
|---|
| 62 | strequal(hivename, "HKEY_PERFORMANCE_DATA"))
|
|---|
| 63 | {
|
|---|
| 64 | (*reg_type) = HKEY_PERFORMANCE_DATA;
|
|---|
| 65 | } else {
|
|---|
| 66 | DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
|
|---|
| 67 | fullname));
|
|---|
| 68 | goto done;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | ret = true;
|
|---|
| 72 |
|
|---|
| 73 | done:
|
|---|
| 74 | TALLOC_FREE(tmp_ctx);
|
|---|
| 75 | return ret;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
|
|---|
| 79 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 80 | const char *name, uint32 access_mask,
|
|---|
| 81 | struct policy_handle *hive_hnd,
|
|---|
| 82 | struct policy_handle *key_hnd)
|
|---|
| 83 | {
|
|---|
| 84 | uint32 hive;
|
|---|
| 85 | NTSTATUS status;
|
|---|
| 86 | struct winreg_String key;
|
|---|
| 87 |
|
|---|
| 88 | ZERO_STRUCT(key);
|
|---|
| 89 |
|
|---|
| 90 | if (!reg_hive_key(mem_ctx, name, &hive, &key.name)) {
|
|---|
| 91 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive, access_mask,
|
|---|
| 95 | hive_hnd);
|
|---|
| 96 | if (!(NT_STATUS_IS_OK(status))) {
|
|---|
| 97 | return status;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, hive_hnd, key, 0,
|
|---|
| 101 | access_mask, key_hnd, NULL);
|
|---|
| 102 | if (!(NT_STATUS_IS_OK(status))) {
|
|---|
| 103 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, hive_hnd, NULL);
|
|---|
| 104 | return status;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | return NT_STATUS_OK;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
|
|---|
| 111 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 112 | struct policy_handle *key_hnd,
|
|---|
| 113 | uint32 *pnum_keys, char ***pnames,
|
|---|
| 114 | char ***pclasses, NTTIME ***pmodtimes)
|
|---|
| 115 | {
|
|---|
| 116 | TALLOC_CTX *mem_ctx;
|
|---|
| 117 | NTSTATUS status;
|
|---|
| 118 | uint32 num_subkeys, max_subkeylen, max_classlen;
|
|---|
| 119 | uint32 num_values, max_valnamelen, max_valbufsize;
|
|---|
| 120 | uint32 i;
|
|---|
| 121 | NTTIME last_changed_time;
|
|---|
| 122 | uint32 secdescsize;
|
|---|
| 123 | struct winreg_String classname;
|
|---|
| 124 | char **names, **classes;
|
|---|
| 125 | NTTIME **modtimes;
|
|---|
| 126 |
|
|---|
| 127 | if (!(mem_ctx = talloc_new(ctx))) {
|
|---|
| 128 | return NT_STATUS_NO_MEMORY;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | ZERO_STRUCT(classname);
|
|---|
| 132 | status = rpccli_winreg_QueryInfoKey(
|
|---|
| 133 | pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
|
|---|
| 134 | &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
|
|---|
| 135 | &max_valbufsize, &secdescsize, &last_changed_time, NULL );
|
|---|
| 136 |
|
|---|
| 137 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 138 | goto error;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | if (num_subkeys == 0) {
|
|---|
| 142 | *pnum_keys = 0;
|
|---|
| 143 | TALLOC_FREE(mem_ctx);
|
|---|
| 144 | return NT_STATUS_OK;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
|
|---|
| 148 | (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
|
|---|
| 149 | (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
|
|---|
| 150 | num_subkeys)))) {
|
|---|
| 151 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 152 | goto error;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | for (i=0; i<num_subkeys; i++) {
|
|---|
| 156 | char c, n;
|
|---|
| 157 | struct winreg_StringBuf class_buf;
|
|---|
| 158 | struct winreg_StringBuf name_buf;
|
|---|
| 159 | NTTIME modtime;
|
|---|
| 160 | WERROR werr;
|
|---|
| 161 |
|
|---|
| 162 | c = '\0';
|
|---|
| 163 | class_buf.name = &c;
|
|---|
| 164 | class_buf.size = max_classlen+2;
|
|---|
| 165 |
|
|---|
| 166 | n = '\0';
|
|---|
| 167 | name_buf.name = &n;
|
|---|
| 168 | name_buf.size = max_subkeylen+2;
|
|---|
| 169 |
|
|---|
| 170 | ZERO_STRUCT(modtime);
|
|---|
| 171 |
|
|---|
| 172 | status = rpccli_winreg_EnumKey(pipe_hnd, mem_ctx, key_hnd,
|
|---|
| 173 | i, &name_buf, &class_buf,
|
|---|
| 174 | &modtime, &werr);
|
|---|
| 175 |
|
|---|
| 176 | if (W_ERROR_EQUAL(werr,
|
|---|
| 177 | WERR_NO_MORE_ITEMS) ) {
|
|---|
| 178 | status = NT_STATUS_OK;
|
|---|
| 179 | break;
|
|---|
| 180 | }
|
|---|
| 181 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 182 | goto error;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | classes[i] = NULL;
|
|---|
| 186 |
|
|---|
| 187 | if (class_buf.name &&
|
|---|
| 188 | (!(classes[i] = talloc_strdup(classes, class_buf.name)))) {
|
|---|
| 189 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 190 | goto error;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | if (!(names[i] = talloc_strdup(names, name_buf.name))) {
|
|---|
| 194 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 195 | goto error;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | if ((!(modtimes[i] = (NTTIME *)talloc_memdup(
|
|---|
| 199 | modtimes, &modtime, sizeof(modtime))))) {
|
|---|
| 200 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 201 | goto error;
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | *pnum_keys = num_subkeys;
|
|---|
| 206 |
|
|---|
| 207 | if (pnames) {
|
|---|
| 208 | *pnames = talloc_move(ctx, &names);
|
|---|
| 209 | }
|
|---|
| 210 | if (pclasses) {
|
|---|
| 211 | *pclasses = talloc_move(ctx, &classes);
|
|---|
| 212 | }
|
|---|
| 213 | if (pmodtimes) {
|
|---|
| 214 | *pmodtimes = talloc_move(ctx, &modtimes);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | status = NT_STATUS_OK;
|
|---|
| 218 |
|
|---|
| 219 | error:
|
|---|
| 220 | TALLOC_FREE(mem_ctx);
|
|---|
| 221 | return status;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
|
|---|
| 225 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 226 | struct policy_handle *key_hnd,
|
|---|
| 227 | uint32 *pnum_values, char ***pvalnames,
|
|---|
| 228 | struct registry_value ***pvalues)
|
|---|
| 229 | {
|
|---|
| 230 | TALLOC_CTX *mem_ctx;
|
|---|
| 231 | NTSTATUS status;
|
|---|
| 232 | uint32 num_subkeys, max_subkeylen, max_classlen;
|
|---|
| 233 | uint32 num_values, max_valnamelen, max_valbufsize;
|
|---|
| 234 | uint32 i;
|
|---|
| 235 | NTTIME last_changed_time;
|
|---|
| 236 | uint32 secdescsize;
|
|---|
| 237 | struct winreg_String classname;
|
|---|
| 238 | struct registry_value **values;
|
|---|
| 239 | char **names;
|
|---|
| 240 |
|
|---|
| 241 | if (!(mem_ctx = talloc_new(ctx))) {
|
|---|
| 242 | return NT_STATUS_NO_MEMORY;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | ZERO_STRUCT(classname);
|
|---|
| 246 | status = rpccli_winreg_QueryInfoKey(
|
|---|
| 247 | pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
|
|---|
| 248 | &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
|
|---|
| 249 | &max_valbufsize, &secdescsize, &last_changed_time, NULL );
|
|---|
| 250 |
|
|---|
| 251 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 252 | goto error;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | if (num_values == 0) {
|
|---|
| 256 | *pnum_values = 0;
|
|---|
| 257 | TALLOC_FREE(mem_ctx);
|
|---|
| 258 | return NT_STATUS_OK;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
|
|---|
| 262 | (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
|
|---|
| 263 | num_values)))) {
|
|---|
| 264 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 265 | goto error;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | for (i=0; i<num_values; i++) {
|
|---|
| 269 | enum winreg_Type type = REG_NONE;
|
|---|
| 270 | uint8 *data = NULL;
|
|---|
| 271 | uint32 data_size;
|
|---|
| 272 | uint32 value_length;
|
|---|
| 273 |
|
|---|
| 274 | char n;
|
|---|
| 275 | struct winreg_ValNameBuf name_buf;
|
|---|
| 276 | WERROR err;
|
|---|
| 277 |
|
|---|
| 278 | n = '\0';
|
|---|
| 279 | name_buf.name = &n;
|
|---|
| 280 | name_buf.size = max_valnamelen + 2;
|
|---|
| 281 |
|
|---|
| 282 | data_size = max_valbufsize;
|
|---|
| 283 | data = (uint8 *)TALLOC(mem_ctx, data_size);
|
|---|
| 284 | value_length = 0;
|
|---|
| 285 |
|
|---|
| 286 | status = rpccli_winreg_EnumValue(pipe_hnd, mem_ctx, key_hnd,
|
|---|
| 287 | i, &name_buf, &type,
|
|---|
| 288 | data, &data_size,
|
|---|
| 289 | &value_length, &err);
|
|---|
| 290 |
|
|---|
| 291 | if ( W_ERROR_EQUAL(err,
|
|---|
| 292 | WERR_NO_MORE_ITEMS) ) {
|
|---|
| 293 | status = NT_STATUS_OK;
|
|---|
| 294 | break;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | if (!(NT_STATUS_IS_OK(status))) {
|
|---|
| 298 | goto error;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | if (name_buf.name == NULL) {
|
|---|
| 302 | status = NT_STATUS_INVALID_PARAMETER;
|
|---|
| 303 | goto error;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | if (!(names[i] = talloc_strdup(names, name_buf.name))) {
|
|---|
| 307 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 308 | goto error;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | err = registry_pull_value(values, &values[i], type, data,
|
|---|
| 312 | data_size, value_length);
|
|---|
| 313 | if (!W_ERROR_IS_OK(err)) {
|
|---|
| 314 | status = werror_to_ntstatus(err);
|
|---|
| 315 | goto error;
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | *pnum_values = num_values;
|
|---|
| 320 |
|
|---|
| 321 | if (pvalnames) {
|
|---|
| 322 | *pvalnames = talloc_move(ctx, &names);
|
|---|
| 323 | }
|
|---|
| 324 | if (pvalues) {
|
|---|
| 325 | *pvalues = talloc_move(ctx, &values);
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | status = NT_STATUS_OK;
|
|---|
| 329 |
|
|---|
| 330 | error:
|
|---|
| 331 | TALLOC_FREE(mem_ctx);
|
|---|
| 332 | return status;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | static NTSTATUS registry_getsd(TALLOC_CTX *mem_ctx,
|
|---|
| 336 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 337 | struct policy_handle *key_hnd,
|
|---|
| 338 | uint32_t sec_info,
|
|---|
| 339 | struct KeySecurityData *sd)
|
|---|
| 340 | {
|
|---|
| 341 | return rpccli_winreg_GetKeySecurity(pipe_hnd, mem_ctx, key_hnd,
|
|---|
| 342 | sec_info, sd, NULL);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 | static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
|
|---|
| 347 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 348 | struct policy_handle *key_hnd,
|
|---|
| 349 | const char *name,
|
|---|
| 350 | const struct registry_value *value)
|
|---|
| 351 | {
|
|---|
| 352 | struct winreg_String name_string;
|
|---|
| 353 | DATA_BLOB blob;
|
|---|
| 354 | NTSTATUS result;
|
|---|
| 355 | WERROR err;
|
|---|
| 356 |
|
|---|
| 357 | err = registry_push_value(mem_ctx, value, &blob);
|
|---|
| 358 | if (!W_ERROR_IS_OK(err)) {
|
|---|
| 359 | return werror_to_ntstatus(err);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | ZERO_STRUCT(name_string);
|
|---|
| 363 |
|
|---|
| 364 | name_string.name = name;
|
|---|
| 365 | result = rpccli_winreg_SetValue(pipe_hnd, blob.data, key_hnd,
|
|---|
| 366 | name_string, value->type,
|
|---|
| 367 | blob.data, blob.length, NULL);
|
|---|
| 368 | TALLOC_FREE(blob.data);
|
|---|
| 369 | return result;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | static NTSTATUS rpc_registry_setvalue_internal(const DOM_SID *domain_sid,
|
|---|
| 373 | const char *domain_name,
|
|---|
| 374 | struct cli_state *cli,
|
|---|
| 375 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 376 | TALLOC_CTX *mem_ctx,
|
|---|
| 377 | int argc,
|
|---|
| 378 | const char **argv )
|
|---|
| 379 | {
|
|---|
| 380 | struct policy_handle hive_hnd, key_hnd;
|
|---|
| 381 | NTSTATUS status;
|
|---|
| 382 | struct registry_value value;
|
|---|
| 383 |
|
|---|
| 384 | status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
|
|---|
| 385 | SEC_RIGHTS_MAXIMUM_ALLOWED,
|
|---|
| 386 | &hive_hnd, &key_hnd);
|
|---|
| 387 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 388 | d_fprintf(stderr, "registry_openkey failed: %s\n",
|
|---|
| 389 | nt_errstr(status));
|
|---|
| 390 | return status;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
|
|---|
| 394 | d_fprintf(stderr, "Too many args for type %s\n", argv[2]);
|
|---|
| 395 | return NT_STATUS_NOT_IMPLEMENTED;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | if (strequal(argv[2], "dword")) {
|
|---|
| 399 | value.type = REG_DWORD;
|
|---|
| 400 | value.v.dword = strtoul(argv[3], NULL, 10);
|
|---|
| 401 | }
|
|---|
| 402 | else if (strequal(argv[2], "sz")) {
|
|---|
| 403 | value.type = REG_SZ;
|
|---|
| 404 | value.v.sz.len = strlen(argv[3])+1;
|
|---|
| 405 | value.v.sz.str = CONST_DISCARD(char *, argv[3]);
|
|---|
| 406 | }
|
|---|
| 407 | else {
|
|---|
| 408 | d_fprintf(stderr, "type \"%s\" not implemented\n", argv[2]);
|
|---|
| 409 | status = NT_STATUS_NOT_IMPLEMENTED;
|
|---|
| 410 | goto error;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
|
|---|
| 414 | argv[1], &value);
|
|---|
| 415 |
|
|---|
| 416 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 417 | d_fprintf(stderr, "registry_setvalue failed: %s\n",
|
|---|
| 418 | nt_errstr(status));
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | error:
|
|---|
| 422 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd, NULL);
|
|---|
| 423 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
|
|---|
| 424 |
|
|---|
| 425 | return NT_STATUS_OK;
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | static int rpc_registry_setvalue( int argc, const char **argv )
|
|---|
| 429 | {
|
|---|
| 430 | if (argc < 4) {
|
|---|
| 431 | d_fprintf(stderr, "usage: net rpc registry setvalue <key> "
|
|---|
| 432 | "<valuename> <type> [<val>]+\n");
|
|---|
| 433 | return -1;
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | return run_rpc_command( NULL, PI_WINREG, 0,
|
|---|
| 437 | rpc_registry_setvalue_internal, argc, argv );
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | static NTSTATUS rpc_registry_deletevalue_internal(const DOM_SID *domain_sid,
|
|---|
| 441 | const char *domain_name,
|
|---|
| 442 | struct cli_state *cli,
|
|---|
| 443 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 444 | TALLOC_CTX *mem_ctx,
|
|---|
| 445 | int argc,
|
|---|
| 446 | const char **argv )
|
|---|
| 447 | {
|
|---|
| 448 | struct policy_handle hive_hnd, key_hnd;
|
|---|
| 449 | NTSTATUS status;
|
|---|
| 450 | struct winreg_String valuename;
|
|---|
| 451 |
|
|---|
| 452 | ZERO_STRUCT(valuename);
|
|---|
| 453 |
|
|---|
| 454 | status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
|
|---|
| 455 | SEC_RIGHTS_MAXIMUM_ALLOWED,
|
|---|
| 456 | &hive_hnd, &key_hnd);
|
|---|
| 457 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 458 | d_fprintf(stderr, "registry_openkey failed: %s\n",
|
|---|
| 459 | nt_errstr(status));
|
|---|
| 460 | return status;
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | valuename.name = argv[1];
|
|---|
| 464 |
|
|---|
| 465 | status = rpccli_winreg_DeleteValue(pipe_hnd, mem_ctx, &key_hnd,
|
|---|
| 466 | valuename, NULL);
|
|---|
| 467 |
|
|---|
| 468 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 469 | d_fprintf(stderr, "registry_deletevalue failed: %s\n",
|
|---|
| 470 | nt_errstr(status));
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd, NULL);
|
|---|
| 474 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
|
|---|
| 475 |
|
|---|
| 476 | return status;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | static int rpc_registry_deletevalue( int argc, const char **argv )
|
|---|
| 480 | {
|
|---|
| 481 | if (argc != 2) {
|
|---|
| 482 | d_fprintf(stderr, "usage: net rpc registry deletevalue <key> "
|
|---|
| 483 | "<valuename>\n");
|
|---|
| 484 | return -1;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | return run_rpc_command( NULL, PI_WINREG, 0,
|
|---|
| 488 | rpc_registry_deletevalue_internal, argc, argv );
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | static NTSTATUS rpc_registry_getvalue_internal(const DOM_SID *domain_sid,
|
|---|
| 492 | const char *domain_name,
|
|---|
| 493 | struct cli_state *cli,
|
|---|
| 494 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 495 | TALLOC_CTX *mem_ctx,
|
|---|
| 496 | int argc,
|
|---|
| 497 | const char **argv)
|
|---|
| 498 | {
|
|---|
| 499 | struct policy_handle hive_hnd, key_hnd;
|
|---|
| 500 | NTSTATUS status;
|
|---|
| 501 | WERROR werr;
|
|---|
| 502 | struct winreg_String valuename;
|
|---|
| 503 | struct registry_value *value = NULL;
|
|---|
| 504 | enum winreg_Type type = REG_NONE;
|
|---|
| 505 | uint8_t *data = NULL;
|
|---|
| 506 | uint32_t data_size = 0;
|
|---|
| 507 | uint32_t value_length = 0;
|
|---|
| 508 | TALLOC_CTX *tmp_ctx = talloc_stackframe();
|
|---|
| 509 |
|
|---|
| 510 | ZERO_STRUCT(valuename);
|
|---|
| 511 |
|
|---|
| 512 | status = registry_openkey(tmp_ctx, pipe_hnd, argv[0],
|
|---|
| 513 | SEC_RIGHTS_MAXIMUM_ALLOWED,
|
|---|
| 514 | &hive_hnd, &key_hnd);
|
|---|
| 515 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 516 | d_fprintf(stderr, "registry_openkey failed: %s\n",
|
|---|
| 517 | nt_errstr(status));
|
|---|
| 518 | return status;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | valuename.name = argv[1];
|
|---|
| 522 |
|
|---|
| 523 | /*
|
|---|
| 524 | * call QueryValue once with data == NULL to get the
|
|---|
| 525 | * needed memory size to be allocated, then allocate
|
|---|
| 526 | * data buffer and call again.
|
|---|
| 527 | */
|
|---|
| 528 | status = rpccli_winreg_QueryValue(pipe_hnd, tmp_ctx, &key_hnd,
|
|---|
| 529 | &valuename,
|
|---|
| 530 | &type,
|
|---|
| 531 | data,
|
|---|
| 532 | &data_size,
|
|---|
| 533 | &value_length,
|
|---|
| 534 | NULL);
|
|---|
| 535 |
|
|---|
| 536 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 537 | d_fprintf(stderr, "registry_queryvalue failed: %s\n",
|
|---|
| 538 | nt_errstr(status));
|
|---|
| 539 | goto done;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | data = (uint8 *)TALLOC(tmp_ctx, data_size);
|
|---|
| 543 | value_length = 0;
|
|---|
| 544 |
|
|---|
| 545 | status = rpccli_winreg_QueryValue(pipe_hnd, tmp_ctx, &key_hnd,
|
|---|
| 546 | &valuename,
|
|---|
| 547 | &type,
|
|---|
| 548 | data,
|
|---|
| 549 | &data_size,
|
|---|
| 550 | &value_length,
|
|---|
| 551 | NULL);
|
|---|
| 552 |
|
|---|
| 553 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 554 | d_fprintf(stderr, "registry_queryvalue failed: %s\n",
|
|---|
| 555 | nt_errstr(status));
|
|---|
| 556 | goto done;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | werr = registry_pull_value(tmp_ctx, &value, type, data,
|
|---|
| 560 | data_size, value_length);
|
|---|
| 561 | if (!W_ERROR_IS_OK(werr)) {
|
|---|
| 562 | status = werror_to_ntstatus(werr);
|
|---|
| 563 | goto done;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | print_registry_value(value);
|
|---|
| 567 |
|
|---|
| 568 | done:
|
|---|
| 569 | rpccli_winreg_CloseKey(pipe_hnd, tmp_ctx, &key_hnd, NULL);
|
|---|
| 570 | rpccli_winreg_CloseKey(pipe_hnd, tmp_ctx, &hive_hnd, NULL);
|
|---|
| 571 |
|
|---|
| 572 | TALLOC_FREE(tmp_ctx);
|
|---|
| 573 |
|
|---|
| 574 | return status;
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | static int rpc_registry_getvalue(int argc, const char **argv)
|
|---|
| 578 | {
|
|---|
| 579 | if (argc != 2) {
|
|---|
| 580 | d_fprintf(stderr, "usage: net rpc registry deletevalue <key> "
|
|---|
| 581 | "<valuename>\n");
|
|---|
| 582 | return -1;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | return run_rpc_command(NULL, PI_WINREG, 0,
|
|---|
| 586 | rpc_registry_getvalue_internal, argc, argv);
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | static NTSTATUS rpc_registry_createkey_internal(const DOM_SID *domain_sid,
|
|---|
| 590 | const char *domain_name,
|
|---|
| 591 | struct cli_state *cli,
|
|---|
| 592 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 593 | TALLOC_CTX *mem_ctx,
|
|---|
| 594 | int argc,
|
|---|
| 595 | const char **argv )
|
|---|
| 596 | {
|
|---|
| 597 | uint32 hive;
|
|---|
| 598 | struct policy_handle hive_hnd, key_hnd;
|
|---|
| 599 | struct winreg_String key, keyclass;
|
|---|
| 600 | enum winreg_CreateAction action;
|
|---|
| 601 | NTSTATUS status;
|
|---|
| 602 |
|
|---|
| 603 | ZERO_STRUCT(key);
|
|---|
| 604 | ZERO_STRUCT(keyclass);
|
|---|
| 605 |
|
|---|
| 606 | if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
|
|---|
| 607 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive,
|
|---|
| 611 | SEC_RIGHTS_MAXIMUM_ALLOWED,
|
|---|
| 612 | &hive_hnd);
|
|---|
| 613 | if (!(NT_STATUS_IS_OK(status))) {
|
|---|
| 614 | return status;
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | action = REG_ACTION_NONE;
|
|---|
| 618 | keyclass.name = "";
|
|---|
| 619 |
|
|---|
| 620 | status = rpccli_winreg_CreateKey(pipe_hnd, mem_ctx, &hive_hnd, key,
|
|---|
| 621 | keyclass, 0, REG_KEY_READ, NULL,
|
|---|
| 622 | &key_hnd, &action, NULL);
|
|---|
| 623 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 624 | d_fprintf(stderr, "createkey returned %s\n",
|
|---|
| 625 | nt_errstr(status));
|
|---|
| 626 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
|
|---|
| 627 | return status;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | switch (action) {
|
|---|
| 631 | case REG_ACTION_NONE:
|
|---|
| 632 | d_printf("createkey did nothing -- huh?\n");
|
|---|
| 633 | break;
|
|---|
| 634 | case REG_CREATED_NEW_KEY:
|
|---|
| 635 | d_printf("createkey created %s\n", argv[0]);
|
|---|
| 636 | break;
|
|---|
| 637 | case REG_OPENED_EXISTING_KEY:
|
|---|
| 638 | d_printf("createkey opened existing %s\n", argv[0]);
|
|---|
| 639 | break;
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd, NULL);
|
|---|
| 643 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
|
|---|
| 644 |
|
|---|
| 645 | return status;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | static int rpc_registry_createkey( int argc, const char **argv )
|
|---|
| 649 | {
|
|---|
| 650 | if (argc != 1) {
|
|---|
| 651 | d_fprintf(stderr, "usage: net rpc registry createkey <key>\n");
|
|---|
| 652 | return -1;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | return run_rpc_command( NULL, PI_WINREG, 0,
|
|---|
| 656 | rpc_registry_createkey_internal, argc, argv );
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | static NTSTATUS rpc_registry_deletekey_internal(const DOM_SID *domain_sid,
|
|---|
| 660 | const char *domain_name,
|
|---|
| 661 | struct cli_state *cli,
|
|---|
| 662 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 663 | TALLOC_CTX *mem_ctx,
|
|---|
| 664 | int argc,
|
|---|
| 665 | const char **argv )
|
|---|
| 666 | {
|
|---|
| 667 | uint32 hive;
|
|---|
| 668 | struct policy_handle hive_hnd;
|
|---|
| 669 | struct winreg_String key;
|
|---|
| 670 | NTSTATUS status;
|
|---|
| 671 |
|
|---|
| 672 | ZERO_STRUCT(key);
|
|---|
| 673 |
|
|---|
| 674 | if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
|
|---|
| 675 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive,
|
|---|
| 679 | SEC_RIGHTS_MAXIMUM_ALLOWED,
|
|---|
| 680 | &hive_hnd);
|
|---|
| 681 | if (!(NT_STATUS_IS_OK(status))) {
|
|---|
| 682 | return status;
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | status = rpccli_winreg_DeleteKey(pipe_hnd, mem_ctx, &hive_hnd, key, NULL);
|
|---|
| 686 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
|
|---|
| 687 |
|
|---|
| 688 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 689 | d_fprintf(stderr, "deletekey returned %s\n",
|
|---|
| 690 | nt_errstr(status));
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | return status;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | static int rpc_registry_deletekey( int argc, const char **argv )
|
|---|
| 697 | {
|
|---|
| 698 | if (argc != 1) {
|
|---|
| 699 | d_fprintf(stderr, "usage: net rpc registry deletekey <key>\n");
|
|---|
| 700 | return -1;
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | return run_rpc_command( NULL, PI_WINREG, 0,
|
|---|
| 704 | rpc_registry_deletekey_internal, argc, argv );
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | /********************************************************************
|
|---|
| 708 | ********************************************************************/
|
|---|
| 709 |
|
|---|
| 710 | static NTSTATUS rpc_registry_enumerate_internal(const DOM_SID *domain_sid,
|
|---|
| 711 | const char *domain_name,
|
|---|
| 712 | struct cli_state *cli,
|
|---|
| 713 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 714 | TALLOC_CTX *mem_ctx,
|
|---|
| 715 | int argc,
|
|---|
| 716 | const char **argv )
|
|---|
| 717 | {
|
|---|
| 718 | POLICY_HND pol_hive, pol_key;
|
|---|
| 719 | NTSTATUS status;
|
|---|
| 720 | uint32 num_subkeys = 0;
|
|---|
| 721 | uint32 num_values = 0;
|
|---|
| 722 | char **names = NULL, **classes = NULL;
|
|---|
| 723 | NTTIME **modtimes = NULL;
|
|---|
| 724 | uint32 i;
|
|---|
| 725 | struct registry_value **values = NULL;
|
|---|
| 726 |
|
|---|
| 727 | if (argc != 1 ) {
|
|---|
| 728 | d_printf("Usage: net rpc registry enumerate <path> [recurse]\n");
|
|---|
| 729 | d_printf("Example: net rpc registry enumerate 'HKLM\\Software\\Samba'\n");
|
|---|
| 730 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
|
|---|
| 734 | &pol_hive, &pol_key);
|
|---|
| 735 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 736 | d_fprintf(stderr, "registry_openkey failed: %s\n",
|
|---|
| 737 | nt_errstr(status));
|
|---|
| 738 | return status;
|
|---|
| 739 | }
|
|---|
| 740 |
|
|---|
| 741 | status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
|
|---|
| 742 | &names, &classes, &modtimes);
|
|---|
| 743 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 744 | d_fprintf(stderr, "enumerating keys failed: %s\n",
|
|---|
| 745 | nt_errstr(status));
|
|---|
| 746 | return status;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | for (i=0; i<num_subkeys; i++) {
|
|---|
| 750 | print_registry_key(names[i], modtimes[i]);
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
|
|---|
| 754 | &names, &values);
|
|---|
| 755 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 756 | d_fprintf(stderr, "enumerating values failed: %s\n",
|
|---|
| 757 | nt_errstr(status));
|
|---|
| 758 | return status;
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| 761 | for (i=0; i<num_values; i++) {
|
|---|
| 762 | print_registry_value_with_name(names[i], values[i]);
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key, NULL);
|
|---|
| 766 | rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive, NULL);
|
|---|
| 767 |
|
|---|
| 768 | return status;
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | /********************************************************************
|
|---|
| 772 | ********************************************************************/
|
|---|
| 773 |
|
|---|
| 774 | static int rpc_registry_enumerate( int argc, const char **argv )
|
|---|
| 775 | {
|
|---|
| 776 | return run_rpc_command( NULL, PI_WINREG, 0,
|
|---|
| 777 | rpc_registry_enumerate_internal, argc, argv );
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | /********************************************************************
|
|---|
| 781 | ********************************************************************/
|
|---|
| 782 |
|
|---|
| 783 | static NTSTATUS rpc_registry_save_internal(const DOM_SID *domain_sid,
|
|---|
| 784 | const char *domain_name,
|
|---|
| 785 | struct cli_state *cli,
|
|---|
| 786 | struct rpc_pipe_client *pipe_hnd,
|
|---|
| 787 | TALLOC_CTX *mem_ctx,
|
|---|
| 788 | int argc,
|
|---|
| 789 | const char **argv )
|
|---|
| 790 | {
|
|---|
| 791 | WERROR result = WERR_GENERAL_FAILURE;
|
|---|
| 792 | POLICY_HND pol_hive, pol_key;
|
|---|
| 793 | NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 794 | struct winreg_String filename;
|
|---|
| 795 |
|
|---|
| 796 | if (argc != 2 ) {
|
|---|
| 797 | d_printf("Usage: net rpc registry backup <path> <file> \n");
|
|---|
| 798 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 | status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_ALL,
|
|---|
| 802 | &pol_hive, &pol_key);
|
|---|
| 803 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 804 | d_fprintf(stderr, "registry_openkey failed: %s\n",
|
|---|
| 805 | nt_errstr(status));
|
|---|
| 806 | return status;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | filename.name = argv[1];
|
|---|
| 810 | status = rpccli_winreg_SaveKey( pipe_hnd, mem_ctx, &pol_key, &filename, NULL, NULL);
|
|---|
| 811 | if ( !W_ERROR_IS_OK(result) ) {
|
|---|
| 812 | d_fprintf(stderr, "Unable to save [%s] to %s:%s\n", argv[0], cli->desthost, argv[1]);
|
|---|
|
|---|