Commit Graph

150 Commits

Author SHA1 Message Date
Robert Manner
45d2638571 plugins/python/example*.py: pep8 fixes (mainly line too long) 2020-02-10 05:24:16 -07:00
Robert Manner
0f3631cc08 plugins/python/regress: load/unload module for each testcase
so they can start from clean state. (My problem was optional argument
tests has destroyed the callbacks.)
2020-02-10 05:24:16 -07:00
Robert Manner
36694fce86 plugins/python/python_plugin_common: close can get custom arguments
For the audit plugin.
Ensure we do not fail if plugin_ctx->py_instance is NULL (because
plugin init has failed).
2020-02-10 05:24:16 -07:00
Robert Manner
a7eb9d3412 plugins/python_plugin_io,policy: fix version display in verbose mode
Unfortunately the test did not catch this mistake, because it only
searches that "Python policy plugin API version" string is present
and does not check the version.
2020-02-10 05:24:16 -07:00
Robert Manner
99f99e8a56 plugins/python/sudo_python_module.c: fix options_as_dict if no equal sign
The intented behaviour was that those get skipped, but the PyList_GetItem
sets the interpreter into error state, so python has raised exception.
2020-02-10 05:24:16 -07:00
Robert Manner
21c02e1732 plugins/python/sudo_python_module.c: use IntEnums instead of constants
It is a bit more code, but it is more "pythonic" and easier to debug
as the enum values also know their names.

It is also an API break, eg. sudo.RC_OK becomes sudo.RC.OK as sudo.RC will
be the "type" of the enum, but I guess that is acceptable before the
initial release.
2020-02-10 05:24:16 -07:00
Robert Manner
67ab6fd5d6 plugins/python/python_plugin_policy: add missing debug return 2020-02-10 05:24:16 -07:00
Robert Manner
62524416eb plugins/python/pyhelpers: add helpers for attribute handling
to simplify code a bit.
2020-02-10 05:24:16 -07:00
Robert Manner
4110800c14 plugins/python/example_io_plugin: close the file at destroy
to avoid warning of debug python build.
2020-02-10 05:24:16 -07:00
Robert Manner
a3a7630ff4 plugins/python: memleak fixes in test
The main problem was that string array objects were constructed
differently:
- if constructed by the test, then the elements were constant
- if constructed by the plugin, then the elements were allocated

Modified it so that now each array contains allocated strings so
they can be handled similarly. For freeing, I have used the
str_array_free function from the plugin, so I have linked its object
into the test runner.

Happy path is now free of "definitely lost" memleaks, so the test
can be used for valgrind.
2020-02-10 05:24:16 -07:00
Todd C. Miller
2fe127d108 Move some scripts from the top level src dir to a scripts dir. 2020-02-06 14:30:26 -07:00
Todd C. Miller
a88a05c1eb Adapt python plugin to new plugin API changes 2020-01-30 13:25:38 -07:00
Todd C. Miller
4e87e1a97b Fix typo in a test: python_policy->close not python_io->close 2020-01-28 13:00:33 -07:00
Robert Manner
f268e851d7 plugins/sudo_python_module: Fix double free in sudo.options_as_dict function
PyArg_ParseTuple sets the py_config_tuple pointer, but it does not
increment the reference count, so by decrementing, we end up freeing
the argument passed in.
2020-01-28 07:20:44 -07:00
Robert Manner
22c64f58c0 plugins/python: use separate python interpreter for each plugin
On each plugin initialization we create a separate python interpreter
which gets stored in the plugin_ctx. The main interpreter is
stored in py_ctx and is used for creating more interpreters (if more plugins
get loaded) and final python deinitialization.

The "traceback" module import and the ImportBlocker initialization was
moved, because it has to happen inside the plugin specific interpreters.
2020-01-24 12:25:15 -07:00
Robert Manner
99f8394182 plugins/python/regress: add a failing textcase about python plugins affect each other
Since python plugins are run inside the same interpreter, they affect
each other's state, which would be better to avoid.
2020-01-24 12:25:15 -07:00
Robert Manner
d3d3a59e08 plugins/python/Makefile.in: update autogenerated header dependencies 2020-01-23 12:46:14 -07:00
Robert Manner
d67c64bb37 plugins/python/pyhelpers: have a default sudo_printf function
Adapted the default sudo_printf from sudoers plugin to be able to print
errors before plugin open() gets called. (This is used by the multiple io
plugin loading to display error for too much plugin load.)

Since this makes us always have a sudo_log, I have removed the logic about
whether it is available or not.
2020-01-23 12:46:14 -07:00
Robert Manner
2eeda38f95 plugins/python/regress: add a testcase for multiple io plugin loading
to verify 2 python plugins can work next to each other.
2020-01-23 12:46:14 -07:00
Robert Manner
3128cc97ca plugins/python: fix return value typo for the error case 2020-01-23 12:46:14 -07:00
Robert Manner
304a7fdc1f plugins/python/plugin_io: enable loading of multiple io plugins
Separate sudo io plugin symbols are created which stores wrapper
functions adding the context of which python plugin the callback is
about.

These sudo io plugin "slots" get generated with macros by the preprocessor.

This makes sudo support loading multiple python IO plugins like this:
(note the differences in the symbol names)

Plugin python_io python_plugin.so ModulePath=... ClassName=SudoIOPlugin1
Plugin python_io1 python_plugin.so ModulePath=... ClassName=SudoIOPlugin2
Plugin python_io2 python_plugin.so ModulePath=... ClassName=SudoIOPlugin3
2020-01-23 12:46:14 -07:00
Robert Manner
edd7356f2a plugins/python/Makefile.in: remove path prefix from examples to make install target work 2020-01-23 12:46:14 -07:00
Todd C. Miller
5913c63642 Add abs_top_srcdir and abs_top_builddir and use them.
Configure provides absolution versions of srcdir, builddir, top_srcdir
and top_builddir.  We can use these instead of calling pwd.
2020-01-20 06:37:42 -07:00
Robert Manner
8cacaabe9b plugins/python/regress: add missing license texts 2020-01-20 06:30:20 -07:00
Robert Manner
b66ecf6e13 plugins/python: various portability improvements 2020-01-20 06:30:20 -07:00
Robert Manner
f3b7f3c0d4 plugins/python/example_{io,conversation}: avoid printing signal number
They are platform dependant, so their test would fail on some platforms.
While we could create separate plugin for the tests, I like the idea that
the examples are ensured to be working.

I believe this is a good compromise for being able to auto update the
test cases.
2020-01-20 06:30:20 -07:00
Robert Manner
40f2189586 plugins/python/regress: load the python plugin dynamically
instead of linking with it.
2020-01-20 06:30:20 -07:00
Robert Manner
383ca80333 plugins/python/regress/testdata: generated data for the pyplugin tests 2020-01-20 06:30:20 -07:00
Robert Manner
5da7bd562c plugins/python/regress: adds tests for python plugin feature and examples 2020-01-20 06:30:20 -07:00
Robert Manner
0b2d0334b7 plugins/python/example_policy_plugin.py: extend user env changing example
Make the demonstration extend the environment with a new variable.
Easier to read, and makes the testing able to check for that it is working.
2020-01-20 06:30:20 -07:00
Robert Manner
185471f263 plugins/python/python_plugin_policy: fix validate() call
When calling validate() python function, TypeError exception was thrown
("argument list must be a tuple"), because the call does not have
arguments, and python does not accept empty tuple for execution. NULL
must be used instead, which was handled as argument construction failure
previously.
2020-01-20 06:30:20 -07:00
Robert Manner
35c85596d7 plugins/python/example_policy_plugin.py: make allowed_commands ordered
Storing them as "tuple" instead of "set", so they have a fix order.
This makes the output of the list() example stable. ("set" is printed out
in random order)
2020-01-20 06:30:20 -07:00
Robert Manner
2ee1dd3ec3 plugins/python: fix confusing version display
IO/Group/Policy Python API version is displayed instead of sudo version,
because that is not very meaningful in this context.

They are only displayed in verbose mode.

Example plugins express it more concrete that they are displaying their
version, not the API version.
2020-01-20 06:30:20 -07:00
Robert Manner
87d76f4bbc plugins/python/example_conversation.py: make log path configurable
Similarly to IO plugin example. (It is easier to test it this way.)
2020-01-20 06:30:20 -07:00
Robert Manner
591d57ad28 plugins/python/example_io_plugin.py: fix backtrace during destructor
If the plugin fails to open the file for writing, constructor will raise
an exception and exit before creating the "_log" member variable.
So the destructor will also raise a backtrace. (Which python ignores, but
dumps out to stderr.)
2020-01-20 06:30:20 -07:00
Robert Manner
5be81eb25b plugins/python/python_plugin_common: raise debug level for module import 2020-01-20 06:30:20 -07:00
Robert Manner
8eab169098 plugins/python/example_debugging: fix typo in comment 2020-01-20 06:30:20 -07:00
Todd C. Miller
d2b7148b6c Using "libtool --clean" to remove regular files is slow.
We only need to use libtool's clean mode to remove files created by libtool.
2020-01-18 05:53:55 -07:00
Robert Manner
3e9d15a4da plugins/python/Makefile.in: fix the install path of examples
Examples are installed by default to "docdir", which refers to
PACKAGE_TARNAME variable which was empty for the python plugin Makefile.in

So the examples were installed to '.../share/doc/examples' instead
of '.../share/doc/sudo/examples'. This also made them be skipped from the
package.

Also the install target now depends on install-doc so the examples
gets installed also (similarly as other examples).
2020-01-02 11:53:08 -05:00
Todd C. Miller
486ee2b71f debug_decl and debug_decl_vars now require a semicolon at the end. 2019-12-22 08:48:16 -07:00
Robert Manner
4ad362dd8f plugins/python: example plugin demonstrating conversation and debug API 2019-12-14 12:55:42 -07:00
Robert Manner
523bcbedb6 plugins/python: add example python group plugin 2019-12-14 12:55:42 -07:00
Robert Manner
d8432fca34 plugins/python: add example python policy plugin 2019-12-14 12:55:42 -07:00
Robert Manner
ee856cc4ba plugins/python: add example io python plugin 2019-12-14 12:55:42 -07:00
Robert Manner
91e6bf8ccd Makefile.in, configure.ac: add python plugin build 2019-12-14 12:55:42 -07:00
Robert Manner
d888d44594 plugins/python: make group plugin able to debug
It does not get the debug settings, so it looks them up through
sudo_conf.
2019-12-14 12:55:42 -07:00
Robert Manner
9b49d44e84 plugins/python: add a sudo python module 2019-12-14 12:55:42 -07:00
Robert Manner
a6bac23bab plugins/python: add ImportBlocker which forbids loading unsafe python modules
If non root can alter any imported python modules, he is able to run
anything he would like to as root user.
This class is a helper to avoid such situation.

This feature can be disabled with 'DeveloperMode=1' plugin option.
2019-12-14 12:55:42 -07:00
Robert Manner
babdcbd031 plugins/python: a plugin which can load policy/io plugin written in python 2019-12-14 12:55:42 -07:00
Robert Manner
311cf122e2 plugins/python: add sudo debug helpers 2019-12-14 12:55:42 -07:00