感觉proftpd对openldap的支持已经有点过时了,2013年以后就再没更新了
https://github.com/proftpd/mod_ldap居然还必须用posixGroup/gidNumber/memberUid的组合,配置为openldap组认证的文章在网上也特别难找。
而apache对openldap的支持要好的多,也是最简单的,相关文章一搜一大把,于是把这篇笔记放在前面
主要参考文章(下一篇proftpd配置openldap组认证也是参考这个法国网站的另一篇文章)
1、http://bouthors.fr/wiki/doku.php?id=en:linux:serveur_web:auth这篇文章是apache 2.2的,在apache 2.4 上会有一些配置方法小变动
一、激活apache的mod_authnz_ldap模块,完成后需要重启apache2
root@mydebian210:~# a2enmod ldap authnz_ldap Enabling module ldap. Considering dependency ldap for authnz_ldap: Module ldap already enabled Enabling module authnz_ldap. To activate the new configuration, you need to run: systemctl restart apache2 root@mydebian210:~# systemctl restart apache2从终端输出可以看出stretch中的apache 2.4缺省已可用mod_ldap模块,实际激活的mod_authnz_ldap模块才是用于认证的
apache 2.4中的mod_ldap模块并不是用于认证的(虽然和proftpd中用于认证的模块mod_ldap名字完全一样)
https://httpd.apache.org/docs/2.4/mod/mod_ldap.html https://httpd.apache.org/docs/2.4/mod/mod_authnz_ldap.html二、配置主目录允许用目录中的.htaccess覆盖主配置文件中认证方面的设置
nano /etc/apache2/apache2.conf
#<Directory /var/www/> # Options Indexes FollowSymLinks # AllowOverride None # Require all granted #</Directory> <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride AuthConfig Require all granted </Directory>重启apache
# apachectl configtest Syntax OK # systemctl restart apache2三、配置.htaccess文件,把index.html改名以免影响autoindex,开始测试
以后的测试都不会用到testgroup,而是以testgroup2为模板,因为testgroup的基类中没有加入posixGroup,
将来是无法与proftpd的openldap组认识进行整合的
# mv /var/www/html/index.html /var/www/html/default.html # mkdir -p /var/www/html/doc_private # cd /var/www/html/doc_private # nano .htaccess配置.htaccess内容抄的参考文章一
AuthName "Please Input Your Password" AuthType Basic AuthBasicProvider ldap AuthLDAPURL ldap://localhost/dc=mydebian210,dc=mydomain,dc=net?uid AuthLDAPBindDN "cn=admin,dc=mydebian210,dc=mydomain,dc=net" AuthLDAPBindPassword "secret_password" AuthzLDAPAuthoritative on #OK Require ldap-user testuser #Satisfy any Require ldap-group cn=testgroup2,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net测试访问http://10.16.97.210目录中只显示default.html,没有看到doc_private目录,
测试访问http://10.16.97.210/doc_private/,报 500 Internal Server Error错误
查看/var/log/apache2/error.log最后几行,其中有出错信息如下
[Tue Oct 20 21:08:34.283390 2020] [core:alert] [pid 1521] [client 10.16.97.100:62005] /var/www/html/doc_private/.htaccess: Invalid command 'AuthzLDAPAuthoritative', perhaps misspelled or defined by a module not included in the server configuration说明问题出在AuthzLDAPAuthoritative配置上,这个配置应该是决定LDAP认证出错时是否尝试其他模块认证用的
在网上搜了一下,原来apache 2.4已经改用AuthLDAPBindAuthoritative了
https://serverfault.com/questions/485871/invalid-command-authzldapauthoritative修改.htaccess
AuthName "Please Input Your Password" AuthType Basic AuthBasicProvider ldap AuthLDAPURL ldap://localhost/dc=mydebian210,dc=mydomain,dc=net?uid AuthLDAPBindDN "cn=admin,dc=mydebian210,dc=mydomain,dc=net" AuthLDAPBindPassword "secret_password" #only in apache 2.2 #AuthzLDAPAuthoritative on #only in apache 2.4 AuthLDAPBindAuthoritative on #OK Require ldap-user testuser #Satisfy any Require ldap-group cn=testgroup2,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net此时无需重启apache2(我感觉用.htaccess好处一是修改后不用重启apache2,二是修改所在目录名后仍然有效
坏处是影响性能,nginx中就因为性能原因不支持.htacces)
再次测试访问http://10.16.97.210目录中只显示default.html,还是没有看到doc_private目录,
测试访问http://10.16.97.210/doc_private/,正常弹出认证窗口,输入前几篇文章中已配置好的
用户名:testuser 密码:ysk,进入到doc_private目录下,主要目标初步达成。
四、ShowForbidden和FancyIndexing
上面测试时发现有.htaccess保护的目录必须知道URL才能进入,
autoindex自动隐藏了目录入口,这种缺省行为不太符合实际需求,经过尝试,发现解决的关键是IndexOptions ShowForbidden
在/etc/apache2/apache2.conf中配置主目录允许这个特征,同时加上了FancyIndexing让界面更美观实用一点(虽然还是很难看)
Options中加入 +MultiViews -ExecCGI,结果apachectl configtest报错,
Either all Options must start with + or -, or no Option may.好像Options中只要有一项有"+"或"-",则必须都有,更正后的设置如下
#<Directory /var/www/> # Options Indexes FollowSymLinks # AllowOverride None # Require all granted #</Directory> <Directory /var/www/> Options +Indexes +FollowSymLinks +MultiViews -ExecCGI AllowOverride AuthConfig IndexOptions ShowForbidden IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* Require all granted </Directory>重启apache
# apachectl configtest Syntax OK # systemctl restart apache2再次测试访问http://10.16.97.210目录中可以显示了default.html和doc_private目录了,
如果没有关闭浏览器,仍然直接就可以进入doc_private目录,这是apache认证的特点,不能注销
退出浏览器再重新打开,访问http://10.16.97.210,进入doc_private又会要求认证,主要目标达成。
五、用户组有多个时如何配置
(一)先增加一个用户testuser2(方法与前几篇一样)
root@mydebian210:~# cat > ~/add_user2_default_unique.ldif << EOF > dn: uid=testuser2,ou=web_ftp_users,dc=mydebian210,dc=mydomain,dc=net > cn: testuser2 > givenName: testuser2 > sn: testuser2 > uid: testuser2 > objectClass: inetOrgPerson > objectClass: top > userPassword: {MD5}lYxrA/e4dMIMkc4u57OpUA== > EOF root@mydebian210:~# ldapadd -x -D cn=admin,dc=mydebian210,dc=mydomain,dc=net -w "secret_password" -f ~/add_user2_default_unique.ldif adding new entry "uid=testuser2,ou=web_ftp_users,dc=mydebian210,dc=mydomain,dc=net"将用户testuser2加入已经存在的testgroup2组中(这里与前几篇不一样,前几篇是建组的同时加入用户)
root@mydebian210:~# cat > ~/add_user_to_group_default_unique.ldif << EOF > dn: cn=testgroup2,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net > changetype: modify > add: uniqueMember > uniqueMember: uid=testuser2,ou=web_ftp_users,dc=mydebian210,dc=mydomain,dc=net > EOF root@mydebian210:~# ldapmodify -D cn=admin,dc=mydebian210,dc=mydomain,dc=net -w "secret_password" -f ~/add_user_to_group_default_unique.ldif modifying entry "cn=testgroup2,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net"(二)再按前几篇一样的方法新建一个用户testuser3,新建一个testgroup3的同时加入testuser3用户
root@mydebian210:~# cat > ~/add_user3_default_unique.ldif << EOF > dn: uid=testuser3,ou=web_ftp_users,dc=mydebian210,dc=mydomain,dc=net > cn: testuser3 > givenName: testuser3 > sn: testuser3 > uid: testuser3 > objectClass: inetOrgPerson > objectClass: top > userPassword: {MD5}lYxrA/e4dMIMkc4u57OpUA== > EOF root@mydebian210:~# ldapadd -x -D cn=admin,dc=mydebian210,dc=mydomain,dc=net -w "secret_password" -f ~/add_user3_default_unique.ldif adding new entry "uid=testuser3,ou=web_ftp_users,dc=mydebian210,dc=mydomain,dc=net" root@mydebian210:~# cat > ~/add_group3_default_unique.ldif << EOF > dn: cn=testgroup3,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net > objectClass: groupOfUniqueNames > cn: testgroup3 > gidNumber: 1003 > description: testgroup3 > uniqueMember: uid=testuser3,ou=web_ftp_users,dc=mydebian210,dc=mydomain,dc=net > EOF root@mydebian210:~# ldapadd -x -D cn=admin,dc=mydebian210,dc=mydomain,dc=net -w "secret_password" -f ~/add_group3_default_unique.ldif adding new entry "cn=testgroup3,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net"(三)修改.htaccess,将两个组的认证配置行用<RequireAny></RequireAny>包含起来,就可以达到目的了,
<RequireAny></RequireAny>中每一条是逻辑“或”的关系,有一条符合就视同通过,其他逻辑关系见
https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html修改后的.htaccess如下
AuthName "Please Input Your Password" AuthType Basic AuthBasicProvider ldap AuthLDAPURL ldap://localhost/dc=mydebian210,dc=mydomain,dc=net?uid AuthLDAPBindDN "cn=admin,dc=mydebian210,dc=mydomain,dc=net" AuthLDAPBindPassword "secret_password" #only in apache 2.2 #AuthzLDAPAuthoritative on #only in apache 2.4 AuthLDAPBindAuthoritative on #OK Require ldap-user testuser #Satisfy any <RequireAny> Require ldap-group cn=testgroup2,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net Require ldap-group cn=testgroup3,ou=web_ftp_groups,dc=mydebian210,dc=mydomain,dc=net </RequireAny>经测试testuser2,testuser3都可以认证通过,说明配置有效,所有目标达成
六、其他话题,如果没有安装PHP或apache与proftpd的数据目录完全分开、下面的内容不是必须的
(一)h5ai
既然安装了PHP,就干脆用h5ai的现代界面替换掉autoindex的古董界面,
选h5ai的最主要原因是它自身并不提供认证,仍然依赖apache自身的认证功能,正好与我的目标契合
h5ai的使用网上文章很多,配置的讲解也很详细,这里只记录下最基本的安装与配置
https://larsjung.de/h5ai/ https://release.larsjung.de/h5ai/h5ai-0.29.2.zip解压到/var/www/html/_h5ai并设置权限
root@mydebian210:~# cd /var/www/html root@mydebian210:/var/www/html# unzip ~/h5ai-0.29.2.zip root@mydebian210:/var/www/html# chown -R www-data:www-data _h5ai/浏览器访问http://10.16.97.210/_h5ai/public/index.php,出错
查看/var/log/apache2/error.log最后几行,其中有出错信息,经过注释掉/var/www/html/_h5ai/.htaccess中相关行,记录报错的各种类型
[Sun Oct 25 22:02:30.873798 2020] [core:alert] [pid 1749] [client 10.16.97.100:50293] /var/www/html/_h5ai/.htaccess: FileETag not allowed here [Sun Oct 25 22:04:26.051967 2020] [core:alert] [pid 1751] [client 10.16.97.100:50502] /var/www/html/_h5ai/.htaccess: AddDefaultCharset not allowed here [Sun Oct 25 22:05:09.072176 2020] [core:alert] [pid 1752] [client 10.16.97.100:50501] /var/www/html/_h5ai/.htaccess: AddCharset not allowed here [Sun Oct 25 22:06:28.001725 2020] [core:alert] [pid 1749] [client 10.16.97.100:50674] /var/www/html/_h5ai/.htaccess: AddType not allowed here [Sun Oct 25 22:07:07.538132 2020] [core:alert] [pid 1755] [client 10.16.97.100:50673] /var/www/html/_h5ai/.htaccess: AddOutputFilterByType not allowed here [Sun Oct 25 22:08:29.577753 2020] [core:alert] [pid 1751] [client 10.16.97.100:50883] /var/www/html/_h5ai/.htaccess: AddEncoding not allowed here [Sun Oct 25 22:08:36.523953 2020] [core:alert] [pid 1751] [client 10.16.97.100:50883] /var/www/html/_h5ai/.htaccess: Options not allowed here注释掉这么多行也不是办法。经过尝试,发现解决的关键是 AllowOverride FileInfo,可以解决除Optioins以外所有报错
Options比较特殊因为参数中有ExecCGI,我并不希望.htaccess覆盖它,最终解决方法是将h5ai的.htaccess中Options部分注释掉
#<IfModule mod_autoindex.c> # Options -Indexes #</IfModule>在/etc/apache2/apache2.conf中配置中新建/var/www/html/_h5ai目录配置允许AllowOverride FileInfo,加入上面注释掉的部分
<Directory /var/www/html/_h5ai/> Options +Indexes +FollowSymLinks +MultiViews -ExecCGI AllowOverride AuthConfig Indexes FileInfo IndexOptions ShowForbidden IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* <IfModule mod_autoindex.c> Options -Indexes </IfModule> Require all granted </Directory>重启apache
# apachectl configtest Syntax OK # systemctl restart apache2浏览器访问http://10.16.97.210/_h5ai/public/index.php,正常,当前口令为空,可以直接登录,
界面是对运行环境进行测试的结果,如果有以下内容,是必须要修改解决的
Public Cache directory no Web server has write access Private Cache directory no Web server has write access这一般是_h5ai目录还是归属于root用户导致的,需要改为www-data用户。chown -R www-data:www-data /var/www/html/_h5ai/
出现以下提示不是必须解决的,要打开这个特性需 apt install php7.0-gd
Image thumbs no PHP GD extension with JPEG support available另一些操作系统层面的软件需求都不是必须的,这些花哨功能的代价是服务器的性能消耗,有的已支持的可以在配置文件中关闭
尤其是将目录打包成压缩文件再下载的功能,假设目录里面有几十G的GHO文件,用户直接下载文件没问题,
用户非要在上级目录选打包成压缩文件再下载怎么对付?这种花哨功能肯定是不能对所有用户都开放的
按h5ai项目主页提供的方法将/_h5ai/public/index.php加入DirectoryIndex行末:nano /etc/apache2/mods-available/dir.conf
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm /_h5ai/public/index.php </IfModule>重启apache
# apachectl configtest Syntax OK # systemctl restart apache2浏览器访问http://10.16.97.210界面正常,后面的工作是定制/var/www/html/_h5ai/private/conf/options.json中的各种参数,
打开这个文件就会发现它引用了互联网字体网站的资源,要在内网用必须修改,否则每次失败尝试都会拖慢网页响应速度
(二)只允许特定目下进行PHP解析
由于我的目标还包括apache与proftpd的数据区一致,同时因为phpldapadmin的原因安装了PHP
这就面临一个重要问题就是要防止FTP用户上传PHP文件,用web调用执行
在网上搜索到下面文章
https://stackoverflow.com/questions/18932756/disable-all-cgi-php-perl-for-a-directory-using-htaccess得到的关键知识就是可以用以下参数禁止某个目录下的任何PHP解析,这个目录下的PHP文件只被当成普通文本文件
php_flag engine off不过要注意的是有的文章指出这个参数只能作用于mod_php,与suPHP、php_fpm之间是冲突的,
另外一点是在.htaccess中设置php_flag engine off并不像其他设置一样可以立即生效,还是必须重启apache2
根据我的实际目标决定全局禁用PHP解析,只对phpldapadmin、self-service-password、_h5ai所在的三个目录打开
在/etc/apache2/apache2.conf中配置成下面这样就可以了
<Directory /var/www/> Options +Indexes +FollowSymLinks +MultiViews -ExecCGI AllowOverride AuthConfig IndexOptions ShowForbidden IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* php_flag engine off Require all granted </Directory> <Directory /var/www/html/_h5ai/> Options +Indexes +FollowSymLinks +MultiViews -ExecCGI AllowOverride AuthConfig Indexes FileInfo IndexOptions ShowForbidden IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* <IfModule mod_autoindex.c> Options -Indexes </IfModule> php_flag engine on Require all granted </Directory>在/var/www/html/下写一个phpinfo();为内容的PHP文件,访问时只会当成文本文件了, 安全性增加了一点点。