[ldap] Samba+LDAP+LAM管理工具应用

weixin_38058113 2008-04-11 04:29:46

Samba+LDAP+LAM管理工具应用
可以在作者的BLOG上找到这篇文章的PDF文件,下载后方便阅读

作者:gavin       
电子邮箱:gavin.zhou@gmail.com    msn:gavin_zhm@msn.com
主页地址:blog.yepn.net
建立日期:2008年04月11日,最后修改日期:2008年04月11日
版本: 0.1
版权说明:本文基于创作共用约定,内容归作者版权所有,欢迎大家转载,但要请保留作者的完整信息和出处,谢谢!

本文参考:RedHat Enterprise Linux Server 4.3 + OpenLDAP + DNS + Samba + Postfix + OpenWebmail 作者 Fandy
          http://bbs.chinaunix.net/thread-805302-1-1.html
          
          使用 OpenLDAP 集中管理用户帐号 作者 Mike O'Reilly IBM
          http://www.ibm.com/developerworks/cn/linux/l-openldap/#author
          
          OpenLDAP 2.1 管理员指南  翻译 NTKO
          http://i18n.linux.net.cn/others/OpenLDAP2.htm#_Toc14210017


作者 注:
由于公司使用的文件服务器空间不足(只有750G),所以准备新装一台服务器。另外,小弟现在管理公司网络,每新来一名员工就要建6-7个ID,网络认证ID,Samba ID,邮件ID,SNS ID,Desknet ID等等,让日常维护工作量大大增加,所以打算在公司应用LDAP服务器,也可以借这次新的Samba服务器,先把SambaID导出到LDAP中。

小弟在服务器构筑过程中也有很多很多的问题多用GOOGLE是最好的方法,如果能找到高人请教也是一种方法,不过总有效的还是GOOGLE一下,这样对自己也是一种学习过程。另外,对于一些LINUX前辈写的文章要仔细阅读特别是安装文件的版本号,往往很小的细节会让你浪费一天的时间。

文章分为
    I.OS准备确认
    II.OpenLDAP和SAMBA安装设置
    III.Samba用户和组导入(smbldap-tool使用例)
    IV.LAM软件安装和应用

服务器环境

1.CentOS 5.0 Linux server 2.6.18-53.1.14.el5
安装后先配置一下yum再升级一下,Centos带的yum站点好像没有REDHAT家的好用,有可能是个人感觉,不过我还是按照Centos网站上写的添加了一下装了一下RPMforge

#yum install yum-priorities
#vi /etc/yum/pluginconf.d/priorities.conf
[main]
enabled=1
#vi /etc/yum.repos.d/
priority=N   #我设置的N=1
#wget http://apt.sw.be/redhat/el5/en/i ... 6-1.el5.rf.i386.rpm
#rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
#yum check-update   #升级一下

2.Apache 2.2.6 Released (现在2.2.8已经Released了)
源码包安装
./configure && make && make install
安装后Document Root为
/usr/local/apache2/htdocs

3.PHP Version 5.2.5
源码包安装
./configure --with-apxs2=/usr/local/apache2/bin/apxs --enable-mbstring --with-zlib --with-gd --with-jpeg-dir --with-png-dir --with-mysql --with-mcrypt  --with-xsl --with-gettext --with-ldap --with-mhash --with-imap --with-imap-ssl && make && make install

对于参数有不明白的朋友可以GOOGLE一下,讲的会比我写的明白。

PHP安装后要设置Apache的配置文件httpd.conf,内容请参考PHP源码包中的INSTALL文件,我就是按上面写的。

4.OpenLDAP和Samba安装都用的yum安装,如果想用源码包安装的话,需要自己安装一些LIB等文件,详细说明请参考我的WIKI网站http://wiki.yepn.net中OpenLDAP中有详细的源包码说明。上面提到的Apache和PHP源码包安装也有详细的说明。

5.对于Samba和LDAP用户管理需要smbldap-tool这个工具用起来很方便。一样用yum安装
#yum -y install smbldap-tool

============================华丽的分划线============================

OpenLDAP配置文件说明


vi /etc/openldap/sladp.conf

特别注意:下面的配置文件使用时请去掉注释,LDAP对配置文件格式有严格要求。

#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/nis.schema
include /etc/openldap/schema/samba.schema
#这个samba.schema文件,如果用yum安装后可以在
/usr/share/doc/samba-3.0.25b/LDAP/下找到复制到上面指定的目录

include /etc/openldap/schema/corba.schema
include /etc/openldap/schema/misc.schema

# Allow LDAPv2 client connections.  This is NOT the default.
allow bind_v2
#允许使用bind v2 有些设备需要用到Samba用V3

# Do not enable referrals until AFTER you have a working directory
# service AND an understanding of referrals.
#referral       ldap://root.openldap.org

pidfile         /var/run/openldap/slapd.pid
argsfile        /var/run/openldap/slapd.args
#loglevel       296  
#调试的时候加的LOG配置文件,运行正常后去掉了,不想生成大量的LOG文件

access to attrs=userPassword
        by self write
        by anonymous auth
        by dn="uid=admin,ou=Users,dc=yepn,dc=com" write
        by * none
access to *
        by self write
        by dn="uid=admin,ou=Users,dc=yepn,dc=com" write
        by * read

#上面是配置访问权限
对于userPassword字段自己可以改写,用户admin可以改写,匿名用户可以认证,其他拒绝。
自己可以写自己dc中的所有权限,admin用户可以写全部,所有人可读。
上面ACL的顺序也很重要,调换的话会出现PAM认证问题。

database        bdb
suffix dc=yepn,dc=yepn
rootdn cn=root,dc=yepn,dc=com
directory       /var/lib/ldap

index objectClass                       eq,pres
index ou,cn,mail,surname,givenname      eq,pres,sub
index uidNumber,gidNumber,loginShell    eq,pres
index uid,memberUid                     eq,pres,sub
index nisMapName,nisMapEntry            eq,pres,sub
index sambaSID,sambaSIDList,sambaGroupType      eq,pres

# Replicas of this database
replogfile /var/lib/ldap/openldap-master-replog
#replica host=172.30.4.91:389 starttls=critical
replica host=172.30.4.91:389
     binddn="cn=root,dc=yepn,dc=com"
     credentials=111111
     bindmethod=simple
#上段为从LDAP服务器,只有一台LDAP服务器的网络配置不太稳定,万一LDAP服务器坏掉,会出现很多系统不能登陆的问题。

rootpw 000000   #密码名文,过后会把ROOT从配置文件中删除,用admin用户管理LDAP

配置文件确认无误后启动LDAP
#service ldap start
看到下面的信息说明你的LDAP正常启动了
Checking configuration files for slapd:  config file testing succeeded
Starting slapd:                                            [  OK  ]
Starting slurpd:                                           [  OK  ]


============================华丽的分划线============================

Samba安装可以参考Fandy的RedHat Enterprise Linux Server 4.3 + OpenLDAP + DNS + Samba + Postfix + OpenWebmail 这篇文章我的Samba配置配置是按照他上面写的配置的。
下面是我的配置文件,简单说明一下,因为原文的作者写的很细。
# Global parameters
# Version 2008-03-28-01
[global]
        workgroup = YEPN
        netbios name = vanilla
        security = user
        server string = Samba Server 7T %v
        encrypt passwords = Yes
        obey pam restrictions = No
        ldap passwd sync = yes
#与LDAP密码同步
        log level = 3
        syslog = 0
        log file = /var/log/samba/log.%m
        max log size = 100000
        time server = Yes
        socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192
        mangling method = hash2
        Dos charset = UTF-8
        Unix charset = UTF-8
        logon script = %U.bat
        logon drive = H:
        domain logons = Yes
        os level = 65
        preferred master = Yes
        passdb backend = ldapsam:ldap://127.0.0.1/
#指定LDAP服务器
        ldap admin dn = uid=root,dc=yepn,dc=net
#可以管理员用户
        ldap suffix = dc=yepn,dc=net
#搜索域
        ldap group suffix = ou=Groups
#用户级的检索字段
        ldap user suffix = ou=Users
#用户检索字段
        add user script = /usr/sbin/smbldap-useradd -a -m "%u"
#添加用户
        ldap ssl = off
        ldap delete dn = Yes
        delete user script = /usr/sbin/smbldap-userdel -r "%u"
#删除用户
        add group script = /usr/sbin/smbldap-groupadd -a -p "%g"
#添加组
        delete group script = /usr/sbin/smbldap-groupdel "%g"
#删除组
        add user to group script = /usr/sbin/smbldap-groupmod -m "%u" "%g"
#把用户加到指定组
        delete user from group script = /usr/sbin/smbldap-groupmod -x "%u" "%g"
#从组中删除用户
        set primary group script = /usr/sbin/smbldap-usermod -g '%g' '%u'
#设定用户的属组
#以上配置是跟LDAP相关的,其他对于共享目录等控制可以自行添加

[homes]
        comment = Home Directories %U, %u
        browseable = no
        writeable = yes
        read only = no
        force create mode = 0700
        force directory mode = 0700
        directory mode = 0700
        create mode = 0700
        inherit acls = yes
        guest ok = no
        printable = no
#homes目录的定义,其实这个作用已经不大了,因为建立目录的时候会通过smbldap-tool

Samba配置完后启动一下
#service smb start
Starting SMB services:                                     [  OK  ]
Starting NMB services:                                     [  OK  ]
显示上面的说明正常启动,第二个服务说明一下是NETBIOS服务
#smbpasswd -w 111111
#把LDAP的管理员密码加到Samba中,这个一定要做,让Samba对LDAP有权修改。

============================华丽的分划线============================

写在Samba安装之前

Samba服务器安装之前,需要修改服务器的PAM认证,让系统用户通过LDAP认证,具体内容可以参考《使用 OpenLDAP 集中管理用户帐号》这篇文章写的很细。如果你的系统可以通过LDAP认证登陆了,请继续向下看,如果不行,不要向下进行。

Samba用户和组导入

原来的Samba用户和组采用系统帐号,现在导入起来也比较方便。
很多文章介绍用MigrationTools这个工具导入,我用的是smbldap-tools这个工具导入的用户和组。
在用smbldap-tool之前先要配置一下,下面是我的配置文件,会说明一下。
用yum安装后配置文件在下面的目录可以找到
# ls /etc/smbldap-tools/
smbldap_bind.conf  smbldap.conf
#vi smbldap_bind.conf
slaveDN="cn=root,dc=yepn,dc=com"
slavePw="111111"
masterDN="cn=root,dc=yepn,dc=com"
masterPw="111111"
#这部分要与前面你配置的LDAP服务器的管理员相同

然后是smbldap.conf配置文件,这个文件在建立用户时比较重要

SID="S-1-5-21-1153389650-4125104348-4025214935"
#SID每台服务器特有的ID号用下面命令生成,这个一定要修改,不然Samba通过LDAP建立的用户会有问题,我开始就因为这个地方困惑了好久。
#net getlocalsid

sambaDomain="YEPN"
#改成自己的Samba域

slaveLDAP="127.0.0.1"
slavePort="389"
masterLDAP="127.0.0.1"
masterPort="389"
#LDAP服务器设置

ldapTLS="0"
#LDAP认证时候加密码,因为在公司内部网络使用,我又不想弄SSL所以没加密,如果有需要的朋友可以参考《使用 OpenLDAP 集中管理用户帐号》这篇文章做一下TLS,这个地方的参数改成1

verify="require"
cafile="/etc/smbldap-tools/ca.pem"
clientcert="/etc/smbldap-tools/smbldap-tools.pem"
clientkey="/etc/smbldap-tools/smbldap-tools.key"
#以上为安装后默认设置没改动过

suffix="dc=yepn,dc=com"
#前面配置LDAP时说过,不再解释了

usersdn="ou=Users,${suffix}"
#用户的检索字段
#computersdn="ou=Computers,${suffix}"
#我不用Samba做PDC,所以去掉了这项

groupsdn="ou=Groups,${suffix}"
#用户组的检索字段

hash_encrypt="SSHA"
#Password的加密方式

#以下为建立UNIX用户时的参数

userLoginShell="/bin/false"
#我不需要用户登陆服务器

userHome="/home/%U"
#用户的home目录,Samba的homes用这个

# Default mode used for user homeDirectory
userHomeDirectoryMode="700"
#HOME目录的权限

# Gecos
userGecos="System User"

# Default User (POSIX and Samba) GID
defaultUserGid="513"
#用户属组ID是SAMBA用户

defaultMaxPasswordAge="45"
#默认值

#下面是Samba用户配置参数
userSmbHome="\\PDC-SRV\%U"
#homes目录

userProfile="\\PDC-SRV\profiles\%U"
userHomeDrive="H:"
userScript="logon.bat"
mailDomain="yepn.net"
with_smbpasswd="0"
smbpasswd="/usr/bin/smbpasswd"
with_slappasswd="0"
slappasswd="/usr/sbin/slappasswd"
#以上除邮件域名外没有修改

smbldap-tool配置完成,fandy介绍了一种用smbldap-tools自带的configure.pl脚本创建smbldap.conf配置文件,我没用这个,感觉还是自己改的比较快。

============================华丽的分划线============================

现在用smbldap-tool初始化LDAP,很简单运行一下面的命令
#smbldap-populate

Populating LDAP directory for domain yepn (你自己服务器的SID号,这个一定要修改)
(using builtin directory structure)

adding new entry: dc=yepn,dc=net
adding new entry: ou=Users,dc=yepn,dc=net
adding new entry: ou=Groups,dc=yepn,dc=net
adding new entry: ou=Computers,dc=yepn,dc=net
adding new entry: ou=Idmap,dc=yepn,dc=net
adding new entry: uid=root,ou=Users,dc=yepn,dc=net
adding new entry: uid=nobody,ou=Users,dc=yepn,dc=net
adding new entry: cn=Domain Admins,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Domain Users,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Domain Guests,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Domain Computers,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Administrators,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Account Operators,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Print Operators,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Backup Operators,ou=Groups,dc=yepn,dc=net
adding new entry: cn=Replicators,ou=Groups,dc=yepn,dc=net
adding new entry: sambaDomainName=yepn,dc=yepn,dc=net

Please provide a password for the domain root:
Changing password for root
New password : 111111 (root的ldap管理密码)
Retype new password : 111111 (root的ldap管理密码)

介绍一下用smbldap-tool来管理帐号

详细操作:
# smbldap-useradd -a -m test (添加一个samba帐号并创建主目录)

# smbldap-groupadd -a -m yepn (添加一个samba组帐号)
注:fandy的文章中没有加-a这个参数,这个参数smbldap-tool帮助中说明-a这个参数为
          add an automatic Security ID for the group (SID).
          The rid of the group is calculated from the gidNumber of the
          group as rid=2*gidNumber+1001. Thus the resulted SID of the
          group is $SID-$rid where $SID and $rid are the domain SID and
          the group rid
所以我感觉还是加上好,这个地方添加用户组的时候一定加-a,这个不会在改变用户属组后看不到共享文件

# smbldap-groupmod -m test yepn (添加user2用户帐号到acc帐号中)
adding user test to group yepn

更改test帐号的密码:

详细操作:
# smbldap-passwd test
# smbpasswd test
#以上两个密令都可以个改Samba密码,不同的是smbldap-passwd会连unix的密码一起修改,为什么要用smbpass修改密码,或许你需要unixpassword用做别的系统的密码,比如邮件,证书认证等。

Changing password for test
New password : test (用户密码)
Retype new password : test (确认用户密码)

添加test帐号的信息:
#可以再修改帐号的信息,默认是smbldap.conf中的配置

详细操作:
# smbldap-userinfo test

查看test帐号的信息:

详细操作:
# smbldap-usershow test

test帐号建立后可以测试一下,看看能不能看到共享文件

# smbclient -L localhost -U test
Password:

如果下面能正常显示共享信息,说明你的Samba服务器已经通过LDAP认证了。

下面会介绍管理LDAP和Samba的工具LAM
============================华丽的分划线============================

把原来的Samba用户和组信息导入到LDAP服务器
很多文章介绍MigrationTools这个工具导入,如果你也想用这个导入,可以GOOGLE一下,这里不做介绍。
因为MigrationTools工具导入用户时没有Samba用户的相关信息,所以我用的是smbldap-tools这个工具导入的用户和组。
先拿到原来Samba服务器上的passwd和group文件

passwd文件删除系统帐号只留下Samba帐号
#awk -F: '{print $1,$3,$4}' passwd > userlist
这样导出来的数据结构是:用户名 UID GID
group文件删除系统帐号只留下Samba组
#awk -F: '{print $1,$3,$4}' group > grouplist
这样导出来的数据结构是:组名 GID 用户列表

有了这两个文件后写一个很简单的脚本文件,通过smbldap-useradd添加用户

#more useradd.sh
#!/bin/bash

while read name uid gid
do
  /usr/sbin/smbldap-useradd -a -u $uid -g $gid -m $name
done < userlist
很简单,都能看明白不解释了,对于smbldap-useradd不明白的朋友可以,man smbldap-useradd看一下各个参数。

建立组用户也一样
#more groupadd.sh
#!/bin/bash

while read name gid member
do
   /usr/sbin/smbldap-groupadd -a -g $gid $name
done < grouplist

再来就是把用户加到指定组中,只是把上面的文件小小修改一下就可以,通过smbldap-groupmod
#more groupmember.sh
#!/bin/bash

while read name gid member
do
   /usr/sbin/smbldap-groupmod -m "$member" $name
done < grouplist

到上面为止用户和组都已经导入,而且设定好了用户的属组。不过Samba用户的密码还都没有设置,这个还是需要设置一下的
在设置密码时,我没找到什么好方法,所以用了expect这个命令,如果有更好的办法,希望能补充一下。
CentOS默认expect这个是没安装的,安装很简单直接yum
#yum -y install expect
先写个改密码的脚本
#more chpass.exp
#!/usr/bin/expect
spawn /usr/sbin/smbldap-passwd [lindex $argv 0]
set password [lindex $argv 1]
expect "*password:"
#sleep .1
send "$password\n"
expect "*password:"
send "$password\n"
expect eof

再用smbldap-passwd来改用户密码
#more userpasswd.sh
#!/bin/bash

while read name uid gid
do
  /usr/bin/expect chpass.exp $name 123456
done < userlist

#这个需要等一段时间,我用户不多只有100多个,如果你用户多的话,可以时间更长

到这里Samba用户全都迁移到LDAP认证了,以后建立用户删除都可以用smbldap-tool来完成。
因为我公司里只有我自己一个人管理这个服务器,如果我休息的时候对于非技术人员来管理LDAP服务器和Samba服务器来说用这个密码会有些麻烦,所以我还要向下写,找了两个GUI的工具来解决

============================华丽的分划线============================

Samba管理工具Webmin,这个不用多介绍网上文章一大片,GOOGLE一下很多,也不是我介绍的重要,通过这个管理Samba服务器会很方便,不过用Webmin管理LDAP服务器的用户和组我没有设置成功,如果有调协成功的朋友,想请教一下

下面介绍LDAP Account Manager(以下简称LAM)这个GUI的管理工具,这个可以管理LDAP和Samba的用户,对我来说特别重要的是可以建立home目录,这点对于非技术人员管理LDAP和Samba服务器相当重要。当然你也可以说用Webmin建立home目录,不过那样会让非技术者相当麻烦。
相信有很多人用phpldapadmin来维护LDAP,我开始也是用这个工具来维护LDAP服务器,而且也感觉这个会比LAM来维护LDAP更简单一些,不过对于Samba来说phpldapadmin不能管理这点有些不足,所以改用LAM。LAM的安装很简单也是PHP的GUI,如果你以前装过phpldapadmin的话那么LAM复制到apache的Document Root目录下就可以运行了。安装过程请参看LAM压缩包中的INSTALL文件。
在LAM的config需要修改一下lam.conf的配置文件,LAM也提供了GUI的修改页面,这个很方便,下面是我用的配置文件
#more lam.conf

ServerURL: ldap://localhost:389
Admins: uid=admin,ou=Users,dc=yepn,dc=com
#这个地方很重要,如果你希望用LAM来建立home目录,就需要给系统建立一个admin帐号,所以这个地方不能设LDAP的root帐号,而且这个admin帐号必需是unix系统中存在的用户。

Passwd: {SSHA}DrIFykO6wONLf65+jmLeif8nbCA= buvDNg==
treesuffix: dc=yepn,dc=net
defaultLanguage: en_GB.utf8:UTF-8:English (Great Britain)
#这个地方的语言配置可以选中文,这点很方便

scriptPath: /usr/local/apache2/htdocs/lam/lib/lamdaemon.pl
#这个是非常关键的建立home目录用的脚本文件
# Server of external Script
scriptServer: localhost
scriptRights: 750
cachetimeout: 5

# Module settings

modules: posixAccount_minUID: 1000
modules: posixAccount_maxUID: 3000
modules: posixAccount_minMachine: 5000
modules: posixAccount_maxMachine: 6000
modules: posixGroup_minGID: 1000
modules: posixGroup_maxGID: 2000
modules: posixGroup_pwdHash: SSHA
modules: posixAccount_pwdHash: SSHA

activeTypes: user,group

types: suffix_user: ou=Users,dc=yepn,dc=net
types: attr_user: #uid;#givenName;#sn;#uidNumber;#gidNumber
types: modules_user: inetOrgPerson,posixAccount,sambaSamAccount

types: suffix_group: ou=Groups,dc=yepn,dc=net
types: attr_group: #cn;#gidNumber;#memberUID;#description
types: modules_group: posixGroup,sambaGroupMapping

#types: suffix_smbDomain: ou=domains,dc=yepn,dc=net
#types: attr_smbDomain: sambaDomainNameomain name;sambaSIDomain SID
#types: modules_smbDomain: sambaDomain

# Access level for this profile.
accessLevel: 100
modules: sambaSamAccount_timeZone: 9
types: suffix_host:
types: attr_host: #cn;#description;#uidNumber;#gidNumber
types: suffix_smbDomain:
types: attr_smbDomain: #sambaDomainName;#sambaSID

改成你自己用LDAP域就OK了

现在登陆LAM就可以管理LDAP和Samba用户,但是还不能建立home目录 [ 本帖最后由 gavinzhm 于 2008-4-11 16:32 编辑 ]
...全文
87 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧