Path: blob/main/plugins/python/example_group_plugin.py
1532 views
import sudo123class SudoGroupPlugin(sudo.Plugin):4"""Example sudo input/output plugin56Demonstrates how to use the sudo group plugin API. Typing annotations are7just here for the help on the syntax (requires python >= 3.5).89On detailed description of the functions refer to sudo_plugin manual (man10sudo_plugin).1112Most functions can express error or reject through their "int" return value13as documented in the manual. The sudo module also has constants for these:14sudo.RC.ACCEPT / sudo.RC.OK 115sudo.RC.REJECT 016sudo.RC.ERROR -117sudo.RC.USAGE_ERROR -21819If the plugin encounters an error, instead of just returning sudo.RC.ERROR20result code it can also add a message describing the problem.21This can be done by raising the special exception:22raise sudo.PluginError("Message")23This added message will be used by the audit plugins.2425If the function returns "None" (for example does not call return), it will26be considered sudo.RC.OK. If an exception other than sudo.PluginError is27raised, its backtrace will be shown to the user and the plugin function28returns sudo.RC.ERROR. If that is not acceptable, catch it.29"""3031# -- Plugin API functions --32def query(self, user: str, group: str, user_pwd: tuple):33"""Query if user is part of the specified group.3435Beware that user_pwd can be None if user is not present in the password36database. Otherwise it is a tuple convertible to pwd.struct_passwd.37"""38hardcoded_user_groups = {39"testgroup": ["testuser1", "testuser2"],40"mygroup": ["test"]41}4243group_has_user = user in hardcoded_user_groups.get(group, [])44return sudo.RC.ACCEPT if group_has_user else sudo.RC.REJECT454647