📄 srv_reg_nt.c
字号:
/* * Unix SMB/CIFS implementation. * RPC Pipe client / server routines * Copyright (C) Andrew Tridgell 1992-1997. * Copyright (C) Luke Kenneth Casson Leighton 1996-1997. * Copyright (C) Paul Ashton 1997. * Copyright (C) Jeremy Allison 2001. * Copyright (C) Gerald Carter 2002-2005. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* Implementation of registry functions. */#include "includes.h"#include "regfio.h"#undef DBGC_CLASS#define DBGC_CLASS DBGC_RPC_SRV#define OUR_HANDLE(hnd) (((hnd)==NULL)?"NULL":(IVAL((hnd)->data5,4)==(uint32)sys_getpid()?"OURS":"OTHER")), \((unsigned int)IVAL((hnd)->data5,4)),((unsigned int)sys_getpid())static struct generic_mapping reg_generic_map = { REG_KEY_READ, REG_KEY_WRITE, REG_KEY_EXECUTE, REG_KEY_ALL };/****************************************************************** free() function for REGISTRY_KEY *****************************************************************/ static void free_regkey_info(void *ptr){ regkey_close_internal( (REGISTRY_KEY*)ptr );}/****************************************************************** Find a registry key handle and return a REGISTRY_KEY *****************************************************************/static REGISTRY_KEY *find_regkey_index_by_hnd(pipes_struct *p, POLICY_HND *hnd){ REGISTRY_KEY *regkey = NULL; if(!find_policy_by_hnd(p,hnd,(void **)(void *)®key)) { DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: ")); return NULL; } return regkey;}/******************************************************************* Function for open a new registry handle and creating a handle Note that P should be valid & hnd should already have space When we open a key, we store the full path to the key as HK[LM|U]\<key>\<key>\... *******************************************************************/ static WERROR open_registry_key( pipes_struct *p, POLICY_HND *hnd, REGISTRY_KEY **keyinfo, REGISTRY_KEY *parent, const char *subkeyname, uint32 access_desired ){ pstring keypath; int path_len; WERROR result = WERR_OK; /* create a full registry path and strip any trailing '\' characters */ pstr_sprintf( keypath, "%s%s%s", parent ? parent->name : "", parent ? "\\" : "", subkeyname ); path_len = strlen( keypath ); if ( path_len && keypath[path_len-1] == '\\' ) keypath[path_len-1] = '\0'; /* now do the internal open */ result = regkey_open_internal( keyinfo, keypath, p->pipe_user.nt_user_token, access_desired ); if ( !W_ERROR_IS_OK(result) ) return result; if ( !create_policy_hnd( p, hnd, free_regkey_info, *keyinfo ) ) { result = WERR_BADFILE; regkey_close_internal( *keyinfo ); } return result;}/******************************************************************* Function for open a new registry handle and creating a handle Note that P should be valid & hnd should already have space *******************************************************************/static BOOL close_registry_key(pipes_struct *p, POLICY_HND *hnd){ REGISTRY_KEY *regkey = find_regkey_index_by_hnd(p, hnd); if ( !regkey ) { DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd))); return False; } close_policy_hnd(p, hnd); return True;}/******************************************************************** retrieve information about the subkeys *******************************************************************/ static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen ){ int num_subkeys, i; uint32 max_len; REGSUBKEY_CTR *subkeys; uint32 len; if ( !key ) return False; if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) return False; if ( fetch_reg_keys( key, subkeys ) == -1 ) return False; /* find the longest string */ max_len = 0; num_subkeys = regsubkey_ctr_numkeys( subkeys ); for ( i=0; i<num_subkeys; i++ ) { len = strlen( regsubkey_ctr_specific_key(subkeys, i) ); max_len = MAX(max_len, len); } *maxnum = num_subkeys; *maxlen = max_len*2; TALLOC_FREE( subkeys ); return True;}/******************************************************************** retrieve information about the values. *******************************************************************/ static BOOL get_value_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen, uint32 *maxsize ){ REGVAL_CTR *values; REGISTRY_VALUE *val; uint32 sizemax, lenmax; int i, num_values; if ( !key ) return False; if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) return False; if ( fetch_reg_values( key, values ) == -1 ) return False; lenmax = sizemax = 0; num_values = regval_ctr_numvals( values ); val = regval_ctr_specific_value( values, 0 ); for ( i=0; i<num_values && val; i++ ) { lenmax = MAX(lenmax, val->valuename ? strlen(val->valuename)+1 : 0 ); sizemax = MAX(sizemax, val->size ); val = regval_ctr_specific_value( values, i ); } *maxnum = num_values; *maxlen = lenmax; *maxsize = sizemax; TALLOC_FREE( values ); return True;}/******************************************************************** reg_close ********************************************************************/WERROR _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u){ /* close the policy handle */ if (!close_registry_key(p, &q_u->pol)) return WERR_BADFID; return WERR_OK;}/******************************************************************* ********************************************************************/WERROR _reg_open_hklm(pipes_struct *p, REG_Q_OPEN_HIVE *q_u, REG_R_OPEN_HIVE *r_u){ REGISTRY_KEY *keyinfo; return open_registry_key( p, &r_u->pol, &keyinfo, NULL, KEY_HKLM, q_u->access );}/******************************************************************* ********************************************************************/WERROR _reg_open_hkpd(pipes_struct *p, REG_Q_OPEN_HIVE *q_u, REG_R_OPEN_HIVE *r_u){ REGISTRY_KEY *keyinfo; return open_registry_key( p, &r_u->pol, &keyinfo, NULL, KEY_HKPD, q_u->access );}/******************************************************************* ********************************************************************/WERROR _reg_open_hkpt(pipes_struct *p, REG_Q_OPEN_HIVE *q_u, REG_R_OPEN_HIVE *r_u){ REGISTRY_KEY *keyinfo; return open_registry_key( p, &r_u->pol, &keyinfo, NULL, KEY_HKPT, q_u->access );}/******************************************************************* ********************************************************************/WERROR _reg_open_hkcr(pipes_struct *p, REG_Q_OPEN_HIVE *q_u, REG_R_OPEN_HIVE *r_u){ REGISTRY_KEY *keyinfo; return open_registry_key( p, &r_u->pol, &keyinfo, NULL, KEY_HKCR, q_u->access );}/******************************************************************* ********************************************************************/WERROR _reg_open_hku(pipes_struct *p, REG_Q_OPEN_HIVE *q_u, REG_R_OPEN_HIVE *r_u){ REGISTRY_KEY *keyinfo; return open_registry_key( p, &r_u->pol, &keyinfo, NULL, KEY_HKU, q_u->access );}/******************************************************************* reg_reply_open_entry ********************************************************************/WERROR _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u){ fstring name; REGISTRY_KEY *parent = find_regkey_index_by_hnd(p, &q_u->pol); REGISTRY_KEY *newkey = NULL; uint32 check_rights; if ( !parent ) return WERR_BADFID; rpcstr_pull( name, q_u->name.string->buffer, sizeof(name), q_u->name.string->uni_str_len*2, 0 ); /* check granted access first; what is the correct mask here? */ check_rights = ( SEC_RIGHTS_ENUM_SUBKEYS| SEC_RIGHTS_CREATE_SUBKEY| SEC_RIGHTS_QUERY_VALUE| SEC_RIGHTS_SET_VALUE); if ( !(parent->access_granted & check_rights) ) { DEBUG(8,("Rights check failed, parent had %04x, check_rights %04x\n",parent->access_granted, check_rights)); return WERR_ACCESS_DENIED; } /* * very crazy, but regedit.exe on Win2k will attempt to call * REG_OPEN_ENTRY with a keyname of "". We should return a new * (second) handle here on the key->name. regedt32.exe does * not do this stupidity. --jerry */ return open_registry_key( p, &r_u->handle, &newkey, parent, name, q_u->access );}/******************************************************************* reg_reply_info ********************************************************************/WERROR _reg_query_value(pipes_struct *p, REG_Q_QUERY_VALUE *q_u, REG_R_QUERY_VALUE *r_u){ WERROR status = WERR_BADFILE; fstring name; REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol ); REGISTRY_VALUE *val = NULL; REGVAL_CTR *regvals; int i; if ( !regkey ) return WERR_BADFID; DEBUG(7,("_reg_info: policy key name = [%s]\n", regkey->name)); DEBUG(7,("_reg_info: policy key type = [%08x]\n", regkey->type)); rpcstr_pull(name, q_u->name.string->buffer, sizeof(name), q_u->name.string->uni_str_len*2, 0); DEBUG(5,("_reg_info: looking up value: [%s]\n", name)); if ( !(regvals = TALLOC_ZERO_P( p->mem_ctx, REGVAL_CTR )) ) return WERR_NOMEM; /* Handle QueryValue calls on HKEY_PERFORMANCE_DATA */ if(regkey->type == REG_KEY_HKPD) { if(strequal(name, "Global")) { uint32 outbuf_len; prs_struct prs_hkpd; prs_init(&prs_hkpd, q_u->bufsize, p->mem_ctx, MARSHALL); status = reg_perfcount_get_hkpd(&prs_hkpd, q_u->bufsize, &outbuf_len, NULL); regval_ctr_addvalue(regvals, "HKPD", REG_BINARY, prs_hkpd.data_p, outbuf_len); val = dup_registry_value(regval_ctr_specific_value(regvals, 0)); prs_mem_free(&prs_hkpd); } else if(strequal(name, "Counter 009")) { uint32 base_index; uint32 buffer_size; char *buffer; buffer = NULL; base_index = reg_perfcount_get_base_index(); buffer_size = reg_perfcount_get_counter_names(base_index, &buffer); regval_ctr_addvalue(regvals, "Counter 009", REG_MULTI_SZ, buffer, buffer_size); val = dup_registry_value(regval_ctr_specific_value(regvals, 0)); if(buffer_size > 0) { SAFE_FREE(buffer); status = WERR_OK; } } else if(strequal(name, "Explain 009")) { uint32 base_index; uint32 buffer_size; char *buffer; buffer = NULL; base_index = reg_perfcount_get_base_index(); buffer_size = reg_perfcount_get_counter_help(base_index, &buffer); regval_ctr_addvalue(regvals, "Explain 009", REG_MULTI_SZ, buffer, buffer_size); val = dup_registry_value(regval_ctr_specific_value(regvals, 0)); if(buffer_size > 0) { SAFE_FREE(buffer); status = WERR_OK; } } else if(isdigit(name[0])) { /* we probably have a request for a specific object here */ uint32 outbuf_len; prs_struct prs_hkpd; prs_init(&prs_hkpd, q_u->bufsize, p->mem_ctx, MARSHALL); status = reg_perfcount_get_hkpd(&prs_hkpd, q_u->bufsize, &outbuf_len, name); regval_ctr_addvalue(regvals, "HKPD", REG_BINARY, prs_hkpd.data_p, outbuf_len); val = dup_registry_value(regval_ctr_specific_value(regvals, 0)); prs_mem_free(&prs_hkpd); } else { DEBUG(3,("Unsupported key name [%s] for HKPD.\n", name)); return WERR_BADFILE; } } /* HKPT calls can be handled out of reg_dynamic.c with the hkpt_params handler */ else { for ( i=0; fetch_reg_values_specific(regkey, &val, i); i++ ) { DEBUG(10,("_reg_info: Testing value [%s]\n", val->valuename)); if ( strequal( val->valuename, name ) ) { DEBUG(10,("_reg_info: Found match for value [%s]\n", name)); status = WERR_OK; break; } free_registry_value( val ); } } init_reg_r_query_value(q_u->ptr_buf, r_u, val, status); TALLOC_FREE( regvals ); free_registry_value( val ); return status;}/***************************************************************************** Implementation of REG_QUERY_KEY ****************************************************************************/ WERROR _reg_query_key(pipes_struct *p, REG_Q_QUERY_KEY *q_u, REG_R_QUERY_KEY *r_u){ WERROR status = WERR_OK; REGISTRY_KEY *regkey = find_regkey_index_by_hnd( p, &q_u->pol ); if ( !regkey ) return WERR_BADFID; if ( !get_subkey_information( regkey, &r_u->num_subkeys, &r_u->max_subkeylen ) ) { DEBUG(0,("_reg_query_key: get_subkey_information() failed!\n")); return WERR_ACCESS_DENIED; } if ( !get_value_information( regkey, &r_u->num_values, &r_u->max_valnamelen, &r_u->max_valbufsize ) ) { DEBUG(0,("_reg_query_key: get_value_information() failed!\n"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -