static VALUE typelib_file_from_typelib(VALUE ole) { HKEY htypelib, hclsid, hversion, hlang; double fver; DWORD i, j, k; LONG err; BOOL found = FALSE; VALUE typelib; VALUE file = Qnil; VALUE clsid; VALUE ver; VALUE lang; err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", &htypelib); if(err != ERROR_SUCCESS) { return Qnil; } for(i = 0; !found; i++) { clsid = reg_enum_key(htypelib, i); if (clsid == Qnil) break; err = reg_open_vkey(htypelib, clsid, &hclsid); if (err != ERROR_SUCCESS) continue; fver = 0; for(j = 0; !found; j++) { ver = reg_enum_key(hclsid, j); if (ver == Qnil) break; err = reg_open_vkey(hclsid, ver, &hversion); if (err != ERROR_SUCCESS || fver > atof(StringValuePtr(ver))) continue; fver = atof(StringValuePtr(ver)); typelib = reg_get_val(hversion, NULL); if (typelib == Qnil) continue; if (rb_str_cmp(typelib, ole) == 0) { for(k = 0; !found; k++) { lang = reg_enum_key(hversion, k); if (lang == Qnil) break; err = reg_open_vkey(hversion, lang, &hlang); if (err == ERROR_SUCCESS) { if ((file = reg_get_typelib_file_path(hlang)) != Qnil) found = TRUE; RegCloseKey(hlang); } } } RegCloseKey(hversion); } RegCloseKey(hclsid); } RegCloseKey(htypelib); return file; }
result_t bsp_can_init(handle_t rx_queue) { result_t result; memid_t key; if(failed(result = reg_open_key(0, "slcan", &key))) return result; return slcan_create(key, rx_queue, &driver); }
static VALUE oletypelib_search_registry(VALUE self, VALUE typelib) { HKEY htypelib, hguid, hversion; DWORD i, j; LONG err; VALUE found = Qfalse; VALUE tlib; VALUE guid; VALUE ver; HRESULT hr; ITypeLib *pTypeLib; err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", &htypelib); if(err != ERROR_SUCCESS) { return Qfalse; } for(i = 0; !found; i++) { guid = reg_enum_key(htypelib, i); if (guid == Qnil) break; err = reg_open_vkey(htypelib, guid, &hguid); if (err != ERROR_SUCCESS) continue; for(j = 0; found == Qfalse; j++) { ver = reg_enum_key(hguid, j); if (ver == Qnil) break; err = reg_open_vkey(hguid, ver, &hversion); if (err != ERROR_SUCCESS) continue; tlib = reg_get_val(hversion, NULL); if (tlib == Qnil) { RegCloseKey(hversion); continue; } if (rb_str_cmp(typelib, tlib) == 0) { hr = oletypelib_from_guid(guid, ver, &pTypeLib); if (SUCCEEDED(hr)) { oletypelib_set_member(self, pTypeLib); found = Qtrue; } } RegCloseKey(hversion); } RegCloseKey(hguid); } RegCloseKey(htypelib); return found; }
static std::wstring improve_name_from_base_path(const std::wstring ®path, bool *hid) { // now try to open the registry key auto device_key = reg_open_key(HKEY_LOCAL_MACHINE, regpath); if (!device_key.valid()) return std::wstring(); // fetch the device description; if it exists, we are finished auto regstring = reg_query_string(device_key, L"DeviceDesc"); if (!regstring.empty()) return trim_prefix(regstring); // if the key name does not contain "HID", it's not going to be in the USB tree; give up *hid = regpath.find(L"HID") != std::string::npos; return std::wstring(); }
static void foreach_subkey(HKEY key, std::function<bool(HKEY)> action) { for (int i = 0; ; i++) { std::wstring name = reg_enum_key(key, i); if (name.empty()) break; safe_regkey subkey = reg_open_key(key, name); if (!subkey.valid()) break; bool shouldcontinue = action(subkey); if (!shouldcontinue) break; } }
/* * call-seq: * * WIN32OLE_TYPELIB.typelibs * * Returns the array of WIN32OLE_TYPELIB object. * * tlibs = WIN32OLE_TYPELIB.typelibs * */ static VALUE foletypelib_s_typelibs(VALUE self) { HKEY htypelib, hguid; DWORD i, j; LONG err; VALUE guid; VALUE version; VALUE name = Qnil; VALUE typelibs = rb_ary_new(); VALUE typelib = Qnil; HRESULT hr; ITypeLib *pTypeLib; err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", &htypelib); if(err != ERROR_SUCCESS) { return typelibs; } for(i = 0; ; i++) { guid = reg_enum_key(htypelib, i); if (guid == Qnil) break; err = reg_open_vkey(htypelib, guid, &hguid); if (err != ERROR_SUCCESS) continue; for(j = 0; ; j++) { version = reg_enum_key(hguid, j); if (version == Qnil) break; if ( (name = reg_get_val2(hguid, StringValuePtr(version))) != Qnil ) { hr = oletypelib_from_guid(guid, version, &pTypeLib); if (SUCCEEDED(hr)) { typelib = create_win32ole_typelib(pTypeLib); rb_ary_push(typelibs, typelib); } } } RegCloseKey(hguid); } RegCloseKey(htypelib); return typelibs; }
static std::wstring improve_name_from_usb_path(const std::wstring ®path) { static const std::wstring usbbasepath(L"SYSTEM\\CurrentControlSet\\Enum\\USB"); // extract the expected parent ID from the regpath size_t last_slash_index = regpath.find_last_of('\\'); if (last_slash_index == std::wstring::npos) return std::wstring(); std::wstring parentid = regpath.substr(last_slash_index + 1); // open the USB key auto usb_key = reg_open_key(HKEY_LOCAL_MACHINE, usbbasepath); if (!usb_key.valid()) return std::wstring(); std::wstring regstring; foreach_subkey(usb_key, [®string, &parentid](HKEY subkey) { foreach_subkey(subkey, [®string, &parentid](HKEY endkey) { std::wstring endparentid = reg_query_string(endkey, L"ParentIdPrefix"); // This key doesn't have a ParentIdPrefix if (endparentid.empty()) return true; // do we have a match? if (parentid.find(endparentid) == 0) regstring = reg_query_string(endkey, L"DeviceDesc"); return regstring.empty(); }); return regstring.empty(); }); return trim_prefix(regstring); }
int reg_set_value(const HKEY base, const char *keypath, const char *newvalue) { HKEY key; DWORD dwType, dwSize; int len; char *val, *path, buffer[1024] = { '\0' }; strncpy(buffer, keypath, 1024); if ( !(val = strrchr(buffer, '\\')) ) val = strrchr(buffer, '/'); if (!*++val) val = NULL; key = reg_open_key(base, keypath, KEY_WRITE); if (RegSetValue(key, val, REG_SZ, newvalue, strlen(newvalue)) != ERROR_SUCCESS) { perror("RegSetValue"); RegCloseKey(key); return errno; } RegCloseKey(key); return 0; }
/* winreg_OpenKey */ static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_OpenKey *r) { struct dcesrv_handle *h, *newh; struct registry_key *key; WERROR result; DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY); key = h->data; switch (security_session_user_level(dce_call->conn->auth_state.session_info)) { case SECURITY_SYSTEM: case SECURITY_ADMINISTRATOR: case SECURITY_USER: if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) { newh = talloc_reference(dce_call->context, h); result = WERR_OK; } else { newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY); result = reg_open_key(newh, key, r->in.keyname.name, (struct registry_key **)&newh->data); } if (W_ERROR_IS_OK(result)) { r->out.handle = &newh->wire_handle; } else { talloc_free(newh); } return result; default: return WERR_ACCESS_DENIED; } }
/** * Print a registry key recursively * * @param level Level at which to print * @param p Key to print * @param fullpath Whether the full pat hshould be printed or just the last bit * @param novals Whether values should not be printed */ static void print_tree(unsigned int level, struct registry_key *p, const char *name, bool fullpath, bool novals) { struct registry_key *subkey; const char *valuename, *keyname; uint32_t valuetype; DATA_BLOB valuedata; struct security_descriptor *sec_desc; WERROR error; unsigned int i; TALLOC_CTX *mem_ctx; for(i = 0; i < level; i++) putchar(' '); puts(name); mem_ctx = talloc_init("print_tree"); for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, p, i, &keyname, NULL, NULL)); i++) { SMB_ASSERT(strlen(keyname) > 0); if (!W_ERROR_IS_OK(reg_open_key(mem_ctx, p, keyname, &subkey))) continue; print_tree(level+1, subkey, (fullpath && strlen(name))? talloc_asprintf(mem_ctx, "%s\\%s", name, keyname): keyname, fullpath, novals); talloc_free(subkey); } talloc_free(mem_ctx); if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { DEBUG(0, ("Error occurred while fetching subkeys for '%s': %s\n", name, win_errstr(error))); } if (!novals) { mem_ctx = talloc_init("print_tree"); for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index( mem_ctx, p, i, &valuename, &valuetype, &valuedata)); i++) { unsigned int j; for(j = 0; j < level+1; j++) putchar(' '); printf("%s\n", reg_val_description(mem_ctx, valuename, valuetype, valuedata)); } talloc_free(mem_ctx); if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { DEBUG(0, ("Error occurred while fetching values for '%s': %s\n", name, win_errstr(error))); } } mem_ctx = talloc_init("sec_desc"); if (!W_ERROR_IS_OK(reg_get_sec_desc(mem_ctx, p, &sec_desc))) { DEBUG(0, ("Error getting security descriptor\n")); } talloc_free(mem_ctx); }
NTSTATUS DriverEntry( IN PDRIVER_OBJECT drv_obj, IN PUNICODE_STRING reg_pth ) { // PIMAGE_DOS_HEADER d_head; // PIMAGE_NT_HEADERS n_head; NTSTATUS status = STATUS_UNSUCCESSFUL; HANDLE h_key = NULL; // u32 tstamp; do { if ( (h_key = reg_open_key(reg_pth->Buffer, NULL)) == NULL ) { DbgMsg("can not open service key\n"); break; } /* get ntoskrnl base */ if ( (ntkrn_base = get_kernel_base()) == NULL ) { DbgMsg("kernel base not found\n"); break; } DbgMsg("kernel base: %x\n", ntkrn_base); /* get ntoskrnl timestamp */ /* turned off because of the futility: dbghelp do this stuf if (reg_query_val(h_key, L"sym_timestamp", &tstamp, sizeof(tstamp)) == 0) { DbgMsg("unable to get ntoskrnl timestamp\n"); break; } */ /* validate ntoskrnl timestamp */ /* d_head = ntkrn_base; n_head = addof(d_head, d_head->e_lfanew); if (n_head->FileHeader.TimeDateStamp != tstamp) { DbgMsg("invalid ntoskrnl timestamp\n"); break; } */ if (init_hook_list() == 0){ DbgMsg("init_hook_list failed\n"); break; } if (init_dbg_item(h_key) == 0) { DbgMsg("init_dbg_item failed\n"); break; } if (init_debug(h_key) == 0) { DbgMsg("init_debug failed\n"); break; } if (init_syscall(h_key) == 0) { DbgMsg("init_syscall failed\n"); break; } DbgMsg("dbgapi initialized\n"); status = STATUS_SUCCESS; } while (0); if (h_key != NULL) { ZwClose(h_key); } return status; }
/* * Generate difference between two keys */ WERROR reg_generate_diff_key(struct registry_key *oldkey, struct registry_key *newkey, const char *path, const struct reg_diff_callbacks *callbacks, void *callback_data) { int i; struct registry_key *t1 = NULL, *t2 = NULL; char *tmppath; const char *keyname1; WERROR error, error1, error2; TALLOC_CTX *mem_ctx = talloc_init("writediff"); uint32_t old_num_subkeys, old_num_values, new_num_subkeys, new_num_values; if (oldkey != NULL) { error = reg_key_get_info(mem_ctx, oldkey, NULL, &old_num_subkeys, &old_num_values, NULL, NULL, NULL, NULL); if (!W_ERROR_IS_OK(error)) { DEBUG(0, ("Error occurred while getting key info: %s\n", win_errstr(error))); talloc_free(mem_ctx); return error; } } else { old_num_subkeys = 0; old_num_values = 0; } /* Subkeys that were changed or deleted */ for (i = 0; i < old_num_subkeys; i++) { error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &keyname1, NULL, NULL); if (!W_ERROR_IS_OK(error1)) { DEBUG(0, ("Error occurred while getting subkey by index: %s\n", win_errstr(error1))); continue; } if (newkey != NULL) { error2 = reg_open_key(mem_ctx, newkey, keyname1, &t2); } else { error2 = WERR_BADFILE; t2 = NULL; } if (!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_BADFILE)) { DEBUG(0, ("Error occured while getting subkey by name: %s\n", win_errstr(error2))); talloc_free(mem_ctx); return error2; } /* if "error2" is going to be "WERR_BADFILE", then newkey */ /* didn't have such a subkey and therefore add a del diff */ tmppath = talloc_asprintf(mem_ctx, "%s\\%s", path, keyname1); if (!W_ERROR_IS_OK(error2)) callbacks->del_key(callback_data, tmppath); /* perform here also the recursive invocation */ error1 = reg_open_key(mem_ctx, oldkey, keyname1, &t1); if (!W_ERROR_IS_OK(error1)) { DEBUG(0, ("Error occured while getting subkey by name: %s\n", win_errstr(error1))); talloc_free(mem_ctx); return error1; } reg_generate_diff_key(t1, t2, tmppath, callbacks, callback_data); talloc_free(tmppath); } if (newkey != NULL) { error = reg_key_get_info(mem_ctx, newkey, NULL, &new_num_subkeys, &new_num_values, NULL, NULL, NULL, NULL); if (!W_ERROR_IS_OK(error)) { DEBUG(0, ("Error occurred while getting key info: %s\n", win_errstr(error))); talloc_free(mem_ctx); return error; } } else { new_num_subkeys = 0; new_num_values = 0; } /* Subkeys that were added */ for(i = 0; i < new_num_subkeys; i++) { error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &keyname1, NULL, NULL); if (!W_ERROR_IS_OK(error1)) { DEBUG(0, ("Error occurred while getting subkey by index: %s\n", win_errstr(error1))); talloc_free(mem_ctx); return error1; } if (oldkey != NULL) { error2 = reg_open_key(mem_ctx, oldkey, keyname1, &t1); if (W_ERROR_IS_OK(error2)) continue; } else { error2 = WERR_BADFILE; t1 = NULL; } if (!W_ERROR_EQUAL(error2, WERR_BADFILE)) { DEBUG(0, ("Error occurred while getting subkey by name: %s\n", win_errstr(error2))); talloc_free(mem_ctx); return error2; } /* oldkey didn't have such a subkey, add add diff */ tmppath = talloc_asprintf(mem_ctx, "%s\\%s", path, keyname1); callbacks->add_key(callback_data, tmppath); /* perform here also the recursive invocation */ error1 = reg_open_key(mem_ctx, newkey, keyname1, &t2); if (!W_ERROR_IS_OK(error1)) { DEBUG(0, ("Error occured while getting subkey by name: %s\n", win_errstr(error1))); talloc_free(mem_ctx); return error1; } reg_generate_diff_key(t1, t2, tmppath, callbacks, callback_data); talloc_free(tmppath); } /* Values that were added or changed */ for(i = 0; i < new_num_values; i++) { const char *name; uint32_t type1, type2; DATA_BLOB contents1, contents2; error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &name, &type1, &contents1); if (!W_ERROR_IS_OK(error1)) { DEBUG(0, ("Unable to get value by index: %s\n", win_errstr(error1))); talloc_free(mem_ctx); return error1; } if (oldkey != NULL) { error2 = reg_key_get_value_by_name(mem_ctx, oldkey, name, &type2, &contents2); } else error2 = WERR_BADFILE; if (!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_BADFILE)) { DEBUG(0, ("Error occurred while getting value by name: %s\n", win_errstr(error2))); talloc_free(mem_ctx); return error2; } if (W_ERROR_IS_OK(error2) && (data_blob_cmp(&contents1, &contents2) == 0) && (type1 == type2)) continue; callbacks->set_value(callback_data, path, name, type1, contents1); } /* Values that were deleted */ for (i = 0; i < old_num_values; i++) { const char *name; uint32_t type; DATA_BLOB contents; error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &name, &type, &contents); if (!W_ERROR_IS_OK(error1)) { DEBUG(0, ("Unable to get value by index: %s\n", win_errstr(error1))); talloc_free(mem_ctx); return error1; } if (newkey != NULL) error2 = reg_key_get_value_by_name(mem_ctx, newkey, name, &type, &contents); else error2 = WERR_BADFILE; if (W_ERROR_IS_OK(error2)) continue; if (!W_ERROR_EQUAL(error2, WERR_BADFILE)) { DEBUG(0, ("Error occurred while getting value by name: %s\n", win_errstr(error2))); talloc_free(mem_ctx); return error2; } callbacks->del_value(callback_data, path, name); } talloc_free(mem_ctx); return WERR_OK; }
static VALUE oletypelib_search_registry2(VALUE self, VALUE args) { HKEY htypelib, hguid, hversion; double fver; DWORD j; LONG err; VALUE found = Qfalse; VALUE tlib; VALUE ver; VALUE version_str; VALUE version = Qnil; VALUE typelib = Qnil; HRESULT hr; ITypeLib *pTypeLib; VALUE guid = rb_ary_entry(args, 0); version_str = make_version_str(rb_ary_entry(args, 1), rb_ary_entry(args, 2)); err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", &htypelib); if(err != ERROR_SUCCESS) { return Qfalse; } err = reg_open_vkey(htypelib, guid, &hguid); if (err != ERROR_SUCCESS) { RegCloseKey(htypelib); return Qfalse; } if (version_str != Qnil) { err = reg_open_vkey(hguid, version_str, &hversion); if (err == ERROR_SUCCESS) { tlib = reg_get_val(hversion, NULL); if (tlib != Qnil) { typelib = tlib; version = version_str; } } RegCloseKey(hversion); } else { fver = 0.0; for(j = 0; ;j++) { ver = reg_enum_key(hguid, j); if (ver == Qnil) break; err = reg_open_vkey(hguid, ver, &hversion); if (err != ERROR_SUCCESS) continue; tlib = reg_get_val(hversion, NULL); if (tlib == Qnil) { RegCloseKey(hversion); continue; } if (fver < atof(StringValuePtr(ver))) { fver = atof(StringValuePtr(ver)); version = ver; typelib = tlib; } RegCloseKey(hversion); } } RegCloseKey(hguid); RegCloseKey(htypelib); if (typelib != Qnil) { hr = oletypelib_from_guid(guid, version, &pTypeLib); if (SUCCEEDED(hr)) { found = Qtrue; oletypelib_set_member(self, pTypeLib); } } return found; }