<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程式設計 &#8211; acm&#039;s blog</title>
	<atom:link href="https://blog.acm.idv.tw/category/%e6%8a%80%e8%a1%93/%e7%a8%8b%e5%bc%8f%e8%a8%ad%e8%a8%88/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.acm.idv.tw</link>
	<description>技術與生活隨筆</description>
	<lastBuildDate>Mon, 12 Sep 2022 01:33:25 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>

<image>
	<url>https://blog.acm.idv.tw/wp-content/uploads/2022/06/cropped-logo_kaffa9-3-32x32.png</url>
	<title>程式設計 &#8211; acm&#039;s blog</title>
	<link>https://blog.acm.idv.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>編譯mosquitto-auth-plug</title>
		<link>https://blog.acm.idv.tw/2022/09/12/%e7%b7%a8%e8%ad%afmosquitto-auth-plug/</link>
					<comments>https://blog.acm.idv.tw/2022/09/12/%e7%b7%a8%e8%ad%afmosquitto-auth-plug/#respond</comments>
		
		<dc:creator><![CDATA[kaffa9]]></dc:creator>
		<pubDate>Mon, 12 Sep 2022 01:33:25 +0000</pubDate>
				<category><![CDATA[程式設計]]></category>
		<category><![CDATA[mosquitto]]></category>
		<category><![CDATA[mqtt]]></category>
		<guid isPermaLink="false">https://kaffa9.com/?p=617</guid>

					<description><![CDATA[這段日子和mosquitto-auth-plug朝夕相處，可以說是對其又愛又恨，在此簡單整理一些編譯mosquitto-auth-plug時需要做的前置作業，也...<p class="read-more"><a class="btn btn-default" href="https://blog.acm.idv.tw/2022/09/12/%e7%b7%a8%e8%ad%afmosquitto-auth-plug/"> Read More<span class="screen-reader-text">  Read More</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p>這段日子和mosquitto-auth-plug朝夕相處，可以說是對其又愛又恨，在此簡單整理一些編譯mosquitto-auth-plug時需要做的前置作業，也方便自己日後查閱。</p>



<span id="more-617"></span>



<h2 class="wp-block-heading">修改config.mk.in</h2>



<ul class="wp-block-list"><li>設定BACKEND_xxx，將想要使用的backend模組（如：mysql），設定為yes。</li><li>設定MOSQUITTO_SRC，指定mosquitto原始碼的位置。</li><li>設定OPENSSLDIR，若無特別需求，可以維持/usr不變。</li></ul>



<h2 class="wp-block-heading">修改auth_plug.c</h2>



<p>由於mosquitto-auth-plug在2018年即不再維護，但mosquitto在某一版以後，修改了mosquitto plugin API定義，這會使得編譯過程發生錯誤，因此需要修改auth-plug.c關於plugin API的部份。</p>



<p>相關API如下：</p>



<ul class="wp-block-list"><li>mosquitto_auth_unpwd_check</li><li>mosquitto_auth_acl_check</li><li>mosquitto_auth_psk_key_get</li></ul>



<p>要將以上3個API參數裡的mosquitto struct前面的const修飾子去除。</p>



<p>為了維持與舊版的相容性，這邊可以使用#if前置處理指令判斷MOSQ_AUTH_PLUGIN_VERSION是否>=4即可，例如：</p>



<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#if MOSQ_AUTH_PLUGIN_VERSION >=4
int mosquitto_auth_unpwd_check(void *userdata, struct mosquitto *client, const char *username, const char *password)
#elif 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</pre>



<h2 class="wp-block-heading">修正訂閱ACL判斷（以be-mysql.c為例）</h2>



<p>在mosquitto的security_default.c裡，預設的ACL判斷邏輯在遇到access == MOSQ_ACL_SUBSCRIBE時，會直接放行通過，然而在mosquitto-auth-plug裡卻不是，這會導致client訂閱某些包含wildcard字元(&#8216;#&#8217;)時，無法成功訂閱，自然也無法正常收到訊息。</p>



<p>可以在be_mysql_aclcheck函式裡新增以下邏輯：</p>



<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// 比照 mosquitto security_default.c 寫法，若 access == MOSQ_ACL_SUBSCRIBE，直接放行通過
if (acc == MOSQ_ACL_SUBSCRIBE)
    return (BACKEND_ALLOW);</pre>



<p>記得前面補上header file宣告：</p>



<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;mosquitto_plugin.h></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.acm.idv.tw/2022/09/12/%e7%b7%a8%e8%ad%afmosquitto-auth-plug/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
