从 PHP 创建 ejabberd 用户
我需要从 PHP 脚本创建一个 ejabberd 用户。我还需要能够将新用户添加到预定义的共享名册中。
我应该使用 exec() 调用 ejabberdctl 还是有更好的方法?
I need to create an ejabberd user from a PHP script. I also need to be able to add the new user to a predefined shared roster.
Should I just call ejabberdctl
using exec()
or is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是我的最终解决方案:
感谢 jldupont 的建议,
ejabberdctl
将是最简单的解决方案,我克服了遇到的障碍并找到了可行的解决方案。默认情况下,apache 的用户没有成功运行 ejabberdctl 的正确权限(并且有充分的理由)。因此,为了使其工作,您必须使用
sudo
调用它。但是... sudo 需要密码,这会带来两个问题:解决方案(对于 Ubuntu) - 在
/etc/sudoers
末尾添加此行:sudoers 文件和 ejabberdctl 的路径可能因其他 Linux 发行版而异。这允许 apache 的用户 (
www-data
) 仅以提升的权限运行ejabberdctl
,并且不需要密码。剩下的就是 PHP 代码:
安全性
需要注意的是,即使您只允许
www-data
运行一个命令,这确实会带来重大的安全风险。如果您使用这种方法,您需要确保通过某种身份验证来保护 PHP 代码,以便不是任何人都可以执行它。除了明显的安全风险之外,它还可能使您的服务器遭受拒绝服务攻击。Here's my final solution:
Thanks to jldupont's advice that
ejabberdctl
would be the easiest solution, I pressed on through the obstacles I ran into and have a working solution.By default, apache's user doesn't have the right privileges to successfully run
ejabberdctl
(and for good reason). So in order for it to work, you have to call it withsudo
. But...sudo
requires a password, which presents 2 problems:Solution (for Ubuntu) - add this line at the end of
/etc/sudoers
:The path to the sudoers file and ejabberdctl may vary for other Linux distros. This allows apache's user (
www-data
) to run onlyejabberdctl
with elevated privileges and without requiring a password.All that's left is the PHP code:
Security
It's important to note that this does present a significant security risk even though you're only allowing one command to be run by
www-data
. If you use this approach, you need to make sure you protect the PHP code behind some sort of authentication so that not just anybody can make it execute. Beyond the obvious security risks, it could open your server up to a Denial of Service attack.我在 2016 年遇到这个问题,有比接受的答案和得票最高的答案更简单的方法来实现这个问题。
https://github.com/fabiang/xmpp
这里是我为添加用户而编写的类:
最后,这里是使用此类的示例代码:
如果服务器没有有效的 SSL 证书,则库调用将失败。放置有效的证书,或使用以下代码片段替换 SocketClient.php 中的这一部分
I came upon this question in 2016, there are much easier ways to implement this than the accepted answer and the highest voted one.
https://github.com/fabiang/xmpp
here is the class I wrote for adding a user:
Finally here is a sample code for using this class:
The library call will fail if the server does not have a valid SSL certificate. Either place a valid certificate, or replace this part in SocketClient.php with the below snippet
在这种特定情况下,ejabberdctl 是迄今为止最简单的。其他选项是:
在 PHP 中实现完整的客户端 XMPP (!)
在 Erlang 中实现一个代理请求的模块:PHP<-- >Erlang 通信需要通过套接字进行,并且会涉及大量的编组工作 (!)
ejabberdctl is by far the easiest in this specific case. The other options are:
Implement a full client XMPP in PHP (!)
Implement a module in Erlang that proxies the requests: the PHP<-->Erlang communication would need to be through a socket and lots of marshaling would be involved (!)
如果您想要在 XMPP 协议中使用 PHP 以干净、安全的方式执行此操作,我建议使用此示例脚本 register_user.php。这是一个可以在 Jaxl PHP 库中找到的示例。
下载Jaxl库并使用如下:
If you want a clean and secure way of doing this using PHP within XMPP protocol, I will recommend working with this example script register_user.php. This is an example that can be found inside Jaxl PHP Library.
Download Jaxl library and use as follows:
最简单的方法是使用 mod_xmlrpc - 它允许您使用 xmlrpc 运行 ejabberdctl 命令。这很容易与库一起使用,例如:
https://github.com/gamenet/php- jabber-rpc
The easiest way of doing this is using mod_xmlrpc - which allows you to run the ejabberdctl commands using xmlrpc. This is easy to use with a library such as:
https://github.com/gamenet/php-jabber-rpc
我已经使用 mod_register_web [1, 2]。它不需要大量的代码,而且我认为它足够安全。 mod_register_web 提供带有简单 POST 表单的 html 页面来注册新用户。
在单独的 http 监听器下启用模块(在我的例子中,端口 5281)。使此端口仅可用于带有“ip”参数的本地请求。
请求示例:
可以使用适当的库(已经在我的框架中)从 php 代码执行请求。
I've solved the problem with mod_register_web [1, 2]. It doesn't require tonnes of code and, I think, is secure enough. mod_register_web provides html page with simple POST form to register new user.
Enable module under separate http listener (in my case, port 5281). Make this port available only for local requests with "ip" parameter.
Request example:
Request can be executed from php code with appropriate library (which was already in my framework).
要应用于 php,您可以使用标准curl
,上面的代码是未发布的 ejabberd 文档中包含的最新的curl代码,
我只是应用它并从 ejabber 文档中分析它,然后将其调整为在 ejabberd 配置 xml 上创建的 ejabberd url 路径
To apply to php you can use standard curl
, and the code above is the most recent curl code contained in the unpublished ejabberd doc,
I just apply it and analyze it from the doc ejabber then I adjust it to the ejabberd url path created on the ejabberd config xml