由 wddllyy 在 09-17-2004 22:30 发表:
[原创]Linux下搭建基于MYSQL认证,Apache+php管理的Squid代理系统
几个月前自己随便搞了一个Squid+mysql的代理认证,apache+php+mysql的代理帐号管理系统,经验拿来和大家一起分享.呵呵.
Squid的Mysql的认证并不是很流行,原因并不是他的性能方面的原因,而是简单的代理认证只需要Ncsa之类简单的密码文件操作,而复杂的多功能的有LDAP数据库,但是我选择了Mysql, 因为我看中了它的网络操作的方便性。Apache+php+mysql正是最流行的经典组合.
我们的代理认证系统最终选择了Squid+Mysql+Php+Apache其中
Squid+mysql组成了认证的效验和认证以后的代理服务。
Mysql+Php+Apache组成了前台的代理用户管理系统。这样就能通过web操作来完成用户的管理的操作。
1 Squid端认证代码的编写
用c语言编写一段Squid能够加载的认证程序,通过这个认证程序来访问Mysql数据库,完成认证的过程。
#include
1<stdio.h>
2
3#include <stdlib.h>
4
5#include <string.h>
6
7#include "mysql.h"
8
9
10
11/* comment out next line if you use clear text password in MySQL DB */
12
13/*#define ENCRYPTED_PASS*/
14
15
16
17/* can use NULL for localhost, current user, or no password */
18
19#define DBHOST "localhost"
20
21#define DBUSER "root"
22
23#define DB "www"
24
25#define DBPASSWORD "*******"
26
27
28
29/* table for the user database for the squid authentication,
30
31column names for auth username and auth password */
32
33#define A_TABLE "user1"
34
35#define A_USERNAME "username"
36
37#define A_PASSWORD "passwd"
38
39
40
41#define BUFSIZE 256
42
43
44
45void main(int argc, char *argv[])
46
47{
48
49char buf[BUFSIZE], qbuf[BUFSIZE];
50
51char *p;
52
53MYSQL mysql,*sock;
54
55MYSQL_RES *res;
56
57
58
59/* make standard output line buffered */
60
61if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
62
63return;
64
65
66
67while (1) {
68
69if (fgets(buf, BUFSIZE, stdin) == NULL)
70
71break;
72
73if ((p = strchr(buf, '\n')) != NULL)
74
75*p = '\0'; /* strip \n */
76
77if ((p = strchr(buf, ' ')) == NULL) {
78
79(void) printf("ERR\n");
80
81continue;
82
83}
84
85*p++ = '\0';
86
87
88
89/* buf is username and p is password now */
90
91
92
93if (!(sock = mysql_connect(&mysql, DBHOST, DBUSER, DBPASSWORD)))
94
95{
96
97/* couldn't connect to database server */
98
99(void) printf("ERR\n");
100
101continue;
102
103}
104
105if (mysql_select_db(sock, DB))
106
107{
108
109/* couldn't use the database */
110
111(void) printf("ERR\n");
112
113mysql_close(sock);
114
115continue;
116
117}
118
119sprintf(qbuf, "select " A_USERNAME " from " A_TABLE " where "
120
121A_USERNAME "='%s' and " A_PASSWORD
122
123
124
125#ifdef ENCRYPTED_PASS
126
127"=password('%s')", buf, p);
128
129#else
130
131"='%s'", buf, p);
132
133#endif
134
135if(mysql_query(sock,qbuf) || !(res=mysql_store_result(sock)))
136
137{
138
139/* query failed */
140
141(void) printf("ERR\n");
142
143mysql_close(sock);
144
145continue;
146
147}
148
149if ( res->row_count !=0 )
150
151(void) printf("OK\n");
152
153else
154
155(void) printf("ERR\n");
156
157mysql_free_result(res);
158
159mysql_close(sock);
160
161}
162
163exit(0);
164
165}
166
167
168
169
170
171代码核心部分就是对Mysql中一张表的访问从而确定客户提供的用户名和密码是否存在于表中。
172
173用gcc编译连接
174
175gcc -I /usr/local/mysql/include -O -o mysql_auth mysql_auth.c \
176
177-L /usr/local/mysql/lib -lmysqlclient -lm
178
179
180
181这样就生成了mysql_auth这个认证程序。代码中的注释很关键,可能对你很有帮助.
182
183
184
1852 Squid配置的修改
186
187auth_param basic program /var/squid/bin/mysql_auth //认证程序的地址
188
189auth_param basic children 5 //初始启动认证个数
190
191auth_param basic realm FreeTown Proxy Caching Domain
192
193auth_p</string.h></stdlib.h></stdio.h>