Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #343 from PierreF/mosquitto-15
Browse files Browse the repository at this point in the history
Compile with mosquitto 1.5 (wip)
  • Loading branch information
jpmens authored May 3, 2018
2 parents 2eeee36 + 19678a7 commit e76d58a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
47 changes: 47 additions & 0 deletions auth-plug.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
# define MOSQ_DENY_ACL MOSQ_ERR_ACL_DENIED
#endif

#if MOSQ_AUTH_PLUGIN_VERSION >= 3
# define mosquitto_auth_opt mosquitto_opt
#endif

#include "log.h"
#include "hash.h"
#include "backends.h"
Expand Down Expand Up @@ -136,6 +140,7 @@ int mosquitto_auth_plugin_init(void **userdata, struct mosquitto_auth_opt *auth_
ud->auth_cachejitter = 0;
ud->aclcache = NULL;
ud->authcache = NULL;
ud->clients = NULL;

/*
* Shove all options Mosquitto gives the plugin into a hash,
Expand Down Expand Up @@ -492,7 +497,11 @@ int mosquitto_auth_security_cleanup(void *userdata, struct mosquitto_auth_opt *a
}


#if MOSQ_AUTH_PLUGIN_VERSION >=3
int mosquitto_auth_unpwd_check(void *userdata, const struct mosquitto *client, const char *username, const char *password)
#else
int mosquitto_auth_unpwd_check(void *userdata, const char *username, const char *password)
#endif
{
struct userdata *ud = (struct userdata *)userdata;
struct backend_p **bep;
Expand All @@ -504,6 +513,23 @@ int mosquitto_auth_unpwd_check(void *userdata, const char *username, const char

_log(LOG_DEBUG, "mosquitto_auth_unpwd_check(%s)", (username) ? username : "<nil>");

#if MOSQ_AUTH_PLUGIN_VERSION >=3
struct cliententry *e;
HASH_FIND(hh, ud->clients, &client, sizeof(void *), e);
if (e) {
free(e->username);
free(e->clientid);
e->username = strdup(username);
e->clientid = strdup("client id not available");
} else {
e = (struct cliententry *)malloc(sizeof(struct cliententry));
e->key = (void *)client;
e->username = strdup(username);
e->clientid = strdup("client id not available");
HASH_ADD(hh, ud->clients, key, sizeof(void *), e);
}
#endif

granted = auth_cache_q(username, password, userdata);
if (granted != MOSQ_ERR_UNKNOWN) {
_log(LOG_DEBUG, "getuser(%s) CACHEDAUTH: %d",
Expand Down Expand Up @@ -566,13 +592,30 @@ int mosquitto_auth_unpwd_check(void *userdata, const char *username, const char
return granted;
}

#if MOSQ_AUTH_PLUGIN_VERSION >= 3
int mosquitto_auth_acl_check(void *userdata, int access, const struct mosquitto *client, const struct mosquitto_acl_msg *msg)
#else
int mosquitto_auth_acl_check(void *userdata, const char *clientid, const char *username, const char *topic, int access)
#endif
{
struct userdata *ud = (struct userdata *)userdata;
struct backend_p **bep;
char *backend_name = NULL;
int match = 0, authorized = FALSE, has_error = FALSE;
int granted = MOSQ_DENY_ACL;
#if MOSQ_AUTH_PLUGIN_VERSION >= 3
struct cliententry *e;
char *clientid = NULL;
char *username = NULL;
const char *topic = msg->topic;
HASH_FIND(hh, ud->clients, &client, sizeof(void *), e);
if (e) {
clientid = e->clientid;
username = e->username;
} else {
return MOSQ_ERR_PLUGIN_DEFER;
}
#endif

if (!username || !*username) { // anonymous users
username = ud->anonusername;
Expand Down Expand Up @@ -691,7 +734,11 @@ int mosquitto_auth_acl_check(void *userdata, const char *clientid, const char *u
}


#if MOSQ_AUTH_PLUGIN_VERSION >= 3
int mosquitto_auth_psk_key_get(void *userdata, const struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len)
#else
int mosquitto_auth_psk_key_get(void *userdata, const char *hint, const char *identity, char *key, int max_key_len)
#endif
{
#if BE_PSK
struct userdata *ud = (struct userdata *)userdata;
Expand Down
4 changes: 3 additions & 1 deletion log.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ void (*_log)(int priority, const char *fmt, ...);

void log_init(void)
{
#if (LIBMOSQUITTO_MAJOR > 1) || ((LIBMOSQUITTO_MAJOR == 1) && (LIBMOSQUITTO_MINOR >= 4))
#if MOSQ_AUTH_PLUGIN_VERSION >= 3
_log = __log;
#elif (LIBMOSQUITTO_MAJOR > 1) || ((LIBMOSQUITTO_MAJOR == 1) && (LIBMOSQUITTO_MINOR >= 4))
_log = mosquitto_log_printf;
#else
_log = __log;
Expand Down
8 changes: 8 additions & 0 deletions userdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
#ifndef __USERDATA_H
# define _USERDATA_H

struct cliententry {
void *key;
char *username;
char *clientid;
UT_hash_handle hh;
};

struct userdata {
struct backend_p **be_list;
char *superusers; /* Static glob list */
Expand All @@ -45,6 +52,7 @@ struct userdata {
time_t auth_cacheseconds; /* number of seconds to cache AUTH lookups */
time_t auth_cachejitter; /* number of seconds to add/remove to cache AUTH lookups TTL */
struct cacheentry *authcache;
struct cliententry *clients;
};

#endif

0 comments on commit e76d58a

Please sign in to comment.