blob: b0007fd504cfa7d7c1ac2b413d9480f6328a26ec [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include "uemf.h"
2#include "wsIntrn.h"
3#include <signal.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7#include <sched.h>
8#ifdef WEBS_SSL_SUPPORT
9#include "websSSL.h"
10#endif
11#include "../interface5.0/zte_web_interface.h"
12
13static char_t *rootWeb = T("etc_ro/web"); /* Root web directory */
14static int port = 80; /* Server port */
15static int retries = 5; /* Server port retries */
16static int finished; /* Finished flag */
17
18/****************************** Forward Declarations **************************/
19
20static int initWebs();
21static int websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,
22 int arg, char_t *url, char_t *path, char_t *query);
23extern void defaultErrorHandler(int etype, char_t *msg);
24extern void defaultTraceHandler(int level, char_t *buf);
25
26//added by liuyingnan for PC Client begin, 20120829
27extern int websXMLPostHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg, char_t *url, char_t *path, char_t *query);
28//added by liuyingnan for PC Client end, 20120829
29
30int main(int argc, char** argv)
31{
32 int rs;
33 struct sched_param param;
34
35 loglevel_init();
36 slog(MISC_PRINT, SLOG_DEBUG, "goahead main\n");
37 bopen(NULL, (60 * 1024), B_USE_MALLOC);
38 signal(SIGPIPE, SIG_IGN);
39 if (initWebs() < 0) {
40 slog(MISC_PRINT, SLOG_ERR, "goahead initWebs failed.");
41 return -1;
42 }
43
44#ifdef WEBS_SSL_SUPPORT
45 websSSLOpen();
46#endif
47 web_init_pwd();
48 web_aes_init();
49
50 slog(MISC_PRINT, SLOG_NORMAL,"[goahead] enter working\n");
51 while (!finished) {
52 if (socketReady(-1) || socketSelect(-1, 1000)) {
53 socketProcess(-1);
54 }
55 websCgiCleanup();
56 emfSchedProcess();
57 }
58
59#ifdef WEBS_SSL_SUPPORT
60 websSSLClose();
61#endif
62
63 websCloseServer();
64 socketClose();
65 bclose();
66 return 0;
67}
68
69static int initWebs()
70{
71 struct in_addr intaddr;
72 const char lan_ip[128] = {0};
73 char host[128], dir[128], webdir[128];
74 char *cp;
75 char_t wbuf[128];
76 socketOpen();
77
78 sc_cfg_get("lan_ipaddr", lan_ip, sizeof(lan_ip));
79 if (0 == strcmp(lan_ip, "")) {
80 slog(MISC_PRINT, SLOG_ERR, "goahead initWebs failed: lan_ip is NULL!");
81 return -1;
82 }
83 intaddr.s_addr = inet_addr(lan_ip);
84 if (intaddr.s_addr == INADDR_NONE) {
85 slog(MISC_PRINT, SLOG_ERR, "goahead initWebs failed: lan_ip is %s, INADDR_NONE!", lan_ip);
86 return -1;
87 }
88 getcwd(dir, sizeof(dir));
89 if ((cp = strrchr(dir, '/'))) {
90 *cp = '\0';
91 }
92 snprintf(webdir, sizeof(webdir), "%s/%s", dir, rootWeb);
93 slog(MISC_PRINT,SLOG_DEBUG, "goahead initWebs info: webdir is %s!",webdir);
94 websSetDefaultDir(webdir);
95 cp = inet_ntoa(intaddr);
96 ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf)));
97 websSetIpaddr(wbuf);
98 websSetHost(wbuf);
99 websSetDefaultPage(T(ZTE_WEB_PAGE_LOGIN_NAME));
100 websOpenServer(port, retries);
101 //websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, WEBS_HANDLER_FIRST);
102 websUrlHandlerDefine(T("/reqproc"), NULL, 0, websFormHandler, 0);
103#ifndef WEBS_SECURITY
104 websUrlHandlerDefine(T("/cgi-bin"), NULL, 0, websCgiHandler, 0);
105 //websUrlHandlerDefine(T("/api/xmlclient/post"), NULL, 0, websXMLPostHandler, 0);
106 websUrlHandlerDefine(T("/mmc2"), NULL, 0, websCgiDownLoadHandler, 0);//http share
107#endif
108 websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, WEBS_HANDLER_LAST);
109 zte_web_init();
110 zte_httpshare_init();//httpshare
111 websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0);
112
113 system("rm -rf /firmware_tmp_file");
114 return 0;
115}
116
117static int websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,
118 int arg, char_t *url, char_t *path, char_t *query)
119{
120 if (*url == '\0' || gstrcmp(url, T("/")) == 0)
121 {
122 websRedirect(wp, zte_web_get_login_page(wp)); /*×ÔÊÊÓ¦ÖÕ¶Ëä¯ÀÀÆ÷*/
123 return 1;
124 }
125 return 0;
126}
127
128void defaultErrorHandler(int etype, char_t *msg)
129{
130 printf("[goahead]e %s\n",msg);
131}
132
133void defaultTraceHandler(int level, char_t *buf)
134{
135 if (buf) {
136 printf("[goahead]t %s\n",buf);
137 }
138}
139
140char_t *websGetCgiCommName(webs_t wp)
141{
142 char_t *pname1, *pname2;
143
144 pname1 = (char_t *)tempnam(T("/var"), T("cgi"));
145 pname2 = bstrdup(B_L, pname1);
146 free(pname1);
147 return pname2;
148}
149
150int websLaunchCgiProc(char_t *cgiPath, char_t **argp, char_t **envp,
151 char_t *stdIn, char_t *stdOut)
152{
153 int pid, fdin, fdout, hstdin, hstdout, rc;
154
155 fdin = fdout = hstdin = hstdout = rc = -1;
156 if ((fdin = open(stdIn, O_RDWR | O_CREAT, 0666)) < 0 ||
157 (fdout = open(stdOut, O_RDWR | O_CREAT, 0666)) < 0 ||
158 (hstdin = dup(0)) == -1 ||
159 (hstdout = dup(1)) == -1 ||
160 dup2(fdin, 0) == -1 ||
161 dup2(fdout, 1) == -1) {
162 goto DONE;
163 }
164
165 rc = pid = fork();
166 if (pid == 0) {
167 int result = execve(cgiPath, argp, envp);
168 slog(MISC_PRINT,SLOG_DEBUG, "content-type: text/html Execution of cgi process :%d", result);
169 exit (0);
170 }
171
172DONE:
173 if (hstdout >= 0) {
174 dup2(hstdout, 1);
175 close(hstdout);
176 }
177 if (hstdin >= 0) {
178 dup2(hstdin, 0);
179 close(hstdin);
180 }
181 if (fdout >= 0) {
182 close(fdout);
183 }
184 if (fdin >= 0) {
185 close(fdin);
186 }
187 return rc;
188}
189
190int websCheckCgiProc(int handle, int *status)
191{
192 if (waitpid(handle, status, WNOHANG) == handle) {
193 return 0;
194 } else {
195 return 1;
196 }
197}
198
199static int i64c(int i)
200{
201 i &= 0x3f;
202 if (i == 0)
203 return '.';
204 if (i == 1)
205 return '/';
206 if (i < 12)
207 return ('0' - 2 + i);
208 if (i < 38)
209 return ('A' - 12 + i);
210 return ('a' - 38 + i);
211}
212
213static int web_make_salt(char *p, int cnt /*, int x */)
214{
215 /* was: x += ... */
216 int x = getpid() + (int)time(0);
217 do {
218 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
219 * (low-order bit is not "random", etc...),
220 * but for our purposes it is good enough */
221 x = x*1664525 + 1013904223;
222 /* BTW, Park and Miller's "minimal standard generator" is
223 * x = x*16807 % ((2^31)-1)
224 * It has no problem with visibly alternating lowest bit
225 * but is also weak in cryptographic sense + needs div,
226 * which needs more code (and slower) on many CPUs */
227 *p++ = i64c(x >> 16);
228 *p++ = i64c(x >> 22);
229 } while (--cnt);
230 *p = '\0';
231 return x;
232}
233
234int web_set_pwd(char *buf)
235{
236 if(buf == NULL)
237 return -1;
238 int bufLen = strlen(buf);
239 if(bufLen <= 0 || bufLen > LOGIN_PSW_MAX_LEN)
240 return -1;
241#ifdef WEBS_SSL_SUPPORT
242 char salt[LOGIN_PSW_MAX_LEN+1]={0};
243 web_make_salt(salt,(sizeof(salt)-1)/2);
244 slog(MISC_PRINT,SLOG_DEBUG, "[goahead]salt %s\n",salt);
245 unsigned char md[LOGIN_PSW_MAX_LEN] = {0};
246 {
247 unsigned char src[LOGIN_PSW_MAX_LEN*2+1] = {0};
248 memcpy(src,salt,LOGIN_PSW_MAX_LEN);
249 memcpy(src+LOGIN_PSW_MAX_LEN,buf,bufLen);
250 SHA256((const unsigned char *)src, bufLen+LOGIN_PSW_MAX_LEN, md);
251 }
252 {
253 char i = 0;
254 char str_md[LOGIN_PSW_MAX_LEN*2+1] = {0};
255 for(i = 0; i < LOGIN_PSW_MAX_LEN; i++)
256 {
257 sprintf(str_md+i+i,"%02x", md[i]);
258 }
259 char psw_buf[100] = {0};
260 snprintf(psw_buf,sizeof(psw_buf),"%s%s",salt,str_md);
261 slog(MISC_PRINT,SLOG_DEBUG, "goahead]set_pw %s\n",psw_buf);
262 return sc_cfg_set("admin_Password", psw_buf);
263 }
264#else
265 return sc_cfg_set("admin_Password", buf);
266#endif
267}
268
269void web_init_pwd(void)
270{
271#ifdef WEBS_SSL_SUPPORT
272 char psw_buf[LOGIN_PSW_MAX_LEN*3+1] = {0};
273 sc_cfg_get("admin_Password", psw_buf, sizeof(psw_buf));
274 slog(MISC_PRINT,SLOG_DEBUG, "[goahead]init_pw %s\n",psw_buf);
275 if(strlen(psw_buf) <= LOGIN_PSW_MAX_LEN)
276 {
277 web_set_pwd(psw_buf);
278 }
279#endif
280}
281
282int web_check_pwd(char* buf)
283{
284 char psw_buf[LOGIN_PSW_MAX_LEN*3+1] = {0};
285 if(buf == NULL)
286 return -1;
287 int bufLen = strlen(buf);
288 if(bufLen <= 0 || bufLen > LOGIN_PSW_MAX_LEN)
289 return -1;
290 sc_cfg_get("admin_Password", psw_buf, sizeof(psw_buf));
291 slog(MISC_PRINT,SLOG_DEBUG, "[goahead]check_pw in %s\n",buf);
292 slog(MISC_PRINT,SLOG_DEBUG, "[goahead]check_pw src %s\n",psw_buf);
293#ifdef WEBS_SSL_SUPPORT
294 unsigned char md[LOGIN_PSW_MAX_LEN] = {0};
295 {
296 unsigned char src[LOGIN_PSW_MAX_LEN*2+1] = {0};
297 memcpy(src,psw_buf,LOGIN_PSW_MAX_LEN);
298 memcpy(src+LOGIN_PSW_MAX_LEN,buf,bufLen);
299 SHA256((const unsigned char *)src, bufLen+LOGIN_PSW_MAX_LEN, md);
300 }
301 {
302 char i = 0;
303 char str_md[65] = {0};
304 for(i = 0; i < LOGIN_PSW_MAX_LEN; i++)
305 {
306 sprintf(str_md+i+i,"%02x", md[i]);
307 }
308 return strncmp(str_md,psw_buf+LOGIN_PSW_MAX_LEN,sizeof(str_md)-1);
309 }
310#else
311 if(bufLen != strlen(psw_buf))
312 return -1;
313
314 return strncmp(buf,psw_buf,bufLen);;
315#endif
316}
317
318int web_make_salt_base64(char *buf, int len)
319{
320 int salt_len;
321 char *salt;
322 if(buf == NULL || len < 5)
323 {
324 return 0;
325 }
326 salt_len = (len - 1)/4*3;
327 salt = balloc(B_L, salt_len+1);
328 if(salt == NULL)
329 {
330 return 0;
331 }
332 web_make_salt(salt,salt_len/2);
333 websEncode64(buf, salt, len);
334 bfree(B_L, salt);
335 *(buf + len -1) = '\0';
336 slog(MISC_PRINT,SLOG_DEBUG, "[goahead]salt_base64=%s\n",buf);
337 return strlen(buf);
338}
339