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

Last change on this file since 1212 was 1212, checked in by alno, 13 years ago

WebIf:

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