source: branches/monitor-improvement/oscam-http.c@ 1029

Last change on this file since 1029 was 1029, checked in by alno, 14 years ago

WebIF: fixed compiler warnings if CS_RDR_INIT_HIST is not set

File size: 36.5 KB
Line 
1//
2// OSCam HTTP server module
3//
4#include "oscam-http-helpers.c"
5#include <stdio.h>
6#include <string.h>
7#include <time.h>
8#include <sys/stat.h>
9#include <dirent.h>
10#include <sys/socket.h>
11
12void refresh_oscam(enum refreshtypes refreshtype){
13int i;
14 switch (refreshtype){
15 case REFR_ACCOUNTS:
16 cs_log("Refresh Accounts requested by WebIF");
17 init_userdb();
18 cs_reinit_clients();
19#ifdef CS_ANTICASC
20 for (i=0; i<CS_MAXPID; i++)
21 if (client[i].typ=='a') {
22 kill(client[i].pid, SIGHUP);
23 break;
24 }
25#endif
26 break;
27
28 case REFR_READERS:
29 cs_log("Refresh Reader");
30 //todo how I can refresh the readers
31 break;
32
33 case REFR_SERVER:
34 cs_log("Refresh Server requested by WebIF");
35 //kill(client[0].pid, SIGHUP);
36 //todo how I can refresh the server after global settings
37 break;
38
39#ifdef CS_ANTICASC
40 case REFR_ANTICASC:
41 cs_log("Refresh Anticascading requested by WebIF");
42 for (i=0; i<CS_MAXPID; i++)
43 if (client[i].typ=='a') {
44 kill(client[i].pid, SIGHUP);
45 break;
46 }
47 break;
48#endif
49 }
50}
51
52void send_oscam_config_global(struct templatevars *vars, FILE *f, struct uriparams *params) {
53 int i;
54
55 if (strcmp(getParam(params, "action"), "execute") == 0){
56 for(i = 0; i < (*params).paramcount; ++i){
57 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
58 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
59 //we use the same function as used for parsing the config tokens
60 chk_t_global((*params).params[i], (*params).values[i]);
61 }
62 }
63 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Global *DONE*</B><BR><BR>");
64 refresh_oscam(REFR_SERVER);
65 }
66 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->srvip));
67 tpl_addVar(vars, 0, "LOGFILE", logfile);
68 tpl_addVar(vars, 0, "PIDFILE", cfg->pidfile);
69 tpl_addVar(vars, 0, "USERFILE", cfg->usrfile);
70 tpl_addVar(vars, 0, "CWLOGDIR", cfg->cwlogdir);
71 tpl_printf(vars, 0, "CLIENTTIMEOUT", "%ld", cfg->ctimeout);
72 tpl_printf(vars, 0, "FALLBACKTIMEOUT", "%ld", cfg->ftimeout);
73 tpl_printf(vars, 0, "CLIENTMAXIDLE", "%d", cfg->cmaxidle);
74 tpl_printf(vars, 0, "CACHEDELAY", "%ld", cfg->delay);
75 tpl_printf(vars, 0, "BINDWAIT", "%d", cfg->bindwait);
76 tpl_printf(vars, 0, "NETPRIO", "%ld", cfg->netprio);
77 tpl_printf(vars, 0, "RESOLVEDELAY", "%d", cfg->resolvedelay);
78 tpl_printf(vars, 0, "SLEEP", "%d", cfg->tosleep);
79 tpl_printf(vars, 0, "UNLOCKPARENTAL", "%d", cfg->ulparent);
80 tpl_printf(vars, 0, "NICE", "%d", cfg->nice);
81 tpl_printf(vars, 0, "SERIALTIMEOUT", "%d", cfg->srtimeout);
82 tpl_printf(vars, 0, "MAXLOGSIZE", "%d", cfg->max_log_size);
83 tpl_printf(vars, 0, "SHOWECMDW", "%d", cfg->show_ecm_dw);
84 tpl_printf(vars, 0, "WAITFORCARDS", "%d", cfg->waitforcards);
85 tpl_printf(vars, 0, "PREFERLOCALCARDS", "%d", cfg->preferlocalcards);
86
87 fputs(tpl_getTpl(vars, "CONFIGGLOBAL"), f);
88}
89
90void send_oscam_config_camd33(struct templatevars *vars, FILE *f, struct uriparams *params) {
91 int i;
92
93 if (strcmp(getParam(params, "action"), "execute") == 0){
94 for(i = 0; i < (*params).paramcount; ++i){
95 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
96 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
97 //we use the same function as used for parsing the config tokens
98 chk_t_camd33((*params).params[i], (*params).values[i]);
99 }
100 }
101 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd33 *DONE*</B><BR><BR>");
102 refresh_oscam(REFR_SERVER);
103 }
104 tpl_printf(vars, 0, "PORT", "%d", cfg->c33_port);
105 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
106 tpl_printf(vars, 0, "PASSIVE", "%d", cfg->c33_passive);
107
108 for (i = 0; i < sizeof(cfg->c33_key); ++i) tpl_printf(vars, 1, "KEY", "%02X",cfg->c33_key[i]);
109 struct s_ip *cip;
110 char *dot="";
111 for (cip = cfg->rad_allowed; cip; cip = cip->next){
112 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
113 if (cip->ip[0] == cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
114 dot=",";
115 }
116
117 fputs(tpl_getTpl(vars, "CONFIGCAMD33"), f);
118}
119
120void send_oscam_config_camd35(struct templatevars *vars, FILE *f, struct uriparams *params) {
121 int i;
122 if (strcmp(getParam(params, "action"),"execute") == 0){
123 for(i = 0; i < (*params).paramcount; ++i){
124 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
125 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
126 //we use the same function as used for parsing the config tokens
127 chk_t_camd35((*params).params[i], (*params).values[i]);
128 }
129 }
130 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 *DONE*</B><BR><BR>");
131 refresh_oscam(REFR_SERVER);
132 }
133 tpl_printf(vars, 0, "PORT", "%d", cfg->c35_port);
134 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
135
136 fputs(tpl_getTpl(vars, "CONFIGCAMD35"), f);
137}
138
139void send_oscam_config_newcamd(struct templatevars *vars, FILE *f, struct uriparams *params) {
140 int i;
141 if (strcmp(getParam(params, "action"),"execute") == 0){
142 for(i = 0; i < (*params).paramcount; ++i){
143 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
144 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
145 //we use the same function as used for parsing the config tokens
146 chk_t_newcamd((*params).params[i], (*params).values[i]);
147 }
148 }
149 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration newcamd *DONE*</B><BR><BR>");
150 refresh_oscam(REFR_SERVER);
151 }
152 int j;
153 char *dot1, *dot2;
154 if (cfg->ncd_ptab.nports>0){
155 dot1 = "";
156 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
157 dot2 = ":";
158 tpl_printf(vars, 1, "PORT", "%s%d@%04X", dot1, cfg->ncd_ptab.ports[i].s_port, cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
159 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
160 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
161 tpl_printf(vars, 1, "PORT", "%s%lX", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
162 dot2 = ",";
163 }
164 }
165 dot1=";";
166 }
167 }
168
169 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
170 for (i=0;i<14;i++) tpl_printf(vars, 1, "KEY", "%02X", cfg->ncd_key[i]);
171
172 fputs(tpl_getTpl(vars, "CONFIGNEWCAMD"), f);
173}
174
175void send_oscam_config_radegast(struct templatevars *vars, FILE *f, struct uriparams *params) {
176 int i;
177 if (strcmp(getParam(params, "action"),"execute") == 0){
178 for(i = 0; i < (*params).paramcount; ++i){
179 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
180 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
181 //we use the same function as used for parsing the config tokens
182 chk_t_radegast((*params).params[i], (*params).values[i]);
183 }
184 }
185 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Radegast *DONE*</B><BR><BR>");
186 refresh_oscam(REFR_SERVER);
187 }
188 tpl_printf(vars, 0, "PORT", "%d", cfg->rad_port);
189 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
190 tpl_addVar(vars, 0, "USER", cfg->rad_usr);
191
192 struct s_ip *cip;
193 char *dot="";
194 for (cip=cfg->rad_allowed; cip; cip=cip->next){
195 tpl_printf(vars, 1, "ALLOWED", "%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
196 if (cip->ip[0] == cip->ip[1]) tpl_printf(vars, 1, "ALLOWED", "-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
197 dot=",";
198 }
199
200 fputs(tpl_getTpl(vars, "CONFIGRADEGAST"), f);
201}
202
203void send_oscam_config_cccam(struct templatevars *vars, FILE *f, struct uriparams *params) {
204 int i;
205 if (strcmp(getParam(params, "action"),"execute") == 0){
206 for(i = 0; i < (*params).paramcount; ++i){
207 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
208 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
209 //we use the same function as used for parsing the config tokens
210 chk_t_cccam((*params).params[i], (*params).values[i]);
211 }
212 }
213 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR>Configuration Cccam Do not yet implemented<BR><BR>");
214 refresh_oscam(REFR_SERVER);
215 }
216 fputs(tpl_getTpl(vars, "CONFIGCCCAM"), f);
217}
218
219void send_oscam_config_gbox(struct templatevars *vars, FILE *f, struct uriparams *params) {
220 int i;
221 if (strcmp(getParam(params, "action"),"execute") == 0){
222 for(i = 0; i < (*params).paramcount; ++i){
223 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
224 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
225 //we use the same function as used for parsing the config tokens
226 chk_t_gbox((*params).params[i], (*params).values[i]);
227 }
228 }
229 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Gbox *DONE*</B><BR><BR>");
230 refresh_oscam(REFR_SERVER);
231 }
232 tpl_printf(vars, 0, "MAXDIST", "%d", cfg->maxdist);
233 for (i=0;i<4;i++) tpl_printf(vars, 1, "PASSWORD", "%02X", cfg->gbox_pwd[i]);
234 tpl_addVar(vars, 0, "IGNORELIST", (char *)cfg->ignorefile);
235 tpl_addVar(vars, 0, "ONLINEINFOS", (char *)cfg->gbxShareOnl);
236 tpl_addVar(vars, 0, "CARDINFOS", (char *)cfg->cardfile);
237 char *dot = "";
238 for (i = 0; i < cfg->num_locals; i++){
239 tpl_printf(vars, 1, "LOCALS", "%s%06lX", dot, cfg->locals[i]);
240 dot=";";
241 }
242 fputs(tpl_getTpl(vars, "CONFIGGBOX"), f);
243}
244
245void send_oscam_config_monitor(struct templatevars *vars, FILE *f, struct uriparams *params) {
246 int i;
247 if (strcmp(getParam(params, "action"),"execute") == 0){
248 for(i = 0; i < (*params).paramcount; ++i){
249 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
250 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
251 //we use the same function as used for parsing the config tokens
252 chk_t_monitor((*params).params[i], (*params).values[i]);
253 }
254 }
255 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Monitor *DONE*</B><BR><BR>");
256 refresh_oscam(REFR_SERVER);
257 }
258 tpl_printf(vars, 0, "MONPORT", "%d", cfg->mon_port);
259 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
260 tpl_printf(vars, 0, "MONPORT", "%d", cfg->mon_aulow);
261 tpl_printf(vars, 0, "HIDECLIENTTO", "%d", cfg->mon_hideclient_to);
262 tpl_printf(vars, 0, "HTTPPORT", "%d", cfg->http_port);
263 tpl_addVar(vars, 0, "HTTPUSER", cfg->http_user);
264 tpl_addVar(vars, 0, "HTTPPASSWORD", cfg->http_pwd);
265 tpl_addVar(vars, 0, "HTTPCSS", cfg->http_css);
266 tpl_printf(vars, 0, "HTTPREFRESH", "%d", cfg->http_refresh);
267 tpl_addVar(vars, 0, "HTTPTPL", cfg->http_tpl);
268
269 struct s_ip *cip;
270 char *dot="";
271 for (cip = cfg->rad_allowed; cip; cip = cip->next){
272 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
273 if (cip->ip[0] == cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
274 dot=",";
275 }
276
277 //Monlevel selector
278 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", cfg->mon_level);
279 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
280
281 fputs(tpl_getTpl(vars, "CONFIGMONITOR"), f);
282}
283
284#ifdef CS_ANTICASC
285void send_oscam_config_anticasc(struct templatevars *vars, FILE *f, struct uriparams *params) {
286 int i;
287 if (strcmp(getParam(params, "action"),"execute") == 0){
288 for(i = 0; i < (*params).paramcount; ++i){
289 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
290 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
291 //we use the same function as used for parsing the config tokens
292 chk_t_ac((*params).params[i], (*params).values[i]);
293 }
294 }
295 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Anticascading *DONE*</B><BR><BR>");
296 refresh_oscam(REFR_ANTICASC);
297 }
298 if (cfg->ac_enabled > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
299 tpl_printf(vars, 0, "NUMUSERS", "%d", "checked");
300 tpl_printf(vars, 0, "SAMPLETIME", "%d", cfg->ac_stime);
301 tpl_printf(vars, 0, "SAMPLES", "%d", cfg->ac_samples);
302 tpl_printf(vars, 0, "PENALTY", "%d", cfg->ac_penalty);
303 tpl_addVar(vars, 0, "ACLOGFILE", cfg->ac_logfile);
304 tpl_printf(vars, 0, "FAKEDELAY", "%d", cfg->ac_fakedelay);
305 tpl_printf(vars, 0, "DENYSAMPLES", "%d", cfg->ac_denysamples);
306 fputs(tpl_getTpl(vars, "CONFIGANTICASC"), f);
307}
308
309#endif
310
311void send_oscam_config(struct templatevars *vars, FILE *f, struct uriparams *params) {
312 char *part = getParam(params, "part");
313 if (!strcmp(part,"camd33")) send_oscam_config_camd33(vars, f, params);
314 else if (!strcmp(part,"camd35")) send_oscam_config_camd35(vars, f, params);
315 else if (!strcmp(part,"newcamd")) send_oscam_config_newcamd(vars, f, params);
316 else if (!strcmp(part,"radegast")) send_oscam_config_radegast(vars, f, params);
317 else if (!strcmp(part,"cccam")) send_oscam_config_cccam(vars, f, params);
318 else if (!strcmp(part,"gbox")) send_oscam_config_gbox(vars, f, params);
319#ifdef CS_ANTICASC
320 else if (!strcmp(part,"anticasc")) send_oscam_config_anticasc(vars, f, params);
321#endif
322 else if (!strcmp(part,"monitor")) send_oscam_config_monitor(vars, f, params);
323 else send_oscam_config_global(vars, f, params);
324}
325
326void send_oscam_reader(struct templatevars *vars, FILE *f) {
327 int ridx;
328 char *ctyp;
329
330 for(ridx=0;ridx<CS_MAXREADER;ridx++){
331 if(!reader[ridx].device[0]) break;
332 switch(reader[ridx].typ){
333 case R_MOUSE : ctyp="mouse"; break;
334 case R_INTERNAL: ctyp="intern"; break;
335 case R_SMART : ctyp="smartreader"; break;
336 case R_CAMD35 : ctyp="camd 3.5x";break;
337 case R_CAMD33 : ctyp="camd 3.3x";break;
338 case R_NEWCAMD : ctyp="newcamd"; break;
339 case R_RADEGAST: ctyp="radegast"; break;
340 case R_SERIAL : ctyp="serial"; break;
341 case R_GBOX : ctyp="gbox"; break;
342#ifdef HAVE_PCSC
343 case R_PCSC : ctyp="pcsc"; break;
344#endif
345 case R_CCCAM : ctyp="cccam"; break;
346 case R_CS378X : ctyp="cs378x"; break;
347 default : ctyp="unknown"; break;
348 }
349 tpl_addVar(vars, 0, "CTYP", ctyp);
350 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
351 tpl_addVar(vars, 0, "READERNAMEENC", tpl_addTmp(vars, urlencode(reader[ridx].label)));
352 tpl_addVar(vars, 1, "READERLIST", tpl_getTpl(vars, "READERSBIT"));
353 }
354 fputs(tpl_getTpl(vars, "READERS"), f);
355}
356
357void send_oscam_reader_config(struct templatevars *vars, FILE *f, struct uriparams *params) {
358 int ridx;
359 char *reader_ = getParam(params, "reader");
360 for(ridx = 0; ridx < CS_MAXREADER && strcmp(reader_, reader[ridx].label) != 0; ++ridx);
361 if(ridx == CS_MAXREADER){
362 tpl_addVar(vars, 0, "MESSAGE", "<BR><BR>Reader not found<BR><BR>");
363 } else if(strcmp(getParam(params, "action"), "execute") == 0){
364 tpl_addVar(vars, 0, "MESSAGE", "<BR><BR>Saving not yet implemented<BR><BR>");
365 refresh_oscam(REFR_READERS);
366 }
367 int i;
368 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
369 tpl_addVar(vars, 0, "DEVICE", reader[ridx].device);
370 tpl_addVar(vars, 0, "NCD_KEY", (char *)reader[ridx].ncd_key);
371 tpl_addVar(vars, 0, "PINCODE", reader[ridx].pincode);
372 tpl_addVar(vars, 0, "EMMFILE", (char *)reader[ridx].emmfile);
373 tpl_addVar(vars, 0, "GBOXPWD", (char *)reader[ridx].gbox_pwd);
374 tpl_printf(vars, 0, "INACTIVITYTIMEOUT", "%d", reader[ridx].tcp_ito);
375 tpl_printf(vars, 0, "RECEIVETIMEOUT", "%d", reader[ridx].tcp_rto);
376 tpl_printf(vars, 0, "DISABLESERVERFILTER", "%d", reader[ridx].ncd_disable_server_filt);
377 tpl_printf(vars, 0, "FALLBACK", "%d", reader[ridx].fallback);
378 tpl_printf(vars, 0, "LOGPORT", "%d", reader[ridx].log_port);
379 tpl_printf(vars, 0, "BOXID", "%ld", reader[ridx].boxid);
380
381 if(reader[ridx].r_port) tpl_printf(vars, 0, "R_PORT", ",%d", reader[ridx].r_port);
382 if(reader[ridx].l_port) {
383 if(reader[ridx].r_port) tpl_printf(vars, 0, "L_PORT", ",%d", reader[ridx].l_port);
384 else tpl_printf(vars, 0, "L_PORT", ",,%d", reader[ridx].l_port);
385 }
386
387 //Group
388 /*restore the settings format of group from long over bitarray*/
389 char *dot = ""; //flag for comma
390 char grpbit[33];
391 long2bitchar(reader[ridx].grp, grpbit);
392 for(i = 0; i < 32; i++){
393 if (grpbit[i] == '1'){
394 tpl_printf(vars, 1, "GRP", "%s%d", dot, i+1);
395 dot = ",";
396 }
397 }
398
399 //services
400 char sidok[33];
401 long2bitchar(reader[ridx].sidtabok, sidok);
402 char sidno[33];
403 long2bitchar(reader[ridx].sidtabno,sidno);
404 struct s_sidtab *sidtab = cfg->sidtab;
405 //build matrix
406 while(sidtab != NULL){
407 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
408 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
409 else tpl_addVar(vars, 0, "CHECKED", "");
410 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDOKBIT"));
411 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
412 else tpl_addVar(vars, 0, "CHECKED", "");
413 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDNOBIT"));
414 sidtab=sidtab->next;
415 }
416
417 // CAID
418 i = 0;
419 CAIDTAB *ctab = &reader[ridx].ctab;
420 while(ctab->caid[i]) {
421 if (i == 0) tpl_printf(vars, 1, "CAIDS", "%04X", ctab->caid[i]);
422 else tpl_printf(vars, 1, "CAIDS", ",%04X", ctab->caid[i]);;
423 if(ctab->mask[i]) tpl_printf(vars, 1, "CAIDS", "&%04X", ctab->mask[i]);
424 i++;
425 }
426 fputs(tpl_getTpl(vars, "READERCONFIG"), f);
427 }
428
429void send_oscam_user_config_edit(struct templatevars *vars, FILE *f, struct uriparams *params){
430 struct s_auth *account, *ptr;
431 char *user;
432
433 if (strcmp(getParam(params, "action"), "Save As") == 0) user = getParam(params, "newuser");
434 else user = getParam(params, "user");
435
436 int i, j;
437
438 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
439
440 // Create a new user if it doesn't yet
441 if (account == NULL){
442 if (!(account=malloc(sizeof(struct s_auth)))){
443 cs_log("Error allocating memory (errno=%d)", errno);
444 return;
445 }
446 if(cfg->account == NULL) cfg->account = account;
447 else {
448 for (ptr = cfg->account; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
449 ptr->next = account;
450 }
451 memset(account, 0, sizeof(struct s_auth));
452 strncpy((char *)account->usr, user, sizeof(account->usr)-1);
453 account->au=(-1);
454 account->monlvl=cfg->mon_level;
455 account->tosleep=cfg->tosleep;
456 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
457 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
458#ifdef CS_ANTICASC
459 account->ac_users=cfg->ac_users;
460 account->ac_penalty=cfg->ac_penalty;
461 account->ac_idx = account->ac_idx + 1;
462#endif
463 tpl_addVar(vars, 1, "MESSAGE", "<b>New user has been added with default settings</b><BR>");
464 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS);
465 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
466 // need to reget account as writing to disk changes account!
467 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
468 }
469
470 if((strcmp(getParam(params, "action"), "Save") == 0) || (strcmp(getParam(params, "action"), "Save As") == 0)){
471 char servicelabels[255]="";
472 //clear group
473 account->grp = 0;
474
475 for(i=0;i<(*params).paramcount;i++){
476 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "user"))){
477 if (!strcmp((*params).params[i], "services"))
478 sprintf(servicelabels + strlen(servicelabels), "%s,", (*params).values[i]);
479 else
480 chk_account((*params).params[i], (*params).values[i], account);
481 }
482 }
483 chk_account("services", servicelabels, account);
484 tpl_addVar(vars, 1, "MESSAGE", "<B>Settings updated</B><BR><BR>");
485 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS);
486 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
487 }
488
489 tpl_addVar(vars, 0, "USERNAME", account->usr);
490 tpl_addVar(vars, 0, "PASSWORD", account->pwd);
491
492 //Expirationdate
493 struct tm * timeinfo = localtime (&account->expirationdate);
494 char buf [80];
495 strftime (buf,80,"%Y-%m-%d",timeinfo);
496 if(strcmp(buf,"1970-01-01")) tpl_addVar(vars, 0, "EXPDATE", buf);
497 //Group
498 /*restore the settings format of group from long over bitarray*/
499 char *dot = ""; //flag for comma
500 char grpbit[33];
501 long2bitchar(account->grp, grpbit);
502 for(i = 0; i < 32; i++){
503 if (grpbit[i] == '1'){
504 tpl_printf(vars, 1, "GROUPS", "%s%d", dot, i+1);
505 dot = ",";
506 }
507 }
508 //Hostname
509 tpl_addVar(vars, 0, "DYNDNS", (char *)account->dyndns);
510
511 //Uniq
512 tpl_printf(vars, 0, "TMP", "UNIQSELECTED%d", account->uniq);
513 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
514
515 //Sleep
516 if(!account->tosleep) tpl_addVar(vars, 0, "SLEEP", "0");
517 else tpl_printf(vars, 0, "SLEEP", "%d", account->tosleep);
518 //Monlevel selector
519 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", account->monlvl);
520 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
521
522 //AU Selector
523 if (!account->au) tpl_addVar(vars, 0, "AUSELECTED", "selected");
524 if (account->autoau == 1) tpl_addVar(vars, 0, "AUTOAUSELECTED", "selected");
525 int ridx;
526 for (ridx=0; ridx<CS_MAXREADER; ridx++){
527 if(!reader[ridx].device[0]) break;
528 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
529 if (account->au == ridx) tpl_addVar(vars, 0, "SELECTED", "selected");
530 else tpl_addVar(vars, 0, "SELECTED", "");
531 tpl_addVar(vars, 1, "RDROPTION", tpl_getTpl(vars, "USEREDITRDRSELECTED"));
532 }
533
534 /* SERVICES */
535 //services - first we have to move the long sidtabok/sidtabno to a binary array
536 char sidok[33];
537 long2bitchar(account->sidtabok,sidok);
538 char sidno[33];
539 long2bitchar(account->sidtabno,sidno);
540 struct s_sidtab *sidtab = cfg->sidtab;
541 //build matrix
542 i=0;
543 while(sidtab != NULL){
544 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
545 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
546 else tpl_addVar(vars, 0, "CHECKED", "");
547 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDOKBIT"));
548 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
549 else tpl_addVar(vars, 0, "CHECKED", "");
550 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDNOBIT"));
551 sidtab=sidtab->next;
552 i++;
553 }
554 /* CAID */
555 dot="";
556 i = 0;
557 CAIDTAB *ctab = &account->ctab;
558 while(ctab->caid[i]) {
559 tpl_printf(vars, 1, "CAIDS", "%s%04X", dot, ctab->caid[i]);
560 if(ctab->mask[i]) tpl_printf(vars, 1, "CAIDS", "&%04X", ctab->mask[i]);
561 if(ctab->cmap[i]) tpl_printf(vars, 1, "CAIDS", ":%04X", ctab->cmap[i]);
562 dot=",";
563 ++i;
564 }
565
566 /*IDENT*/
567 dot="";
568 FTAB *ftab = &account->ftab;
569 for (i = 0; i < ftab->nfilts; ++i){
570 tpl_printf(vars, 1, "IDENTS", "%s%04X", dot, ftab->filts[i].caid);
571 dot=":";
572 for (j = 0; j < ftab->filts[i].nprids; ++j) {
573 tpl_printf(vars, 1, "IDENTS", "%s%06lX", dot, ftab->filts[i].prids[j]);
574 dot=",";
575 }
576 dot=";";
577 }
578
579 /*Betatunnel*/
580 dot="";
581 i = 0;
582 TUNTAB *ttab = &account->ttab;
583 while(ttab->bt_caidfrom[i]) {
584 tpl_printf(vars, 1, "BETATUNNELS", "%s%04X", dot, ttab->bt_caidfrom[i]);
585 if(ttab->bt_srvid[i]) tpl_printf(vars, 1, "BETATUNNELS", ".%04X", ttab->bt_srvid[i]);
586 if(ttab->bt_caidto[i]) tpl_printf(vars, 1, "BETATUNNELS", ":%04X", ttab->bt_caidto[i]);
587 dot=",";
588 ++i;
589 }
590
591#ifdef CS_ANTICASC
592 tpl_printf(vars, 0, "AC_USERS", "%d", account->ac_users);
593 tpl_printf(vars, 0, "AC_PENALTY", "%d", account->ac_penalty);
594#endif
595 fputs(tpl_getTpl(vars, "USEREDIT"), f);
596}
597
598void send_oscam_user_config(struct templatevars *vars, FILE *f, struct uriparams *params) {
599 struct s_auth *account, *account2;
600 char *user = getParam(params, "user");
601 int i, found = 0;
602
603 if (strcmp(getParam(params, "action"), "delete") == 0){
604 account=cfg->account;
605 if(strcmp(account->usr, user) == 0){
606 cfg->account = account->next;
607 free(account);
608 found = 1;
609 } else if (account->next != NULL){
610 do{
611 if(strcmp(account->next->usr, user) == 0){
612 account2 = account->next;
613 account->next = account2->next;
614 free(account2);
615 found = 1;
616 break;
617 }
618 } while ((account = account->next) && (account->next != NULL));
619 }
620
621 if (found > 0){
622 tpl_addVar(vars, 1, "MESSAGE", "<b>Account has been deleted!</b><BR>");
623 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS);
624 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
625 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified user doesn't exist. No deletion will be made!</b><BR>");
626 }
627 /* List accounts*/
628 char *status = "offline";
629 char *expired = "";
630 char *classname="offline";
631 char *lastchan="&nbsp;";
632 time_t now = time((time_t)0);
633 int isec=0;
634
635 for (account=cfg->account; (account) ; account=account->next){
636 expired = ""; classname="offline";
637 if(account->expirationdate && account->expirationdate<time(NULL)){
638 expired = " (expired)";
639 classname = "expired";
640 }
641 status="offline";
642
643 //search account in active clients
644 for (i=0; i<CS_MAXPID; i++)
645 if (!strcmp(client[i].usr, account->usr)){
646 //30 secs without ecm is offline
647 if ((now - client[i].lastecm) < 30){
648 status = "<b>online</b>";classname="online";
649 lastchan = monitor_get_srvname(client[i].last_srvid);
650 isec = now - client[i].last;
651 }
652 }
653 tpl_addVar(vars, 0, "CLASSNAME", classname);
654 tpl_addVar(vars, 0, "USER", account->usr);
655 tpl_addVar(vars, 0, "USERENC", tpl_addTmp(vars, urlencode(account->usr)));
656 tpl_addVar(vars, 0, "STATUS", status);
657 tpl_addVar(vars, 0, "EXPIRED", expired);
658 tpl_addVar(vars, 0, "LASTCHANNEL", lastchan);
659 tpl_printf(vars, 0, "IDLESECS", "%d", isec);
660 tpl_addVar(vars, 1, "USERCONFIGS", tpl_getTpl(vars, "USERCONFIGLISTBIT"));
661 isec = 0;
662 lastchan = "&nbsp;";
663 }
664 fputs(tpl_getTpl(vars, "USERCONFIGLIST"), f);
665}
666
667void send_oscam_entitlement(struct templatevars *vars, FILE *f, struct uriparams *params) {
668 /* build entitlements from reader init history */
669#ifdef CS_RDR_INIT_HIST
670 int ridx;
671 char *p;
672 char *reader_ = getParam(params, "reader");
673 if(strlen(reader_) > 0){
674 for (ridx=0; ridx<CS_MAXREADER && strcmp(reader_, reader[ridx].label) != 0; ridx++);
675 if(ridx<CS_MAXREADER){
676 for (p=(char *)reader[ridx].init_history; *p; p+=strlen(p)+1){
677 tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p);
678 }
679 }
680 tpl_addVar(vars, 0, "READERNAME", reader_);
681#else
682 tpl_addVar(vars, 0, "LOGHISTORY", "The flag CS_RDR_INIT_HIST is not set in your binary<BR>\n");
683#endif
684 }
685 fputs(tpl_getTpl(vars, "ENTITLEMENTS"), f);
686}
687
688void send_oscam_status(struct templatevars *vars, FILE *f) {
689 int i;
690 char *usr;
691 int lsec, isec, cnr, con, cau;
692 time_t now = time((time_t)0);
693 struct tm *lt;
694 for (i=0; i<CS_MAXPID; i++) {
695 if (client[i].pid) {
696 if ((cfg->mon_hideclient_to <= 0) || (((now-client[i].lastecm)/60)<cfg->mon_hideclient_to) ||
697 (((now-client[i].lastemm)/60)<cfg->mon_hideclient_to) || (client[i].typ!='c')){
698 lsec=now-client[i].login;
699 isec=now-client[i].last;
700 usr=client[i].usr;
701
702 if (((client[i].typ=='r') || (client[i].typ=='p')) && (con=cs_idx2ridx(i))>=0) usr=reader[con].label;
703
704 if (client[i].dup) con=2;
705 else if ((client[i].tosleep) && (now-client[i].lastswitch>client[i].tosleep)) con=1;
706 else con=0;
707
708 if (i-cdiff>0) cnr=i-cdiff;
709 else cnr=(i>1) ? i-1 : 0;
710
711 if( (cau=client[i].au+1) && (now-client[i].lastemm)/60 > cfg->mon_aulow) cau=-cau;
712
713 lt=localtime(&client[i].login);
714
715 tpl_printf(vars, 0, "CLIENTPID", "%d", client[i].pid);
716 tpl_printf(vars, 0, "CLIENTTYPE", "%c", client[i].typ);
717 tpl_printf(vars, 0, "CLIENTCNR", "%d", cnr);
718 tpl_addVar(vars, 0, "CLIENTUSER", usr);
719 tpl_printf(vars, 0, "CLIENTCAU", "%d", cau);
720 tpl_printf(vars, 0, "CLIENTCRYPTED", "%d", client[i].crypted);
721 tpl_printf(vars, 0, "CLIENTIP", "%s", cs_inet_ntoa(client[i].ip));
722 tpl_printf(vars, 0, "CLIENTPORT", "%d", client[i].port);
723 tpl_addVar(vars, 0, "CLIENTPROTO", monitor_get_proto(i));
724 tpl_printf(vars, 0, "CLIENTLOGINDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
725 tpl_printf(vars, 0, "CLIENTLOGINTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
726 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%d", lsec);
727 tpl_printf(vars, 0, "CLIENTCAID", "%04X", client[i].last_caid);
728 tpl_printf(vars, 0, "CLIENTSRVID", "%04X", client[i].last_srvid);
729 tpl_addVar(vars, 0, "CLIENTSRVNAME", monitor_get_srvname(client[i].last_srvid));
730 tpl_printf(vars, 0, "CLIENTIDLESECS", "%d", isec);
731 tpl_printf(vars, 0, "CLIENTCON", "%d", con);
732 tpl_printf(vars, 0, "CWOK", "%d", client[i].cwfound);
733 tpl_printf(vars, 0, "CWNOK", "%d", client[i].cwnot);
734 tpl_addVar(vars, 1, "CLIENTSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
735 }
736 }
737}
738
739#ifdef CS_LOGHISTORY
740 for (i=(*loghistidx+3) % CS_MAXLOGHIST; i!=*loghistidx; i=(i+1) % CS_MAXLOGHIST){
741 char *p_usr, *p_txt;
742 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
743 p_txt=p_usr+32;
744 if (p_txt[0]) tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p_txt+8);
745 }
746#else
747 tpl_addVar(vars, 0, "LOGHISTORY", "the flag CS_LOGHISTORY is not set in your binary<BR>\n");
748#endif
749
750 fputs(tpl_getTpl(vars, "STATUS"), f);
751}
752
753void send_oscam_services(struct templatevars *vars, FILE *f) {
754 struct s_sidtab *sidtab = cfg->sidtab;
755 int i;
756
757 while(sidtab != NULL){
758 for (i=0; i<sidtab->num_caid; i++){
759 if (i==0) tpl_printf(vars, 0, "CAIDS", "%04X", sidtab->caid[i]);
760 else tpl_printf(vars, 1, "CAIDS", ",%04X", sidtab->caid[i]);
761 }
762
763 for (i=0; i<sidtab->num_provid; i++){
764 if (i==0) tpl_printf(vars, 0, "PROVIDS", "%ld08X", sidtab->provid[i]);
765 else tpl_printf(vars, 1, "PROVIDS", ",%ld08X", sidtab->provid[i]);
766 }
767
768 for (i=0; i<sidtab->num_srvid; i++){
769 if (i==0) tpl_printf(vars, 0, "SRVIDS", "%04X", sidtab->srvid[i]);
770 else tpl_printf(vars, 1, "SRVIDS", ",%04X", sidtab->srvid[i]);
771 }
772 tpl_addVar(vars, 0, "LABEL", sidtab->label);
773 tpl_printf(vars, 0, "CAIDNUM", "%d", sidtab->num_caid);
774 tpl_printf(vars, 0, "PROVIDNUM", "%d",sidtab->num_provid);
775 tpl_printf(vars, 0, "SRVIDNUM", "%d", sidtab->num_srvid);
776 tpl_addVar(vars, 1, "SIDTABS", tpl_getTpl(vars, "SIDTABBIT"));
777 sidtab=sidtab->next;
778 }
779 fputs(tpl_getTpl(vars, "SIDTAB"), f);
780}
781
782void send_oscam_savetpls(struct templatevars *vars, FILE *f){
783 if(strlen(cfg->http_tpl) > 0){
784 tpl_printf(vars, 0, "CNT", "%d", tpl_saveIncludedTpls(cfg->http_tpl));
785 tpl_addVar(vars, 0, "PATH", cfg->http_tpl);
786 } else tpl_addVar(vars, 0, "CNT", "0");
787 fputs(tpl_getTpl(vars, "SAVETEMPLATES"), f);
788}
789
790int process_request(FILE *f, struct in_addr in) {
791 char buf[4096];
792 char tmp[4096];
793
794 int authok = 0;
795 char expectednonce[64];
796
797 char *method;
798 char *path;
799 char *protocol;
800 char *pch;
801 char *pch2;
802 /* List of possible pages */
803 char *pages[]={ "/config.html",
804 "/readers.html",
805 "/entitlements.html",
806 "/status.html",
807 "/userconfig.html",
808 "/readerconfig.html",
809 "/services.html",
810 "/user_edit.html",
811 "/site.css",
812 "/savetemplates.html"};
813 int pagescnt = sizeof(pages)/sizeof(char *); // Calculate the amount of items in array
814
815 int pgidx = -1;
816 int i;
817 int parsemode = 1;
818 struct uriparams params;
819 params.paramcount = 0;
820
821 /* First line always includes the GET/POST request */
822 if (!fgets(buf, sizeof(buf), f)) return -1;
823 method = strtok(buf, " ");
824 path = strtok(NULL, " ");
825 protocol = strtok(NULL, "\r");
826 if(method == NULL || path == NULL || protocol == NULL) return -1;
827
828 pch=path;
829 /* advance pointer to beginning of query string */
830 while(pch[0] != '?' && pch[0] != '\0') ++pch;
831 if(pch[0] == '?'){
832 pch[0] = '\0';
833 ++pch;
834 }
835
836 /* Map page to our static page definitions */
837 for (i=0; i<pagescnt; i++){
838 if (!strcmp(path, pages[i])) pgidx = i;
839 }
840
841 /* Parse url parameters; parsemode = 1 means parsing next param, parsemode = -1 parsing next
842 value; pch2 points to the beginning of the currently parsed string, pch is the current position */
843 pch2=pch;
844 while(pch[0] != '\0'){
845 if((parsemode == 1 && pch[0] == '=') || (parsemode == -1 && pch[0] == '&')){
846 pch[0] = '\0';
847 urldecode(pch2);
848 if(parsemode == 1) {
849 if(params.paramcount >= MAXGETPARAMS) break;
850 ++params.paramcount;
851 params.params[params.paramcount-1] = pch2;
852 } else {
853 params.values[params.paramcount-1] = pch2;
854 }
855 parsemode = -parsemode;
856 pch2 = pch + 1;
857 }
858 ++pch;
859 }
860 /* last value wasn't processed in the loop yet... */
861 if(parsemode == -1 && params.paramcount <= MAXGETPARAMS){
862 urldecode(pch2);
863 params.values[params.paramcount-1] = pch2;
864 }
865
866 if(strlen(cfg->http_user) == 0 || strlen(cfg->http_pwd) == 0) authok = 1;
867 else calculate_nonce(expectednonce, sizeof(expectednonce)/sizeof(char));
868
869 /* Read remaining request (we're only interested in auth header) */
870 while (fgets(tmp, sizeof(tmp), f)) {
871 if (tmp[0] == '\r' && tmp[1] == '\n') break;
872 else if(authok == 0 && strlen(tmp) > 50 && strncmp(tmp, "Authorization:", 14) == 0 && strstr(tmp, "Digest") != NULL) {
873 authok = check_auth(tmp, method, path, expectednonce);
874 }
875 }
876
877 //cs_debug("%s %d\n", path, pgidx);
878 //for(i=0; i < params.paramcount; ++i) cs_debug("%s : %s\n", params.params[i], params.values[i]);
879
880 fseek(f, 0, SEEK_CUR); // Force change of stream direction
881
882 if(authok != 1){
883 strcpy(tmp, "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\"");
884 strcat(tmp, AUTHREALM);
885 strcat(tmp, "\", qop=\"auth\", opaque=\"\", nonce=\"");
886 strcat(tmp, expectednonce);
887 strcat(tmp, "\"");
888 if(authok == 2) strcat(tmp, ", stale=true");
889 send_headers(f, 401, "Unauthorized", tmp, "text/html");
890 return 0;
891 }
892
893 /*build page*/
894 send_headers(f, 200, "OK", NULL, "text/html");
895 if(pgidx == 8) send_css(f);
896 else {
897 time_t t;
898 struct templatevars *vars = tpl_create();
899 struct tm *lt;
900 time(&t);
901
902 lt=localtime(&t);
903 tpl_addVar(vars, 0, "CS_VERSION", CS_VERSION);
904 tpl_addVar(vars, 0, "CS_SVN_VERSION", CS_SVN_VERSION);
905 if(cfg->http_refresh > 0 && (pgidx == 3 || pgidx == -1)){
906 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
907 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
908 }
909 tpl_printf(vars, 0, "CURDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
910 tpl_printf(vars, 0, "CURTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
911 switch(pgidx){
912 case 0: send_oscam_config(vars, f, &params); break;
913 case 1: send_oscam_reader(vars, f); break;
914 case 2: send_oscam_entitlement(vars, f, &params); break;
915 case 3: send_oscam_status(vars, f); break;
916 case 4: send_oscam_user_config(vars, f, &params); break;
917 case 5: send_oscam_reader_config(vars, f, &params); break;
918 case 6: send_oscam_services(vars, f); break;
919 case 7: send_oscam_user_config_edit(vars, f, &params); break;
920 case 9: send_oscam_savetpls(vars, f); break;
921 default: send_oscam_status(vars, f); break;
922 }
923 tpl_clear(vars);
924 }
925 return 0;
926}
927
928void http_srv() {
929 int i,sock, reuse =1;
930 struct sockaddr_in sin;
931 struct sockaddr_in remote;
932 socklen_t len = sizeof(remote);
933 char *tmp;
934
935 /* Prepare lookup array for conversion between ascii and hex */
936 tmp = malloc(3*sizeof(char));
937 for(i=0; i<256; i++) {
938 snprintf(tmp, 3,"%02x", i);
939 memcpy(hex2ascii[i], tmp, 2);
940 }
941 free(tmp);
942 /* Create random string for nonce value generation */
943 srand(time(NULL));
944 create_rand_str(noncekey,32);
945
946 /* Startup server */
947 if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
948 cs_log("HTTP Server: Creating socket failed! (errno=%d)", errno);
949 return;
950 }
951 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){
952 cs_log("HTTP Server: Setting SO_REUSEADDR via setsockopt failed! (errno=%d)", errno);
953 }
954
955 memset(&sin, 0, sizeof sin);
956 sin.sin_family = AF_INET;
957 sin.sin_addr.s_addr = INADDR_ANY;
958 sin.sin_port = htons(cfg->http_port);
959 if((bind(sock, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
960 cs_log("HTTP Server couldn't bind on port %d (errno=%d). Not starting HTTP!", cfg->http_port, errno);
961 close(sock);
962 return;
963 }
964 if (listen(sock, SOMAXCONN) < 0){
965 cs_log("HTTP Server: Call to listen() failed! (errno=%d)", errno);
966 close(sock);
967 return;
968 }
969 cs_log("HTTP Server listening on port %d", cfg->http_port);
970 while (1)
971 {
972 int s;
973 FILE *f;
974 if((s = accept(sock, (struct sockaddr *) &remote, &len)) < 0){
975 cs_log("HTTP Server: Error calling accept() (errno=%d).", errno);
976 break;
977 }
978
979 f = fdopen(s, "r+");
980 process_request(f, remote.sin_addr);
981 fflush(f);
982 fclose(f);
983 shutdown(s, SHUT_WR);
984 close(s);
985 }
986 close(sock);
987}
Note: See TracBrowser for help on using the repository browser.