2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 1992-2000,
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
7 Copyright (C) Paul Ashton 1997-2000.
8 Copyright (C) Jeremy Allison 1999.
9 Copyright (C) Simo Sorce 2001
10 Copyright (C) Jeremy Cooper 2004
11 Copyright (C) Gerald (Jerry) Carter 2005
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "rpc_client.h"
31 /* Shutdown a server */
33 /*******************************************************************
34 internal connect to a registry hive root (open a registry policy)
35 *******************************************************************/
37 static WERROR cli_reg_open_hive_int(struct cli_state *cli,
38 TALLOC_CTX *mem_ctx, uint16 op_code,
40 uint32 access_mask, POLICY_HND *hnd)
44 prs_struct qbuf, rbuf;
49 init_reg_q_open_hive(&in, access_mask);
51 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, op_code,
56 WERR_GENERAL_FAILURE );
58 if ( !W_ERROR_IS_OK( out.status ) )
61 memcpy( hnd, &out.pol, sizeof(POLICY_HND) );
66 /*******************************************************************
67 connect to a registry hive root (open a registry policy)
68 *******************************************************************/
70 WERROR cli_reg_connect(struct cli_state *cli, TALLOC_CTX *mem_ctx,
71 uint32 reg_type, uint32 access_mask,
76 ZERO_STRUCTP(reg_hnd);
80 case HKEY_CLASSES_ROOT:
81 op_code = REG_OPEN_HKCR;
82 op_name = "REG_OPEN_HKCR";
84 case HKEY_LOCAL_MACHINE:
85 op_code = REG_OPEN_HKLM;
86 op_name = "REG_OPEN_HKLM";
89 op_code = REG_OPEN_HKU;
90 op_name = "REG_OPEN_HKU";
92 case HKEY_PERFORMANCE_DATA:
93 op_code = REG_OPEN_HKPD;
94 op_name = "REG_OPEN_HKPD";
97 return WERR_INVALID_PARAM;
100 return cli_reg_open_hive_int(cli, mem_ctx, op_code, op_name,
101 access_mask, reg_hnd);
105 /*******************************************************************
106 *******************************************************************/
108 WERROR cli_reg_shutdown(struct cli_state * cli, TALLOC_CTX *mem_ctx,
109 const char *msg, uint32 timeout, BOOL do_reboot,
114 prs_struct qbuf, rbuf;
117 return WERR_INVALID_PARAM;
122 /* Marshall data and send request */
124 init_reg_q_shutdown(&in, msg, timeout, do_reboot, force);
126 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_SHUTDOWN,
131 WERR_GENERAL_FAILURE );
136 /*******************************************************************
137 *******************************************************************/
139 WERROR cli_reg_abort_shutdown(struct cli_state * cli, TALLOC_CTX *mem_ctx)
141 REG_Q_ABORT_SHUTDOWN in;
142 REG_R_ABORT_SHUTDOWN out;
143 prs_struct qbuf, rbuf;
148 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_ABORT_SHUTDOWN,
151 reg_io_q_abort_shutdown,
152 reg_io_r_abort_shutdown,
153 WERR_GENERAL_FAILURE );
159 /****************************************************************************
160 do a REG Unknown 0xB command. sent after a create key or create value.
161 this might be some sort of "sync" or "refresh" command, sent after
162 modification of the registry...
163 ****************************************************************************/
164 WERROR cli_reg_flush_key(struct cli_state *cli, TALLOC_CTX *mem_ctx,
169 prs_struct qbuf, rbuf;
174 init_reg_q_flush_key(&in, hnd);
176 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_FLUSH_KEY,
181 WERR_GENERAL_FAILURE );
186 /****************************************************************************
188 ****************************************************************************/
189 WERROR cli_reg_query_key(struct cli_state *cli, TALLOC_CTX *mem_ctx,
191 char *key_class, uint32 *class_len,
192 uint32 *num_subkeys, uint32 *max_subkeylen,
193 uint32 *max_classlen, uint32 *num_values,
194 uint32 *max_valnamelen, uint32 *max_valbufsize,
195 uint32 *sec_desc, NTTIME *mod_time)
199 prs_struct qbuf, rbuf;
200 uint32 saved_class_len = *class_len;
205 init_reg_q_query_key( &in, hnd, key_class );
207 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_QUERY_KEY,
212 WERR_GENERAL_FAILURE );
214 if ( W_ERROR_EQUAL( out.status, WERR_MORE_DATA ) ) {
217 *class_len = out.key_class.string->uni_max_len;
218 if ( *class_len > saved_class_len )
221 /* set a string of spaces and NULL terminate */
223 memset( key_class, (int)' ', *class_len );
224 key_class[*class_len] = '\0';
226 init_reg_q_query_key( &in, hnd, key_class );
230 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_QUERY_KEY,
235 WERR_GENERAL_FAILURE );
238 if ( !W_ERROR_IS_OK( out.status ) )
241 *class_len = out.key_class.string->uni_max_len;
242 unistr2_to_ascii(key_class, out.key_class.string, saved_class_len-1);
243 *num_subkeys = out.num_subkeys ;
244 *max_subkeylen = out.max_subkeylen ;
245 *num_values = out.num_values ;
246 *max_valnamelen = out.max_valnamelen;
247 *max_valbufsize = out.max_valbufsize;
248 *sec_desc = out.sec_desc ;
249 *mod_time = out.mod_time ;
250 /* Maybe: *max_classlen = out.reserved; */
255 /****************************************************************************
256 ****************************************************************************/
258 WERROR cli_reg_getversion(struct cli_state *cli, TALLOC_CTX *mem_ctx,
259 POLICY_HND *hnd, uint32 *version)
262 REG_R_GETVERSION out;
263 prs_struct qbuf, rbuf;
268 init_reg_q_getversion(&in, hnd);
270 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_GETVERSION,
275 WERR_GENERAL_FAILURE );
278 if ( !W_ERROR_IS_OK( out.status ) )
281 *version = out.win_version;
286 /****************************************************************************
288 ****************************************************************************/
289 WERROR cli_reg_query_value(struct cli_state *cli, TALLOC_CTX *mem_ctx,
290 POLICY_HND *hnd, const char *val_name,
291 uint32 *type, REGVAL_BUFFER *buffer)
293 REG_Q_QUERY_VALUE in;
294 REG_R_QUERY_VALUE out;
295 prs_struct qbuf, rbuf;
300 init_reg_q_query_value(&in, hnd, val_name, buffer);
302 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_QUERY_VALUE,
305 reg_io_q_query_value,
306 reg_io_r_query_value,
307 WERR_GENERAL_FAILURE );
310 if ( !W_ERROR_IS_OK( out.status ) )
314 *buffer = *out.value;
319 /****************************************************************************
320 do a REG Set Key Security
321 ****************************************************************************/
322 WERROR cli_reg_set_key_sec(struct cli_state *cli, TALLOC_CTX *mem_ctx,
323 POLICY_HND *hnd, uint32 sec_info,
324 size_t secdesc_size, SEC_DESC *sec_desc)
326 REG_Q_SET_KEY_SEC in;
327 REG_R_SET_KEY_SEC out;
328 prs_struct qbuf, rbuf;
329 SEC_DESC_BUF *sec_desc_buf;
334 /* Flatten the security descriptor */
336 if ( !(sec_desc_buf = make_sec_desc_buf(mem_ctx, secdesc_size, sec_desc)) )
337 return WERR_GENERAL_FAILURE;
339 init_reg_q_set_key_sec(&in, hnd, sec_info, sec_desc_buf);
341 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_SET_KEY_SEC,
344 reg_io_q_set_key_sec,
345 reg_io_r_set_key_sec,
346 WERR_GENERAL_FAILURE );
353 /****************************************************************************
354 do a REG Query Key Security
355 ****************************************************************************/
356 WERROR cli_reg_get_key_sec(struct cli_state *cli, TALLOC_CTX *mem_ctx,
357 POLICY_HND *hnd, uint32 sec_info,
358 uint32 *sec_buf_size, SEC_DESC_BUF *sec_buf)
360 REG_Q_GET_KEY_SEC in;
361 REG_R_GET_KEY_SEC out;
362 prs_struct qbuf, rbuf;
367 init_reg_q_get_key_sec(&in, hnd, sec_info, *sec_buf_size, sec_buf);
369 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_GET_KEY_SEC,
372 reg_io_q_get_key_sec,
373 reg_io_r_get_key_sec,
374 WERR_GENERAL_FAILURE );
377 /* this might be able to return WERR_MORE_DATA, I'm not sure */
379 if ( !W_ERROR_IS_OK( out.status ) )
383 *sec_buf_size = out.data->len;
388 /****************************************************************************
389 do a REG Delete Value
390 ****************************************************************************/
391 WERROR cli_reg_delete_val(struct cli_state *cli, TALLOC_CTX *mem_ctx,
392 POLICY_HND *hnd, char *val_name)
394 REG_Q_DELETE_VALUE in;
395 REG_R_DELETE_VALUE out;
396 prs_struct qbuf, rbuf;
401 init_reg_q_delete_val(&in, hnd, val_name);
403 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_DELETE_VALUE,
406 reg_io_q_delete_value,
407 reg_io_r_delete_value,
408 WERR_GENERAL_FAILURE );
413 /****************************************************************************
415 ****************************************************************************/
416 WERROR cli_reg_delete_key(struct cli_state *cli, TALLOC_CTX *mem_ctx,
417 POLICY_HND *hnd, char *key_name)
420 REG_R_DELETE_KEY out;
421 prs_struct qbuf, rbuf;
426 init_reg_q_delete_key(&in, hnd, key_name);
428 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_DELETE_KEY,
433 WERR_GENERAL_FAILURE );
438 /****************************************************************************
440 ****************************************************************************/
441 WERROR cli_reg_create_key(struct cli_state *cli, TALLOC_CTX *mem_ctx,
442 POLICY_HND *hnd, char *key_name, char *key_class,
443 uint32 access_desired, POLICY_HND *key)
446 REG_R_CREATE_KEY out;
447 prs_struct qbuf, rbuf;
449 SEC_DESC_BUF *sec_buf;
455 if ( !(sec = make_sec_desc(mem_ctx, 1, SEC_DESC_SELF_RELATIVE,
456 NULL, NULL, NULL, NULL, &sec_len)) )
458 return WERR_GENERAL_FAILURE;
461 if ( !(sec_buf = make_sec_desc_buf(mem_ctx, sec_len, sec)) )
462 return WERR_GENERAL_FAILURE;
464 init_reg_q_create_key(&in, hnd, key_name, key_class, access_desired, sec_buf);
466 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_CREATE_KEY,
471 WERR_GENERAL_FAILURE );
474 if ( !W_ERROR_IS_OK( out.status ) )
477 memcpy( key, &out.handle, sizeof(POLICY_HND) );
482 /****************************************************************************
484 ****************************************************************************/
485 WERROR cli_reg_enum_key(struct cli_state *cli, TALLOC_CTX *mem_ctx,
486 POLICY_HND *hnd, int key_index, fstring key_name,
487 fstring class_name, time_t *mod_time)
491 prs_struct qbuf, rbuf;
496 init_reg_q_enum_key(&in, hnd, key_index);
498 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_ENUM_KEY,
503 WERR_GENERAL_FAILURE );
505 if ( !W_ERROR_IS_OK(out.status) )
508 if ( out.keyname.string )
509 rpcstr_pull( key_name, out.keyname.string->buffer, sizeof(fstring), -1, STR_TERMINATE );
511 fstrcpy( key_name, "(Default)" );
513 if ( out.classname && out.classname->string )
514 rpcstr_pull( class_name, out.classname->string->buffer, sizeof(fstring), -1, STR_TERMINATE );
516 fstrcpy( class_name, "" );
518 *mod_time = nt_time_to_unix(out.time);
523 /****************************************************************************
524 do a REG Create Value
525 ****************************************************************************/
526 WERROR cli_reg_set_val(struct cli_state *cli, TALLOC_CTX *mem_ctx,
527 POLICY_HND *hnd, char *val_name, uint32 type,
532 prs_struct qbuf, rbuf;
537 init_reg_q_set_val(&in, hnd, val_name, type, data);
539 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_SET_VALUE,
544 WERR_GENERAL_FAILURE );
549 /****************************************************************************
551 ****************************************************************************/
552 WERROR cli_reg_enum_val(struct cli_state *cli, TALLOC_CTX *mem_ctx,
553 POLICY_HND *hnd, int idx,
554 fstring val_name, uint32 *type, REGVAL_BUFFER *value)
557 REG_R_ENUM_VALUE out;
558 prs_struct qbuf, rbuf;
563 init_reg_q_enum_val(&in, hnd, idx, 0x0100, 0x1000);
565 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_ENUM_VALUE,
570 WERR_GENERAL_FAILURE );
572 if ( W_ERROR_EQUAL(out.status, WERR_MORE_DATA) ) {
576 init_reg_q_enum_val(&in, hnd, idx, 0x0100, *out.buffer_len1);
580 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_ENUM_VALUE,
585 WERR_GENERAL_FAILURE );
588 if ( !W_ERROR_IS_OK(out.status) )
591 unistr2_to_ascii(val_name, out.name.string, sizeof(fstring)-1);
598 /****************************************************************************
599 ****************************************************************************/
601 WERROR cli_reg_open_entry(struct cli_state *cli, TALLOC_CTX *mem_ctx,
602 POLICY_HND *hnd, char *key_name,
603 uint32 access_desired, POLICY_HND *key_hnd)
606 REG_R_OPEN_ENTRY out;
607 prs_struct qbuf, rbuf;
612 init_reg_q_open_entry(&in, hnd, key_name, access_desired);
614 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_OPEN_ENTRY,
619 WERR_GENERAL_FAILURE );
621 if ( !W_ERROR_IS_OK( out.status ) )
624 memcpy( key_hnd, &out.handle, sizeof(POLICY_HND) );
629 /****************************************************************************
630 ****************************************************************************/
632 WERROR cli_reg_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
637 prs_struct qbuf, rbuf;
642 init_reg_q_close(&in, hnd);
644 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_CLOSE,
649 WERR_GENERAL_FAILURE );
654 /****************************************************************************
656 ****************************************************************************/
657 WERROR cli_reg_save_key( struct cli_state *cli, TALLOC_CTX *mem_ctx,
658 POLICY_HND *hnd, const char *filename )
662 prs_struct qbuf, rbuf;
667 init_q_reg_save_key( &in, hnd, filename );
669 CLI_DO_RPC( cli, mem_ctx, PI_WINREG, REG_SAVE_KEY,
674 WERR_GENERAL_FAILURE );
681 #################################################################
683 #################################################################
686 /*****************************************************************
687 Splits out the start of the key (HKLM or HKU) and the rest of the key.
688 *****************************************************************/
690 BOOL reg_split_hive(const char *full_keyname, uint32 *reg_type, pstring key_name)
694 if (!next_token(&full_keyname, tmp, "\\", sizeof(tmp)))
699 DEBUG(10, ("reg_split_key: hive %s\n", tmp));
701 if (strequal(tmp, "HKLM") || strequal(tmp, "HKEY_LOCAL_MACHINE"))
702 (*reg_type) = HKEY_LOCAL_MACHINE;
703 else if (strequal(tmp, "HKCR") || strequal(tmp, "HKEY_CLASSES_ROOT"))
704 (*reg_type) = HKEY_CLASSES_ROOT;
705 else if (strequal(tmp, "HKU") || strequal(tmp, "HKEY_USERS"))
706 (*reg_type) = HKEY_USERS;
707 else if (strequal(tmp, "HKPD")||strequal(tmp, "HKEY_PERFORMANCE_DATA"))
708 (*reg_type) = HKEY_PERFORMANCE_DATA;
710 DEBUG(10,("reg_split_key: unrecognised hive key %s\n", tmp));
714 if (next_token(&full_keyname, tmp, "\n\r", sizeof(tmp)))
715 pstrcpy(key_name, tmp);
719 DEBUG(10, ("reg_split_key: name %s\n", key_name));