1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
--- src/http_auth.c.orig 2016-07-16 10:06:16 UTC
+++ src/http_auth.c
@@ -30,6 +30,7 @@
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
+#include <mysql/mysql.h>
#include "md5.h"
@@ -194,9 +195,119 @@ static int http_auth_get_password(server
fclose(fp);
} else if (p->conf.auth_backend == AUTH_BACKEND_LDAP) {
return 0;
- }
+ } else if (p->conf.auth_backend == AUTH_BACKEND_MYSQL) {
+ MYSQL_RES *result;
+ MYSQL_ROW row;
+ int port = atoi(p->conf.auth_mysql_port->ptr);
+ char q[255];
- return -1;
+ if (p->conf.auth_mysql_socket->ptr != NULL)
+ if (0 == strcmp(p->conf.auth_mysql_socket->ptr, "")) p->conf.auth_mysql_socket->ptr = NULL;
+
+ p->conf.mysql_conn = mysql_init(NULL);
+
+ if (mysql_real_connect(p->conf.mysql_conn, p->conf.auth_mysql_host->ptr, p->conf.auth_mysql_user->ptr, p->conf.auth_mysql_pass->ptr, p->conf.auth_mysql_db->ptr, port, p->conf.auth_mysql_socket->ptr, 0))
+ {
+//#define MY_HOSTING
+
+#ifdef MY_HOSTING
+ char my_full_realm[255];
+ char *my_realm = NULL;
+ char *my_domain = NULL;
+
+ char *uname;
+ size_t unamelen;
+
+ unamelen = strlen(username->ptr);
+ uname = malloc(unamelen*2+1);
+
+ mysql_real_escape_string(p->conf.mysql_conn,
+ uname, username->ptr,
+ (unsigned long)unamelen);
+
+ strcpy(my_full_realm, realm->ptr);
+ my_realm = strtok(my_full_realm, "@");
+
+ if (my_realm != NULL)
+ my_domain = strtok(NULL, "@");
+
+ sprintf(q, "SELECT %s FROM %s, %s WHERE %s='%s' AND %s='%s' AND %s='%s' AND %s=%s",
+ p->conf.auth_mysql_col_pass->ptr,
+
+ p->conf.auth_mysql_users_table->ptr,
+ p->conf.auth_mysql_domains_table->ptr,
+
+ p->conf.auth_mysql_col_user->ptr,
+ uname,
+
+ p->conf.auth_mysql_col_realm->ptr,
+ my_realm,
+
+ p->conf.auth_mysql_col_domain->ptr,
+ my_domain,
+
+ p->conf.auth_mysql_domains_table_col_domain_id->ptr,
+ p->conf.auth_mysql_users_table_col_domain_id->ptr
+ );
+
+ free(uname);
+#else
+ // sanitize username & realm by taguchi@ff.iij4u.or.jp
+ char *uname, *urealm;
+ size_t unamelen, urealmlen;
+
+ unamelen = strlen(username->ptr);
+ urealmlen = strlen(realm->ptr);
+ uname = malloc(unamelen*2+1);
+ urealm = malloc(urealmlen*2+1);
+
+ mysql_real_escape_string(p->conf.mysql_conn,
+ uname, username->ptr,
+ (unsigned long)unamelen);
+
+ mysql_real_escape_string(p->conf.mysql_conn,
+ urealm, realm->ptr,
+ (unsigned long)unamelen);
+
+ mysql_real_escape_string(p->conf.mysql_conn,
+ urealm, realm->ptr,
+ (unsigned long)urealmlen);
+
+ sprintf(q, "SELECT %s FROM %s WHERE %s='%s' AND %s='%s'",
+ p->conf.auth_mysql_col_pass->ptr,
+ p->conf.auth_mysql_users_table->ptr,
+ p->conf.auth_mysql_col_user->ptr,
+ uname,
+ p->conf.auth_mysql_col_realm->ptr,
+ urealm
+ );
+
+ free(uname);
+ free(urealm);
+#endif
+
+ mysql_query(p->conf.mysql_conn, q);
+ result = mysql_store_result(p->conf.mysql_conn);
+ if (mysql_num_rows(result) == 1)
+ {
+ /* found */
+ row = mysql_fetch_row(result);
+ buffer_copy_string_len(password, row[0], strlen(row[0]));
+
+ return 0;
+ } else
+ {
+ /* not found */
+ return -1;
+ }
+
+ mysql_free_result(result);
+ mysql_close(p->conf.mysql_conn);
+
+ p->conf.mysql_conn = NULL;
+ } else
+ return -1;
+ }
}
int http_auth_match_rules(server *srv, array *req, const char *username, const char *group, const char *host) {
@@ -711,6 +822,60 @@ static int http_auth_basic_password_comp
return 0;
#endif
+ } else if (p->conf.auth_backend == AUTH_BACKEND_MYSQL) {
+ /*
+ we check for md5 crypt() now
+ request by Nicola Tiling <nti@w4w.net>
+ */
+ if (password->ptr[0] == '$' && password->ptr[2] == '$')
+ {
+ char salt[32];
+ char *crypted;
+ size_t salt_len = 0;
+ char *dollar = NULL;
+
+ if (NULL == (dollar = strchr(password->ptr + 3, '$'))) {
+ fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
+ return -1;
+ }
+
+ salt_len = dollar - password->ptr;
+
+ if (salt_len > sizeof(salt) - 1)
+ {
+ fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
+ return -1;
+ }
+
+ strncpy(salt, password->ptr, salt_len);
+
+ salt[salt_len] = '\0';
+
+ crypted = crypt(pw, salt);
+
+ if (0 == strcmp(password->ptr, crypted))
+ {
+ return 0;
+ } else {
+ fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
+ }
+ } else
+ /* plain md5 check now */
+ {
+ li_MD5_CTX Md5Ctx;
+ HASH HA1;
+ char a1[256];
+
+ li_MD5_Init(&Md5Ctx);
+ li_MD5_Update(&Md5Ctx, (unsigned char *)pw, strlen(pw));
+ li_MD5_Final(HA1, &Md5Ctx);
+
+ CvtHex(HA1, a1);
+
+ if (0 == strcmp(password->ptr, a1)) {
+ return 0;
+ }
+ }
}
return -1;
}
|