Open*** 实战3:Open***+MySQL 实现用户登录认证

大纲

一、前言

二、概述

三、具体配置过程

四、总结

注,实战环境 CentOS 5.5 x86_64,软件版本 Open*** 2.1,软件下载:http://yunpan.cn/QzT8fGsX8S75a  访问密码 e8e4。


一、前言

在上一篇博客中我们提出一个问题,下面我们来回顾一下:

前面做的实验都是由服务端先生成客户端证书,然后分发到客户端上,让客户端通过证书连接到服务器上。但有时候,这样的分发是比较麻烦的(也不安全)。这样,我们可以考虑另外一种方式: 只在服务端制作客户端证书,而客户端只需要有ca.crt文件,而不需要拿到客户端证书,当登陆服务器的时候是通过用户名和密码即可登录Open***服务器。在这一篇博客中我们和大家解决这个问题,嘿嘿!


二、概述

在这里简单的和大家先说一下实验步骤:

1.安装相关软件包

  • mysql服务器

  • PAM组件(pam_mysql)

  • sasl

2.创建Open***使用的数据库与表

3.创建测试用户

4.配置PAM mysql认证模块

5.测试pam_mysql是否工作正常

6.配置Open***服务器及客户端配置文件

7.测试连接

注,本文的配置过程紧接着上一篇博文,不清楚的博友可以先看上一篇博文。


三、具体配置过程

1.安装相关软件包

  • mysql服务器

  • PAM组件(pam_mysql)

  • sasl

[root@gateway ~]# yum install -y mysql mysql-devel
[root@gateway ~]# yum install -y pam_krb5 pam_mysql pam pam-devel
[root@gateway ~]# yum install -y cyrus-sasl cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-lib cyrus-sasl-gssapi

2.创建Open***使用的数据库与表

[root@gateway ~]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 65  
Server version: 5.0.37-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective  
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database ***;
mysql> show databases; 
+--------------------+  
| Database           |  
+--------------------+  
| information_schema |   
| mysql              |   
| test               |   
| ***                |   
+--------------------+  
4 rows in set (0.02 sec)
mysql> use ***;
mysql> CREATE TABLE ***user ( name char(20) NOT NULL, password char(128) default NULL, active int(10) NOT NULL DEFAULT 1, PRIMARY KEY (name)  );
mysql> show tables; 
+---------------+  
| Tables_in_*** |  
+---------------+  
| ***user       |   
+---------------+  
1 row in set (0.00 sec)

3.创建测试用户

[root@gateway ~]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 65  
Server version: 5.0.37-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective  
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> insert into ***user (name,password) values('user1',password('123456'));
mysql> insert into ***user (name,password) values('user2',password('123456'));
mysql> insert into ***user (name,password) values('user3',password('123456'));
mysql> select * from ***user; 
+-------+-------------------------------------------+--------+  
| name  | password                                  | active |  
+-------+-------------------------------------------+--------+  
| user1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |   
| user2 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |   
| user3 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |   
+-------+-------------------------------------------+--------+  
3 rows in set (0.00 sec)
注,我这里创建三个测试用户。

4.配置PAM mysql认证模块

[root@gateway ~]# vim /etc/pam.d/open***
auth required pam_mysql.so user=*** passwd=***123 host=127.0.0.1 db=*** \ 
table=***user usercolumn=name passwdcolumn=password \  
where=active=1 sqllog=0 crypt=2  
account required pam_mysql.so user=*** passwd=***123 host=127.0.0.1 db=*** \  
table=***user usercolumn=name passwdcolumn=password \  
where=active=1 sqllog=0 crypt=2  
#crypt(0) -- Used to decide to use MySQL's PASSWORD() function or crypt()  
#0 = No encryption. Passwords in database in plaintext. NOT recommended!  
#1 = Use crypt  
#2 = Use MySQL PASSWORD() function

5.测试pam_mysql是否工作正常

[root@gateway ~]# /etc/init.d/saslauthd start 
启动 saslauthd:                                           [确定]  
[root@gateway ~]# chkconfig saslauthd on  
[root@gateway ~]# chkconfig saslauthd --list  
saslauthd          0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
[root@gateway ~]# testsaslauthd -u user1 -p 123456 -s open*** 
0: OK "Success."

注,如果出现: 0: OK “Success.”, 表示测试成功。如果出现错误可以从系统日志及安全日志里看到出错信息,系统日志: /var/log/messages与安全日志: /var/log/secure。

6.配置Open***服务器及客户端配置文件

1).增加open***认证模块

[root@gateway open***]# cd /etc/open***/ 
[root@gateway open***]# ls | grep open***-auth-pam  
open***-auth-pam.so

注,这个认证模块我已经上传到云盘了,需要的朋友自行下载。

2).修改server.conf配置文件

[root@gateway open***]# vim server.conf
port 1194 
proto udp  
dev tap  
ca ca.crt  
cert server.crt  
key server.key  
dh dh1024.pem  
server 10.8.0.0 255.255.255.0  
push "route 192.168.189.0 255.255.255.0"  
;client-to-client  
keepalive 10 120  
comp-lzo  
persist-key  
persist-tun  
status open***-status.log  
verb 3  
push "dhcp-option DNS 10.8.0.1"  
push "dhcp-option DNS 8.8.8.8"  
push "dhcp-option DNS 8.8.4.4"  
plugin ./open***-auth-pam.so open*** #申明open***使用的插件, open***为插件参数,和pam_mysql的service name是一样的
client-cert-not-required #不请求客户的CA证书, 使用用户名/密码验证 (本配置中没指定, 使用双重认证, 证书和密码验证)
log open***.log

3).修改客户端配置文件  

client  
dev tap  
proto udp  
remote *.*.*.* 1194 #公网IP  
resolv-retry infinite  
nobind  
persist-key  
persist-tun  
ca 146\\ca.crt  
cert 146\\client1.crt  
key 146\\client1.key  
ns-cert-type server  
comp-lzo  
verb 3  
auth-user-pass #在客户端配置文件中加入中加入这一行,重新启动客户端连接到*** server时就需要输入用户名和密码了。

注,下面我们来连接测试一下!

7.测试连接

client test

好了,到这里我们的配置就全部完成了,嘿嘿!


四、总结

1.问题说明(有关open***的PAM认证插件)

Open*** 2.1以上的Open***的open***-auth-pam.so都会出现验证错误的问题,这里需要我们重新编译一个低版本的,我这里用2.0.7的,大家也可以使用2.0.9版本的。大家可以在云盘中自行下载。

2.在配置Open***的同时大家得注意配置iptables防火墙,有时候连接不上Open***大多数是因为防火墙的问题。


好了,今天的博客就到这里了,最后希望大家有所收获^_^……


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章