source: trunk/oscam-http.c@ 2028

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

remove toggle effect in webif debug setting

File size: 71.7 KB
Line 
1#ifdef WEBIF
2//
3// OSCam HTTP server module
4//
5#include "oscam-http-helpers.c"
6#include <stdio.h>
7#include <string.h>
8#include <time.h>
9#include <sys/stat.h>
10#include <dirent.h>
11#include <sys/socket.h>
12
13extern struct s_reader *reader;
14
15static int running = 1;
16
17void refresh_oscam(enum refreshtypes refreshtype, struct in_addr in) {
18 int i;
19 switch (refreshtype) {
20 case REFR_ACCOUNTS:
21 cs_log("Refresh Accounts requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
22 kill(client[0].pid, SIGHUP);
23#ifdef CS_ANTICASC
24 for (i=0; i<CS_MAXPID; i++)
25 if (client[i].typ=='a') {
26 kill(client[i].pid, SIGHUP);
27 break;
28 }
29#endif
30 break;
31
32 case REFR_READERS:
33#ifdef CS_RDR_INIT_HIST
34 kill(client[0].pid, SIGUSR2);
35 cs_log("Refresh Reader/Tiers requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
36#endif
37 break;
38
39 case REFR_SERVER:
40 cs_log("Refresh Server requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
41 //kill(client[0].pid, SIGHUP);
42 //todo how I can refresh the server after global settings
43 break;
44
45 case REFR_SERVICES:
46 cs_log("Refresh Services requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
47 //init_sidtab();
48 kill(client[0].pid, SIGHUP);
49 break;
50
51#ifdef CS_ANTICASC
52 case REFR_ANTICASC:
53 cs_log("Refresh Anticascading requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
54 for (i=0; i<CS_MAXPID; i++)
55 if (client[i].typ=='a') {
56 kill(client[i].pid, SIGHUP);
57 break;
58 }
59 break;
60#endif
61 }
62}
63
64void send_oscam_config_global(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
65 int i;
66
67 if (strcmp(getParam(params, "action"), "execute") == 0) {
68 for(i = 0; i < (*params).paramcount; ++i) {
69 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
70 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
71 //we use the same function as used for parsing the config tokens
72
73 chk_t_global((*params).params[i], (*params).values[i]);
74 }
75 }
76 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Global done. You should restart Oscam now.</B><BR><BR>");
77 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
78 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
79 }
80 if (cfg->srvip != 0)
81 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->srvip));
82 if (cfg->pidfile != NULL) tpl_addVar(vars, 0, "PIDFILE", cfg->pidfile);
83 if(cfg->disableuserfile == 1)
84 tpl_addVar(vars, 0, "CHKDISABLEUSERFILE", "checked");
85 if (cfg->usrfile != NULL) tpl_addVar(vars, 0, "USERFILE", cfg->usrfile);
86 if(cfg->disablelog == 1)
87 tpl_addVar(vars, 0, "CHKDISABLELOG", "checked");
88 if (cfg->logfile != NULL) tpl_addVar(vars, 0, "LOGFILE", cfg->logfile);
89 if (cfg->cwlogdir != NULL) tpl_addVar(vars, 0, "CWLOGDIR", cfg->cwlogdir);
90 tpl_printf(vars, 0, "USERFILEFLAG", "%d", cfg->usrfileflag);
91 tpl_printf(vars, 0, "CLIENTTIMEOUT", "%ld", cfg->ctimeout);
92 tpl_printf(vars, 0, "FALLBACKTIMEOUT", "%ld", cfg->ftimeout);
93 tpl_printf(vars, 0, "CLIENTMAXIDLE", "%d", cfg->cmaxidle);
94 tpl_printf(vars, 0, "CACHEDELAY", "%ld", cfg->delay);
95 tpl_printf(vars, 0, "BINDWAIT", "%d", cfg->bindwait);
96 tpl_printf(vars, 0, "NETPRIO", "%ld", cfg->netprio);
97 if (cfg->clientdyndns)
98 tpl_addVar(vars, 0, "CHKCLIENTDYNDNS", "checked");
99 tpl_printf(vars, 0, "RESOLVEDELAY", "%d", cfg->resolvedelay);
100 tpl_printf(vars, 0, "SLEEP", "%d", cfg->tosleep);
101 if (cfg->ulparent == 1)
102 tpl_addVar(vars, 0, "UNLOCKPARENTAL", "checked");
103 tpl_printf(vars, 0, "NICE", "%d", cfg->nice);
104 tpl_printf(vars, 0, "SERIALTIMEOUT", "%d", cfg->srtimeout);
105 tpl_printf(vars, 0, "MAXLOGSIZE", "%d", cfg->max_log_size);
106 if (cfg->waitforcards == 1)
107 tpl_addVar(vars, 0, "WAITFORCARDS", "checked");
108 if (cfg->preferlocalcards == 1)
109 tpl_addVar(vars, 0, "PREFERLOCALCARDS", "checked");
110
111 fputs(tpl_getTpl(vars, "CONFIGGLOBAL"), f);
112}
113
114void send_oscam_config_camd33(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
115 int i;
116
117 if (strcmp(getParam(params, "action"), "execute") == 0) {
118 for(i = 0; i < (*params).paramcount; ++i) {
119 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
120 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
121 if (strcmp((*params).params[i], "nocrypt") == 0) {
122 clear_sip(&cfg->c33_plain);
123 }
124 //we use the same function as used for parsing the config tokens
125 chk_t_camd33((*params).params[i], (*params).values[i]);
126 }
127 }
128 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd33 done. You should restart Oscam now.</B><BR><BR>");
129 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
130 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
131 }
132 tpl_printf(vars, 0, "PORT", "%d", cfg->c33_port);
133 if (cfg->c33_srvip != 0)
134 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
135 if (cfg->c33_passive == 1)
136 tpl_addVar(vars, 0, "PASSIVE", "checked");
137
138 for (i = 0; i < (int) sizeof(cfg->c33_key); ++i) tpl_printf(vars, 1, "KEY", "%02X",cfg->c33_key[i]);
139 struct s_ip *cip;
140 char *dot="";
141 for (cip = cfg->c33_plain; cip; cip = cip->next) {
142 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
143 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", cs_inet_ntoa(cip->ip[1]));
144 dot=",";
145 }
146
147 fputs(tpl_getTpl(vars, "CONFIGCAMD33"), f);
148}
149
150void send_oscam_config_camd35(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
151 int i;
152 if (strcmp(getParam(params, "action"),"execute") == 0) {
153 for(i = 0; i < (*params).paramcount; ++i) {
154 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
155 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
156 //we use the same function as used for parsing the config tokens
157 chk_t_camd35((*params).params[i], (*params).values[i]);
158 }
159 }
160 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 done. You should restart Oscam now.</B><BR><BR>");
161 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
162 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
163 }
164 tpl_printf(vars, 0, "PORT", "%d", cfg->c35_port);
165 if (cfg->c35_tcp_srvip != 0)
166 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
167
168 if (cfg->c35_suppresscmd08)
169 tpl_addVar(vars, 0, "SUPPRESSCMD08", "checked");
170
171 fputs(tpl_getTpl(vars, "CONFIGCAMD35"), f);
172}
173
174void send_oscam_config_camd35tcp(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
175 int i;
176 if (strcmp(getParam(params, "action"),"execute") == 0) {
177 clear_ptab(&cfg->c35_tcp_ptab); /*clear Porttab*/
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_camd35_tcp((*params).params[i], (*params).values[i]);
183 }
184 }
185 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 TCP done. You should restart Oscam now.</B><BR><BR>");
186 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
187 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
188 }
189
190 int j;
191 char *dot1, *dot2;
192 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
193 dot1 = "";
194 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i) {
195 tpl_printf(vars, 1, "PORT", "%s%d@%04X", dot1, cfg->c35_tcp_ptab.ports[i].s_port, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].caid);
196 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0) {
197 tpl_printf(vars, 1, "PORT", ":");
198 dot2 = "";
199 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j) {
200 tpl_printf(vars, 1, "PORT", "%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
201 dot2 = ",";
202 }
203 }
204 dot1=";";
205 }
206 }
207 if (cfg->c35_tcp_srvip != 0)
208 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
209
210 fputs(tpl_getTpl(vars, "CONFIGCAMD35TCP"), f);
211}
212
213void send_oscam_config_newcamd(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
214 int i;
215 if (strcmp(getParam(params, "action"),"execute") == 0) {
216 clear_ptab(&cfg->ncd_ptab); /*clear Porttab*/
217 for(i = 0; i < (*params).paramcount; ++i) {
218 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
219 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
220 //we use the same function as used for parsing the config tokens
221 if (strcmp((*params).params[i], "allowed") == 0) {
222 clear_sip(&cfg->ncd_allowed);
223 }
224 chk_t_newcamd((*params).params[i], (*params).values[i]);
225 }
226 }
227 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Newcamd done. You should restart Oscam now.</B><BR><BR>");
228 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
229 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
230 }
231 int j;
232 char *dot1, *dot2;
233 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)) {
234 dot1 = "";
235 for(i = 0; i < cfg->ncd_ptab.nports; ++i) {
236 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);
237 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0) {
238 tpl_printf(vars, 1, "PORT", ":");
239 dot2 = "";
240 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j) {
241 tpl_printf(vars, 1, "PORT", "%s%06X", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
242 dot2 = ",";
243 }
244 }
245 dot1=";";
246 }
247 }
248
249 if (cfg->ncd_srvip != 0)
250 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
251 for (i=0;i<14;i++) tpl_printf(vars, 1, "KEY", "%02X", cfg->ncd_key[i]);
252
253 struct s_ip *cip;
254 char *dot="";
255 for (cip = cfg->ncd_allowed; cip; cip = cip->next) {
256 tpl_printf(vars, 1, "ALLOWED", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
257 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "ALLOWED", "-%s", cs_inet_ntoa(cip->ip[1]));
258 dot=",";
259 }
260
261 if (cfg->ncd_keepalive)
262 tpl_addVar(vars, 0, "KEEPALIVE", "checked");
263 if (cfg->ncd_mgclient)
264 tpl_addVar(vars, 0, "MGCLIENTCHK", "checked");
265
266 fputs(tpl_getTpl(vars, "CONFIGNEWCAMD"), f);
267}
268
269void send_oscam_config_radegast(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
270 int i;
271 if (strcmp(getParam(params, "action"),"execute") == 0) {
272 for(i = 0; i < (*params).paramcount; ++i) {
273 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
274 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
275 if (strcmp((*params).params[i], "allowed") == 0) {
276 clear_sip(&cfg->rad_allowed);
277 }
278 //we use the same function as used for parsing the config tokens
279 chk_t_radegast((*params).params[i], (*params).values[i]);
280 }
281 }
282 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Radegast done. You should restart Oscam now.</B><BR><BR>");
283 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
284 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
285 }
286 tpl_printf(vars, 0, "PORT", "%d", cfg->rad_port);
287 if (cfg->rad_srvip != 0)
288 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
289 tpl_addVar(vars, 0, "USER", cfg->rad_usr);
290
291 struct s_ip *cip;
292 char *dot="";
293 for (cip=cfg->rad_allowed; cip; cip=cip->next) {
294 tpl_printf(vars, 1, "ALLOWED", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
295 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "ALLOWED", "-%s", cs_inet_ntoa(cip->ip[1]));
296 dot=",";
297 }
298
299 fputs(tpl_getTpl(vars, "CONFIGRADEGAST"), f);
300}
301
302void send_oscam_config_cccam(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
303 int i;
304 if (strcmp(getParam(params, "action"),"execute") == 0) {
305 for(i = 0; i < (*params).paramcount; ++i) {
306 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
307 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
308 //we use the same function as used for parsing the config tokens
309 chk_t_cccam((*params).params[i], (*params).values[i]);
310 }
311 }
312 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
313 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
314 }
315
316 tpl_printf(vars, 1, "PORT", "%d", cfg->cc_port);
317 tpl_printf(vars, 0, "RESHARE", "%d", cfg->cc_reshare);
318 tpl_printf(vars, 0, "VERSION", "%s", cfg->cc_version);
319 tpl_printf(vars, 0, "BUILD", "%s", cfg->cc_build);
320
321 fputs(tpl_getTpl(vars, "CONFIGCCCAM"), f);
322}
323
324#ifdef CS_WITH_GBOX
325void send_oscam_config_gbox(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
326 int i;
327 if (strcmp(getParam(params, "action"),"execute") == 0) {
328 for(i = 0; i < (*params).paramcount; ++i) {
329 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
330 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
331 //we use the same function as used for parsing the config tokens
332 chk_t_gbox((*params).params[i], (*params).values[i]);
333 }
334 }
335 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Gbox done. You should restart Oscam now.</B><BR><BR>");
336 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
337 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
338 }
339 tpl_printf(vars, 0, "MAXDIST", "%d", cfg->maxdist);
340 for (i=0;i<4;i++) tpl_printf(vars, 1, "PASSWORD", "%02X", cfg->gbox_pwd[i]);
341 tpl_addVar(vars, 0, "IGNORELIST", (char *)cfg->ignorefile);
342 tpl_addVar(vars, 0, "ONLINEINFOS", (char *)cfg->gbxShareOnl);
343 tpl_addVar(vars, 0, "CARDINFOS", (char *)cfg->cardfile);
344 char *dot = "";
345 for (i = 0; i < cfg->num_locals; i++) {
346 tpl_printf(vars, 1, "LOCALS", "%s%06lX", dot, cfg->locals[i]);
347 dot=";";
348 }
349 fputs(tpl_getTpl(vars, "CONFIGGBOX"), f);
350}
351#endif
352
353void send_oscam_config_monitor(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
354 int i;
355 if (strcmp(getParam(params, "action"),"execute") == 0) {
356
357 //cleanup
358 clear_sip(&cfg->mon_allowed);
359 clear_sip(&cfg->http_allowed);
360
361 for(i = 0; i < (*params).paramcount; ++i) {
362 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
363 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
364
365 //we use the same function as used for parsing the config tokens
366 if (strstr((*params).params[i], "http")) {
367 chk_t_webif((*params).params[i], (*params).values[i]);
368 } else {
369 chk_t_monitor((*params).params[i], (*params).values[i]);
370 }
371 }
372 }
373 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Monitor done. You should restart Oscam now.</B><BR><BR>");
374 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
375 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
376 }
377 tpl_printf(vars, 0, "MONPORT", "%d", cfg->mon_port);
378 if (cfg->mon_srvip != 0)
379 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
380 tpl_printf(vars, 0, "AULOW", "%d", cfg->mon_aulow);
381 tpl_printf(vars, 0, "HIDECLIENTTO", "%d", cfg->mon_hideclient_to);
382 if(cfg->mon_appendchaninfo)
383 tpl_addVar(vars, 0, "APPENDCHANINFO", "checked");
384 tpl_printf(vars, 0, "HTTPPORT", "%d", cfg->http_port);
385 tpl_addVar(vars, 0, "HTTPUSER", cfg->http_user);
386 tpl_addVar(vars, 0, "HTTPPASSWORD", cfg->http_pwd);
387
388 // css style selector
389 if(strlen(cfg->http_css) == 0) {
390 tpl_addVar(vars, 0, "CSSOPTIONS", "<option value=\"\" selected>embedded</option>\n");
391 } else {
392 tpl_addVar(vars, 0, "CSSOPTIONS", "<option value=\"\">embedded</option>\n");
393 }
394
395 DIR *hdir;
396 struct dirent *entry;
397 hdir = opendir(cs_confdir);
398 do {
399 entry = readdir(hdir);
400 if ((entry) && (strstr(entry->d_name, ".css"))) {
401 if (strstr(cfg->http_css, entry->d_name)) {
402 tpl_printf(vars, 1, "CSSOPTIONS", "<option value=\"%s%s\" selected>%s%s</option>\n",cs_confdir,entry->d_name,cs_confdir,entry->d_name);
403 } else {
404 tpl_printf(vars, 1, "CSSOPTIONS", "<option value=\"%s%s\">%s%s</option>\n",cs_confdir,entry->d_name,cs_confdir,entry->d_name);
405 }
406 }
407 } while (entry);
408 closedir(hdir);
409
410 tpl_printf(vars, 0, "HTTPREFRESH", "%d", cfg->http_refresh);
411 tpl_addVar(vars, 0, "HTTPTPL", cfg->http_tpl);
412 tpl_addVar(vars, 0, "HTTPSCRIPT", cfg->http_script);
413 if (cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
414
415 struct s_ip *cip;
416 char *dot="";
417 for (cip = cfg->mon_allowed; cip; cip = cip->next) {
418 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
419 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", cs_inet_ntoa(cip->ip[1]));
420 dot=",";
421 }
422
423 dot="";
424 for (cip = cfg->http_allowed; cip; cip = cip->next) {
425 tpl_printf(vars, 1, "HTTPALLOW", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
426 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "HTTPALLOW", "-%s", cs_inet_ntoa(cip->ip[1]));
427 dot=",";
428 }
429
430 tpl_printf(vars, 0, "HTTPDYNDNS", "%s", cfg->http_dyndns);
431
432 //Monlevel selector
433 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", cfg->mon_level);
434 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
435
436 fputs(tpl_getTpl(vars, "CONFIGMONITOR"), f);
437}
438
439void send_oscam_config_serial(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
440 int i;
441 if (strcmp(getParam(params, "action"),"execute") == 0) {
442 //cfg->ser_device[0]='\0';
443 memset(cfg->ser_device, 0, sizeof(cfg->ser_device));
444 for(i = 0; i < (*params).paramcount; ++i) {
445 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
446 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
447 //we use the same function as used for parsing the config tokens
448 if((*params).values[i][0])
449 chk_t_serial((*params).params[i], (*params).values[i]);
450 }
451 }
452 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Monitor done. You should restart Oscam now.</B><BR><BR>");
453 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
454 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
455 }
456
457 if (cfg->ser_device[0]){
458 char sdevice[512];
459 cs_strncpy(sdevice, cfg->ser_device, sizeof(sdevice));
460 char *ptr;
461 char delimiter[2]; delimiter[0] = 1; delimiter[1] = '\0';
462 ptr = strtok(sdevice, delimiter);
463 while(ptr != NULL) {
464 tpl_printf(vars, 0, "SERIALDEVICE", "%s", ptr);
465 tpl_addVar(vars, 1, "DEVICES", tpl_getTpl(vars, "CONFIGSERIALDEVICEBIT"));
466 ptr = strtok(NULL, delimiter);
467 }
468 }
469
470 tpl_printf(vars, 0, "SERIALDEVICE", "%s", "");
471 tpl_addVar(vars, 1, "DEVICES", tpl_getTpl(vars, "CONFIGSERIALDEVICEBIT"));
472
473 fputs(tpl_getTpl(vars, "CONFIGSERIAL"), f);
474}
475
476#ifdef HAVE_DVBAPI
477void send_oscam_config_dvbapi(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
478 int i;
479 if (strcmp(getParam(params, "action"),"execute") == 0) {
480 for(i = 0; i < (*params).paramcount; ++i) {
481 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
482 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
483 //we use the same function as used for parsing the config tokens
484 chk_t_dvbapi((*params).params[i], (*params).values[i]);
485 }
486 }
487 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration DVB Api done. You should restart Oscam now.</B><BR><BR>");
488 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
489 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
490 }
491
492 if (cfg->dvbapi_enabled > 0) tpl_addVar(vars, 0, "ENABLEDCHECKED", "checked");
493 if (cfg->dvbapi_au > 0) tpl_addVar(vars, 0, "AUCHECKED", "checked");
494 tpl_addVar(vars, 0, "BOXTYPE", cfg->dvbapi_boxtype);
495 tpl_addVar(vars, 0, "USER", cfg->dvbapi_usr);
496
497 fputs(tpl_getTpl(vars, "CONFIGDVBAPI"), f);
498}
499#endif
500
501#ifdef CS_ANTICASC
502void send_oscam_config_anticasc(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
503 int i;
504 if (strcmp(getParam(params, "action"),"execute") == 0) {
505 for(i = 0; i < (*params).paramcount; ++i) {
506 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
507 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
508 //we use the same function as used for parsing the config tokens
509 chk_t_ac((*params).params[i], (*params).values[i]);
510 }
511 }
512 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Anticascading done. You should restart Oscam now.</B><BR><BR>");
513 refresh_oscam(REFR_ANTICASC, in);
514 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
515 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
516 }
517 if (cfg->ac_enabled > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
518 tpl_printf(vars, 0, "NUMUSERS", "%d", cfg->ac_users);
519 tpl_printf(vars, 0, "SAMPLETIME", "%d", cfg->ac_stime);
520 tpl_printf(vars, 0, "SAMPLES", "%d", cfg->ac_samples);
521 tpl_printf(vars, 0, "PENALTY", "%d", cfg->ac_penalty);
522 tpl_addVar(vars, 0, "ACLOGFILE", cfg->ac_logfile);
523 tpl_printf(vars, 0, "FAKEDELAY", "%d", cfg->ac_fakedelay);
524 tpl_printf(vars, 0, "DENYSAMPLES", "%d", cfg->ac_denysamples);
525 fputs(tpl_getTpl(vars, "CONFIGANTICASC"), f);
526}
527#endif
528
529void send_oscam_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
530 char *part = getParam(params, "part");
531 if (!strcmp(part,"camd33")) send_oscam_config_camd33(vars, f, params, in);
532 else if (!strcmp(part,"camd35")) send_oscam_config_camd35(vars, f, params, in);
533 else if (!strcmp(part,"camd35tcp")) send_oscam_config_camd35tcp(vars, f, params, in);
534 else if (!strcmp(part,"newcamd")) send_oscam_config_newcamd(vars, f, params, in);
535 else if (!strcmp(part,"radegast")) send_oscam_config_radegast(vars, f, params, in);
536 else if (!strcmp(part,"cccam")) send_oscam_config_cccam(vars, f, params, in);
537#ifdef CS_WITH_GBOX
538 else if (!strcmp(part,"gbox")) send_oscam_config_gbox(vars, f, params, in);
539#endif
540#ifdef HAVE_DVBAPI
541 else if (!strcmp(part,"dvbapi")) send_oscam_config_dvbapi(vars, f, params, in);
542#endif
543#ifdef CS_ANTICASC
544 else if (!strcmp(part,"anticasc")) send_oscam_config_anticasc(vars, f, params, in);
545#endif
546 else if (!strcmp(part,"monitor")) send_oscam_config_monitor(vars, f, params, in);
547 else if (!strcmp(part,"serial")) send_oscam_config_serial(vars, f, params, in);
548 else send_oscam_config_global(vars, f, params, in);
549}
550
551void send_oscam_reader(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
552 int readeridx, isphysical = 0;
553 char *ctyp;
554 int i;
555 //uchar dummy[1]={0x00};
556
557 if (strcmp(getParam(params, "action"), "reread") == 0) {
558 readeridx = atoi(getParam(params, "ridx"));
559 //reset the counters
560 for (i = 0; i < 4; i++) {
561 reader[readeridx].emmerror[i] = 0;
562 reader[readeridx].emmwritten[i] = 0;
563 reader[readeridx].emmskipped[i] = 0;
564 reader[readeridx].emmblocked[i] = 0;
565 }
566 //write_to_pipe(client[reader[readeridx].cs_idx)].fd_m2c, PIP_ID_CIN, dummy, 1); // do not work for whatever reason
567 refresh_oscam(REFR_READERS, in); // refresh all reader because write pipe seams not work from here
568 }
569
570 for(readeridx = 0; readeridx < CS_MAXREADER; readeridx++) {
571 isphysical = 0;
572
573 if(!reader[readeridx].device[0]) break;
574
575 tpl_addVar(vars, 0, "READERNAME", reader[readeridx].label);
576 tpl_addVar(vars, 0, "READERNAMEENC", tpl_addTmp(vars, urlencode(reader[readeridx].label)));
577
578 switch(reader[readeridx].typ) {
579 case R_MOUSE :
580 ctyp = "mouse";
581 isphysical = 1;
582 break;
583 case R_INTERNAL:
584 ctyp = "intern";
585 isphysical = 1;
586 break;
587 case R_SMART :
588 ctyp = "smartreader";
589 isphysical = 1;
590 break;
591 case R_SERIAL :
592 ctyp = "serial";
593 isphysical = 1;
594 break;
595 case R_DB2COM1 :
596 ctyp = "dbox COM1";
597 isphysical = 1;
598 break;
599 case R_DB2COM2 :
600 ctyp = "dbox COM2";
601 isphysical = 1;
602 break;
603 case R_CAMD35 : ctyp="camd 3.5x";break;
604 case R_CAMD33 : ctyp="camd 3.3x";break;
605 case R_NEWCAMD : ctyp="newcamd"; break;
606 case R_RADEGAST: ctyp="radegast"; break;
607#ifdef CS_WITH_GBOX
608 case R_GBOX : ctyp="gbox"; break;
609#endif
610#ifdef HAVE_PCSC
611 case R_PCSC :
612 ctyp="pcsc";
613 isphysical = 1;
614 break;
615#endif
616 case R_CCCAM : ctyp="cccam"; break;
617 case R_CS378X : ctyp="cs378x"; break;
618 default : ctyp="unknown"; break;
619 }
620
621 tpl_printf(vars, 0, "EMMERRORUK", "%d", reader[readeridx].emmerror[UNKNOWN]);
622 tpl_printf(vars, 0, "EMMERRORG", "%d", reader[readeridx].emmerror[GLOBAL]);
623 tpl_printf(vars, 0, "EMMERRORS", "%d", reader[readeridx].emmerror[SHARED]);
624 tpl_printf(vars, 0, "EMMERRORUQ", "%d", reader[readeridx].emmerror[UNIQUE]);
625
626 tpl_printf(vars, 0, "EMMWRITTENUK", "%d", reader[readeridx].emmwritten[UNKNOWN]);
627 tpl_printf(vars, 0, "EMMWRITTENG", "%d", reader[readeridx].emmwritten[GLOBAL]);
628 tpl_printf(vars, 0, "EMMWRITTENS", "%d", reader[readeridx].emmwritten[SHARED]);
629 tpl_printf(vars, 0, "EMMWRITTENUQ", "%d", reader[readeridx].emmwritten[UNIQUE]);
630
631 tpl_printf(vars, 0, "EMMSKIPPEDUK", "%d", reader[readeridx].emmskipped[UNKNOWN]);
632 tpl_printf(vars, 0, "EMMSKIPPEDG", "%d", reader[readeridx].emmskipped[GLOBAL]);
633 tpl_printf(vars, 0, "EMMSKIPPEDS", "%d", reader[readeridx].emmskipped[SHARED]);
634 tpl_printf(vars, 0, "EMMSKIPPEDUQ", "%d", reader[readeridx].emmskipped[UNIQUE]);
635
636 tpl_printf(vars, 0, "EMMBLOCKEDUK", "%d", reader[readeridx].emmblocked[UNKNOWN]);
637 tpl_printf(vars, 0, "EMMBLOCKEDG", "%d", reader[readeridx].emmblocked[GLOBAL]);
638 tpl_printf(vars, 0, "EMMBLOCKEDS", "%d", reader[readeridx].emmblocked[SHARED]);
639 tpl_printf(vars, 0, "EMMBLOCKEDUQ", "%d", reader[readeridx].emmblocked[UNIQUE]);
640
641
642 if (isphysical == 1) {
643 tpl_printf(vars, 0, "RIDX", "%d", readeridx);
644 tpl_addVar(vars, 0, "REFRICO", ICREF);
645 tpl_addVar(vars, 0, "READERREFRESH", tpl_getTpl(vars, "READERREFRESHBIT"));
646#ifdef CS_RDR_INIT_HIST
647 tpl_addVar(vars, 0, "ENTICO", ICENT);
648 tpl_addVar(vars, 0, "ENTITLEMENT", tpl_getTpl(vars, "READERENTITLEBIT"));
649#endif
650 } else {
651 tpl_printf(vars, 0, "RIDX", "");
652 tpl_addVar(vars, 0, "READERREFRESH","");
653#ifdef CS_RDR_INIT_HIST
654 tpl_addVar(vars, 0, "ENTITLEMENT","");
655#endif
656 }
657
658 tpl_addVar(vars, 0, "CTYP", ctyp);
659 tpl_addVar(vars, 0, "EDIICO", ICEDI);
660 tpl_addVar(vars, 1, "READERLIST", tpl_getTpl(vars, "READERSBIT"));
661
662 }
663 fputs(tpl_getTpl(vars, "READERS"), f);
664}
665
666void send_oscam_reader_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
667 int ridx;
668 char *reader_ = getParam(params, "reader");
669 for(ridx = 0; ridx < CS_MAXREADER && strcmp(reader_, reader[ridx].label) != 0; ++ridx);
670 if(ridx == CS_MAXREADER) {
671 tpl_addVar(vars, 0, "MESSAGE", "<BR><BR>Reader not found<BR><BR>");
672 } else if(strcmp(getParam(params, "action"), "execute") == 0) {
673 tpl_addVar(vars, 0, "MESSAGE", "<BR><BR>Saving not yet implemented<BR><BR>");
674 refresh_oscam(REFR_READERS, in);
675 }
676 int i;
677
678 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
679 tpl_printf(vars, 0, "DEVICE", "%s", reader[ridx].device);
680 tpl_addVar(vars, 0, "NCD_KEY", (char *)reader[ridx].ncd_key);
681 tpl_addVar(vars, 0, "PINCODE", reader[ridx].pincode);
682 //tpl_addVar(vars, 0, "EMMFILE", (char *)reader[ridx].emmfile);
683 tpl_printf(vars, 0, "INACTIVITYTIMEOUT", "%d", reader[ridx].tcp_ito);
684 tpl_printf(vars, 0, "RECEIVETIMEOUT", "%d", reader[ridx].tcp_rto);
685 tpl_printf(vars, 0, "DISABLESERVERFILTER", "%d", reader[ridx].ncd_disable_server_filt);
686 tpl_printf(vars, 0, "FALLBACK", "%d", reader[ridx].fallback);
687 tpl_printf(vars, 0, "LOGPORT", "%d", reader[ridx].log_port);
688 tpl_printf(vars, 0, "BOXID", "%ld", reader[ridx].boxid);
689 tpl_addVar(vars, 0, "USER", reader[ridx].r_usr);
690 tpl_addVar(vars, 0, "PASS", reader[ridx].r_pwd);
691 tpl_addVar(vars, 0, "RSAKEY", (char *)reader[ridx].rsa_mod);
692 tpl_addVar(vars, 0, "BOXKEY", (char *)reader[ridx].nagra_boxkey);
693
694 if (reader[ridx].detect&0x80)
695 tpl_printf(vars, 0, "DETECT", "!%s", RDR_CD_TXT[reader[ridx].detect&0x7f]);
696 else
697 tpl_printf(vars, 0, "DETECT", "%s", RDR_CD_TXT[reader[ridx].detect&0x7f]);
698
699 tpl_printf(vars, 0, "MHZ", "%d", reader[ridx].mhz);
700 tpl_printf(vars, 0, "CARDMHZ", "%d", reader[ridx].cardmhz);
701
702#ifdef CS_WITH_GBOX
703 tpl_addVar(vars, 0, "GBOXPWD", (char *)reader[ridx].gbox_pwd);
704 tpl_addVar(vars, 0, "PREMIUM", reader[ridx].gbox_prem);
705#endif
706
707 if(reader[ridx].r_port) tpl_printf(vars, 0, "R_PORT", ",%d", reader[ridx].r_port);
708 if(reader[ridx].l_port) {
709 if(reader[ridx].r_port) tpl_printf(vars, 0, "L_PORT", ",%d", reader[ridx].l_port);
710 else tpl_printf(vars, 0, "L_PORT", ",,%d", reader[ridx].l_port);
711 }
712
713 //group
714 char *value = mk_t_group((ulong*)reader[ridx].grp);
715 tpl_printf(vars, 0, "GRP", "%s", value);
716 free(value);
717
718 //services
719 char sidok[33];
720 long2bitchar(reader[ridx].sidtabok, sidok);
721 char sidno[33];
722 long2bitchar(reader[ridx].sidtabno,sidno);
723 struct s_sidtab *sidtab = cfg->sidtab;
724 //build matrix
725 i = 0;
726 while(sidtab != NULL) {
727 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
728 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
729 else tpl_addVar(vars, 0, "CHECKED", "");
730 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDOKBIT"));
731 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
732 else tpl_addVar(vars, 0, "CHECKED", "");
733 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDNOBIT"));
734 sidtab=sidtab->next;
735 i++;
736 }
737
738 // CAID
739 value = mk_t_caidtab(&reader[ridx].ctab);
740 tpl_addVar(vars, 0, "CAIDS", value);
741 free(value);
742
743 //ident
744 value = mk_t_ftab(&reader[ridx].ftab);
745 tpl_printf(vars, 0, "IDENTS", "%s\n", value);
746 free(value);
747
748 //class
749 CLASSTAB *clstab = &reader[ridx].cltab;
750 char *dot="";
751 for(i = 0; i < clstab->an; ++i) {
752 tpl_printf(vars, 1, "CLASS", "%s%02x", dot, (int)clstab->aclass[i]);
753 dot=",";
754 }
755 for(i = 0; i < clstab->bn; ++i) {
756 tpl_printf(vars, 0, "CLASS", "%s!%02x", dot, (int)clstab->bclass[i]);
757 dot=",";
758 }
759
760 //chid
761 int j;
762 dot="";
763 FTAB *ftab = &reader[ridx].fchid;
764 for (i = 0; i < ftab->nfilts; ++i) {
765 tpl_printf(vars, 1, "CHIDS", "%s%04X", dot, ftab->filts[i].caid);
766 dot=":";
767 for (j = 0; j < ftab->filts[i].nprids; ++j) {
768 tpl_printf(vars, 1, "CHIDS", "%s%06lX", dot, ftab->filts[i].prids[j]);
769 dot=",";
770 }
771 dot=";";
772 }
773
774 tpl_printf(vars, 0, "SHOWCLS", "%d", reader[ridx].show_cls);
775 tpl_printf(vars, 0, "MAXQLEN", "%d", reader[ridx].maxqlen);
776 tpl_printf(vars, 0, "EMMCACHE", "%d,%d,&d", reader[ridx].cachemm, reader[ridx].rewritemm, reader[ridx].logemm);
777
778 //savenano
779 int all = 1;
780 dot="";
781 for(i = 0; i < 256; ++i) {
782 if(!(reader[ridx].b_nano[i] & 0x02)) {
783 all = 0;
784 break;
785 }
786 }
787 if (all == 1) tpl_printf(vars, 0, "SAVENANO", "all", reader[ridx].maxqlen);
788 else {
789 for(i = 0; i < 256; ++i) {
790 if(reader[ridx].b_nano[i] & 0x02) tpl_printf(vars, 1, "SAVENANO", "%s%02x\n", dot, i);
791 dot=",";
792 }
793 }
794
795 //blocknano
796 dot="";
797 for(i = 0; i < 256; ++i) {
798 if(!(reader[ridx].b_nano[i] & 0x01)) {
799 all = 0;
800 break;
801 }
802 }
803 if (all == 1) tpl_printf(vars, 0, "BLOCKNANO", "all", reader[ridx].maxqlen);
804 else {
805 for(i = 0; i < 256; ++i) {
806 if(reader[ridx].b_nano[i] & 0x01) tpl_printf(vars, 1, "BLOCKNANO", "%s%02x\n", dot, i);
807 dot=",";
808 }
809 }
810
811 tpl_addVar(vars, 0, "CCCVERSION", reader[ridx].cc_version);
812 tpl_addVar(vars, 0, "CCCBUILD", reader[ridx].cc_build);
813 tpl_printf(vars, 0, "CCCMAXHOP", "%d", reader[ridx].cc_maxhop);
814
815 // Show only parameters which needed for the reader
816 switch (reader[ridx].typ) {
817
818 case R_MOUSE :
819 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGMOUSEBIT"));
820 break;
821 case R_SMART :
822 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSMARTBIT"));
823 break;
824 case R_INTERNAL:
825 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGINTERNALBIT"));
826 break;
827 case R_SERIAL :
828 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSERIALBIT"));
829 break;
830 case R_CAMD35 :
831 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCAMD35BIT"));
832 break;
833 case R_CS378X :
834 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCS378XBIT"));
835 break;
836 case R_RADEGAST:
837 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGRADEGASTBIT"));
838 break;
839 case R_NEWCAMD :
840 if ( reader[ridx].ncd_proto == NCD_525 )
841 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGNCD525BIT"));
842 else if ( reader[ridx].ncd_proto == NCD_524 )
843 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGNCD524BIT"));
844 break;
845#ifdef CS_WITH_GBOX
846 case R_GBOX :
847 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGGBOXBIT"));
848 break;
849#endif
850#ifdef HAVE_PCSC
851 case R_PCSC :
852 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGPCSCBIT"));
853 break;
854#endif
855
856 }
857 //READERCONFIGMOUSEBIT
858 fputs(tpl_getTpl(vars, "READERCONFIG"), f);
859}
860
861void send_oscam_user_config_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
862 struct s_auth *account, *ptr;
863 char user[128];
864
865 if (strcmp(getParam(params, "action"), "Save As") == 0) cs_strncpy(user, getParam(params, "newuser"), sizeof(user)/sizeof(char));
866 else cs_strncpy(user, getParam(params, "user"), sizeof(user)/sizeof(char));
867
868 int i;
869
870 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
871
872 // Create a new user if it doesn't yet
873 if (account == NULL) {
874 i = 1;
875 while(strlen(user) < 1) {
876 snprintf(user, sizeof(user)/sizeof(char) - 1, "NEWUSER%d", i);
877 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
878 if(account != NULL) user[0] = '\0';
879 ++i;
880 }
881 if (!(account=malloc(sizeof(struct s_auth)))) {
882 cs_log("Error allocating memory (errno=%d)", errno);
883 return;
884 }
885 if(cfg->account == NULL) cfg->account = account;
886 else {
887 for (ptr = cfg->account; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
888 ptr->next = account;
889 }
890 memset(account, 0, sizeof(struct s_auth));
891 cs_strncpy((char *)account->usr, user, sizeof(account->usr));
892 account->au=(-1);
893 account->monlvl=cfg->mon_level;
894 account->tosleep=cfg->tosleep;
895 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
896 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
897 account->expirationdate=(time_t)NULL;
898#ifdef CS_ANTICASC
899 account->ac_users=cfg->ac_users;
900 account->ac_penalty=cfg->ac_penalty;
901 account->ac_idx = account->ac_idx + 1;
902#endif
903 tpl_addVar(vars, 1, "MESSAGE", "<b>New user has been added with default settings</b><BR>");
904 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
905 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
906 // need to reget account as writing to disk changes account!
907 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
908 }
909
910 if((strcmp(getParam(params, "action"), "Save") == 0) || (strcmp(getParam(params, "action"), "Save As") == 0)) {
911 char servicelabels[255]="";
912 //clear group
913 account->grp = 0;
914 //clear caidtab before it re-readed by chk_t
915 clear_caidtab(&account->ctab);
916
917 for(i=0;i<(*params).paramcount;i++) {
918 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "user")) && (strcmp((*params).params[i], "newuser"))) {
919 if (!strcmp((*params).params[i], "expdate"))
920 account->expirationdate=(time_t)NULL;
921 if (!strcmp((*params).params[i], "services"))
922 sprintf(servicelabels + strlen(servicelabels), "%s,", (*params).values[i]);
923 else
924 chk_account((*params).params[i], (*params).values[i], account);
925 }
926 }
927 chk_account("services", servicelabels, account);
928 tpl_addVar(vars, 1, "MESSAGE", "<B>Settings updated</B><BR><BR>");
929 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
930 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
931 }
932 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
933
934 tpl_addVar(vars, 0, "USERNAME", account->usr);
935 tpl_addVar(vars, 0, "PASSWORD", account->pwd);
936
937 //Disabled
938 if(account->disabled)
939 tpl_addVar(vars, 0, "DISABLEDCHECKED", "checked");
940
941 //Expirationdate
942 struct tm * timeinfo = localtime (&account->expirationdate);
943 char buf [80];
944 strftime (buf,80,"%Y-%m-%d",timeinfo);
945 if(strcmp(buf,"1970-01-01")) tpl_addVar(vars, 0, "EXPDATE", buf);
946
947 //Group
948 char *value = mk_t_group((ulong*)account->grp);
949 tpl_addVar(vars, 0, "GROUPS", value);
950 free(value);
951
952 //Hostname
953 tpl_addVar(vars, 0, "DYNDNS", (char *)account->dyndns);
954
955 //Uniq
956 tpl_printf(vars, 0, "TMP", "UNIQSELECTED%d", account->uniq);
957 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
958
959 //Sleep
960 if(!account->tosleep) tpl_addVar(vars, 0, "SLEEP", "0");
961 else tpl_printf(vars, 0, "SLEEP", "%d", account->tosleep);
962 //Monlevel selector
963 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", account->monlvl);
964 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
965
966 //AU Selector
967 if (!account->au) tpl_addVar(vars, 0, "AUSELECTED", "selected");
968 if (account->autoau == 1) tpl_addVar(vars, 0, "AUTOAUSELECTED", "selected");
969 int ridx;
970 for (ridx=0; ridx<CS_MAXREADER; ridx++) {
971 if(!reader[ridx].device[0]) break;
972 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
973 if (account->au == ridx) tpl_addVar(vars, 0, "SELECTED", "selected");
974 else tpl_addVar(vars, 0, "SELECTED", "");
975 tpl_addVar(vars, 1, "RDROPTION", tpl_getTpl(vars, "USEREDITRDRSELECTED"));
976 }
977
978 /* SERVICES */
979 //services - first we have to move the long sidtabok/sidtabno to a binary array
980 char sidok[33];
981 long2bitchar(account->sidtabok,sidok);
982 char sidno[33];
983 long2bitchar(account->sidtabno,sidno);
984 struct s_sidtab *sidtab = cfg->sidtab;
985 //build matrix
986 i=0;
987 while(sidtab != NULL) {
988 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
989 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
990 else tpl_addVar(vars, 0, "CHECKED", "");
991 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDOKBIT"));
992 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
993 else tpl_addVar(vars, 0, "CHECKED", "");
994 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDNOBIT"));
995 sidtab=sidtab->next;
996 i++;
997 }
998
999 // CAID
1000 value = mk_t_caidtab(&account->ctab);
1001 tpl_addVar(vars, 0, "CAIDS", value);
1002 free(value);
1003
1004 //ident
1005 value = mk_t_ftab(&account->ftab);
1006 tpl_printf(vars, 0, "IDENTS", "%s\n", value);
1007 free(value);
1008
1009 //Betatunnel
1010 value = mk_t_tuntab(&account->ttab);
1011 tpl_addVar(vars, 0, "BETATUNNELS", value);
1012 free(value);
1013
1014 //SUPPRESSCMD08
1015 if (account->c35_suppresscmd08)
1016 tpl_addVar(vars, 0, "SUPPRESSCMD08", "checked");
1017
1018 //Keepalive
1019 if (account->ncd_keepalive)
1020 tpl_addVar(vars, 0, "KEEPALIVE", "checked");
1021
1022#ifdef CS_ANTICASC
1023 tpl_printf(vars, 0, "AC_USERS", "%d", account->ac_users);
1024 tpl_printf(vars, 0, "AC_PENALTY", "%d", account->ac_penalty);
1025#endif
1026 fputs(tpl_getTpl(vars, "USEREDIT"), f);
1027}
1028
1029void send_oscam_user_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1030 struct s_auth *account, *account2;
1031 char *user = getParam(params, "user");
1032 int i, found = 0;
1033 int hideclient = 10;
1034
1035 if (cfg->mon_hideclient_to > 10)
1036 hideclient = cfg->mon_hideclient_to;
1037
1038 if (strcmp(getParam(params, "action"), "reinit") == 0) {
1039 if(!cfg->http_readonly)
1040 refresh_oscam(REFR_ACCOUNTS, in);
1041 }
1042
1043 if (strcmp(getParam(params, "action"), "delete") == 0) {
1044 if(cfg->http_readonly) {
1045 tpl_addVar(vars, 1, "MESSAGE", "<b>Webif is in readonly mode. No deletion will be made!</b><BR>");
1046 } else {
1047 account=cfg->account;
1048 if(strcmp(account->usr, user) == 0) {
1049 cfg->account = account->next;
1050 free(account);
1051 found = 1;
1052 } else if (account->next != NULL) {
1053 do {
1054 if(strcmp(account->next->usr, user) == 0) {
1055 account2 = account->next;
1056 account->next = account2->next;
1057 free(account2);
1058 found = 1;
1059 break;
1060 }
1061 }while ((account = account->next) && (account->next != NULL));
1062 }
1063
1064 if (found > 0) {
1065 tpl_addVar(vars, 1, "MESSAGE", "<b>Account has been deleted!</b><BR>");
1066 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
1067 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
1068 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified user doesn't exist. No deletion will be made!</b><BR>");
1069 }
1070 }
1071
1072
1073
1074 if ((strcmp(getParam(params, "action"), "disable") == 0) || (strcmp(getParam(params, "action"), "enable") == 0)) {
1075 for (account=cfg->account; (account); account=account->next) {
1076 if(strcmp(getParam(params, "user"), account->usr) == 0) {
1077 if(strcmp(getParam(params, "action"), "disable") == 0)
1078 account->disabled = 1;
1079 else
1080 account->disabled = 0;
1081 found = 1;
1082 }
1083 }
1084
1085 if (found > 0) {
1086 tpl_addVar(vars, 1, "MESSAGE", "<b>Account has been switched!</b><BR>");
1087 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
1088 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
1089 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified user doesn't exist. No deletion will be made!</b><BR>");
1090 }
1091
1092 /* List accounts*/
1093 char *status = "offline";
1094 char *expired = "";
1095 char *classname="offline";
1096 char *lastchan="&nbsp;";
1097 time_t now = time((time_t)0);
1098 int isec = 0, isonline = 0;
1099
1100 for (account=cfg->account; (account); account=account->next) {
1101 //clear for next client
1102 expired = ""; classname = "offline"; status = "offline";
1103 isonline = 0;
1104 tpl_addVar(vars, 0, "CWOK", "");
1105 tpl_addVar(vars, 0, "CWNOK", "");
1106 tpl_addVar(vars, 0, "CWIGN", "");
1107 tpl_addVar(vars, 0, "CWTOUT", "");
1108 tpl_addVar(vars, 0, "CWCACHE", "");
1109 tpl_addVar(vars, 0, "CWTUN", "");
1110 tpl_addVar(vars, 0, "CLIENTPROTO","");
1111 tpl_addVar(vars, 0, "IDLESECS","");
1112 tpl_addVar(vars, 0, "CWLASTRESPONSET","");
1113 tpl_addVar(vars, 0, "EMMOK","");
1114 tpl_addVar(vars, 0, "EMMNOK","");
1115 tpl_addVar(vars, 0, "CLIENTPROTO","");
1116 tpl_addVar(vars, 0, "LASTCHANNEL", "");
1117
1118 if(account->expirationdate && account->expirationdate<time(NULL)) {
1119 expired = " (expired)";
1120 classname = "expired";
1121 }
1122 if(account->disabled != 0) {
1123 expired = " (disabled)";
1124 classname = "disabled";
1125 tpl_addVar(vars, 0, "SWITCHICO", ICENA);
1126 tpl_addVar(vars, 0, "SWITCHTITLE", "enable this account");
1127 tpl_addVar(vars, 0, "SWITCH", "enable");
1128 } else {
1129 tpl_addVar(vars, 0, "SWITCHICO", ICDIS);
1130 tpl_addVar(vars, 0, "SWITCHTITLE", "disable this account");
1131 tpl_addVar(vars, 0, "SWITCH", "disable");
1132 }
1133
1134 //search account in active clients
1135 int cwok = 0, cwnok = 0, cwign = 0, cwtout = 0, cwcache = 0, cwtun = 0, emmok = 0, emmnok = 0;
1136 int secs = 0, fullmins =0, mins =0, hours =0, lastresponsetm = 0;
1137 char *proto = "";
1138
1139 for (i=0; i<CS_MAXPID; i++)
1140 if (!strcmp(client[i].usr, account->usr)) {
1141 //set client to offline depending on hideclient_to
1142 if ((now - client[i].lastecm) < hideclient) {
1143 status = "<b>online</b>"; classname="online";
1144 isonline = 1;
1145 proto = monitor_get_proto(i);
1146 lastchan = get_servicename(client[i].last_srvid, client[i].last_caid);
1147 lastresponsetm = client[i].cwlastresptime;
1148 isec = now - client[i].last;
1149 if(isec > 0) {
1150 secs = isec % 60;
1151 if (isec > 60) {
1152 fullmins = isec / 60;
1153 mins = fullmins % 60;
1154 if(fullmins > 60) hours = fullmins / 60;
1155 }
1156 }
1157 }
1158
1159 cwok += client[i].cwfound;
1160 cwnok += client[i].cwnot;
1161 cwign += client[i].cwignored;
1162 cwtout += client[i].cwtout;
1163 cwcache += client[i].cwcache;
1164 cwtun += client[i].cwtun;
1165 emmok += client[i].emmok;
1166 emmnok += client[i].emmnok;
1167 }
1168
1169 if ( isonline > 0 ) {
1170 tpl_printf(vars, 0, "CWOK", "%d", cwok);
1171 tpl_printf(vars, 0, "CWNOK", "%d", cwnok);
1172 tpl_printf(vars, 0, "CWIGN", "%d", cwign);
1173 tpl_printf(vars, 0, "CWTOUT", "%d", cwtout);
1174 tpl_printf(vars, 0, "CWCACHE", "%d", cwcache);
1175 tpl_printf(vars, 0, "CWTUN", "%d", cwtun);
1176 tpl_printf(vars, 0, "EMMOK", "%d", emmok);
1177 tpl_printf(vars, 0, "EMMNOK", "%d", emmnok);
1178 tpl_addVar(vars, 0, "LASTCHANNEL", lastchan);
1179 tpl_printf(vars, 0, "CWLASTRESPONSET", "%d", lastresponsetm);
1180 tpl_addVar(vars, 0, "CLIENTPROTO", proto);
1181 tpl_printf(vars, 0, "IDLESECS", "%02d:%02d:%02d", hours, mins, secs);
1182
1183 }
1184
1185 tpl_addVar(vars, 0, "CLASSNAME", classname);
1186 tpl_addVar(vars, 0, "USER", account->usr);
1187 tpl_addVar(vars, 0, "USERENC", tpl_addTmp(vars, urlencode(account->usr)));
1188 tpl_addVar(vars, 0, "STATUS", status);
1189 tpl_addVar(vars, 0, "EXPIRED", expired);
1190 tpl_addVar(vars, 0, "DELICO", ICDEL);
1191 tpl_addVar(vars, 0, "EDIICO", ICEDI);
1192
1193 tpl_addVar(vars, 1, "USERCONFIGS", tpl_getTpl(vars, "USERCONFIGLISTBIT"));
1194 isec = 0;
1195 lastchan = "&nbsp;";
1196 }
1197
1198 if ((strcmp(getParam(params, "part"), "adduser") == 0) && (!cfg->http_readonly)) {
1199 tpl_addVar(vars, 1, "NEWUSERFORM", tpl_getTpl(vars, "ADDNEWUSER"));
1200 } else {
1201 if(cfg->http_refresh > 0) {
1202 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
1203 tpl_addVar(vars, 0, "REFRESHURL", "userconfig.html");
1204 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1205 }
1206 }
1207
1208 fputs(tpl_getTpl(vars, "USERCONFIGLIST"), f);
1209}
1210
1211void send_oscam_entitlement(struct templatevars *vars, FILE *f, struct uriparams *params) {
1212 /* build entitlements from reader init history */
1213#ifdef CS_RDR_INIT_HIST
1214 int ridx;
1215 char *p;
1216 char *reader_ = getParam(params, "reader");
1217 if(strlen(reader_) > 0) {
1218 for (ridx=0; ridx<CS_MAXREADER && strcmp(reader_, reader[ridx].label) != 0; ridx++);
1219 if(ridx<CS_MAXREADER) {
1220 for (p=(char *)reader[ridx].init_history; *p; p+=strlen(p)+1) {
1221 tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p);
1222 }
1223 }
1224 tpl_addVar(vars, 0, "READERNAME", reader_);
1225 }
1226#else
1227 tpl_addVar(vars, 0, "LOGHISTORY", "Your binary has not been compiled with the \
1228 CS_RDR_INIT_HIST flag (some architectures disable this \
1229 per default to save ressources). Please recompile if you \
1230 need this feature! This is not a bug!<BR>\n");
1231#endif
1232 fputs(tpl_getTpl(vars, "ENTITLEMENTS"), f);
1233}
1234
1235void send_oscam_status(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1236 int i;
1237 char *usr;
1238 int lsec, isec, cnr, con, cau;
1239 time_t now = time((time_t)0);
1240 struct tm *lt;
1241
1242 if (strcmp(getParam(params, "action"), "kill") == 0)
1243 kill(atoi(getParam(params, "pid")), SIGQUIT);
1244
1245 char *debuglvl = getParam(params, "debug");
1246 if(strlen(debuglvl) > 0) {
1247 int lvl = atoi(debuglvl);
1248 if (cfg->debuglvl != lvl) {
1249 cfg->debuglvl = lvl;
1250 kill(client[0].pid, SIGUSR1);
1251 }
1252 }
1253
1254 char *hideidx = getParam(params, "hide");
1255 if(strlen(hideidx) > 0)
1256 client[atoi(hideidx)].wihidden = 1;
1257
1258 char *hideidle = getParam(params, "hideidle");
1259 if(strlen(hideidle) > 0) {
1260 if (atoi(hideidle) == 2) {
1261 for (i=0; i<CS_MAXPID; i++)
1262 client[i].wihidden = 0;
1263 } else {
1264 int oldval = cfg->http_hide_idle_clients;
1265 chk_t_webif("httphideidleclients", hideidle);
1266 if(oldval != cfg->http_hide_idle_clients) {
1267 refresh_oscam(REFR_SERVER, in);
1268 }
1269 }
1270 }
1271
1272 if(cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED1", "selected");
1273 else tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED0", "selected");
1274
1275 for (i=0; i<CS_MAXPID; i++) {
1276 if (client[i].pid && client[i].wihidden != 1) {
1277
1278 if((cfg->http_hide_idle_clients == 1) && (client[i].typ == 'c') && ((now - client[i].lastecm) > cfg->mon_hideclient_to)) continue;
1279
1280 lsec=now-client[i].login;
1281 isec=now-client[i].last;
1282 usr=client[i].usr;
1283
1284 if (((client[i].typ=='r') || (client[i].typ=='p')) && (con=cs_idx2ridx(i))>=0) usr=reader[con].label;
1285
1286 if (client[i].dup) con=2;
1287 else if ((client[i].tosleep) && (now-client[i].lastswitch>client[i].tosleep)) con=1;
1288 else con=0;
1289
1290 if (i-cdiff>0) cnr=i-cdiff;
1291 else cnr=(i>1) ? i-1 : 0;
1292
1293 if( (cau=client[i].au+1) && (now-client[i].lastemm)/60 > cfg->mon_aulow) cau=-cau;
1294
1295 lt=localtime(&client[i].login);
1296
1297 tpl_printf(vars, 0, "HIDEIDX", "%d", i);
1298 tpl_addVar(vars, 0, "HIDEICON", ICHID);
1299 if(client[i].typ == 'c' && !cfg->http_readonly) {
1300 tpl_printf(vars, 0, "CLIENTPID", "%d&nbsp;", client[i].pid);
1301 tpl_printf(vars, 1, "CLIENTPID", "<A HREF=\"status.html?action=kill&pid=%d\" TITLE=\"Kill this client\"><IMG SRC=\"%s\" ALT=\"Kill\" STYLE=\"float:right\"></A>", client[i].pid, ICKIL);
1302 } else {
1303 tpl_printf(vars, 0, "CLIENTPID", "%d&nbsp;", client[i].pid);
1304 }
1305
1306 tpl_printf(vars, 0, "CLIENTTYPE", "%c", client[i].typ);
1307 tpl_printf(vars, 0, "CLIENTCNR", "%d", cnr);
1308 tpl_addVar(vars, 0, "CLIENTUSER", usr);
1309 tpl_printf(vars, 0, "CLIENTCAU", "%d", cau);
1310 tpl_printf(vars, 0, "CLIENTCRYPTED", "%d", client[i].crypted);
1311 tpl_printf(vars, 0, "CLIENTIP", "%s", cs_inet_ntoa(client[i].ip));
1312 tpl_printf(vars, 0, "CLIENTPORT", "%d", client[i].port);
1313 tpl_addVar(vars, 0, "CLIENTPROTO", monitor_get_proto(i));
1314 tpl_printf(vars, 0, "CLIENTLOGINDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
1315 tpl_printf(vars, 0, "CLIENTLOGINTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
1316
1317 int secs = 0, fullmins =0, mins =0, fullhours =0, hours =0, days =0;
1318 if(lsec > 0) {
1319 secs = lsec % 60;
1320 if (lsec > 60) {
1321 fullmins = lsec / 60;
1322 mins = fullmins % 60;
1323 if(fullmins > 60) {
1324 fullhours = fullmins / 60;
1325 hours = fullhours % 24;
1326 days = fullhours / 24;
1327 }
1328 }
1329 }
1330 if(days == 0)
1331 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%02d:%02d:%02d", hours, mins, secs);
1332 else
1333 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
1334
1335 tpl_printf(vars, 0, "CLIENTCAID", "%04X", client[i].last_caid);
1336 tpl_printf(vars, 0, "CLIENTSRVID", "%04X", client[i].last_srvid);
1337
1338 int j, found = 0;
1339 struct s_srvid *srvid = cfg->srvid;
1340
1341 while (srvid != NULL) {
1342 if (srvid->srvid == client[i].last_srvid) {
1343 for (j=0; j < srvid->ncaid; j++) {
1344 if (srvid->caid[j] == client[i].last_caid) {
1345 found = 1;
1346 break;
1347 }
1348 }
1349 }
1350 if (found == 1)
1351 break;
1352 else
1353 srvid = srvid->next;
1354 }
1355
1356 if (found == 1) {
1357 tpl_printf(vars, 0, "CLIENTSRVPROVIDER","%s : ", srvid->prov);
1358 tpl_addVar(vars, 0, "CLIENTSRVNAME", srvid->name);
1359 tpl_addVar(vars, 0, "CLIENTSRVTYPE", srvid->type);
1360 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION", srvid->desc);
1361 } else {
1362 tpl_addVar(vars, 0, "CLIENTSRVPROVIDER","");
1363 tpl_printf(vars, 0, "CLIENTSRVNAME","");
1364 tpl_addVar(vars, 0, "CLIENTSRVTYPE","");
1365 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION","");
1366 }
1367
1368 secs = 0, fullmins =0, mins =0, fullhours =0, hours =0, days =0;
1369 if(isec > 0) {
1370 secs = isec % 60;
1371 if (isec > 60) {
1372 fullmins = isec / 60;
1373 mins = fullmins % 60;
1374 if(fullmins > 60) {
1375 fullhours = fullmins / 60;
1376 hours = fullhours % 24;
1377 days = fullhours / 24;
1378 }
1379 }
1380 }
1381 if(days == 0)
1382 tpl_printf(vars, 0, "CLIENTIDLESECS", "%02d:%02d:%02d", hours, mins, secs);
1383 else
1384 tpl_printf(vars, 0, "CLIENTIDLESECS", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
1385 if(con == 2) tpl_printf(vars, 0, "CLIENTCON", "Duplicate");
1386 else if (con == 1) tpl_printf(vars, 0, "CLIENTCON", "Sleep");
1387 else tpl_printf(vars, 0, "CLIENTCON", "OK");
1388 tpl_addVar(vars, 1, "CLIENTSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
1389 }
1390 }
1391
1392#ifdef CS_LOGHISTORY
1393 for (i=(*loghistidx+3) % CS_MAXLOGHIST; i!=*loghistidx; i=(i+1) % CS_MAXLOGHIST) {
1394 char *p_usr, *p_txt;
1395 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
1396 p_txt=p_usr+32;
1397 if (p_txt[0]) tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p_txt+8);
1398 }
1399#else
1400 tpl_addVar(vars, 0, "LOGHISTORY", "the flag CS_LOGHISTORY is not set in your binary<BR>\n");
1401#endif
1402
1403 tpl_printf(vars, 0, "SDEBUG", "%s&nbsp;%d to&nbsp;\n", "Switch Debug from", cfg->debuglvl);
1404 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=0\" title=\"no debugging (default)\">0</A>&nbsp;\n");
1405 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=1\" title=\"detailed error messages\">1</A>&nbsp;\n");
1406 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=2\" title=\"ATR parsing info, ECM dumps, CW dumps\">2</A>&nbsp;\n");
1407 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=4\" title=\"traffic from/to the reader\">4</A>&nbsp;\n");
1408 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=8\" title=\"traffic from/to the clients\">8</A>&nbsp;\n");
1409 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=16\" title=\"traffic to the reader-device on IFD layer\">16</A>&nbsp;\n");
1410 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=32\" title=\"traffic to the reader-device on I/O layer\">32</A>&nbsp;\n");
1411 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=64\" title=\"EMM logging\">64</A>&nbsp;\n");
1412 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"status.html?debug=255\" title=\"debug all\">255</A>\n");
1413
1414 fputs(tpl_getTpl(vars, "STATUS"), f);
1415}
1416
1417void send_oscam_services_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1418 struct s_sidtab *sidtab,*ptr;
1419 char label[128];
1420 int i;
1421
1422 cs_strncpy(label, strtolower(getParam(params, "service")), sizeof(label)/sizeof(char));
1423
1424 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1425
1426 if (sidtab == NULL) {
1427 i = 1;
1428 while(strlen(label) < 1) {
1429 snprintf(label, sizeof(label)/sizeof(char) - 1, "newservice%d", i);
1430 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab = sidtab->next);
1431 if(sidtab != NULL) label[0] = '\0';
1432 ++i;
1433 }
1434 if (!(sidtab=malloc(sizeof(struct s_sidtab)))) {
1435 cs_log("Error allocating memory (errno=%d)", errno);
1436 return;
1437 }
1438
1439 if(cfg->sidtab == NULL) cfg->sidtab = sidtab;
1440 else {
1441 for (ptr = cfg->sidtab; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
1442 ptr->next = sidtab;
1443 }
1444 memset(sidtab, 0, sizeof(struct s_sidtab));
1445 cs_strncpy((char *)sidtab->label, label, sizeof(sidtab->label));
1446
1447 tpl_addVar(vars, 1, "MESSAGE", "<b>New service has been added</b><BR>");
1448 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1449 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
1450
1451 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1452 }
1453
1454 if (strcmp(getParam(params, "action"), "Save") == 0) {
1455 for(i=0;i<(*params).paramcount;i++) {
1456 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "service"))) {
1457 chk_sidtab((*params).params[i], (*params).values[i], sidtab);
1458 }
1459 }
1460 tpl_addVar(vars, 1, "MESSAGE", "<B>Services updated</B><BR><BR>");
1461 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1462 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
1463
1464 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1465 }
1466
1467 tpl_addVar(vars, 0, "LABEL", sidtab->label);
1468 tpl_addVar(vars, 0, "LABELENC", urlencode(sidtab->label));
1469
1470 for (i=0; i<sidtab->num_caid; i++) {
1471 if (i==0) tpl_printf(vars, 0, "CAIDS", "%04X", sidtab->caid[i]);
1472 else tpl_printf(vars, 1, "CAIDS", ",%04X", sidtab->caid[i]);
1473 }
1474 for (i=0; i<sidtab->num_provid; i++) {
1475 if (i==0) tpl_printf(vars, 0, "PROVIDS", "%06lX", sidtab->provid[i]);
1476 else tpl_printf(vars, 1, "PROVIDS", ",%06lX", sidtab->provid[i]);
1477 }
1478 for (i=0; i<sidtab->num_srvid; i++) {
1479 if (i==0) tpl_printf(vars, 0, "SRVIDS", "%04X", sidtab->srvid[i]);
1480 else tpl_printf(vars, 1, "SRVIDS", ",%04X", sidtab->srvid[i]);
1481 }
1482 fputs(tpl_getTpl(vars, "SERVICEEDIT"), f);
1483}
1484
1485void send_oscam_services(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1486 struct s_sidtab *sidtab, *sidtab2;
1487 char *service = getParam(params, "service");
1488 int i, found = 0;
1489
1490 if (strcmp(getParam(params, "action"), "delete") == 0) {
1491 if(cfg->http_readonly) {
1492 tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry, Webif is in readonly mode. No deletion will be made!</b><BR>");
1493 } else {
1494 sidtab=cfg->sidtab;
1495 if(strcmp(sidtab->label, service) == 0) {
1496 cfg->sidtab = sidtab->next;
1497 free(sidtab);
1498 found = 1;
1499 } else if (sidtab->next != NULL) {
1500 do {
1501 if(strcmp(sidtab->next->label, service) == 0) {
1502 sidtab2 = sidtab->next;
1503 sidtab->next = sidtab2->next;
1504 free(sidtab2);
1505 found = 1;
1506 break;
1507 }
1508 } while ((sidtab = sidtab->next) && (sidtab->next != NULL));
1509 }
1510 if (found > 0) {
1511 tpl_addVar(vars, 1, "MESSAGE", "<b>Service has been deleted!</b><BR>");
1512 if (write_services() == 0) refresh_oscam(REFR_SERVICES, in);
1513 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
1514 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified service doesn't exist. No deletion will be made!</b><BR>");
1515 }
1516 }
1517
1518 sidtab = cfg->sidtab;
1519 // Show List
1520 while(sidtab != NULL) {
1521 tpl_printf(vars, 0, "SID","");
1522 if ((strcmp(getParam(params, "service"), sidtab->label) == 0) && (strcmp(getParam(params, "action"), "list") == 0) ) {
1523 tpl_printf(vars, 0, "SIDCLASS","sidlist");
1524 tpl_printf(vars, 1, "SID", "<div style=\"float:right;background-color:red;color:white\"><A HREF=\"services.html\" style=\"color:white;text-decoration:none\">X</A></div>");
1525 for (i=0; i<sidtab->num_srvid; i++) {
1526 tpl_printf(vars, 1, "SID", "%04X : %s<BR>", sidtab->srvid[i], get_servicename(sidtab->srvid[i], sidtab->caid[0]));
1527 }
1528 } else {
1529 tpl_printf(vars, 0, "SIDCLASS","");
1530 tpl_printf(vars, 0, "SID","<A HREF=\"services.html?service=%s&action=list\">Show Services</A>",tpl_addTmp(vars, urlencode(sidtab->label)));
1531 }
1532 tpl_addVar(vars, 0, "LABELENC", tpl_addTmp(vars, urlencode(sidtab->label)));
1533 tpl_addVar(vars, 0, "LABEL", sidtab->label);
1534 tpl_addVar(vars, 0, "SIDLIST", tpl_getTpl(vars, "SERVICECONFIGSIDBIT"));
1535 tpl_addVar(vars, 0, "EDIICO", ICEDI);
1536 tpl_addVar(vars, 0, "DELICO", ICDEL);
1537 tpl_addVar(vars, 1, "SERVICETABS", tpl_getTpl(vars, "SERVICECONFIGLISTBIT"));
1538 sidtab=sidtab->next;
1539 }
1540 fputs(tpl_getTpl(vars, "SERVICECONFIGLIST"), f);
1541}
1542
1543void send_oscam_savetpls(struct templatevars *vars, FILE *f) {
1544 if(strlen(cfg->http_tpl) > 0) {
1545 tpl_printf(vars, 0, "CNT", "%d", tpl_saveIncludedTpls(cfg->http_tpl));
1546 tpl_addVar(vars, 0, "PATH", cfg->http_tpl);
1547 } else tpl_addVar(vars, 0, "CNT", "0");
1548 fputs(tpl_getTpl(vars, "SAVETEMPLATES"), f);
1549}
1550
1551void send_oscam_shutdown(struct templatevars *vars, FILE *f, struct uriparams *params) {
1552 if (strcmp(getParam(params, "action"), "Shutdown") == 0) {
1553 tpl_addVar(vars, 0, "STYLESHEET", CSS);
1554 tpl_printf(vars, 0, "REFRESHTIME", "%d", SHUTDOWNREFRESH);
1555 tpl_addVar(vars, 0, "REFRESHURL", "status.html");
1556 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1557 tpl_printf(vars, 0, "SECONDS", "%d", SHUTDOWNREFRESH);
1558 fputs(tpl_getTpl(vars, "SHUTDOWN"), f);
1559 running = 0;
1560 } else {
1561 fputs(tpl_getTpl(vars, "PRESHUTDOWN"), f);
1562 }
1563}
1564
1565void send_oscam_script(struct templatevars *vars, FILE *f) {
1566
1567 char *result = "not found";
1568 int rc = 0;
1569 if(!cfg->http_readonly) {
1570 if(cfg->http_script[0]) {
1571 tpl_addVar(vars, 0, "SCRIPTNAME",cfg->http_script);
1572 rc = system(cfg->http_script);
1573 if(rc == -1) {
1574 result = "done";
1575 } else {
1576 result = "failed";
1577 }
1578 } else {
1579 tpl_addVar(vars, 0, "SCRIPTNAME", "no script defined");
1580 }
1581 tpl_addVar(vars, 0, "SCRIPTRESULT", result);
1582 tpl_printf(vars, 0, "CODE", "%d", rc);
1583 } else {
1584 tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry, Webif is in readonly mode. No script execution possible!</b><BR>");
1585 }
1586 fputs(tpl_getTpl(vars, "SCRIPT"), f);
1587
1588}
1589
1590void send_oscam_scanusb(struct templatevars *vars, FILE *f) {
1591 FILE *fp;
1592 int err=0;
1593 char path[1035];
1594
1595 fp = popen("lsusb", "r");
1596 if (fp == NULL) {
1597 tpl_addVar(vars, 0, "USBENTRY", "Failed to run lusb");
1598 tpl_printf(vars, 0, "USBENTRY", "%s", path);
1599 tpl_addVar(vars, 1, "USBBIT", tpl_getTpl(vars, "SCANUSBBIT"));
1600 err = 1;
1601 }
1602
1603 if(!err) {
1604 while (fgets(path, sizeof(path)-1, fp) != NULL) {
1605 tpl_printf(vars, 0, "USBENTRY", "%s", path);
1606 tpl_addVar(vars, 1, "USBBIT", tpl_getTpl(vars, "SCANUSBBIT"));
1607 }
1608 }
1609 pclose(fp);
1610 fputs(tpl_getTpl(vars, "SCANUSB"), f);
1611}
1612
1613void send_oscam_files(struct templatevars *vars, FILE *f, struct uriparams *params) {
1614
1615 char *stoplog = getParam(params, "stoplog");
1616 if(strlen(stoplog) > 0)
1617 cfg->disablelog = atoi(stoplog);
1618
1619 char *stopusrlog = getParam(params, "stopusrlog");
1620 if(strlen(stopusrlog) > 0)
1621 cfg->disableuserfile = atoi(stopusrlog);
1622
1623 char *debuglvl = getParam(params, "debug");
1624 if(strlen(debuglvl) > 0) {
1625 int lvl = atoi(debuglvl);
1626 if (cfg->debuglvl != lvl) {
1627 cfg->debuglvl = lvl;
1628 kill(client[0].pid, SIGUSR1);
1629 }
1630 }
1631 char targetfile[256];
1632
1633 if (strcmp(getParam(params, "part"), "conf") == 0)
1634 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.conf");
1635 else if (strcmp(getParam(params, "part"), "user") == 0)
1636 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.user");
1637 else if (strcmp(getParam(params, "part"), "server") == 0)
1638 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.server");
1639 else if (strcmp(getParam(params, "part"), "services") == 0)
1640 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.services");
1641 else if (strcmp(getParam(params, "part"), "srvid") == 0)
1642 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.srvid");
1643 else if (strcmp(getParam(params, "part"), "logfile") == 0) {
1644 snprintf(targetfile, 255,"%s", cfg->logfile);
1645
1646 if (strcmp(getParam(params, "clear"), "logfile") == 0) {
1647 if(strlen(targetfile) > 0) {
1648 FILE *file = fopen(targetfile,"w");
1649 fclose(file);
1650 }
1651 }
1652
1653 tpl_printf(vars, 0, "SDEBUG", "%s&nbsp;%d to&nbsp;\n", "Switch Debug from", cfg->debuglvl);
1654 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=0\" title=\"no debugging (default)\">0</A>&nbsp;\n");
1655 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=1\" title=\"detailed error messages\">1</A>&nbsp;\n");
1656 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=2\" title=\"ATR parsing info, ECM dumps, CW dumps\">2</A>&nbsp;\n");
1657 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=4\" title=\"traffic from/to the reader\">4</A>&nbsp;\n");
1658 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=8\" title=\"traffic from/to the clients\">8</A>&nbsp;\n");
1659 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=16\" title=\"traffic to the reader-device on IFD layer\">16</A>&nbsp;\n");
1660 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=32\" title=\"traffic to the reader-device on I/O layer\">32</A>&nbsp;\n");
1661 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=64\" title=\"EMM logging\">64</A>&nbsp;\n");
1662 tpl_addVar(vars, 1, "SDEBUG", "<A HREF=\"files.html?part=logfile&debug=255\" title=\"debug all\">255</A>&nbsp;&nbsp;|\n");
1663
1664 if(!cfg->disablelog)
1665 tpl_printf(vars, 0, "SLOG", "<A HREF=\"files.html?part=logfile&stoplog=%d\">%s</A>&nbsp;&nbsp;|&nbsp;&nbsp;\n", 1, "Stop Log");
1666 else
1667 tpl_printf(vars, 0, "SLOG", "<A HREF=\"files.html?part=logfile&stoplog=%d\">%s</A>&nbsp;&nbsp;|&nbsp;&nbsp;\n", 0, "Start Log");
1668
1669 tpl_printf(vars, 0, "SCLEAR", "<A HREF=\"files.html?part=logfile&clear=logfile\">%s</A><BR><BR>\n", "Clear Log");
1670 }
1671 else if (strcmp(getParam(params, "part"), "userfile") == 0) {
1672 snprintf(targetfile, 255,"%s", cfg->usrfile);
1673 if (strcmp(getParam(params, "clear"), "usrfile") == 0) {
1674 if(strlen(targetfile) > 0) {
1675 FILE *file = fopen(targetfile,"w");
1676 fclose(file);
1677 }
1678 }
1679
1680 if(!cfg->disableuserfile)
1681 tpl_printf(vars, 0, "SLOG", "<A HREF=\"files.html?part=userfile&stopusrlog=%d\">%s</A>&nbsp;&nbsp;|&nbsp;&nbsp;\n", 1, "Stop Log");
1682 else
1683 tpl_printf(vars, 0, "SLOG", "<A HREF=\"files.html?part=userfile&stopusrlog=%d\">%s</A>&nbsp;&nbsp;|&nbsp;&nbsp;\n", 0, "Start Log");
1684
1685 tpl_printf(vars, 0, "SCLEAR", "<A HREF=\"files.html?part=userfile&clear=usrfile\">%s</A><BR><BR>\n", "Clear Log");
1686
1687 }
1688#ifdef CS_ANTICASC
1689 else if (strcmp(getParam(params, "part"), "anticasc") == 0)
1690 snprintf(targetfile, 255,"%s", cfg->ac_logfile);
1691#endif
1692
1693 if(strlen(targetfile) > 0 && file_exists(targetfile) == 1) {
1694 FILE *fp;
1695 char buffer[256];
1696
1697 if((fp = fopen(targetfile,"r")) == NULL) return;
1698 while (fgets(buffer, sizeof(buffer), fp) != NULL)
1699 tpl_printf(vars, 1, "FILECONTENT", "%s", buffer);
1700 fclose (fp);
1701 }
1702
1703 fputs(tpl_getTpl(vars, "FILE"), f);
1704}
1705
1706int process_request(FILE *f, struct in_addr in) {
1707
1708 client[cs_idx].last = time((time_t)0); //reset last busy time
1709
1710 int ok=0;
1711 struct s_ip *p_ip;
1712 in_addr_t addr = cs_inet_order(in.s_addr);
1713
1714 for (p_ip = cfg->http_allowed; (p_ip) && (!ok); p_ip = p_ip->next)
1715 ok =((addr >= p_ip->ip[0]) && (addr <= p_ip->ip[1]));
1716
1717 if (!ok && cfg->http_dyndns[0]) {
1718 if(cfg->http_dynip == addr) {
1719 ok = 1;
1720 } else {
1721 struct hostent *rht;
1722 struct sockaddr_in udp_sa;
1723 rht = gethostbyname((const char *) cfg->http_dyndns);
1724 if (rht) {
1725 memcpy(&udp_sa.sin_addr, rht->h_addr, sizeof(udp_sa.sin_addr));
1726 cfg->http_dynip = cs_inet_order(udp_sa.sin_addr.s_addr);
1727 if (cfg->http_dynip == addr)
1728 ok = 1;
1729 }
1730 }
1731 }
1732
1733 if (!ok) {
1734 send_error(f, 403, "Forbidden", NULL, "Access denied.");
1735 cs_log("unauthorized access from %s", inet_ntoa(*(struct in_addr *)&in));
1736 return 0;
1737 }
1738
1739 char buf[4096];
1740 char tmp[4096];
1741
1742 int authok = 0;
1743 char expectednonce[64];
1744
1745 char *method;
1746 char *path;
1747 char *protocol;
1748 char *pch;
1749 char *pch2;
1750 /* List of possible pages */
1751 char *pages[]= {
1752 "/config.html",
1753 "/readers.html",
1754 "/entitlements.html",
1755 "/status.html",
1756 "/userconfig.html",
1757 "/readerconfig.html",
1758 "/services.html",
1759 "/user_edit.html",
1760 "/site.css",
1761 "/services_edit.html",
1762 "/savetemplates.html",
1763 "/shutdown.html",
1764 "/script.html",
1765 "/scanusb.html",
1766 "/files.html"};
1767
1768 int pagescnt = sizeof(pages)/sizeof(char *); // Calculate the amount of items in array
1769
1770 int pgidx = -1;
1771 int i;
1772 int parsemode = 1;
1773 struct uriparams params;
1774 params.paramcount = 0;
1775
1776 /* First line always includes the GET/POST request */
1777 if (!fgets(buf, sizeof(buf), f)) return -1;
1778 method = strtok(buf, " ");
1779 path = strtok(NULL, " ");
1780 protocol = strtok(NULL, "\r");
1781 if(method == NULL || path == NULL || protocol == NULL) return -1;
1782
1783 pch=path;
1784 /* advance pointer to beginning of query string */
1785 while(pch[0] != '?' && pch[0] != '\0') ++pch;
1786 if(pch[0] == '?') {
1787 pch[0] = '\0';
1788 ++pch;
1789 }
1790
1791 /* Map page to our static page definitions */
1792 for (i=0; i<pagescnt; i++) {
1793 if (!strcmp(path, pages[i])) pgidx = i;
1794 }
1795
1796 /* Parse url parameters; parsemode = 1 means parsing next param, parsemode = -1 parsing next
1797 value; pch2 points to the beginning of the currently parsed string, pch is the current position */
1798 pch2=pch;
1799 while(pch[0] != '\0') {
1800 if((parsemode == 1 && pch[0] == '=') || (parsemode == -1 && pch[0] == '&')) {
1801 pch[0] = '\0';
1802 urldecode(pch2);
1803 if(parsemode == 1) {
1804 if(params.paramcount >= MAXGETPARAMS) break;
1805 ++params.paramcount;
1806 params.params[params.paramcount-1] = pch2;
1807 } else {
1808 params.values[params.paramcount-1] = pch2;
1809 }
1810 parsemode = -parsemode;
1811 pch2 = pch + 1;
1812 }
1813 ++pch;
1814 }
1815 /* last value wasn't processed in the loop yet... */
1816 if(parsemode == -1 && params.paramcount <= MAXGETPARAMS) {
1817 urldecode(pch2);
1818 params.values[params.paramcount-1] = pch2;
1819 }
1820
1821 if(strlen(cfg->http_user) == 0 || strlen(cfg->http_pwd) == 0) authok = 1;
1822 else calculate_nonce(expectednonce, sizeof(expectednonce)/sizeof(char));
1823
1824 /* Read remaining request (we're only interested in auth header) */
1825 while (fgets(tmp, sizeof(tmp), f)) {
1826 if (tmp[0] == '\r' && tmp[1] == '\n') break;
1827 else if(authok == 0 && strlen(tmp) > 50 && strncmp(tmp, "Authorization:", 14) == 0 && strstr(tmp, "Digest") != NULL) {
1828 authok = check_auth(tmp, method, path, expectednonce);
1829 }
1830 }
1831
1832 //cs_debug("%s %d\n", path, pgidx);
1833 //for(i=0; i < params.paramcount; ++i) cs_debug("%s : %s\n", params.params[i], params.values[i]);
1834
1835 fseek(f, 0, SEEK_CUR); // Force change of stream direction
1836
1837 if(authok != 1) {
1838 strcpy(tmp, "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\"");
1839 strcat(tmp, AUTHREALM);
1840 strcat(tmp, "\", qop=\"auth\", opaque=\"\", nonce=\"");
1841 strcat(tmp, expectednonce);
1842 strcat(tmp, "\"");
1843 if(authok == 2) strcat(tmp, ", stale=true");
1844 send_headers(f, 401, "Unauthorized", tmp, "text/html");
1845 return 0;
1846 }
1847
1848 /*build page*/
1849 send_headers(f, 200, "OK", NULL, "text/html");
1850 if(pgidx == 8) send_css(f);
1851 else {
1852 time_t t;
1853 struct templatevars *vars = tpl_create();
1854 struct tm *lt;
1855 struct tm *st;
1856 time(&t);
1857
1858 lt = localtime(&t);
1859
1860 tpl_addVar(vars, 0, "CS_VERSION", CS_VERSION);
1861 tpl_addVar(vars, 0, "CS_SVN_VERSION", CS_SVN_VERSION);
1862 tpl_addVar(vars, 0, "PATCHLEVEL", " 10");
1863 tpl_addVar(vars, 0, "ICO", ICMAI);
1864 if(cfg->http_refresh > 0 && (pgidx == 3 || pgidx == -1)) {
1865 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
1866 tpl_addVar(vars, 0, "REFRESHURL", "status.html");
1867 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1868 }
1869 tpl_printf(vars, 0, "CURDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
1870 tpl_printf(vars, 0, "CURTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
1871 st = localtime(&client[0].login);
1872 tpl_printf(vars, 0, "STARTDATE", "%02d.%02d.%02d", st->tm_mday, st->tm_mon+1, st->tm_year%100);
1873 tpl_printf(vars, 0, "STARTTIME", "%02d:%02d:%02d", st->tm_hour, st->tm_min, st->tm_sec);
1874
1875 time_t now = time((time_t)0);
1876 int lsec = now - client[0].login;
1877 int secs = 0, fullmins = 0, mins = 0, fullhours = 0, hours = 0, days = 0;
1878 if(lsec > 0) {
1879 secs = lsec % 60;
1880 if (lsec > 60) {
1881 fullmins = lsec / 60;
1882 mins = fullmins % 60;
1883 if(fullmins > 60) {
1884 fullhours = fullmins / 60;
1885 hours = fullhours % 24;
1886 days = fullhours / 24;
1887 }
1888 }
1889 }
1890 if(days == 0)
1891 tpl_printf(vars, 0, "UPTIME", "%02d:%02d:%02d", hours, mins, secs);
1892 else
1893 tpl_printf(vars, 0, "UPTIME", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
1894
1895 tpl_printf(vars, 0, "CURIP", "%s", inet_ntoa(*(struct in_addr *)&in));
1896 if(cfg->http_readonly)
1897 tpl_addVar(vars, 1, "BTNDISABLED", "DISABLED");
1898
1899 switch(pgidx) {
1900 case 0: send_oscam_config(vars, f, &params, in); break;
1901 case 1: send_oscam_reader(vars, f, &params, in); break;
1902 case 2: send_oscam_entitlement(vars, f, &params); break;
1903 case 3: send_oscam_status(vars, f, &params, in); break;
1904 case 4: send_oscam_user_config(vars, f, &params, in); break;
1905 case 5: send_oscam_reader_config(vars, f, &params, in); break;
1906 case 6: send_oscam_services(vars, f, &params, in); break;
1907 case 7: send_oscam_user_config_edit(vars, f, &params, in); break;
1908 //case 8: css file
1909 case 9: send_oscam_services_edit(vars, f, &params, in); break;
1910 case 10: send_oscam_savetpls(vars, f); break;
1911 case 11: send_oscam_shutdown(vars, f, &params); break;
1912 case 12: send_oscam_script(vars, f); break;
1913 case 13: send_oscam_scanusb(vars, f); break;
1914 case 14: send_oscam_files(vars, f, &params); break;
1915 default: send_oscam_status(vars, f, &params, in); break;
1916 }
1917 tpl_clear(vars);
1918 }
1919 return 0;
1920}
1921
1922void http_srv() {
1923 int i,sock, reuse = 1;
1924 struct sockaddr_in sin;
1925 struct sockaddr_in remote;
1926 socklen_t len = sizeof(remote);
1927 char *tmp;
1928
1929 /* Prepare lookup array for conversion between ascii and hex */
1930 tmp = malloc(3 * sizeof(char));
1931 for(i = 0; i < 256; i++) {
1932 snprintf(tmp, 3,"%02x", i);
1933 memcpy(hex2ascii[i], tmp, 2);
1934 }
1935 free(tmp);
1936 /* Create random string for nonce value generation */
1937 srand(time(NULL));
1938 create_rand_str(noncekey,32);
1939
1940 /* Startup server */
1941 if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
1942 cs_log("HTTP Server: Creating socket failed! (errno=%d)", errno);
1943 return;
1944 }
1945 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
1946 cs_log("HTTP Server: Setting SO_REUSEADDR via setsockopt failed! (errno=%d)", errno);
1947 }
1948
1949 memset(&sin, 0, sizeof sin);
1950 sin.sin_family = AF_INET;
1951 sin.sin_addr.s_addr = INADDR_ANY;
1952 sin.sin_port = htons(cfg->http_port);
1953 if((bind(sock, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
1954 cs_log("HTTP Server couldn't bind on port %d (errno=%d). Not starting HTTP!", cfg->http_port, errno);
1955 close(sock);
1956 return;
1957 }
1958 if (listen(sock, SOMAXCONN) < 0) {
1959 cs_log("HTTP Server: Call to listen() failed! (errno=%d)", errno);
1960 close(sock);
1961 return;
1962 }
1963 cs_log("HTTP Server listening on port %d", cfg->http_port);
1964 struct pollfd pfd[1];
1965 int rc;
1966 pfd[0].fd = sock;
1967 pfd[0].events = (POLLIN | POLLPRI);
1968
1969 while (running) {
1970 int s;
1971 FILE *f;
1972
1973 rc = poll(pfd, 1, 1000);
1974 if (master_pid != getppid())
1975 cs_exit(0);
1976
1977 if (rc > 0) {
1978 if((s = accept(sock, (struct sockaddr *) &remote, &len)) < 0) {
1979 cs_log("HTTP Server: Error calling accept() (errno=%d).", errno);
1980 break;
1981 }
1982
1983 f = fdopen(s, "r+");
1984 process_request(f, remote.sin_addr);
1985 fflush(f);
1986 fclose(f);
1987 shutdown(s, SHUT_WR);
1988 close(s);
1989 }
1990 }
1991
1992 close(sock);
1993 kill(client[0].pid, SIGQUIT);
1994}
1995#endif
Note: See TracBrowser for help on using the repository browser.