2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 1992-2000,
6 Copyright (C) Jeremy Allison 1999 - 2005
7 Copyright (C) Simo Sorce 2001
8 Copyright (C) Jeremy Cooper 2004
9 Copyright (C) Gerald (Jerry) Carter 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "rpc_client.h"
29 /* Shutdown a server */
31 /*******************************************************************
32 internal connect to a registry hive root (open a registry policy)
33 *******************************************************************/
35 static WERROR rpccli_reg_open_hive_int(struct rpc_pipe_client *cli,
36 TALLOC_CTX *mem_ctx, uint16 op_code,
38 uint32 access_mask, POLICY_HND *hnd)
42 prs_struct qbuf, rbuf;
47 init_reg_q_open_hive(&in, access_mask);
49 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, op_code,
54 WERR_GENERAL_FAILURE );
56 if ( !W_ERROR_IS_OK( out.status ) )
59 memcpy( hnd, &out.pol, sizeof(POLICY_HND) );
64 /*******************************************************************
65 connect to a registry hive root (open a registry policy)
66 *******************************************************************/
68 WERROR rpccli_reg_connect(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
69 uint32 reg_type, uint32 access_mask,
74 ZERO_STRUCTP(reg_hnd);
78 case HKEY_CLASSES_ROOT:
79 op_code = REG_OPEN_HKCR;
80 op_name = "REG_OPEN_HKCR";
82 case HKEY_LOCAL_MACHINE:
83 op_code = REG_OPEN_HKLM;
84 op_name = "REG_OPEN_HKLM";
87 op_code = REG_OPEN_HKU;
88 op_name = "REG_OPEN_HKU";
90 case HKEY_PERFORMANCE_DATA:
91 op_code = REG_OPEN_HKPD;
92 op_name = "REG_OPEN_HKPD";
95 return WERR_INVALID_PARAM;
98 return rpccli_reg_open_hive_int(cli, mem_ctx, op_code, op_name,
99 access_mask, reg_hnd);
103 /*******************************************************************
104 *******************************************************************/
106 WERROR rpccli_reg_shutdown(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
107 const char *msg, uint32 timeout, BOOL do_reboot,
112 prs_struct qbuf, rbuf;
115 return WERR_INVALID_PARAM;
120 /* Marshall data and send request */
122 init_reg_q_shutdown(&in, msg, timeout, do_reboot, force);
124 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SHUTDOWN,
129 WERR_GENERAL_FAILURE );
134 /*******************************************************************
135 *******************************************************************/
137 WERROR rpccli_reg_abort_shutdown(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx)
139 REG_Q_ABORT_SHUTDOWN in;
140 REG_R_ABORT_SHUTDOWN out;
141 prs_struct qbuf, rbuf;
146 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ABORT_SHUTDOWN,
149 reg_io_q_abort_shutdown,
150 reg_io_r_abort_shutdown,
151 WERR_GENERAL_FAILURE );
157 /****************************************************************************
158 do a REG Unknown 0xB command. sent after a create key or create value.
159 this might be some sort of "sync" or "refresh" command, sent after
160 modification of the registry...
161 ****************************************************************************/
163 WERROR rpccli_reg_flush_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
168 prs_struct qbuf, rbuf;
173 init_reg_q_flush_key(&in, hnd);
175 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_FLUSH_KEY,
180 WERR_GENERAL_FAILURE );
185 /****************************************************************************
187 ****************************************************************************/
189 WERROR rpccli_reg_query_key(struct rpc_pipe_client *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_WERR( 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_WERR( 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 rpccli_reg_getversion(struct rpc_pipe_client *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_WERR( 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 ****************************************************************************/
290 WERROR rpccli_reg_query_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
291 POLICY_HND *hnd, const char *val_name,
292 uint32 *type, REGVAL_BUFFER *buffer)
294 REG_Q_QUERY_VALUE in;
295 REG_R_QUERY_VALUE out;
296 prs_struct qbuf, rbuf;
301 init_reg_q_query_value(&in, hnd, val_name, buffer);
303 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_QUERY_VALUE,
306 reg_io_q_query_value,
307 reg_io_r_query_value,
308 WERR_GENERAL_FAILURE );
311 if ( !W_ERROR_IS_OK( out.status ) )
315 *buffer = *out.value;
320 /****************************************************************************
321 do a REG Set Key Security
322 ****************************************************************************/
324 WERROR rpccli_reg_set_key_sec(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
325 POLICY_HND *hnd, uint32 sec_info,
326 size_t secdesc_size, SEC_DESC *sec_desc)
328 REG_Q_SET_KEY_SEC in;
329 REG_R_SET_KEY_SEC out;
330 prs_struct qbuf, rbuf;
331 SEC_DESC_BUF *sec_desc_buf;
336 /* Flatten the security descriptor */
338 if ( !(sec_desc_buf = make_sec_desc_buf(mem_ctx, secdesc_size, sec_desc)) )
339 return WERR_GENERAL_FAILURE;
341 init_reg_q_set_key_sec(&in, hnd, sec_info, sec_desc_buf);
343 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SET_KEY_SEC,
346 reg_io_q_set_key_sec,
347 reg_io_r_set_key_sec,
348 WERR_GENERAL_FAILURE );
355 /****************************************************************************
356 do a REG Query Key Security
357 ****************************************************************************/
359 WERROR rpccli_reg_get_key_sec(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
360 POLICY_HND *hnd, uint32 sec_info,
361 uint32 *sec_buf_size, SEC_DESC_BUF *sec_buf)
363 REG_Q_GET_KEY_SEC in;
364 REG_R_GET_KEY_SEC out;
365 prs_struct qbuf, rbuf;
370 init_reg_q_get_key_sec(&in, hnd, sec_info, *sec_buf_size, sec_buf);
372 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_GET_KEY_SEC,
375 reg_io_q_get_key_sec,
376 reg_io_r_get_key_sec,
377 WERR_GENERAL_FAILURE );
380 /* this might be able to return WERR_MORE_DATA, I'm not sure */
382 if ( !W_ERROR_IS_OK( out.status ) )
386 *sec_buf_size = out.data->len;
391 /****************************************************************************
392 do a REG Delete Value
393 ****************************************************************************/
395 WERROR rpccli_reg_delete_val(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
396 POLICY_HND *hnd, char *val_name)
398 REG_Q_DELETE_VALUE in;
399 REG_R_DELETE_VALUE out;
400 prs_struct qbuf, rbuf;
405 init_reg_q_delete_val(&in, hnd, val_name);
407 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_DELETE_VALUE,
410 reg_io_q_delete_value,
411 reg_io_r_delete_value,
412 WERR_GENERAL_FAILURE );
417 /****************************************************************************
419 ****************************************************************************/
421 WERROR rpccli_reg_delete_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
422 POLICY_HND *hnd, char *key_name)
425 REG_R_DELETE_KEY out;
426 prs_struct qbuf, rbuf;
431 init_reg_q_delete_key(&in, hnd, key_name);
433 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_DELETE_KEY,
438 WERR_GENERAL_FAILURE );
443 /****************************************************************************
445 ****************************************************************************/
447 WERROR rpccli_reg_create_key_ex(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
448 POLICY_HND *hnd, char *key_name, char *key_class,
449 uint32 access_desired, POLICY_HND *key)
451 REG_Q_CREATE_KEY_EX in;
452 REG_R_CREATE_KEY_EX out;
453 prs_struct qbuf, rbuf;
455 SEC_DESC_BUF *sec_buf;
461 if ( !(sec = make_sec_desc(mem_ctx, 1, SEC_DESC_SELF_RELATIVE,
462 NULL, NULL, NULL, NULL, &sec_len)) ) {
463 return WERR_GENERAL_FAILURE;
466 if ( !(sec_buf = make_sec_desc_buf(mem_ctx, sec_len, sec)) )
467 return WERR_GENERAL_FAILURE;
469 init_reg_q_create_key_ex(&in, hnd, key_name, key_class, access_desired, sec_buf);
471 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_CREATE_KEY_EX,
474 reg_io_q_create_key_ex,
475 reg_io_r_create_key_ex,
476 WERR_GENERAL_FAILURE );
479 if ( !W_ERROR_IS_OK( out.status ) )
482 memcpy( key, &out.handle, sizeof(POLICY_HND) );
487 /****************************************************************************
489 ****************************************************************************/
491 WERROR rpccli_reg_enum_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
492 POLICY_HND *hnd, int key_index, fstring key_name,
493 fstring class_name, time_t *mod_time)
497 prs_struct qbuf, rbuf;
502 init_reg_q_enum_key(&in, hnd, key_index);
504 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ENUM_KEY,
509 WERR_GENERAL_FAILURE );
511 if ( !W_ERROR_IS_OK(out.status) )
514 if ( out.keyname.string )
515 rpcstr_pull( key_name, out.keyname.string->buffer, sizeof(fstring), -1, STR_TERMINATE );
517 fstrcpy( key_name, "(Default)" );
519 if ( out.classname && out.classname->string )
520 rpcstr_pull( class_name, out.classname->string->buffer, sizeof(fstring), -1, STR_TERMINATE );
522 fstrcpy( class_name, "" );
524 *mod_time = nt_time_to_unix(*out.time);
529 /****************************************************************************
530 do a REG Create Value
531 ****************************************************************************/
533 WERROR rpccli_reg_set_val(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
534 POLICY_HND *hnd, char *val_name, uint32 type,
539 prs_struct qbuf, rbuf;
544 init_reg_q_set_val(&in, hnd, val_name, type, data);
546 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SET_VALUE,
551 WERR_GENERAL_FAILURE );
556 /****************************************************************************
558 ****************************************************************************/
560 WERROR rpccli_reg_enum_val(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
561 POLICY_HND *hnd, int idx,
562 fstring val_name, uint32 *type, REGVAL_BUFFER *value)
565 REG_R_ENUM_VALUE out;
566 prs_struct qbuf, rbuf;
571 init_reg_q_enum_val(&in, hnd, idx, 0x0100, 0x1000);
573 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ENUM_VALUE,
578 WERR_GENERAL_FAILURE );
580 if ( W_ERROR_EQUAL(out.status, WERR_MORE_DATA) ) {
584 init_reg_q_enum_val(&in, hnd, idx, 0x0100, *out.buffer_len1);
588 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ENUM_VALUE,
593 WERR_GENERAL_FAILURE );
596 if ( !W_ERROR_IS_OK(out.status) )
599 unistr2_to_ascii(val_name, out.name.string, sizeof(fstring)-1);
606 /****************************************************************************
607 ****************************************************************************/
609 WERROR rpccli_reg_open_entry(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
610 POLICY_HND *hnd, char *key_name,
611 uint32 access_desired, POLICY_HND *key_hnd)
614 REG_R_OPEN_ENTRY out;
615 prs_struct qbuf, rbuf;
620 init_reg_q_open_entry(&in, hnd, key_name, access_desired);
622 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_OPEN_ENTRY,
627 WERR_GENERAL_FAILURE );
629 if ( !W_ERROR_IS_OK( out.status ) )
632 memcpy( key_hnd, &out.handle, sizeof(POLICY_HND) );
637 /****************************************************************************
638 ****************************************************************************/
640 WERROR rpccli_reg_close(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
645 prs_struct qbuf, rbuf;
650 init_reg_q_close(&in, hnd);
652 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_CLOSE,
657 WERR_GENERAL_FAILURE );
662 /****************************************************************************
664 ****************************************************************************/
666 WERROR rpccli_reg_save_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
667 POLICY_HND *hnd, const char *filename )
671 prs_struct qbuf, rbuf;
676 init_q_reg_save_key( &in, hnd, filename );
678 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SAVE_KEY,
683 WERR_GENERAL_FAILURE );
690 #################################################################
692 #################################################################
695 /*****************************************************************
696 Splits out the start of the key (HKLM or HKU) and the rest of the key.
697 *****************************************************************/
699 BOOL reg_split_hive(const char *full_keyname, uint32 *reg_type, pstring key_name)
703 if (!next_token(&full_keyname, tmp, "\\", sizeof(tmp)))
708 DEBUG(10, ("reg_split_key: hive %s\n", tmp));
710 if (strequal(tmp, "HKLM") || strequal(tmp, "HKEY_LOCAL_MACHINE"))
711 (*reg_type) = HKEY_LOCAL_MACHINE;
712 else if (strequal(tmp, "HKCR") || strequal(tmp, "HKEY_CLASSES_ROOT"))
713 (*reg_type) = HKEY_CLASSES_ROOT;
714 else if (strequal(tmp, "HKU") || strequal(tmp, "HKEY_USERS"))
715 (*reg_type) = HKEY_USERS;
716 else if (strequal(tmp, "HKPD")||strequal(tmp, "HKEY_PERFORMANCE_DATA"))
717 (*reg_type) = HKEY_PERFORMANCE_DATA;
719 DEBUG(10,("reg_split_key: unrecognised hive key %s\n", tmp));
723 if (next_token(&full_keyname, tmp, "\n\r", sizeof(tmp)))
724 pstrcpy(key_name, tmp);
728 DEBUG(10, ("reg_split_key: name %s\n", key_name));