Skip to content

Commit

Permalink
Support for plugin_init func
Browse files Browse the repository at this point in the history
  • Loading branch information
mbachry committed Sep 3, 2013
1 parent 9362341 commit b9c6ee2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ Python module
Python module should do required initializations when it's imported
and provide following global functions:

* `plugin_init(opts)`: called on plugin init, `opts` holds a tuple of
(key, value) 2-tuples with all `auth_opt_` params from mosquitto
configuration (except `auth_opt_pyauth_module`)

* `unpwd_check(username, password)`: return `True` if given
username and password pair is allowed to log in
54 changes: 45 additions & 9 deletions auth_plugin_pyauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct pyauth_data {
#define unused __attribute__((unused))

#ifdef PYAUTH_DEBUG
__attribute__((format(1, 2)))
__attribute__((format(printf, 1, 2)))
static void debug(const char *fmt, ...)
{
va_list ap;
Expand All @@ -31,6 +31,17 @@ static void debug(const char *fmt unused, ...)
}
#endif

__attribute__((format(printf, 2, 3)))
static void die(bool print_exception, const char *fmt, ...)
{
if (print_exception)
PyErr_Print();
va_list ap;
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}

int mosquitto_auth_plugin_version(void)
{
Expand All @@ -48,21 +59,46 @@ int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth
debug("pyauth_module = %s", data->module_name);
}
}
if (data->module_name == NULL) {
fprintf(stderr, "pyauth_module config param missing\n");
exit(1);
}
if (data->module_name == NULL)
die(false, "pyauth_module config param missing");

Py_Initialize();

data->module = PyImport_ImportModule(data->module_name);
if (data->module == NULL) {
fprintf(stderr, "failed to import module: %s\n", data->module_name);
exit(1);
}
if (data->module == NULL)
die(true, "failed to import module: %s", data->module_name);

data->unpwd_check_func = PyObject_GetAttrString(data->module, "unpwd_check");

PyObject *init_func = PyObject_GetAttrString(data->module, "plugin_init");
if (init_func != NULL) {
PyObject *optlist = PyTuple_New(auth_opt_count - 1); /* -1 because of skipped "pyauth_module" */
if (optlist == NULL)
die(true, "python module initialization failed");

int idx = 0;
for (int i = 0; i < auth_opt_count; i++) {
if (!strcmp(auth_opts[i].key, "pyauth_module"))
continue;

PyObject *elt = PyTuple_Pack(2,
PyString_FromString(auth_opts[i].key),
PyString_FromString(auth_opts[i].value));
if (elt == NULL)
die(true, "python module initialization failed");

PyTuple_SET_ITEM(optlist, idx++, elt);
}

PyObject *res = PyObject_CallFunctionObjArgs(init_func, optlist, NULL);
if (res == NULL)
die(true, "python module initialization failed");
Py_DECREF(res);

Py_DECREF(optlist);
Py_DECREF(init_func);
}

*user_data = data;
return MOSQ_ERR_SUCCESS;
}
Expand Down
6 changes: 6 additions & 0 deletions testauth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from pprint import pprint

def plugin_init(opts):
print 'plugin_init'
pprint(opts)

def unpwd_check(username, password):
print username, password
return True

0 comments on commit b9c6ee2

Please sign in to comment.