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_reg_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
;
87 pstrcpy( parent_keyname
, parent
->name
);
88 pstrcat( parent_keyname
, "\\" );
91 *parent_keyname
= '\0';
94 DEBUG(7,("open_registry_key: name = [%s][%s]\n", parent_keyname
, subkeyname
));
96 /* All registry keys **must** have a name of non-zero length */
98 if (!subkeyname
|| !*subkeyname
)
99 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
101 if ((regkey
=(REGISTRY_KEY
*)malloc(sizeof(REGISTRY_KEY
))) == NULL
)
102 return NT_STATUS_NO_MEMORY
;
104 ZERO_STRUCTP( regkey
);
106 DLIST_ADD( regkeys_list
, regkey
);
110 pstrcpy( regkey
->name
, parent_keyname
);
111 pstrcat( regkey
->name
, subkeyname
);
113 /* try to use en existing hook. Otherwise, try to lookup our own */
115 if ( parent
&& parent
->hook
)
116 regkey
->hook
= parent
->hook
;
118 regkey
->hook
= reghook_cache_find( regkey
->name
);
120 if ( regkey
->hook
) {
121 DEBUG(10,("open_registry_key: Assigned REGISTRY_HOOK to [%s]\n",
125 /* check if the path really exists...num_subkeys should be >= 0 */
127 num_subkeys
= fetch_reg_keys( regkey
, &subkeys
);
129 /* if the subkey count failed, bail out */
131 if ( num_subkeys
== -1 ) {
134 /* don't really know what to return here */
136 result
= NT_STATUS_ACCESS_DENIED
;
140 * This would previously return NT_STATUS_TOO_MANY_SECRETS
141 * that doesn't sound quite right to me --jerry
144 if ( !create_policy_hnd( p
, hnd
, free_reg_info
, regkey
) )
145 result
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
148 DEBUG(7,("open_registry_key: exit\n"));
150 SAFE_FREE( subkeys
);
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 char *subkeys
= NULL
;
189 /* first use any registry hook available.
190 Fall back to tdb if non available */
192 num_subkeys
= fetch_reg_keys( key
, &subkeys
);
194 if ( num_subkeys
== -1 )
197 /* find the longest string */
201 for ( i
=0; i
<num_subkeys
; i
++ ) {
203 max_len
= MAX(max_len
, len
);
207 *maxnum
= num_subkeys
;
215 /********************************************************************
216 retrieve information about the values. We don't store values
217 here. The registry tdb is intended to be a frontend to oether
218 Samba tdb's (such as ntdrivers.tdb).
219 *******************************************************************/
221 static BOOL
get_value_information( REGISTRY_KEY
*key
, uint32
*maxnum
,
222 uint32
*maxlen
, uint32
*maxsize
)
224 REGISTRY_VALUE
*val
= NULL
;
225 uint32 i
, sizemax
, lenmax
;
231 num_values
= fetch_reg_values( key
, &val
);
233 if ( num_values
== -1 )
237 lenmax
= sizemax
= 0;
239 for ( i
=0; i
<num_values
; i
++ ) {
240 lenmax
= MAX(lenmax
, strlen(val
[i
].valuename
)+1 );
241 sizemax
= MAX(sizemax
, val
[i
].size
);
244 *maxnum
= num_values
;
254 /********************************************************************
256 ********************************************************************/
258 NTSTATUS
_reg_close(pipes_struct
*p
, REG_Q_CLOSE
*q_u
, REG_R_CLOSE
*r_u
)
260 /* set up the REG unknown_1 response */
261 ZERO_STRUCT(r_u
->pol
);
263 /* close the policy handle */
264 if (!close_registry_key(p
, &q_u
->pol
))
265 return NT_STATUS_OBJECT_NAME_INVALID
;
270 /*******************************************************************
272 ********************************************************************/
274 NTSTATUS
_reg_open_hklm(pipes_struct
*p
, REG_Q_OPEN_HKLM
*q_u
, REG_R_OPEN_HKLM
*r_u
)
276 return open_registry_key( p
, &r_u
->pol
, NULL
, KEY_HKLM
, 0x0 );
279 /*******************************************************************
281 ********************************************************************/
283 NTSTATUS
_reg_open_hku(pipes_struct
*p
, REG_Q_OPEN_HKU
*q_u
, REG_R_OPEN_HKU
*r_u
)
285 return open_registry_key( p
, &r_u
->pol
, NULL
, KEY_HKU
, 0x0 );
288 /*******************************************************************
290 ********************************************************************/
292 NTSTATUS
_reg_open_entry(pipes_struct
*p
, REG_Q_OPEN_ENTRY
*q_u
, REG_R_OPEN_ENTRY
*r_u
)
296 REGISTRY_KEY
*key
= find_regkey_index_by_hnd(p
, &q_u
->pol
);
299 DEBUG(5,("reg_open_entry: Enter\n"));
302 return NT_STATUS_INVALID_HANDLE
;
304 rpcstr_pull(name
,q_u
->uni_name
.buffer
,sizeof(name
),q_u
->uni_name
.uni_str_len
*2,0);
306 DEBUG(5,("reg_open_entry: Enter\n"));
308 result
= open_registry_key( p
, &pol
, key
, name
, 0x0 );
310 init_reg_r_open_entry( r_u
, &pol
, result
);
312 DEBUG(5,("reg_open_entry: Exit\n"));
317 /*******************************************************************
319 ********************************************************************/
321 NTSTATUS
_reg_info(pipes_struct
*p
, REG_Q_INFO
*q_u
, REG_R_INFO
*r_u
)
323 NTSTATUS status
= NT_STATUS_OK
;
325 uint32 type
= 0x1; /* key type: REG_SZ */
326 UNISTR2
*uni_key
= NULL
;
329 REGISTRY_KEY
*key
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
331 DEBUG(5,("_reg_info: Enter\n"));
334 return NT_STATUS_INVALID_HANDLE
;
336 DEBUG(7,("_reg_info: policy key name = [%s]\n", key
->name
));
338 rpcstr_pull(name
, q_u
->uni_type
.buffer
, sizeof(name
), q_u
->uni_type
.uni_str_len
*2, 0);
340 DEBUG(5,("reg_info: checking subkey: %s\n", name
));
342 uni_key
= (UNISTR2
*)talloc_zero(p
->mem_ctx
, sizeof(UNISTR2
));
343 buf
= (BUFFER2
*)talloc_zero(p
->mem_ctx
, sizeof(BUFFER2
));
345 if (!uni_key
|| !buf
)
346 return NT_STATUS_NO_MEMORY
;
348 if ( strequal(name
, "RefusePasswordChange") ) {
350 status
= NT_STATUS_NO_SUCH_FILE
;
351 init_unistr2(uni_key
, "", 0);
352 init_buffer2(buf
, (uint8
*) uni_key
->buffer
, uni_key
->uni_str_len
*2);
359 switch (lp_server_role()) {
360 case ROLE_DOMAIN_PDC
:
361 case ROLE_DOMAIN_BDC
:
364 case ROLE_STANDALONE
:
367 case ROLE_DOMAIN_MEMBER
:
372 /* This makes the server look like a member server to clients */
373 /* which tells clients that we have our own local user and */
374 /* group databases and helps with ACL support. */
376 init_unistr2(uni_key
, value
, strlen(value
)+1);
377 init_buffer2(buf
, (uint8
*)uni_key
->buffer
, uni_key
->uni_str_len
*2);
380 init_reg_r_info(q_u
->ptr_buf
, r_u
, buf
, type
, status
);
382 DEBUG(5,("reg_open_entry: Exit\n"));
388 /*****************************************************************************
389 Implementation of REG_QUERY_KEY
390 ****************************************************************************/
392 NTSTATUS
_reg_query_key(pipes_struct
*p
, REG_Q_QUERY_KEY
*q_u
, REG_R_QUERY_KEY
*r_u
)
394 NTSTATUS status
= NT_STATUS_OK
;
395 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
397 DEBUG(5,("_reg_query_key: Enter\n"));
400 return NT_STATUS_INVALID_HANDLE
;
402 if ( !get_subkey_information( regkey
, &r_u
->num_subkeys
, &r_u
->max_subkeylen
) )
403 return NT_STATUS_ACCESS_DENIED
;
405 if ( !get_value_information( regkey
, &r_u
->num_values
, &r_u
->max_valnamelen
, &r_u
->max_valbufsize
) )
406 return NT_STATUS_ACCESS_DENIED
;
409 r_u
->sec_desc
= 0x00000078; /* size for key's sec_desc */
411 /* Win9x set this to 0x0 since it does not keep timestamps.
412 Doing the same here for simplicity --jerry */
414 ZERO_STRUCT(r_u
->mod_time
);
416 DEBUG(5,("_reg_query_key: Exit\n"));
422 /*****************************************************************************
423 Implementation of REG_UNKNOWN_1A
424 ****************************************************************************/
426 NTSTATUS
_reg_unknown_1a(pipes_struct
*p
, REG_Q_UNKNOWN_1A
*q_u
, REG_R_UNKNOWN_1A
*r_u
)
428 NTSTATUS status
= NT_STATUS_OK
;
429 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
431 DEBUG(5,("_reg_unknown_1a: Enter\n"));
434 return NT_STATUS_INVALID_HANDLE
;
436 r_u
->unknown
= 0x00000005; /* seems to be consistent...no idea what it means */
438 DEBUG(5,("_reg_unknown_1a: Exit\n"));
444 /*****************************************************************************
445 Implementation of REG_ENUM_KEY
446 ****************************************************************************/
448 NTSTATUS
_reg_enum_key(pipes_struct
*p
, REG_Q_ENUM_KEY
*q_u
, REG_R_ENUM_KEY
*r_u
)
450 NTSTATUS status
= NT_STATUS_OK
;
451 REGISTRY_KEY
*regkey
= find_regkey_index_by_hnd( p
, &q_u
->pol
);
455 DEBUG(5,("_reg_enum_key: Enter\n"));
458 return NT_STATUS_INVALID_HANDLE
;
460 DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey
->name
));
462 if ( !fetch_reg_keys_specific( regkey
, &subkey
, q_u
->key_index
) )
464 status
= NT_STATUS_NO_MORE_ENTRIES
;
468 DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey
));
470 /* subkey has the string name now */
472 init_reg_r_enum_key( r_u
, subkey
, q_u
->unknown_1
, q_u
->unknown_2
);
474 DEBUG(5,("_reg_enum_key: Exit\n"));
482 /*******************************************************************
484 ********************************************************************/
486 #define SHUTDOWN_R_STRING "-r"
487 #define SHUTDOWN_F_STRING "-f"
490 NTSTATUS
_reg_shutdown(pipes_struct
*p
, REG_Q_SHUTDOWN
*q_u
, REG_R_SHUTDOWN
*r_u
)
492 NTSTATUS status
= NT_STATUS_OK
;
493 pstring shutdown_script
;
494 UNISTR2 unimsg
= q_u
->uni_msg
;
502 rpcstr_pull (message
, unimsg
.buffer
, sizeof(message
), unimsg
.uni_str_len
*2,0);
504 alpha_strcpy (chkmsg
, message
, NULL
, sizeof(message
));
506 snprintf(timeout
, sizeof(timeout
), "%d", q_u
->timeout
);
508 snprintf(r
, sizeof(r
), (q_u
->flags
& REG_REBOOT_ON_SHUTDOWN
)?SHUTDOWN_R_STRING
:"");
510 snprintf(f
, sizeof(f
), (q_u
->flags
& REG_FORCE_SHUTDOWN
)?SHUTDOWN_F_STRING
:"");
512 pstrcpy(shutdown_script
, lp_shutdown_script());
514 if(*shutdown_script
) {
516 all_string_sub(shutdown_script
, "%m", chkmsg
, sizeof(shutdown_script
));
517 all_string_sub(shutdown_script
, "%t", timeout
, sizeof(shutdown_script
));
518 all_string_sub(shutdown_script
, "%r", r
, sizeof(shutdown_script
));
519 all_string_sub(shutdown_script
, "%f", f
, sizeof(shutdown_script
));
520 shutdown_ret
= smbrun(shutdown_script
,NULL
);
521 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script
,shutdown_ret
));
527 /*******************************************************************
529 ********************************************************************/
531 NTSTATUS
_reg_abort_shutdown(pipes_struct
*p
, REG_Q_ABORT_SHUTDOWN
*q_u
, REG_R_ABORT_SHUTDOWN
*r_u
)
533 NTSTATUS status
= NT_STATUS_OK
;
534 pstring abort_shutdown_script
;
536 pstrcpy(abort_shutdown_script
, lp_abort_shutdown_script());
538 if(*abort_shutdown_script
) {
539 int abort_shutdown_ret
;
540 abort_shutdown_ret
= smbrun(abort_shutdown_script
,NULL
);
541 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script
,abort_shutdown_ret
));