예제 #1
0
string ConvertNameToLong(string_view const Object)
{
	string strDest;
	if (!os::fs::GetLongPathName(null_terminated(Object).c_str(), strDest))
	{
		assign(strDest, Object);

		if (!HasPathPrefix(Object))
		{
			if (os::fs::GetLongPathName(NTPath(Object), strDest))
			{
				switch (ParsePath(strDest))
				{
				case root_type::unc_drive_letter:
					strDest.erase(0, 4); // \\?\X:\path -> X:\path
					break;

				case root_type::unc_remote:
					strDest.erase(2, 6); // \\?\UNC\server -> \\server
					break;

				default:
					// should never happen
					break;
				}
			}
		}
	}
	return strDest;
}
예제 #2
0
size_t GetMountPointLen(string_view const abs_path, string_view const drive_root)
{
	if (starts_with_icase(abs_path, drive_root))
		return drive_root.size();

	size_t dir_offset = 0;
	if (ParsePath(abs_path, &dir_offset) == root_type::volume)
		return dir_offset;

	string vol_guid(drive_root);
	switch (ParsePath(drive_root))
	{
	case root_type::volume:
		break;
	case root_type::drive_letter:
		if (os::fs::GetVolumeNameForVolumeMountPoint(null_terminated(drive_root).c_str(), vol_guid))
			break;
		[[fallthrough]];
	default:
		return 0;
	}

	string mount_point = TryConvertVolumeGuidToDrivePath(vol_guid, abs_path);
	return mount_point.size();
}
예제 #3
0
int DialogBuilder::AddTextWrap(const wchar_t *text, bool center, int width)
{
	int LineCount = 0;
	for(const auto& i: wrapped_text(string_view(text), width <= 0? ScrX - 1 - 10 : width))
	{
		const auto Text = AddText(null_terminated(i).c_str());
		Text->Flags = center? DIF_CENTERTEXT : 0;
		++LineCount;
	}

	return LineCount;
}
예제 #4
0
/* ARGSUSED */
static int
unload_soft_module(dev_t dev, caddr_t arg, int mode, int *rval)
{
	crypto_unload_soft_module_t unload_soft_module;
	char *name;
	uint32_t rv;
	int error = 0;

	if (copyin(arg, &unload_soft_module,
	    sizeof (unload_soft_module)) != 0) {
		error = EFAULT;
		goto out2;
	}

	name = unload_soft_module.sm_name;
	/* make sure the name is null terminated */
	if (!null_terminated(name)) {
		unload_soft_module.sm_return_value = CRYPTO_ARGUMENTS_BAD;
		if (copyout(&unload_soft_module, arg,
		    sizeof (unload_soft_module)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	rv = crypto_unload_soft_module(name);
out:
	unload_soft_module.sm_return_value = rv;

	if (copyout(&unload_soft_module, arg,
	    sizeof (unload_soft_module)) != 0) {
		error = EFAULT;
	}
out2:
	if (audit_active)
		audit_cryptoadm(CRYPTO_UNLOAD_SOFT_MODULE, name, NULL, 0, 0,
		    rv, error);

	return (error);
}
예제 #5
0
/* ARGSUSED */
static int
load_soft_config(dev_t dev, caddr_t arg, int mode, int *rval)
{
	crypto_load_soft_config_t soft_config;
	crypto_mech_name_t *entries;
	size_t size;
	uint_t count;
	ulong_t offset;
	char *name;
	uint32_t rv;
	int error = 0;

	if (copyin(arg, &soft_config, sizeof (soft_config)) != 0) {
		error = EFAULT;
		goto out2;
	}

	name = soft_config.sc_name;
	/* make sure the name is null terminated */
	if (!null_terminated(name)) {
		soft_config.sc_return_value = CRYPTO_ARGUMENTS_BAD;
		if (copyout(&soft_config, arg, sizeof (soft_config)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	count = soft_config.sc_count;
	if (count == 0) {
		if (crypto_load_soft_config(name, 0, NULL) != 0) {
			rv = CRYPTO_FAILED;
		} else {
			rv = CRYPTO_SUCCESS;
		}
		goto out;
	}

	if (count > KCF_MAXMECHS) {
		rv = CRYPTO_ARGUMENTS_BAD;
		goto out;
	}

	size = count * sizeof (crypto_mech_name_t);
	entries = kmem_alloc(size, KM_SLEEP);

	offset = offsetof(crypto_load_soft_config_t, sc_list);
	if (copyin(arg + offset, entries, size) != 0) {
		kmem_free(entries, size);
		error = EFAULT;
		goto out2;
	}

	/*
	 * 'entries' is consumed (but not freed) by
	 * crypto_load_soft_config()
	 */
	if (crypto_load_soft_config(name, count, entries) != 0) {
		kmem_free(entries, size);
		rv = CRYPTO_FAILED;
		goto out;
	}
	rv = CRYPTO_SUCCESS;
out:
	soft_config.sc_return_value = rv;

	if (copyout(&soft_config, arg, sizeof (soft_config)) != 0) {
		error = EFAULT;
	}
out2:
	if (audit_active)
		audit_cryptoadm(CRYPTO_LOAD_SOFT_CONFIG, name, entries, count,
		    0, rv, error);
	return (error);
}
예제 #6
0
/* ARGSUSED */
static int
load_dev_disabled(dev_t dev, caddr_t arg, int mode, int *rval)
{
	crypto_load_dev_disabled_t dev_disabled;
	crypto_mech_name_t *entries;
	size_t size;
	ulong_t offset;
	uint_t count;
	uint_t instance;
	char *dev_name;
	uint32_t rv;
	int error = 0;

	if (copyin(arg, &dev_disabled, sizeof (dev_disabled)) != 0) {
		error =  EFAULT;
		goto out2;
	}

	dev_name = dev_disabled.dd_dev_name;
	/* make sure the device name is null terminated */
	if (!null_terminated(dev_name)) {
		rv = CRYPTO_ARGUMENTS_BAD;
		goto out;
	}

	count = dev_disabled.dd_count;
	instance = dev_disabled.dd_dev_instance;
	if (count == 0) {
		/* remove the entry */
		if (crypto_load_dev_disabled(dev_name, instance, 0, NULL) != 0)
			rv = CRYPTO_FAILED;
		else
			rv = CRYPTO_SUCCESS;
		goto out;
	}

	if (count > KCF_MAXMECHS) {
		rv = CRYPTO_ARGUMENTS_BAD;
		goto out;
	}

	size = count * sizeof (crypto_mech_name_t);
	entries = kmem_alloc(size, KM_SLEEP);

	offset = offsetof(crypto_load_dev_disabled_t, dd_list);
	if (copyin(arg + offset, entries, size) != 0) {
		kmem_free(entries, size);
		error = EFAULT;
		goto out2;
	}

	/* 'entries' consumed (but not freed) by crypto_load_dev_disabled() */
	if (crypto_load_dev_disabled(dev_name, instance, count, entries) != 0) {
		kmem_free(entries, size);
		rv = CRYPTO_FAILED;
		goto out;
	}
	rv = CRYPTO_SUCCESS;
out:
	dev_disabled.dd_return_value = rv;

	if (copyout(&dev_disabled, arg, sizeof (dev_disabled)) != 0) {
		error = EFAULT;
	}
out2:
	if (audit_active)
		audit_cryptoadm(CRYPTO_LOAD_DEV_DISABLED, dev_name, entries,
		    count, instance, rv, error);
	return (error);
}
예제 #7
0
/* ARGSUSED */
static int
get_soft_info(dev_t dev, caddr_t arg, int mode, int *rval)
{
	crypto_get_soft_info_t soft_info;
	crypto_mech_name_t *entries;
	size_t copyout_size;
	uint_t count;
	ulong_t offset;
	char *name;

	if (copyin(arg, &soft_info, sizeof (soft_info)) != 0)
		return (EFAULT);

	name = soft_info.si_name;
	/* make sure the provider name is null terminated */
	if (!null_terminated(name)) {
		soft_info.si_return_value = CRYPTO_ARGUMENTS_BAD;
		if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	/* get mechanism names from the core module */
	if (crypto_get_soft_info(name, &count, &entries) != 0) {
		soft_info.si_return_value = CRYPTO_FAILED;
		if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	/* check if buffer is too small */
	if (count > soft_info.si_count) {
		soft_info.si_count = count;
		soft_info.si_return_value = CRYPTO_BUFFER_TOO_SMALL;
		crypto_free_mech_list(entries, count);
		if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	soft_info.si_count = count;
	soft_info.si_return_value = CRYPTO_SUCCESS;
	copyout_size = count * sizeof (crypto_mech_name_t);

	/* copyout the first stuff */
	if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
		crypto_free_mech_list(entries, count);
		return (EFAULT);
	}

	/* copyout entries */
	offset = offsetof(crypto_get_soft_info_t, si_list);
	if (copyout(entries, arg + offset, copyout_size) != 0) {
		crypto_free_mech_list(entries, count);
		return (EFAULT);
	}
	crypto_free_mech_list(entries, count);
	return (0);
}
예제 #8
0
/* ARGSUSED */
static int
get_dev_info(dev_t dev, caddr_t arg, int mode, int *rval)
{
	crypto_get_dev_info_t dev_info;
	crypto_mech_name_t *entries;
	size_t copyout_size;
	uint_t count;
	ulong_t offset;
	char *dev_name;
	int rv;

	if (copyin(arg, &dev_info, sizeof (dev_info)) != 0)
		return (EFAULT);

	dev_name = dev_info.di_dev_name;
	/* make sure the device name is null terminated */
	if (!null_terminated(dev_name)) {
		dev_info.di_return_value = CRYPTO_ARGUMENTS_BAD;
		if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	/* get mechanism names from the core module */
	if ((rv = crypto_get_dev_info(dev_name, dev_info.di_dev_instance,
	    &count, &entries)) != CRYPTO_SUCCESS) {
		dev_info.di_return_value = rv;
		if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	/* check if buffer is too small */
	if (count > dev_info.di_count) {
		dev_info.di_count = count;
		dev_info.di_return_value = CRYPTO_BUFFER_TOO_SMALL;
		crypto_free_mech_list(entries, count);
		if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
			return (EFAULT);
		}
		return (0);
	}

	dev_info.di_count = count;
	dev_info.di_return_value = CRYPTO_SUCCESS;

	copyout_size = count * sizeof (crypto_mech_name_t);

	/* copyout the first stuff */
	if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
		crypto_free_mech_list(entries, count);
		return (EFAULT);
	}

	/* copyout entries */
	offset = offsetof(crypto_get_dev_info_t, di_list);
	if (copyout(entries, arg + offset, copyout_size) != 0) {
		crypto_free_mech_list(entries, count);
		return (EFAULT);
	}
	crypto_free_mech_list(entries, count);
	return (0);
}
예제 #9
0
bool SQLiteDb::Open(string_view const DbFile, bool Local, bool WAL)
{
	const auto& v1_opener = [](string_view const Name, database_ptr& Db)
	{
		return sqlite::sqlite3_open16(null_terminated(Name).c_str(), &ptr_setter(Db)) == SQLITE_OK;
	};

	const auto& v2_opener = [WAL](string_view const Name, database_ptr& Db)
	{
		return sqlite::sqlite3_open_v2(encoding::utf8::get_bytes(Name).c_str(), &ptr_setter(Db), WAL? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY, nullptr) == SQLITE_OK;
	};

	m_Path = GetDatabasePath(DbFile, Local);

	const auto mem_db = DbFile == MemoryDbName;

	if (!Global->Opt->ReadOnlyConfig || mem_db)
	{
		if (!mem_db && db_exists < 0)
		{
			db_exists = os::fs::is_file(m_Path)? +1 : 0;
		}
		if (!v1_opener(m_Path, m_Db))
			return false;

		sqlite::sqlite3_busy_timeout(m_Db.get(), 1000);
		return true;
	}

	// copy db to memory
	//
	if (!v1_opener(MemoryDbName, m_Db))
		return false;

	bool ok = true, copied = false;

	if (os::fs::is_file(m_Path))
	{
		database_ptr db_source;

		if (db_exists < 0)
			db_exists = +1;

		if (WAL && !can_create_file(concat(m_Path, L'.', GuidToStr(CreateUuid())))) // can't open db -- copy to %TEMP%
		{
			string strTmp;
			os::fs::GetTempPath(strTmp);
			append(strTmp, str(GetCurrentProcessId()), L'-', DbFile);
			ok = copied = os::fs::copy_file(m_Path, strTmp, nullptr, nullptr, nullptr, 0);
			if (ok)
			{
				os::fs::set_file_attributes(strTmp, FILE_ATTRIBUTE_NORMAL);
				m_Path = strTmp;
				ok = v1_opener(m_Path, db_source);
			}
		}
		else
		{
			ok = v2_opener(m_Path, db_source);
		}

		if (ok)
		{
			sqlite::sqlite3_busy_timeout(db_source.get(), 1000);
			const auto db_backup = sqlite::sqlite3_backup_init(m_Db.get(), "main", db_source.get(), "main");
			ok = (nullptr != db_backup);
			if (ok)
			{
				sqlite::sqlite3_backup_step(db_backup, -1);
				sqlite::sqlite3_backup_finish(db_backup);
				ok = sqlite::sqlite3_errcode(m_Db.get()) == SQLITE_OK;
			}
		}
	}

	if (copied)
		os::fs::delete_file(m_Path);

	assign(m_Path, MemoryDbName);
	if (!ok)
		Close();
	return ok;
}