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 /****************************************************************************
136 ****************************************************************************/
138 WERROR rpccli_reg_query_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
140 char *key_class, uint32 *class_len,
141 uint32 *num_subkeys, uint32 *max_subkeylen,
142 uint32 *max_classlen, uint32 *num_values,
143 uint32 *max_valnamelen, uint32 *max_valbufsize,
144 uint32 *sec_desc, NTTIME *mod_time)
148 prs_struct qbuf, rbuf;
149 uint32 saved_class_len = *class_len;
154 init_reg_q_query_key( &in, hnd, key_class );
156 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_QUERY_KEY,
161 WERR_GENERAL_FAILURE );
163 if ( W_ERROR_EQUAL( out.status, WERR_MORE_DATA ) ) {
166 *class_len = out.key_class.string->uni_max_len;
167 if ( *class_len > saved_class_len )
170 /* set a string of spaces and NULL terminate */
172 memset( key_class, (int)' ', *class_len );
173 key_class[*class_len] = '\0';
175 init_reg_q_query_key( &in, hnd, key_class );
179 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_QUERY_KEY,
184 WERR_GENERAL_FAILURE );
187 if ( !W_ERROR_IS_OK( out.status ) )
190 *class_len = out.key_class.string->uni_max_len;
191 unistr2_to_ascii(key_class, out.key_class.string, saved_class_len-1);
192 *num_subkeys = out.num_subkeys ;
193 *max_subkeylen = out.max_subkeylen ;
194 *num_values = out.num_values ;
195 *max_valnamelen = out.max_valnamelen;
196 *max_valbufsize = out.max_valbufsize;
197 *sec_desc = out.sec_desc ;
198 *mod_time = out.mod_time ;
199 /* Maybe: *max_classlen = out.reserved; */
204 /****************************************************************************
205 ****************************************************************************/
207 WERROR rpccli_reg_getversion(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
208 POLICY_HND *hnd, uint32 *version)
211 REG_R_GETVERSION out;
212 prs_struct qbuf, rbuf;
217 init_reg_q_getversion(&in, hnd);
219 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_GETVERSION,
224 WERR_GENERAL_FAILURE );
227 if ( !W_ERROR_IS_OK( out.status ) )
230 *version = out.win_version;
235 /****************************************************************************
237 ****************************************************************************/
239 WERROR rpccli_reg_query_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
240 POLICY_HND *hnd, const char *val_name,
241 uint32 *type, REGVAL_BUFFER *buffer)
243 REG_Q_QUERY_VALUE in;
244 REG_R_QUERY_VALUE out;
245 prs_struct qbuf, rbuf;
250 init_reg_q_query_value(&in, hnd, val_name, buffer);
252 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_QUERY_VALUE,
255 reg_io_q_query_value,
256 reg_io_r_query_value,
257 WERR_GENERAL_FAILURE );
260 if ( !W_ERROR_IS_OK( out.status ) )
264 *buffer = *out.value;
269 /****************************************************************************
270 do a REG Set Key Security
271 ****************************************************************************/
273 WERROR rpccli_reg_set_key_sec(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
274 POLICY_HND *hnd, uint32 sec_info,
275 size_t secdesc_size, SEC_DESC *sec_desc)
277 REG_Q_SET_KEY_SEC in;
278 REG_R_SET_KEY_SEC out;
279 prs_struct qbuf, rbuf;
280 SEC_DESC_BUF *sec_desc_buf;
285 /* Flatten the security descriptor */
287 if ( !(sec_desc_buf = make_sec_desc_buf(mem_ctx, secdesc_size, sec_desc)) )
288 return WERR_GENERAL_FAILURE;
290 init_reg_q_set_key_sec(&in, hnd, sec_info, sec_desc_buf);
292 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SET_KEY_SEC,
295 reg_io_q_set_key_sec,
296 reg_io_r_set_key_sec,
297 WERR_GENERAL_FAILURE );
304 /****************************************************************************
305 do a REG Query Key Security
306 ****************************************************************************/
308 WERROR rpccli_reg_get_key_sec(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
309 POLICY_HND *hnd, uint32 sec_info,
310 uint32 *sec_buf_size, SEC_DESC_BUF *sec_buf)
312 REG_Q_GET_KEY_SEC in;
313 REG_R_GET_KEY_SEC out;
314 prs_struct qbuf, rbuf;
319 init_reg_q_get_key_sec(&in, hnd, sec_info, *sec_buf_size, sec_buf);
321 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_GET_KEY_SEC,
324 reg_io_q_get_key_sec,
325 reg_io_r_get_key_sec,
326 WERR_GENERAL_FAILURE );
329 /* this might be able to return WERR_MORE_DATA, I'm not sure */
331 if ( !W_ERROR_IS_OK( out.status ) )
335 *sec_buf_size = out.data->len;
340 /****************************************************************************
341 do a REG Delete Value
342 ****************************************************************************/
344 WERROR rpccli_reg_delete_val(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
345 POLICY_HND *hnd, char *val_name)
347 REG_Q_DELETE_VALUE in;
348 REG_R_DELETE_VALUE out;
349 prs_struct qbuf, rbuf;
354 init_reg_q_delete_val(&in, hnd, val_name);
356 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_DELETE_VALUE,
359 reg_io_q_delete_value,
360 reg_io_r_delete_value,
361 WERR_GENERAL_FAILURE );
366 /****************************************************************************
368 ****************************************************************************/
370 WERROR rpccli_reg_delete_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
371 POLICY_HND *hnd, char *key_name)
374 REG_R_DELETE_KEY out;
375 prs_struct qbuf, rbuf;
380 init_reg_q_delete_key(&in, hnd, key_name);
382 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_DELETE_KEY,
387 WERR_GENERAL_FAILURE );
392 /****************************************************************************
394 ****************************************************************************/
396 WERROR rpccli_reg_create_key_ex(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
397 POLICY_HND *hnd, char *key_name, char *key_class,
398 uint32 access_desired, POLICY_HND *key)
400 REG_Q_CREATE_KEY_EX in;
401 REG_R_CREATE_KEY_EX out;
402 prs_struct qbuf, rbuf;
404 SEC_DESC_BUF *sec_buf;
410 if ( !(sec = make_sec_desc(mem_ctx, 1, SEC_DESC_SELF_RELATIVE,
411 NULL, NULL, NULL, NULL, &sec_len)) ) {
412 return WERR_GENERAL_FAILURE;
415 if ( !(sec_buf = make_sec_desc_buf(mem_ctx, sec_len, sec)) )
416 return WERR_GENERAL_FAILURE;
418 init_reg_q_create_key_ex(&in, hnd, key_name, key_class, access_desired, sec_buf);
420 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_CREATE_KEY_EX,
423 reg_io_q_create_key_ex,
424 reg_io_r_create_key_ex,
425 WERR_GENERAL_FAILURE );
428 if ( !W_ERROR_IS_OK( out.status ) )
431 memcpy( key, &out.handle, sizeof(POLICY_HND) );
436 /****************************************************************************
438 ****************************************************************************/
440 WERROR rpccli_reg_enum_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
441 POLICY_HND *hnd, int key_index, fstring key_name,
442 fstring class_name, time_t *mod_time)
446 prs_struct qbuf, rbuf;
451 init_reg_q_enum_key(&in, hnd, key_index);
453 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ENUM_KEY,
458 WERR_GENERAL_FAILURE );
460 if ( !W_ERROR_IS_OK(out.status) )
463 if ( out.keyname.string )
464 rpcstr_pull( key_name, out.keyname.string->buffer, sizeof(fstring), -1, STR_TERMINATE );
466 fstrcpy( key_name, "(Default)" );
468 if ( out.classname && out.classname->string )
469 rpcstr_pull( class_name, out.classname->string->buffer, sizeof(fstring), -1, STR_TERMINATE );
471 fstrcpy( class_name, "" );
473 *mod_time = nt_time_to_unix(*out.time);
478 /****************************************************************************
479 do a REG Create Value
480 ****************************************************************************/
482 WERROR rpccli_reg_set_val(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
483 POLICY_HND *hnd, char *val_name, uint32 type,
488 prs_struct qbuf, rbuf;
493 init_reg_q_set_val(&in, hnd, val_name, type, data);
495 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SET_VALUE,
500 WERR_GENERAL_FAILURE );
505 /****************************************************************************
507 ****************************************************************************/
509 WERROR rpccli_reg_enum_val(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
510 POLICY_HND *hnd, int idx,
511 fstring val_name, uint32 *type, REGVAL_BUFFER *value)
514 REG_R_ENUM_VALUE out;
515 prs_struct qbuf, rbuf;
520 init_reg_q_enum_val(&in, hnd, idx, 0x0100, 0x1000);
522 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ENUM_VALUE,
527 WERR_GENERAL_FAILURE );
529 if ( W_ERROR_EQUAL(out.status, WERR_MORE_DATA) ) {
533 init_reg_q_enum_val(&in, hnd, idx, 0x0100, *out.buffer_len1);
537 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_ENUM_VALUE,
542 WERR_GENERAL_FAILURE );
545 if ( !W_ERROR_IS_OK(out.status) )
548 unistr2_to_ascii(val_name, out.name.string, sizeof(fstring)-1);
555 /****************************************************************************
556 ****************************************************************************/
558 WERROR rpccli_reg_open_entry(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
559 POLICY_HND *hnd, char *key_name,
560 uint32 access_desired, POLICY_HND *key_hnd)
563 REG_R_OPEN_ENTRY out;
564 prs_struct qbuf, rbuf;
569 init_reg_q_open_entry(&in, hnd, key_name, access_desired);
571 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_OPEN_ENTRY,
576 WERR_GENERAL_FAILURE );
578 if ( !W_ERROR_IS_OK( out.status ) )
581 memcpy( key_hnd, &out.handle, sizeof(POLICY_HND) );
586 /****************************************************************************
587 ****************************************************************************/
589 WERROR rpccli_reg_close(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
594 prs_struct qbuf, rbuf;
599 init_reg_q_close(&in, hnd);
601 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_CLOSE,
606 WERR_GENERAL_FAILURE );
611 /****************************************************************************
613 ****************************************************************************/
615 WERROR rpccli_reg_save_key(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
616 POLICY_HND *hnd, const char *filename )
620 prs_struct qbuf, rbuf;
625 init_q_reg_save_key( &in, hnd, filename );
627 CLI_DO_RPC_WERR( cli, mem_ctx, PI_WINREG, REG_SAVE_KEY,
632 WERR_GENERAL_FAILURE );
639 #################################################################
641 #################################################################
644 /*****************************************************************
645 Splits out the start of the key (HKLM or HKU) and the rest of the key.
646 *****************************************************************/
648 BOOL reg_split_hive(const char *full_keyname, uint32 *reg_type, pstring key_name)
652 if (!next_token(&full_keyname, tmp, "\\", sizeof(tmp)))
657 DEBUG(10, ("reg_split_key: hive %s\n", tmp));
659 if (strequal(tmp, "HKLM") || strequal(tmp, "HKEY_LOCAL_MACHINE"))
660 (*reg_type) = HKEY_LOCAL_MACHINE;
661 else if (strequal(tmp, "HKCR") || strequal(tmp, "HKEY_CLASSES_ROOT"))
662 (*reg_type) = HKEY_CLASSES_ROOT;
663 else if (strequal(tmp, "HKU") || strequal(tmp, "HKEY_USERS"))
664 (*reg_type) = HKEY_USERS;
665 else if (strequal(tmp, "HKPD")||strequal(tmp, "HKEY_PERFORMANCE_DATA"))
666 (*reg_type) = HKEY_PERFORMANCE_DATA;
668 DEBUG(10,("reg_split_key: unrecognised hive key %s\n", tmp));
672 if (next_token(&full_keyname, tmp, "\n\r", sizeof(tmp)))
673 pstrcpy(key_name, tmp);
677 DEBUG(10, ("reg_split_key: name %s\n", key_name));