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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/* $Id$ */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <event.h>
#include "bbs.h"
struct timeval tv = {5, 0};
struct event ev;
int clients = 0;
#define READ_BLOCK 256
#define MAX_CLIENTS 10
#define AUTH_PASSWDS BBSHOME "/.AUTH_PASSWDS"
#define ENTRY_SIZE 64
struct client_state {
struct event ev;
int state;
int uid;
int len;
int response;
struct evbuffer *evb;
};
enum {
FSM_ENTER,
FSM_AUTH,
FSM_SETPASSWD,
FSM_RESPOND,
FSM_EXIT
};
/**
* 瑼X亙蝣
* @return 1 - 撖蝣潮航炊, 0 - 撖蝣潭迤蝣
*/
int check_passwd(int uid, char *passwd)
{
char buf[ENTRY_SIZE];
int i, result = 1;
if ((i = open(AUTH_PASSWDS, O_WRONLY)) < 0)
return result;
if (lseek(i, uid * ENTRY_SIZE, SEEK_SET) < 0)
goto end;
if (read(i, buf, ENTRY_SIZE) < ENTRY_SIZE)
goto end;
if (!strcmp(buf, crypt(passwd, buf)))
result = 0;
end:
memset(buf, 0, ENTRY_SIZE);
close(i);
return result;
}
/**
* 閮剖啣蝣
* DES crypt 撠勗末
*/
void set_passwd(int uid, char *passwd)
{
char buf[ENTRY_SIZE];
char saltc[3], c;
int i;
i = 9 * random();
saltc[0] = i & 077;
saltc[1] = (i >> 6) & 077;
for (i = 0; i < 2; i++) {
c = saltc[i] + '.';
if (c > '9')
c += 7;
if (c > 'Z')
c += 6;
saltc[i] = c;
}
saltc[2] = '\0';
memset(buf, 0, sizeof(buf));
strlcpy(buf, crypt(passwd, saltc), sizeof(buf));
if ((i = open(AUTH_PASSWDS, O_WRONLY)) < 0)
return;
if (lseek(i, uid * ENTRY_SIZE, SEEK_SET) < 0)
goto close;
write(i, buf, ENTRY_SIZE);
close:
close(i);
}
void connection_client(int cfd, short event, void *arg)
{
struct client_state *cs = arg;
int cmd;
static char buf[128];
// ignore clients that timeout
if (event & EV_TIMEOUT)
cs->state = FSM_EXIT;
if (event & EV_READ) {
if (evbuffer_read(cs->evb, cfd, READ_BLOCK) < 0)
cs->state = FSM_EXIT;
}
while (1) {
switch (cs->state) {
case FSM_ENTER:
if (EVBUFFER_LENGTH(cs->evb) < sizeof(int))
goto break_out;
evbuffer_remove(cs->evb, &cmd, sizeof(cmd));
cs->state = FSM_AUTH;
case FSM_AUTH:
if (EVBUFFER_LENGTH(cs->evb) < sizeof(int) * 2)
goto break_out;
evbuffer_remove(cs->evb, &cs->uid, sizeof(cs->uid));
evbuffer_remove(cs->evb, &cs->len, sizeof(cs->len));
if (EVBUFFER_LENGTH(cs->evb) < cs->len)
goto break_out;
memset(buf, 0, sizeof(buf));
evbuffer_remove(cs->evb, buf, cs->len);
cs->response = check_passwd(cs->uid, buf);
memset(buf, 0, sizeof(buf));
if (cmd == 2)
cs->state = FSM_SETPASSWD;
else {
cs->state = FSM_RESPOND;
goto break_out;
}
case FSM_SETPASSWD:
if (EVBUFFER_LENGTH(cs->evb) < sizeof(int))
goto break_out;
evbuffer_remove(cs->evb, &cs->len, sizeof(cs->len));
if (EVBUFFER_LENGTH(cs->evb) < cs->len)
goto break_out;
memset(buf, 0, sizeof(buf));
evbuffer_remove(cs->evb, buf, cs->len);
set_passwd(cs->uid, buf);
memset(buf, 0, sizeof(buf));
cs->state = FSM_RESPOND;
goto break_out;
case FSM_RESPOND:
write(cfd, &cs->response, sizeof(cs->response));
cs->state = FSM_EXIT;
case FSM_EXIT:
if (clients == MAX_CLIENTS)
event_add(&ev, NULL);
close(cfd);
evbuffer_free(cs->evb);
free(cs);
clients--;
return;
}
}
break_out:
if (cs->state == FSM_RESPOND)
event_set(&cs->ev, cfd, EV_WRITE, (void *) connection_client, cs);
event_add(&cs->ev, &tv);
}
void connection_accept(int fd, short event, void *arg)
{
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);
int cfd;
if ((cfd = accept(fd, (struct sockaddr *)&clientaddr, &len)) < 0 )
return;
fcntl(cfd, F_SETFL, fcntl(cfd, F_GETFL, 0) | O_NONBLOCK);
struct client_state *cs = calloc(1, sizeof(struct client_state));
cs->state = 0;
cs->evb = evbuffer_new();
event_set(&cs->ev, cfd, EV_READ, (void *) connection_client, cs);
event_add(&cs->ev, &tv);
clients++;
if (clients > MAX_CLIENTS)
event_del(&ev);
}
int main(int argc, char *argv[])
{
int ch, port = 5121, sfd;
char *iface_ip = NULL;
Signal(SIGPIPE, SIG_IGN);
while( (ch = getopt(argc, argv, "p:i:h")) != -1 )
switch( ch ){
case 'p':
port = atoi(optarg);
break;
case 'i':
iface_ip = optarg;
break;
case 'h':
default:
fprintf(stderr, "usage: authserver [-i interface_ip] [-p port]\n");
return 1;
}
if( (sfd = tobind(iface_ip, port)) < 0 )
return 1;
srandom(getpid() + time(NULL));
event_init();
event_set(&ev, sfd, EV_READ | EV_PERSIST, connection_accept, &ev);
event_add(&ev, NULL);
event_dispatch();
return 0;
}
|