Lua: Load init.lua from plugins directory

Instead of having a global init.lua in datafile_dir that may
contain library code, load the init.lua script from the plugins
directories, similar to other Lua scripts, but guaranteed to
be the first one loaded.

This is consistent with our practice and avoids overwriting the
customizable share/wireshark/init.lua with each instalation or
upgrade.

It also should allow using package.path correctly (which does
not include the configuration directory).

The init.lua in the configuration directory is still loaded for
backward compatibility. It generates a warning in the console.
This commit is contained in:
João Valverde 2023-08-22 13:29:06 +01:00
parent d1f08edcba
commit 9fb85a847d
9 changed files with 61 additions and 106 deletions

View File

@ -2385,28 +2385,6 @@ if (BUILD_logray AND ENABLE_APPLICATION_BUNDLE)
endforeach()
endif()
# Install Lua files in staging directory such that Lua can used when Wireshark
# is ran from the build directory. For install targets, see
# epan/wslua/CMakeLists.txt
if(LUA_FOUND AND ENABLE_LUA)
set(_lua_files
"${CMAKE_SOURCE_DIR}/epan/wslua/init.lua"
)
foreach(_lua_file ${_lua_files})
get_filename_component(_lua_filename "${_lua_file}" NAME)
list(APPEND copy_data_files_depends
"${DATAFILE_DIR}/${_lua_filename}")
add_custom_command(OUTPUT "${DATAFILE_DIR}/${_lua_filename}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${_lua_file}"
"${DATAFILE_DIR}/${_lua_filename}"
DEPENDS
"${_lua_file}"
)
endforeach()
endif(LUA_FOUND AND ENABLE_LUA)
# doc/*.html handled elsewhere.
set(_protocol_data_dir ${CMAKE_SOURCE_DIR}/resources/protocols)
# Glob patterns relative to the source directory that should be copied to
# ${DATAFILE_DIR} (including directory prefixes)

View File

@ -195,6 +195,11 @@ The following features are new (or have been significantly updated) since versio
* Installation of development header must be done explicitly using the CMake
command ``cmake --install <builddir> --component Development``.
* The "init.lua" file is now loaded from any of the Lua plugins directory.
Previously it was loaded from the personal configuration directory. (For
backward-compatibility this is still allowed; note that deprecated features
may be removed in a future release).
=== Removed Features and Support
* With the addition of the universal and consistent filtering support for

View File

@ -14,21 +14,37 @@ applications. Wireshark contains an embedded Lua 5.2 interpreter which
can be used to write dissectors, taps, and capture file readers
and writers.
Wiresharks Lua interpreter starts by loading a file named `init.lua` from
Wireshark's link:{wireshark-users-guide-url}ChAppFilesConfigurationSection.html[_global configuration directory_].
The _global configuration directory_'s `init.lua` controls whether or not Lua
scripts are enabled via the
If Lua is enabled, Wireshark will first try to load a file named `init.lua`
from the global link:{wireshark-users-guide-url}ChPluginFolders.html[_plugins directory_].
and then from the users
link:{wireshark-users-guide-url}ChAppFilesConfigurationSection.html[_personal plugins directory_]. Then all files ending with _.lua_ are loaded from the global plugins
directory. Then Then all files ending with _.lua_ in the personal Lua plugin's
directory.
Whether or not Lua scripts are enabled can be controlled via the
_$$enable_lua$$_ variable. Lua scripts are enabled by
default. To disable Lua scripts, set the _$$enable_lua$$_ variable to _false_.
Wireshark 2.6 and earlier enabled or disabled Lua scripts using
the variable _$$disable_lua$$_ (deprecated). If both _$$enable_lua$$_ and
_$$disable_lua$$_ are present, _$$disable_lua$$_ is ignored.
If Lua is enabled, Wireshark will try to load a file named `init.lua`
from the users
link:{wireshark-users-guide-url}ChAppFilesConfigurationSection.html[_personal configuration directory_]
and all files ending with _.lua_ in the global and the personal
link:{wireshark-users-guide-url}ChPluginFolders.html[_plugins directory_].
.Example for init.lua
[source,lua]
----
-- Set enable_lua to false to disable Lua support.
enable_lua = true
if not enable_lua then
return
end
-- If false and Wireshark was started as (setuid) root, then the user
-- will not be able to execute custom Lua scripts from the personal
-- configuration directory, the -Xlua_script command line option or
-- the Lua Evaluate menu option in the GUI.
-- Note: Not checked on Windows. running_superuser is always false.
run_user_scripts_when_superuser = true
----
The command line option _$$-X lua_script:$$++file.lua++_ can also be used to load
specific Lua scripts.

View File

@ -109,13 +109,6 @@ add_custom_target(
set_target_properties(register_wslua PROPERTIES FOLDER "Libs/epan/wslua")
install(
FILES
init.lua
DESTINATION
${CMAKE_INSTALL_DATADIR}
)
add_library(wslua OBJECT
${WSLUA_FILES}
)

View File

@ -1,26 +0,0 @@
-- init.lua
--
-- initialize wireshark's lua
--
-- This file is going to be executed before any other lua script.
-- It can be used to load libraries, disable functions and more.
--
-- Wireshark - Network traffic analyzer
-- By Gerald Combs <gerald@wireshark.org>
-- Copyright 1998 Gerald Combs
--
-- SPDX-License-Identifier: GPL-2.0-or-later
-- Set enable_lua to false to disable Lua support.
enable_lua = true
if not enable_lua then
return
end
-- If false and Wireshark was started as (setuid) root, then the user
-- will not be able to execute custom Lua scripts from the personal
-- configuration directory, the -Xlua_script command line option or
-- the Lua Evaluate menu option in the GUI.
-- Note: Not checked on Windows. running_superuser is always false.
run_user_scripts_when_superuser = true

View File

@ -733,6 +733,7 @@ static gboolean lua_load_plugin_script(const gchar* name,
const gchar* dirname,
const int file_count)
{
ws_debug("Loading lua script: %s", filename);
if (lua_load_script(filename, dirname, file_count)) {
wslua_add_plugin(name, get_current_plugin_version(), filename);
clear_current_plugin_version();
@ -767,8 +768,12 @@ static int lua_load_plugins(const char *dirname, register_cb cb, gpointer client
while ((file = ws_dir_read_name(dir)) != NULL) {
name = ws_dir_get_name(file);
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
continue; /* skip "." and ".." */
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 ||
strcmp(name, "init.lua") == 0) {
/* skip "." and ".." */
/* init.lua was already loaded if it exists, skip */
continue;
}
filename = ws_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dirname, name);
if (test_for_directory(filename) == EISDIR) {
@ -867,19 +872,11 @@ static int lua_load_pers_plugins(register_cb cb, gpointer client_data,
}
int wslua_count_plugins(void) {
gchar* filename;
int plugins_counter;
/* count global scripts */
plugins_counter = lua_load_global_plugins(NULL, NULL, TRUE);
/* count users init.lua */
filename = get_persconffile_path("init.lua", FALSE);
if ((file_exists(filename))) {
plugins_counter++;
}
g_free(filename);
/* count user scripts */
plugins_counter += lua_load_pers_plugins(NULL, NULL, TRUE);
@ -1595,12 +1592,32 @@ void wslua_init(register_cb cb, gpointer client_data) {
}
/* load system's init.lua */
filename = get_datafile_path("init.lua");
if (( file_exists(filename))) {
filename = g_build_filename(get_plugins_dir(), "init.lua", (char *)NULL);
if (file_exists(filename)) {
ws_debug("Loading init.lua file: %s", filename);
lua_load_internal_script(filename);
}
g_free(filename);
/* load user's init.lua */
/* if we are indeed superuser run user scripts only if told to do so */
if (!started_with_special_privs() || run_anyway) {
filename = g_build_filename(get_plugins_pers_dir(), "init.lua", (char *)NULL);
if (file_exists(filename)) {
ws_debug("Loading init.lua file: %s", filename);
lua_load_internal_script(filename);
}
g_free(filename);
/* For backward compatibility also load it from the configuration directory. */
filename = get_persconffile_path("init.lua", FALSE);
if (file_exists(filename)) {
ws_message("Loading init.lua file from deprecated path: %s", filename);
lua_load_internal_script(filename);
}
g_free(filename);
}
filename = NULL;
/* check if lua is to be disabled */
@ -1636,15 +1653,7 @@ void wslua_init(register_cb cb, gpointer client_data) {
lua_pop(L,1); /* pop the getglobal result */
/* if we are indeed superuser run user scripts only if told to do so */
if ( (!started_with_special_privs()) || run_anyway ) {
/* load users init.lua */
filename = get_persconffile_path("init.lua", FALSE);
if ((file_exists(filename))) {
if (cb)
(*cb)(RA_LUA_PLUGINS, get_basename(filename), client_data);
lua_load_internal_script(filename);
}
g_free(filename);
if (!started_with_special_privs() || run_anyway) {
/* load user scripts */
lua_load_pers_plugins(cb, client_data, FALSE);

View File

@ -179,11 +179,6 @@ if (BUILD_wireshark)
set(_all_manifest_contents "${_all_manifest_contents}File \"${_path}\"\n")
endforeach()
endif()
if(LUA_FOUND)
foreach(_script "init.lua")
set(_all_manifest_contents "${_all_manifest_contents}File \"\${STAGING_DIR}\\${_script}\"\n")
endforeach()
endif()
file(WRITE "${_all_manifest}" "${_all_manifest_contents}")
endif()
@ -220,9 +215,6 @@ if (BUILD_logray)
)
set(_all_manifest_contents "${_all_manifest_contents}File \"\${STAGING_DIR}\\${_dll}\"\n")
endforeach()
foreach(_script "init.lua")
set(_all_manifest_contents "${_all_manifest_contents}File \"\${STAGING_DIR}\\${_script}\"\n")
endforeach()
file(WRITE "${_all_manifest}" "${_all_manifest_contents}")
endif()

View File

@ -144,12 +144,6 @@ foreach(_dll ${CARES_DLL} ${PCRE2_DLL} ${GCRYPT_DLLS}
SET(unique_component ${unique_component} ${_dll})
ENDIF(NOT "${unique_component}" MATCHES "(^|;)${_dll}(;|$)")
endforeach()
foreach(_script "init.lua")
STRING(REGEX REPLACE "[-|\\.]" "_" _wix_name ${_script})
file(APPEND "${_all_manifest_wix}" " <Component Id=\"cmp${_wix_name}\" Guid=\"*\">\n")
file(APPEND "${_all_manifest_wix}" " <File Id=\"fil${_wix_name}\" KeyPath=\"yes\" Source=\"$(var.Staging.Dir)\\${_script}\"/>\n")
file(APPEND "${_all_manifest_wix}" " </Component>\n")
endforeach()
file(APPEND "${_all_manifest_wix}" " </DirectoryRef>\n")
file(APPEND "${_all_manifest_wix}" " </Fragment>\n")
file(APPEND "${_all_manifest_wix}" " <Fragment>\n")
@ -182,10 +176,6 @@ foreach(_dll ${CARES_DLL} ${PCRE2_DLL} ${GCRYPT_DLLS}
SET(unique_file ${unique_file} ${_dll})
ENDIF(NOT "${unique_file}" MATCHES "(^|;)${_dll}(;|$)")
endforeach()
foreach(_script "init.lua")
STRING(REGEX REPLACE "[-|\\.]" "_" _wix_name ${_script})
file(APPEND "${_all_manifest_wix}" " <ComponentRef Id=\"cmp${_wix_name}\" />\n")
endforeach()
file(APPEND "${_all_manifest_wix}" " </ComponentGroup>\n")
file(APPEND "${_all_manifest_wix}" " </Fragment>\n")
file(APPEND "${_all_manifest_wix}" "\n</Wix>\n")

View File

@ -714,7 +714,6 @@
["datafile_path"] = '<function 33>',
["deregister_filehandler"] = '<function 382>',
["dofile"] = '<function 383>',
["enable_lua"] = true,
["error"] = '<function 384>',
["expert"] = {
["group"] = {
@ -909,7 +908,6 @@
["rex_pcre2"] = {
["_VERSION"] = "Lrexlib 2.9.1 (for PCRE2)"
},
["run_user_scripts_when_superuser"] = '<filtered>',
["running_superuser"] = '<filtered>',
["select"] = '<function 474>',
["set_color_filter_slot"] = '<function 475>',