/src/mosquitto/src/plugin_init.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2011-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <stdio.h> |
22 | | #include <string.h> |
23 | | |
24 | | #include "mosquitto/broker.h" |
25 | | #include "mosquitto_broker_internal.h" |
26 | | #include "mosquitto/broker_plugin.h" |
27 | | #include "lib_load.h" |
28 | | #include "utlist.h" |
29 | | |
30 | | typedef int (*FUNC_auth_plugin_version)(void); |
31 | | typedef int (*FUNC_plugin_version)(int, const int *); |
32 | | |
33 | | |
34 | | void LIB_ERROR(void) |
35 | 0 | { |
36 | | #ifdef WIN32 |
37 | | char *buf; |
38 | | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
39 | | NULL, GetLastError(), LANG_NEUTRAL, (LPTSTR)&buf, 0, NULL); |
40 | | log__printf(NULL, MOSQ_LOG_ERR, "Load error: %s", buf); |
41 | | LocalFree(buf); |
42 | | #else |
43 | 0 | log__printf(NULL, MOSQ_LOG_ERR, "Load error: %s", dlerror()); |
44 | 0 | #endif |
45 | 0 | } |
46 | | |
47 | | |
48 | | static int plugin__load_single(mosquitto_plugin_id_t *plugin) |
49 | 0 | { |
50 | 0 | void *lib; |
51 | 0 | int (*plugin_version)(int, const int *) = NULL; |
52 | 0 | int (*plugin_auth_version)(void) = NULL; |
53 | 0 | int version; |
54 | 0 | int rc; |
55 | 0 | const int plugin_versions[] = {5, 4, 3, 2}; |
56 | 0 | int plugin_version_count = sizeof(plugin_versions)/sizeof(int); |
57 | |
|
58 | 0 | if(plugin->config.security_option_count == 0){ |
59 | 0 | return MOSQ_ERR_SUCCESS; |
60 | 0 | } |
61 | | |
62 | 0 | memset(&plugin->lib, 0, sizeof(struct mosquitto__plugin_lib)); |
63 | |
|
64 | 0 | log__printf(NULL, MOSQ_LOG_INFO, "Loading plugin: %s", plugin->config.path); |
65 | |
|
66 | 0 | lib = LIB_LOAD(plugin->config.path); |
67 | 0 | if(!lib){ |
68 | 0 | log__printf(NULL, MOSQ_LOG_ERR, |
69 | 0 | "Error: Unable to load plugin \"%s\".", plugin->config.path); |
70 | 0 | LIB_ERROR(); |
71 | 0 | return MOSQ_ERR_UNKNOWN; |
72 | 0 | } |
73 | | |
74 | 0 | plugin->lib.lib = NULL; |
75 | 0 | if((plugin_version = (FUNC_plugin_version)LIB_SYM(lib, "mosquitto_plugin_version"))){ |
76 | 0 | version = plugin_version(plugin_version_count, plugin_versions); |
77 | 0 | }else if((plugin_auth_version = (FUNC_auth_plugin_version)LIB_SYM(lib, "mosquitto_auth_plugin_version"))){ |
78 | 0 | version = plugin_auth_version(); |
79 | 0 | }else{ |
80 | 0 | log__printf(NULL, MOSQ_LOG_ERR, |
81 | 0 | "Error: Unable to load auth plugin function mosquitto_auth_plugin_version() or mosquitto_plugin_version()."); |
82 | 0 | LIB_ERROR(); |
83 | 0 | LIB_CLOSE(lib); |
84 | 0 | return MOSQ_ERR_UNKNOWN; |
85 | 0 | } |
86 | | |
87 | 0 | plugin->lib.version = version; |
88 | 0 | if(version == 5){ |
89 | 0 | rc = plugin__load_v5(plugin, lib); |
90 | 0 | if(rc){ |
91 | 0 | return rc; |
92 | 0 | } |
93 | 0 | }else if(version == 4){ |
94 | 0 | rc = plugin__load_v4(plugin, lib); |
95 | 0 | if(rc){ |
96 | 0 | return rc; |
97 | 0 | } |
98 | 0 | }else if(version == 3){ |
99 | 0 | rc = plugin__load_v3(plugin, lib); |
100 | 0 | if(rc){ |
101 | 0 | return rc; |
102 | 0 | } |
103 | 0 | }else if(version == 2){ |
104 | 0 | rc = plugin__load_v2(plugin, lib); |
105 | 0 | if(rc){ |
106 | 0 | return rc; |
107 | 0 | } |
108 | 0 | }else{ |
109 | 0 | log__printf(NULL, MOSQ_LOG_ERR, |
110 | 0 | "Error: Unsupported auth plugin version (got %d, expected %d).", |
111 | 0 | version, MOSQ_PLUGIN_VERSION); |
112 | 0 | LIB_ERROR(); |
113 | |
|
114 | 0 | LIB_CLOSE(lib); |
115 | 0 | return MOSQ_ERR_UNKNOWN; |
116 | 0 | } |
117 | 0 | return MOSQ_ERR_SUCCESS; |
118 | 0 | } |
119 | | |
120 | | |
121 | | int plugin__load_all(void) |
122 | 0 | { |
123 | 0 | int rc = MOSQ_ERR_SUCCESS; |
124 | |
|
125 | 0 | for(int i=0; i<db.plugin_count; i++){ |
126 | 0 | rc = plugin__load_single(db.plugins[i]); |
127 | 0 | if(rc){ |
128 | 0 | return rc; |
129 | 0 | } |
130 | 0 | } |
131 | 0 | return MOSQ_ERR_SUCCESS; |
132 | 0 | } |
133 | | |
134 | | |
135 | | static int plugin__security_init_single(mosquitto_plugin_id_t *plugin, bool reload) |
136 | 0 | { |
137 | 0 | int rc; |
138 | |
|
139 | 0 | if(plugin->lib.version == 5){ |
140 | 0 | rc = MOSQ_ERR_SUCCESS; |
141 | 0 | }else if(plugin->lib.version == 4){ |
142 | 0 | rc = plugin->lib.security_init_v4( |
143 | 0 | plugin->lib.user_data, |
144 | 0 | plugin->config.options, |
145 | 0 | plugin->config.option_count, |
146 | 0 | reload); |
147 | |
|
148 | 0 | }else if(plugin->lib.version == 3){ |
149 | 0 | rc = plugin->lib.security_init_v3( |
150 | 0 | plugin->lib.user_data, |
151 | 0 | plugin->config.options, |
152 | 0 | plugin->config.option_count, |
153 | 0 | reload); |
154 | |
|
155 | 0 | }else if(plugin->lib.version == 2){ |
156 | 0 | rc = plugin->lib.security_init_v2( |
157 | 0 | plugin->lib.user_data, |
158 | 0 | (struct mosquitto_auth_opt *)plugin->config.options, |
159 | 0 | plugin->config.option_count, |
160 | 0 | reload); |
161 | 0 | }else{ |
162 | 0 | rc = MOSQ_ERR_INVAL; |
163 | 0 | } |
164 | |
|
165 | 0 | return rc; |
166 | 0 | } |
167 | | |
168 | | |
169 | | int mosquitto_security_init(bool reload) |
170 | 0 | { |
171 | 0 | int rc; |
172 | |
|
173 | 0 | for(int i=0; i<db.plugin_count; i++){ |
174 | 0 | rc = plugin__security_init_single(db.plugins[i], reload); |
175 | 0 | if(rc != MOSQ_ERR_SUCCESS){ |
176 | 0 | return rc; |
177 | 0 | } |
178 | 0 | } |
179 | 0 | rc = mosquitto_security_init_default(); |
180 | 0 | return rc; |
181 | 0 | } |