virtual registry framework with initial printing hooks.
[Samba.git] / source / rpc_server / srv_reg_nt.c
blobd5b2394b40f51febcbb056eadb01c0e1fa71d324
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 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);
49 SAFE_FREE(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 **)&regkey)) {
61 DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: "));
62 return NULL;
65 return regkey;
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 int num_subkeys;
84 char *subkeys = NULL;
86 if ( parent ) {
87 pstrcpy( parent_keyname, parent->name );
88 pstrcat( parent_keyname, "\\" );
90 else
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 );
108 /* copy the name */
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;
117 else
118 regkey->hook = reghook_cache_find( regkey->name );
120 if ( regkey->hook ) {
121 DEBUG(10,("open_registry_key: Assigned REGISTRY_HOOK to [%s]\n",
122 regkey->name ));
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 ) {
132 SAFE_FREE( regkey );
134 /* don't really know what to return here */
136 result = NT_STATUS_ACCESS_DENIED;
138 else {
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 );
152 return result;
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);
164 if ( !regkey ) {
165 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
166 return False;
169 close_policy_hnd(p, hnd);
171 return True;
174 /********************************************************************
175 retrieve information about the subkeys
176 *******************************************************************/
178 static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen )
180 int num_subkeys, i;
181 uint32 max_len;
182 char *subkeys = NULL;
183 uint32 len;
184 char *s;
186 if ( !key )
187 return False;
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 )
195 return False;
197 /* find the longest string */
199 max_len = 0;
200 s = subkeys;
201 for ( i=0; i<num_subkeys; i++ ) {
202 len = strlen(s);
203 max_len = MAX(max_len, len);
204 s += len + 1;
207 *maxnum = num_subkeys;
208 *maxlen = max_len*2;
210 SAFE_FREE(subkeys);
212 return True;
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;
226 int num_values;
228 if ( !key )
229 return False;
231 num_values = fetch_reg_values( key, &val );
233 if ( num_values == -1 )
234 return False;
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;
245 *maxlen = lenmax;
246 *maxsize = sizemax;
248 SAFE_FREE( val );
250 return True;
254 /********************************************************************
255 reg_close
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;
267 return NT_STATUS_OK;
270 /*******************************************************************
271 reg_reply_open
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 /*******************************************************************
280 reg_reply_open
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 /*******************************************************************
289 reg_reply_open_entry
290 ********************************************************************/
292 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
294 POLICY_HND pol;
295 fstring name;
296 REGISTRY_KEY *key = find_regkey_index_by_hnd(p, &q_u->pol);
297 NTSTATUS result;
299 DEBUG(5,("reg_open_entry: Enter\n"));
301 if ( !key )
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"));
314 return r_u->status;
317 /*******************************************************************
318 reg_reply_info
319 ********************************************************************/
321 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
323 NTSTATUS status = NT_STATUS_OK;
324 char *value = NULL;
325 uint32 type = 0x1; /* key type: REG_SZ */
326 UNISTR2 *uni_key = NULL;
327 BUFFER2 *buf = NULL;
328 fstring name;
329 REGISTRY_KEY *key = find_regkey_index_by_hnd( p, &q_u->pol );
331 DEBUG(5,("_reg_info: Enter\n"));
333 if ( !key )
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") ) {
349 type=0xF770;
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);
354 buf->buf_max_len=4;
356 goto out;
359 switch (lp_server_role()) {
360 case ROLE_DOMAIN_PDC:
361 case ROLE_DOMAIN_BDC:
362 value = "LanmanNT";
363 break;
364 case ROLE_STANDALONE:
365 value = "ServerNT";
366 break;
367 case ROLE_DOMAIN_MEMBER:
368 value = "WinNT";
369 break;
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);
379 out:
380 init_reg_r_info(q_u->ptr_buf, r_u, buf, type, status);
382 DEBUG(5,("reg_open_entry: Exit\n"));
384 return status;
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"));
399 if ( !regkey )
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"));
418 return status;
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"));
433 if ( !regkey )
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"));
440 return status;
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 );
452 char *subkey;
455 DEBUG(5,("_reg_enum_key: Enter\n"));
457 if ( !regkey )
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;
465 goto done;
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"));
476 done:
477 SAFE_FREE( subkey );
478 return status;
482 /*******************************************************************
483 reg_shutdwon
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;
495 pstring message;
496 pstring chkmsg;
497 fstring timeout;
498 fstring r;
499 fstring f;
501 /* message */
502 rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
503 /* security check */
504 alpha_strcpy (chkmsg, message, NULL, sizeof(message));
505 /* timeout */
506 snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
507 /* reboot */
508 snprintf(r, sizeof(r), (q_u->flags & REG_REBOOT_ON_SHUTDOWN)?SHUTDOWN_R_STRING:"");
509 /* force */
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) {
515 int shutdown_ret;
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));
524 return status;
527 /*******************************************************************
528 reg_abort_shutdwon
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));
544 return status;