2005-08-06 05:09:27 +04:00
/ *
winreg rpc utility functions
Copyright Andrew Tridgell 2005
2008-03-28 09:08:49 +03:00
released under the GNU GPL version 3 or later
2005-08-06 05:09:27 +04:00
* /
2005-08-17 05:25:58 +04:00
libinclude ( "base.js" ) ;
/ *
close a handle
* /
2005-08-23 06:00:09 +04:00
function _ _winreg _close ( handle )
2005-08-17 05:25:58 +04:00
{
var io = irpcObj ( ) ;
io . input . handle = handle ;
2005-08-23 06:00:09 +04:00
this . winreg _CloseKey ( io ) ;
2005-08-17 05:25:58 +04:00
}
2005-08-06 05:09:27 +04:00
/ *
open a hive
* /
2005-08-23 06:00:09 +04:00
function _ _winreg _open _hive ( hive )
2005-08-06 05:09:27 +04:00
{
var io = irpcObj ( ) ;
io . input . system _name = NULL ;
2005-10-25 13:30:48 +04:00
io . input . access _mask = this . SEC _FLAG _MAXIMUM _ALLOWED ;
2005-08-06 05:09:27 +04:00
var status ;
if ( hive == "HKLM" ) {
2005-08-23 06:00:09 +04:00
status = this . winreg _OpenHKLM ( io ) ;
2005-08-06 05:09:27 +04:00
} else if ( hive == "HKCR" ) {
2005-08-23 06:00:09 +04:00
status = this . winreg _OpenHKCR ( io ) ;
2005-08-06 05:09:27 +04:00
} else if ( hive == "HKPD" ) {
2005-08-23 06:00:09 +04:00
status = this . winreg _OpenHKPD ( io ) ;
2005-08-06 05:09:27 +04:00
} else if ( hive == "HKU" ) {
2005-08-23 06:00:09 +04:00
status = this . winreg _OpenHKU ( io ) ;
2005-08-06 05:09:27 +04:00
} else {
2005-08-24 12:32:51 +04:00
this . _last _error = "Unknown hive " + hive ;
2005-08-06 05:09:27 +04:00
return undefined ;
}
if ( ! status . is _ok ) {
return undefined ;
}
return io . output . handle ;
}
/ *
open a handle to a path
* /
2005-08-23 06:00:09 +04:00
function _ _winreg _open _path ( path )
2005-08-06 05:09:27 +04:00
{
var s = string _init ( ) ;
var i , components = s . split ( '\\' , path ) ;
2005-08-09 02:29:44 +04:00
/* cope with a leading slash */
if ( components [ 0 ] == '' ) {
for ( i = 0 ; i < ( components . length - 1 ) ; i ++ ) {
components [ i ] = components [ i + 1 ] ;
}
2005-11-02 04:04:00 +03:00
delete ( components [ i ] ) ;
2005-08-09 02:29:44 +04:00
}
2005-08-06 05:09:27 +04:00
2005-08-09 02:29:44 +04:00
if ( components . length == 0 ) {
return undefined ;
}
2005-08-23 06:00:09 +04:00
var handle = this . open _hive ( components [ 0 ] ) ;
2005-08-06 05:09:27 +04:00
if ( handle == undefined ) {
return undefined ;
}
2005-08-09 02:29:44 +04:00
if ( components . length == 1 ) {
return handle ;
}
2005-08-06 05:09:27 +04:00
2005-08-09 02:29:44 +04:00
var hpath = components [ 1 ] ;
for ( i = 2 ; i < components . length ; i ++ ) {
hpath = hpath + "\\" + components [ i ] ;
2005-08-06 05:09:27 +04:00
}
2005-08-09 02:29:44 +04:00
io = irpcObj ( ) ;
2006-09-16 00:55:43 +04:00
io . input . parent _handle = handle ;
2005-08-09 02:29:44 +04:00
io . input . keyname = hpath ;
io . input . unknown = 0 ;
2005-08-23 06:00:09 +04:00
io . input . access _mask = this . SEC _FLAG _MAXIMUM _ALLOWED ;
var status = this . winreg _OpenKey ( io ) ;
2005-08-17 05:25:58 +04:00
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 05:25:58 +04:00
2005-08-09 02:29:44 +04:00
if ( ! status . is _ok ) {
return undefined ;
}
if ( io . output . result != "WERR_OK" ) {
return undefined ;
}
return io . output . handle ;
2005-08-06 05:09:27 +04:00
}
/ *
return a list of keys for a winreg server given a path
usage :
2005-08-23 06:00:09 +04:00
list = reg . enum _path ( path ) ;
2005-08-06 05:09:27 +04:00
* /
2005-08-23 06:00:09 +04:00
function _ _winreg _enum _path ( path )
2005-08-06 05:09:27 +04:00
{
2005-11-02 04:04:00 +03:00
var list = new Array ( 0 ) ;
2005-08-09 02:29:44 +04:00
if ( path == null || path == "\\" || path == "" ) {
return new Array ( "HKLM" , "HKU" ) ;
}
2005-08-06 05:09:27 +04:00
2005-08-23 06:00:09 +04:00
var handle = this . open _path ( path ) ;
2005-08-06 05:09:27 +04:00
if ( handle == undefined ) {
return undefined ;
}
var io = irpcObj ( ) ;
io . input . handle = handle ;
2005-08-17 05:25:58 +04:00
io . input . name = new Object ( ) ;
io . input . name . length = 0 ;
io . input . name . size = 32 ;
io . input . name . name = NULL ;
2006-09-16 00:55:43 +04:00
io . input . keyclass = new Object ( ) ;
io . input . keyclass . length = 0 ;
io . input . keyclass . size = 1024 ;
io . input . keyclass . name = NULL ;
2005-08-17 05:25:58 +04:00
io . input . last _changed _time = 0 ;
2005-08-06 05:09:27 +04:00
var idx = 0 ;
for ( idx = 0 ; idx >= 0 ; idx ++ ) {
2005-08-17 05:25:58 +04:00
io . input . enum _index = idx ;
2005-08-23 06:00:09 +04:00
var status = this . winreg _EnumKey ( io ) ;
2005-08-17 05:25:58 +04:00
if ( ! status . is _ok ) {
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 16:27:28 +04:00
return list ;
2005-08-17 05:25:58 +04:00
}
var out = io . output ;
if ( out . result == "WERR_MORE_DATA" ) {
io . input . name . size = io . input . name . size * 2 ;
idx -- ;
if ( io . input . name . size > 32000 ) {
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 16:27:28 +04:00
return list ;
2005-08-17 05:25:58 +04:00
}
continue ;
}
if ( out . result != "WERR_OK" ) {
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 05:25:58 +04:00
return list ;
}
list [ list . length ] = out . name . name ;
}
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 05:25:58 +04:00
return list ;
}
/ *
return a list of values for a winreg server given a path
usage :
2005-08-23 06:00:09 +04:00
list = reg . enum _values ( path ) ;
2005-08-17 05:25:58 +04:00
each returned list element is an object containing a name , a
type and a value
* /
2005-08-23 06:00:09 +04:00
function _ _winreg _enum _values ( path )
2005-08-17 05:25:58 +04:00
{
2005-08-23 06:00:09 +04:00
var data = datablob _init ( ) ;
2005-11-02 04:04:00 +03:00
var list = new Array ( 0 ) ;
2005-08-17 05:25:58 +04:00
2005-08-23 06:00:09 +04:00
var handle = this . open _path ( path ) ;
2005-08-17 05:25:58 +04:00
if ( handle == undefined ) {
return undefined ;
}
var io = irpcObj ( ) ;
io . input . handle = handle ;
io . input . name = new Object ( ) ;
io . input . name . length = 0 ;
io . input . name . size = 128 ;
io . input . name . name = "" ;
io . input . type = 0 ;
io . input . value = new Array ( 0 ) ;
io . input . size = 1024 ;
io . input . length = 0 ;
var idx ;
for ( idx = 0 ; idx >= 0 ; idx ++ ) {
io . input . enum _index = idx ;
2005-08-23 06:00:09 +04:00
var status = this . winreg _EnumValue ( io ) ;
2005-08-17 05:25:58 +04:00
if ( ! status . is _ok ) {
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 16:27:28 +04:00
return list ;
2005-08-17 05:25:58 +04:00
}
2005-08-06 05:09:27 +04:00
var out = io . output ;
2005-08-17 05:25:58 +04:00
if ( out . result == "WERR_MORE_DATA" ) {
io . input . size = io . input . size * 2 ;
io . input . name . size = io . input . name . size * 2 ;
idx -- ;
/* limit blobs to 1M */
if ( io . input . size > 1000000 ) {
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-17 16:27:28 +04:00
return list ;
2005-08-17 05:25:58 +04:00
}
continue ;
}
2005-08-06 05:09:27 +04:00
if ( out . result != "WERR_OK" ) {
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-06 05:09:27 +04:00
return list ;
}
2005-08-17 05:25:58 +04:00
var el = new Object ( ) ;
el . name = out . name . name ;
el . type = out . type ;
2005-08-23 06:00:09 +04:00
el . rawvalue = out . value ;
el . value = data . regToVar ( el . rawvalue , el . type ) ;
2005-08-17 05:25:58 +04:00
el . size = out . size ;
list [ list . length ] = el ;
2005-08-06 05:09:27 +04:00
}
2005-08-23 06:00:09 +04:00
this . close ( handle ) ;
2005-08-06 05:09:27 +04:00
return list ;
}
2005-08-23 06:00:09 +04:00
2005-08-24 12:32:51 +04:00
/ *
create a new key
ok = reg . create _key ( path , key ) ;
* /
function _ _winreg _create _key ( path , key )
{
var handle = this . open _path ( path ) ;
if ( handle == undefined ) {
return undefined ;
}
var io = irpcObj ( ) ;
io . input . handle = handle ;
io . input . name = key ;
2006-09-16 00:55:43 +04:00
io . input . keyclass = NULL ;
2005-08-24 12:32:51 +04:00
io . input . options = 0 ;
2005-10-25 13:30:48 +04:00
io . input . access _mask = this . SEC _FLAG _MAXIMUM _ALLOWED ;
2005-08-24 12:32:51 +04:00
io . input . secdesc = NULL ;
io . input . action _taken = 0 ;
var status = this . winreg _CreateKey ( io ) ;
this . close ( handle ) ;
if ( ! status . is _ok ) {
return false ;
}
if ( io . output . result != "WERR_OK" ) {
return false ;
}
this . close ( io . output . new _handle ) ;
return true ;
}
2005-08-23 06:00:09 +04:00
/ *
return a string for a winreg type
* /
function _ _winreg _typestring ( type )
{
return this . typenames [ type ] ;
}
/ *
initialise the winreg lib , returning an object
* /
function winregObj ( )
{
var reg = winreg _init ( ) ;
security _init ( reg ) ;
reg . typenames = new Array ( "REG_NONE" , "REG_SZ" , "REG_EXPAND_SZ" , "REG_BINARY" ,
"REG_DWORD" , "REG_DWORD_BIG_ENDIAN" , "REG_LINK" , "REG_MULTI_SZ" ,
"REG_RESOURCE_LIST" , "REG_FULL_RESOURCE_DESCRIPTOR" ,
"REG_RESOURCE_REQUIREMENTS_LIST" , "REG_QWORD" ) ;
2005-08-24 12:32:51 +04:00
reg . close = _ _winreg _close ;
reg . open _hive = _ _winreg _open _hive ;
reg . open _path = _ _winreg _open _path ;
reg . enum _path = _ _winreg _enum _path ;
2005-08-23 06:00:09 +04:00
reg . enum _values = _ _winreg _enum _values ;
2005-08-24 12:32:51 +04:00
reg . create _key = _ _winreg _create _key ;
reg . typestring = _ _winreg _typestring ;
2005-08-23 06:00:09 +04:00
return reg ;
}