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

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

WebIf:

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