2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997.
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997.
6 * Copyright (C) Paul Ashton 1997.
7 * Copyright (C) Jeremy Allison 2001.
8 * Copyright (C) Gerald Carter 2002.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* Implementation of registry functions. */
30 #define DBGC_CLASS DBGC_RPC_SRV
32 #define OUR_HANDLE(hnd) (((hnd)==NULL)?"NULL":(IVAL((hnd)->data5,4)==(uint32)sys_getpid()?"OURS":"OTHER")), \
33 ((unsigned int)IVAL((hnd)->data5,4)),((unsigned int)sys_getpid())
36 static REGISTRY_KEY
*regkeys_list
;
39 /******************************************************************
40 free() function for REGISTRY_KEY
41 *****************************************************************/
43 static void free_regkey_info(void *ptr
)
45 REGISTRY_KEY
*info
= (REGISTRY_KEY
*)ptr
;
47 DLIST_REMOVE(regkeys_list
, info
);
52 /******************************************************************
53 Find a registry key handle and return a REGISTRY_KEY
54 *****************************************************************/
56 static REGISTRY_KEY
*find_regkey_index_by_hnd(pipes_struct
*p
, POLICY_HND
*hnd
)
58 REGISTRY_KEY
*regkey
= NULL
;
60 if(!find_policy_by_hnd(p
,hnd
,(void **)®key
)) {
61 DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: "));
69 /*******************************************************************
70 Function for open a new registry handle and creating a handle
71 Note that P should be valid & hnd should already have space
73 When we open a key, we store the full path to the key as
74 HK[LM|U]\<key>\<key>\...
75 *******************************************************************/
77 static NTSTATUS
open_registry_key(pipes_struct
*p
, POLICY_HND
*hnd
, REGISTRY_KEY
*parent
,
78 char *subkeyname
, uint32 access_granted
)
80 REGISTRY_KEY
*regkey
= NULL
;
81 pstring parent_keyname
;
82 NTSTATUS result
= NT_STATUS_OK
;
83 REGSUBKEY_CTR subkeys
;
86 pstrcpy( parent_keyname
, parent
->name
);
87 pstrcat( parent_keyname
, "\\" );
90 *parent_keyname
= '\0';
92 DEBUG(7,("open_registry_key: name = [%s][%s]\n", parent_keyname
, subkeyname
));
94 /* All registry keys **must** have a name of non-zero length */
96 if (!subkeyname
|| !*subkeyname
)
97 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
99 if ((regkey
=(REGISTRY_KEY
*)malloc(sizeof(REGISTRY_KEY
))) == NULL
)
100 return NT_STATUS_NO_MEMORY
;
102 ZERO_STRUCTP( regkey
);
106 pstrcpy( regkey
->name
, parent_keyname
);
107 pstrcat( regkey
->name
, subkeyname
);
109 /* Look up the table of registry I/O operations */
111 if ( !(regkey
->hook
= reghook_cache_find( regkey
->name
)) ) {
112 DEBUG(0,("open_registry_key: Failed to assigned a REGISTRY_HOOK to [%s]\n",
114 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
117 /* check if the path really exists; failed is indicated by -1 */
118 /* if the subkey count failed, bail out */
120 ZERO_STRUCTP( &subkeys
);
122 regsubkey_ctr_init( &subkeys
);
124 if ( fetch_reg_keys( regkey
, &subkeys
) == -1 ) {
126 /* don't really know what to return here */
128 result
= NT_STATUS_ACCESS_DENIED
;
132 * This would previously return NT_STATUS_TOO_MANY_SECRETS
133 * that doesn't sound quite right to me --jerry
136 if ( !create_policy_hnd( p
, hnd
, free_regkey_info
, regkey
) )
137 result
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
142 regsubkey_ctr_destroy( &subkeys
);
144 if ( ! NT_STATUS_IS_OK(result
) )
147 DLIST_ADD( regkeys_list
, regkey
);
150 DEBUG(7,("open_registry_key: exit\n"));
155 /*******************************************************************
156 Function for open a new registry handle and creating a handle
157 Note that P should be valid & hnd should already have space
158 *******************************************************************/
160 static BOOL
close_registry_key(pipes_struct
*p
, POLICY_HND
*hnd
)
162 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd(p
, hnd
);
165 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd
)));
169 close_policy_hnd(p
, hnd
);
174 /********************************************************************
175 retrieve information about the subkeys
176 *******************************************************************/
178 static BOOL
get_subkey_information( REGISTRY_KEY
*key
, uint32
*maxnum
, uint32
*maxlen
)
182 REGSUBKEY_CTR subkeys
;
188 ZERO_STRUCTP( &subkeys
);
190 regsubkey_ctr_init( &subkeys
);
192 if ( fetch_reg_keys( key
, &subkeys
) == -1 )
195 /* find the longest string */
198 num_subkeys
= regsubkey_ctr_numkeys( &subkeys
);
200 for ( i
=0; i
<num_subkeys
; i
++ ) {
201 len
= strlen( regsubkey_ctr_specific_key(&subkeys
, i
) );
202 max_len
= MAX(max_len
, len
);
205 *maxnum
= num_subkeys
;
208 regsubkey_ctr_destroy( &subkeys
);
213 /********************************************************************
214 retrieve information about the values. We don't store values
215 here. The registry tdb is intended to be a frontend to oether
216 Samba tdb's (such as ntdrivers.tdb).
217 *******************************************************************/
219 static BOOL
get_value_information( REGISTRY_KEY
*key
, uint32
*maxnum
,
220 uint32
*maxlen
, uint32
*maxsize
)
224 uint32 sizemax
, lenmax
;
230 ZERO_STRUCTP( &val
);
232 regval_ctr_init( &values
);
234 if ( fetch_reg_values( key
, &values
) == -1 )
237 lenmax
= sizemax
= 0;
238 num_values
= regval_ctr_numvals( &values
);
240 for ( i
=0; i
<num_values
&& val
; i
++ ) {
241 val
= regval_ctr_specific_value( &values
, i
);
242 lenmax
= MAX(lenmax
, strlen(val
[i
].valuename
)+1 );
243 sizemax
= MAX(sizemax
, val
[i
].size
);
246 *maxnum
= num_values
;
250 regval_ctr_destroy( &values
);
256 /********************************************************************
258 ********************************************************************/
260 NTSTATUS
_reg_close(pipes_struct
*p
, REG_Q_CLOSE
*q_u
, REG_R_CLOSE
*r_u
)
262 /* set up the REG unknown_1 response */
263 ZERO_STRUCT(r_u
->pol
);
265 /* close the policy handle */
266 if (!close_registry_key(p
, &q_u
->pol
))
267 return NT_STATUS_OBJECT_NAME_INVALID
;
272 /*******************************************************************
274 ********************************************************************/
276 NTSTATUS
_reg_open_hklm(pipes_struct
*p
, REG_Q_OPEN_HKLM
*q_u
, REG_R_OPEN_HKLM
*r_u
)
278 return open_registry_key( p
, &r_u
->pol
, NULL
, KEY_HKLM
, 0x0 );
281 /*******************************************************************
283 ********************************************************************/
285 NTSTATUS
_reg_open_hku(pipes_struct
*p
, REG_Q_OPEN_HKU
*q_u
, REG_R_OPEN_HKU
*r_u
)
287 return open_registry_key( p
, &r_u
->pol
, NULL
, KEY_HKU
, 0x0 );
290 /*******************************************************************
292 ********************************************************************/
294 NTSTATUS
_reg_open_entry(pipes_struct
*p
, REG_Q_OPEN_ENTRY
*q_u
, REG_R_OPEN_ENTRY
*r_u
)
298 REGISTRY_KEY
*key
= find_regkey_index_by_hnd(p
, &q_u
->pol
);
301 DEBUG(5,("reg_open_entry: Enter\n"));
304 return NT_STATUS_INVALID_HANDLE
;
306 rpcstr_pull(name
,q_u
->uni_name
.buffer
,sizeof(name
),q_u
->uni_name
.uni_str_len
*2,0);
308 DEBUG(5,("reg_open_entry: Enter\n"));
310 result
= open_registry_key( p
, &pol
, key
, name
, 0x0 );
312 init_reg_r_open_entry( r_u
, &pol
, result
);
314 DEBUG(5,("reg_open_entry: Exit\n"));
319 /*******************************************************************
321 ********************************************************************/
323 NTSTATUS
_reg_info(pipes_struct
*p
, REG_Q_INFO
*q_u
, REG_R_INFO
*r_u
)
325 NTSTATUS status
= NT_STATUS_OK
;
327 uint32 type
= 0x1; /* key type: REG_SZ */
328 UNISTR2
*uni_key
= NULL
;
331 REGISTRY_KEY
*key
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
333 DEBUG(5,("_reg_info: Enter\n"));
336 return NT_STATUS_INVALID_HANDLE
;
338 DEBUG(7,("_reg_info: policy key name = [%s]\n", key
->name
));
340 rpcstr_pull(name
, q_u
->uni_type
.buffer
, sizeof(name
), q_u
->uni_type
.uni_str_len
*2, 0);
342 DEBUG(5,("reg_info: checking subkey: %s\n", name
));
344 uni_key
= (UNISTR2
*)talloc_zero(p
->mem_ctx
, sizeof(UNISTR2
));
345 buf
= (BUFFER2
*)talloc_zero(p
->mem_ctx
, sizeof(BUFFER2
));
347 if (!uni_key
|| !buf
)
348 return NT_STATUS_NO_MEMORY
;
350 if ( strequal(name
, "RefusePasswordChange") ) {
352 status
= NT_STATUS_NO_SUCH_FILE
;
353 init_unistr2(uni_key
, "", 0);
354 init_buffer2(buf
, (uint8
*) uni_key
->buffer
, uni_key
->uni_str_len
*2);
361 switch (lp_server_role()) {
362 case ROLE_DOMAIN_PDC
:
363 case ROLE_DOMAIN_BDC
:
366 case ROLE_STANDALONE
:
369 case ROLE_DOMAIN_MEMBER
:
374 /* This makes the server look like a member server to clients */
375 /* which tells clients that we have our own local user and */
376 /* group databases and helps with ACL support. */
378 init_unistr2(uni_key
, value
, strlen(value
)+1);
379 init_buffer2(buf
, (uint8
*)uni_key
->buffer
, uni_key
->uni_str_len
*2);
382 init_reg_r_info(q_u
->ptr_buf
, r_u
, buf
, type
, status
);
384 DEBUG(5,("_reg_info: Exit\n"));
390 /*****************************************************************************
391 Implementation of REG_QUERY_KEY
392 ****************************************************************************/
394 NTSTATUS
_reg_query_key(pipes_struct
*p
, REG_Q_QUERY_KEY
*q_u
, REG_R_QUERY_KEY
*r_u
)
396 NTSTATUS status
= NT_STATUS_OK
;
397 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
399 DEBUG(5,("_reg_query_key: Enter\n"));
402 return NT_STATUS_INVALID_HANDLE
;
404 if ( !get_subkey_information( regkey
, &r_u
->num_subkeys
, &r_u
->max_subkeylen
) )
405 return NT_STATUS_ACCESS_DENIED
;
407 if ( !get_value_information( regkey
, &r_u
->num_values
, &r_u
->max_valnamelen
, &r_u
->max_valbufsize
) )
408 return NT_STATUS_ACCESS_DENIED
;
411 r_u
->sec_desc
= 0x00000078; /* size for key's sec_desc */
413 /* Win9x set this to 0x0 since it does not keep timestamps.
414 Doing the same here for simplicity --jerry */
416 ZERO_STRUCT(r_u
->mod_time
);
418 DEBUG(5,("_reg_query_key: Exit\n"));
424 /*****************************************************************************
425 Implementation of REG_UNKNOWN_1A
426 ****************************************************************************/
428 NTSTATUS
_reg_unknown_1a(pipes_struct
*p
, REG_Q_UNKNOWN_1A
*q_u
, REG_R_UNKNOWN_1A
*r_u
)
430 NTSTATUS status
= NT_STATUS_OK
;
431 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
433 DEBUG(5,("_reg_unknown_1a: Enter\n"));
436 return NT_STATUS_INVALID_HANDLE
;
438 r_u
->unknown
= 0x00000005; /* seems to be consistent...no idea what it means */
440 DEBUG(5,("_reg_unknown_1a: Exit\n"));
446 /*****************************************************************************
447 Implementation of REG_ENUM_KEY
448 ****************************************************************************/
450 NTSTATUS
_reg_enum_key(pipes_struct
*p
, REG_Q_ENUM_KEY
*q_u
, REG_R_ENUM_KEY
*r_u
)
452 NTSTATUS status
= NT_STATUS_OK
;
453 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
457 DEBUG(5,("_reg_enum_key: Enter\n"));
460 return NT_STATUS_INVALID_HANDLE
;
462 DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey
->name
));
464 if ( !fetch_reg_keys_specific( regkey
, &subkey
, q_u
->key_index
) )
466 status
= NT_STATUS_NO_MORE_ENTRIES
;
470 DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey
));
472 /* subkey has the string name now */
474 init_reg_r_enum_key( r_u
, subkey
, q_u
->unknown_1
, q_u
->unknown_2
);
476 DEBUG(5,("_reg_enum_key: Exit\n"));
484 /*******************************************************************
486 ********************************************************************/
488 #define SHUTDOWN_R_STRING "-r"
489 #define SHUTDOWN_F_STRING "-f"
492 NTSTATUS
_reg_shutdown(pipes_struct
*p
, REG_Q_SHUTDOWN
*q_u
, REG_R_SHUTDOWN
*r_u
)
494 NTSTATUS status
= NT_STATUS_OK
;
495 pstring shutdown_script
;
496 UNISTR2 unimsg
= q_u
->uni_msg
;
504 rpcstr_pull (message
, unimsg
.buffer
, sizeof(message
), unimsg
.uni_str_len
*2,0);
506 alpha_strcpy (chkmsg
, message
, NULL
, sizeof(message
));
508 snprintf(timeout
, sizeof(timeout
), "%d", q_u
->timeout
);
510 snprintf(r
, sizeof(r
), (q_u
->flags
& REG_REBOOT_ON_SHUTDOWN
)?SHUTDOWN_R_STRING
:"");
512 snprintf(f
, sizeof(f
), (q_u
->flags
& REG_FORCE_SHUTDOWN
)?SHUTDOWN_F_STRING
:"");
514 pstrcpy(shutdown_script
, lp_shutdown_script());
516 if(*shutdown_script
) {
518 all_string_sub(shutdown_script
, "%m", chkmsg
, sizeof(shutdown_script
));
519 all_string_sub(shutdown_script
, "%t", timeout
, sizeof(shutdown_script
));
520 all_string_sub(shutdown_script
, "%r", r
, sizeof(shutdown_script
));
521 all_string_sub(shutdown_script
, "%f", f
, sizeof(shutdown_script
));
522 shutdown_ret
= smbrun(shutdown_script
,NULL
);
523 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script
,shutdown_ret
));
529 /*******************************************************************
531 ********************************************************************/
533 NTSTATUS
_reg_abort_shutdown(pipes_struct
*p
, REG_Q_ABORT_SHUTDOWN
*q_u
, REG_R_ABORT_SHUTDOWN
*r_u
)
535 NTSTATUS status
= NT_STATUS_OK
;
536 pstring abort_shutdown_script
;
538 pstrcpy(abort_shutdown_script
, lp_abort_shutdown_script());
540 if(*abort_shutdown_script
) {
541 int abort_shutdown_ret
;
542 abort_shutdown_ret
= smbrun(abort_shutdown_script
,NULL
);
543 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script
,abort_shutdown_ret
));