Add username accessor.

pull/215/head
Roger A. Light 9 years ago
parent ccedc6d709
commit 63f46a999f

@ -27,10 +27,11 @@ OBJS= mosquitto.o \
logging.o \
loop.o \
memory_mosq.o \
persist.o \
net.o \
net_mosq.o \
packet_mosq.o \
persist.o \
plugin.o \
read_handle.o \
security.o \
security_default.o \
@ -125,6 +126,9 @@ persist.o : persist.c persist.h mosquitto_broker_internal.h
packet_mosq.o : ../lib/packet_mosq.c ../lib/packet_mosq.h
${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@
plugin.o : plugin.c mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@
read_handle.o : read_handle.c mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@

@ -1 +1,2 @@
_mosquitto_log_printf
_mosquitto_client_username

@ -1,3 +1,4 @@
{
mosquitto_log_printf;
mosquitto_client_username;
};

@ -17,6 +17,8 @@ Contributors:
#ifndef MOSQUITTO_BROKER_H
#define MOSQUITTO_BROKER_H
struct mosquitto;
struct mosquitto_opt {
char *key;
char *value;
@ -54,4 +56,20 @@ struct mosquitto_opt {
*/
void mosquitto_log_printf(int level, const char *fmt, ...);
/* =========================================================================
*
* Client Functions
*
* Use these functions to access client information.
*
* ========================================================================= */
/*
* Function: mosquitto_client_username
*
* Retrieve the username associated with a client.
*/
const char *mosquitto_client_username(const struct mosquitto *client);
#endif

@ -0,0 +1,30 @@
/*
Copyright (c) 2016 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "mosquitto_internal.h"
#include "mosquitto_broker_internal.h"
const char *mosquitto_client_username(const struct mosquitto *context)
{
#ifdef WITH_BRIDGE
if(context->bridge){
return context->bridge->local_username;
}else
#endif
{
return context->username;
}
}

@ -16,6 +16,9 @@ Contributors:
/* This is a skeleton authentication and access control plugin that simply defers all checks. */
#include <stdio.h>
#include "mosquitto_broker.h"
#include "mosquitto_plugin.h"
#include "mosquitto.h"
@ -46,6 +49,7 @@ int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_auth_opt *
int mosquitto_auth_acl_check(void *user_data, int access, const struct mosquitto *client, struct mosquitto_acl_msg *msg)
{
printf("mosquitto_acl_check(u:%s)\n", mosquitto_client_username(client));
return MOSQ_ERR_PLUGIN_DEFER;
}

@ -235,7 +235,6 @@ int mosquitto_security_cleanup(struct mosquitto_db *db, bool reload)
int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int access)
{
char *username;
int rc;
int i;
struct mosquitto_acl_msg msg;
@ -244,15 +243,6 @@ int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, cons
return MOSQ_ERR_ACL_DENIED;
}
#ifdef WITH_BRIDGE
if(context->bridge){
username = context->bridge->local_username;
}else
#endif
{
username = context->username;
}
rc = mosquitto_acl_check_default(db, context, topic, access);
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;

@ -1,6 +1,7 @@
#include <string.h>
#include <string.h>
#include <mosquitto.h>
#include <mosquitto_broker.h>
#include <mosquitto_plugin.h>
int mosquitto_auth_plugin_version(void)
@ -28,9 +29,11 @@ int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_auth_opt *
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_acl_check(void *user_data, const char *clientid, const char *username, const char *topic, int access)
int mosquitto_auth_acl_check(void *user_data, int access, const struct mosquitto *client, struct mosquitto_acl_msg *msg)
{
if(!strcmp(username, "readonly") && access == MOSQ_ACL_READ){
const char *username = mosquitto_client_username(client);
if(username && !strcmp(username, "readonly") && access == MOSQ_ACL_READ){
return MOSQ_ERR_SUCCESS;
}else{
return MOSQ_ERR_ACL_DENIED;

Loading…
Cancel
Save