ejabberd - Comments for "external authentication login script using c#" https://www.ejabberd.im/node/1617 en Just thought I would include https://www.ejabberd.im/node/1617#comment-59245 <p>Just thought I would include an example of the two sets of comments combined.</p> <p>The config is in C:\Program Files (x86)\ejabberd-2.1.1\conf<br /> The file is ejabberd.cfg</p> <div class="codeblock"><code>%%<br />%% auth_method: Method used to authenticate the users.<br />%% The default method is the internal.<br />%% If you want to use a different method, <br />%% comment this line and enable the correct ones.<br />%%<br />%{auth_method, internal}. <p>%%<br />%% Authentication using external script<br />%% Make sure the script is executable by ejabberd.<br />%%<br />{auth_method, external, internal}.<br />{extauth_program, &quot;c:\\temp\\ConsoleApplication1.exe&quot;}. </p></code></div> <p>FYI - I had issues with spaces in the path and a lack of escaped \ i.e. \\. e.g. I found this failed to work "c:\\Program Files (x86)\\ConsoleApplication1.exe" and left the server running with this error:</p> <div class="codeblock"><code>** {badarg,[{extauth,call_port,2},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {ejabberd_auth_external,check_password,3},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {ejabberd_auth,check_password_loop,2},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {cyrsasl_plain,mech_step,2},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {cyrsasl,server_step,2},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {ejabberd_c2s,wait_for_feature_request,2},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {gen_fsm,handle_msg,7},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {proc_lib,init_p,5}]}</code></div> <p>Anyhow the code I have working is here:</p> <div class="codeblock"><code>using System;<br />using System.IO;<br />using System.Text; <p>namespace ConsoleApplication1<br />{<br />&nbsp;&nbsp;&nbsp; internal class Program<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private static void Main(string[] args)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const int maxbufferLength = 1000; // maximum buffer len<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var outputMessage = new char[maxbufferLength]; // buffer</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var stdinStream = Console.OpenStandardInput();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var binaryReader = new BinaryReader(stdinStream);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Read data from stdin<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var b1 = binaryReader.ReadByte();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var b2 = binaryReader.ReadByte();</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Perform big endian conversion<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var expectedInputLength = b2 + (b1 * 256);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Check for buffer overrun<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (expectedInputLength &gt; maxbufferLength)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new InternalBufferOverflowException(string.Format(&quot;{0} &gt; {1}&quot;, expectedInputLength,<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;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxbufferLength));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Read opcode, username and password<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var bytes = binaryReader.ReadBytes(expectedInputLength);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var data = Encoding.ASCII.GetString(bytes);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var elements = data.Split(&#039;:&#039;);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * PERFORM AUTHENTICATION HERE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Prepare return value, first short is always 2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outputMessage[0] = (char)0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outputMessage[1] = (char)2;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Second short is 1 for success, 0 for failure<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outputMessage[2] = (char)0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outputMessage[3] = (char)1; // or 0 for failure</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Send return value<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.Out.Write(outputMessage, 0, 4);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}</p></code></div> Wed, 07 Nov 2012 12:51:33 +0000 mark.p.jones comment 59245 at https://www.ejabberd.im sizeof(char) in .NET is 2. https://www.ejabberd.im/node/1617#comment-58480 <p>sizeof(char) in .NET is 2. This code will break if the incoming data is sufficiently large. Use a binary reader to read the correct number of bytes from stdin.</p> <div class="codeblock"><code>// (this should be outside your while(true) loop)<br />var stdinStream = Console.OpenStandardInput();<br />var binReader = new BinaryReader(stdinStream); <p>// Read data from stdin<br />var b1 = binReader.ReadByte();<br />var b2 = binReader.ReadByte();</p> <p>// Perform big endian conversion<br />int len = b2 + (b1 * 256);</p> <p>// Check for buffer overrun<br />if (len &gt; maxbuflen)<br />&nbsp;&nbsp;&nbsp; throw new InternalBufferOverflowException(string.Format(&quot;{0} &gt; {1}&quot;, len, maxbuflen));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />// Read opcode, username and password<br />var bytes = binReader.ReadBytes(len);<br />var data = ASCIIEncoding.ASCII.GetString(bytes);<br />string[] elements = data.Split(&#039;:&#039;);</p> <p>// etc etc ...</p></code></div> Fri, 30 Mar 2012 22:46:47 +0000 ooo27 comment 58480 at https://www.ejabberd.im I added this page, so people https://www.ejabberd.im/node/1617#comment-25279 <p>I added this page, so people can find your example easier: <noindex><a href="/check_csharp" rel="nofollow" >check_csharp</a></noindex></p> Tue, 03 Jul 2007 17:56:32 +0000 mfoss comment 25279 at https://www.ejabberd.im not anonymous anymore https://www.ejabberd.im/node/1617#comment-25277 <p>Sorry, didn't register to the site before posting. Now I have an account.<br /> fc</p> Tue, 03 Jul 2007 17:03:02 +0000 fcipriani comment 25277 at https://www.ejabberd.im