免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7046 | 回复: 13
打印 上一主题 下一主题

[proxy] SQUID的ncsa_auth认证原理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-02 22:48 |只看该作者 |倒序浏览
前天看了一下ncsa_auth认证程序的源码,发现这个认证其实是非常简单的。
squid启动时运行ncsa_auth passwd密码文件

如果你的认证用户不是很多的话,写个sh就可以解决问题了
ncsa_auth.sh

内容如下
#认证用户名为abc密码123

  1. while true ; do
  2. read a
  3. user=`echo a| /usr/bin/cut -d ' ' -f1`
  4. pass=`echo a|/usr/bin/cut -d ' ' -f2`
  5. if [ "$user" == "abc" ] ; then
  6.          if [ "$pass" == "123" ] ; then
  7.                     echo OK;
  8.         else
  9.                     echo ERR;
  10.         fi
  11. else
  12.          echo ERR
  13.   fi
  14. done
  15. #结束

  16. 呵呵,如果你的认证不是很频繁的话。但是你希望与你的数据库认证集成在一起的话
  17. 你也可以用php连结数据库达到这个目的
  18. 在ncsa_auth.sh里的内容应该如下:
  19. /usr/local/bin/php /usr/local/etc/ncsa_auth.php

  20. 在ncsa_auth.php内容
  21. <?php

  22. $fd = fopen('php://stdin', 'r');
  23. while(true){
  24.         $buffer = fgets($fd, 4096);
  25.         $lsstr=explode(' ',$buffer);
  26.         $sql_str="select user from bbs_user where userid like '$lsstr[0]' and pass like '$lsstr[1]'";
  27.         #如果用户名与密码正确的话
  28.          if(true) echo 'OK';
  29.          else echo 'ERR';
  30. }
  31. ?>;
复制代码

抛砖引玉,希望大家在用squid认证能够有所启发,当然如果你觉得效率低下的话就使用C,直接修改squid自带的ncsa_auth.c加了数据库查询语句就行了。

论坛徽章:
0
2 [报告]
发表于 2005-03-06 21:11 |只看该作者

SQUID的ncsa_auth认证原理



顶!
Good!

论坛徽章:
0
3 [报告]
发表于 2005-03-12 04:00 |只看该作者

SQUID的ncsa_auth认证原理

顶上来,大家都了解一下。

BTW:请大家多多关注我们这个板子,多推荐好帖子,增加我们这个板子的精华数量。谢谢!!

论坛徽章:
0
4 [报告]
发表于 2005-03-12 09:32 |只看该作者

SQUID的ncsa_auth认证原理

正在弄squid的认证呢,呵呵,谢谢楼主了。
UP!

论坛徽章:
0
5 [报告]
发表于 2005-03-12 10:45 |只看该作者

SQUID的ncsa_auth认证原理

请教一下楼主

  1. while true ; do
  2. read a
  3. user=`echo a| /usr/bin/cut -d ' ' -f1`
  4. pass=`echo a|/usr/bin/cut -d ' ' -f2`
  5. if [ "$user" == "abc" ] ; then
  6.         if [ "$pass" == "123" ] ; then
  7.                    echo OK;
  8.        else
  9.                    echo ERR;
  10.        fi
  11. else
  12.         echo ERR
  13. fi
  14. done
  15. #结束
复制代码

这个shell用于哪里?
好像和squid认证无关啊~
另外,ncsa的密码是MD5后的,而楼主这个shell是明码

我只是想知道,这个shell是做什么的呢?

论坛徽章:
0
6 [报告]
发表于 2005-03-12 13:45 |只看该作者

SQUID的ncsa_auth认证原理

观望 学习

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2005-03-12 14:28 |只看该作者

SQUID的ncsa_auth认证原理

原帖由 "platinum" 发表:
这个shell用于哪里?
好像和squid认证无关啊~
另外,ncsa的密码是MD5后的,而楼主这个shell是明码

我只是想知道,这个shell是做什么的呢?


我也想知道,是用这个来代替ncsa吗?
还是解剖他的原理?

论坛徽章:
0
8 [报告]
发表于 2005-03-12 17:43 |只看该作者

SQUID的ncsa_auth认证原理

看了上面大家的回贴,我就再解释一下吧。

我们使用htpasswd产生的那个密码文件里的密码是用MD5加密的。

但是squid传递给ncsa_auth认证程序的密码使用的是明码,
然后由ncsa_auth认证程序再用md5加密传给他的密码后
再与用htpasswd产生的那个密码文件里的密码相比较。
如果符合就认为密码正确。

另外以上的sh与使用php密码认证的程序。
就是接到squid的密码后,不使用md5加密接收到的密码,直接比较,

以上的两个程序
他们都是可以直接替代ncsa_auth这个认证程序的。

OK,如果还有疑问,可以再提问,不过我觉得SQUID他这样的认证是比较合理的。现时也比较直接明了,

设想一下,如果你的认证用户非常多,如有1000人以上同时认证,我们可以简单的使用上面的高级语言如C、php、perl、java等语言,只要他的代码里支持套接字sockets接口你就可以设计一个集中式的认证系统。

以PHP举例:

代码如顶搂的语言里只是稍微修改一下。

  1. 在ncsa_auth.php内容
  2. <?php

  3. $fd = fopen('php://stdin', 'r');
  4. while(true){
  5.        $buffer = fgets($fd, 4096);
  6.        $lsstr=explode(' ',$buffer);
  7. #这里使用套接字把用户名与密码发送到后台认证中心去
  8.      $sfd=fscoket("x.x.x.x",)
  9.        write      
  10.        read
  11.        fclose($sfd);
  12. }
  13. fclose($fd);
  14. ?>;
复制代码

这里之后,一个比较好的认证中心这可以产生了。之所以选用PHP认证,是因为这段时间用PHP比较多。
这样之后,你就可以使用多台SQUID,多台认证中心,一个中央数据库。

呵呵,不知道各位看过我的解释与说明之后,是不是有所启发呢?
没有做不到,只有想不到的事情。

论坛徽章:
0
9 [报告]
发表于 2005-03-16 07:21 |只看该作者

SQUID的ncsa_auth认证原理

这个是ncsa_auth.c的源代码

  1. /*
  2. * ncsa_auth.c
  3. *
  4. * AUTHOR: Arjan de Vet <Arjan.deVet@adv.iae.nl>;
  5. *
  6. * Example authentication program for Squid, based on the original
  7. * proxy_auth code from client_side.c, written by
  8. * Jon Thackray <jrmt@uk.gdscorp.com>;.
  9. *
  10. * Uses a NCSA httpd style password file for authentication with the
  11. * following improvements suggested by various people:
  12. *
  13. * - comment lines are possible and should start with a '#';
  14. * - empty or blank lines are possible;
  15. * - extra fields in the password file are ignored; this makes it
  16. *   possible to use a Unix password file but I do not recommend that.
  17. *
  18. */

  19. #include "config.h"
  20. #if HAVE_STDIO_H
  21. #include <stdio.h>;
  22. #endif
  23. #if HAVE_STDLIB_H
  24. #include <stdlib.h>;
  25. #endif
  26. #if HAVE_UNISTD_H
  27. #include <unistd.h>;
  28. #endif
  29. #if HAVE_STRING_H
  30. #include <string.h>;
  31. #endif
  32. #if HAVE_SYS_TYPES_H
  33. #include <sys/types.h>;
  34. #endif
  35. #if HAVE_SYS_STAT_H
  36. #include <sys/stat.h>;
  37. #endif
  38. #if HAVE_CRYPT_H
  39. #include <crypt.h>;
  40. #endif

  41. #include "util.h"
  42. #include "hash.h"

  43. static hash_table *hash = NULL;
  44. static HASHFREE my_free;

  45. typedef struct _user_data {
  46.     /* first two items must be same as hash_link */
  47.     char *user;
  48.     struct _user_data *next;
  49.     char *passwd;
  50. } user_data;

  51. static void
  52. my_free(void *p)
  53. {
  54.     user_data *u = p;
  55.     xfree(u->;user);
  56.     xfree(u->;passwd);
  57.     xfree(u);
  58. }

  59. static void
  60. read_passwd_file(const char *passwdfile)
  61. {
  62.     FILE *f;
  63.     char buf[8192];
  64.     user_data *u;
  65.     char *user;
  66.     char *passwd;
  67.     if (hash != NULL) {
  68.         hashFreeItems(hash, my_free);
  69.     }
  70.     /* initial setup */
  71.     hash = hash_create((HASHCMP *) strcmp, 7921, hash_string);
  72.     if (NULL == hash) {
  73.         fprintf(stderr, "ncsa_auth: cannot create hash table\n");
  74.         exit(1);
  75.     }
  76.     f = fopen(passwdfile, "r");
  77.     if (NULL == f) {
  78.         fprintf(stderr, "%s: %s\n", passwdfile, xstrerror());
  79.         exit(1);
  80.     }
  81.     while (fgets(buf, 8192, f) != NULL) {
  82.         if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
  83.             (buf[0] == '\n'))
  84.             continue;
  85.         user = strtok(buf, ":\n\r");
  86.         passwd = strtok(NULL, ":\n\r");
  87.         if ((strlen(user) >; 0)  & passwd) {
  88.             u = xmalloc(sizeof(*u));
  89.             u->;user = xstrdup(user);
  90.             u->;passwd = xstrdup(passwd);
  91.             hash_join(hash, (hash_link *) u);
  92.         }
  93.     }
  94.     fclose(f);
  95. }

  96. int
  97. main(int argc, char **argv)
  98. {
  99.     struct stat sb;
  100.     time_t change_time = 0;
  101.     char buf[256];
  102.     char *user, *passwd, *p;
  103.     user_data *u;
  104.     setbuf(stdout, NULL);
  105.     if (argc != 2) {
  106.         fprintf(stderr, "Usage: ncsa_auth <passwordfile>;\n");
  107.         exit(1);
  108.     }
  109.     while (fgets(buf, 256, stdin) != NULL) {
  110.         if ((p = strchr(buf, '\n')) != NULL)
  111.             *p = '\0';          /* strip \n */
  112.         if (stat(argv[1],  sb) == 0) {
  113.             if (sb.st_mtime != change_time) {
  114.                 read_passwd_file(argv[1]);
  115.                 change_time = sb.st_mtime;
  116.             }
  117.         }
  118.         if ((user = strtok(buf, " ")) == NULL) {
  119.             printf("ERR\n");
  120.             continue;
  121.         }
  122.         if ((passwd = strtok(NULL, "")) == NULL) {
  123.             printf("ERR\n");
  124.             continue;
  125.         }
  126.         rfc1738_unescape(user);
  127.         rfc1738_unescape(passwd);
  128.         u = hash_lookup(hash, user);
  129.         if (u == NULL) {
  130.             printf("ERR\n");
  131.         } else if (strcmp(u->;passwd, (char *) crypt(passwd, u->;passwd))) {
  132.             printf("ERR\n");
  133.         } else {
  134.             printf("OK\n");
  135.         }
  136.     }
  137.     exit(0);
  138. }
复制代码

看了一下,关键问题是在这里

  1.         if ((user = strtok(buf, " ")) == NULL) {
  2.             printf("ERR\n");
  3.             continue;
  4.         }
  5.         if ((passwd = strtok(NULL, "")) == NULL) {
  6.             printf("ERR\n");
  7.             continue;
  8.         }
  9.         rfc1738_unescape(user);
  10.         rfc1738_unescape(passwd);
  11.         u = hash_lookup(hash, user);
  12.         if (u == NULL) {
  13.             printf("ERR\n");
  14.         } else if (strcmp(u->;passwd, (char *) crypt(passwd, u->;passwd))) {
  15.             printf("ERR\n");
  16.         } else {
  17.             printf("OK\n");
  18.         }
复制代码


如果用ncsa_auth.php来做“认证中心”,squid又怎么去调用呢?

论坛徽章:
0
10 [报告]
发表于 2005-03-16 10:04 |只看该作者

SQUID的ncsa_auth认证原理

你没仔细看我项楼的贴子啊.

我写得使用PHP认证是
squid先调用的是ncsa_auth.sh这个文件
也就是在squid.conf这个配置文件写的认证程序为ncsa_auth.sh

而这个ncsa_auth.sh文件的内容就是调用php去执行ncsa_auth.php
ncsa_auth.sh这个文件的内容如下:

  1. /usr/local/bin/php /usr/local/squid/etc/ncsa_auth.php
复制代码

#end
由这个ncsa_auth.sh来调用ncsa_auth.php
这样就可以了啊.
然后再在ncsa_auth.php
里面获得squid传递的用户名与密码.

你仔细研究一下我楼上的那个贴子.
就应该能够明白,如果你告诉我说不懂PHP,那我也无话可说了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP