ejabberd - Comments for "mod_rest" https://www.ejabberd.im/node/3411 en mod_rest and system commands https://www.ejabberd.im/node/3411#comment-54626 <p>Hi!</p> <p>Since I have strange problems with <code>ejabberd_xmlrpc</code> after updating erlang to R13B01, I looked for another ways to reach desired result, and found, that <code>mod_rest</code> can supply needed functionality. Besides sending messages, <code>README.txt</code> declares:<br /> <cite>This module can also be used as a frontend to execute ejabberd commands.<br /> ...<br /> Instead of an XMPP stanza, you can provide an ejabberd command to execute:<br /> registered_users localhost<br /> </cite></p> <p>At first, I tried message sending - it work greatly(on the contrary of <code>mod_xmlrpc</code> messages, sent to offline contacts, are always delivered), but when I tested ejabberd command, the result was negative:<br /> <code>ejabberd.log</code>:<br /> <div class="codeblock"><code>=INFO REPORT==== 2009-09-03 07:36:16 ===<br />D(&lt;0.183.0&gt;:ejabberd_http:129) : S: [{[&quot;re3st&quot;],mod_rest}]<br />=INFO REPORT==== 2009-09-03 07:36:16 ===<br />I(&lt;0.183.0&gt;:ejabberd_http:131) : started: {gen_tcp,#Port&lt;0.3389&gt;}<br />=INFO REPORT==== 2009-09-03 07:36:17 ===<br />D(&lt;0.279.0&gt;:ejabberd_http:242) : (#Port&lt;0.3389&gt;) http query: &#039;POST&#039; /re3st<br />=INFO REPORT==== 2009-09-03 07:36:17 ===<br />D(&lt;0.279.0&gt;:ejabberd_http:405) : client data: &quot; --config /usr/home/jabber2/etc/ejabberd/ejabberd_c.cfg --ctl-config /usr/home/jabber2/etc/ejabberd/ejabberdctl_c.cfg --node c@localhost --logs /usr/home/jabber2/var/log/ejabberd/c --spool /usr/home/jabber2/var/lib/ejabberd/c registered_users company.loc&quot;<br />=INFO REPORT==== 2009-09-03 07:36:17 ===<br />D(&lt;0.279.0&gt;:ejabberd_http:310) : [&quot;re3st&quot;] matches [&quot;re3st&quot;]<br />=INFO REPORT==== 2009-09-03 07:36:17 ===<br />I(&lt;0.279.0&gt;:mod_rest:88) : Data: &quot; --config /usr/home/jabber2/etc/ejabberd/ejabberd_c.cfg --ctl-config /usr/home/jabber2/etc/ejabberd/ejabberdctl_c.cfg --node c@localhost --logs /usr/home/jabber2/var/log/ejabberd/c --spool /usr/home/jabber2/var/lib/ejabberd/c registered_users company.loc&quot;</code></div> </p><p><code>script output</code>:<br /> <div class="codeblock"><code>Warning: file_get_contents(<noindex><a href="http://127.0.0.1:5285/re3st" title="http://127.0.0.1:5285/re3st" rel="nofollow" >http://127.0.0.1:5285/re3st</a></noindex>): failed to open stream: HTTP request failed! HTTP/1.0 406 Not Acceptable</code></div> </p><p>Command used<br /> <code> --config /usr/home/jabber2/etc/ejabberd/ejabberd_c.cfg --ctl-config /usr/home/jabber2/etc/ejabberd/ejabberdctl_c.cfg --node c@localhost --logs /usr/home/jabber2/var/log/ejabberd/c --spool /usr/home/jabber2/var/lib/ejabberd/c registered_users company.loc</code><br /> is a full command, with which I operate with <code>ejabberdctl</code>, as I have specific server configuration(see <a href="http://www.ejabberd.im/node/3276" title="http://www.ejabberd.im/node/3276">http://www.ejabberd.im/node/3276</a>).<br /> Using simple <code>registered_users company.loc</code> gave the same result.</p> <p>System info:</p> <ul> <li>Version:</li> <p>ejabberd 2.1.0-alpha<br /> <cite>(by the way, I expected to get at least 2.1.0-beta1 from svn(<noindex><a href="https://svn.process-one.net/ejabberd" title="https://svn.process-one.net/ejabberd" rel="nofollow" >https://svn.process-one.net/ejabberd</a></noindex>))</cite></p> <li>SVN:</li> <p>-ejabberd:2582<br /> -ejabberd-modules:1020</p> <li>ejabberd_c.cfg(cut):</li> <div class="codeblock"><code>{modules,<br /> [<br />...<br />&nbsp;&nbsp;&nbsp; {mod_admin_extra, []},<br />&nbsp;&nbsp;&nbsp; {mod_rest, [ {allowed_ips, [ {127,0,0,1} ]} ]},<br />...<br /> ]}.<br />{listen,<br /> [<br />...<br />&nbsp;&nbsp;&nbsp; {5285, ejabberd_http, [<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {request_handlers, [<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[&quot;re3st&quot;], mod_rest}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {ip, {127,0,0,1}}<br />&nbsp;&nbsp;&nbsp; ]}<br />...<br /> ]}.</code></div> <li>script.php:</li> <div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$request </span><span style="color: #007700">= </span><span style="color: #DD0000">"&lt;message to=\"user01@company.loc\" from=\"robot@company.loc\"&gt;&lt;body&gt;World at "</span><span style="color: #007700">.</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"r"</span><span style="color: #007700">).</span><span style="color: #DD0000">"&lt;/body&gt;&lt;/message&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$cmd </span><span style="color: #007700">= </span><span style="color: #DD0000">"/usr/home/jabber2/sbin/ejabberdctl"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$config </span><span style="color: #007700">= </span><span style="color: #DD0000">" --config /usr/home/jabber2/etc/ejabberd/ejabberd_c.cfg" </span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">" --ctl-config /usr/home/jabber2/etc/ejabberd/ejabberdctl_c.cfg" </span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">" --node c@localhost" </span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">" --logs /usr/home/jabber2/var/log/ejabberd/c" </span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">" --spool /usr/home/jabber2/var/lib/ejabberd/c"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$request </span><span style="color: #007700">= </span><span style="color: #0000BB">$config </span><span style="color: #007700">. </span><span style="color: #DD0000">" registered_users company.loc"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array(</span><span style="color: #DD0000">'http' </span><span style="color: #007700">=&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'method' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"POST"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'header' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Content-Length: "</span><span style="color: #007700">.</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$request</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'content' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$request<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br />);<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://127.0.0.1:5285/re3st"</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"file:[</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">]\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </ul> <p>Modified version of<br /> <div class="codeblock"><code>ejabberd_c.cfg&lt;/code:<br />&lt;code&gt;<br />...<br />{acl, bot, {user, &quot;robot&quot;, &quot;company.loc&quot;}}.<br />{access, botctl, [{allow,bot}]}.<br />...<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {mod_rest, [<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {allowed_ips, [ {127,0,0,1} ]}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,{access_commands, [ {botctl, all, []} ]}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]},</code></div> </p><p>and <code>script.php</code>:</p> <div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">...<br /></span><span style="color: #0000BB">$request </span><span style="color: #007700">= </span><span style="color: #DD0000">"--auth robot company.loc robot registered_users <br />company.loc"</span><span style="color: #007700">;<br />...<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> <p>didn't change anything.</p> <p>The question is obvious :-)))) What do I do wrong?<br /> Thanks.</p> Thu, 03 Sep 2009 12:26:35 +0000 nu3ga comment 54626 at https://www.ejabberd.im Only trunk SVN. Workaround for 2.0.x branch https://www.ejabberd.im/node/3411#comment-54136 <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>What does allowed_destinations control? Should that be JIDs? </p></div> <p>Yes, JID of destinations. Check the README.txt</p> <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>Is that code in the 2.0.x branch?</p></div> <p>No, all the options are only implemented in mod_rest trunk, which requires ejabberd trunk. The reason is that only ejabberd trunk reports the Host in ejabberd_http requests, and that is required to know where to read the configured options.</p> <p>If you don't want to use ejabberd trunk SVN, here is a workaround: Copy mod_rest.erl from trunk to the 2.0.x branch. Then apply this patch:</p> <pre> --- mod_rest.erl (revisiĆ³n: 931) +++ mod_rest.erl (copia de trabajo) @@ -63,10 +63,9 @@ process([], #request{method = 'POST', data = Data, - host = Host, ip = ClientIp }) -&gt; - maybe_post_request(Data, Host, ClientIp); + maybe_post_request(Data, "example.com", ClientIp); process(_Path, _Request) -&gt; ?DEBUG("Got request to ~p: ~p", [_Path, _Request]), {200, [], "Try POSTing a stanza."}. </pre><p>You need to explicitly indicate the ejabberd vhost in the code. Now you can configure the options in mod_rest.</p> <p>I didn't try, but I imagine this will work.</p> Tue, 31 Mar 2009 19:24:24 +0000 mfoss comment 54136 at https://www.ejabberd.im Awesome, thanks! Is that https://www.ejabberd.im/node/3411#comment-54135 <p>Awesome, thanks! Is that code in the 2.0.x branch? What does allowed_destinations control? Should that be JIDs?</p> Tue, 31 Mar 2009 19:11:11 +0000 spoonmanp comment 54135 at https://www.ejabberd.im New options allowed_destinations and allowed_stanza_types https://www.ejabberd.im/node/3411#comment-54134 <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>an allowed_stanzas option would be great. </p></div> <p>I've added options allowed_destinations and allowed_stanza_types. You could betatest the new code.</p> Tue, 31 Mar 2009 19:06:55 +0000 mfoss comment 54134 at https://www.ejabberd.im That's exactly what I had in https://www.ejabberd.im/node/3411#comment-54131 <p>That's exactly what I had in mind, I simply changed it to accept a wildcard instead of a message stanza, but an allowed_stanzas option would be great.</p> Tue, 31 Mar 2009 17:56:41 +0000 spoonmanp comment 54131 at https://www.ejabberd.im Maybe just a simple restriction https://www.ejabberd.im/node/3411#comment-54128 <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>Just curious why mod_rest is only setup to accept stanzas?</p></div> <p>I guess because the original author only needed 'message' stanzas, and he wanted to restrict the service.</p> <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>Is there any problems that I should be aware of?</p></div> <p>Looking at the code, I don't see any reason why there could be problems with other types of stanzas. You can try it, and report if you find some problem.</p> <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>I modified it so it will accept other types of stanzas, for example, so a server can post something to a pubsub node on behalf of a user.</p></div> <p>I guess it would be even better if that would be configurable as a mod_rest option: {allowed_stanzas, [message, presence, iq]}, or only some of them.</p> Tue, 31 Mar 2009 17:21:40 +0000 mfoss comment 54128 at https://www.ejabberd.im why does it only accept <message/> stanzas https://www.ejabberd.im/node/3411#comment-54127 <p>Just curious why mod_rest is only setup to accept stanzas? I modified it so it will accept other types of stanzas, for example, so a server can post something to a pubsub node on behalf of a user. Is there any problems that I should be aware of?</p> <p>Dave</p> Tue, 31 Mar 2009 17:03:46 +0000 spoonmanp comment 54127 at https://www.ejabberd.im Use firewall. In trunk there is configurable option https://www.ejabberd.im/node/3411#comment-54125 <div class="quote-msg"> <div class="quote-author"><em>spoonmanp</em> wrote:</div> <p>Would it be easy to modify it so only requests from a defined IP address will be handled by mod_rest?</p></div> <p>You can filter by IP address in your network firewall, instead of in ejabberd. Just configure the ejabberd listener for mod_rest in a custom, specific IP port number, so you can add a rule in your firewall to restrict access to that port number.</p> <p>If you are running ejabberd trunk from SVN and mod_rest trunk SVN, then you can try a new configurable option in mod_rest that I added today: allowed_ips.</p> Tue, 31 Mar 2009 15:15:58 +0000 mfoss comment 54125 at https://www.ejabberd.im accept request from a specific ip only https://www.ejabberd.im/node/3411#comment-54114 <p>Would it be easy to modify it so only requests from a defined IP address will be handled by mod_rest?</p> Wed, 25 Mar 2009 21:26:38 +0000 spoonmanp comment 54114 at https://www.ejabberd.im Excellent https://www.ejabberd.im/node/3411#comment-54048 <p>It works a charm now.</p> <p>Many Many Thanks.</p> <p>Paul</p> Tue, 03 Mar 2009 15:44:09 +0000 PaulKinlan comment 54048 at https://www.ejabberd.im Excellent https://www.ejabberd.im/node/3411#comment-54047 <p>Will try it now... Thanks for the reply and the fix... Will let you know how it goes on.</p> Tue, 03 Mar 2009 14:19:32 +0000 PaulKinlan comment 54047 at https://www.ejabberd.im Try the new mod_rest branch ejabberd-2.0.x https://www.ejabberd.im/node/3411#comment-54045 <div class="quote-msg"> <div class="quote-author"><em>PaulKinlan</em> wrote:</div> <p>I am not too sure what version of ejabbered I am using (but I installed it via apt-get today).</p></div> <p>Probably you have ejabberd 2.0.1 or 2.0.3.</p> <div class="quote-msg"> <div class="quote-author"><em>PaulKinlan</em> wrote:</div> <p>mod_rest is the lastet version via SVN,</p></div> <p>That is the problem: mod_rest trunk SVN is only compatible with ejabberd trunk SVN, but its README.txt didn't explain this requirement.</p> <p>Now I've added mod_rest/branches/ejabberd-2.0.x to SVN, which works with ejabberd 2.0.1 and 2.0.3, and documented the requirements.</p> <div class="quote-msg"> <div class="quote-author"><em>PaulKinlan</em> wrote:</div> <p>I have followed the instructions.<br /> ...<br /> I am using Fiddler on windows to construct the request to ejabberd on my ubuntu box. </p></div> <p>By the way, I added to mod_rest's README.txt some examples to send POST calls using Wget and Lynx.</p> Mon, 02 Mar 2009 11:33:00 +0000 mfoss comment 54045 at https://www.ejabberd.im