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

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

WebIf:

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