source: trunk/oscam-http.c@ 4149

Last change on this file since 4149 was 4141, checked in by dingo35, 13 years ago

all: simplify debug system, add D_DVBAPI = -d128, eliminate cs_ptyp which complicates stuff unnecc

File size: 100.5 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2#include "globals.h"
3#ifdef WEBIF
4//
5// OSCam HTTP server module
6//
7
8#include <stdio.h>
9#include <string.h>
10#include <time.h>
11#include <sys/stat.h>
12#include <dirent.h>
13#include <sys/socket.h>
14#include "oscam-http-helpers.c"
15#include "module-cccam.h"
16#include "module-stat.h"
17
18extern void restart_cardreader(struct s_reader *rdr, int restart);
19
20static int running = 1;
21
22#ifdef CS_ANTICASC
23static void kill_ac_client(void)
24{
25 struct s_client *cl;
26 for (cl=first_client->next; cl ; cl=cl->next)
27 if (cl->typ=='a') {
28 cs_accounts_chk();
29 break;
30 }
31}
32#endif
33
34struct s_reader *get_reader_by_label(char *lbl){
35 struct s_reader *rdr;
36 for (rdr = first_reader; rdr ; rdr = rdr->next) {
37 if (strcmp(lbl, rdr->label) == 0)
38 return rdr;
39 }
40 return NULL;
41}
42
43void refresh_oscam(enum refreshtypes refreshtype, struct in_addr in) {
44
45 switch (refreshtype) {
46 case REFR_ACCOUNTS:
47 cs_log("Refresh Accounts requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
48 init_userdb(&cfg->account);
49 cs_reinit_clients();
50
51#ifdef CS_ANTICASC
52 kill_ac_client();
53#endif
54 break;
55
56 case REFR_READERS:
57 cs_card_info();
58 cs_log("Refresh Reader/Tiers requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
59 break;
60
61 case REFR_SERVER:
62 cs_log("Refresh Server requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
63 //kill(first_client->pid, SIGHUP);
64 //todo how I can refresh the server after global settings
65 break;
66
67 case REFR_SERVICES:
68 cs_log("Refresh Services requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
69 //init_sidtab();
70 cs_reinit_clients();
71 break;
72
73#ifdef CS_ANTICASC
74 case REFR_ANTICASC:
75 cs_log("Refresh Anticascading requested by WebIF from %s", inet_ntoa(*(struct in_addr *)&in));
76 kill_ac_client();
77#endif
78 default:
79 break;
80 }
81}
82
83void send_oscam_config_global(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
84 int i;
85
86 if (strcmp(getParam(params, "action"), "execute") == 0) {
87 for(i = 0; i < (*params).paramcount; ++i) {
88 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
89 //tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
90 //we use the same function as used for parsing the config tokens
91
92 chk_t_global((*params).params[i], (*params).values[i]);
93 }
94 }
95 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Global done. You should restart Oscam now.</B><BR><BR>");
96 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
97 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
98 }
99 if (cfg->srvip != 0)
100 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->srvip));
101 if(cfg->disableuserfile == 1)
102 tpl_addVar(vars, 0, "CHKDISABLEUSERFILE", "checked");
103 if (cfg->usrfile != NULL) tpl_addVar(vars, 0, "USERFILE", cfg->usrfile);
104 if(cfg->disablelog == 1)
105 tpl_addVar(vars, 0, "CHKDISABLELOG", "checked");
106 if (cfg->logfile != NULL) tpl_addVar(vars, 0, "LOGFILE", cfg->logfile);
107 if (cfg->cwlogdir != NULL) tpl_addVar(vars, 0, "CWLOGDIR", cfg->cwlogdir);
108 tpl_printf(vars, 0, "USERFILEFLAG", "%d", cfg->usrfileflag);
109 tpl_printf(vars, 0, "CLIENTTIMEOUT", "%ld", cfg->ctimeout);
110 tpl_printf(vars, 0, "FALLBACKTIMEOUT", "%ld", cfg->ftimeout);
111 tpl_printf(vars, 0, "CLIENTMAXIDLE", "%d", cfg->cmaxidle);
112 tpl_printf(vars, 0, "CACHEDELAY", "%ld", cfg->delay);
113 tpl_printf(vars, 0, "BINDWAIT", "%d", cfg->bindwait);
114 tpl_printf(vars, 0, "NETPRIO", "%ld", cfg->netprio);
115 if (cfg->clientdyndns)
116 tpl_addVar(vars, 0, "CHKCLIENTDYNDNS", "checked");
117 tpl_printf(vars, 0, "RESOLVEDELAY", "%d", cfg->resolvedelay);
118 tpl_printf(vars, 0, "SLEEP", "%d", cfg->tosleep);
119 if (cfg->ulparent == 1)
120 tpl_addVar(vars, 0, "UNLOCKPARENTAL", "checked");
121 tpl_printf(vars, 0, "NICE", "%d", cfg->nice);
122 tpl_printf(vars, 0, "SERIALTIMEOUT", "%d", cfg->srtimeout);
123 tpl_printf(vars, 0, "MAXLOGSIZE", "%d", cfg->max_log_size);
124 if (cfg->waitforcards == 1)
125 tpl_addVar(vars, 0, "WAITFORCARDS", "checked");
126 if (cfg->preferlocalcards == 1)
127 tpl_addVar(vars, 0, "PREFERLOCALCARDS", "checked");
128 if (cfg->saveinithistory == 1)
129 tpl_addVar(vars, 0, "SAVEINITHISTORY", "checked");
130 if (cfg->reader_restart_seconds)
131 tpl_printf(vars, 0, "READERRESTARTSECONDS", "%d", cfg->reader_restart_seconds);
132
133 if (cfg->resolve_gethostbyname == 1)
134 tpl_addVar(vars, 0, "RESOLVER1", "selected");
135 else
136 tpl_addVar(vars, 0, "RESOLVER0", "selected");
137
138 tpl_printf(vars, 0, "FAILBANTIME", "%d", cfg->failbantime);
139
140
141#ifdef CS_WITH_DOUBLECHECK
142 if(cfg->double_check == 1)
143 tpl_addVar(vars, 0, "DCHECKCSELECTED", "selected");
144#endif
145
146
147
148 webif_write(tpl_getTpl(vars, "CONFIGGLOBAL"), f);
149}
150
151void send_oscam_config_loadbalancer(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
152 int i;
153 if (strcmp(getParam(params, "action"),"execute") == 0) {
154
155 memset(cfg->ser_device, 0, sizeof(cfg->ser_device));
156 for(i = 0; i < (*params).paramcount; ++i) {
157 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
158 //tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
159 //we use the same function as used for parsing the config tokens
160 if((*params).values[i][0])
161 chk_t_global((*params).params[i], (*params).values[i]);
162 }
163 }
164 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Loadbalancer done.</B><BR><BR>");
165 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
166 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
167 }
168
169 tpl_printf(vars, 0, "TMP", "LBMODE%d", cfg->lb_mode);
170 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
171
172 tpl_printf(vars, 0, "LBSAVE", "%d",cfg->lb_save);
173
174 tpl_printf(vars, 0, "LBNBESTREADERS", "%d",cfg->lb_nbest_readers);
175 tpl_printf(vars, 0, "LBNFBREADERS", "%d",cfg->lb_nfb_readers);
176 tpl_printf(vars, 0, "LBMINECMCOUNT", "%d",cfg->lb_min_ecmcount);
177 tpl_printf(vars, 0, "LBMAXECEMCOUNT", "%d",cfg->lb_max_ecmcount);
178 tpl_printf(vars, 0, "LBREOPENSECONDS", "%d",cfg->lb_reopen_seconds);
179
180 webif_write(tpl_getTpl(vars, "CONFIGLOADBALANCER"), f);
181
182}
183
184void send_oscam_config_camd33(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
185 int i;
186
187 if (strcmp(getParam(params, "action"), "execute") == 0) {
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 if (strcmp((*params).params[i], "nocrypt") == 0) {
192 clear_sip(&cfg->c33_plain);
193 }
194 //we use the same function as used for parsing the config tokens
195 chk_t_camd33((*params).params[i], (*params).values[i]);
196 }
197 }
198 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd33 done. You should restart Oscam now.</B><BR><BR>");
199 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
200 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
201 }
202
203 if (cfg->c33_port) {
204 tpl_printf(vars, 0, "PORT", "%d", cfg->c33_port);
205 if (cfg->c33_srvip != 0)
206 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
207 if (cfg->c33_passive == 1)
208 tpl_addVar(vars, 0, "PASSIVE", "checked");
209
210 for (i = 0; i < (int) sizeof(cfg->c33_key); ++i) tpl_printf(vars, 1, "KEY", "%02X",cfg->c33_key[i]);
211 struct s_ip *cip;
212 char *dot="";
213 for (cip = cfg->c33_plain; cip; cip = cip->next) {
214 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
215 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", cs_inet_ntoa(cip->ip[1]));
216 dot=",";
217 }
218 }
219
220 webif_write(tpl_getTpl(vars, "CONFIGCAMD33"), f);
221}
222
223void send_oscam_config_camd35(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
224 int i;
225 if ((strcmp(getParam(params, "action"),"execute") == 0) && (getParam(params, "port"))[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 //we use the same function as used for parsing the config tokens
230 chk_t_camd35((*params).params[i], (*params).values[i]);
231 }
232 }
233 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 done. You should restart Oscam now.</B><BR><BR>");
234 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
235 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
236 }
237
238 if (cfg->c35_port) {
239 tpl_printf(vars, 0, "PORT", "%d", cfg->c35_port);
240 if (cfg->c35_srvip != 0)
241 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_srvip));
242
243 if (cfg->c35_suppresscmd08)
244 tpl_addVar(vars, 0, "SUPPRESSCMD08", "checked");
245 }
246 webif_write(tpl_getTpl(vars, "CONFIGCAMD35"), f);
247}
248
249void send_oscam_config_camd35tcp(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
250 int i;
251 if ((strcmp(getParam(params, "action"),"execute") == 0) && (getParam(params, "port"))[0]) {
252 clear_ptab(&cfg->c35_tcp_ptab); /*clear Porttab*/
253 for(i = 0; i < (*params).paramcount; ++i) {
254 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
255 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
256 //we use the same function as used for parsing the config tokens
257 chk_t_camd35_tcp((*params).params[i], (*params).values[i]);
258 }
259 }
260 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration camd35 TCP done. You should restart Oscam now.</B><BR><BR>");
261 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
262 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
263 }
264
265 int j;
266 char *dot1, *dot2;
267 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
268 dot1 = "";
269 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i) {
270 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);
271 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 1) {
272 tpl_printf(vars, 1, "PORT", ":");
273 dot2 = "";
274 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j) {
275 tpl_printf(vars, 1, "PORT", "%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
276 dot2 = ",";
277 }
278 }
279 dot1=";";
280 }
281
282 if (cfg->c35_tcp_srvip != 0)
283 tpl_addVar(vars, 1, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
284
285 }
286 webif_write(tpl_getTpl(vars, "CONFIGCAMD35TCP"), f);
287}
288
289void send_oscam_config_newcamd(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
290 int i;
291 if (strcmp(getParam(params, "action"),"execute") == 0) {
292 clear_ptab(&cfg->ncd_ptab); /*clear Porttab*/
293 for(i = 0; i < (*params).paramcount; ++i) {
294 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
295 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
296 //we use the same function as used for parsing the config tokens
297 if (strcmp((*params).params[i], "allowed") == 0) {
298 clear_sip(&cfg->ncd_allowed);
299 }
300 chk_t_newcamd((*params).params[i], (*params).values[i]);
301 }
302 }
303 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Newcamd done. You should restart Oscam now.</B><BR><BR>");
304 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
305 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
306 }
307 int j;
308 char *dot1, *dot2;
309 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)) {
310 dot1 = "";
311 for(i = 0; i < cfg->ncd_ptab.nports; ++i) {
312 tpl_printf(vars, 1, "PORT", "%s%d", dot1, cfg->ncd_ptab.ports[i].s_port);
313
314 // separate DES Key
315 if(cfg->ncd_ptab.ports[i].ncd_key_is_set){
316 int k;
317 tpl_printf(vars, 1, "PORT", "{");
318 for (k = 0; k < 14; k++)
319 tpl_printf(vars, 1, "PORT", "%02X", cfg->ncd_ptab.ports[i].ncd_key[k]);
320 tpl_printf(vars, 1, "PORT", "}");
321 }
322
323 tpl_printf(vars, 1, "PORT", "@%04X", cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
324
325 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0) {
326 tpl_printf(vars, 1, "PORT", ":");
327 dot2 = "";
328 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j) {
329 tpl_printf(vars, 1, "PORT", "%s%06X", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
330 dot2 = ",";
331 }
332 }
333 dot1=";";
334 }
335
336
337 if (cfg->ncd_srvip != 0)
338 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
339
340 for (i=0;i<14;i++) tpl_printf(vars, 1, "KEY", "%02X", cfg->ncd_key[i]);
341
342 struct s_ip *cip;
343 char *dot="";
344 for (cip = cfg->ncd_allowed; cip; cip = cip->next) {
345 tpl_printf(vars, 1, "ALLOWED", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
346 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "ALLOWED", "-%s", cs_inet_ntoa(cip->ip[1]));
347 dot=",";
348 }
349
350 if (cfg->ncd_keepalive)
351 tpl_addVar(vars, 0, "KEEPALIVE", "checked");
352 if (cfg->ncd_mgclient)
353 tpl_addVar(vars, 0, "MGCLIENTCHK", "checked");
354 }
355 webif_write(tpl_getTpl(vars, "CONFIGNEWCAMD"), f);
356}
357
358void send_oscam_config_radegast(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
359 int i;
360 if (strcmp(getParam(params, "action"),"execute") == 0) {
361 for(i = 0; i < (*params).paramcount; ++i) {
362 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
363 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
364 if (strcmp((*params).params[i], "allowed") == 0) {
365 clear_sip(&cfg->rad_allowed);
366 }
367 //we use the same function as used for parsing the config tokens
368 chk_t_radegast((*params).params[i], (*params).values[i]);
369 }
370 }
371 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Radegast done. You should restart Oscam now.</B><BR><BR>");
372 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
373 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
374 }
375 tpl_printf(vars, 0, "PORT", "%d", cfg->rad_port);
376 if (cfg->rad_srvip != 0)
377 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
378 tpl_addVar(vars, 0, "USER", cfg->rad_usr);
379
380 struct s_ip *cip;
381 char *dot="";
382 for (cip=cfg->rad_allowed; cip; cip=cip->next) {
383 tpl_printf(vars, 1, "ALLOWED", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
384 if (cip->ip[0] != cip->ip[1])
385 tpl_printf(vars, 1, "ALLOWED", "-%s", cs_inet_ntoa(cip->ip[1]));
386 dot=",";
387 }
388
389 webif_write(tpl_getTpl(vars, "CONFIGRADEGAST"), f);
390}
391
392void send_oscam_config_cccam(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
393 int i;
394 if (strcmp(getParam(params, "action"),"execute") == 0) {
395 for(i = 0; i < (*params).paramcount; ++i) {
396 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
397 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
398 //we use the same function as used for parsing the config tokens
399 chk_t_cccam((*params).params[i], (*params).values[i]);
400 }
401 }
402 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
403 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
404 }
405
406 tpl_printf(vars, 1, "PORT", "%d", cfg->cc_port);
407 tpl_printf(vars, 0, "RESHARE", "%d", cfg->cc_reshare);
408
409 if (!strcmp((char*)cfg->cc_version,"2.0.11")) {
410 tpl_addVar(vars, 0, "VERSIONSELECTED0", "selected");
411 } else if (!strcmp((char*)cfg->cc_version,"2.1.1")) {
412 tpl_addVar(vars, 0, "VERSIONSELECTED1", "selected");
413 } else if (!strcmp((char*)cfg->cc_version,"2.1.2")) {
414 tpl_addVar(vars, 0, "VERSIONSELECTED2", "selected");
415 } else if (!strcmp((char*)cfg->cc_version,"2.1.3")) {
416 tpl_addVar(vars, 0, "VERSIONSELECTED3", "selected");
417 } else if (!strcmp((char*)cfg->cc_version,"2.1.4")) {
418 tpl_addVar(vars, 0, "VERSIONSELECTED4", "selected");
419 } else if (!strcmp((char*)cfg->cc_version,"2.2.0")) {
420 tpl_addVar(vars, 0, "VERSIONSELECTED5", "selected");
421 } else if (!strcmp((char*)cfg->cc_version,"2.2.1")) {
422 tpl_addVar(vars, 0, "VERSIONSELECTED6", "selected");
423 }
424
425 tpl_printf(vars, 0, "UPDATEINTERVAL", "%d", cfg->cc_update_interval);
426
427 tpl_printf(vars, 0, "TMP", "MINIMIZECARDSELECTED%d", cfg->cc_minimize_cards);
428 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
429
430 if (cfg->cc_ignore_reshare)
431 tpl_printf(vars, 0, "IGNORERESHARE", "selected");
432
433 if (cfg->cc_keep_connected)
434 tpl_printf(vars, 0, "KEEPCONNECTED", "selected");
435
436
437 webif_write(tpl_getTpl(vars, "CONFIGCCCAM"), f);
438}
439
440#ifdef CS_WITH_GBOX
441void send_oscam_config_gbox(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
442 int i;
443 if (strcmp(getParam(params, "action"),"execute") == 0) {
444 for(i = 0; i < (*params).paramcount; ++i) {
445 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
446 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
447 //we use the same function as used for parsing the config tokens
448 chk_t_gbox((*params).params[i], (*params).values[i]);
449 }
450 }
451 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Gbox done. You should restart Oscam now.</B><BR><BR>");
452 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
453 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
454 }
455 tpl_printf(vars, 0, "MAXDIST", "%d", cfg->maxdist);
456 for (i=0;i<4;i++) tpl_printf(vars, 1, "PASSWORD", "%02X", cfg->gbox_pwd[i]);
457 tpl_addVar(vars, 0, "IGNORELIST", (char *)cfg->ignorefile);
458 tpl_addVar(vars, 0, "ONLINEINFOS", (char *)cfg->gbxShareOnl);
459 tpl_addVar(vars, 0, "CARDINFOS", (char *)cfg->cardfile);
460 char *dot = "";
461 for (i = 0; i < cfg->num_locals; i++) {
462 tpl_printf(vars, 1, "LOCALS", "%s%06lX", dot, cfg->locals[i]);
463 dot=";";
464 }
465 webif_write(tpl_getTpl(vars, "CONFIGGBOX"), f);
466}
467#endif
468
469void send_oscam_config_monitor(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
470 int i;
471 if (strcmp(getParam(params, "action"),"execute") == 0) {
472
473 //cleanup
474 clear_sip(&cfg->mon_allowed);
475 clear_sip(&cfg->http_allowed);
476
477 for(i = 0; i < (*params).paramcount; ++i) {
478 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
479 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
480
481 //we use the same function as used for parsing the config tokens
482 if (strstr((*params).params[i], "http")) {
483 chk_t_webif((*params).params[i], (*params).values[i]);
484 } else {
485 chk_t_monitor((*params).params[i], (*params).values[i]);
486 }
487 }
488 }
489 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Monitor done. You should restart Oscam now.</B><BR><BR>");
490 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
491 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
492 }
493 tpl_printf(vars, 0, "MONPORT", "%d", cfg->mon_port);
494 if (cfg->mon_srvip != 0)
495 tpl_addVar(vars, 0, "SERVERIP", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
496 tpl_printf(vars, 0, "AULOW", "%d", cfg->mon_aulow);
497 tpl_printf(vars, 0, "HIDECLIENTTO", "%d", cfg->mon_hideclient_to);
498 if(cfg->mon_appendchaninfo)
499 tpl_addVar(vars, 0, "APPENDCHANINFO", "checked");
500
501#ifdef WITH_SSL
502 if(cfg->http_use_ssl)
503 tpl_printf(vars, 0, "HTTPPORT", "+%d", cfg->http_port);
504 else
505 tpl_printf(vars, 0, "HTTPPORT", "%d", cfg->http_port);
506#else
507 tpl_printf(vars, 0, "HTTPPORT", "%d", cfg->http_port);
508#endif
509
510 tpl_addVar(vars, 0, "HTTPUSER", cfg->http_user);
511 tpl_addVar(vars, 0, "HTTPPASSWORD", cfg->http_pwd);
512
513 // css style selector
514 if(strlen(cfg->http_css) == 0) {
515 tpl_addVar(vars, 0, "CSSOPTIONS", "<option value=\"\" selected>embedded</option>\n");
516 } else {
517 tpl_addVar(vars, 0, "CSSOPTIONS", "<option value=\"\">embedded</option>\n");
518 }
519
520 DIR *hdir;
521 struct dirent *entry;
522 hdir = opendir(cs_confdir);
523 do {
524 entry = readdir(hdir);
525 if ((entry) && (strstr(entry->d_name, ".css"))) {
526 if (strstr(cfg->http_css, entry->d_name)) {
527 tpl_printf(vars, 1, "CSSOPTIONS", "<option value=\"%s%s\" selected>%s%s</option>\n",cs_confdir,entry->d_name,cs_confdir,entry->d_name);
528 } else {
529 tpl_printf(vars, 1, "CSSOPTIONS", "<option value=\"%s%s\">%s%s</option>\n",cs_confdir,entry->d_name,cs_confdir,entry->d_name);
530 }
531 }
532 } while (entry);
533 closedir(hdir);
534
535 tpl_printf(vars, 0, "HTTPREFRESH", "%d", cfg->http_refresh);
536 tpl_addVar(vars, 0, "HTTPTPL", cfg->http_tpl);
537 tpl_addVar(vars, 0, "HTTPSCRIPT", cfg->http_script);
538 tpl_addVar(vars, 0, "HTTPJSCRIPT", cfg->http_jscript);
539
540 if (cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
541
542 struct s_ip *cip;
543 char *dot="";
544 for (cip = cfg->mon_allowed; cip; cip = cip->next) {
545 tpl_printf(vars, 1, "NOCRYPT", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
546 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "NOCRYPT", "-%s", cs_inet_ntoa(cip->ip[1]));
547 dot=",";
548 }
549
550 dot="";
551 for (cip = cfg->http_allowed; cip; cip = cip->next) {
552 tpl_printf(vars, 1, "HTTPALLOW", "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
553 if (cip->ip[0] != cip->ip[1]) tpl_printf(vars, 1, "HTTPALLOW", "-%s", cs_inet_ntoa(cip->ip[1]));
554 dot=",";
555 }
556
557 tpl_printf(vars, 0, "HTTPDYNDNS", "%s", cfg->http_dyndns);
558
559 //Monlevel selector
560 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", cfg->mon_level);
561 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
562
563 if (cfg->http_full_cfg)
564 tpl_addVar(vars, 0, "HTTPSAVEFULLSELECT", "selected");
565
566 webif_write(tpl_getTpl(vars, "CONFIGMONITOR"), f);
567}
568
569void send_oscam_config_serial(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
570 int i;
571 if (strcmp(getParam(params, "action"),"execute") == 0) {
572 //cfg->ser_device[0]='\0';
573 memset(cfg->ser_device, 0, sizeof(cfg->ser_device));
574 for(i = 0; i < (*params).paramcount; ++i) {
575 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
576 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
577 //we use the same function as used for parsing the config tokens
578 if((*params).values[i][0])
579 chk_t_serial((*params).params[i], (*params).values[i]);
580 }
581 }
582 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Serial done. You should restart Oscam now.</B><BR><BR>");
583 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
584 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
585 }
586
587 if (cfg->ser_device[0]){
588 char sdevice[512];
589 cs_strncpy(sdevice, cfg->ser_device, sizeof(sdevice));
590 char *ptr;
591 char delimiter[2]; delimiter[0] = 1; delimiter[1] = '\0';
592 ptr = strtok(sdevice, delimiter);
593 while(ptr != NULL) {
594 tpl_printf(vars, 0, "SERIALDEVICE", "%s", ptr);
595 tpl_addVar(vars, 1, "DEVICES", tpl_getTpl(vars, "CONFIGSERIALDEVICEBIT"));
596 ptr = strtok(NULL, delimiter);
597 }
598 }
599
600 tpl_printf(vars, 0, "SERIALDEVICE", "%s", "");
601 tpl_addVar(vars, 1, "DEVICES", tpl_getTpl(vars, "CONFIGSERIALDEVICEBIT"));
602
603 webif_write(tpl_getTpl(vars, "CONFIGSERIAL"), f);
604}
605
606#ifdef HAVE_DVBAPI
607void send_oscam_config_dvbapi(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
608 int i;
609 if (strcmp(getParam(params, "action"),"execute") == 0) {
610 for(i = 0; i < (*params).paramcount; ++i) {
611 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
612 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
613 //we use the same function as used for parsing the config tokens
614 chk_t_dvbapi((*params).params[i], (*params).values[i]);
615 }
616 }
617 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration DVB Api done. You should restart Oscam now.</B><BR><BR>");
618 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
619 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
620 }
621
622 if (cfg->dvbapi_enabled > 0)
623 tpl_addVar(vars, 0, "ENABLEDCHECKED", "checked");
624
625 if (cfg->dvbapi_au > 0)
626 tpl_addVar(vars, 0, "AUCHECKED", "checked");
627
628 tpl_printf(vars, 0, "BOXTYPE", "<option value=\"\"%s>None</option>\n", cfg->dvbapi_boxtype == 0 ? " selected" : "");
629 for (i=1; i<=BOXTYPES; i++) {
630 tpl_printf(vars, 1, "BOXTYPE", "<option%s>%s</option>\n", cfg->dvbapi_boxtype == i ? " selected" : "", boxdesc[i]);
631 }
632
633 if(cfg->dvbapi_usr[0])
634 tpl_addVar(vars, 0, "USER", cfg->dvbapi_usr);
635
636 //PMT Mode
637 tpl_printf(vars, 0, "TMP", "PMTMODESELECTED%d", cfg->dvbapi_pmtmode);
638 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
639
640 webif_write(tpl_getTpl(vars, "CONFIGDVBAPI"), f);
641}
642#endif
643
644#ifdef CS_ANTICASC
645void send_oscam_config_anticasc(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
646 int i;
647 if (strcmp(getParam(params, "action"),"execute") == 0) {
648 for(i = 0; i < (*params).paramcount; ++i) {
649 if ((strcmp((*params).params[i], "part")) && (strcmp((*params).params[i], "action"))) {
650 tpl_printf(vars, 1, "MESSAGE", "Parameter: %s set to Value: %s<BR>\n", (*params).params[i], (*params).values[i]);
651 //we use the same function as used for parsing the config tokens
652 chk_t_ac((*params).params[i], (*params).values[i]);
653 }
654 }
655 tpl_addVar(vars, 1, "MESSAGE", "<BR><BR><B>Configuration Anticascading done. You should restart Oscam now.</B><BR><BR>");
656 refresh_oscam(REFR_ANTICASC, in);
657 if(write_config()==0) refresh_oscam(REFR_SERVER, in);
658 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
659 }
660 if (cfg->ac_enabled > 0) tpl_addVar(vars, 0, "CHECKED", "checked");
661 tpl_printf(vars, 0, "NUMUSERS", "%d", cfg->ac_users);
662 tpl_printf(vars, 0, "SAMPLETIME", "%d", cfg->ac_stime);
663 tpl_printf(vars, 0, "SAMPLES", "%d", cfg->ac_samples);
664 tpl_printf(vars, 0, "PENALTY", "%d", cfg->ac_penalty);
665 tpl_addVar(vars, 0, "ACLOGFILE", cfg->ac_logfile);
666 tpl_printf(vars, 0, "FAKEDELAY", "%d", cfg->ac_fakedelay);
667 tpl_printf(vars, 0, "DENYSAMPLES", "%d", cfg->ac_denysamples);
668 webif_write(tpl_getTpl(vars, "CONFIGANTICASC"), f);
669}
670#endif
671
672void send_oscam_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
673 char *part = getParam(params, "part");
674 if (!strcmp(part,"camd33")) send_oscam_config_camd33(vars, f, params, in);
675 else if (!strcmp(part,"camd35")) send_oscam_config_camd35(vars, f, params, in);
676 else if (!strcmp(part,"camd35tcp")) send_oscam_config_camd35tcp(vars, f, params, in);
677 else if (!strcmp(part,"newcamd")) send_oscam_config_newcamd(vars, f, params, in);
678 else if (!strcmp(part,"radegast")) send_oscam_config_radegast(vars, f, params, in);
679 else if (!strcmp(part,"cccam")) send_oscam_config_cccam(vars, f, params, in);
680#ifdef CS_WITH_GBOX
681 else if (!strcmp(part,"gbox")) send_oscam_config_gbox(vars, f, params, in);
682#endif
683#ifdef HAVE_DVBAPI
684 else if (!strcmp(part,"dvbapi")) send_oscam_config_dvbapi(vars, f, params, in);
685#endif
686#ifdef CS_ANTICASC
687 else if (!strcmp(part,"anticasc")) send_oscam_config_anticasc(vars, f, params, in);
688#endif
689 else if (!strcmp(part,"monitor")) send_oscam_config_monitor(vars, f, params, in);
690 else if (!strcmp(part,"serial")) send_oscam_config_serial(vars, f, params, in);
691 else if (!strcmp(part,"loadbalancer")) send_oscam_config_loadbalancer(vars, f, params, in);
692 else send_oscam_config_global(vars, f, params, in);
693}
694
695void send_oscam_reader(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
696 struct s_reader *rdr;
697 int i;
698 //uchar dummy[1]={0x00};
699
700 if ((strcmp(getParam(params, "action"), "disable") == 0) || (strcmp(getParam(params, "action"), "enable") == 0)) {
701 rdr = get_reader_by_label(getParam(params, "label"));
702 if (strcmp(getParam(params, "action"), "enable") == 0)
703 rdr->enable = 1;
704 else
705 rdr->enable = 0;
706 if(write_server()==0)
707 refresh_oscam(REFR_READERS, in);
708 }
709
710 if (strcmp(getParam(params, "action"), "delete") == 0) {
711 rdr = get_reader_by_label(getParam(params, "label"));
712 rdr->deleted = 1;
713
714 if(write_server()==0) {
715 refresh_oscam(REFR_READERS, in);
716 //printf("would kill now PID %d\n", reader[atoi(getParam(params, "reader"))].pid);
717 //if(reader[atoi(getParam(params, "reader"))].pid)
718 // kill(reader[atoi(getParam(params, "reader"))].pid, SIGQUIT);
719 }
720 else
721 tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
722 }
723
724 if (strcmp(getParam(params, "action"), "reread") == 0) {
725 rdr = get_reader_by_label(getParam(params, "label"));
726
727 //reset the counters
728 for (i = 0; i < 4; i++) {
729 rdr->emmerror[i] = 0;
730 rdr->emmwritten[i] = 0;
731 rdr->emmskipped[i] = 0;
732 rdr->emmblocked[i] = 0;
733 }
734 //write_to_pipe(client[reader[readeridx].cs_idx)].fd_m2c, PIP_ID_CIN, dummy, 1); // do not work for whatever reason
735 refresh_oscam(REFR_READERS, in); // refresh all reader because write pipe seams not work from here
736 }
737
738 for (i = 0, rdr = first_reader; rdr && rdr->label[0]; rdr = rdr->next, i++);
739 tpl_printf(vars, 0, "NEXTREADER", "Reader-%d", i); //Next Readername
740
741 for (rdr = first_reader; rdr ; rdr = rdr->next) {
742 if(rdr->label[0] && rdr->typ && !rdr->deleted) {
743
744 if (rdr->enable)
745 tpl_addVar(vars, 0, "READERCLASS", "enabledreader");
746 else
747 tpl_addVar(vars, 0, "READERCLASS", "disabledreader");
748
749 tpl_addVar(vars, 0, "READERNAME", rdr->label);
750 tpl_addVar(vars, 0, "READERNAMEENC", tpl_addTmp(vars, urlencode(rdr->label)));
751 tpl_printf(vars, 0, "EMMERRORUK", "%d", rdr->emmerror[UNKNOWN]);
752 tpl_printf(vars, 0, "EMMERRORG", "%d", rdr->emmerror[GLOBAL]);
753 tpl_printf(vars, 0, "EMMERRORS", "%d", rdr->emmerror[SHARED]);
754 tpl_printf(vars, 0, "EMMERRORUQ", "%d", rdr->emmerror[UNIQUE]);
755
756 tpl_printf(vars, 0, "EMMWRITTENUK", "%d", rdr->emmwritten[UNKNOWN]);
757 tpl_printf(vars, 0, "EMMWRITTENG", "%d", rdr->emmwritten[GLOBAL]);
758 tpl_printf(vars, 0, "EMMWRITTENS", "%d", rdr->emmwritten[SHARED]);
759 tpl_printf(vars, 0, "EMMWRITTENUQ", "%d", rdr->emmwritten[UNIQUE]);
760
761 tpl_printf(vars, 0, "EMMSKIPPEDUK", "%d", rdr->emmskipped[UNKNOWN]);
762 tpl_printf(vars, 0, "EMMSKIPPEDG", "%d", rdr->emmskipped[GLOBAL]);
763 tpl_printf(vars, 0, "EMMSKIPPEDS", "%d", rdr->emmskipped[SHARED]);
764 tpl_printf(vars, 0, "EMMSKIPPEDUQ", "%d", rdr->emmskipped[UNIQUE]);
765
766 tpl_printf(vars, 0, "EMMBLOCKEDUK", "%d", rdr->emmblocked[UNKNOWN]);
767 tpl_printf(vars, 0, "EMMBLOCKEDG", "%d", rdr->emmblocked[GLOBAL]);
768 tpl_printf(vars, 0, "EMMBLOCKEDS", "%d", rdr->emmblocked[SHARED]);
769 tpl_printf(vars, 0, "EMMBLOCKEDUQ", "%d", rdr->emmblocked[UNIQUE]);
770
771 tpl_addVar(vars, 0, "DELICO", ICDEL);
772
773 //call stats
774 tpl_addVar(vars, 0, "STATICO", ICSTA);
775
776 if (!(rdr->typ & R_IS_NETWORK)) { //reader is physical
777 tpl_addVar(vars, 0, "REFRICO", ICREF);
778 tpl_addVar(vars, 0, "READERREFRESH", tpl_getTpl(vars, "READERREFRESHBIT"));
779
780 tpl_addVar(vars, 0, "ENTICO", ICENT);
781 tpl_addVar(vars, 0, "ENTITLEMENT", tpl_getTpl(vars, "READERENTITLEBIT"));
782
783 } else {
784 tpl_addVar(vars, 0, "READERREFRESH","");
785 if (rdr->typ == R_CCCAM) {
786 tpl_addVar(vars, 0, "ENTICO", ICENT);
787 tpl_addVar(vars, 0, "ENTITLEMENT", tpl_getTpl(vars, "READERENTITLEBIT"));
788 } else {
789 tpl_addVar(vars, 0, "ENTITLEMENT","");
790 }
791
792 }
793
794 if(rdr->enable == 0) {
795 tpl_addVar(vars, 0, "SWITCHICO", ICENA);
796 tpl_addVar(vars, 0, "SWITCHTITLE", "enable this reader");
797 tpl_addVar(vars, 0, "SWITCH", "enable");
798 } else {
799 tpl_addVar(vars, 0, "SWITCHICO", ICDIS);
800 tpl_addVar(vars, 0, "SWITCHTITLE", "disable this reader");
801 tpl_addVar(vars, 0, "SWITCH", "disable");
802 }
803
804 tpl_addVar(vars, 0, "CTYP", reader_get_type_desc(rdr, 1));
805 tpl_addVar(vars, 0, "EDIICO", ICEDI);
806 tpl_addVar(vars, 1, "READERLIST", tpl_getTpl(vars, "READERSBIT"));
807 }
808 }
809
810#ifdef CS_WITH_GBOX
811 tpl_addVar(vars, 0, "ADDPROTOCOL", "<option>gbox</option>");
812#endif
813
814 webif_write(tpl_getTpl(vars, "READERS"), f);
815}
816
817void send_oscam_reader_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
818 int i, ridx=0;
819 char *reader_ = getParam(params, "label");
820 char *value;
821
822 struct s_reader *rdr;
823 for (ridx=0,rdr=first_reader; rdr && rdr->label[0]; rdr=rdr->next, ridx++); //last reader
824
825 if(strcmp(getParam(params, "action"), "Add") == 0) {
826 // Add new reader
827 struct s_reader *newrdr;
828 newrdr = malloc(sizeof(struct s_reader));
829
830 if (newrdr) {
831 memset(newrdr, 0, sizeof(struct s_reader));
832 for (rdr = first_reader; rdr->next ; rdr = rdr->next); // get last rdr
833 rdr->next = newrdr;
834 newrdr->next = NULL; // terminate list
835 newrdr->enable = 0; // do not start the reader because must configured before
836 strcpy(newrdr->pincode, "none");
837 for (i = 1; i < CS_MAXCAIDTAB; newrdr->ctab.mask[i++] = 0xffff);
838 for (i = 0; i < (*params).paramcount; ++i) {
839 if (strcmp((*params).params[i], "action"))
840 chk_reader((*params).params[i], (*params).values[i], newrdr);
841 }
842 reader_ = newrdr->label;
843 }
844
845
846 } else if(strcmp(getParam(params, "action"), "Save") == 0) {
847
848 rdr = get_reader_by_label(getParam(params, "label"));
849 char servicelabels[255]="";
850
851 clear_caidtab(&rdr->ctab);
852 clear_ftab(&rdr->ftab);
853 clear_ftab(&rdr->fchid);
854
855 rdr->grp = 0;
856 rdr->auprovid = 0;
857 for(i = 0; i < (*params).paramcount; ++i) {
858 if ((strcmp((*params).params[i], "reader")) && (strcmp((*params).params[i], "action"))) {
859 if (!strcmp((*params).params[i], "services"))
860 snprintf(servicelabels + strlen(servicelabels), sizeof(servicelabels), "%s,", (*params).values[i]);
861 else
862 if(strlen((*params).values[i]) > 0)
863 chk_reader((*params).params[i], (*params).values[i], rdr);
864 }
865 //printf("param %s value %s\n",(*params).params[i], (*params).values[i]);
866 }
867 chk_reader("services", servicelabels, rdr);
868
869 if(write_server()==0) {
870 refresh_oscam(REFR_READERS, in);
871 /*// fixme: restart_cardreader causes segfaults sometimes
872 if (rdr->typ & R_IS_NETWORK)
873 restart_cardreader(rdr, 1); //physical readers make trouble if re-started */
874 }
875 else
876 tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
877 }
878
879 for (ridx=0,rdr=first_reader; rdr && strcmp(reader_, rdr->label); rdr=rdr->next, ++ridx);
880 tpl_addVar(vars, 0, "READERNAME", rdr->label);
881
882 if(rdr->enable)
883 tpl_addVar(vars, 0, "ENABLED", "checked");
884
885 tpl_printf(vars, 0, "ACCOUNT", "%s", rdr->r_usr);
886
887#ifdef CS_WITH_GBOX
888 if (strlen(rdr->gbox_pwd) > 0)
889 tpl_printf(vars, 0, "PASSWORD", "%s", rdr->gbox_pwd);
890 else if (strlen(rdr->r_pwd) > 0)
891 tpl_printf(vars, 0, "PASSWORD", "%s", rdr->r_pwd);
892 else
893 tpl_printf(vars, 0, "PASSWORD", "%s", "");
894#else
895 tpl_printf(vars, 0, "PASSWORD", "%s", rdr->r_pwd);
896#endif
897
898 for (i=0; i<14; i++)
899 tpl_printf(vars, 1, "NCD_KEY", "%02X", rdr->ncd_key[i]);
900
901 tpl_addVar(vars, 0, "PINCODE", rdr->pincode);
902 //tpl_addVar(vars, 0, "EMMFILE", (char *)rdr->emmfile);
903 tpl_printf(vars, 0, "INACTIVITYTIMEOUT", "%d", rdr->tcp_ito);
904 tpl_printf(vars, 0, "RECEIVETIMEOUT", "%d", rdr->tcp_rto);
905 tpl_printf(vars, 0, "DISABLESERVERFILTER", "%d", rdr->ncd_disable_server_filt);
906
907 if(rdr->fallback)
908 tpl_addVar(vars, 0, "FALLBACKCHECKED", "checked");
909
910 tpl_printf(vars, 0, "LOGPORT", "%d", rdr->log_port);
911
912 if(rdr->boxid)
913 tpl_printf(vars, 0, "BOXID", "%08X", rdr->boxid);
914
915 tpl_addVar(vars, 0, "USER", rdr->r_usr);
916 tpl_addVar(vars, 0, "PASS", rdr->r_pwd);
917
918 if(rdr->audisabled)
919 tpl_addVar(vars, 0, "AUDISABLED", "checked");
920
921 if(rdr->auprovid)
922 tpl_printf(vars, 0, "AUPROVID", "%06lX", rdr->auprovid);
923
924 if(rdr->force_irdeto)
925 tpl_addVar(vars, 0, "FORCEIRDETOCHECKED", "checked");
926
927 //aeskey
928 int has_aeskey = 0;
929 for (i = 0; i < 16 ;i++) {
930 if(rdr->aes_key[i]) {
931 has_aeskey++;
932 }
933 }
934 if (has_aeskey) {
935 for (i = 0; i < 16; i++) tpl_printf(vars, 1, "AESKEY", "%02X", rdr->aes_key[i]);
936 }
937
938 //check for tiger
939 int tigerkey = 0;
940 for (i = 64; i < 120; i++) {
941 if(rdr->rsa_mod[i] > 0) {
942 tigerkey = 1;
943 break;
944 }
945 }
946
947 if(rdr->has_rsa) {
948 if (!tigerkey) {
949 for (i = 0; i < 64; i++) tpl_printf(vars, 1, "RSAKEY", "%02X", rdr->rsa_mod[i]);
950 for (i = 0; i < 8 ; i++) tpl_printf(vars, 1, "BOXKEY", "%02X", rdr->nagra_boxkey[i]);
951 }
952 }
953 if (tigerkey) {
954 for (i = 0; i < 120; i++) tpl_printf(vars, 1, "TIGERRSAKEY", "%02X", rdr->rsa_mod[i]);
955 for (i = 0; i < 8 ; i++) tpl_printf(vars, 1, "BOXKEY", "%02X", rdr->nagra_boxkey[i]);
956 }
957
958 if ( rdr->atr[0])
959 for (i = 0; i < rdr->atrlen/2; i++)
960 tpl_printf(vars, 1, "ATR", "%02X", rdr->atr[i]);
961
962 if(rdr->smargopatch)
963 tpl_addVar(vars, 0, "SMARGOPATCHCHECKED", "checked");
964
965 if (rdr->detect&0x80)
966 tpl_printf(vars, 0, "DETECT", "!%s", RDR_CD_TXT[rdr->detect&0x7f]);
967 else
968 tpl_printf(vars, 0, "DETECT", "%s", RDR_CD_TXT[rdr->detect&0x7f]);
969
970 tpl_printf(vars, 0, "MHZ", "%d", rdr->mhz);
971 tpl_printf(vars, 0, "CARDMHZ", "%d", rdr->cardmhz);
972
973#ifdef CS_WITH_GBOX
974 tpl_addVar(vars, 0, "GBOXPWD", (char *)rdr->gbox_pwd);
975 tpl_addVar(vars, 0, "PREMIUM", rdr->gbox_prem);
976#endif
977
978 tpl_printf(vars, 0, "DEVICE", "%s", rdr->device);
979 if(rdr->r_port)
980 tpl_printf(vars, 1, "DEVICE", ",%d", rdr->r_port);
981 if(rdr->l_port) {
982 if(rdr->r_port)
983 tpl_printf(vars, 1, "DEVICE", ",%d", rdr->l_port);
984 else
985 tpl_printf(vars, 1, "DEVICE", ",,%d", rdr->l_port);
986 }
987
988 //group
989 value = mk_t_group(rdr->grp);
990 tpl_printf(vars, 0, "GRP", "%s", value);
991 free(value);
992
993 if(rdr->lb_weight)
994 tpl_printf(vars, 0, "LBWEIGHT", "%d", rdr->lb_weight);
995
996 //services
997 char sidok[MAX_SIDBITS+1];
998 sidtabbits2bitchar(rdr->sidtabok, sidok);
999 char sidno[MAX_SIDBITS+1];
1000 sidtabbits2bitchar(rdr->sidtabno,sidno);
1001 struct s_sidtab *sidtab = cfg->sidtab;
1002 //build matrix
1003 i = 0;
1004 while(sidtab != NULL) {
1005 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
1006 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
1007 else tpl_addVar(vars, 0, "CHECKED", "");
1008 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDOKBIT"));
1009 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
1010 else tpl_addVar(vars, 0, "CHECKED", "");
1011 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "READERCONFIGSIDNOBIT"));
1012 sidtab=sidtab->next;
1013 i++;
1014 }
1015
1016 // CAID
1017 value = mk_t_caidtab(&rdr->ctab);
1018 tpl_addVar(vars, 0, "CAIDS", value);
1019 free(value);
1020
1021 //ident
1022 value = mk_t_ftab(&rdr->ftab);
1023 tpl_printf(vars, 0, "IDENTS", "%s", value);
1024 free(value);
1025
1026 //CHID
1027 value = mk_t_ftab(&rdr->fchid);
1028 tpl_printf(vars, 0, "CHIDS", "%s", value);
1029 free(value);
1030
1031 //class
1032 CLASSTAB *clstab = &rdr->cltab;
1033 char *dot="";
1034 for(i = 0; i < clstab->an; ++i) {
1035 tpl_printf(vars, 1, "CLASS", "%s%02x", dot, (int)clstab->aclass[i]);
1036 dot=",";
1037 }
1038
1039 for(i = 0; i < clstab->bn; ++i) {
1040 tpl_printf(vars, 0, "CLASS", "%s!%02x", dot, (int)clstab->bclass[i]);
1041 dot=",";
1042 }
1043
1044 tpl_printf(vars, 0, "SHOWCLS", "%d", rdr->show_cls);
1045 tpl_printf(vars, 0, "MAXQLEN", "%d", rdr->maxqlen);
1046
1047 if(rdr->cachemm)
1048 tpl_printf(vars, 0, "EMMCACHE", "%d,%d,%d", rdr->cachemm, rdr->rewritemm, rdr->logemm);
1049
1050 //savenano
1051 int all = 1;
1052 dot="";
1053 for(i = 0; i < 256; ++i) {
1054 if(!(rdr->b_nano[i] & 0x02)) {
1055 all = 0;
1056 break;
1057 }
1058 }
1059 if (all == 1) tpl_addVar(vars, 0, "SAVENANO", "all");
1060 else {
1061 for(i = 0; i < 256; ++i) {
1062 if(rdr->b_nano[i] & 0x02) tpl_printf(vars, 1, "SAVENANO", "%s%02x\n", dot, i);
1063 dot=",";
1064 }
1065 }
1066
1067 //blocknano
1068 dot="";
1069 for(i = 0; i < 256; ++i) {
1070 if(!(rdr->b_nano[i] & 0x01)) {
1071 all = 0;
1072 break;
1073 }
1074 }
1075 if (all == 1) tpl_addVar(vars, 0, "BLOCKNANO", "all");
1076 else {
1077 for(i = 0; i < 256; ++i) {
1078 if(rdr->b_nano[i] & 0x01) tpl_printf(vars, 1, "BLOCKNANO", "%s%02x\n", dot, i);
1079 dot=",";
1080 }
1081 }
1082
1083 if (rdr->blockemm_unknown)
1084 tpl_addVar(vars, 0, "BLOCKEMMUNKNOWNCHK", "checked");
1085 if (rdr->blockemm_u)
1086 tpl_addVar(vars, 0, "BLOCKEMMUNIQCHK", "checked");
1087 if (rdr->blockemm_s)
1088 tpl_addVar(vars, 0, "BLOCKEMMSHAREDCHK", "checked");
1089 if (rdr->blockemm_g)
1090 tpl_addVar(vars, 0, "BLOCKEMMGLOBALCHK", "checked");
1091
1092 if (rdr->deprecated)
1093 tpl_addVar(vars, 0, "DEPRECATEDCHCHECKED", "checked");
1094
1095 if (!strcmp(rdr->cc_version, "2.0.11")) {
1096 tpl_addVar(vars, 0, "CCCVERSIONSELECTED0", "selected");
1097 } else if (!strcmp(rdr->cc_version, "2.1.1")) {
1098 tpl_addVar(vars, 0, "CCCVERSIONSELECTED1", "selected");
1099 } else if (!strcmp(rdr->cc_version, "2.1.2")) {
1100 tpl_addVar(vars, 0, "CCCVERSIONSELECTED2", "selected");
1101 } else if (!strcmp(rdr->cc_version, "2.1.3")) {
1102 tpl_addVar(vars, 0, "CCCVERSIONSELECTED3", "selected");
1103 } else if (!strcmp(rdr->cc_version, "2.1.4")) {
1104 tpl_addVar(vars, 0, "CCCVERSIONSELECTED4", "selected");
1105 } else if (!strcmp(rdr->cc_version, "2.2.0")) {
1106 tpl_addVar(vars, 0, "CCCVERSIONSELECTED5", "selected");
1107 } else if (!strcmp(rdr->cc_version, "2.2.1")) {
1108 tpl_addVar(vars, 0, "CCCVERSIONSELECTED6", "selected");
1109 }
1110
1111#ifdef LIBUSB
1112 tpl_addVar(vars, 0, "DEVICEEP", tpl_getTpl(vars, "READERCONFIGDEVICEEPBIT"));
1113
1114 if(!rdr->device_endpoint) {
1115 tpl_addVar(vars, 0, "DEVICEOUTEP0", "selected");
1116 } else if (rdr->device_endpoint == 0x82) {
1117 tpl_addVar(vars, 0, "DEVICEOUTEP1", "selected");
1118 } else if (rdr->device_endpoint == 0x81) {
1119 tpl_addVar(vars, 0, "DEVICEOUTEP2", "selected");
1120 }
1121#else
1122 tpl_addVar(vars, 0, "DEVICEEP", "not avail LIBUSB");
1123#endif
1124
1125 tpl_printf(vars, 0, "TMP", "NDSVERSION%d", rdr->ndsversion);
1126 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
1127
1128 tpl_printf(vars, 0, "TMP", "NAGRAREAD%d", rdr->nagra_read);
1129 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
1130
1131 tpl_printf(vars, 0, "CCCMAXHOP", "%d", rdr->cc_maxhop);
1132 if(rdr->cc_want_emu)
1133 tpl_addVar(vars, 0, "CCCWANTEMUCHECKED", "checked");
1134
1135 if(rdr->cc_keepalive)
1136 tpl_addVar(vars, 0, "KEEPALIVECHECKED", "selected");
1137
1138 if(rdr->cc_reshare)
1139 tpl_printf(vars, 0, "RESHARE", "%d", rdr->cc_reshare);
1140
1141 // Show only parameters which needed for the reader
1142 switch (rdr->typ) {
1143 case R_CONSTCW:
1144 tpl_addVar(vars, 0, "PROTOCOL", "constcw");
1145 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1146 break;
1147 case R_DB2COM1:
1148 case R_DB2COM2:
1149 case R_MOUSE :
1150 tpl_addVar(vars, 0, "PROTOCOL", "mouse");
1151 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1152 break;
1153 case R_MP35:
1154 tpl_addVar(vars, 0, "PROTOCOL", "mp35");
1155 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1156 break;
1157 case R_SC8in1 :
1158 tpl_addVar(vars, 0, "PROTOCOL", "sc8in1");
1159 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1160 break;
1161 case R_SMART :
1162 tpl_addVar(vars, 0, "PROTOCOL", "smartreader");
1163 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1164 break;
1165 case R_INTERNAL:
1166 tpl_addVar(vars, 0, "PROTOCOL", "internal");
1167 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1168 break;
1169 case R_SERIAL :
1170 tpl_addVar(vars, 0, "PROTOCOL", "serial");
1171 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1172 break;
1173 case R_CAMD35 :
1174 tpl_addVar(vars, 0, "PROTOCOL", "camd35");
1175 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCAMD35BIT"));
1176 break;
1177 case R_CS378X :
1178 tpl_addVar(vars, 0, "PROTOCOL", "cs378x");
1179 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCS378XBIT"));
1180 break;
1181 case R_RADEGAST:
1182 tpl_addVar(vars, 0, "PROTOCOL", "radegast");
1183 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGRADEGASTBIT"));
1184 break;
1185 case R_NEWCAMD :
1186 if ( rdr->ncd_proto == NCD_525 ){
1187 tpl_addVar(vars, 0, "PROTOCOL", "newcamd525");
1188 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGNCD525BIT"));
1189 } else if ( rdr->ncd_proto == NCD_524 ) {
1190 tpl_addVar(vars, 0, "PROTOCOL", "newcamd524");
1191 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGNCD524BIT"));
1192 }
1193 break;
1194 case R_CCCAM :
1195 tpl_addVar(vars, 0, "PROTOCOL", "cccam");
1196 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGCCCAMBIT"));
1197 break;
1198#ifdef CS_WITH_GBOX
1199 case R_GBOX :
1200 tpl_addVar(vars, 0, "PROTOCOL", "gbox");
1201 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGGBOXBIT"));
1202 break;
1203#endif
1204#ifdef HAVE_PCSC
1205 case R_PCSC :
1206 tpl_addVar(vars, 0, "PROTOCOL", "pcsc");
1207 tpl_addVar(vars, 1, "READERDEPENDINGCONFIG", tpl_getTpl(vars, "READERCONFIGSTDHWREADERBIT"));
1208 break;
1209#endif
1210 default :
1211 tpl_addVar(vars, 1, "MESSAGE", "<b>Error: protocol not resolvable</b><BR>");
1212 tpl_printf(vars, 1, "MESSAGE", "<b>Error: protocol number: %d readername: %s readeridx: %d</b><BR>", rdr->typ, rdr->label, ridx);
1213 break;
1214
1215 }
1216 //READERCONFIGMOUSEBIT
1217 webif_write(tpl_getTpl(vars, "READERCONFIG"), f);
1218}
1219
1220void send_oscam_reader_stats(struct templatevars *vars, FILE *f, struct uriparams *params) {
1221
1222 struct s_reader *rdr = get_reader_by_label(getParam(params, "label"));
1223
1224 tpl_printf(vars, 0, "LABEL", "%s", rdr->label);
1225
1226 char *stxt[]={"found", "cache1", "cache2", "emu",
1227 "not found", "timeout", "sleeping",
1228 "fake", "invalid", "corrupt", "no card", "expdate",
1229 "disabled", "stopped"};
1230
1231 int rc2hide = (-1);
1232 if (strlen(getParam(params, "hide")) > 0)
1233 rc2hide = atoi(getParam(params, "hide"));
1234
1235 if (rdr->lb_stat) {
1236
1237 LL_ITER *it = ll_iter_create(rdr->lb_stat);
1238 READER_STAT *stat = ll_iter_next(it);
1239 while (stat) {
1240
1241 if (!(stat->rc == rc2hide)) {
1242 tpl_printf(vars, 0, "CHANNEL", "%04X:%06lX:%04X", stat->caid, stat->prid, stat->srvid);
1243 tpl_printf(vars, 0, "CHANNELNAME","%s", get_servicename(stat->srvid, stat->caid));
1244 tpl_printf(vars, 0, "RC", "%s", stxt[stat->rc]);
1245 tpl_printf(vars, 0, "TIME", "%dms", stat->time_avg);
1246 tpl_printf(vars, 0, "COUNT", "%d", stat->ecm_count);
1247 if(stat->last_received) {
1248 struct tm *lt = localtime(&stat->last_received);
1249 tpl_printf(vars, 0, "LAST", "%02d.%02d.%02d %02d:%02d:%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100, lt->tm_hour, lt->tm_min, lt->tm_sec);
1250
1251 } else {
1252 tpl_addVar(vars, 0, "LAST","never");
1253 }
1254 if(stat->rc == 4) {
1255 tpl_addVar(vars, 1, "READERSTATSROWNOTFOUND", tpl_getTpl(vars, "READERSTATSBIT"));
1256 tpl_addVar(vars, 0, "READERSTATSNFHEADLINE", "<TR><TD CLASS=\"subheadline\" colspan=\"6\">Not found</TD></TR>\n");
1257 }
1258 else
1259 tpl_addVar(vars, 1, "READERSTATSROWFOUND", tpl_getTpl(vars, "READERSTATSBIT"));
1260 }
1261
1262 stat = ll_iter_next(it);
1263 }
1264
1265 ll_iter_release(it);
1266
1267 } else {
1268 tpl_addVar(vars, 1, "READERSTATSROW","<TR><TD colspan=\"6\"> No statistics found </TD></TR>");
1269 }
1270
1271 webif_write(tpl_getTpl(vars, "READERSTATS"), f);
1272}
1273
1274void send_oscam_user_config_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1275 struct s_auth *account, *ptr;
1276 char user[128];
1277
1278 if (strcmp(getParam(params, "action"), "Save As") == 0) cs_strncpy(user, getParam(params, "newuser"), sizeof(user)/sizeof(char));
1279 else cs_strncpy(user, getParam(params, "user"), sizeof(user)/sizeof(char));
1280
1281 int i;
1282
1283 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
1284
1285 // Create a new user if it doesn't yet
1286 if (account == NULL) {
1287 i = 1;
1288 while(strlen(user) < 1) {
1289 snprintf(user, sizeof(user)/sizeof(char) - 1, "NEWUSER%d", i);
1290 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
1291 if(account != NULL) user[0] = '\0';
1292 ++i;
1293 }
1294 if (!(account=malloc(sizeof(struct s_auth)))) {
1295 cs_log("Error allocating memory (errno=%d)", errno);
1296 return;
1297 }
1298 if(cfg->account == NULL) cfg->account = account;
1299 else {
1300 for (ptr = cfg->account; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
1301 ptr->next = account;
1302 }
1303 memset(account, 0, sizeof(struct s_auth));
1304 cs_strncpy((char *)account->usr, user, sizeof(account->usr));
1305 account->aureader=NULL;
1306 account->monlvl=cfg->mon_level;
1307 account->tosleep=cfg->tosleep;
1308 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
1309 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
1310 account->expirationdate=(time_t)NULL;
1311#ifdef CS_ANTICASC
1312 account->ac_users=cfg->ac_users;
1313 account->ac_penalty=cfg->ac_penalty;
1314 account->ac_idx = account->ac_idx + 1;
1315#endif
1316 tpl_addVar(vars, 1, "MESSAGE", "<b>New user has been added with default settings</b><BR>");
1317
1318 if (write_userdb(cfg->account)==0)
1319 refresh_oscam(REFR_ACCOUNTS, in);
1320 else
1321 tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
1322 // need to reget account as writing to disk changes account!
1323 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
1324 }
1325
1326 if((strcmp(getParam(params, "action"), "Save") == 0) || (strcmp(getParam(params, "action"), "Save As") == 0)) {
1327 char servicelabels[255]="";
1328 //clear group
1329 account->grp = 0;
1330 //clear caidtab before it re-readed by chk_t
1331 clear_caidtab(&account->ctab);
1332 //clear Betatunnel before it re-readed by chk_t
1333 clear_tuntab(&account->ttab);
1334 //clear ident before it re-readed by chk_t
1335 clear_ftab(&account->ftab);
1336 //clear CHID before it re-readed by chk_t
1337 clear_ftab(&account->fchid);
1338
1339 for(i=0;i<(*params).paramcount;i++) {
1340 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "user")) && (strcmp((*params).params[i], "newuser"))) {
1341 if (!strcmp((*params).params[i], "expdate"))
1342 account->expirationdate=(time_t)NULL;
1343 if (!strcmp((*params).params[i], "services"))
1344 snprintf(servicelabels + strlen(servicelabels), sizeof(servicelabels), "%s,", (*params).values[i]);
1345 else
1346 chk_account((*params).params[i], (*params).values[i], account);
1347 }
1348 }
1349 chk_account("services", servicelabels, account);
1350 tpl_addVar(vars, 1, "MESSAGE", "<B>Settings updated</B><BR><BR>");
1351
1352 if (write_userdb(cfg->account)==0)
1353 refresh_oscam(REFR_ACCOUNTS, in);
1354 else
1355 tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
1356 }
1357 for (account = cfg->account; account != NULL && strcmp(user, account->usr) != 0; account = account->next);
1358
1359 tpl_addVar(vars, 0, "USERNAME", account->usr);
1360 tpl_addVar(vars, 0, "PASSWORD", account->pwd);
1361
1362 //Disabled
1363 if(account->disabled)
1364 tpl_addVar(vars, 0, "DISABLEDCHECKED", "selected");
1365
1366 //Expirationdate
1367 struct tm * timeinfo = localtime (&account->expirationdate);
1368 char buf [80];
1369 strftime (buf,80,"%Y-%m-%d",timeinfo);
1370 if(strcmp(buf,"1970-01-01")) tpl_addVar(vars, 0, "EXPDATE", buf);
1371
1372 if(account->allowedtimeframe[0] && account->allowedtimeframe[1]) {
1373 tpl_printf(vars, 0, "ALLOWEDTIMEFRAME", "%02d:%02d-%02d:%02d",
1374 account->allowedtimeframe[0]/60,
1375 account->allowedtimeframe[0]%60,
1376 account->allowedtimeframe[1]/60,
1377 account->allowedtimeframe[1]%60 );
1378 }
1379
1380 //Group
1381 char *value = mk_t_group(account->grp);
1382 tpl_addVar(vars, 0, "GROUPS", value);
1383 free(value);
1384
1385 //Hostname
1386 tpl_addVar(vars, 0, "DYNDNS", (char *)account->dyndns);
1387
1388 //Uniq
1389 tpl_printf(vars, 0, "TMP", "UNIQSELECTED%d", account->uniq);
1390 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
1391
1392 //Sleep
1393 if(!account->tosleep) tpl_addVar(vars, 0, "SLEEP", "0");
1394 else tpl_printf(vars, 0, "SLEEP", "%d", account->tosleep);
1395 //Monlevel selector
1396 tpl_printf(vars, 0, "TMP", "MONSELECTED%d", account->monlvl);
1397 tpl_addVar(vars, 0, tpl_getVar(vars, "TMP"), "selected");
1398
1399 //AU Selector
1400 if (!account->aureader) tpl_addVar(vars, 0, "AUSELECTED", "selected");
1401 if (account->autoau == 1) tpl_addVar(vars, 0, "AUTOAUSELECTED", "selected");
1402 struct s_reader *rdr;
1403 for (rdr=first_reader; rdr ; rdr=rdr->next) {
1404 if(!rdr->device[0]) break;
1405 tpl_addVar(vars, 0, "READERNAME", rdr->label);
1406 if (account->aureader == rdr) tpl_addVar(vars, 0, "SELECTED", "selected");
1407 else tpl_addVar(vars, 0, "SELECTED", "");
1408 tpl_addVar(vars, 1, "RDROPTION", tpl_getTpl(vars, "USEREDITRDRSELECTED"));
1409 }
1410
1411 /* SERVICES */
1412 //services - first we have to move the long sidtabok/sidtabno to a binary array
1413 char sidok[MAX_SIDBITS+1];
1414 sidtabbits2bitchar(account->sidtabok,sidok);
1415 char sidno[MAX_SIDBITS+1];
1416 sidtabbits2bitchar(account->sidtabno,sidno);
1417 struct s_sidtab *sidtab = cfg->sidtab;
1418 //build matrix
1419 i=0;
1420 while(sidtab != NULL) {
1421 tpl_addVar(vars, 0, "SIDLABEL", sidtab->label);
1422 if(sidok[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
1423 else tpl_addVar(vars, 0, "CHECKED", "");
1424 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDOKBIT"));
1425 if(sidno[i]=='1') tpl_addVar(vars, 0, "CHECKED", "checked");
1426 else tpl_addVar(vars, 0, "CHECKED", "");
1427 tpl_addVar(vars, 1, "SIDS", tpl_getTpl(vars, "USEREDITSIDNOBIT"));
1428 sidtab=sidtab->next;
1429 i++;
1430 }
1431
1432 // CAID
1433 value = mk_t_caidtab(&account->ctab);
1434 tpl_addVar(vars, 0, "CAIDS", value);
1435 free(value);
1436
1437 //ident
1438 value = mk_t_ftab(&account->ftab);
1439 tpl_printf(vars, 0, "IDENTS", "%s", value);
1440 free(value);
1441
1442 //CHID
1443 value = mk_t_ftab(&account->fchid);
1444 tpl_printf(vars, 0, "CHIDS", "%s", value);
1445 free(value);
1446
1447 //Betatunnel
1448 value = mk_t_tuntab(&account->ttab);
1449 tpl_addVar(vars, 0, "BETATUNNELS", value);
1450 free(value);
1451
1452 //SUPPRESSCMD08
1453 if (account->c35_suppresscmd08)
1454 tpl_addVar(vars, 0, "SUPPRESSCMD08", "selected");
1455
1456 //Sleepsend
1457 tpl_printf(vars, 0, "SLEEPSEND", "%d", account->c35_sleepsend);
1458
1459 //Keepalive
1460 if (account->ncd_keepalive)
1461 tpl_addVar(vars, 0, "KEEPALIVE", "selected");
1462
1463#ifdef CS_ANTICASC
1464 tpl_printf(vars, 0, "AC_USERS", "%d", account->ac_users);
1465 tpl_printf(vars, 0, "AC_PENALTY", "%d", account->ac_penalty);
1466#endif
1467
1468 tpl_printf(vars, 0, "CCCMAXHOPS", "%d", account->cccmaxhops);
1469 tpl_printf(vars, 0, "CCCRESHARE", "%d", account->cccreshare);
1470
1471 //Failban
1472 tpl_printf(vars, 0, "FAILBAN", "%d", account->failban);
1473
1474 webif_write(tpl_getTpl(vars, "USEREDIT"), f);
1475}
1476
1477void send_oscam_user_config(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
1478 struct s_auth *account, *account2;
1479 char *user = getParam(params, "user");
1480 int found = 0;
1481 int hideclient = 10;
1482
1483 if (cfg->mon_hideclient_to > 10)
1484 hideclient = cfg->mon_hideclient_to;
1485
1486 if (strcmp(getParam(params, "action"), "reinit") == 0) {
1487 if(!cfg->http_readonly)
1488 refresh_oscam(REFR_ACCOUNTS, in);
1489 }
1490
1491 if (strcmp(getParam(params, "action"), "delete") == 0) {
1492 if(cfg->http_readonly) {
1493 tpl_addVar(vars, 1, "MESSAGE", "<b>Webif is in readonly mode. No deletion will be made!</b><BR>");
1494 } else {
1495 account = cfg->account;
1496 if(strcmp(account->usr, user) == 0) {
1497 cfg->account = account->next;
1498 free(account);
1499 found = 1;
1500 } else if (account->next != NULL) {
1501 do {
1502 if(strcmp(account->next->usr, user) == 0) {
1503 account2 = account->next;
1504 account->next = account2->next;
1505 free(account2);
1506 found = 1;
1507 break;
1508 }
1509 }while ((account = account->next) && (account->next != NULL));
1510 }
1511
1512 if (found > 0) {
1513 tpl_addVar(vars, 1, "MESSAGE", "<b>Account has been deleted!</b><BR>");
1514
1515 if (write_userdb(cfg->account)==0)
1516 refresh_oscam(REFR_ACCOUNTS, in);
1517 else
1518 tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
1519
1520 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified user doesn't exist. No deletion will be made!</b><BR>");
1521 }
1522 }
1523
1524
1525
1526 if ((strcmp(getParam(params, "action"), "disable") == 0) || (strcmp(getParam(params, "action"), "enable") == 0)) {
1527 for (account=cfg->account; (account); account=account->next) {
1528 if(strcmp(getParam(params, "user"), account->usr) == 0) {
1529 if(strcmp(getParam(params, "action"), "disable") == 0)
1530 account->disabled = 1;
1531 else
1532 account->disabled = 0;
1533 found = 1;
1534 }
1535 }
1536
1537 if (found > 0) {
1538 tpl_addVar(vars, 1, "MESSAGE", "<b>Account has been switched!</b><BR>");
1539 if (write_userdb(cfg->account)==0)
1540 refresh_oscam(REFR_ACCOUNTS, in);
1541 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified user doesn't exist. No deletion will be made!</b><BR>");
1542 }
1543
1544 /* List accounts*/
1545 char *status = "offline";
1546 char *expired = "";
1547 char *classname="offline";
1548 char *lastchan="&nbsp;";
1549 time_t now = time((time_t)0);
1550 int isec = 0, isonline = 0;
1551
1552 //for (account=cfg->account; (account); account=account->next) {
1553 for (account=cfg->account; (account); account=account->next) {
1554 //clear for next client
1555 expired = ""; classname = "offline"; status = "offline";
1556 isonline = 0;
1557 tpl_addVar(vars, 0, "CWOK", "");
1558 tpl_addVar(vars, 0, "CWNOK", "");
1559 tpl_addVar(vars, 0, "CWIGN", "");
1560 tpl_addVar(vars, 0, "CWTOUT", "");
1561 tpl_addVar(vars, 0, "CWCACHE", "");
1562 tpl_addVar(vars, 0, "CWTUN", "");
1563 tpl_addVar(vars, 0, "CLIENTPROTO","");
1564 tpl_addVar(vars, 0, "IDLESECS","");
1565 tpl_addVar(vars, 0, "CWLASTRESPONSET","");
1566 tpl_addVar(vars, 0, "EMMOK","");
1567 tpl_addVar(vars, 0, "EMMNOK","");
1568 tpl_addVar(vars, 0, "CLIENTPROTO","");
1569 tpl_addVar(vars, 0, "LASTCHANNEL", "");
1570
1571 if(account->expirationdate && account->expirationdate<time(NULL)) {
1572 expired = " (expired)";
1573 classname = "expired";
1574 }
1575 if(account->disabled != 0) {
1576 expired = " (disabled)";
1577 classname = "disabled";
1578 tpl_addVar(vars, 0, "SWITCHICO", ICENA);
1579 tpl_addVar(vars, 0, "SWITCHTITLE", "enable this account");
1580 tpl_addVar(vars, 0, "SWITCH", "enable");
1581 } else {
1582 tpl_addVar(vars, 0, "SWITCHICO", ICDIS);
1583 tpl_addVar(vars, 0, "SWITCHTITLE", "disable this account");
1584 tpl_addVar(vars, 0, "SWITCH", "disable");
1585 }
1586
1587 //search account in active clients
1588 int cwok = 0, cwnok = 0, cwign = 0, cwtout = 0, cwcache = 0, cwtun = 0, emmok = 0, emmnok = 0;
1589 int secs = 0, fullmins =0, mins =0, hours =0, lastresponsetm = 0;
1590 char *proto = "";
1591
1592 struct s_client *cl;
1593 for (cl=first_client; cl ; cl=cl->next) {
1594 if (!strcmp(cl->usr, account->usr)) {
1595 //set client to offline depending on hideclient_to
1596 if ((now - cl->lastecm) < hideclient) {
1597 status = "<b>online</b>"; classname="online";
1598 isonline = 1;
1599 proto = monitor_get_proto(cl);
1600 lastchan = get_servicename(cl->last_srvid, cl->last_caid);
1601 lastresponsetm = cl->cwlastresptime;
1602 isec = now - cl->last;
1603 if(isec > 0) {
1604 secs = isec % 60;
1605 if (isec > 60) {
1606 fullmins = isec / 60;
1607 mins = fullmins % 60;
1608 if(fullmins > 60) hours = fullmins / 60;
1609 }
1610 }
1611 }
1612
1613 cwok += cl->cwfound;
1614 cwnok += cl->cwnot;
1615 cwign += cl->cwignored;
1616 cwtout += cl->cwtout;
1617 cwcache += cl->cwcache;
1618 cwtun += cl->cwtun;
1619 emmok += cl->emmok;
1620 emmnok += cl->emmnok;
1621 }
1622 }
1623 if ( isonline > 0 || ((isonline == 0) && (!cfg->http_hide_idle_clients))) {
1624 tpl_printf(vars, 0, "CWOK", "%d", cwok);
1625 tpl_printf(vars, 0, "CWNOK", "%d", cwnok);
1626 tpl_printf(vars, 0, "CWIGN", "%d", cwign);
1627 tpl_printf(vars, 0, "CWTOUT", "%d", cwtout);
1628 tpl_printf(vars, 0, "CWCACHE", "%d", cwcache);
1629 tpl_printf(vars, 0, "CWTUN", "%d", cwtun);
1630 tpl_printf(vars, 0, "EMMOK", "%d", emmok);
1631 tpl_printf(vars, 0, "EMMNOK", "%d", emmnok);
1632 tpl_addVar(vars, 0, "LASTCHANNEL", lastchan);
1633 tpl_printf(vars, 0, "CWLASTRESPONSET", "%d", lastresponsetm);
1634 tpl_addVar(vars, 0, "CLIENTPROTO", proto);
1635 tpl_printf(vars, 0, "IDLESECS", "%02d:%02d:%02d", hours, mins, secs);
1636
1637 }
1638
1639 tpl_addVar(vars, 0, "CLASSNAME", classname);
1640 tpl_addVar(vars, 0, "USER", account->usr);
1641 tpl_addVar(vars, 0, "USERENC", tpl_addTmp(vars, urlencode(account->usr)));
1642 tpl_addVar(vars, 0, "STATUS", status);
1643 tpl_addVar(vars, 0, "EXPIRED", expired);
1644 tpl_addVar(vars, 0, "DELICO", ICDEL);
1645 tpl_addVar(vars, 0, "EDIICO", ICEDI);
1646
1647 tpl_addVar(vars, 1, "USERCONFIGS", tpl_getTpl(vars, "USERCONFIGLISTBIT"));
1648 isec = 0;
1649 lastchan = "&nbsp;";
1650 }
1651
1652 if ((strcmp(getParam(params, "part"), "adduser") == 0) && (!cfg->http_readonly)) {
1653 tpl_addVar(vars, 1, "NEWUSERFORM", tpl_getTpl(vars, "ADDNEWUSER"));
1654 } else {
1655 if(cfg->http_refresh > 0) {
1656 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
1657 tpl_addVar(vars, 0, "REFRESHURL", "userconfig.html");
1658 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
1659 }
1660 }
1661 tpl_printf(vars, 1, "TOTAL_CWOK", "%ld", first_client->cwfound);
1662 tpl_printf(vars, 1, "TOTAL_CWNOK", "%ld", first_client->cwnot);
1663 tpl_printf(vars, 1, "TOTAL_CWIGN", "%ld", first_client->cwignored);
1664 tpl_printf(vars, 1, "TOTAL_CWTOUT", "%ld", first_client->cwtout);
1665 tpl_printf(vars, 1, "TOTAL_CWCACHE", "%ld", first_client->cwcache);
1666 tpl_printf(vars, 1, "TOTAL_CWTUN", "%ld", first_client->cwtun);
1667
1668 webif_write(tpl_getTpl(vars, "USERCONFIGLIST"), f);
1669}
1670
1671char *strend(char *ch) {
1672 while (*ch) ch++;
1673 return ch;
1674}
1675
1676void send_oscam_entitlement(struct templatevars *vars, FILE *f, struct uriparams *params) {
1677 /* build entitlements from reader init history */
1678 char *reader_ = getParam(params, "label");
1679
1680 if (cfg->saveinithistory && strlen(reader_) > 0) {
1681 struct s_reader *rdr = get_reader_by_label(getParam(params, "label"));
1682
1683 if (rdr->typ == R_CCCAM && rdr->enable == 1) {
1684
1685 tpl_addVar(vars, 0, "READERNAME", rdr->label);
1686
1687 int caidcount = 0;
1688 char *provider = "";
1689
1690 struct cc_card *card;
1691 struct s_client *rc = rdr->client;
1692 struct cc_data *rcc = (rc)?rc->cc:NULL;
1693
1694 if (rcc && rcc->cards) {
1695 pthread_mutex_lock(&rcc->cards_busy);
1696 char *buf = malloc(4000);
1697 uint8 serbuf[8];
1698
1699 LL_ITER *it = ll_iter_create(rcc->cards);
1700 while ((card = ll_iter_next(it))) {
1701
1702 tpl_printf(vars, 0, "HOST", "%s:%d", rdr->device, rdr->r_port);
1703 if (cc_UA_valid(card->hexserial)) { //Add UA:
1704 cc_UA_cccam2oscam(card->hexserial, serbuf, card->caid);
1705 tpl_printf(vars, 1, "HOST", "<BR>\nUA_Oscam:%s", cs_hexdump(0, serbuf, 8));
1706 tpl_printf(vars, 1, "HOST", "<BR>\nUA_CCcam:%s", cs_hexdump(0, card->hexserial, 8));
1707 }
1708 tpl_printf(vars, 0, "CAID", "%04X", card->caid);
1709
1710 int cs = get_cardsystem(card->caid);
1711
1712 if (cs)
1713 tpl_printf(vars, 0, "SYSTEM", "%s", cardsystem[cs-1].desc);
1714 else
1715 tpl_printf(vars, 0, "SYSTEM", "???");
1716
1717 tpl_printf(vars, 0, "SHAREID", "%08X", card->id);
1718 tpl_printf(vars, 0, "REMOTEID", "%08X", card->remote_id);
1719 tpl_printf(vars, 0, "UPHOPS", "%d", card->hop);
1720 tpl_printf(vars, 0, "MAXDOWN", "%d", card->maxdown);
1721
1722 LL_ITER *pit = ll_iter_create(card->providers);
1723 char *p = buf;
1724 *p = 0;
1725 struct cc_provider *prov;
1726 while ((prov = ll_iter_next(pit))) {
1727 provider = get_provider(card->caid, prov->prov);
1728 sprintf(p, "%s", provider);
1729 p = strend(p);
1730 //add SA:
1731 if (prov->sa[0] || prov->sa[1] || prov->sa[2] || prov->sa[3]) {
1732 sprintf(p, " SA:%02X%02X%02X%02X",
1733 prov->sa[0], prov->sa[1], prov->sa[2], prov->sa[3]);
1734 p = strend(p);
1735 }
1736 sprintf(p, "<BR>\n");
1737 p = strend(p);
1738 }
1739
1740 tpl_printf(vars, 0, "PROVIDERS", buf);
1741
1742 ll_iter_release(pit);
1743 LL_ITER *nit = ll_iter_create(card->remote_nodes);
1744 p = buf;
1745 *p = 0;
1746 uint8 *node;
1747 while ((node = ll_iter_next(nit))) {
1748 sprintf(p, "%02X%02X%02X%02X%02X%02X%02X%02X<BR>\n",
1749 node[0], node[1], node[2], node[3], node[4], node[5], node[6], node[7]);
1750 p = strend(p);
1751 }
1752 tpl_printf(vars, 0, "NODES", buf);
1753
1754 ll_iter_release(nit);
1755
1756 tpl_addVar(vars, 1, "CCCAMSTATSENTRY", tpl_getTpl(vars, "ENTITLEMENTCCCAMENTRYBIT"));
1757 caidcount++;
1758 }
1759 ll_iter_release(it);
1760 free(buf);
1761 pthread_mutex_unlock(&rcc->cards_busy);
1762
1763 tpl_printf(vars, 0, "TOTALS", "card count=%d", caidcount);
1764 tpl_addVar(vars, 0, "ENTITLEMENTCONTENT", tpl_getTpl(vars, "ENTITLEMENTCCCAMBIT"));
1765
1766 } else {
1767 tpl_addVar(vars, 0, "ENTITLEMENTCONTENT", tpl_getTpl(vars, "ENTITLEMENTGENERICBIT"));
1768 tpl_printf(vars, 0, "LOGHISTORY", "no cardfile found<BR>\n");
1769 }
1770
1771 } else {
1772 tpl_addVar(vars, 0, "LOGHISTORY", "->");
1773 // normal non-cccam reader
1774 FILE *fp;
1775 char filename[256];
1776 char buffer[128];
1777
1778 int ridx;
1779 for (ridx=0,rdr=first_reader; rdr && strcmp(reader_, rdr->label); rdr=rdr->next, ridx++);
1780
1781 snprintf(filename, sizeof(filename), "%s/reader%d", get_tmp_dir(), ridx);
1782 fp = fopen(filename, "r");
1783
1784 if (fp) {
1785 while (fgets(buffer, 128, fp) != NULL) {
1786 tpl_printf(vars, 1, "LOGHISTORY", "%s<BR>\n", buffer);
1787 }
1788 fclose(fp);
1789 }
1790 tpl_addVar(vars, 0, "READERNAME", rdr->label);
1791 tpl_addVar(vars, 0, "ENTITLEMENTCONTENT", tpl_getTpl(vars, "ENTITLEMENTGENERICBIT"));
1792 }
1793
1794 } else {
1795 tpl_addVar(vars, 0, "LOGHISTORY",
1796 "You have to set saveinithistory=1 in your config to see Entitlements!<BR>\n");
1797 tpl_addVar(vars, 0, "ENTITLEMENTCONTENT", tpl_getTpl(vars, "ENTITLEMENTGENERICBIT"));
1798 }
1799
1800 webif_write(tpl_getTpl(vars, "ENTITLEMENTS"), f);
1801}
1802
1803void send_oscam_status(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in, int apicall) {
1804 int i;
1805 char *usr;
1806 int lsec, isec, con, cau;
1807 time_t now = time((time_t)0);
1808 struct tm *lt;
1809
1810 if (strcmp(getParam(params, "action"), "kill") == 0) {
1811 struct s_client *cl = get_client_by_tid(atol(getParam(params, "threadid")));
1812 if (cl) {
1813 kill_thread(cl);
1814 cs_log("Client %s killed by WebIF from %s", cl->usr, inet_ntoa(*(struct in_addr *)&in));
1815 }
1816 }
1817
1818 if (strcmp(getParam(params, "action"), "restart") == 0) {
1819 struct s_reader *rdr = get_reader_by_label(getParam(params, "label"));
1820 if(rdr) {
1821 restart_cardreader(rdr, 1);
1822 cs_log("Reader %s restarted by WebIF from %s", rdr->label, inet_ntoa(*(struct in_addr *)&in));
1823 }
1824 }
1825
1826 if (strcmp(getParam(params, "action"), "resetstat") == 0) {
1827 struct s_reader *rdr = get_reader_by_label(getParam(params, "label"));
1828 if(rdr) {
1829 clear_reader_stat(rdr);
1830 cs_log("Reader %s stats resetted by WebIF from %s", rdr->label, inet_ntoa(*(struct in_addr *)&in));
1831 }
1832 }
1833
1834 char *debuglvl = getParam(params, "debug");
1835 if(strlen(debuglvl) > 0)
1836 cs_dblevel = atoi(debuglvl);
1837
1838 if(getParamDef(params, "hide", NULL)) {
1839 ulong clidx;
1840 clidx = atol(getParamDef(params, "hide", NULL));
1841 struct s_client *hideidx = get_client_by_tid(clidx);
1842 if(hideidx)
1843 hideidx->wihidden = 1;
1844 }
1845
1846 char *hideidle = getParam(params, "hideidle");
1847 if(strlen(hideidle) > 0) {
1848 if (atoi(hideidle) == 2) {
1849 struct s_client *cl;
1850 for (cl=first_client; cl ; cl=cl->next)
1851 cl->wihidden = 0;
1852 }
1853 else {
1854 int oldval = cfg->http_hide_idle_clients;
1855 chk_t_webif("httphideidleclients", hideidle);
1856 if(oldval != cfg->http_hide_idle_clients) {
1857 refresh_oscam(REFR_SERVER, in);
1858 }
1859 }
1860 }
1861
1862 if(cfg->http_hide_idle_clients > 0) tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED1", "selected");
1863 else tpl_addVar(vars, 0, "HIDEIDLECLIENTSSELECTED0", "selected");
1864
1865 int user_count_all = 0, user_count_active = 0;
1866 int reader_count_all = 0, reader_count_active = 0;
1867 int proxy_count_all = 0, proxy_count_active = 0;
1868
1869 struct s_client *cl;
1870 for (i=0, cl=first_client; cl ; cl=cl->next, i++) {
1871 // Reset template variables
1872 tpl_addVar(vars, 0, "CLIENTLBVALUE","");
1873 if (cl->typ=='c')
1874 user_count_all++;
1875 else if (cl->typ=='p')
1876 proxy_count_all++;
1877 else if (cl->typ=='r')
1878 reader_count_all++;
1879
1880 if (cl->wihidden != 1) {
1881
1882 if((cfg->http_hide_idle_clients == 1) && (cl->typ == 'c') && ((now - cl->lastecm) > cfg->mon_hideclient_to)) continue;
1883
1884 if (cl->typ=='c')
1885 user_count_active++;
1886 else if (cl->typ=='r' && cl->reader->card_status==CARD_INSERTED)
1887 reader_count_active++;
1888 else if (cl->typ=='p' && (cl->reader->card_status==CARD_INSERTED ||cl->reader->tcp_connected))
1889 proxy_count_active++;
1890
1891 lsec=now-cl->login;
1892 isec=now-cl->last;
1893 usr=cl->usr;
1894
1895 if (((cl->typ=='r') || (cl->typ=='p')) && (con=get_ridx(cl->reader)>=0)) usr=cl->reader->label;
1896
1897 if (cl->dup) con=2;
1898 else if ((cl->tosleep) && (now-cl->lastswitch>cl->tosleep)) con=1;
1899 else con=0;
1900
1901 if( (cau=get_ridx(cl->aureader)+1) && (now-cl->lastemm)/60 > cfg->mon_aulow) cau=-cau;
1902
1903 lt=localtime(&cl->login);
1904
1905 tpl_printf(vars, 0, "HIDEIDX", "%ld", cl->thread);
1906 tpl_addVar(vars, 0, "HIDEICON", ICHID);
1907 if(cl->typ == 'c' && !cfg->http_readonly) {
1908 //tpl_printf(vars, 0, "CSIDX", "%d&nbsp;", i);
1909 tpl_printf(vars, 0, "CSIDX", "<A HREF=\"status.html?action=kill&threadid=%ld\" TITLE=\"Kill this client\"><IMG SRC=\"%s\" ALT=\"Kill\"></A>", cl->thread, ICKIL);
1910 }
1911 else if((cl->typ == 'p') && !cfg->http_readonly) {
1912 //tpl_printf(vars, 0, "CLIENTPID", "%d&nbsp;", cl->ridx);
1913 tpl_printf(vars, 0, "CSIDX", "<A HREF=\"status.html?action=restart&label=%s\" TITLE=\"Restart this reader/ proxy\"><IMG SRC=\"%s\" ALT=\"Restart\"></A>", cl->reader->label, ICKIL);
1914 }
1915 else {
1916 tpl_printf(vars, 0, "CSIDX", "%8X&nbsp;", cl->thread);
1917 }
1918
1919 tpl_printf(vars, 0, "CLIENTTYPE", "%c", cl->typ);
1920 tpl_printf(vars, 0, "CLIENTCNR", "%d", get_threadnum(cl));
1921 tpl_addVar(vars, 0, "CLIENTUSER", usr);
1922 tpl_printf(vars, 0, "CLIENTCAU", "%d", cau);
1923 tpl_printf(vars, 0, "CLIENTCRYPTED", "%d", cl->crypted);
1924 tpl_printf(vars, 0, "CLIENTIP", "%s", cs_inet_ntoa(cl->ip));
1925 tpl_printf(vars, 0, "CLIENTPORT", "%d", cl->port);
1926 char *proto = monitor_get_proto(cl);
1927
1928 if ((strcmp(proto,"newcamd") == 0) && (cl->typ == 'c'))
1929 tpl_printf(vars, 0, "CLIENTPROTO","%s (%s)", proto, get_ncd_client_name(cl->ncd_client_id));
1930 else if (((strcmp(proto,"cccam") == 0) || (strcmp(proto,"cccam ext") == 0)) && (cl->typ == 'c')) {
1931 //else if ((strcmp(proto,"cccam") == 0) || (strcmp(proto,"cccam ext") == 0)) {
1932 struct cc_data *cc = cl->cc;
1933 if(cc) {
1934 tpl_printf(vars, 0, "CLIENTPROTO", "%s (%s-%s)", proto, cc->remote_version, cc->remote_build);
1935 if(strcmp(proto,"cccam ext") == 0)
1936 tpl_printf(vars, 0, "CLIENTPROTOTITLE", "%s", cc->remote_oscam);
1937 else
1938 tpl_addVar(vars, 0, "CLIENTPROTOTITLE", ""); //unset tpl var
1939 }
1940 }
1941 else
1942 tpl_printf(vars, 0, "CLIENTPROTO","%s", proto);
1943
1944 tpl_printf(vars, 0, "CLIENTLOGINDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
1945 tpl_printf(vars, 1, "CLIENTLOGINDATE", " %02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
1946
1947 int secs = 0, fullmins =0, mins =0, fullhours =0, hours =0, days =0;
1948 if(lsec > 0) {
1949 secs = lsec % 60;
1950 if (lsec > 60) {
1951 fullmins = lsec / 60;
1952 mins = fullmins % 60;
1953 if(fullmins > 60) {
1954 fullhours = fullmins / 60;
1955 hours = fullhours % 24;
1956 days = fullhours / 24;
1957 }
1958 }
1959 }
1960 if(days == 0)
1961 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%02d:%02d:%02d", hours, mins, secs);
1962 else
1963 tpl_printf(vars, 0, "CLIENTLOGINSECS", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
1964
1965
1966 if (isec < cfg->mon_hideclient_to || cfg->mon_hideclient_to == 0) {
1967
1968 if (((cl->typ!='r') || (cl->typ!='p')) && (cl->lastreader[0]))
1969 tpl_printf(vars, 0, "CLIENTLBVALUE", "%s", cl->lastreader);
1970
1971 tpl_printf(vars, 0, "CLIENTCAID", "%04X", cl->last_caid);
1972 tpl_printf(vars, 0, "CLIENTSRVID", "%04X", cl->last_srvid);
1973
1974 int j, found = 0;
1975 struct s_srvid *srvid = cfg->srvid;
1976
1977 while (srvid != NULL) {
1978 if (srvid->srvid == cl->last_srvid) {
1979 for (j=0; j < srvid->ncaid; j++) {
1980 if (srvid->caid[j] == cl->last_caid) {
1981 found = 1;
1982 break;
1983 }
1984 }
1985 }
1986 if (found == 1)
1987 break;
1988 else
1989 srvid = srvid->next;
1990 }
1991
1992 if (found == 1) {
1993 tpl_printf(vars, 0, "CLIENTSRVPROVIDER","%s: ", srvid->prov);
1994 tpl_addVar(vars, 0, "CLIENTSRVNAME", srvid->name);
1995 tpl_addVar(vars, 0, "CLIENTSRVTYPE", srvid->type);
1996 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION", srvid->desc);
1997 } else {
1998 tpl_addVar(vars, 0, "CLIENTSRVPROVIDER","");
1999 tpl_printf(vars, 0, "CLIENTSRVNAME","");
2000 tpl_addVar(vars, 0, "CLIENTSRVTYPE","");
2001 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION","");
2002 }
2003
2004 } else {
2005 tpl_printf(vars, 0, "CLIENTCAID", "0000");
2006 tpl_printf(vars, 0, "CLIENTSRVID", "0000");
2007 tpl_addVar(vars, 0, "CLIENTSRVPROVIDER","");
2008 tpl_printf(vars, 0, "CLIENTSRVNAME","");
2009 tpl_addVar(vars, 0, "CLIENTSRVTYPE","");
2010 tpl_addVar(vars, 0, "CLIENTSRVDESCRIPTION","");
2011 tpl_printf(vars, 0, "CLIENTLBVALUE","");
2012
2013 }
2014
2015 secs = 0, fullmins =0, mins =0, fullhours =0, hours =0, days =0;
2016 if(isec > 0) {
2017 secs = isec % 60;
2018 if (isec > 60) {
2019 fullmins = isec / 60;
2020 mins = fullmins % 60;
2021 if(fullmins > 60) {
2022 fullhours = fullmins / 60;
2023 hours = fullhours % 24;
2024 days = fullhours / 24;
2025 }
2026 }
2027 }
2028 if(days == 0)
2029 tpl_printf(vars, 0, "CLIENTIDLESECS", "%02d:%02d:%02d", hours, mins, secs);
2030 else
2031 tpl_printf(vars, 0, "CLIENTIDLESECS", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
2032 if(con == 2) tpl_printf(vars, 0, "CLIENTCON", "Duplicate");
2033 else if (con == 1) tpl_printf(vars, 0, "CLIENTCON", "Sleep");
2034 else
2035 {
2036 char *txt = "OK";
2037 if (cl->typ == 'r' || cl->typ == 'p') //reader or proxy
2038 {
2039 struct s_reader *rdr = cl->reader;
2040 if (rdr->lbvalue)
2041 tpl_printf(vars, 0, "CLIENTLBVALUE", "<A HREF=\"status.html?action=resetstat&label=%s\" TITLE=\"Reset statistics for this reader/ proxy\">%d</A>", rdr->label, rdr->lbvalue);
2042 else
2043 tpl_printf(vars, 0, "CLIENTLBVALUE", "<A HREF=\"status.html?action=resetstat&label=%s\" TITLE=\"Reset statistics for this reader/ proxy\">%s</A>", rdr->label, "no data");
2044
2045 switch(rdr->card_status)
2046 {
2047 case NO_CARD: txt = "OFF"; break;
2048 case CARD_NEED_INIT: txt = "NEEDINIT"; break;
2049 case CARD_INSERTED:
2050 if (cl->typ=='p')
2051 txt = "CONNECTED";
2052 else
2053 txt = "CARDOK";
2054 break;
2055 case CARD_FAILURE: txt = "ERROR"; break;
2056 default: txt = "UNDEF";
2057 }
2058 }
2059 tpl_printf(vars, 0, "CLIENTCON", txt);
2060 }
2061
2062 if (!apicall){
2063 // select right suborder
2064 if (cl->typ == 'c') {
2065 tpl_addVar(vars, 1, "CLIENTSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
2066 tpl_printf(vars, 0, "CLIENTHEADLINE", "<TR><TD CLASS=\"subheadline\" colspan=\"17\">Clients %d/%d</TD></TR>\n",
2067 user_count_active, user_count_all);
2068 }
2069 else if (cl->typ == 'r') {
2070 tpl_addVar(vars, 1, "READERSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
2071 tpl_printf(vars, 0, "READERHEADLINE", "<TR><TD CLASS=\"subheadline\" colspan=\"17\">Readers %d/%d</TD></TR>\n",
2072 reader_count_active, reader_count_all);
2073 }
2074 else if (cl->typ == 'p') {
2075 tpl_addVar(vars, 1, "PROXYSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
2076 tpl_printf(vars, 0, "PROXYHEADLINE", "<TR><TD CLASS=\"subheadline\" colspan=\"17\">Proxies %d/%d</TD></TR>\n",
2077 proxy_count_active, proxy_count_all);
2078 }
2079 else
2080 tpl_addVar(vars, 1, "SERVERSTATUS", tpl_getTpl(vars, "CLIENTSTATUSBIT"));
2081
2082 } else {
2083 tpl_addVar(vars, 1, "APISTATUSBITS", tpl_getTpl(vars, "APISTATUSBIT"));
2084 }
2085
2086 }
2087 if (cl->next == NULL)
2088 break;
2089 }
2090 //tpl_printf(vars, 0, "USERCOUNTALL", "%d", user_count_all);
2091 //tpl_printf(vars, 0, "USERCOUNTACTIVE", "%d", user_count_active);
2092
2093 //tpl_printf(vars, 0, "READERCOUNTALL", "%d", reader_count_all);
2094 //tpl_printf(vars, 0, "READERCOUNTACTIVE", "%d", reader_count_active);
2095
2096 //tpl_printf(vars, 0, "PROXYCOUNTALL", "%d", proxy_count_all);
2097 //tpl_printf(vars, 0, "PROXYCOUNTACTIVE", "%d", proxy_count_active);
2098
2099#ifdef CS_LOGHISTORY
2100 for (i=(loghistidx+3) % CS_MAXLOGHIST; i!=loghistidx; i=(i+1) % CS_MAXLOGHIST) {
2101 char *p_usr, *p_txt;
2102 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
2103 p_txt=p_usr+32;
2104
2105 if (!apicall) {
2106 if (p_txt[0]) tpl_printf(vars, 1, "LOGHISTORY", "<span class=\"%s\">%s</span><br>\n", p_usr, p_txt+8);
2107 } else {
2108 tpl_printf(vars, 1, "LOGHISTORY", "%s", p_txt+8);
2109 }
2110 }
2111#else
2112 tpl_addVar(vars, 0, "LOGHISTORY", "the flag CS_LOGHISTORY is not set in your binary<BR>\n");
2113#endif
2114
2115 // Debuglevel Selector
2116 tpl_addVar(vars, 0, "NEXTPAGE", "status.html");
2117 tpl_printf(vars, 0, "ACTDEBUG", "%d", cs_dblevel);
2118 tpl_addVar(vars, 0, "SDEBUG", tpl_getTpl(vars, "DEBUGSELECT"));
2119
2120 if(!apicall)
2121 webif_write(tpl_getTpl(vars, "STATUS"), f);
2122 else
2123 webif_write(tpl_getTpl(vars, "APISTATUS"), f);
2124
2125}
2126
2127void send_oscam_services_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
2128 struct s_sidtab *sidtab,*ptr;
2129 char label[128];
2130 int i;
2131
2132 cs_strncpy(label, strtolower(getParam(params, "service")), sizeof(label)/sizeof(char));
2133
2134 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
2135
2136 if (sidtab == NULL) {
2137 i = 1;
2138 while(strlen(label) < 1) {
2139 snprintf(label, sizeof(label)/sizeof(char) - 1, "newservice%d", i);
2140 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab = sidtab->next);
2141 if(sidtab != NULL) label[0] = '\0';
2142 ++i;
2143 }
2144 if (!(sidtab=malloc(sizeof(struct s_sidtab)))) {
2145 cs_log("Error allocating memory (errno=%d)", errno);
2146 return;
2147 }
2148
2149 if(cfg->sidtab == NULL) cfg->sidtab = sidtab;
2150 else {
2151 for (ptr = cfg->sidtab; ptr != NULL && ptr->next != NULL; ptr = ptr->next);
2152 ptr->next = sidtab;
2153 }
2154 memset(sidtab, 0, sizeof(struct s_sidtab));
2155 cs_strncpy((char *)sidtab->label, label, sizeof(sidtab->label));
2156
2157 tpl_addVar(vars, 1, "MESSAGE", "<b>New service has been added</b><BR>");
2158 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
2159 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
2160
2161 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
2162 }
2163
2164 if (strcmp(getParam(params, "action"), "Save") == 0) {
2165 for(i=0;i<(*params).paramcount;i++) {
2166 if ((strcmp((*params).params[i], "action")) && (strcmp((*params).params[i], "service"))) {
2167 chk_sidtab((*params).params[i], (*params).values[i], sidtab);
2168 }
2169 }
2170 tpl_addVar(vars, 1, "MESSAGE", "<B>Services updated</B><BR><BR>");
2171 if (write_services()==0) refresh_oscam(REFR_SERVICES, in);
2172 else tpl_addVar(vars, 1, "MESSAGE", "<B>Write Config failed</B><BR><BR>");
2173
2174 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
2175 }
2176
2177 tpl_addVar(vars, 0, "LABEL", sidtab->label);
2178 tpl_addVar(vars, 0, "LABELENC", urlencode(sidtab->label));
2179
2180 for (i=0; i<sidtab->num_caid; i++) {
2181 if (i==0) tpl_printf(vars, 0, "CAIDS", "%04X", sidtab->caid[i]);
2182 else tpl_printf(vars, 1, "CAIDS", ",%04X", sidtab->caid[i]);
2183 }
2184 for (i=0; i<sidtab->num_provid; i++) {
2185 if (i==0) tpl_printf(vars, 0, "PROVIDS", "%06lX", sidtab->provid[i]);
2186 else tpl_printf(vars, 1, "PROVIDS", ",%06lX", sidtab->provid[i]);
2187 }
2188 for (i=0; i<sidtab->num_srvid; i++) {
2189 if (i==0) tpl_printf(vars, 0, "SRVIDS", "%04X", sidtab->srvid[i]);
2190 else tpl_printf(vars, 1, "SRVIDS", ",%04X", sidtab->srvid[i]);
2191 }
2192 webif_write(tpl_getTpl(vars, "SERVICEEDIT"), f);
2193}
2194
2195void send_oscam_services(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
2196 struct s_sidtab *sidtab, *sidtab2;
2197 char *service = getParam(params, "service");
2198 int i, found = 0;
2199
2200 if (strcmp(getParam(params, "action"), "delete") == 0) {
2201 if(cfg->http_readonly) {
2202 tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry, Webif is in readonly mode. No deletion will be made!</b><BR>");
2203 } else {
2204 sidtab=cfg->sidtab;
2205 if(strcmp(sidtab->label, service) == 0) {
2206 cfg->sidtab = sidtab->next;
2207 free(sidtab);
2208 found = 1;
2209 } else if (sidtab->next != NULL) {
2210 do {
2211 if(strcmp(sidtab->next->label, service) == 0) {
2212 sidtab2 = sidtab->next;
2213 sidtab->next = sidtab2->next;
2214 free(sidtab2);
2215 found = 1;
2216 break;
2217 }
2218 } while ((sidtab = sidtab->next) && (sidtab->next != NULL));
2219 }
2220 if (found > 0) {
2221 tpl_addVar(vars, 1, "MESSAGE", "<b>Service has been deleted!</b><BR>");
2222 if (write_services() == 0) refresh_oscam(REFR_SERVICES, in);
2223 else tpl_addVar(vars, 1, "MESSAGE", "<b>Writing services to disk failed!</b><BR>");
2224 } else tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry but the specified service doesn't exist. No deletion will be made!</b><BR>");
2225 }
2226 }
2227
2228 sidtab = cfg->sidtab;
2229 // Show List
2230 while(sidtab != NULL) {
2231 tpl_printf(vars, 0, "SID","");
2232 if ((strcmp(getParam(params, "service"), sidtab->label) == 0) && (strcmp(getParam(params, "action"), "list") == 0) ) {
2233 tpl_printf(vars, 0, "SIDCLASS","sidlist");
2234 tpl_printf(vars, 1, "SID", "<div style=\"float:right;background-color:red;color:white\"><A HREF=\"services.html\" style=\"color:white;text-decoration:none\">X</A></div>");
2235 for (i=0; i<sidtab->num_srvid; i++) {
2236 tpl_printf(vars, 1, "SID", "%04X : %s<BR>", sidtab->srvid[i], get_servicename(sidtab->srvid[i], sidtab->caid[0]));
2237 }
2238 } else {
2239 tpl_printf(vars, 0, "SIDCLASS","");
2240 tpl_printf(vars, 0, "SID","<A HREF=\"services.html?service=%s&action=list\">Show Services</A>",tpl_addTmp(vars, urlencode(sidtab->label)));
2241 }
2242 tpl_addVar(vars, 0, "LABELENC", tpl_addTmp(vars, urlencode(sidtab->label)));
2243 tpl_addVar(vars, 0, "LABEL", sidtab->label);
2244 tpl_addVar(vars, 0, "SIDLIST", tpl_getTpl(vars, "SERVICECONFIGSIDBIT"));
2245 tpl_addVar(vars, 0, "EDIICO", ICEDI);
2246 tpl_addVar(vars, 0, "DELICO", ICDEL);
2247 tpl_addVar(vars, 1, "SERVICETABS", tpl_getTpl(vars, "SERVICECONFIGLISTBIT"));
2248 sidtab=sidtab->next;
2249 }
2250 webif_write(tpl_getTpl(vars, "SERVICECONFIGLIST"), f);
2251}
2252
2253void send_oscam_savetpls(struct templatevars *vars, FILE *f) {
2254 if(strlen(cfg->http_tpl) > 0) {
2255 tpl_printf(vars, 0, "CNT", "%d", tpl_saveIncludedTpls(cfg->http_tpl));
2256 tpl_addVar(vars, 0, "PATH", cfg->http_tpl);
2257 } else tpl_addVar(vars, 0, "CNT", "0");
2258 webif_write(tpl_getTpl(vars, "SAVETEMPLATES"), f);
2259}
2260
2261void send_oscam_shutdown(struct templatevars *vars, FILE *f, struct uriparams *params) {
2262 if (strcmp(getParam(params, "action"), "Shutdown") == 0) {
2263 tpl_addVar(vars, 0, "STYLESHEET", CSS);
2264 tpl_printf(vars, 0, "REFRESHTIME", "%d", SHUTDOWNREFRESH);
2265 tpl_addVar(vars, 0, "REFRESHURL", "status.html");
2266 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
2267 tpl_printf(vars, 0, "SECONDS", "%d", SHUTDOWNREFRESH);
2268 webif_write(tpl_getTpl(vars, "SHUTDOWN"), f);
2269 running = 0;
2270
2271 struct s_client *cl;
2272 for (cl=first_client->next; cl ; cl=cl->next)
2273 kill_thread(cl);
2274 exit(SIGQUIT);
2275 } else {
2276 webif_write(tpl_getTpl(vars, "PRESHUTDOWN"), f);
2277 }
2278}
2279
2280void send_oscam_script(struct templatevars *vars, FILE *f) {
2281
2282 char *result = "not found";
2283 int rc = 0;
2284 if(!cfg->http_readonly) {
2285 if(cfg->http_script[0]) {
2286 tpl_addVar(vars, 0, "SCRIPTNAME",cfg->http_script);
2287 rc = system(cfg->http_script);
2288 if(rc == -1) {
2289 result = "done";
2290 } else {
2291 result = "failed";
2292 }
2293 } else {
2294 tpl_addVar(vars, 0, "SCRIPTNAME", "no script defined");
2295 }
2296 tpl_addVar(vars, 0, "SCRIPTRESULT", result);
2297 tpl_printf(vars, 0, "CODE", "%d", rc);
2298 } else {
2299 tpl_addVar(vars, 1, "MESSAGE", "<b>Sorry, Webif is in readonly mode. No script execution possible!</b><BR>");
2300 }
2301 webif_write(tpl_getTpl(vars, "SCRIPT"), f);
2302
2303}
2304
2305void send_oscam_scanusb(struct templatevars *vars, FILE *f) {
2306 FILE *fp;
2307 int err=0;
2308 char path[1035];
2309
2310 fp = popen("lsusb -v | egrep '^Bus|^ *iSerial|^ *iProduct'", "r");
2311 if (fp == NULL) {
2312 tpl_addVar(vars, 0, "USBENTRY", "Failed to run lusb");
2313 tpl_printf(vars, 0, "USBENTRY", "%s", path);
2314 tpl_addVar(vars, 1, "USBBIT", tpl_getTpl(vars, "SCANUSBBIT"));
2315 err = 1;
2316 }
2317
2318 if(!err) {
2319 while (fgets(path, sizeof(path)-1, fp) != NULL) {
2320 tpl_addVar(vars, 0, "USBENTRYCLASS", "");
2321 if (strstr(path,"Bus ")) {
2322 tpl_printf(vars, 0, "USBENTRY", "%s", path);
2323 tpl_addVar(vars, 0, "USBENTRYCLASS", "CLASS=\"scanusbsubhead\"");
2324 } else {
2325 tpl_printf(vars, 0, "USBENTRY", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s", path);
2326 }
2327 tpl_addVar(vars, 1, "USBBIT", tpl_getTpl(vars, "SCANUSBBIT"));
2328 }
2329 }
2330 pclose(fp);
2331 webif_write(tpl_getTpl(vars, "SCANUSB"), f);
2332}
2333
2334void send_oscam_files(struct templatevars *vars, FILE *f, struct uriparams *params) {
2335
2336 int writable=0;
2337
2338 char *stoplog = getParam(params, "stoplog");
2339 if(strlen(stoplog) > 0)
2340 cfg->disablelog = atoi(stoplog);
2341
2342 char *stopusrlog = getParam(params, "stopusrlog");
2343 if(strlen(stopusrlog) > 0)
2344 cfg->disableuserfile = atoi(stopusrlog);
2345
2346 char *debuglvl = getParam(params, "debug");
2347 if(strlen(debuglvl) > 0)
2348 cs_dblevel = atoi(debuglvl);
2349
2350 char targetfile[256];
2351
2352 if (strcmp(getParam(params, "part"), "conf") == 0) {
2353 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.conf");
2354 writable = 1;
2355 }
2356 else if (strcmp(getParam(params, "part"), "version") == 0)
2357 snprintf(targetfile, 255,"%s%s", get_tmp_dir(), "/oscam.version");
2358 else if (strcmp(getParam(params, "part"), "user") == 0) {
2359 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.user");
2360 writable = 1;
2361 }
2362 else if (strcmp(getParam(params, "part"), "server") == 0) {
2363 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.server");
2364 writable = 1;
2365 }
2366 else if (strcmp(getParam(params, "part"), "services") == 0) {
2367 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.services");
2368 writable = 1;
2369 }
2370 else if (strcmp(getParam(params, "part"), "srvid") == 0) {
2371 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.srvid");
2372 writable = 1;
2373 }
2374 else if (strcmp(getParam(params, "part"), "provid") == 0) {
2375 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.provid");
2376 writable = 1;
2377 }
2378 else if (strcmp(getParam(params, "part"), "tiers") == 0) {
2379 snprintf(targetfile, 255,"%s%s", cs_confdir, "oscam.tiers");
2380 writable = 1;
2381 }
2382 else if (strcmp(getParam(params, "part"), "logfile") == 0) {
2383 snprintf(targetfile, 255,"%s", cfg->logfile);
2384
2385 if (strcmp(getParam(params, "clear"), "logfile") == 0) {
2386 if(strlen(targetfile) > 0) {
2387 FILE *file = fopen(targetfile,"w");
2388 fclose(file);
2389 }
2390 }
2391
2392 // Debuglevel Selector
2393 tpl_addVar(vars, 0, "NEXTPAGE", "files.html");
2394 tpl_addVar(vars, 0, "CUSTOMPARAM", "&part=logfile");
2395 tpl_printf(vars, 0, "ACTDEBUG", "%d", cs_dblevel);
2396 tpl_addVar(vars, 0, "SDEBUG", tpl_getTpl(vars, "DEBUGSELECT"));
2397
2398 if(!cfg->disablelog)
2399 tpl_printf(vars, 0, "SLOG", "<BR><A CLASS=\"debugl\" HREF=\"files.html?part=logfile&stoplog=%d\">%s</A><SPAN CLASS=\"debugt\">&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN>\n", 1, "Stop Log");
2400 else
2401 tpl_printf(vars, 0, "SLOG", "<BR><A CLASS=\"debugl\" HREF=\"files.html?part=logfile&stoplog=%d\">%s</A><SPAN CLASS=\"debugt\">&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN>\n", 0, "Start Log");
2402
2403 tpl_printf(vars, 0, "SCLEAR", "<A CLASS=\"debugl\" HREF=\"files.html?part=logfile&clear=logfile\">%s</A><BR><BR>\n", "Clear Log");
2404 }
2405 else if (strcmp(getParam(params, "part"), "userfile") == 0) {
2406 snprintf(targetfile, 255,"%s", cfg->usrfile);
2407 if (strcmp(getParam(params, "clear"), "usrfile") == 0) {
2408 if(strlen(targetfile) > 0) {
2409 FILE *file = fopen(targetfile,"w");
2410 fclose(file);
2411 }
2412 }
2413
2414 if(!cfg->disableuserfile)
2415 tpl_printf(vars, 0, "SLOG", "<A HREF=\"files.html?part=userfile&stopusrlog=%d\">%s</A>&nbsp;&nbsp;|&nbsp;&nbsp;\n", 1, "Stop Log");
2416 else
2417 tpl_printf(vars, 0, "SLOG", "<A HREF=\"files.html?part=userfile&stopusrlog=%d\">%s</A>&nbsp;&nbsp;|&nbsp;&nbsp;\n", 0, "Start Log");
2418
2419 tpl_printf(vars, 0, "SCLEAR", "<A HREF=\"files.html?part=userfile&clear=usrfile\">%s</A><BR><BR>\n", "Clear Log");
2420 tpl_addVar(vars, 0, "FILTER", "<FORM ACTION=\"files.html\" method=\"get\">\n");
2421 tpl_addVar(vars, 1, "FILTER", "<INPUT name=\"part\" type=\"hidden\" value=\"userfile\">\n");
2422 tpl_addVar(vars, 1, "FILTER", "<SELECT name=\"filter\">\n");
2423 tpl_printf(vars, 1, "FILTER", "<OPTION value=\"%s\">%s</OPTION>\n", "all", "all");
2424
2425 struct s_auth *account;
2426 for (account = cfg->account; (account); account = account->next) {
2427 tpl_printf(vars, 1, "FILTER", "<OPTION value=\"%s\" %s>%s</OPTION>\n", account->usr, strcmp(getParam(params, "filter"), account->usr) ? "":"selected", account->usr);
2428 }
2429 tpl_addVar(vars, 1, "FILTER", "</SELECT><input type=\"submit\" name=\"action\" value=\"Filter\" title=\"Filter for a specific user\"></FORM>\n");
2430
2431 }
2432#ifdef CS_ANTICASC
2433 else if (strcmp(getParam(params, "part"), "anticasc") == 0)
2434 snprintf(targetfile, 255,"%s", cfg->ac_logfile);
2435#endif
2436
2437#ifdef HAVE_DVBAPI
2438 else if (strcmp(getParam(params, "part"), "dvbapi") == 0) {
2439 snprintf(targetfile, 255, "%s%s", cs_confdir, "oscam.dvbapi");
2440 writable = 1;
2441 }
2442#endif
2443
2444
2445 if (!strstr(targetfile, "/dev/")) {
2446
2447 if (strcmp(getParam(params, "action"), "Save") == 0) {
2448 if((strlen(targetfile) > 0) && (file_exists(targetfile) == 1)) {
2449 FILE *fpsave;
2450 char *fcontent = getParam(params, "filecontent");
2451 urldecode(fcontent);
2452
2453 if((fpsave = fopen(targetfile,"w"))){
2454 fprintf(fpsave,"%s",fcontent);
2455 fclose(fpsave);
2456
2457#ifdef HAVE_DVBAPI
2458 if (strcmp(getParam(params, "part"), "dvbapi") == 0)
2459 dvbapi_read_priority();
2460#endif
2461 }
2462 }
2463 }
2464
2465 if((strlen(targetfile) > 0) && (file_exists(targetfile) == 1)) {
2466 FILE *fp;
2467 char buffer[256];
2468
2469 if((fp = fopen(targetfile,"r")) == NULL) return;
2470 while (fgets(buffer, sizeof(buffer), fp) != NULL)
2471 if (!strcmp(getParam(params, "filter"), "all"))
2472 tpl_printf(vars, 1, "FILECONTENT", "%s", buffer);
2473 else
2474 if(strstr(buffer,getParam(params, "filter")))
2475 tpl_printf(vars, 1, "FILECONTENT", "%s", buffer);
2476 fclose (fp);
2477 } else {
2478 tpl_addVar(vars, 1, "FILECONTENT", "File not exist");
2479 }
2480 } else {
2481 tpl_addVar(vars, 1, "FILECONTENT", "File not valid");
2482 }
2483
2484 tpl_addVar(vars, 0, "PART", getParam(params, "part"));
2485
2486 if (!writable) {
2487 tpl_addVar(vars, 0, "WRITEPROTECTION", "You cannot change content of this file");
2488 tpl_addVar(vars, 0, "BTNDISABLED", "DISABLED");
2489 }
2490
2491
2492 webif_write(tpl_getTpl(vars, "FILE"), f);
2493}
2494
2495void send_oscam_failban(struct templatevars *vars, FILE *f, struct uriparams *params) {
2496
2497 uint ip2delete = 0;
2498 LLIST_D__ITR itr;
2499 V_BAN *v_ban_entry = llist_itr_init(cfg->v_list, &itr);
2500
2501 if (strcmp(getParam(params, "action"), "delete") == 0) {
2502 sscanf(getParam(params, "intip"), "%u", &ip2delete);
2503 while (v_ban_entry) {
2504 if (v_ban_entry->v_ip == ip2delete) {
2505 free(v_ban_entry);
2506 llist_itr_remove(&itr);
2507 break;
2508 }
2509 v_ban_entry = llist_itr_next(&itr);
2510 }
2511 }
2512
2513 time_t now = time((time_t)0);
2514 v_ban_entry = llist_itr_init(cfg->v_list, &itr);
2515
2516 while (v_ban_entry) {
2517 tpl_addVar(vars, 0, "DELICO", ICDEL);
2518 tpl_printf(vars, 0, "IPADDRESS", "%s", cs_inet_ntoa(v_ban_entry->v_ip));
2519
2520 struct tm *st ;
2521 st = localtime(&v_ban_entry->v_time);
2522
2523 tpl_printf(vars, 0, "VIOLATIONDATE", "%02d.%02d.%02d %02d:%02d:%02d",
2524 st->tm_mday, st->tm_mon+1,
2525 st->tm_year%100, st->tm_hour,
2526 st->tm_min, st->tm_sec);
2527
2528 int lsec = (cfg->failbantime * 60) - (now - v_ban_entry->v_time);
2529 int secs = 0, fullmins =0, mins =0, fullhours =0, hours =0, days =0;
2530 if(lsec > 0) {
2531 secs = lsec % 60;
2532 if (lsec > 60) {
2533 fullmins = lsec / 60;
2534 mins = fullmins % 60;
2535 if(fullmins > 60) {
2536 fullhours = fullmins / 60;
2537 hours = fullhours % 24;
2538 days = fullhours / 24;
2539 }
2540 }
2541 }
2542 if(days == 0)
2543 tpl_printf(vars, 0, "LEFTTIME", "%02d:%02d:%02d", hours, mins, secs);
2544 else
2545 tpl_printf(vars, 0, "LEFTTIME", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
2546
2547 tpl_printf(vars, 0, "INTIP", "%u", v_ban_entry->v_ip);
2548 tpl_addVar(vars, 1, "FAILBANROW", tpl_getTpl(vars, "FAILBANBIT"));
2549 v_ban_entry = llist_itr_next(&itr);
2550 }
2551
2552 webif_write(tpl_getTpl(vars, "FAILBAN"), f);
2553}
2554
2555void send_oscam_api(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) {
2556 if (strcmp(getParam(params, "part"), "status") == 0) {
2557 send_oscam_status(vars, f, params, in, 1);
2558 }
2559}
2560
2561void webif_parse_request(struct uriparams *params, char *pch) {
2562 /* Parse url parameters; parsemode = 1 means parsing next param, parsemode = -1 parsing next
2563 value; pch2 points to the beginning of the currently parsed string, pch is the current position */
2564
2565 char *pch2;
2566 int parsemode = 1;
2567
2568 pch2=pch;
2569 while(pch[0] != '\0') {
2570 if((parsemode == 1 && pch[0] == '=') || (parsemode == -1 && pch[0] == '&')) {
2571 pch[0] = '\0';
2572 urldecode(pch2);
2573 if(parsemode == 1) {
2574 if(params->paramcount >= MAXGETPARAMS) break;
2575 ++params->paramcount;
2576 params->params[params->paramcount-1] = pch2;
2577 } else {
2578 params->values[params->paramcount-1] = pch2;
2579 }
2580 parsemode = -parsemode;
2581 pch2 = pch + 1;
2582 }
2583 ++pch;
2584 }
2585 /* last value wasn't processed in the loop yet... */
2586 if(parsemode == -1 && params->paramcount <= MAXGETPARAMS) {
2587 urldecode(pch2);
2588 params->values[params->paramcount-1] = pch2;
2589 }
2590}
2591
2592int process_request(FILE *f, struct in_addr in) {
2593
2594 cur_client()->last = time((time_t)0); //reset last busy time
2595
2596 int ok=0,v=cv();
2597 struct s_ip *p_ip;
2598 in_addr_t addr = cs_inet_order(in.s_addr);
2599
2600 for (p_ip = cfg->http_allowed; (p_ip) && (!ok); p_ip = p_ip->next)
2601 ok =((addr >= p_ip->ip[0]) && (addr <= p_ip->ip[1]))?v:0;
2602
2603 if (!ok && cfg->http_dyndns[0]) {
2604 if(cfg->http_dynip && cfg->http_dynip == addr) {
2605 ok = v;
2606
2607 } else {
2608
2609 if (cfg->resolve_gethostbyname) {
2610
2611 pthread_mutex_lock(&gethostbyname_lock);
2612 struct hostent *rht;
2613 struct sockaddr_in udp_sa;
2614
2615 rht = gethostbyname((const char *) cfg->http_dyndns);
2616 if (rht) {
2617 memcpy(&udp_sa.sin_addr, rht->h_addr, sizeof(udp_sa.sin_addr));
2618 cfg->http_dynip = cs_inet_order(udp_sa.sin_addr.s_addr);
2619 if (cfg->http_dynip == addr)
2620 ok = v;
2621 } else {
2622 cs_log("can't resolve %s", cfg->http_dyndns);
2623 }
2624 pthread_mutex_unlock(&gethostbyname_lock);
2625
2626 } else {
2627
2628 struct addrinfo hints, *res = NULL;
2629 memset(&hints, 0, sizeof(hints));
2630 hints.ai_socktype = SOCK_STREAM;
2631 hints.ai_family = AF_INET;
2632 hints.ai_protocol = IPPROTO_TCP;
2633
2634 int err = getaddrinfo((const char*)cfg->http_dyndns, NULL, &hints, &res);
2635 if (err != 0 || !res || !res->ai_addr) {
2636 cs_log("can't resolve %s, error: %s", cfg->http_dyndns, err ? gai_strerror(err) : "unknown");
2637 }
2638 else {
2639 cfg->http_dynip = cs_inet_order(((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr);
2640 ok = v;
2641 }
2642 if (res) freeaddrinfo(res);
2643
2644 }
2645 }
2646 }
2647
2648 if (!ok) {
2649 send_error(f, 403, "Forbidden", NULL, "Access denied.");
2650 cs_log("unauthorized access from %s", inet_ntoa(*(struct in_addr *)&in));
2651 return 0;
2652 }
2653
2654 char buf[10240];
2655 char *tmp;
2656
2657 int authok = 0;
2658 char expectednonce[64];
2659
2660 char *method;
2661 char *path;
2662 char *protocol;
2663 char *pch;
2664 /* List of possible pages */
2665 char *pages[]= {
2666 "/config.html",
2667 "/readers.html",
2668 "/entitlements.html",
2669 "/status.html",
2670 "/userconfig.html",
2671 "/readerconfig.html",
2672 "/services.html",
2673 "/user_edit.html",
2674 "/site.css",
2675 "/services_edit.html",
2676 "/savetemplates.html",
2677 "/shutdown.html",
2678 "/script.html",
2679 "/scanusb.html",
2680 "/files.html",
2681 "/readerstats.html",
2682 "/failban.html",
2683 "/oscam.js",
2684 "/oscamapi.html"};
2685
2686 int pagescnt = sizeof(pages)/sizeof(char *); // Calculate the amount of items in array
2687
2688 int pgidx = -1;
2689 int i;
2690 struct uriparams params;
2691 params.paramcount = 0;
2692
2693 /* First line always includes the GET/POST request */
2694 char *saveptr1=NULL;
2695 int n;
2696 if ((n=webif_read(buf, sizeof(buf)-1, f)) <= 0) {
2697 cs_debug_mask(D_CLIENT, "webif read error %d", n);
2698 return -1;
2699 }
2700 buf[n]='\0';
2701
2702 method = strtok_r(buf, " ", &saveptr1);
2703 path = strtok_r(NULL, " ", &saveptr1);
2704 protocol = strtok_r(NULL, "\r", &saveptr1);
2705 if(method == NULL || path == NULL || protocol == NULL) return -1;
2706 tmp=protocol+strlen(protocol)+2;
2707
2708 pch=path;
2709 /* advance pointer to beginning of query string */
2710 while(pch[0] != '?' && pch[0] != '\0') ++pch;
2711 if(pch[0] == '?') {
2712 pch[0] = '\0';
2713 ++pch;
2714 }
2715
2716 /* Map page to our static page definitions */
2717 for (i=0; i<pagescnt; i++) {
2718 if (!strcmp(path, pages[i])) pgidx = i;
2719 }
2720
2721 webif_parse_request(&params, pch);
2722
2723 if(strlen(cfg->http_user) == 0 || strlen(cfg->http_pwd) == 0) authok = 1;
2724 else calculate_nonce(expectednonce, sizeof(expectednonce)/sizeof(char));
2725
2726 char *str1, *saveptr=NULL;
2727
2728 for (str1=strtok_r(tmp, "\n", &saveptr); str1; str1=strtok_r(NULL, "\n", &saveptr)) {
2729 if (strlen(str1)==1) {
2730 if (strcmp(method, "POST")==0) {
2731 webif_parse_request(&params, str1+2);
2732 }
2733 break;
2734 }
2735 if(authok == 0 && strlen(str1) > 50 && strncmp(str1, "Authorization:", 14) == 0 && strstr(str1, "Digest") != NULL) {
2736 authok = check_auth(str1, method, path, expectednonce);
2737 }
2738 }
2739
2740 if(authok != 1) {
2741 strcpy(tmp, "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\"");
2742 strcat(tmp, AUTHREALM);
2743 strcat(tmp, "\", qop=\"auth\", opaque=\"\", nonce=\"");
2744 strcat(tmp, expectednonce);
2745 strcat(tmp, "\"");
2746 if(authok == 2) strcat(tmp, ", stale=true");
2747 send_headers(f, 401, "Unauthorized", tmp, "text/html");
2748 return 0;
2749 }
2750
2751 /*build page*/
2752 if(pgidx == 8) {
2753 send_headers(f, 200, "OK", NULL, "text/css");
2754 send_file(f, 1);
2755 } else if (pgidx == 17) {
2756 send_headers(f, 200, "OK", NULL, "text/javascript");
2757 send_file(f, 2);
2758 } else {
2759 send_headers(f, 200, "OK", NULL, "text/html");
2760 time_t t;
2761 struct templatevars *vars = tpl_create();
2762 struct tm *lt;
2763 struct tm *st;
2764 time(&t);
2765
2766 lt = localtime(&t);
2767
2768 tpl_addVar(vars, 0, "CS_VERSION", CS_VERSION);
2769 tpl_addVar(vars, 0, "CS_SVN_VERSION", CS_SVN_VERSION);
2770 tpl_addVar(vars, 0, "ICO", ICMAI);
2771 if(cfg->http_refresh > 0 && (pgidx == 3 || pgidx == -1)) {
2772 tpl_printf(vars, 0, "REFRESHTIME", "%d", cfg->http_refresh);
2773 tpl_addVar(vars, 0, "REFRESHURL", "status.html");
2774 tpl_addVar(vars, 0, "REFRESH", tpl_getTpl(vars, "REFRESH"));
2775 }
2776 tpl_printf(vars, 0, "CURDATE", "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year%100);
2777 tpl_printf(vars, 0, "CURTIME", "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
2778 st = localtime(&first_client->login);
2779 tpl_printf(vars, 0, "STARTDATE", "%02d.%02d.%02d", st->tm_mday, st->tm_mon+1, st->tm_year%100);
2780 tpl_printf(vars, 0, "STARTTIME", "%02d:%02d:%02d", st->tm_hour, st->tm_min, st->tm_sec);
2781 tpl_printf(vars, 0, "PROCESSID", "%d", server_pid);
2782
2783 time_t now = time((time_t)0);
2784 int lsec = now - first_client->login;
2785 int secs = 0, fullmins = 0, mins = 0, fullhours = 0, hours = 0, days = 0;
2786 if(lsec > 0) {
2787 secs = lsec % 60;
2788 if (lsec > 60) {
2789 fullmins = lsec / 60;
2790 mins = fullmins % 60;
2791 if(fullmins > 60) {
2792 fullhours = fullmins / 60;
2793 hours = fullhours % 24;
2794 days = fullhours / 24;
2795 }
2796 }
2797 }
2798 if(days == 0)
2799 tpl_printf(vars, 0, "UPTIME", "%02d:%02d:%02d", hours, mins, secs);
2800 else
2801 tpl_printf(vars, 0, "UPTIME", "%02dd %02d:%02d:%02d", days, hours, mins, secs);
2802
2803 tpl_printf(vars, 0, "CURIP", "%s", inet_ntoa(*(struct in_addr *)&in));
2804 if(cfg->http_readonly)
2805 tpl_addVar(vars, 1, "BTNDISABLED", "DISABLED");
2806
2807 switch(pgidx) {
2808 case 0: send_oscam_config(vars, f, &params, in); break;
2809 case 1: send_oscam_reader(vars, f, &params, in); break;
2810 case 2: send_oscam_entitlement(vars, f, &params); break;
2811 case 3: send_oscam_status(vars, f, &params, in, 0); break;
2812 case 4: send_oscam_user_config(vars, f, &params, in); break;
2813 case 5: send_oscam_reader_config(vars, f, &params, in); break;
2814 case 6: send_oscam_services(vars, f, &params, in); break;
2815 case 7: send_oscam_user_config_edit(vars, f, &params, in); break;
2816 //case 8: css file
2817 case 9: send_oscam_services_edit(vars, f, &params, in); break;
2818 case 10: send_oscam_savetpls(vars, f); break;
2819 case 11: send_oscam_shutdown(vars, f, &params); break;
2820 case 12: send_oscam_script(vars, f); break;
2821 case 13: send_oscam_scanusb(vars, f); break;
2822 case 14: send_oscam_files(vars, f, &params); break;
2823 case 15: send_oscam_reader_stats(vars, f, &params); break;
2824 case 16: send_oscam_failban(vars, f, &params); break;
2825 //case 8: js file
2826 case 18: send_oscam_api(vars, f, &params, in); break;
2827 default: send_oscam_status(vars, f, &params, in, 0); break;
2828 }
2829 tpl_clear(vars);
2830 }
2831 return 0;
2832}
2833
2834#ifdef WITH_SSL
2835SSL_CTX *webif_init_ssl() {
2836 SSL_library_init();
2837 SSL_load_error_strings();
2838
2839 SSL_METHOD *meth;
2840 SSL_CTX *ctx;
2841
2842 static const char *cs_cert="oscam.pem";
2843
2844 meth = SSLv3_method();
2845
2846 ctx = SSL_CTX_new(meth);
2847
2848 char path[128];
2849
2850 if (cfg->http_cert[0]==0)
2851 sprintf(path, "%s%s", cs_confdir, cs_cert);
2852 else
2853 strcpy(path, cfg->http_cert);
2854
2855 if (!ctx) {
2856 ERR_print_errors_fp(stderr);
2857 return NULL;
2858 }
2859
2860 if (SSL_CTX_use_certificate_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
2861 ERR_print_errors_fp(stderr);
2862 return NULL;
2863 }
2864
2865 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
2866 ERR_print_errors_fp(stderr);
2867 return NULL;
2868 }
2869
2870 if (!SSL_CTX_check_private_key(ctx)) {
2871 cs_log("SSL: Private key does not match the certificate public key");
2872 return NULL;
2873 }
2874 cs_log("load ssl certificate file %s", path);
2875 return ctx;
2876}
2877#endif
2878
2879void http_srv() {
2880 struct s_client * cl = cs_fork(first_client->ip);
2881 if (cl == NULL) return;
2882 cl->thread = pthread_self();
2883 pthread_setspecific(getclient, cl);
2884 strcpy(cl->usr, first_client->usr);
2885 cl->typ = 'h';
2886 int i,sock, reuse = 1;
2887 struct sockaddr_in sin;
2888 struct sockaddr_in remote;
2889 socklen_t len = sizeof(remote);
2890 char *tmp;
2891
2892 /* Prepare lookup array for conversion between ascii and hex */
2893 tmp = malloc(3 * sizeof(char));
2894 for(i = 0; i < 256; i++) {
2895 snprintf(tmp, 3,"%02x", i);
2896 memcpy(hex2ascii[i], tmp, 2);
2897 }
2898 free(tmp);
2899 /* Create random string for nonce value generation */
2900 srand(time(NULL));
2901 create_rand_str(noncekey,32);
2902
2903 /* Startup server */
2904 if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
2905 cs_log("HTTP Server: Creating socket failed! (errno=%d)", errno);
2906 return;
2907 }
2908 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
2909 cs_log("HTTP Server: Setting SO_REUSEADDR via setsockopt failed! (errno=%d)", errno);
2910 }
2911
2912 memset(&sin, 0, sizeof sin);
2913 sin.sin_family = AF_INET;
2914 sin.sin_addr.s_addr = INADDR_ANY;
2915 sin.sin_port = htons(cfg->http_port);
2916 if((bind(sock, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
2917 cs_log("HTTP Server couldn't bind on port %d (errno=%d). Not starting HTTP!", cfg->http_port, errno);
2918 close(sock);
2919 return;
2920 }
2921 if (listen(sock, SOMAXCONN) < 0) {
2922 cs_log("HTTP Server: Call to listen() failed! (errno=%d)", errno);
2923 close(sock);
2924 return;
2925 }
2926 cs_log("HTTP Server listening on port %d%s", cfg->http_port, cfg->http_use_ssl ? " (SSL)" : "");
2927 struct pollfd pfd2[1];
2928 int rc;
2929 pfd2[0].fd = sock;
2930 pfd2[0].events = (POLLIN | POLLPRI);
2931
2932#ifdef WITH_SSL
2933 SSL_CTX *ctx = NULL;
2934 if (cfg->http_use_ssl)
2935 ctx = webif_init_ssl();
2936
2937 if (ctx==NULL)
2938 cfg->http_use_ssl = 0;
2939#endif
2940
2941 while (running) {
2942 int s;
2943
2944 rc = poll(pfd2, 1, 1000);
2945
2946 if (rc > 0) {
2947 if((s = accept(sock, (struct sockaddr *) &remote, &len)) < 0) {
2948 cs_log("HTTP Server: Error calling accept() (errno=%d).", errno);
2949 break;
2950 }
2951#ifdef WITH_SSL
2952 if (cfg->http_use_ssl) {
2953 SSL *ssl;
2954 ssl = SSL_new(ctx);
2955 SSL_set_fd(ssl, s);
2956 if (SSL_accept(ssl) != -1)
2957 process_request((FILE *)ssl, remote.sin_addr);
2958 else {
2959 cfg->http_use_ssl=0;
2960 FILE *f;
2961 f = fdopen(s, "r+");
2962 send_error(f, 200, "Bad Request", NULL, "This web server is running in SSL mode.");
2963 fclose(f);
2964 cfg->http_use_ssl=1;
2965 }
2966 SSL_shutdown(ssl);
2967 close(s);
2968 SSL_free(ssl);
2969 } else
2970#endif
2971 {
2972 FILE *f;
2973 f = fdopen(s, "r+");
2974 process_request(f, remote.sin_addr);
2975 fflush(f);
2976 fclose(f);
2977 shutdown(s, SHUT_WR);
2978 close(s);
2979 }
2980 }
2981 }
2982#ifdef WITH_SSL
2983 if (cfg->http_use_ssl)
2984 SSL_CTX_free(ctx);
2985#endif
2986 cs_log("HTTP Server: Shutdown requested from %s", inet_ntoa(*(struct in_addr *)&remote.sin_addr));
2987 close(sock);
2988 exit(SIGQUIT);
2989}
2990#endif
Note: See TracBrowser for help on using the repository browser.