Newer
Older
/* Read access.ars file */
if((file=fopen(str,"r"))!=NULL) {
fgets(session->req.ars,sizeof(session->req.ars),file);
fclose(file);
}
else {
/* If cannot open access.ars, only allow sysop access */
SAFECOPY(session->req.ars,"LEVEL 90");
break;
}
/* Truncate at \r or \n - can use last_slash since I'm done with it.*/
truncsp(session->req.ars);
}
SAFECOPY(str,path);
}
if(session->req.ars[0] && !(check_ars(session->req.ars,session))) {
/* No authentication provided */
sprintf(str,"401 Unauthorized%s%s: Basic realm=\"%s\""
,newline,get_header(HEAD_WWWAUTH),scfg.sys_name);
send_error(str,session);
return(FALSE);
}
return(TRUE);
}
static void respond(http_session_t * session)
{
if(session->http_ver > HTTP_0_9)
send_file=send_headers(session,"200 OK");
if(send_file)
sock_sendfile(session->socket,session->req.request);
close_request(session);
}
void http_session_thread(void* arg)
{
char host_ip[64];
char* host_name;
HOSTENT* host;
SOCKET socket;
http_session_t session=*(http_session_t*)arg;
free(arg); /* now we don't need to worry about freeing the session */
socket=session.socket;
lprintf("%04d Session thread started", session.socket);
thread_up(FALSE /* setuid */);
session.finished=FALSE;
if(startup->options&BBS_OPT_NO_HOST_LOOKUP)
host=NULL;
else
host=gethostbyaddr ((char *)&session.addr.sin_addr
,sizeof(session.addr.sin_addr),AF_INET);
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
if(host!=NULL && host->h_name!=NULL)
host_name=host->h_name;
else
host_name="<no name>";
if(!(startup->options&BBS_OPT_NO_HOST_LOOKUP))
lprintf("%04d Hostname: %s", session.socket, host_name);
if(trashcan(&scfg,host_ip,"ip")) {
close_socket(session.socket);
lprintf("%04d !CLIENT BLOCKED in ip.can: %s", session.socket, host_ip);
thread_down();
return;
}
if(trashcan(&scfg,host_name,"host")) {
close_socket(session.socket);
lprintf("%04d !CLIENT BLOCKED in host.can: %s", session.socket, host_name);
thread_down();
return;
}
active_clients++;
update_clients();
while(!session.finished && server_socket!=INVALID_SOCKET) {
memset(&(session.req), 0, sizeof(session.req));
if(get_req(&session)) {
lprintf("%04d Got request %s method %d version %d"
,session.socket
,session.req.request,session.req.method,session.http_ver);
if((session.http_ver<HTTP_1_0)||parse_headers(&session)) {
if(check_request(&session)) {
respond(&session);
}
}
}
}
active_clients--;
update_clients();
// client_off(session.socket);
thread_down();
lprintf("%04d Session thread terminated (%u clients, %u threads remain)"
,socket, active_clients, thread_count);
}
void DLLCALL web_terminate(void)
{
recycle_server=FALSE;
if(server_socket!=INVALID_SOCKET) {
lprintf("%04d Web Terminate: closing socket",server_socket);
close_socket(server_socket);
server_socket=INVALID_SOCKET;
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
}
}
static void cleanup(int code)
{
free_cfg(&scfg);
if(server_socket!=INVALID_SOCKET) {
close_socket(server_socket);
server_socket=INVALID_SOCKET;
}
update_clients();
#ifdef _WINSOCKAPI_
if(WSAInitialized && WSACleanup()!=0)
lprintf("0000 !WSACleanup ERROR %d",ERROR_VALUE);
#endif
thread_down();
status("Down");
lprintf("#### Web Server thread terminated (%u threads remain)", thread_count);
if(startup!=NULL && startup->terminated!=NULL)
startup->terminated(code);
}
const char* DLLCALL web_ver(void)
{
static char ver[256];
char compiler[32];
DESCRIBE_COMPILER(compiler);
sscanf("$Revision$" + 11, "%s", revision);
sprintf(ver,"%s %s%s "
"Compiled %s %s with %s"
,server_name
,revision
#ifdef _DEBUG
," Debug"
#else
,""
#endif
,__DATE__, __TIME__, compiler);
return(ver);
}
void DLLCALL web_server(void* arg)
{
int i;
int result;
time_t start;
WORD host_port;
char path[MAX_PATH+1];
char logstr[256];
SOCKADDR_IN server_addr={0};
SOCKADDR_IN client_addr;
socklen_t client_addr_len;
SOCKET client_socket;
SOCKET high_socket_set;
fd_set socket_set;
time_t t;
time_t initialized=0;
char host_ip[32];
char compiler[32];
http_session_t * session;
struct timeval tv;
startup=(web_startup_t*)arg;
if(startup==NULL) {
sbbs_beep(100,500);
fprintf(stderr, "No startup structure passed!\n");
return;
}
if(startup->size!=sizeof(web_startup_t)) { /* verify size */
sbbs_beep(100,500);
sbbs_beep(300,500);
sbbs_beep(100,500);
fprintf(stderr, "Invalid startup structure!\n");
return;
}
/* Setup intelligent defaults */
if(startup->port==0) startup->port=IPPORT_HTTP;
if(startup->index_file_name[0]==0) SAFECOPY(startup->index_file_name,"index.html");
if(startup->root_dir[0]==0) SAFECOPY(startup->root_dir,"../html");
if(startup->error_dir[0]==0) SAFECOPY(startup->error_dir,"../html/error");
if(startup->max_inactivity==0) startup->max_inactivity=120; /* seconds */
/* Copy html directories */
SAFECOPY(root_dir,startup->root_dir);
SAFECOPY(error_dir,startup->error_dir);
/* Change to absolute path */
prep_dir(scfg.ctrl_dir, root_dir);
prep_dir(scfg.ctrl_dir, error_dir);
startup->recycle_now=FALSE;
recycle_server=TRUE;
do {
thread_up(FALSE /* setuid */);
status("Initializing");
memset(&scfg, 0, sizeof(scfg));
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
,revision
#ifdef _DEBUG
," Debug"
#else
,""
#endif
);
DESCRIBE_COMPILER(compiler);
lprintf("Compiled %s %s with %s", __DATE__, __TIME__, compiler);
srand(time(NULL)); /* Seed random number generator */
sbbs_random(10); /* Throw away first number */
if(!winsock_startup()) {
cleanup(1);
return;
}
t=time(NULL);
lprintf("Initializing on %.24s with options: %lx"
,ctime(&t),startup->options);
/* Initial configuration and load from CNF files */
SAFECOPY(scfg.ctrl_dir,startup->ctrl_dir);
lprintf("Loading configuration files from %s", scfg.ctrl_dir);
scfg.size=sizeof(scfg);
SAFECOPY(logstr,UNKNOWN_LOAD_ERROR);
if(!load_cfg(&scfg, NULL, FALSE, logstr)) {
lprintf("!ERROR %s",logstr);
lprintf("!FAILED to load configuration files");
cleanup(1);
return;
}
scfg_reloaded=TRUE;
sprintf(path,"%smime_types.cfg",scfg.ctrl_dir);
if(!read_mime_types(path)) {
lprintf("!ERROR %d reading %s", ERROR_VALUE,path);
cleanup(1);
return;
}
if(startup->host_name[0]==0)
SAFECOPY(startup->host_name,scfg.sys_inetaddr);
if(!(scfg.sys_misc&SM_LOCAL_TZ) && !(startup->options&BBS_OPT_LOCAL_TIMEZONE)) {
if(putenv("TZ=UTC0"))
lprintf("!putenv() FAILED");
tzset();
}
active_clients=0;
update_clients();
/* open a socket and wait for a client */
server_socket = open_socket(SOCK_STREAM);
if(server_socket == INVALID_SOCKET) {
lprintf("!ERROR %d creating HTTP socket", ERROR_VALUE);
cleanup(1);
return;
}
lprintf("Web Server socket %d opened",server_socket);
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
/*****************************/
/* Listen for incoming calls */
/*****************************/
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_addr.s_addr = htonl(startup->interface_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(startup->port);
if(startup->seteuid!=NULL)
startup->seteuid(FALSE);
result = bind(server_socket,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(startup->seteuid!=NULL)
startup->seteuid(TRUE);
if(result != 0) {
lprintf("!ERROR %d (%d) binding HTTP socket to port %d"
,result, ERROR_VALUE,startup->port);
lprintf(BIND_FAILURE_HELP);
cleanup(1);
return;
}
result = listen(server_socket, 1);
if(result != 0) {
lprintf("!ERROR %d (%d) listening on HTTP socket", result, ERROR_VALUE);
cleanup(1);
return;
}
lprintf("Web Server listening on port %d",startup->port);
status("Listening");
/* signal caller that we've started up successfully */
if(startup->started!=NULL)
startup->started();
lprintf("Web Server thread started.");
while(server_socket!=INVALID_SOCKET) {
/* check for re-cycle semaphores */
if(!(startup->options&BBS_OPT_NO_RECYCLE)) {
sprintf(path,"%swebsrvr.rec",scfg.ctrl_dir);
t=fdate(path);
if(!active_clients && t!=-1 && t>initialized) {
lprintf("0000 Recycle semaphore file (%s) detected",path);
initialized=t;
break;
}
if(!active_clients && startup->recycle_now==TRUE) {
lprintf("0000 Recycle semaphore signaled");
startup->recycle_now=FALSE;
break;
}
}
/* now wait for connection */
FD_ZERO(&socket_set);
FD_SET(server_socket,&socket_set);
high_socket_set=server_socket+1;
tv.tv_sec=2;
tv.tv_usec=0;
if((i=select(high_socket_set,&socket_set,NULL,NULL,&tv))<1) {
if(i==0) {
mswait(1);
continue;
}
if(ERROR_VALUE==EINTR)
lprintf("Web Server listening interrupted");
else if(ERROR_VALUE == ENOTSOCK)
lprintf("Web Server socket closed");
else
lprintf("!ERROR %d selecting socket",ERROR_VALUE);
break;
}
client_addr_len = sizeof(client_addr);
if(FD_ISSET(server_socket,&socket_set))
client_socket = accept(server_socket, (struct sockaddr *)&client_addr
,&client_addr_len);
else {
lprintf("!NO SOCKETS set by select");
continue;
}
if(client_socket == INVALID_SOCKET) {
if(ERROR_VALUE == ENOTSOCK || ERROR_VALUE == EINTR) {
lprintf("Web Server socket closed");
break;
}
lprintf("!ERROR %d accepting connection", ERROR_VALUE);
}
strcpy(host_ip,inet_ntoa(client_addr.sin_addr));
host_port=ntohs(client_addr.sin_port);
lprintf("%04d HTTP connection accepted from: %s port %u"
,client_socket
,host_ip, host_port);
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
if((session=malloc(sizeof(http_session_t)))==NULL) {
lprintf("%04d !ERROR allocating %u bytes of memory for service_client"
,client_socket, sizeof(http_session_t));
mswait(3000);
close_socket(client_socket);
continue;
}
memset(session, 0, sizeof(http_session_t));
session->addr=client_addr;
session->socket=client_socket;
_beginthread(http_session_thread, 0, session);
}
lprintf("Closing Server Socket %d", server_socket);
close_socket(server_socket);
server_socket=INVALID_SOCKET;
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
if(http_threads_running) {
lprintf("Waiting for %d connection threads to terminate...", http_threads_running);
start=time(NULL);
while(http_threads_running) {
if(time(NULL)-start>TIMEOUT_THREAD_WAIT) {
lprintf("!TIMEOUT waiting for %d http thread(s) to "
"terminate", http_threads_running);
break;
}
mswait(100);
}
}
cleanup(0);
if(recycle_server) {
lprintf("Recycling server...");
mswait(2000);
}
} while(recycle_server);
}