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 REGSTR_PRODUCTTYPE "ProductType"
33 #define REG_PT_WINNT "WinNT"
34 #define REG_PT_LANMANNT "LanmanNT"
35 #define REG_PT_SERVERNT "ServerNT"
37 #define OUR_HANDLE(hnd) (((hnd)==NULL)?"NULL":(IVAL((hnd)->data5,4)==(uint32)sys_getpid()?"OURS":"OTHER")), \
38 ((unsigned int)IVAL((hnd)->data5,4)),((unsigned int)sys_getpid())
41 static REGISTRY_KEY *regkeys_list;
44 /******************************************************************
45 free() function for REGISTRY_KEY
46 *****************************************************************/
48 static void free_regkey_info(void *ptr)
50 REGISTRY_KEY *info = (REGISTRY_KEY*)ptr;
52 DLIST_REMOVE(regkeys_list, info);
57 /******************************************************************
58 Find a registry key handle and return a REGISTRY_KEY
59 *****************************************************************/
61 static REGISTRY_KEY *find_regkey_index_by_hnd(pipes_struct *p, POLICY_HND *hnd)
63 REGISTRY_KEY *regkey = NULL;
65 if(!find_policy_by_hnd(p,hnd,(void **)®key)) {
66 DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: "));
74 /*******************************************************************
75 Function for open a new registry handle and creating a handle
76 Note that P should be valid & hnd should already have space
78 When we open a key, we store the full path to the key as
79 HK[LM|U]\<key>\<key>\...
80 *******************************************************************/
82 static NTSTATUS open_registry_key(pipes_struct *p, POLICY_HND *hnd, REGISTRY_KEY *parent,
83 char *subkeyname, uint32 access_granted )
85 REGISTRY_KEY *regkey = NULL;
86 NTSTATUS result = NT_STATUS_OK;
87 REGSUBKEY_CTR subkeys;
89 DEBUG(7,("open_registry_key: name = [%s][%s]\n",
90 parent ? parent->name : "NULL", subkeyname));
92 if ((regkey=(REGISTRY_KEY*)malloc(sizeof(REGISTRY_KEY))) == NULL)
93 return NT_STATUS_NO_MEMORY;
95 ZERO_STRUCTP( regkey );
98 * very crazy, but regedit.exe on Win2k will attempt to call
99 * REG_OPEN_ENTRY with a keyname of "". We should return a new
100 * (second) handle here on the key->name. regedt32.exe does
101 * not do this stupidity. --jerry
104 if (!subkeyname || !*subkeyname ) {
105 pstrcpy( regkey->name, parent->name );
108 pstrcpy( regkey->name, "" );
110 pstrcat( regkey->name, parent->name );
111 pstrcat( regkey->name, "\\" );
113 pstrcat( regkey->name, subkeyname );
116 /* Look up the table of registry I/O operations */
118 if ( !(regkey->hook = reghook_cache_find( regkey->name )) ) {
119 DEBUG(0,("open_registry_key: Failed to assigned a REGISTRY_HOOK to [%s]\n",
121 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
124 /* check if the path really exists; failed is indicated by -1 */
125 /* if the subkey count failed, bail out */
127 ZERO_STRUCTP( &subkeys );
129 regsubkey_ctr_init( &subkeys );
131 if ( fetch_reg_keys( regkey, &subkeys ) == -1 ) {
133 /* don't really know what to return here */
134 result = NT_STATUS_NO_SUCH_FILE;
138 * This would previously return NT_STATUS_TOO_MANY_SECRETS
139 * that doesn't sound quite right to me --jerry
142 if ( !create_policy_hnd( p, hnd, free_regkey_info, regkey ) )
143 result = NT_STATUS_OBJECT_NAME_NOT_FOUND;
148 regsubkey_ctr_destroy( &subkeys );
150 if ( ! NT_STATUS_IS_OK(result) )
153 DLIST_ADD( regkeys_list, regkey );
156 DEBUG(7,("open_registry_key: exit\n"));
161 /*******************************************************************
162 Function for open a new registry handle and creating a handle
163 Note that P should be valid & hnd should already have space
164 *******************************************************************/
166 static BOOL close_registry_key(pipes_struct *p, POLICY_HND *hnd)
168 REGISTRY_KEY *regkey = find_regkey_index_by_hnd(p, hnd);
171 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
175 close_policy_hnd(p, hnd);
180 /********************************************************************
181 retrieve information about the subkeys
182 *******************************************************************/
184 static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen )
188 REGSUBKEY_CTR subkeys;
194 ZERO_STRUCTP( &subkeys );
196 regsubkey_ctr_init( &subkeys );
198 if ( fetch_reg_keys( key, &subkeys ) == -1 )
201 /* find the longest string */
204 num_subkeys = regsubkey_ctr_numkeys( &subkeys );
206 for ( i=0; i<num_subkeys; i++ ) {
207 len = strlen( regsubkey_ctr_specific_key(&subkeys, i) );
208 max_len = MAX(max_len, len);
211 *maxnum = num_subkeys;
214 regsubkey_ctr_destroy( &subkeys );
219 /********************************************************************
220 retrieve information about the values. We don't store values
221 here. The registry tdb is intended to be a frontend to oether
222 Samba tdb's (such as ntdrivers.tdb).
223 *******************************************************************/
225 static BOOL get_value_information( REGISTRY_KEY *key, uint32 *maxnum,
226 uint32 *maxlen, uint32 *maxsize )
230 uint32 sizemax, lenmax;
237 ZERO_STRUCTP( &values );
239 regval_ctr_init( &values );
241 if ( fetch_reg_values( key, &values ) == -1 )
244 lenmax = sizemax = 0;
245 num_values = regval_ctr_numvals( &values );
247 val = regval_ctr_specific_value( &values, 0 );
249 for ( i=0; i<num_values && val; i++ )
251 lenmax = MAX(lenmax, strlen(val->valuename)+1 );
252 sizemax = MAX(sizemax, val->size );
254 val = regval_ctr_specific_value( &values, i );
257 *maxnum = num_values;
261 regval_ctr_destroy( &values );
267 /********************************************************************
269 ********************************************************************/
271 NTSTATUS _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u)
273 /* set up the REG unknown_1 response */
274 ZERO_STRUCT(r_u->pol);
276 /* close the policy handle */
277 if (!close_registry_key(p, &q_u->pol))
278 return NT_STATUS_OBJECT_NAME_INVALID;
283 /*******************************************************************
284 ********************************************************************/
286 NTSTATUS _reg_open_hklm(pipes_struct *p, REG_Q_OPEN_HKLM *q_u, REG_R_OPEN_HKLM *r_u)
288 return open_registry_key( p, &r_u->pol, NULL, KEY_HKLM, 0x0 );
291 /*******************************************************************
292 ********************************************************************/
294 NTSTATUS _reg_open_hkcr(pipes_struct *p, REG_Q_OPEN_HKCR *q_u, REG_R_OPEN_HKCR *r_u)
296 return open_registry_key( p, &r_u->pol, NULL, KEY_HKCR, 0x0 );
299 /*******************************************************************
300 ********************************************************************/
302 NTSTATUS _reg_open_hku(pipes_struct *p, REG_Q_OPEN_HKU *q_u, REG_R_OPEN_HKU *r_u)
304 return open_registry_key( p, &r_u->pol, NULL, KEY_HKU, 0x0 );
307 /*******************************************************************
309 ********************************************************************/
311 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
315 REGISTRY_KEY *key = find_regkey_index_by_hnd(p, &q_u->pol);
318 DEBUG(5,("reg_open_entry: Enter\n"));
321 return NT_STATUS_INVALID_HANDLE;
323 rpcstr_pull(name,q_u->uni_name.buffer,sizeof(name),q_u->uni_name.uni_str_len*2,0);
325 DEBUG(5,("reg_open_entry: Enter\n"));
327 result = open_registry_key( p, &pol, key, name, 0x0 );
329 init_reg_r_open_entry( r_u, &pol, result );
331 DEBUG(5,("reg_open_entry: Exit\n"));
336 /*******************************************************************
338 ********************************************************************/
340 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
342 NTSTATUS status = NT_STATUS_NO_SUCH_FILE;
344 char *value_ascii = "";
347 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
348 REGISTRY_VALUE *val = NULL;
349 REGISTRY_VALUE emptyval;
353 DEBUG(5,("_reg_info: Enter\n"));
356 return NT_STATUS_INVALID_HANDLE;
358 DEBUG(7,("_reg_info: policy key name = [%s]\n", regkey->name));
360 rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
362 DEBUG(5,("reg_info: looking up value: [%s]\n", name));
364 ZERO_STRUCTP( ®vals );
366 regval_ctr_init( ®vals );
368 /* couple of hard coded registry values */
370 if ( strequal(name, "RefusePasswordChange") ) {
371 ZERO_STRUCTP( &emptyval );
377 if ( strequal(name, REGSTR_PRODUCTTYPE) ) {
378 /* This makes the server look like a member server to clients */
379 /* which tells clients that we have our own local user and */
380 /* group databases and helps with ACL support. */
382 switch (lp_server_role()) {
383 case ROLE_DOMAIN_PDC:
384 case ROLE_DOMAIN_BDC:
385 value_ascii = REG_PT_LANMANNT;
387 case ROLE_STANDALONE:
388 value_ascii = REG_PT_SERVERNT;
390 case ROLE_DOMAIN_MEMBER:
391 value_ascii = REG_PT_WINNT;
394 value_length = push_ucs2(value, value, value_ascii,
396 STR_TERMINATE|STR_NOALIGN);
397 regval_ctr_addvalue(®vals, REGSTR_PRODUCTTYPE, REG_SZ,
398 value, value_length);
400 val = dup_registry_value( regval_ctr_specific_value( ®vals, 0 ) );
402 status = NT_STATUS_OK;
407 /* else fall back to actually looking up the value */
409 for ( i=0; fetch_reg_values_specific(regkey, &val, i); i++ )
411 DEBUG(10,("_reg_info: Testing value [%s]\n", val->valuename));
412 if ( StrCaseCmp( val->valuename, name ) == 0 ) {
413 DEBUG(10,("_reg_info: Found match for value [%s]\n", name));
414 status = NT_STATUS_OK;
418 free_registry_value( val );
423 new_init_reg_r_info(q_u->ptr_buf, r_u, val, status);
425 regval_ctr_destroy( ®vals );
426 free_registry_value( val );
428 DEBUG(5,("_reg_info: Exit\n"));
434 /*****************************************************************************
435 Implementation of REG_QUERY_KEY
436 ****************************************************************************/
438 NTSTATUS _reg_query_key(pipes_struct *p, REG_Q_QUERY_KEY *q_u, REG_R_QUERY_KEY *r_u)
440 NTSTATUS status = NT_STATUS_OK;
441 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
443 DEBUG(5,("_reg_query_key: Enter\n"));
446 return NT_STATUS_INVALID_HANDLE;
448 if ( !get_subkey_information( regkey, &r_u->num_subkeys, &r_u->max_subkeylen ) )
449 return NT_STATUS_ACCESS_DENIED;
451 if ( !get_value_information( regkey, &r_u->num_values, &r_u->max_valnamelen, &r_u->max_valbufsize ) )
452 return NT_STATUS_ACCESS_DENIED;
455 r_u->sec_desc = 0x00000078; /* size for key's sec_desc */
457 /* Win9x set this to 0x0 since it does not keep timestamps.
458 Doing the same here for simplicity --jerry */
460 ZERO_STRUCT(r_u->mod_time);
462 DEBUG(5,("_reg_query_key: Exit\n"));
468 /*****************************************************************************
469 Implementation of REG_UNKNOWN_1A
470 ****************************************************************************/
472 NTSTATUS _reg_unknown_1a(pipes_struct *p, REG_Q_UNKNOWN_1A *q_u, REG_R_UNKNOWN_1A *r_u)
474 NTSTATUS status = NT_STATUS_OK;
475 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
477 DEBUG(5,("_reg_unknown_1a: Enter\n"));
480 return NT_STATUS_INVALID_HANDLE;
482 r_u->unknown = 0x00000005; /* seems to be consistent...no idea what it means */
484 DEBUG(5,("_reg_unknown_1a: Exit\n"));
490 /*****************************************************************************
491 Implementation of REG_ENUM_KEY
492 ****************************************************************************/
494 NTSTATUS _reg_enum_key(pipes_struct *p, REG_Q_ENUM_KEY *q_u, REG_R_ENUM_KEY *r_u)
496 NTSTATUS status = NT_STATUS_OK;
497 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
501 DEBUG(5,("_reg_enum_key: Enter\n"));
504 return NT_STATUS_INVALID_HANDLE;
506 DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey->name));
508 if ( !fetch_reg_keys_specific( regkey, &subkey, q_u->key_index ) )
510 status = NT_STATUS_NO_MORE_ENTRIES;
514 DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey));
516 /* subkey has the string name now */
518 init_reg_r_enum_key( r_u, subkey, q_u->unknown_1, q_u->unknown_2 );
520 DEBUG(5,("_reg_enum_key: Exit\n"));
527 /*****************************************************************************
528 Implementation of REG_ENUM_VALUE
529 ****************************************************************************/
531 NTSTATUS _reg_enum_value(pipes_struct *p, REG_Q_ENUM_VALUE *q_u, REG_R_ENUM_VALUE *r_u)
533 NTSTATUS status = NT_STATUS_OK;
534 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
538 DEBUG(5,("_reg_enum_value: Enter\n"));
541 return NT_STATUS_INVALID_HANDLE;
543 DEBUG(8,("_reg_enum_key: enumerating values for key [%s]\n", regkey->name));
545 if ( !fetch_reg_values_specific( regkey, &val, q_u->val_index ) )
547 status = NT_STATUS_NO_MORE_ENTRIES;
551 DEBUG(10,("_reg_enum_value: retrieved value named [%s]\n", val->valuename));
553 /* subkey has the string name now */
555 init_reg_r_enum_val( r_u, val );
558 DEBUG(5,("_reg_enum_value: Exit\n"));
561 free_registry_value( val );
567 /*******************************************************************
569 ********************************************************************/
571 #define SHUTDOWN_R_STRING "-r"
572 #define SHUTDOWN_F_STRING "-f"
575 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
577 NTSTATUS status = NT_STATUS_OK;
578 pstring shutdown_script;
579 UNISTR2 unimsg = q_u->uni_msg;
587 rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
589 alpha_strcpy (chkmsg, message, NULL, sizeof(message));
591 snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
593 snprintf(r, sizeof(r), (q_u->flags & REG_REBOOT_ON_SHUTDOWN)?SHUTDOWN_R_STRING:"");
595 snprintf(f, sizeof(f), (q_u->flags & REG_FORCE_SHUTDOWN)?SHUTDOWN_F_STRING:"");
597 pstrcpy(shutdown_script, lp_shutdown_script());
599 if(*shutdown_script) {
601 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
602 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
603 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
604 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
605 shutdown_ret = smbrun(shutdown_script,NULL);
606 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
612 /*******************************************************************
614 ********************************************************************/
616 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
618 NTSTATUS status = NT_STATUS_OK;
619 pstring abort_shutdown_script;
621 pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
623 if(*abort_shutdown_script) {
624 int abort_shutdown_ret;
625 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
626 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
632 /*******************************************************************
634 ********************************************************************/
636 NTSTATUS _reg_save_key(pipes_struct *p, REG_Q_SAVE_KEY *q_u, REG_R_SAVE_KEY *r_u)
638 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
640 DEBUG(5,("_reg_save_key: Enter\n"));
643 * basically this is a no op function which just gverifies
644 * that the client gave us a valid registry key handle
648 return NT_STATUS_INVALID_HANDLE;
650 DEBUG(8,("_reg_save_key: berifying backup of key [%s]\n", regkey->name));