auth_plugin_v4.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mosquitto.h>
  4. #include <mosquitto_broker.h>
  5. #include <mosquitto_plugin.h>
  6. int mosquitto_auth_plugin_version(void)
  7. {
  8. return 4;
  9. }
  10. int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_opt *auth_opts, int auth_opt_count)
  11. {
  12. return MOSQ_ERR_SUCCESS;
  13. }
  14. int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count)
  15. {
  16. return MOSQ_ERR_SUCCESS;
  17. }
  18. int mosquitto_auth_security_init(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count, bool reload)
  19. {
  20. return MOSQ_ERR_SUCCESS;
  21. }
  22. int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count, bool reload)
  23. {
  24. return MOSQ_ERR_SUCCESS;
  25. }
  26. int mosquitto_auth_acl_check(void *user_data, int access, struct mosquitto *client, const struct mosquitto_acl_msg *msg)
  27. {
  28. const char *username = mosquitto_client_username(client);
  29. if(username && !strcmp(username, "readonly") && access == MOSQ_ACL_READ){
  30. return MOSQ_ERR_SUCCESS;
  31. }else if(username && !strcmp(username, "readonly") && access == MOSQ_ACL_SUBSCRIBE &&!strchr(msg->topic, '#') && !strchr(msg->topic, '+')) {
  32. return MOSQ_ERR_SUCCESS;
  33. }else if(username && !strcmp(username, "readwrite")){
  34. if((!strcmp(msg->topic, "readonly") && access == MOSQ_ACL_READ)
  35. || !strcmp(msg->topic, "writeable")){
  36. return MOSQ_ERR_SUCCESS;
  37. }else{
  38. return MOSQ_ERR_ACL_DENIED;
  39. }
  40. }else{
  41. return MOSQ_ERR_ACL_DENIED;
  42. }
  43. }
  44. int mosquitto_auth_unpwd_check(void *user_data, struct mosquitto *client, const char *username, const char *password)
  45. {
  46. if(!strcmp(username, "test-username") && password && !strcmp(password, "cnwTICONIURW")){
  47. return MOSQ_ERR_SUCCESS;
  48. }else if(!strcmp(username, "readonly") || !strcmp(username, "readwrite")){
  49. return MOSQ_ERR_SUCCESS;
  50. }else if(!strcmp(username, "test-username@v2")){
  51. return MOSQ_ERR_PLUGIN_DEFER;
  52. }else{
  53. return MOSQ_ERR_AUTH;
  54. }
  55. }
  56. int mosquitto_auth_psk_key_get(void *user_data, struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len)
  57. {
  58. return MOSQ_ERR_AUTH;
  59. }