FreeBSD上搭建squid代理mysql认证服务器

来源:岁月联盟 编辑:zhu 时间:2008-01-18
FreeBSD上搭建squid代理mysql认证服务器内容简介:【FreeBSD教程】 首先我要感谢hj的帮助,不管是在我学习linux上,还是FreeBSD上虽然更多的是java上。 接着声明一下,本人的语文水平很不好,写的文档可能比较糟糕,但是看在我辛辛劳苦作了好几个通宵的   【FreeBSD教程】首先我要感谢hj的帮助,不管是在我学习linux上,还是FreeBSD上虽然更多的是java上。
  接着声明一下,本人的语文水平很不好,写的文档可能比较糟糕,但是看在我辛辛劳苦作了好几个通宵的份上希望有人在转载时注名出处 http://www.jnull.com/jive/thread.jsp?nav=false&forum=7&thread=943 和作者wjp,还有第一段感谢的人。^_^谢谢。
  
  文档的开头先介绍一下我为什么用这些东西作为代理服务器的开发。
  
  一.我试用了squid,socks5,wingate,kingate,觉得squid适用像我们学校这种单位应用。
  它在性能和节省带宽,提高访问速度上很有优势。
  二.我用的操作系统是FreeBSD,虽然我是BSD的超级菜鸟,但是在浏览squid的FAQ时看到,squid可能在FreeBSD上运行是比较快的,加上hj的极力推荐,我相信FreeBSD不会让大家失望的。
  三.刚开始我用的是NSCA这种认证,这也是,中文文档比较多的一种认证方式,但是我觉得它的开发很困难,我是不懂怎么写一个htpasswd的,像改密码,什么的都比较,困难。而且我的直觉上觉的mysql认证会比NSCA快,所以我选用了mysql,还有朋友用LDAP的,很遗憾因为我不会搭建LDAP服务器,就没有用它,感觉上LDAP的表现也会同样出色的。
  
  以上就是我选用这三个组合的原因,不知各位大侠同意否。
  
  操作系统和软件的安装,本人学FreeBSD没有一个星期,全靠hj的帮助,还有FreeBSD手册才搞定得,我装的是FreeBSD4.8从ftp.freeBSD.org上下载的mini版,大家可以参考FreeBSD的手册 http://people.freebsdchina.org/kinki 我现在会的就是能把FreeBSD安装完,设好ip上网,大家肯定比我厉害^_^。
  
  现在的BSD上是没有squid和mysql的,我先安装squid。我是用ports安装的(假如你不会和我一样菜的话,我建议你仔细读一下 http://www.jnull.com/jive/thread.jsp?forum=7&thread=183&tstart=45&trange=15 这篇文章,我相信你也会喜欢ports的): BSD# cd /usr/ports/www/squid
  BSD# make install
  BSD# make clean 假如你能上网的话那么squid就装完了。我现在装的是squid2.5这个版本的,这个版本和2.4比在认证上有了很大的改变。接着是mysql了。
  BSD# cd /usr/ports/databases/mysql323-server
  BSD# make install
  BSD# make clean 同样的easy,哈哈!我太喜欢ports了。我装的是mysql3-23-58这个版本。接下来去下载squid的认证模块 http://people.fsn.hu/~airween/mysql_auth/mysql_auth-0.5.tar.gz 我把它放在/tmp下(这儿是e文的文档http://people.fsn.hu/~airween/mysql_auth/ )。
  BSD# tar -zxvf mysql_auth-0.5.tar.gz
  BSD# cd mysql_auth-0.5
  BSD# make
  BSD# make install 当你make时可能会出错,问题出在Makefile这个文件上,那个mysql_auth的作者他libmysqlclients.a这两个文件的位置和我的这两个文件的位置不同就会出错,我贴出我刚开始的错误 BSD# make
  gcc -I/usr/local/include -L/usr/local/lib -c src/mysql_auth.c
  gcc -I/usr/local/include -L/usr/local/lib -c src/confparser.c
  gcc -I/usr/local/include -L/usr/local/lib -c src/mypasswd.c
  gcc -o mysql_auth src/mysql_auth.c src/confparser.c -lmysqlclient -I/usr/local/include -L/usr/local/lib
  /usr/libexec/elf/ld: cannot find -lmysqlclient
  *** Error code 1
  
  Stop in /tmp/10-13/mysql_auth-0.5. 以上就是那两个文件不对的错误,我是重新修改了Makefile的以下是Makefile的内容 CC = gcc
  CFLAGS = -I/usr/local/include -L/usr/local/lib
  LDFLAGS = -lmysqlclient
  SRC = src
  OBJS = $(SRC)/mysql_auth.o $(SRC)/confparser.o $(SRC)/mypasswd.o
  INSTALL = /usr/bin/install
  CONF = $(SRC)/mysql_auth.conf
  all : mysql_auth mypasswd
  clean:
  rm -rf src/*.o *.o mysql_auth mypasswd
  
  mysql_auth: $(OBJS)
  $(CC) -o $@ $(SRC)/mysql_auth.c $(SRC)/confparser.c $(LDFLAGS) $(CFLAGS)
  
  mypasswd: $(OBJS)
  $(CC) -o $@ $(SRC)/mypasswd.c $(SRC)/confparser.c $(LDFLAGS) $(CFLAGS)
  
  install:
  $(INSTALL) -o nobody -g nogroup -m 755 mysql_auth /usr/local/squid/bin/mysql_auth
  $(INSTALL) -o root -g wheel -m 700 mypasswd /usr/local/bin/mypasswd
  $(INSTALL) -o nobody -g nogroup -m 600 $(CONF) /usr/local/squid/etc/mysql_auth.conf
  $(INSTALL) -o nobody -g nogroup -m 600 $(CONF) /usr/local/squid/etc/mysql_auth.conf.default 要害是 CFLAGS = -I/usr/local/include -L/usr/local/lib 这一行它认为libmysqlclients.a在/usr/local/lib这个目录下,实际上我的在/usr/local/lib/mysql 下,这个就是错误的原因,所以你要改写这句话把它改成你的libmysqlclients.a所在的地方,(这个文件cp 到/usr/local/lib不行我试过,不知道别人怎么样)
  我的就是 CFLAGS = -I/usr/local/include -L/usr/local/lib/mysql 这样就ok了,相信你也能ok的。
  然后在make install,假如这儿过不去,那么可能是这个问题看看你现在有没有对/usr/local/squid/bin和/usr/local/squid/etc
  这个有权限,假如没有改过来,假如没有这个目录那么mkdir建立这两个目录,相信这样之后你就能通过编译了。
  哈哈。差不多快搞定?br> 修改squid.conf
  BSD# cd /usr/local/etc/squid
  BSD# vi squid.conf
  acl password proxy_auth REQUIRED
  http_access allow password
  auth_param basic program /usr/local/squid/bin/mysql_auth 有这三句话,mysql的认证就可以了,但是你要保证squid对mysql_auth有权限。假如什么都是对的认证通不过往往就是mysql_auth的权限问题。
  运行squid看看正常吗, squid –z 先初始话cache,然后 squid -NCd1 假如squid出错你就看看squid的文档去吧,网上又很多的,随便提一句很可能是你的文件夹的权限问题,像log这类看看squid对她有没有权限。

  Squid完成了接着是mysql了我的数据库没有初始话,一些安全措施都没有作,用的是root用户而且密码是空的所以可能和实际做的不一样了,实际的root的应该有密码
  BSD# cd /tmp/10-13/mysql_auth-0.5/scripts
  BSD# cd path/to/mysql_auth-source/scripts
  BSD# mysql -u root -p < create_script
  Enter password: 你的root的密码,假如没有那么就没有这行输入
  To add a user:
  BSD#> mysql –u root-p mysql_auth
  Enter password:
  Welcome message...

  mysql> insert into data values ('wjp', '123456');
  Query OK, 1 row affected (0.00 sec)

  mysql>

  假如你希望data表里存的密码是加密的那么就用password这个函数如下假如这儿用的是加密方式那么mysql_auth.conf中也要指定是加密方式,默认的密码是不加密的。mysql_auth.conf中的选项是 encrypt_password_form NO 改成YES就行了 encrypt_password_form YES shell> mysql -u your_user_name -p mysql_auth
  Enter password:
  Welcome message...

  mysql> insert into data values ('wjp', password("123456"));
  Query OK, 1 row affected (0.00 sec)

到这一步你运行mysql,运行squid,然后设上代理用wjp这个帐号,用123456这个密码就可以了,good luck!^_^

  假如你的mysql不和squid装在一台机器上,你就要配置mysql_auth.conf这个文件了以下是对mysql_auth.conf简单的解释,说明写的很清楚,假如你熟悉mysql的话作这个没有问题的。我也不多说了。假如不太明白的我建议先看看mysql。

  现在来看看mysql_auth.conf

# mysql_auth.conf - an mysql authenticator config file
  # this is the default name. you can call this by other name,
  # but set up it in mysql_auth-source/src/define.h.
  #
  # comment: first character in line is '#'
  # empty line (EOL at first) allowed
  #
  # format of parameters and their values:
  # parameter - SPACE(S) and/or TAB(S) - value
  #
  # IMPORTANT: see the mysql_auth-source/scripts/create_script
  # this configuration file made by this script
  #
  # by Ervin Hegedus, 2002

  # hostname
  #
  # where is the mysql server - the server hostname or IP address;
  # first 'hostname' directive, and after space(s) or tab(s) its
  # value
  #
  # default:
  #这个选项就是指定mysql数据库的,假如不是同一台机器,那么请指定ip或者hostname.
  hostname localhost

  # user
  #
  # which user can connect to database
  # default:
  #这个就是mysql_auth是以什么用户连接mysql数据库的默认是squid,假如你想改变,mysql
  #数据库中应该有这个用户可以登陆的
  user squid

  # password
  #
  # user's password for database, that store the accounts
  # default:
  #连接用的密码
  password squid

  # database
  #
  # mysql database name, where accounts places are
  # default:
  #用到的库
  database mysql_auth

  # next three directives tells what will the select query,
  # like this:
  # SELECT * FROM table WHERE user_column LIKE "username" AND password_column LIKE "password"
  # where username and password comes from client in HTTP header,
  # and user_column and password_column is the columns name in table
  # this is an easy way to tune this program to your existing database

  # table
  #
  # the table name, where accounts exist in user-password pair
  # default:
  #用到的表
  table data

  # user_column
  #
  # user column name in table
  # if you already have a database, what contains user-password
  # pair, you can set it here
  #帐号字段
  user_column user

  # password_column
  #
  # password column name in table
  # like user column name above
  #用户密码字段
  password_column password

  # encrypt_password_form
  #
  # passwords are stored in encrypted form,
  # using mysql internal 'password()' function
  # this mean, you just storing the passwords encrypted format,
  # Squid and clients doesn't use encrypt form!
  # The value is case insensitive (YES/yes or not one of these).
  # For backward compatibility, default is NO.
  #是否使用密码文存储
  encrypt_password_form NO

图片内容