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

Last change on this file since 1258 was 1258, checked in by alno, 11 years ago

WebIf:

  • Shutdown Button implemented. This Button kills the Oscam. If you use a restart script you can use it as restart. However, Shutdown only ends oscam.
File size: 50.1 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
12static int running = 1;
13
14void refresh_oscam(enum refreshtypes refreshtype, struct in_addr in){
15int i;
16 switch (refreshtype){
17 case REFR_ACCOUNTS:
18 cs_log("Refresh Accounts requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
19 kill(client[0].pid, SIGHUP);
20#ifdef CS_ANTICASC
21 for (i=0; i<CS_MAXPID; i++)
22 if (client[i].typ=='a') {
23 kill(client[i].pid, SIGHUP);
24 break;
25 }
26#endif
27 break;
28
29 case REFR_READERS:
30 cs_log("Refresh Reader");
31 //todo how I can refresh the readers
32 break;
33
34 case REFR_SERVER:
35 cs_log("Refresh Server requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
36 //kill(client[0].pid, SIGHUP);
37 //todo how I can refresh the server after global settings
38 break;
39
40 case REFR_SERVICES:
41 cs_log("Refresh Services requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
42 //init_sidtab();
43 kill(client[0].pid, SIGHUP);
44 break;
45
46#ifdef CS_ANTICASC
47 case REFR_ANTICASC:
48 cs_log("Refresh Anticascading requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
49 for (i=0; i<CS_MAXPID; i++)
50 if (client[i].typ=='a') {
51 kill(client[i].pid, SIGHUP);
52 break;
53 }
54 break;
55#endif
56 }
57}
58
59void send_oscam_config_global(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
60 int i;
61
62 if (strcmp(getParam(params, "action"), "execute") == 0){
63 for(i = 0; i < (*params).paramcount; ++i){
64 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
65 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
66 //we use the same function as used for parsing the config tokens
67 chk_t_global((*params).params[i], (*params).values[i]);
68 }
69 }
70 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Global *DONE*</B><BR><BR>");
71 /*if(write_config()==0) */refresh_oscam(REFR_SERVER, in);
72 }
73 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->srvip));
74 tpl_addVar(vars, 0, "LOGFILE", logfile);
75 tpl_addVar(vars, 0, "PIDFILE", cfg->pidfile);
76 tpl_addVar(vars, 0, "USERFILE", cfg->usrfile);
77 tpl_addVar(vars, 0, "CWLOGDIR", cfg->cwlogdir);
78 tpl_printf(vars, 0, "CLIENTTIMEOUT", "%ld", cfg->ctimeout/1000);
79 tpl_printf(vars, 0, "FALLBACKTIMEOUT", "%ld", cfg->ftimeout/1000);
80 tpl_printf(vars, 0, "CLIENTMAXIDLE", "%d", cfg->cmaxidle);
81 tpl_printf(vars, 0, "CACHEDELAY", "%ld", cfg->delay/1000);
82 tpl_printf(vars, 0, "BINDWAIT", "%d", cfg->bindwait);
83 tpl_printf(vars, 0, "NETPRIO", "%ld", cfg->netprio);
84 tpl_printf(vars, 0, "RESOLVEDELAY", "%d", cfg->resolvedelay);
85 tpl_printf(vars, 0, "SLEEP", "%d", cfg->tosleep);
86 tpl_printf(vars, 0, "UNLOCKPARENTAL", "%d", cfg->ulparent);
87 tpl_printf(vars, 0, "NICE", "%d", cfg->nice);
88 tpl_printf(vars, 0, "SERIALTIMEOUT", "%d", cfg->srtimeout);
89 tpl_printf(vars, 0, "MAXLOGSIZE", "%d", cfg->max_log_size);
90 tpl_printf(vars, 0, "SHOWECMDW", "%d", cfg->show_ecm_dw);
91 tpl_printf(vars, 0, "WAITFORCARDS", "%d", cfg->waitforcards);
92 tpl_printf(vars, 0, "PREFERLOCALCARDS", "%d", cfg->preferlocalcards);
93
94 fputs(tpl_getTpl(vars, "CONFIGGLOBAL"), f);
95}
96
97void send_oscam_config_camd33(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
98 int i;
99
100 if (strcmp(getParam(params, "action"), "execute") == 0){
101 for(i = 0; i < (*params).paramcount; ++i){
102 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
103 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
104 if (strcmp((*params).params[i], "nocrypt") == 0){
105 clear_sip(&cfg->c33_plain);
106 }
107 //we use the same function as used for parsing the config tokens
108 chk_t_camd33((*params).params[i], (*params).values[i]);
109 }
110 }
111 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd33 *DONE*</B><BR><BR>");
112 refresh_oscam(REFR_SERVER, in);
113 }
114 tpl_printf(vars, 0, "PORT", "%d", cfg->c33_port);
115 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
116 tpl_printf(vars, 0, "PASSIVE", "%d", cfg->c33_passive);
117
118 for (i = 0; i < (int) sizeof(cfg->c33_key); ++i) tpl_printf(vars, 1, "KEY", "%02X",cfg->c33_key[i]);
119 struct s_ip *cip;
120 char *dot="";
121 for (cip = cfg->c33_plain; cip; cip = cip->next){
122 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
123 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", cs_inet_ntoa(cip->ip[1]));
124 dot=",";
125 }
126
127 fputs(tpl_getTpl(vars, "CONFIGCAMD33"), f);
128}
129
130void send_oscam_config_camd35(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
131 int i;
132 if (strcmp(getParam(params, "action"),"execute") == 0){
133 for(i = 0; i < (*params).paramcount; ++i){
134 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
135 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
136 //we use the same function as used for parsing the config tokens
137 chk_t_camd35((*params).params[i], (*params).values[i]);
138 }
139 }
140 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 *DONE*</B><BR><BR>");
141 refresh_oscam(REFR_SERVER, in);
142 }
143 tpl_printf(vars, 0, "PORT", "%d", cfg->c35_port);
144 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
145
146 fputs(tpl_getTpl(vars, "CONFIGCAMD35"), f);
147}
148
149void send_oscam_config_camd35tcp(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
150 int i;
151 if (strcmp(getParam(params, "action"),"execute") == 0){
152 clear_ptab(&cfg->c35_tcp_ptab); /*clear Porttab*/
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_tcp((*params).params[i], (*params).values[i]);
158 }
159 }
160 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 TCP *DONE*</B><BR><BR>");
161 refresh_oscam(REFR_SERVER, in);
162 }
163
164 int j;
165 char *dot1, *dot2;
166 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)){
167 dot1 = "";
168 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i){
169 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);
170 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0){
171 tpl_printf(vars, 1, "PORT", ":");
172 dot2 = "";
173 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j){
174 tpl_printf(vars, 1, "PORT", "%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
175 dot2 = ",";
176 }
177 }
178 dot1=";";
179 }
180 }
181 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
182
183 fputs(tpl_getTpl(vars, "CONFIGCAMD35TCP"), f);
184}
185
186void send_oscam_config_newcamd(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
187 int i;
188 if (strcmp(getParam(params, "action"),"execute") == 0){
189 clear_ptab(&cfg->ncd_ptab); /*clear Porttab*/
190 for(i = 0; i < (*params).paramcount; ++i){
191 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
192 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
193 //we use the same function as used for parsing the config tokens
194 chk_t_newcamd((*params).params[i], (*params).values[i]);
195 }
196 }
197 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Newcamd *DONE*</B><BR><BR>");
198 refresh_oscam(REFR_SERVER, in);
199 }
200 int j;
201 char *dot1, *dot2;
202 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)){
203 dot1 = "";
204 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
205 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);
206 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
207 tpl_printf(vars, 1, "PORT", ":");
208 dot2 = "";
209 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
210 tpl_printf(vars, 1, "PORT", "%s%lX", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
211 dot2 = ",";
212 }
213 }
214 dot1=";";
215 }
216 }
217
218 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
219 for (i=0;i<14;i++) tpl_printf(vars, 1, "KEY", "%02X", cfg->ncd_key[i]);
220
221 fputs(tpl_getTpl(vars, "CONFIGNEWCAMD"), f);
222}
223
224void send_oscam_config_radegast(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
225 int i;
226 if (strcmp(getParam(params, "action"),"execute") == 0){
227 for(i = 0; i < (*params).paramcount; ++i){
228 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
229 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
230 if (strcmp((*params).params[i], "allowed") == 0){
231 clear_sip(&cfg->rad_allowed);
232 }
233 //we use the same function as used for parsing the config tokens
234 chk_t_radegast((*params).params[i], (*params).values[i]);
235 }
236 }
237 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Radegast *DONE*</B><BR><BR>");
238 refresh_oscam(REFR_SERVER, in);
239 }
240 tpl_printf(vars, 0, "PORT", "%d", cfg->rad_port);
241 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
242 tpl_addVar(vars, 0, "USER", cfg->rad_usr);
243
244 struct s_ip *cip;
245 char *dot="";
246 for (cip=cfg->rad_allowed; cip; cip=cip->next){
247 tpl_printf(vars, 1, "ALLOWED", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
248 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "ALLOWED", "-%s", cs_inet_ntoa(cip->ip[1]));
249 dot=",";
250 }
251
252 fputs(tpl_getTpl(vars, "CONFIGRADEGAST"), f);
253}
254
255void send_oscam_config_cccam(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
256 int i;
257 if (strcmp(getParam(params, "action"),"execute") == 0){
258 for(i = 0; i < (*params).paramcount; ++i){
259 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
260 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
261 //we use the same function as used for parsing the config tokens
262 chk_t_cccam((*params).params[i], (*params).values[i]);
263 }
264 }
265 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR>Configuration Cccam Do not yet implemented<BR><BR>");
266 refresh_oscam(REFR_SERVER, in);
267 }
268 fputs(tpl_getTpl(vars, "CONFIGCCCAM"), f);
269}
270
271#ifdef CS_WITH_GBOX
272void send_oscam_config_gbox(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
273 int i;
274 if (strcmp(getParam(params, "action"),"execute") == 0){
275 for(i = 0; i < (*params).paramcount; ++i){
276 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
277 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
278 //we use the same function as used for parsing the config tokens
279 chk_t_gbox((*params).params[i], (*params).values[i]);
280 }
281 }
282 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Gbox *DONE*</B><BR><BR>");
283 refresh_oscam(REFR_SERVER, in);
284 }
285 tpl_printf(vars, 0, "MAXDIST", "%d", cfg->maxdist);
286 for (i=0;i<4;i++) tpl_printf(vars, 1, "PASSWORD", "%02X", cfg->gbox_pwd[i]);
287 tpl_addVar(vars, 0, "IGNORELIST", (char *)cfg->ignorefile);
288 tpl_addVar(vars, 0, "ONLINEINFOS", (char *)cfg->gbxShareOnl);
289 tpl_addVar(vars, 0, "CARDINFOS", (char *)cfg->cardfile);
290 char *dot = "";
291 for (i = 0; i < cfg->num_locals; i++){
292 tpl_printf(vars, 1, "LOCALS", "%s%06lX", dot, cfg->locals[i]);
293 dot=";";
294 }
295 fputs(tpl_getTpl(vars, "CONFIGGBOX"), f);
296}
297#endif
298
299void send_oscam_config_monitor(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
300 int i;
301 if (strcmp(getParam(params, "action"),"execute") == 0){
302 for(i = 0; i < (*params).paramcount; ++i){
303 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
304 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
305 if (strcmp((*params).params[i], "nocrypt") == 0){
306 clear_sip(&cfg->mon_allowed);
307 }
308 //we use the same function as used for parsing the config tokens
309 chk_t_monitor((*params).params[i], (*params).values[i]);
310 }
311 }
312 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Monitor *DONE*</B><BR><BR>");
313 refresh_oscam(REFR_SERVER, in);
314 }
315 tpl_printf(vars, 0, "MONPORT", "%d", cfg->mon_port);
316 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
317 tpl_printf(vars, 0, "AULOW", "%d", cfg->mon_aulow);
318 tpl_printf(vars, 0, "HIDECLIENTTO", "%d", cfg->mon_hideclient_to);
319 tpl_printf(vars, 0, "HTTPPORT", "%d", cfg->http_port);
320 tpl_addVar(vars, 0, "HTTPUSER", cfg->http_user);
321 tpl_addVar(vars, 0, "HTTPPASSWORD", cfg->http_pwd);
322 tpl_addVar(vars, 0, "HTTPCSS", cfg->http_css);
323 tpl_printf(vars, 0, "HTTPREFRESH", "%d", cfg->http_refresh);
324 tpl_addVar(vars, 0, "HTTPTPL", cfg->http_tpl);
325 if (cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
326
327 struct s_ip *cip;
328 char *dot="";
329 for (cip = cfg->mon_allowed; cip; cip = cip->next){
330 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
331 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", cs_inet_ntoa(cip->ip[1]));
332 dot=",";
333 }
334
335 //Monlevel selector
336 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", cfg->mon_level);
337 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
338
339 fputs(tpl_getTpl(vars, "CONFIGMONITOR"), f);
340}
341
342void send_oscam_config_serial(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
343 int i;
344 if (strcmp(getParam(params, "action"),"execute") == 0){
345 for(i = 0; i < (*params).paramcount; ++i){
346 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
347 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
348 //we use the same function as used for parsing the config tokens
349 chk_t_serial((*params).params[i], (*params).values[i]);
350 }
351 }
352 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Serial *DONE*</B><BR><BR>");
353 refresh_oscam(REFR_SERVER, in);
354 }
355
356 tpl_addVar(vars, 0, "SERIALDEVICE", cfg->ser_device);
357
358 fputs(tpl_getTpl(vars, "CONFIGSERIAL"), f);
359}
360
361#ifdef HAVE_DVBAPI
362void send_oscam_config_dvbapi(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
363 int i;
364 if (strcmp(getParam(params, "action"),"execute") == 0){
365 for(i = 0; i < (*params).paramcount; ++i){
366 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
367 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
368 //we use the same function as used for parsing the config tokens
369 chk_t_dvbapi((*params).params[i], (*params).values[i]);
370 }
371 }
372 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration DVB Api *DONE*</B><BR><BR>");
373 refresh_oscam(REFR_SERVER, in);
374 }
375
376 if (cfg->dvbapi_enabled > 0) tpl_addVar(vars, 0, "ENABLEDCHECKED", "checked");
377 if (cfg->dvbapi_au > 0) tpl_addVar(vars, 0, "AUCHECKED", "checked");
378 tpl_addVar(vars, 0, "BOXTYPE", cfg->dvbapi_boxtype);
379 tpl_addVar(vars, 0, "USER", cfg->dvbapi_usr);
380
381 fputs(tpl_getTpl(vars, "CONFIGDVBAPI"), f);
382}
383#endif
384
385#ifdef CS_ANTICASC
386void send_oscam_config_anticasc(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
387 int i;
388 if (strcmp(getParam(params, "action"),"execute") == 0){
389 for(i = 0; i < (*params).paramcount; ++i){
390 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))){
391 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
392 //we use the same function as used for parsing the config tokens
393 chk_t_ac((*params).params[i], (*params).values[i]);
394 }
395 }
396 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Anticascading *DONE*</B><BR><BR>");
397 refresh_oscam(REFR_ANTICASC, in);
398 }
399 if (cfg->ac_enabled > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
400 tpl_printf(vars, 0, "NUMUSERS", "%d", cfg->ac_users);
401 tpl_printf(vars, 0, "SAMPLETIME", "%d", cfg->ac_stime);
402 tpl_printf(vars, 0, "SAMPLES", "%d", cfg->ac_samples);
403 tpl_printf(vars, 0, "PENALTY", "%d", cfg->ac_penalty);
404 tpl_addVar(vars, 0, "ACLOGFILE", cfg->ac_logfile);
405 tpl_printf(vars, 0, "FAKEDELAY", "%d", cfg->ac_fakedelay);
406 tpl_printf(vars, 0, "DENYSAMPLES", "%d", cfg->ac_denysamples);
407 fputs(tpl_getTpl(vars, "CONFIGANTICASC"), f);
408}
409#endif
410
411void send_oscam_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
412 char *part = getParam(params, "part");
413 if (!strcmp(part,"camd33")) send_oscam_config_camd33(vars, f, params, in);
414 else if (!strcmp(part,"camd35")) send_oscam_config_camd35(vars, f, params, in);
415 else if (!strcmp(part,"camd35tcp")) send_oscam_config_camd35tcp(vars, f, params, in);
416 else if (!strcmp(part,"newcamd")) send_oscam_config_newcamd(vars, f, params, in);
417 else if (!strcmp(part,"radegast")) send_oscam_config_radegast(vars, f, params, in);
418 else if (!strcmp(part,"cccam")) send_oscam_config_cccam(vars, f, params, in);
419#ifdef CS_WITH_GBOX
420 else if (!strcmp(part,"gbox")) send_oscam_config_gbox(vars, f, params, in);
421#endif
422#ifdef HAVE_DVBAPI
423 else if (!strcmp(part,"dvbapi")) send_oscam_config_dvbapi(vars, f, params, in);
424#endif
425#ifdef CS_ANTICASC
426 else if (!strcmp(part,"anticasc")) send_oscam_config_anticasc(vars, f, params, in);
427#endif
428 else if (!strcmp(part,"monitor")) send_oscam_config_monitor(vars, f, params, in);
429 else if (!strcmp(part,"serial")) send_oscam_config_serial(vars, f, params, in);
430 else send_oscam_config_global(vars, f, params, in);
431}
432
433void send_oscam_reader(struct templatevars *vars, FILE *f) {
434 int ridx;
435 char *ctyp;
436
437 for(ridx=0;ridx<CS_MAXREADER;ridx++){
438 if(!reader[ridx].device[0]) break;
439 switch(reader[ridx].typ){
440 case R_MOUSE : ctyp="mouse"; break;
441 case R_INTERNAL: ctyp="intern"; break;
442 case R_SMART : ctyp="smartreader"; break;
443 case R_CAMD35 : ctyp="camd 3.5x";break;
444 case R_CAMD33 : ctyp="camd 3.3x";break;
445 case R_NEWCAMD : ctyp="newcamd"; break;
446 case R_RADEGAST: ctyp="radegast"; break;
447 case R_SERIAL : ctyp="serial"; break;
448#ifdef CS_WITH_GBOX
449 case R_GBOX : ctyp="gbox"; break;
450#endif
451#ifdef HAVE_PCSC
452 case R_PCSC : ctyp="pcsc"; break;
453#endif
454 case R_CCCAM : ctyp="cccam"; break;
455 case R_CS378X : ctyp="cs378x"; break;
456 default : ctyp="unknown"; break;
457 }
458 tpl_addVar(vars, 0, "CTYP", ctyp);
459 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
460 tpl_addVar(vars, 0, "READERNAMEENC", tpl_addTmp(vars, urlencode(reader[ridx].label)));
461 tpl_addVar(vars, 1, "READERLIST", tpl_getTpl(vars, "READERSBIT"));
462 }
463 fputs(tpl_getTpl(vars, "READERS"), f);
464}
465
466void send_oscam_reader_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
467 int ridx;
468 char *reader_ = getParam(params, "reader");
469 for(ridx = 0; ridx < CS_MAXREADER && strcmp(reader_, reader[ridx].label) != 0; ++ridx);
470 if(ridx == CS_MAXREADER){
471 tpl_addVar(vars, 0, "MESSAGE", "<BR><BR>Reader not found<BR><BR>");
472 } else if(strcmp(getParam(params, "action"), "execute") == 0){
473 tpl_addVar(vars, 0, "MESSAGE", "<BR><BR>Saving not yet implemented<BR><BR>");
474 refresh_oscam(REFR_READERS, in);
475 }
476 int i;
477
478 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
479 tpl_printf(vars, 0, "DEVICE", "%s", reader[ridx].device);
480 tpl_addVar(vars, 0, "NCD_KEY", (char *)reader[ridx].ncd_key);
481 tpl_addVar(vars, 0, "PINCODE", reader[ridx].pincode);
482 tpl_addVar(vars, 0, "EMMFILE", (char *)reader[ridx].emmfile);
483 tpl_printf(vars, 0, "INACTIVITYTIMEOUT", "%d", reader[ridx].tcp_ito);
484 tpl_printf(vars, 0, "RECEIVETIMEOUT", "%d", reader[ridx].tcp_rto);
485 tpl_printf(vars, 0, "DISABLESERVERFILTER", "%d", reader[ridx].ncd_disable_server_filt);
486 tpl_printf(vars, 0, "FALLBACK", "%d", reader[ridx].fallback);
487 tpl_printf(vars, 0, "LOGPORT", "%d", reader[ridx].log_port);
488 tpl_printf(vars, 0, "BOXID", "%ld", reader[ridx].boxid);
489 tpl_addVar(vars, 0, "USER", reader[ridx].r_usr);
490 tpl_addVar(vars, 0, "PASS", reader[ridx].r_pwd);
491 tpl_addVar(vars, 0, "RSAKEY", (char *)reader[ridx].rsa_mod);
492 tpl_addVar(vars, 0, "BOXKEY", (char *)reader[ridx].nagra_boxkey);
493
494 if (reader[ridx].detect&0x80)
495 tpl_printf(vars, 0, "DETECT", "!%s", RDR_CD_TXT[reader[ridx].detect&0x7f]);
496 else
497 tpl_printf(vars, 0, "DETECT", "%s", RDR_CD_TXT[reader[ridx].detect&0x7f]);
498
499 tpl_printf(vars, 0, "MHZ", "%d", reader[ridx].mhz);
500 tpl_printf(vars, 0, "CARDMHZ", "%d", reader[ridx].cardmhz);
501
502
503#ifdef CS_WITH_GBOX
504 tpl_addVar(vars, 0, "GBOXPWD", (char *)reader[ridx].gbox_pwd);
505 tpl_addVar(vars, 0, "PREMIUM", reader[ridx].gbox_prem);
506#endif
507
508 if(reader[ridx].r_port) tpl_printf(vars, 0, "R_PORT", ",%d", reader[ridx].r_port);
509 if(reader[ridx].l_port) {
510 if(reader[ridx].r_port) tpl_printf(vars, 0, "L_PORT", ",%d", reader[ridx].l_port);
511 else tpl_printf(vars, 0, "L_PORT", ",,%d", reader[ridx].l_port);
512 }
513
514 //group
515 char *value = mk_t_group((ulong*)reader[ridx].grp);
516 tpl_printf(vars, 0, "GRP", "%s", value);
517 free(value);
518
519 //services
520 char sidok[33];
521 long2bitchar(reader[ridx].sidtabok, sidok);
522 char sidno[33];
523 long2bitchar(reader[ridx].sidtabno,sidno);
524 struct s_sidtab *sidtab = cfg->sidtab;
525 //build matrix
526 i = 0;
527 while(sidtab != NULL){
528 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
529 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
530 else tpl_addVar(vars, 0, "CHECKED", "");
531 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDOKBIT"));
532 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
533 else tpl_addVar(vars, 0, "CHECKED", "");
534 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDNOBIT"));
535 sidtab=sidtab->next;
536 i++;
537 }
538
539 // CAID
540 value = mk_t_caidtab(&reader[ridx].ctab);
541 tpl_addVar(vars, 0, "CAIDS", value);
542 free(value);
543
544 //ident
545 value = mk_t_ftab(&reader[ridx].ftab);
546 tpl_printf(vars, 0, "IDENTS", "%s\n", value);
547 free(value);
548
549 //class
550 CLASSTAB *clstab = &reader[ridx].cltab;
551 char *dot="";
552 for(i = 0; i < clstab->an; ++i){
553 tpl_printf(vars, 1, "CLASS", "%s%02x", dot, (int)clstab->aclass[i]);
554 dot=",";
555 }
556 for(i = 0; i < clstab->bn; ++i){
557 tpl_printf(vars, 0, "CLASS", "%s!%02x", dot, (int)clstab->bclass[i]);
558 dot=",";
559 }
560
561 //chid
562 int j;
563 dot="";
564 FTAB *ftab = &reader[ridx].fchid;
565 for (i = 0; i < ftab->nfilts; ++i){
566 tpl_printf(vars, 1, "CHIDS", "%s%04X", dot, ftab->filts[i].caid);
567 dot=":";
568 for (j = 0; j < ftab->filts[i].nprids; ++j) {
569 tpl_printf(vars, 1, "CHIDS", "%s%06lX", dot, ftab->filts[i].prids[j]);
570 dot=",";
571 }
572 dot=";";
573 }
574
575 tpl_printf(vars, 0, "SHOWCLS", "%d", reader[ridx].show_cls);
576 tpl_printf(vars, 0, "MAXQLEN", "%d", reader[ridx].maxqlen);
577 tpl_printf(vars, 0, "EMMCACHE", "%d,%d,&d", reader[ridx].cachemm, reader[ridx].rewritemm, reader[ridx].logemm);
578
579 //savenano
580 int all = 1;
581 dot="";
582 for(i = 0; i < 256; ++i){
583 if(!(reader[ridx].b_nano[i] & 0x02)){
584 all = 0;
585 break;
586 }
587 }
588 if (all == 1) tpl_printf(vars, 0, "SAVENANO", "all", reader[ridx].maxqlen);
589 else {
590 for(i = 0; i < 256; ++i){
591 if(reader[ridx].b_nano[i] & 0x02) tpl_printf(vars, 1, "SAVENANO", "%s%02x\n", dot, i);
592 dot=",";
593 }
594 }
595
596 //blocknano
597 dot="";
598 for(i = 0; i < 256; ++i){
599 if(!(reader[ridx].b_nano[i] & 0x01)){
600 all = 0;
601 break;
602 }
603 }
604 if (all == 1) tpl_printf(vars, 0, "BLOCKNANO", "all", reader[ridx].maxqlen);
605 else {
606 for(i = 0; i < 256; ++i){
607 if(reader[ridx].b_nano[i] & 0x01) tpl_printf(vars, 1, "BLOCKNANO", "%s%02x\n", dot, i);
608 dot=",";
609 }
610 }
611
612 tpl_addVar(vars, 0, "CCCVERSION", reader[ridx].cc_version);
613 tpl_addVar(vars, 0, "CCCBUILD", reader[ridx].cc_build);
614 tpl_printf(vars, 0, "CCCMAXHOP", "%d", reader[ridx].cc_maxhop);
615
616
617 // Show only parameters which needed for the reader
618 switch (reader[ridx].typ) {
619
620 case R_MOUSE :
621 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGMOUSEBIT"));
622 break;
623 case R_SMART :
624 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSMARTBIT"));
625 break;
626 case R_INTERNAL:
627 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGINTERNALBIT"));
628 break;
629 case R_SERIAL :
630 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSERIALBIT"));
631 break;
632 case R_CAMD35 :
633 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCAMD35BIT"));
634 break;
635 case R_CS378X :
636 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCS378XBIT"));
637 break;
638 case R_RADEGAST:
639 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGRADEGASTBIT"));
640 break;
641 case R_NEWCAMD :
642 if ( reader[ridx].ncd_proto == NCD_525 )
643 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGNCD525BIT"));
644 else if ( reader[ridx].ncd_proto == NCD_524 )
645 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGNCD524BIT"));
646 break;
647#ifdef CS_WITH_GBOX
648 case R_GBOX :
649 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGGBOXBIT"));
650 break;
651#endif
652#ifdef HAVE_PCSC
653 case R_PCSC :
654 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGPCSCBIT"));
655 break;
656#endif
657
658
659 }
660 //READERCONFIGMOUSEBIT
661 fputs(tpl_getTpl(vars, "READERCONFIG"), f);
662}
663
664void send_oscam_user_config_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in){
665 struct s_auth *account, *ptr;
666 char user[128];
667
668 if (strcmp(getParam(params, "action"), "Save As") == 0) cs_strncpy(user, getParam(params, "newuser"), sizeof(user)/sizeof(char));
669 else cs_strncpy(user, getParam(params, "user"), sizeof(user)/sizeof(char));
670
671 int i;
672
673 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
674
675 // Create a new user if it doesn't yet
676 if (account == NULL){
677 i = 1;
678 while(strlen(user) < 1){
679 snprintf(user, sizeof(user)/sizeof(char) - 1, "NEWUSER%d", i);
680 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
681 if(account != NULL) user[0] = '\0';
682 ++i;
683 }
684 if (!(account=malloc(sizeof(struct s_auth)))){
685 cs_log("Error allocating memory (errno=%d)", errno);
686 return;
687 }
688 if(cfg->account == NULL) cfg->account = account;
689 else {
690 for (ptr = cfg->account; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
691 ptr->next = account;
692 }
693 memset(account, 0, sizeof(struct s_auth));
694 cs_strncpy((char *)account->usr, user, sizeof(account->usr));
695 account->au=(-1);
696 account->monlvl=cfg->mon_level;
697 account->tosleep=cfg->tosleep;
698 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
699 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
700 account->expirationdate=(time_t)NULL;
701#ifdef CS_ANTICASC
702 account->ac_users=cfg->ac_users;
703 account->ac_penalty=cfg->ac_penalty;
704 account->ac_idx = account->ac_idx + 1;
705#endif
706 tpl_addVar(vars, 1, "MESSAGE", "<b>New user has been added with default settings</b><BR>");
707 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
708 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
709 // need to reget account as writing to disk changes account!
710 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
711 }
712
713 if((strcmp(getParam(params, "action"), "Save") == 0) || (strcmp(getParam(params, "action"), "Save As") == 0)){
714 char servicelabels[255]="";
715 //clear group
716 account->grp = 0;
717
718 for(i=0;i<(*params).paramcount;i++){
719 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "user")) && (strcmp((*params).params[i], "newuser"))){
720 if (!strcmp((*params).params[i], "expdate"))
721 account->expirationdate=(time_t)NULL;
722 if (!strcmp((*params).params[i], "services"))
723 sprintf(servicelabels + strlen(servicelabels), "%s,", (*params).values[i]);
724 else
725 chk_account((*params).params[i], (*params).values[i], account);
726 }
727 }
728 chk_account("services", servicelabels, account);
729 tpl_addVar(vars, 1, "MESSAGE", "<B>Settings updated</B><BR><BR>");
730 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
731 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
732 }
733 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
734
735 tpl_addVar(vars, 0, "USERNAME", account->usr);
736 tpl_addVar(vars, 0, "PASSWORD", account->pwd);
737
738 //Expirationdate
739 struct tm * timeinfo = localtime (&account->expirationdate);
740 char buf [80];
741 strftime (buf,80,"%Y-%m-%d",timeinfo);
742 if(strcmp(buf,"1970-01-01")) tpl_addVar(vars, 0, "EXPDATE", buf);
743
744 //Group
745 char *value = mk_t_group((ulong*)account->grp);
746 tpl_addVar(vars, 0, "GROUPS", value);
747 free(value);
748
749 //Hostname
750 tpl_addVar(vars, 0, "DYNDNS", (char *)account->dyndns);
751
752 //Uniq
753 tpl_printf(vars, 0, "TMP", "UNIQSELECTED%d", account->uniq);
754 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
755
756 //Sleep
757 if(!account->tosleep) tpl_addVar(vars, 0, "SLEEP", "0");
758 else tpl_printf(vars, 0, "SLEEP", "%d", account->tosleep);
759 //Monlevel selector
760 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", account->monlvl);
761 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
762
763 //AU Selector
764 if (!account->au) tpl_addVar(vars, 0, "AUSELECTED", "selected");
765 if (account->autoau == 1) tpl_addVar(vars, 0, "AUTOAUSELECTED", "selected");
766 int ridx;
767 for (ridx=0; ridx<CS_MAXREADER; ridx++){
768 if(!reader[ridx].device[0]) break;
769 tpl_addVar(vars, 0, "READERNAME", reader[ridx].label);
770 if (account->au == ridx) tpl_addVar(vars, 0, "SELECTED", "selected");
771 else tpl_addVar(vars, 0, "SELECTED", "");
772 tpl_addVar(vars, 1, "RDROPTION", tpl_getTpl(vars, "USEREDITRDRSELECTED"));
773 }
774
775 /* SERVICES */
776 //services - first we have to move the long sidtabok/sidtabno to a binary array
777 char sidok[33];
778 long2bitchar(account->sidtabok,sidok);
779 char sidno[33];
780 long2bitchar(account->sidtabno,sidno);
781 struct s_sidtab *sidtab = cfg->sidtab;
782 //build matrix
783 i=0;
784 while(sidtab != NULL){
785 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
786 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
787 else tpl_addVar(vars, 0, "CHECKED", "");
788 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDOKBIT"));
789 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
790 else tpl_addVar(vars, 0, "CHECKED", "");
791 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDNOBIT"));
792 sidtab=sidtab->next;
793 i++;
794 }
795
796 // CAID
797 value = mk_t_caidtab(&account->ctab);
798 tpl_addVar(vars, 0, "CAIDS", value);
799 free(value);
800
801 //ident
802 value = mk_t_ftab(&account->ftab);
803 tpl_printf(vars, 0, "IDENTS", "%s\n", value);
804 free(value);
805
806 //Betatunnel
807 value = mk_t_tuntab(&account->ttab);
808 tpl_addVar(vars, 0, "BETATUNNELS", value);
809 free(value);
810
811#ifdef CS_ANTICASC
812 tpl_printf(vars, 0, "AC_USERS", "%d", account->ac_users);
813 tpl_printf(vars, 0, "AC_PENALTY", "%d", account->ac_penalty);
814#endif
815 fputs(tpl_getTpl(vars, "USEREDIT"), f);
816}
817
818void send_oscam_user_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
819 struct s_auth *account, *account2;
820 char *user = getParam(params, "user");
821 int i, found = 0;
822
823 if (strcmp(getParam(params, "action"), "delete") == 0){
824 account=cfg->account;
825 if(strcmp(account->usr, user) == 0){
826 cfg->account = account->next;
827 free(account);
828 found = 1;
829 } else if (account->next != NULL){
830 do{
831 if(strcmp(account->next->usr, user) == 0){
832 account2 = account->next;
833 account->next = account2->next;
834 free(account2);
835 found = 1;
836 break;
837 }
838 } while ((account = account->next) && (account->next != NULL));
839 }
840
841 if (found > 0){
842 tpl_addVar(vars, 1, "MESSAGE", "<b>Account has been deleted!</b><BR>");
843 if (write_userdb()==0) refresh_oscam(REFR_ACCOUNTS, in);
844 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing configuration to disk failed!</b><BR>");
845 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified user doesn't exist. No deletion will be made!</b><BR>");
846 }
847 /* List accounts*/
848 char *status = "offline";
849 char *expired = "";
850 char *classname="offline";
851 char *lastchan="&nbsp;";
852 time_t now = time((time_t)0);
853 int isec=0;
854
855 for (account=cfg->account; (account) ; account=account->next){
856 expired = ""; classname="offline";
857 if(account->expirationdate && account->expirationdate<time(NULL)){
858 expired = " (expired)";
859 classname = "expired";
860 }
861 status="offline";
862
863 //search account in active clients
864 for (i=0; i<CS_MAXPID; i++)
865 if (!strcmp(client[i].usr, account->usr)){
866 //30 secs without ecm is offline
867 if ((now - client[i].lastecm) < 30){
868 status = "<b>online</b>";classname="online";
869 lastchan = monitor_get_srvname(client[i].last_srvid, client[i].last_caid);
870 isec = now - client[i].last;
871 }
872 }
873 tpl_addVar(vars, 0, "CLASSNAME", classname);
874 tpl_addVar(vars, 0, "USER", account->usr);
875 tpl_addVar(vars, 0, "USERENC", tpl_addTmp(vars, urlencode(account->usr)));
876 tpl_addVar(vars, 0, "STATUS", status);
877 tpl_addVar(vars, 0, "EXPIRED", expired);
878 tpl_addVar(vars, 0, "LASTCHANNEL", lastchan);
879
880 int secs = 0, fullmins =0, mins =0, hours =0;
881 if(isec > 0){
882 secs = isec % 60;
883 if (isec > 60){
884 fullmins = isec / 60;
885 mins = fullmins % 60;
886 if(fullmins > 60) hours = fullmins / 60;
887 }
888 }
889
890 tpl_printf(vars, 0, "IDLESECS", "%02d:%02d:%02d", hours, mins, secs);
891 tpl_addVar(vars, 1, "USERCONFIGS", tpl_getTpl(vars, "USERCONFIGLISTBIT"));
892 isec = 0;
893 lastchan = "&nbsp;";
894 }
895 fputs(tpl_getTpl(vars, "USERCONFIGLIST"), f);
896}
897
898void send_oscam_entitlement(struct templatevars *vars, FILE *f, struct uriparams *params) {
899 /* build entitlements from reader init history */
900#ifdef CS_RDR_INIT_HIST
901 int ridx;
902 char *p;
903 char *reader_ = getParam(params, "reader");
904 if(strlen(reader_) > 0){
905 for (ridx=0; ridx<CS_MAXREADER && strcmp(reader_, reader[ridx].label) != 0; ridx++);
906 if(ridx<CS_MAXREADER){
907 for (p=(char *)reader[ridx].init_history; *p; p+=strlen(p)+1){
908 tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p);
909 }
910 }
911 tpl_addVar(vars, 0, "READERNAME", reader_);
912 }
913#else
914 tpl_addVar(vars, 0, "LOGHISTORY", "Your binary has not been compiled with the \
915 CS_RDR_INIT_HIST flag (some architectures disable this \
916 per default to save ressources). Please recompile if you \
917 need this feature! This is not a bug!<BR>\n");
918#endif
919 fputs(tpl_getTpl(vars, "ENTITLEMENTS"), f);
920}
921
922void send_oscam_status(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
923 int i;
924 char *usr;
925 int lsec, isec, cnr, con, cau;
926 time_t now = time((time_t)0);
927 struct tm *lt;
928
929
930 char *hideidle = getParam(params, "hideidle");
931 if(strlen(hideidle) > 0){
932 int oldval = cfg->http_hide_idle_clients;
933 chk_t_monitor("httphideidleclients", hideidle);
934 if(oldval != cfg->http_hide_idle_clients){
935 refresh_oscam(REFR_SERVER, in);
936 }
937 }
938
939 if(cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED1", "selected");
940 else tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED0", "selected");
941
942 for (i=0; i<CS_MAXPID; i++) {
943 if (client[i].pid) {
944
945 if((cfg->http_hide_idle_clients == 1) && (client[i].typ == 'c') && ((now - client[i].lastecm) > cfg->mon_hideclient_to)) continue;
946
947 lsec=now-client[i].login;
948 isec=now-client[i].last;
949 usr=client[i].usr;
950
951 if (((client[i].typ=='r') || (client[i].typ=='p')) && (con=cs_idx2ridx(i))>=0) usr=reader[con].label;
952
953 if (client[i].dup) con=2;
954 else if ((client[i].tosleep) && (now-client[i].lastswitch>client[i].tosleep)) con=1;
955 else con=0;
956
957 if (i-cdiff>0) cnr=i-cdiff;
958 else cnr=(i>1) ? i-1 : 0;
959
960 if( (cau=client[i].au+1) && (now-client[i].lastemm)/60 > cfg->mon_aulow) cau=-cau;
961
962 lt=localtime(&client[i].login);
963
964 tpl_printf(vars, 0, "CLIENTPID", "%d", client[i].pid);
965 tpl_printf(vars, 0, "CLIENTTYPE", "%c", client[i].typ);
966 tpl_printf(vars, 0, "CLIENTCNR", "%d", cnr);
967 tpl_addVar(vars, 0, "CLIENTUSER", usr);
968 tpl_printf(vars, 0, "CLIENTCAU", "%d", cau);
969 tpl_printf(vars, 0, "CLIENTCRYPTED", "%d", client[i].crypted);
970 tpl_printf(vars, 0, "CLIENTIP", "%s", cs_inet_ntoa(client[i].ip));
971 tpl_printf(vars, 0, "CLIENTPORT", "%d", client[i].port);
972 tpl_addVar(vars, 0, "CLIENTPROTO", monitor_get_proto(i));
973 tpl_printf(vars, 0, "CLIENTLOGINDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
974 tpl_printf(vars, 0, "CLIENTLOGINTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
975
976 int secs = 0, fullmins =0, mins =0, hours =0;
977 if(lsec > 0){
978 secs = lsec % 60;
979 if (lsec > 60){
980 fullmins = lsec / 60;
981 mins = fullmins % 60;
982 if(fullmins > 60) hours = fullmins / 60;
983 }
984 }
985
986 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%02d:%02d:%02d", hours, mins, secs);
987 tpl_printf(vars, 0, "CLIENTCAID", "%04X", client[i].last_caid);
988 tpl_printf(vars, 0, "CLIENTSRVID", "%04X", client[i].last_srvid);
989
990 int j, found = 0;
991 struct s_srvid *srvid = cfg->srvid;
992 if (srvid){
993 while ((srvid = srvid->next) && (srvid->next != NULL)){
994 if (srvid->srvid == client[i].last_srvid){
995 for (j=0; j < srvid->ncaid; j++){
996 if (srvid->caid[j] == client[i].last_caid){
997 found = 1;
998 break;
999 }
1000 }
1001 }
1002 if (found == 1) break;
1003 }
1004 }
1005 if (found == 1){
1006 tpl_printf(vars, 0, "CLIENTSRVPROVIDER","%s : ", srvid->prov);
1007 tpl_addVar(vars, 0, "CLIENTSRVNAME", srvid->name);
1008 tpl_addVar(vars, 0, "CLIENTSRVTYPE", srvid->type);
1009 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION", srvid->desc);
1010 }else{
1011 tpl_addVar(vars, 0, "CLIENTSRVPROVIDER","");
1012 tpl_printf(vars, 0, "CLIENTSRVNAME","");
1013 tpl_addVar(vars, 0, "CLIENTSRVTYPE","");
1014 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION","");
1015 }
1016
1017 secs = 0; fullmins =0; mins =0; hours =0;
1018 if(isec > 0){
1019 secs = isec % 60;
1020 if (isec > 60){
1021 fullmins = isec / 60;
1022 mins = fullmins % 60;
1023 if(fullmins > 60) hours = fullmins / 60;
1024 }
1025 }
1026
1027 tpl_printf(vars, 0, "CLIENTIDLESECS", "%02d:%02d:%02d", hours, mins, secs);
1028 if(con == 2) tpl_printf(vars, 0, "CLIENTCON", "Duplicate");
1029 else if (con == 1) tpl_printf(vars, 0, "CLIENTCON", "Sleep");
1030 else tpl_printf(vars, 0, "CLIENTCON", "OK");
1031 tpl_printf(vars, 0, "CWOK", "%d", client[i].cwfound);
1032 tpl_printf(vars, 0, "CWNOK", "%d", client[i].cwnot);
1033 tpl_addVar(vars, 1, "CLIENTSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
1034 }
1035 }
1036
1037#ifdef CS_LOGHISTORY
1038 for (i=(*loghistidx+3) % CS_MAXLOGHIST; i!=*loghistidx; i=(i+1) % CS_MAXLOGHIST){
1039 char *p_usr, *p_txt;
1040 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
1041 p_txt=p_usr+32;
1042 if (p_txt[0]) tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p_txt+8);
1043 }
1044#else
1045 tpl_addVar(vars, 0, "LOGHISTORY", "the flag CS_LOGHISTORY is not set in your binary<BR>\n");
1046#endif
1047
1048 fputs(tpl_getTpl(vars, "STATUS"), f);
1049}
1050
1051void send_oscam_services_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1052 struct s_sidtab *sidtab,*ptr;
1053 char label[128];
1054 int i;
1055
1056 cs_strncpy(label, strtolower(getParam(params, "service")), sizeof(label)/sizeof(char));
1057
1058 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1059
1060 if (sidtab == NULL){
1061 i = 1;
1062 while(strlen(label) < 1){
1063 snprintf(label, sizeof(label)/sizeof(char) - 1, "newservice%d", i);
1064 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab = sidtab->next);
1065 if(sidtab != NULL) label[0] = '\0';
1066 ++i;
1067 }
1068 if (!(sidtab=malloc(sizeof(struct s_sidtab)))){
1069 cs_log("Error allocating memory (errno=%d)", errno);
1070 return;
1071 }
1072
1073 if(cfg->sidtab == NULL) cfg->sidtab = sidtab;
1074 else {
1075 for (ptr = cfg->sidtab; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
1076 ptr->next = sidtab;
1077 }
1078 memset(sidtab, 0, sizeof(struct s_sidtab));
1079 cs_strncpy((char *)sidtab->label, label, sizeof(sidtab->label));
1080
1081 tpl_addVar(vars, 1, "MESSAGE", "<b>New service has been added</b><BR>");
1082 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1083 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
1084
1085 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1086 }
1087
1088 if (strcmp(getParam(params, "action"), "Save") == 0){
1089 for(i=0;i<(*params).paramcount;i++){
1090 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "service"))){
1091 chk_sidtab((*params).params[i], (*params).values[i], sidtab);
1092 }
1093 }
1094 tpl_addVar(vars, 1, "MESSAGE", "<B>Services updated</B><BR><BR>");
1095 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1096 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
1097
1098 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1099 }
1100
1101 tpl_addVar(vars, 0, "LABEL", sidtab->label);
1102 tpl_addVar(vars, 0, "LABELENC", urlencode(sidtab->label));
1103
1104 for (i=0; i<sidtab->num_caid; i++){
1105 if (i==0) tpl_printf(vars, 0, "CAIDS", "%04X", sidtab->caid[i]);
1106 else tpl_printf(vars, 1, "CAIDS", ",%04X", sidtab->caid[i]);
1107 }
1108 for (i=0; i<sidtab->num_provid; i++){
1109 if (i==0) tpl_printf(vars, 0, "PROVIDS", "%08lX", sidtab->provid[i]);
1110 else tpl_printf(vars, 1, "PROVIDS", ",%08lX", sidtab->provid[i]);
1111 }
1112 for (i=0; i<sidtab->num_srvid; i++){
1113 if (i==0) tpl_printf(vars, 0, "SRVIDS", "%04X", sidtab->srvid[i]);
1114 else tpl_printf(vars, 1, "SRVIDS", ",%04X", sidtab->srvid[i]);
1115 }
1116 fputs(tpl_getTpl(vars, "SERVICEEDIT"), f);
1117}
1118
1119void send_oscam_services(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1120 struct s_sidtab *sidtab, *sidtab2;
1121 char *service = getParam(params, "service");
1122 int i, found = 0;
1123
1124 if (strcmp(getParam(params, "action"), "delete") == 0){
1125 sidtab=cfg->sidtab;
1126 if(strcmp(sidtab->label, service) == 0){
1127 cfg->sidtab = sidtab->next;
1128 free(sidtab);
1129 found = 1;
1130 } else if (sidtab->next != NULL){
1131 do{
1132 if(strcmp(sidtab->next->label, service) == 0){
1133 sidtab2 = sidtab->next;
1134 sidtab->next = sidtab2->next;
1135 free(sidtab2);
1136 found = 1;
1137 break;
1138 }
1139 } while ((sidtab = sidtab->next) && (sidtab->next != NULL));
1140 }
1141 if (found > 0){
1142 tpl_addVar(vars, 1, "MESSAGE", "<b>Service has been deleted!</b><BR>");
1143 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1144 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
1145 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified service doesn't exist. No deletion will be made!</b><BR>");
1146 }
1147
1148 sidtab=cfg->sidtab;
1149 // Show List
1150 while(sidtab != NULL){
1151 tpl_printf(vars, 0, "SID","");
1152 if ((strcmp(getParam(params, "service"), sidtab->label) == 0) && (strcmp(getParam(params, "action"), "list") == 0) ){
1153 tpl_printf(vars, 0, "SIDCLASS","sidlist");
1154 for (i=0; i<sidtab->num_srvid; i++){
1155 tpl_printf(vars, 1, "SID", "%04X : %s<BR>", sidtab->srvid[i], monitor_get_srvname(sidtab->srvid[i], sidtab->caid[0]));
1156 }
1157 } else {
1158 tpl_printf(vars, 0, "SIDCLASS","");
1159 tpl_printf(vars, 0, "SID","<A HREF=\"services.html?service=%s&action=list\">Show Services</A>",tpl_addTmp(vars, urlencode(sidtab->label)));
1160 }
1161 tpl_addVar(vars, 0, "LABELENC", tpl_addTmp(vars, urlencode(sidtab->label)));
1162 tpl_addVar(vars, 0, "LABEL", sidtab->label);
1163 tpl_addVar(vars, 0, "SIDLIST", tpl_getTpl(vars, "SERVICECONFIGSIDBIT"));
1164 tpl_addVar(vars, 1, "SERVICETABS", tpl_getTpl(vars, "SERVICECONFIGLISTBIT"));
1165 sidtab=sidtab->next;
1166 }
1167 fputs(tpl_getTpl(vars, "SERVICECONFIGLIST"), f);
1168}
1169
1170void send_oscam_savetpls(struct templatevars *vars, FILE *f){
1171 if(strlen(cfg->http_tpl) > 0){
1172 tpl_printf(vars, 0, "CNT", "%d", tpl_saveIncludedTpls(cfg->http_tpl));
1173 tpl_addVar(vars, 0, "PATH", cfg->http_tpl);
1174 } else tpl_addVar(vars, 0, "CNT", "0");
1175 fputs(tpl_getTpl(vars, "SAVETEMPLATES"), f);
1176}
1177
1178void send_oscam_shutdown(struct templatevars *vars, FILE *f){
1179 tpl_printf(vars, 0, "REFRESHTIME", "%d", SHUTDOWNREFRESH);
1180 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1181 tpl_printf(vars, 0, "SECONDS", "%d", SHUTDOWNREFRESH);
1182 fputs(tpl_getTpl(vars, "SHUTDOWN"), f);
1183 running = 0;
1184}
1185
1186int process_request(FILE *f, struct in_addr in) {
1187 char buf[4096];
1188 char tmp[4096];
1189
1190 int authok = 0;
1191 char expectednonce[64];
1192
1193 char *method;
1194 char *path;
1195 char *protocol;
1196 char *pch;
1197 char *pch2;
1198 /* List of possible pages */
1199 char *pages[]={
1200 "/config.html",
1201 "/readers.html",
1202 "/entitlements.html",
1203 "/status.html",
1204 "/userconfig.html",
1205 "/readerconfig.html",
1206 "/services.html",
1207 "/user_edit.html",
1208 "/site.css",
1209 "/services_edit.html",
1210 "/savetemplates.html",
1211 "/shutdown.html"};
1212 int pagescnt = sizeof(pages)/sizeof(char *); // Calculate the amount of items in array
1213
1214 int pgidx = -1;
1215 int i;
1216 int parsemode = 1;
1217 struct uriparams params;
1218 params.paramcount = 0;
1219
1220 /* First line always includes the GET/POST request */
1221 if (!fgets(buf, sizeof(buf), f)) return -1;
1222 method = strtok(buf, " ");
1223 path = strtok(NULL, " ");
1224 protocol = strtok(NULL, "\r");
1225 if(method == NULL || path == NULL || protocol == NULL) return -1;
1226
1227 pch=path;
1228 /* advance pointer to beginning of query string */
1229 while(pch[0] != '?' && pch[0] != '\0') ++pch;
1230 if(pch[0] == '?'){
1231 pch[0] = '\0';
1232 ++pch;
1233 }
1234
1235 /* Map page to our static page definitions */
1236 for (i=0; i<pagescnt; i++){
1237 if (!strcmp(path, pages[i])) pgidx = i;
1238 }
1239
1240 /* Parse url parameters; parsemode = 1 means parsing next param, parsemode = -1 parsing next
1241 value; pch2 points to the beginning of the currently parsed string, pch is the current position */
1242 pch2=pch;
1243 while(pch[0] != '\0'){
1244 if((parsemode == 1 && pch[0] == '=') || (parsemode == -1 && pch[0] == '&')){
1245 pch[0] = '\0';
1246 urldecode(pch2);
1247 if(parsemode == 1) {
1248 if(params.paramcount >= MAXGETPARAMS) break;
1249 ++params.paramcount;
1250 params.params[params.paramcount-1] = pch2;
1251 } else {
1252 params.values[params.paramcount-1] = pch2;
1253 }
1254 parsemode = -parsemode;
1255 pch2 = pch + 1;
1256 }
1257 ++pch;
1258 }
1259 /* last value wasn't processed in the loop yet... */
1260 if(parsemode == -1 && params.paramcount <= MAXGETPARAMS){
1261 urldecode(pch2);
1262 params.values[params.paramcount-1] = pch2;
1263 }
1264
1265 if(strlen(cfg->http_user) == 0 || strlen(cfg->http_pwd) == 0) authok = 1;
1266 else calculate_nonce(expectednonce, sizeof(expectednonce)/sizeof(char));
1267
1268 /* Read remaining request (we're only interested in auth header) */
1269 while (fgets(tmp, sizeof(tmp), f)) {
1270 if (tmp[0] == '\r' && tmp[1] == '\n') break;
1271 else if(authok == 0 && strlen(tmp) > 50 && strncmp(tmp, "Authorization:", 14) == 0 && strstr(tmp, "Digest") != NULL) {
1272 authok = check_auth(tmp, method, path, expectednonce);
1273 }
1274 }
1275
1276 //cs_debug("%s %d\n", path, pgidx);
1277 //for(i=0; i < params.paramcount; ++i) cs_debug("%s : %s\n", params.params[i], params.values[i]);
1278
1279 fseek(f, 0, SEEK_CUR); // Force change of stream direction
1280
1281 if(authok != 1){
1282 strcpy(tmp, "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\"");
1283 strcat(tmp, AUTHREALM);
1284 strcat(tmp, "\", qop=\"auth\", opaque=\"\", nonce=\"");
1285 strcat(tmp, expectednonce);
1286 strcat(tmp, "\"");
1287 if(authok == 2) strcat(tmp, ", stale=true");
1288 send_headers(f, 401, "Unauthorized", tmp, "text/html");
1289 return 0;
1290 }
1291
1292 /*build page*/
1293 send_headers(f, 200, "OK", NULL, "text/html");
1294 if(pgidx == 8) send_css(f);
1295 else {
1296 time_t t;
1297 struct templatevars *vars = tpl_create();
1298 struct tm *lt;
1299 time(&t);
1300
1301 lt=localtime(&t);
1302 tpl_addVar(vars, 0, "CS_VERSION", CS_VERSION);
1303 tpl_addVar(vars, 0, "CS_SVN_VERSION", CS_SVN_VERSION);
1304 if(cfg->http_refresh > 0 && (pgidx == 3 || pgidx == -1)){
1305 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
1306 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1307 }
1308 tpl_printf(vars, 0, "CURDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
1309 tpl_printf(vars, 0, "CURTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
1310 tpl_printf(vars, 0, "CURIP", "%s", inet_ntoa(*(struct in_addr *)&in));
1311 switch(pgidx){
1312 case 0: send_oscam_config(vars, f, &params, in); break;
1313 case 1: send_oscam_reader(vars, f); break;
1314 case 2: send_oscam_entitlement(vars, f, &params); break;
1315 case 3: send_oscam_status(vars, f, &params, in); break;
1316 case 4: send_oscam_user_config(vars, f, &params, in); break;
1317 case 5: send_oscam_reader_config(vars, f, &params, in); break;
1318 case 6: send_oscam_services(vars, f, &params, in); break;
1319 case 7: send_oscam_user_config_edit(vars, f, &params, in); break;
1320 case 9: send_oscam_services_edit(vars, f, &params, in); break;
1321 case 10: send_oscam_savetpls(vars, f); break;
1322 case 11: send_oscam_shutdown(vars, f); break;
1323 default: send_oscam_status(vars, f, &params, in); break;
1324 }
1325 tpl_clear(vars);
1326 }
1327 return 0;
1328}
1329
1330void http_srv() {
1331 int i,sock, reuse =1;
1332 struct sockaddr_in sin;
1333 struct sockaddr_in remote;
1334 socklen_t len = sizeof(remote);
1335 char *tmp;
1336
1337 /* Prepare lookup array for conversion between ascii and hex */
1338 tmp = malloc(3*sizeof(char));
1339 for(i=0; i<256; i++) {
1340 snprintf(tmp, 3,"%02x", i);
1341 memcpy(hex2ascii[i], tmp, 2);
1342 }
1343 free(tmp);
1344 /* Create random string for nonce value generation */
1345 srand(time(NULL));
1346 create_rand_str(noncekey,32);
1347
1348 /* Startup server */
1349 if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
1350 cs_log("HTTP Server: Creating socket failed! (errno=%d)", errno);
1351 return;
1352 }
1353 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){
1354 cs_log("HTTP Server: Setting SO_REUSEADDR via setsockopt failed! (errno=%d)", errno);
1355 }
1356
1357 memset(&sin, 0, sizeof sin);
1358 sin.sin_family = AF_INET;
1359 sin.sin_addr.s_addr = INADDR_ANY;
1360 sin.sin_port = htons(cfg->http_port);
1361 if((bind(sock, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
1362 cs_log("HTTP Server couldn't bind on port %d (errno=%d). Not starting HTTP!", cfg->http_port, errno);
1363 close(sock);
1364 return;
1365 }
1366 if (listen(sock, SOMAXCONN) < 0){
1367 cs_log("HTTP Server: Call to listen() failed! (errno=%d)", errno);
1368 close(sock);
1369 return;
1370 }
1371 cs_log("HTTP Server listening on port %d", cfg->http_port);
1372 while (running)
1373 {
1374 int s;
1375 FILE *f;
1376 if((s = accept(sock, (struct sockaddr *) &remote, &len)) < 0){
1377 cs_log("HTTP Server: Error calling accept() (errno=%d).", errno);
1378 break;
1379 }
1380
1381 f = fdopen(s, "r+");
1382 process_request(f, remote.sin_addr);
1383 fflush(f);
1384 fclose(f);
1385 shutdown(s, SHUT_WR);
1386 close(s);
1387 }
1388 close(sock);
1389 kill(client[0].pid, SIGQUIT);
1390}
Note: See TracBrowser for help on using the repository browser.