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

Last change on this file since 1046 was 1046, checked in by Admin, 13 years ago

Fix chk_iprange and the functions to display nocrypt and allowed-fields in WebIf

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