usrmgr expects unicode as ProductType.
[Samba/gebeck_regimport.git] / source3 / rpc_server / srv_reg_nt.c
blobf96de7e5339c894fa421e54008bdf3804df2b84b
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 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 */
135 result = NT_STATUS_NO_SUCH_FILE;
137 else {
139 * This would previously return NT_STATUS_TOO_MANY_SECRETS
140 * that doesn't sound quite right to me --jerry
143 if ( !create_policy_hnd( p, hnd, free_regkey_info, regkey ) )
144 result = NT_STATUS_OBJECT_NAME_NOT_FOUND;
147 /* clean up */
149 regsubkey_ctr_destroy( &subkeys );
151 if ( ! NT_STATUS_IS_OK(result) )
152 SAFE_FREE( regkey );
153 else
154 DLIST_ADD( regkeys_list, regkey );
157 DEBUG(7,("open_registry_key: exit\n"));
159 return result;
162 /*******************************************************************
163 Function for open a new registry handle and creating a handle
164 Note that P should be valid & hnd should already have space
165 *******************************************************************/
167 static BOOL close_registry_key(pipes_struct *p, POLICY_HND *hnd)
169 REGISTRY_KEY *regkey = find_regkey_index_by_hnd(p, hnd);
171 if ( !regkey ) {
172 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
173 return False;
176 close_policy_hnd(p, hnd);
178 return True;
181 /********************************************************************
182 retrieve information about the subkeys
183 *******************************************************************/
185 static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen )
187 int num_subkeys, i;
188 uint32 max_len;
189 REGSUBKEY_CTR subkeys;
190 uint32 len;
192 if ( !key )
193 return False;
195 ZERO_STRUCTP( &subkeys );
197 regsubkey_ctr_init( &subkeys );
199 if ( fetch_reg_keys( key, &subkeys ) == -1 )
200 return False;
202 /* find the longest string */
204 max_len = 0;
205 num_subkeys = regsubkey_ctr_numkeys( &subkeys );
207 for ( i=0; i<num_subkeys; i++ ) {
208 len = strlen( regsubkey_ctr_specific_key(&subkeys, i) );
209 max_len = MAX(max_len, len);
212 *maxnum = num_subkeys;
213 *maxlen = max_len*2;
215 regsubkey_ctr_destroy( &subkeys );
217 return True;
220 /********************************************************************
221 retrieve information about the values. We don't store values
222 here. The registry tdb is intended to be a frontend to oether
223 Samba tdb's (such as ntdrivers.tdb).
224 *******************************************************************/
226 static BOOL get_value_information( REGISTRY_KEY *key, uint32 *maxnum,
227 uint32 *maxlen, uint32 *maxsize )
229 REGVAL_CTR values;
230 REGISTRY_VALUE *val;
231 uint32 sizemax, lenmax;
232 int i, num_values;
234 if ( !key )
235 return False;
238 ZERO_STRUCTP( &values );
240 regval_ctr_init( &values );
242 if ( fetch_reg_values( key, &values ) == -1 )
243 return False;
245 lenmax = sizemax = 0;
246 num_values = regval_ctr_numvals( &values );
248 val = regval_ctr_specific_value( &values, 0 );
250 for ( i=0; i<num_values && val; i++ )
252 lenmax = MAX(lenmax, strlen(val->valuename)+1 );
253 sizemax = MAX(sizemax, val->size );
255 val = regval_ctr_specific_value( &values, i );
258 *maxnum = num_values;
259 *maxlen = lenmax;
260 *maxsize = sizemax;
262 regval_ctr_destroy( &values );
264 return True;
268 /********************************************************************
269 reg_close
270 ********************************************************************/
272 NTSTATUS _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u)
274 /* set up the REG unknown_1 response */
275 ZERO_STRUCT(r_u->pol);
277 /* close the policy handle */
278 if (!close_registry_key(p, &q_u->pol))
279 return NT_STATUS_OBJECT_NAME_INVALID;
281 return NT_STATUS_OK;
284 /*******************************************************************
285 ********************************************************************/
287 NTSTATUS _reg_open_hklm(pipes_struct *p, REG_Q_OPEN_HKLM *q_u, REG_R_OPEN_HKLM *r_u)
289 return open_registry_key( p, &r_u->pol, NULL, KEY_HKLM, 0x0 );
292 /*******************************************************************
293 ********************************************************************/
295 NTSTATUS _reg_open_hkcr(pipes_struct *p, REG_Q_OPEN_HKCR *q_u, REG_R_OPEN_HKCR *r_u)
297 return open_registry_key( p, &r_u->pol, NULL, KEY_HKCR, 0x0 );
300 /*******************************************************************
301 ********************************************************************/
303 NTSTATUS _reg_open_hku(pipes_struct *p, REG_Q_OPEN_HKU *q_u, REG_R_OPEN_HKU *r_u)
305 return open_registry_key( p, &r_u->pol, NULL, KEY_HKU, 0x0 );
308 /*******************************************************************
309 reg_reply_open_entry
310 ********************************************************************/
312 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
314 POLICY_HND pol;
315 fstring name;
316 REGISTRY_KEY *key = find_regkey_index_by_hnd(p, &q_u->pol);
317 NTSTATUS result;
319 DEBUG(5,("reg_open_entry: Enter\n"));
321 if ( !key )
322 return NT_STATUS_INVALID_HANDLE;
324 rpcstr_pull(name,q_u->uni_name.buffer,sizeof(name),q_u->uni_name.uni_str_len*2,0);
326 DEBUG(5,("reg_open_entry: Enter\n"));
328 result = open_registry_key( p, &pol, key, name, 0x0 );
330 init_reg_r_open_entry( r_u, &pol, result );
332 DEBUG(5,("reg_open_entry: Exit\n"));
334 return r_u->status;
337 /*******************************************************************
338 reg_reply_info
339 ********************************************************************/
341 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
343 NTSTATUS status = NT_STATUS_NO_SUCH_FILE;
344 fstring name;
345 char *value_ascii = "";
346 fstring value;
347 int value_length;
348 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
349 REGISTRY_VALUE *val = NULL;
350 REGISTRY_VALUE emptyval;
351 REGVAL_CTR regvals;
352 int i;
354 DEBUG(5,("_reg_info: Enter\n"));
356 if ( !regkey )
357 return NT_STATUS_INVALID_HANDLE;
359 DEBUG(7,("_reg_info: policy key name = [%s]\n", regkey->name));
361 rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
363 DEBUG(5,("reg_info: looking up value: [%s]\n", name));
365 ZERO_STRUCTP( &regvals );
367 regval_ctr_init( &regvals );
369 /* couple of hard coded registry values */
371 if ( strequal(name, "RefusePasswordChange") ) {
372 ZERO_STRUCTP( &emptyval );
373 val = &emptyval;
375 goto out;
378 if ( strequal(name, REGSTR_PRODUCTTYPE) ) {
379 /* This makes the server look like a member server to clients */
380 /* which tells clients that we have our own local user and */
381 /* group databases and helps with ACL support. */
383 switch (lp_server_role()) {
384 case ROLE_DOMAIN_PDC:
385 case ROLE_DOMAIN_BDC:
386 value_ascii = REG_PT_LANMANNT;
387 break;
388 case ROLE_STANDALONE:
389 value_ascii = REG_PT_SERVERNT;
390 break;
391 case ROLE_DOMAIN_MEMBER:
392 value_ascii = REG_PT_WINNT;
393 break;
395 value_length = push_ucs2(value, value, value_ascii,
396 sizeof(value),
397 STR_TERMINATE|STR_NOALIGN);
398 regval_ctr_addvalue(&regvals, REGSTR_PRODUCTTYPE, REG_SZ,
399 value, value_length);
401 val = dup_registry_value( regval_ctr_specific_value( &regvals, 0 ) );
403 status = NT_STATUS_OK;
405 goto out;
408 /* else fall back to actually looking up the value */
410 for ( i=0; fetch_reg_values_specific(regkey, &val, i); i++ )
412 DEBUG(10,("_reg_info: Testing value [%s]\n", val->valuename));
413 if ( StrCaseCmp( val->valuename, name ) == 0 ) {
414 DEBUG(10,("_reg_info: Found match for value [%s]\n", name));
415 status = NT_STATUS_OK;
416 break;
419 free_registry_value( val );
423 out:
424 new_init_reg_r_info(q_u->ptr_buf, r_u, val, status);
426 regval_ctr_destroy( &regvals );
427 free_registry_value( val );
429 DEBUG(5,("_reg_info: Exit\n"));
431 return status;
435 /*****************************************************************************
436 Implementation of REG_QUERY_KEY
437 ****************************************************************************/
439 NTSTATUS _reg_query_key(pipes_struct *p, REG_Q_QUERY_KEY *q_u, REG_R_QUERY_KEY *r_u)
441 NTSTATUS status = NT_STATUS_OK;
442 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
444 DEBUG(5,("_reg_query_key: Enter\n"));
446 if ( !regkey )
447 return NT_STATUS_INVALID_HANDLE;
449 if ( !get_subkey_information( regkey, &r_u->num_subkeys, &r_u->max_subkeylen ) )
450 return NT_STATUS_ACCESS_DENIED;
452 if ( !get_value_information( regkey, &r_u->num_values, &r_u->max_valnamelen, &r_u->max_valbufsize ) )
453 return NT_STATUS_ACCESS_DENIED;
456 r_u->sec_desc = 0x00000078; /* size for key's sec_desc */
458 /* Win9x set this to 0x0 since it does not keep timestamps.
459 Doing the same here for simplicity --jerry */
461 ZERO_STRUCT(r_u->mod_time);
463 DEBUG(5,("_reg_query_key: Exit\n"));
465 return status;
469 /*****************************************************************************
470 Implementation of REG_UNKNOWN_1A
471 ****************************************************************************/
473 NTSTATUS _reg_unknown_1a(pipes_struct *p, REG_Q_UNKNOWN_1A *q_u, REG_R_UNKNOWN_1A *r_u)
475 NTSTATUS status = NT_STATUS_OK;
476 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
478 DEBUG(5,("_reg_unknown_1a: Enter\n"));
480 if ( !regkey )
481 return NT_STATUS_INVALID_HANDLE;
483 r_u->unknown = 0x00000005; /* seems to be consistent...no idea what it means */
485 DEBUG(5,("_reg_unknown_1a: Exit\n"));
487 return status;
491 /*****************************************************************************
492 Implementation of REG_ENUM_KEY
493 ****************************************************************************/
495 NTSTATUS _reg_enum_key(pipes_struct *p, REG_Q_ENUM_KEY *q_u, REG_R_ENUM_KEY *r_u)
497 NTSTATUS status = NT_STATUS_OK;
498 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
499 char *subkey = NULL;
502 DEBUG(5,("_reg_enum_key: Enter\n"));
504 if ( !regkey )
505 return NT_STATUS_INVALID_HANDLE;
507 DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey->name));
509 if ( !fetch_reg_keys_specific( regkey, &subkey, q_u->key_index ) )
511 status = NT_STATUS_NO_MORE_ENTRIES;
512 goto done;
515 DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey));
517 /* subkey has the string name now */
519 init_reg_r_enum_key( r_u, subkey, q_u->unknown_1, q_u->unknown_2 );
521 DEBUG(5,("_reg_enum_key: Exit\n"));
523 done:
524 SAFE_FREE( subkey );
525 return status;
528 /*****************************************************************************
529 Implementation of REG_ENUM_VALUE
530 ****************************************************************************/
532 NTSTATUS _reg_enum_value(pipes_struct *p, REG_Q_ENUM_VALUE *q_u, REG_R_ENUM_VALUE *r_u)
534 NTSTATUS status = NT_STATUS_OK;
535 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
536 REGISTRY_VALUE *val;
539 DEBUG(5,("_reg_enum_value: Enter\n"));
541 if ( !regkey )
542 return NT_STATUS_INVALID_HANDLE;
544 DEBUG(8,("_reg_enum_key: enumerating values for key [%s]\n", regkey->name));
546 if ( !fetch_reg_values_specific( regkey, &val, q_u->val_index ) )
548 status = NT_STATUS_NO_MORE_ENTRIES;
549 goto done;
552 DEBUG(10,("_reg_enum_value: retrieved value named [%s]\n", val->valuename));
554 /* subkey has the string name now */
556 init_reg_r_enum_val( r_u, val );
559 DEBUG(5,("_reg_enum_value: Exit\n"));
561 done:
562 free_registry_value( val );
564 return status;
568 /*******************************************************************
569 reg_shutdwon
570 ********************************************************************/
572 #define SHUTDOWN_R_STRING "-r"
573 #define SHUTDOWN_F_STRING "-f"
576 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
578 NTSTATUS status = NT_STATUS_OK;
579 pstring shutdown_script;
580 UNISTR2 unimsg = q_u->uni_msg;
581 pstring message;
582 pstring chkmsg;
583 fstring timeout;
584 fstring r;
585 fstring f;
587 /* message */
588 rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
589 /* security check */
590 alpha_strcpy (chkmsg, message, NULL, sizeof(message));
591 /* timeout */
592 snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
593 /* reboot */
594 snprintf(r, sizeof(r), (q_u->flags & REG_REBOOT_ON_SHUTDOWN)?SHUTDOWN_R_STRING:"");
595 /* force */
596 snprintf(f, sizeof(f), (q_u->flags & REG_FORCE_SHUTDOWN)?SHUTDOWN_F_STRING:"");
598 pstrcpy(shutdown_script, lp_shutdown_script());
600 if(*shutdown_script) {
601 int shutdown_ret;
602 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
603 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
604 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
605 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
606 shutdown_ret = smbrun(shutdown_script,NULL);
607 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
610 return status;
613 /*******************************************************************
614 reg_abort_shutdwon
615 ********************************************************************/
617 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
619 NTSTATUS status = NT_STATUS_OK;
620 pstring abort_shutdown_script;
622 pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
624 if(*abort_shutdown_script) {
625 int abort_shutdown_ret;
626 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
627 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
630 return status;
633 /*******************************************************************
634 REG_SAVE_KEY (0x14)
635 ********************************************************************/
637 NTSTATUS _reg_save_key(pipes_struct *p, REG_Q_SAVE_KEY *q_u, REG_R_SAVE_KEY *r_u)
639 REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
641 DEBUG(5,("_reg_save_key: Enter\n"));
644 * basically this is a no op function which just gverifies
645 * that the client gave us a valid registry key handle
648 if ( !regkey )
649 return NT_STATUS_INVALID_HANDLE;
651 DEBUG(8,("_reg_save_key: berifying backup of key [%s]\n", regkey->name));
654 return NT_STATUS_OK;