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

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

WebIF:

  • !!!!! implementation of a new oscam.srvid Format !!!!
  • the Format and good sorted list you find here: http://streamboard.gmc.to/wiki/index.php/Srvid#microMon.srvid
  • the old oscam.srvid format is not parsable from this version onwards
  • The identification of a channel works now over srvid + caid. So doubles should be a historical nightmare
File size: 49.2 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 char *hideidle = getParam(params, "hideidle");
929 if(strlen(hideidle) > 0){
930 int oldval = cfg->http_hide_idle_clients;
931 chk_t_monitor("httphideidleclients", hideidle);
932 if(oldval != cfg->http_hide_idle_clients){
933 refresh_oscam(REFR_SERVER, in);
934 }
935 }
936
937 if(cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED1", "selected");
938 else tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED0", "selected");
939
940 for (i=0; i<CS_MAXPID; i++) {
941 if (client[i].pid) {
942
943 if((cfg->http_hide_idle_clients == 1) && (client[i].typ == 'c') && ((now - client[i].lastecm) > cfg->mon_hideclient_to)) continue;
944
945 lsec=now-client[i].login;
946 isec=now-client[i].last;
947 usr=client[i].usr;
948
949 if (((client[i].typ=='r') || (client[i].typ=='p')) && (con=cs_idx2ridx(i))>=0) usr=reader[con].label;
950
951 if (client[i].dup) con=2;
952 else if ((client[i].tosleep) && (now-client[i].lastswitch>client[i].tosleep)) con=1;
953 else con=0;
954
955 if (i-cdiff>0) cnr=i-cdiff;
956 else cnr=(i>1) ? i-1 : 0;
957
958 if( (cau=client[i].au+1) && (now-client[i].lastemm)/60 > cfg->mon_aulow) cau=-cau;
959
960 lt=localtime(&client[i].login);
961
962 tpl_printf(vars, 0, "CLIENTPID", "%d", client[i].pid);
963 tpl_printf(vars, 0, "CLIENTTYPE", "%c", client[i].typ);
964 tpl_printf(vars, 0, "CLIENTCNR", "%d", cnr);
965 tpl_addVar(vars, 0, "CLIENTUSER", usr);
966 tpl_printf(vars, 0, "CLIENTCAU", "%d", cau);
967 tpl_printf(vars, 0, "CLIENTCRYPTED", "%d", client[i].crypted);
968 tpl_printf(vars, 0, "CLIENTIP", "%s", cs_inet_ntoa(client[i].ip));
969 tpl_printf(vars, 0, "CLIENTPORT", "%d", client[i].port);
970 tpl_addVar(vars, 0, "CLIENTPROTO", monitor_get_proto(i));
971 tpl_printf(vars, 0, "CLIENTLOGINDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
972 tpl_printf(vars, 0, "CLIENTLOGINTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
973
974 int secs = 0, fullmins =0, mins =0, hours =0;
975 if(lsec > 0){
976 secs = lsec % 60;
977 if (lsec > 60){
978 fullmins = lsec / 60;
979 mins = fullmins % 60;
980 if(fullmins > 60) hours = fullmins / 60;
981 }
982 }
983
984 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%02d:%02d:%02d", hours, mins, secs);
985 tpl_printf(vars, 0, "CLIENTCAID", "%04X", client[i].last_caid);
986 tpl_printf(vars, 0, "CLIENTSRVID", "%04X", client[i].last_srvid);
987 tpl_addVar(vars, 0, "CLIENTSRVNAME", monitor_get_srvname(client[i].last_srvid, client[i].last_caid));
988
989 secs = 0; fullmins =0; mins =0; hours =0;
990 if(isec > 0){
991 secs = isec % 60;
992 if (isec > 60){
993 fullmins = isec / 60;
994 mins = fullmins % 60;
995 if(fullmins > 60) hours = fullmins / 60;
996 }
997 }
998
999 tpl_printf(vars, 0, "CLIENTIDLESECS", "%02d:%02d:%02d", hours, mins, secs);
1000 if(con == 2) tpl_printf(vars, 0, "CLIENTCON", "Duplicate");
1001 else if (con == 1) tpl_printf(vars, 0, "CLIENTCON", "Sleep");
1002 else tpl_printf(vars, 0, "CLIENTCON", "OK");
1003 tpl_printf(vars, 0, "CWOK", "%d", client[i].cwfound);
1004 tpl_printf(vars, 0, "CWNOK", "%d", client[i].cwnot);
1005 tpl_addVar(vars, 1, "CLIENTSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
1006 }
1007 }
1008
1009#ifdef CS_LOGHISTORY
1010 for (i=(*loghistidx+3) % CS_MAXLOGHIST; i!=*loghistidx; i=(i+1) % CS_MAXLOGHIST){
1011 char *p_usr, *p_txt;
1012 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
1013 p_txt=p_usr+32;
1014 if (p_txt[0]) tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", p_txt+8);
1015 }
1016#else
1017 tpl_addVar(vars, 0, "LOGHISTORY", "the flag CS_LOGHISTORY is not set in your binary<BR>\n");
1018#endif
1019
1020 fputs(tpl_getTpl(vars, "STATUS"), f);
1021}
1022
1023void send_oscam_services_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1024 struct s_sidtab *sidtab,*ptr;
1025 char label[128];
1026 int i;
1027
1028 strncpy(label, strtolower(getParam(params, "service")), sizeof(label)/sizeof(char) - 1);
1029 label[sizeof(label)/sizeof(char) - 1] = '\0';
1030
1031 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1032
1033 if (sidtab == NULL){
1034 i = 1;
1035 while(strlen(label) < 1){
1036 snprintf(label, sizeof(label)/sizeof(char) - 1, "newservice%d", i);
1037 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab = sidtab->next);
1038 if(sidtab != NULL) label[0] = '\0';
1039 ++i;
1040 }
1041 if (!(sidtab=malloc(sizeof(struct s_sidtab)))){
1042 cs_log("Error allocating memory (errno=%d)", errno);
1043 return;
1044 }
1045
1046 if(cfg->sidtab == NULL) cfg->sidtab = sidtab;
1047 else {
1048 for (ptr = cfg->sidtab; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
1049 ptr->next = sidtab;
1050 }
1051 memset(sidtab, 0, sizeof(struct s_sidtab));
1052 strncpy((char *)sidtab->label, label, sizeof(sidtab->label)-1);
1053
1054 tpl_addVar(vars, 1, "MESSAGE", "<b>New service has been added</b><BR>");
1055 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1056 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
1057
1058 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1059 }
1060
1061 if (strcmp(getParam(params, "action"), "Save") == 0){
1062 for(i=0;i<(*params).paramcount;i++){
1063 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "service"))){
1064 chk_sidtab((*params).params[i], (*params).values[i], sidtab);
1065 }
1066 }
1067 tpl_addVar(vars, 1, "MESSAGE", "<B>Services updated</B><BR><BR>");
1068 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1069 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
1070
1071 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
1072 }
1073
1074 tpl_addVar(vars, 0, "LABEL", sidtab->label);
1075 tpl_addVar(vars, 0, "LABELENC", urlencode(sidtab->label));
1076
1077 for (i=0; i<sidtab->num_caid; i++){
1078 if (i==0) tpl_printf(vars, 0, "CAIDS", "%04X", sidtab->caid[i]);
1079 else tpl_printf(vars, 1, "CAIDS", ",%04X", sidtab->caid[i]);
1080 }
1081 for (i=0; i<sidtab->num_provid; i++){
1082 if (i==0) tpl_printf(vars, 0, "PROVIDS", "%08lX", sidtab->provid[i]);
1083 else tpl_printf(vars, 1, "PROVIDS", ",%08lX", sidtab->provid[i]);
1084 }
1085 for (i=0; i<sidtab->num_srvid; i++){
1086 if (i==0) tpl_printf(vars, 0, "SRVIDS", "%04X", sidtab->srvid[i]);
1087 else tpl_printf(vars, 1, "SRVIDS", ",%04X", sidtab->srvid[i]);
1088 }
1089 fputs(tpl_getTpl(vars, "SERVICEEDIT"), f);
1090}
1091
1092void send_oscam_services(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1093 struct s_sidtab *sidtab, *sidtab2;
1094 char *service = getParam(params, "service");
1095 int i, found = 0;
1096
1097 if (strcmp(getParam(params, "action"), "delete") == 0){
1098 sidtab=cfg->sidtab;
1099 if(strcmp(sidtab->label, service) == 0){
1100 cfg->sidtab = sidtab->next;
1101 free(sidtab);
1102 found = 1;
1103 } else if (sidtab->next != NULL){
1104 do{
1105 if(strcmp(sidtab->next->label, service) == 0){
1106 sidtab2 = sidtab->next;
1107 sidtab->next = sidtab2->next;
1108 free(sidtab2);
1109 found = 1;
1110 break;
1111 }
1112 } while ((sidtab = sidtab->next) && (sidtab->next != NULL));
1113 }
1114 if (found > 0){
1115 tpl_addVar(vars, 1, "MESSAGE", "<b>Service has been deleted!</b><BR>");
1116 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
1117 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
1118 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified service doesn't exist. No deletion will be made!</b><BR>");
1119 }
1120
1121 sidtab=cfg->sidtab;
1122 // Show List
1123 while(sidtab != NULL){
1124 tpl_printf(vars, 0, "SID","");
1125 if ((strcmp(getParam(params, "service"), sidtab->label) == 0) && (strcmp(getParam(params, "action"), "list") == 0) ){
1126 tpl_printf(vars, 0, "SIDCLASS","sidlist");
1127 for (i=0; i<sidtab->num_srvid; i++){
1128 tpl_printf(vars, 1, "SID", "%04X : %s<BR>", sidtab->srvid[i], monitor_get_srvname(sidtab->srvid[i], sidtab->caid[0]));
1129 }
1130 } else {
1131 tpl_printf(vars, 0, "SIDCLASS","");
1132 tpl_printf(vars, 0, "SID","<A HREF=\"services.html?service=%s&action=list\">Show Services</A>",tpl_addTmp(vars, urlencode(sidtab->label)));
1133 }
1134 tpl_addVar(vars, 0, "LABELENC", tpl_addTmp(vars, urlencode(sidtab->label)));
1135 tpl_addVar(vars, 0, "LABEL", sidtab->label);
1136 tpl_addVar(vars, 0, "SIDLIST", tpl_getTpl(vars, "SERVICECONFIGSIDBIT"));
1137 tpl_addVar(vars, 1, "SERVICETABS", tpl_getTpl(vars, "SERVICECONFIGLISTBIT"));
1138 sidtab=sidtab->next;
1139 }
1140 fputs(tpl_getTpl(vars, "SERVICECONFIGLIST"), f);
1141}
1142
1143void send_oscam_savetpls(struct templatevars *vars, FILE *f){
1144 if(strlen(cfg->http_tpl) > 0){
1145 tpl_printf(vars, 0, "CNT", "%d", tpl_saveIncludedTpls(cfg->http_tpl));
1146 tpl_addVar(vars, 0, "PATH", cfg->http_tpl);
1147 } else tpl_addVar(vars, 0, "CNT", "0");
1148 fputs(tpl_getTpl(vars, "SAVETEMPLATES"), f);
1149}
1150
1151int process_request(FILE *f, struct in_addr in) {
1152 char buf[4096];
1153 char tmp[4096];
1154
1155 int authok = 0;
1156 char expectednonce[64];
1157
1158 char *method;
1159 char *path;
1160 char *protocol;
1161 char *pch;
1162 char *pch2;
1163 /* List of possible pages */
1164 char *pages[]={
1165 "/config.html",
1166 "/readers.html",
1167 "/entitlements.html",
1168 "/status.html",
1169 "/userconfig.html",
1170 "/readerconfig.html",
1171 "/services.html",
1172 "/user_edit.html",
1173 "/site.css",
1174 "/services_edit.html",
1175 "/savetemplates.html"};
1176 int pagescnt = sizeof(pages)/sizeof(char *); // Calculate the amount of items in array
1177
1178 int pgidx = -1;
1179 int i;
1180 int parsemode = 1;
1181 struct uriparams params;
1182 params.paramcount = 0;
1183
1184 /* First line always includes the GET/POST request */
1185 if (!fgets(buf, sizeof(buf), f)) return -1;
1186 method = strtok(buf, " ");
1187 path = strtok(NULL, " ");
1188 protocol = strtok(NULL, "\r");
1189 if(method == NULL || path == NULL || protocol == NULL) return -1;
1190
1191 pch=path;
1192 /* advance pointer to beginning of query string */
1193 while(pch[0] != '?' && pch[0] != '\0') ++pch;
1194 if(pch[0] == '?'){
1195 pch[0] = '\0';
1196 ++pch;
1197 }
1198
1199 /* Map page to our static page definitions */
1200 for (i=0; i<pagescnt; i++){
1201 if (!strcmp(path, pages[i])) pgidx = i;
1202 }
1203
1204 /* Parse url parameters; parsemode = 1 means parsing next param, parsemode = -1 parsing next
1205 value; pch2 points to the beginning of the currently parsed string, pch is the current position */
1206 pch2=pch;
1207 while(pch[0] != '\0'){
1208 if((parsemode == 1 && pch[0] == '=') || (parsemode == -1 && pch[0] == '&')){
1209 pch[0] = '\0';
1210 urldecode(pch2);
1211 if(parsemode == 1) {
1212 if(params.paramcount >= MAXGETPARAMS) break;
1213 ++params.paramcount;
1214 params.params[params.paramcount-1] = pch2;
1215 } else {
1216 params.values[params.paramcount-1] = pch2;
1217 }
1218 parsemode = -parsemode;
1219 pch2 = pch + 1;
1220 }
1221 ++pch;
1222 }
1223 /* last value wasn't processed in the loop yet... */
1224 if(parsemode == -1 && params.paramcount <= MAXGETPARAMS){
1225 urldecode(pch2);
1226 params.values[params.paramcount-1] = pch2;
1227 }
1228
1229 if(strlen(cfg->http_user) == 0 || strlen(cfg->http_pwd) == 0) authok = 1;
1230 else calculate_nonce(expectednonce, sizeof(expectednonce)/sizeof(char));
1231
1232 /* Read remaining request (we're only interested in auth header) */
1233 while (fgets(tmp, sizeof(tmp), f)) {
1234 if (tmp[0] == '\r' && tmp[1] == '\n') break;
1235 else if(authok == 0 && strlen(tmp) > 50 && strncmp(tmp, "Authorization:", 14) == 0 && strstr(tmp, "Digest") != NULL) {
1236 authok = check_auth(tmp, method, path, expectednonce);
1237 }
1238 }
1239
1240 //cs_debug("%s %d\n", path, pgidx);
1241 //for(i=0; i < params.paramcount; ++i) cs_debug("%s : %s\n", params.params[i], params.values[i]);
1242
1243 fseek(f, 0, SEEK_CUR); // Force change of stream direction
1244
1245 if(authok != 1){
1246 strcpy(tmp, "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\"");
1247 strcat(tmp, AUTHREALM);
1248 strcat(tmp, "\", qop=\"auth\", opaque=\"\", nonce=\"");
1249 strcat(tmp, expectednonce);
1250 strcat(tmp, "\"");
1251 if(authok == 2) strcat(tmp, ", stale=true");
1252 send_headers(f, 401, "Unauthorized", tmp, "text/html");
1253 return 0;
1254 }
1255
1256 /*build page*/
1257 send_headers(f, 200, "OK", NULL, "text/html");
1258 if(pgidx == 8) send_css(f);
1259 else {
1260 time_t t;
1261 struct templatevars *vars = tpl_create();
1262 struct tm *lt;
1263 time(&t);
1264
1265 lt=localtime(&t);
1266 tpl_addVar(vars, 0, "CS_VERSION", CS_VERSION);
1267 tpl_addVar(vars, 0, "CS_SVN_VERSION", CS_SVN_VERSION);
1268 if(cfg->http_refresh > 0 && (pgidx == 3 || pgidx == -1)){
1269 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
1270 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1271 }
1272 tpl_printf(vars, 0, "CURDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
1273 tpl_printf(vars, 0, "CURTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
1274 tpl_printf(vars, 0, "CURIP", "%s", inet_ntoa(*(struct in_addr *)&in));
1275 switch(pgidx){
1276 case 0: send_oscam_config(vars, f, &params, in); break;
1277 case 1: send_oscam_reader(vars, f); break;
1278 case 2: send_oscam_entitlement(vars, f, &params); break;
1279 case 3: send_oscam_status(vars, f, &params, in); break;
1280 case 4: send_oscam_user_config(vars, f, &params, in); break;
1281 case 5: send_oscam_reader_config(vars, f, &params, in); break;
1282 case 6: send_oscam_services(vars, f, &params, in); break;
1283 case 7: send_oscam_user_config_edit(vars, f, &params, in); break;
1284 case 9: send_oscam_services_edit(vars, f, &params, in); break;
1285 case 10: send_oscam_savetpls(vars, f); break;
1286 default: send_oscam_status(vars, f, &params, in); break;
1287 }
1288 tpl_clear(vars);
1289 }
1290 return 0;
1291}
1292
1293void http_srv() {
1294 int i,sock, reuse =1;
1295 struct sockaddr_in sin;
1296 struct sockaddr_in remote;
1297 socklen_t len = sizeof(remote);
1298 char *tmp;
1299
1300 /* Prepare lookup array for conversion between ascii and hex */
1301 tmp = malloc(3*sizeof(char));
1302 for(i=0; i<256; i++) {
1303 snprintf(tmp, 3,"%02x", i);
1304 memcpy(hex2ascii[i], tmp, 2);
1305 }
1306 free(tmp);
1307 /* Create random string for nonce value generation */
1308 srand(time(NULL));
1309 create_rand_str(noncekey,32);
1310
1311 /* Startup server */
1312 if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
1313 cs_log("HTTP Server: Creating socket failed! (errno=%d)", errno);
1314 return;
1315 }
1316 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){
1317 cs_log("HTTP Server: Setting SO_REUSEADDR via setsockopt failed! (errno=%d)", errno);
1318 }
1319
1320 memset(&sin, 0, sizeof sin);
1321 sin.sin_family = AF_INET;
1322 sin.sin_addr.s_addr = INADDR_ANY;
1323 sin.sin_port = htons(cfg->http_port);
1324 if((bind(sock, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
1325 cs_log("HTTP Server couldn't bind on port %d (errno=%d). Not starting HTTP!", cfg->http_port, errno);
1326 close(sock);
1327 return;
1328 }
1329 if (listen(sock, SOMAXCONN) < 0){
1330 cs_log("HTTP Server: Call to listen() failed! (errno=%d)", errno);
1331 close(sock);
1332 return;
1333 }
1334 cs_log("HTTP Server listening on port %d", cfg->http_port);
1335 while (1)
1336 {
1337 int s;
1338 FILE *f;
1339 if((s = accept(sock, (struct sockaddr *) &remote, &len)) < 0){
1340 cs_log("HTTP Server: Error calling accept() (errno=%d).", errno);
1341 break;
1342 }
1343
1344 f = fdopen(s, "r+");
1345 process_request(f, remote.sin_addr);
1346 fflush(f);
1347 fclose(f);
1348 shutdown(s, SHUT_WR);
1349 close(s);
1350 }
1351 close(sock);
1352}
Note: See TracBrowser for help on using the repository browser.