removing extra debug statement
[Samba.git] / source / rpc_server / srv_reg_nt.c
blobdb711dc6af9f0d95c4d9ceb0013eed309511ceb7
1 /*
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. */
27 #include "includes.h"
29 #undef DBGC_CLASS
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);
54 SAFE_FREE(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 **)&regkey)) {
66 DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: "));
67 return NULL;
70 return regkey;
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 const 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 );
97 /*
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 );
107 else {
108 pstrcpy( regkey->name, "" );
109 if ( parent ) {
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",
120 regkey->name ));
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;
136 else {
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;
146 /* clean up */
148 regsubkey_ctr_destroy( &subkeys );
150 if ( ! NT_STATUS_IS_OK(result) )
151 SAFE_FREE( regkey );
152 else
153 DLIST_ADD( regkeys_list, regkey );
156 DEBUG(7,("open_registry_key: exit\n"));
158 return result;
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);
170 if ( !regkey ) {
171 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
172 return False;
175 close_policy_hnd(p, hnd);
177 return True;
180 /********************************************************************
181 retrieve information about the subkeys
182 *******************************************************************/
184 static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen )
186 int num_subkeys, i;
187 uint32 max_len;
188 REGSUBKEY_CTR subkeys;
189 uint32 len;
191 if ( !key )
192 return False;
194 ZERO_STRUCTP( &subkeys );
196 regsubkey_ctr_init( &subkeys );
198 if ( fetch_reg_keys( key, &subkeys ) == -1 )
199 return False;
201 /* find the longest string */
203 max_len = 0;
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;
212 *maxlen = max_len*2;
214 regsubkey_ctr_destroy( &subkeys );
216 return True;
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 )
228 REGVAL_CTR values;
229 REGISTRY_VALUE *val;
230 uint32 sizemax, lenmax;
231 int i, num_values;
233 if ( !key )
234 return False;
237 ZERO_STRUCTP( &values );
239 regval_ctr_init( &values );
241 if ( fetch_reg_values( key, &values ) == -1 )
242 return False;
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;
258 *maxlen = lenmax;
259 *maxsize = sizemax;
261 regval_ctr_destroy( &values );
263 return True;
267 /********************************************************************
268 reg_close
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;
280 return NT_STATUS_OK;
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 /*******************************************************************
308 reg_reply_open_entry
309 ********************************************************************/
311 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
313 POLICY_HND pol;
314 fstring name;
315 REGISTRY_KEY *key = find_regkey_index_by_hnd(p, &q_u->pol);
316 NTSTATUS result;
318 DEBUG(5,("reg_open_entry: Enter\n"));
320 if ( !key )
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 result = open_registry_key( p, &pol, key, name, 0x0 );
327 init_reg_r_open_entry( r_u, &pol, result );
329 DEBUG(5,("reg_open_entry: Exit\n"));
331 return r_u->status;
334 /*******************************************************************
335 reg_reply_info
336 ********************************************************************/
338 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
340 NTSTATUS status = NT_STATUS_NO_SUCH_FILE;
341 fstring name;
342 const char *value_ascii = "";
343 fstring value;
344 int value_length;
345 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
346 REGISTRY_VALUE *val = NULL;
347 REGISTRY_VALUE emptyval;
348 REGVAL_CTR regvals;
349 int i;
351 DEBUG(5,("_reg_info: Enter\n"));
353 if ( !regkey )
354 return NT_STATUS_INVALID_HANDLE;
356 DEBUG(7,("_reg_info: policy key name = [%s]\n", regkey->name));
358 rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
360 DEBUG(5,("reg_info: looking up value: [%s]\n", name));
362 ZERO_STRUCTP( &regvals );
364 regval_ctr_init( &regvals );
366 /* couple of hard coded registry values */
368 if ( strequal(name, "RefusePasswordChange") ) {
369 ZERO_STRUCTP( &emptyval );
370 val = &emptyval;
372 goto out;
375 if ( strequal(name, REGSTR_PRODUCTTYPE) ) {
376 /* This makes the server look like a member server to clients */
377 /* which tells clients that we have our own local user and */
378 /* group databases and helps with ACL support. */
380 switch (lp_server_role()) {
381 case ROLE_DOMAIN_PDC:
382 case ROLE_DOMAIN_BDC:
383 value_ascii = REG_PT_LANMANNT;
384 break;
385 case ROLE_STANDALONE:
386 value_ascii = REG_PT_SERVERNT;
387 break;
388 case ROLE_DOMAIN_MEMBER:
389 value_ascii = REG_PT_WINNT;
390 break;
392 value_length = push_ucs2(value, value, value_ascii,
393 sizeof(value),
394 STR_TERMINATE|STR_NOALIGN);
395 regval_ctr_addvalue(&regvals, REGSTR_PRODUCTTYPE, REG_SZ,
396 value, value_length);
398 val = dup_registry_value( regval_ctr_specific_value( &regvals, 0 ) );
400 status = NT_STATUS_OK;
402 goto out;
405 /* else fall back to actually looking up the value */
407 for ( i=0; fetch_reg_values_specific(regkey, &val, i); i++ )
409 DEBUG(10,("_reg_info: Testing value [%s]\n", val->valuename));
410 if ( StrCaseCmp( val->valuename, name ) == 0 ) {
411 DEBUG(10,("_reg_info: Found match for value [%s]\n", name));
412 status = NT_STATUS_OK;
413 break;
416 free_registry_value( val );
420 out:
421 new_init_reg_r_info(q_u->ptr_buf, r_u, val, status);
423 regval_ctr_destroy( &regvals );
424 free_registry_value( val );
426 DEBUG(5,("_reg_info: Exit\n"));
428 return status;
432 /*****************************************************************************
433 Implementation of REG_QUERY_KEY
434 ****************************************************************************/
436 NTSTATUS _reg_query_key(pipes_struct *p, REG_Q_QUERY_KEY *q_u, REG_R_QUERY_KEY *r_u)
438 NTSTATUS status = NT_STATUS_OK;
439 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
441 DEBUG(5,("_reg_query_key: Enter\n"));
443 if ( !regkey )
444 return NT_STATUS_INVALID_HANDLE;
446 if ( !get_subkey_information( regkey, &r_u->num_subkeys, &r_u->max_subkeylen ) )
447 return NT_STATUS_ACCESS_DENIED;
449 if ( !get_value_information( regkey, &r_u->num_values, &r_u->max_valnamelen, &r_u->max_valbufsize ) )
450 return NT_STATUS_ACCESS_DENIED;
453 r_u->sec_desc = 0x00000078; /* size for key's sec_desc */
455 /* Win9x set this to 0x0 since it does not keep timestamps.
456 Doing the same here for simplicity --jerry */
458 ZERO_STRUCT(r_u->mod_time);
460 DEBUG(5,("_reg_query_key: Exit\n"));
462 return status;
466 /*****************************************************************************
467 Implementation of REG_UNKNOWN_1A
468 ****************************************************************************/
470 NTSTATUS _reg_unknown_1a(pipes_struct *p, REG_Q_UNKNOWN_1A *q_u, REG_R_UNKNOWN_1A *r_u)
472 NTSTATUS status = NT_STATUS_OK;
473 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
475 DEBUG(5,("_reg_unknown_1a: Enter\n"));
477 if ( !regkey )
478 return NT_STATUS_INVALID_HANDLE;
480 r_u->unknown = 0x00000005; /* seems to be consistent...no idea what it means */
482 DEBUG(5,("_reg_unknown_1a: Exit\n"));
484 return status;
488 /*****************************************************************************
489 Implementation of REG_ENUM_KEY
490 ****************************************************************************/
492 NTSTATUS _reg_enum_key(pipes_struct *p, REG_Q_ENUM_KEY *q_u, REG_R_ENUM_KEY *r_u)
494 NTSTATUS status = NT_STATUS_OK;
495 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
496 char *subkey = NULL;
499 DEBUG(5,("_reg_enum_key: Enter\n"));
501 if ( !regkey )
502 return NT_STATUS_INVALID_HANDLE;
504 DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey->name));
506 if ( !fetch_reg_keys_specific( regkey, &subkey, q_u->key_index ) )
508 status = NT_STATUS_NO_MORE_ENTRIES;
509 goto done;
512 DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey));
514 /* subkey has the string name now */
516 init_reg_r_enum_key( r_u, subkey, q_u->unknown_1, q_u->unknown_2 );
518 DEBUG(5,("_reg_enum_key: Exit\n"));
520 done:
521 SAFE_FREE( subkey );
522 return status;
525 /*****************************************************************************
526 Implementation of REG_ENUM_VALUE
527 ****************************************************************************/
529 NTSTATUS _reg_enum_value(pipes_struct *p, REG_Q_ENUM_VALUE *q_u, REG_R_ENUM_VALUE *r_u)
531 NTSTATUS status = NT_STATUS_OK;
532 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
533 REGISTRY_VALUE *val;
536 DEBUG(5,("_reg_enum_value: Enter\n"));
538 if ( !regkey )
539 return NT_STATUS_INVALID_HANDLE;
541 DEBUG(8,("_reg_enum_key: enumerating values for key [%s]\n", regkey->name));
543 if ( !fetch_reg_values_specific( regkey, &val, q_u->val_index ) )
545 status = NT_STATUS_NO_MORE_ENTRIES;
546 goto done;
549 DEBUG(10,("_reg_enum_value: retrieved value named [%s]\n", val->valuename));
551 /* subkey has the string name now */
553 init_reg_r_enum_val( r_u, val );
556 DEBUG(5,("_reg_enum_value: Exit\n"));
558 done:
559 free_registry_value( val );
561 return status;
565 /*******************************************************************
566 reg_shutdwon
567 ********************************************************************/
569 #define SHUTDOWN_R_STRING "-r"
570 #define SHUTDOWN_F_STRING "-f"
573 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
575 NTSTATUS status = NT_STATUS_OK;
576 pstring shutdown_script;
577 UNISTR2 unimsg = q_u->uni_msg;
578 pstring message;
579 pstring chkmsg;
580 fstring timeout;
581 fstring r;
582 fstring f;
584 /* message */
585 rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
586 /* security check */
587 alpha_strcpy (chkmsg, message, NULL, sizeof(message));
588 /* timeout */
589 snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
590 /* reboot */
591 snprintf(r, sizeof(r), (q_u->reboot) ? SHUTDOWN_R_STRING : "");
592 /* force */
593 snprintf(f, sizeof(f), (q_u->force) ? SHUTDOWN_F_STRING : "");
595 pstrcpy(shutdown_script, lp_shutdown_script());
597 if(*shutdown_script) {
598 int shutdown_ret;
599 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
600 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
601 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
602 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
603 shutdown_ret = smbrun(shutdown_script,NULL);
604 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
607 return status;
610 /*******************************************************************
611 reg_abort_shutdwon
612 ********************************************************************/
614 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
616 NTSTATUS status = NT_STATUS_OK;
617 pstring abort_shutdown_script;
619 pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
621 if(*abort_shutdown_script) {
622 int abort_shutdown_ret;
623 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
624 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
627 return status;
630 /*******************************************************************
631 REG_SAVE_KEY (0x14)
632 ********************************************************************/
634 NTSTATUS _reg_save_key(pipes_struct *p, REG_Q_SAVE_KEY *q_u, REG_R_SAVE_KEY *r_u)
636 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
638 DEBUG(5,("_reg_save_key: Enter\n"));
641 * basically this is a no op function which just gverifies
642 * that the client gave us a valid registry key handle
645 if ( !regkey )
646 return NT_STATUS_INVALID_HANDLE;
648 DEBUG(8,("_reg_save_key: berifying backup of key [%s]\n", regkey->name));
651 return NT_STATUS_OK;