您的位置:首页 > 运维架构

open***通过ldap或ad统一认证解决方案思路分享

2012-09-10 23:55 330 查看

缘起:成百上千台服务器,上千人的公司,对于账户统一认证的需求非常强烈,一个人入职开账号太繁琐了,走了删除也费劲,最近给一个公司做了AD+LDAP账户统一认证解决方案,两个公司内部网络和几个机房的IDC网络做统一账户认证,其中包括SVN,POSFIX,***,FTP,SAMBA, LINUX登录等等。实际部署实施过程涉及了lvs,haproxy,ha,keepalived等,其中,大家普遍对于***(OPEN*** OR PPTP)通过LDAP或AD认证比较困惑。这里老男孩把***,通过本地文件授权,通过LDAP统一认证的脚本发出来和大家分享。

1)open***配置文件调用的脚本开发思路(配置文件里调用)

  1. #!/usr/bin/python  
  2. ################################################  
  3. #this scripts is created by oldboy  
  4. #oldboy QQ:49000448  
  5. #site:http://www.etiantian.org  
  6. #blog:http://oldboy.blog.51cto.com  
  7. #oldboy trainning QQ group: 208160987 226199307  44246017  
  8. ################################################  
  9.  
  10. import sys  
  11. import os  
  12. import logging  
  13. import ldap  
  14.  
  15. # settings for ldap  
  16. ldap_uri = "ldap://127.0.0.1:389" 
  17. ldap_starttls = True 
  18. ldap_dn = "cn=%s,ou=users,ou=accounts,dc=intra,dc=etiantian,dc=org" 
  19.  
  20. # settings for logging  
  21. log_filename = "/tmp/check_old1boy.log" 
  22. log_format = "%(asctime)s %(levelname)s %(message)s" 
  23. log_level = logging.DEBUG  
  24.  
  25. # settings for authorization  
  26. auth_filename = "/etc/open***/old-boy-users.conf" 
  27.  
  28. def get_users(fpath):  
  29.     fp = open(fpath, "rb")  
  30.     lines = fp.readlines()  
  31.     fp.close()  
  32.     users = {}  
  33.     for line in lines:  
  34.         line = line.strip()  
  35.         if len(line) <= 0 or line.startswith('#'):  
  36.             continue 
  37.         users[line] = True 
  38.     return users  
  39.  
  40. def get_credits(fpath):  
  41.     fp = open(fpath, "rb")  
  42.     lines = fp.readlines()  
  43.     fp.close()  
  44.     assert len(lines)>=2, "invalid credit file" 
  45.     username = lines[0].strip()  
  46.     password = lines[1].strip()  
  47.     return (username, password)  
  48.  
  49. def check_credits(username, password):  
  50.     passed = False 
  51.     ldap.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3)  
  52.     l = ldp.initialize(ldap_uri)  
  53.     if ldap_starttls:  
  54.         l.start_tls_s()  
  55.     try:  
  56.         l.simple_bind_s(ldap_dn % (username,), password)  
  57.         passed = True 
  58.     except ldap.INVALID_CREDENTIALS, e:  
  59.         logging.error("username/password failed verifying")  
  60.     l.unbind()  
  61.     return passed  
  62.  
  63. def main(argv):  
  64.     credit_fpath = argv[1]  
  65.     (username,password) = get_credits(credit fpath)  
  66.     if len(username) <= 0  or len(password) <= 0   
  67.         logging.error("invalid creadits for user '%s'" % username)  
  68.         return 1 
  69.     logging.info("user '%s' request logining" % username)  
  70.     if check_credits(username, password):  
  71.         users = get_users(auth_filename)  
  72.         if not username in users:  
  73.             logging.error("user '%s' not authorized to access" % username)  
  74.             return 1 
  75.         logging.info("access of user '%s' granted" % username)  
  76.         return 0 
  77.     else:  
  78.         logging.error("access of user '%s' denied" % username)  
  79.         return 1 
  80.  
  81. if __name__ = "__main__":  
  82.     logging.Config(format=logformat,filename=log_filename,level=log_level)  
  83.     if len(sys.argv) != 2:  
  84.         logging.fatal("usage: %s <credit-file>" % sys.argv[0])  
  85.         sys.exit(1)  
  86.     rcode = 1 
  87.     try:  
  88.         rcode = main(sys.argv)  
  89.     except Exception   :  
  90.         logging.fatal("exception happened: %s" % str())  
  91.         rcode = 1 
  92.     sys.exit(rcode)  
  93. 提示:在***配置中通过auth-user...参数调用脚本,简单配置下就可以实现通过LDAP认证了。效果很好,可以写PHP页面授权给行政人员管理(邮件),
  94. 网管来管理(内部***,SAMBA,FTP,SVN),小运维(外部,服务器账户,SVN,***等)。
  95.  

 2)授权用户通过LDAP验证登录***的脚本(可以给公司的初级运维维护)

  1. #!/bin/sh  
  2. ################################################  
  3. #this scripts is created by oldboy  
  4. #oldboy QQ:49000448  
  5. #site:http://www.etiantian.org  
  6. #blog:http://oldboy.blog.51cto.com  
  7. #oldboy trainning QQ group: 208160987 226199307  44246017  
  8. ################################################  
  9. # Source function library.  
  10. . /etc/init.d/functions  
  11. #config file path  
  12. FILE_PATH=/etc/open***/oldboy_users.conf  
  13. [ ! -f $FILE_PATH ] && exit;  
  14. usage(){      
  15.     cat <<EOF  
  16.         USAGE: `basename $0` {-add|-del|-search} username  
  17. EOF  
  18. }  
  19.  
  20. #judge run user 
  21. if [ $UID -ne 0 ] ;then 
  22.          echo "Yore not supper user,please call root!" 
  23.          exit 1;  
  24. fi  
  25.  
  26. #judge arg numbers.  
  27. if [ $# -ne 2 ] ;then 
  28.    usage  
  29.    exit  
  30. fi  
  31.  
  32. RETVAL=0   
  33. case "$1" in 
  34.   -a|-add)  
  35.          shift  
  36.      if grep "^$1$" ${FILE_PATH} >>/dev/null 2>&1;then 
  37.             action $"***user,$1 is exist" /bin/false 
  38.         exit  
  39.          else 
  40.             chattr -i ${FILE_PATH}   
  41.             /bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)   
  42.         echo "$1" >> ${FILE_PATH}  
  43.             [ $? -eq 0 ] && action $"Add $1" /bin/true 
  44.         chattr +i ${FILE_PATH}  
  45.      20000  fi  
  46.       ;;  
  47.   -d|-del)  
  48.          shift  
  49.          if [ `grep "^$1$" ${FILE_PATH}|wc -l` -lt 1 ];then 
  50.              action $"***user,$1 is not exist." /bin/false 
  51.              exit  
  52.          else 
  53.              chattr -i ${FILE_PATH}   
  54.              /bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)   
  55.              sed -i "/^${1}$/d" ${FILE_PATH}    
  56.              [ $? -eq 0 ] && action $"Del $1" /bin/true 
  57.              chattr +i ${FILE_PATH}  
  58.              exit  
  59.      fi  
  60.       ;;  
  61.   -s|-search)  
  62.          shift  
  63.          if [ `grep "^$1$" ${FILE_PATH}|wc -l` -lt 1 ];then 
  64.              echo $"***user,$1 is not exist.";exit  
  65.          else 
  66.          echo $"***user,$1 is exist.";exit  
  67.      fi  
  68.      ;;  
  69.   *)    
  70.          usage  
  71.          exit  
  72.          ;;  
  73. esac  
  74. exit $RETVAL  
  75. 提示:脚本内容长,实际上非常简单的。就是修改一个文件。

 说明:由于系统及配置环境的差异,本文内容仅供大家参考。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: