http_server.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. #include "lwip/opt.h"
  2. #include "lwip/arch.h"
  3. #include "lwip/api.h"
  4. #include "lwip/tcp.h"
  5. #include "http_server.h"
  6. #include "web_params_api.h"
  7. #include "parameters.h"
  8. #include "urlcode.h"
  9. #include "trap_params.h"
  10. #include "fsdata.c"
  11. #include "settings_api.h"
  12. #include "netconf.h"
  13. #include "common_config.h"
  14. //#include "testing.h"
  15. #include "rtc.h"
  16. #include "rng.h"
  17. #include "megatec.h"
  18. #include "log.h"
  19. #include "hal.h"
  20. #ifdef PRINTF_STDLIB
  21. #include <stdio.h>
  22. #endif
  23. #ifdef PRINTF_CUSTOM
  24. #include "tinystdio.h"
  25. #endif
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "FreeRTOS.h"
  29. #include "task.h"
  30. #include "fr_timers.h"
  31. static int fs_open(char *name, struct fs_file *file);
  32. static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len);
  33. static err_t http_sent_history(void *arg, struct tcp_pcb *pcb, u16_t len);
  34. static err_t http_sent_log(void *arg, struct tcp_pcb *pcb, u16_t len);
  35. static void http_sent_log_err(void * arg, err_t err);
  36. static void send_data(struct tcp_pcb *pcb, struct http_state *hs);
  37. static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len);
  38. static uint32_t Parse_Content_Length(char *data, uint32_t len);
  39. static void HTTP_SetUserCookie(char *str, uint8_t user_id);
  40. static void HTTP_UpdateUserLoginTime(uint8_t user_id);
  41. static void HTTP_ForceUserLogout(uint8_t user_id);
  42. void LogoutTimerCallback(TimerHandle_t pxTimer);
  43. int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
  44. SET_PAGE_t SET_PAGE = SET_PAGE_IDLE;
  45. #define SEND_BUF_MAX_LEN 2000
  46. #define RECIVE_BUF_MAX_LEN 1500
  47. char sendBuf[SEND_BUF_MAX_LEN];
  48. uint16_t sendBufLoadLen = 0;
  49. uint16_t printLen = 0;
  50. //char printBuf[1000];
  51. char receiveBuf[RECIVE_BUF_MAX_LEN];
  52. uint16_t receivedBufLen = 0;
  53. #define MAX_POST_REQ_LEN 256
  54. char post_req_data[MAX_POST_REQ_LEN];
  55. uint32_t post_data_count;
  56. uint32_t log_post_reqn;
  57. /* Logout timeout, 30 minutes */
  58. #define WEB_LOGOUT_TIME configTICK_RATE_HZ*60*30
  59. /* Max user active sessions count */
  60. #define WEB_USER_MAX_SESSION_COUNT 5
  61. typedef struct {
  62. char cookie[MAX_WEB_COOKIE_LEN];
  63. TimerHandle_t LogoutTimer;
  64. } auth_session_t;
  65. struct {
  66. //auth_session_t session[WEB_USER_MAX_SESSION_COUNT];
  67. char cookie[MAX_WEB_COOKIE_LEN];
  68. TimerHandle_t LogoutTimer;
  69. } users[MAX_WEB_USERS];
  70. bool Authenticated = false;
  71. /* Level of currently logged-in user */
  72. uint8_t seclevel = 0xFF;
  73. static volatile uint32_t DataFlag2=0;
  74. static volatile uint32_t size =0;
  75. static uint32_t TotalReceived=0;
  76. static volatile uint32_t TotalData=0;
  77. static uint32_t ContentLengthOffset =0, BrowserFlag=0;
  78. static const char Content_Length[17] =
  79. /* Content Length */
  80. {0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67,0x74, 0x68, 0x3a, 0x20, };
  81. const char HTTP_200_OK[] = "HTTP/1.1 200 OK\r\n\r\n";
  82. /* utf-8 marker to support MS Excel */
  83. const char UTF8_BOM[] = {0xEF, 0xBB, 0xBF, 0x00};
  84. unsigned long log_ptr = 0;
  85. unsigned long log_size = 0;
  86. bool fLogTransInprog = false;
  87. /**
  88. * @brief Общая структура настроек
  89. */
  90. extern SETTINGS_t sSettings;
  91. /**
  92. * @brief closes tcp connection
  93. * @param pcb: pointer to a tcp_pcb struct
  94. * @param hs: pointer to a http_state struct
  95. * @retval
  96. */
  97. static void close_conn(struct tcp_pcb *pcb, struct http_state *hs)
  98. {
  99. tcp_arg(pcb, NULL);
  100. tcp_sent(pcb, NULL);
  101. tcp_recv(pcb, NULL);
  102. mem_free(hs);
  103. tcp_close(pcb);
  104. }
  105. /**
  106. * @brief callback function for handling TCP HTTP traffic
  107. * @param arg: pointer to an argument structure to be passed to callback function
  108. * @param pcb: pointer to a tcp_pcb structure
  109. * @param p: pointer to a packet buffer
  110. * @param err: LwIP error code
  111. * @retval err
  112. */
  113. static err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  114. {
  115. char *data, *ptr;
  116. struct http_state *hs;
  117. char CookieBuf[50];
  118. char *CookiePtr = NULL;
  119. char name[MAX_WEB_COOKIE_LEN];
  120. char id[MAX_WEB_COOKIE_LEN];
  121. uint8_t nameLen = 0, idLen = 0;
  122. uint32_t DataOffset;
  123. struct fs_file file = {0, 0};
  124. uint32_t i;
  125. hs = arg;
  126. if (err == ERR_OK && p != NULL)
  127. {
  128. tcp_recved(pcb, p->tot_len);
  129. if (hs->file == NULL)
  130. {
  131. data = p->payload;
  132. /*
  133. printLen = p->tot_len;
  134. memcpy(printBuf, p->payload , printLen);
  135. printf(printBuf);
  136. */
  137. receivedBufLen = p->tot_len;
  138. memcpy(receiveBuf, p->payload , receivedBufLen);
  139. receiveBuf[receivedBufLen] = '\0';
  140. // printf("receive %s \r\n", receiveBuf);
  141. /* Get cookie "uname" value */
  142. CookiePtr = strstr(receiveBuf, "uname=");
  143. strncpy(CookieBuf, CookiePtr, 50);
  144. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  145. memset(name, 0, MAX_WEB_COOKIE_LEN);
  146. GetCookieValue(CookieBuf, "uname=", name, &nameLen);
  147. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  148. //printf("********uname= %s\r\n", name);
  149. /* Get cookie "id" value */
  150. CookiePtr = strstr(receiveBuf, "id=");
  151. strncpy(CookieBuf, CookiePtr, 50);
  152. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  153. memset(id, 0, MAX_WEB_COOKIE_LEN);
  154. GetCookieValue(CookieBuf, "id=", id, &idLen);
  155. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  156. //printf("********id= %s\r\n", id);
  157. /* Id of currently logged-in user */
  158. uint8_t user_id;
  159. if(DataFlag2 == 0){
  160. /* Level of currently logged-in user */
  161. seclevel = 0xFF;
  162. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  163. HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
  164. if (strncmp(id, CookieBuf, idLen) == 0 ) {
  165. GetUserLevelInt(user_id, &seclevel);
  166. Authenticated = true;
  167. break;
  168. }
  169. Authenticated = false;
  170. seclevel = 0xFF;
  171. }
  172. }
  173. if(DataFlag2 >= 1)
  174. Authenticated = true;
  175. if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
  176. {
  177. HTTP_LOGIN(sendBuf, &sendBufLoadLen);
  178. hs->file = sendBuf;
  179. hs->left = sendBufLoadLen;
  180. send_data(pcb, hs);
  181. tcp_sent(pcb, http_sent);
  182. }
  183. else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
  184. {
  185. if (strncmp(data, "GET /main.css", 13) == 0) // +
  186. {
  187. fs_open("/main.css", &file);
  188. hs->file = file.data;
  189. hs->left = file.len;
  190. send_data(pcb, hs);
  191. tcp_sent(pcb, http_sent);
  192. }
  193. else if (strncmp(data, "GET /rotek.png", 14) == 0) // +
  194. {
  195. fs_open("/rotek.png", &file);
  196. hs->file = file.data;
  197. hs->left = file.len;
  198. send_data(pcb, hs);
  199. tcp_sent(pcb, http_sent);
  200. }
  201. else if (strncmp(data, "GET /favicon.ico", 16) == 0) // ?
  202. {
  203. fs_open("/favicon.ico", &file);
  204. hs->file = file.data;
  205. hs->left = file.len;
  206. send_data(pcb, hs);
  207. tcp_sent(pcb, http_sent);
  208. }
  209. else if (strncmp(data, "GET /role.js", 12) == 0)
  210. {
  211. fs_open("/role.js", &file);
  212. hs->file = file.data;
  213. hs->left = file.len;
  214. send_data(pcb, hs);
  215. tcp_sent(pcb, http_sent);
  216. }
  217. else if ((strncmp(data, "POST /login.cgi", 15) == 0) || (log_post_reqn > 0))
  218. {
  219. uint32_t i, offset = 0, req_data_received = 0;
  220. //printf("request 1: %d\r\n", receivedBufLen);
  221. /* parse packet for Content-length field */
  222. post_data_count = Parse_Content_Length(data, p->tot_len);
  223. //printf("Content-length: %d\r\n", (int)post_data_count);
  224. if (post_data_count < MAX_POST_REQ_LEN) {
  225. memset(post_req_data, 0, MAX_POST_REQ_LEN);
  226. /* parse packet for "\r\n\r\n" */
  227. for (i = 0; i < receivedBufLen; i++)
  228. {
  229. if (strncmp ((char*)(data+i), "\r\n\r\n", 4) == 0)
  230. {
  231. offset = i+4;
  232. //printf("offset: %d\r\n", (int)offset);
  233. break;
  234. }
  235. }
  236. req_data_received = receivedBufLen - offset;
  237. //printf("req data received: %d\r\n", (int)req_data_received);
  238. /* Check if "\r\n\r\n" was found */
  239. if (offset != 0) {
  240. /* if data was splited in two packets */
  241. if (req_data_received < post_data_count) {
  242. /* Copy request data to buffer */
  243. snprintf(post_req_data, req_data_received, "%s", receiveBuf);
  244. //printf("copied: %d\r\n", (int)req_data_received);
  245. post_data_count -= req_data_received;
  246. }
  247. /* if data received completely */
  248. else {
  249. strncat(post_req_data, (char *)(data + offset), post_data_count);
  250. //printf("post_req_data: %s\r\n", post_req_data);
  251. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) {
  252. hs->file = sendBuf;
  253. hs->left = sendBufLoadLen;
  254. send_data(pcb, hs);
  255. tcp_sent(pcb, http_sent);
  256. }
  257. else {
  258. /* Redirect to login page */
  259. fs_open("/login.html", &file);
  260. hs->file = file.data;
  261. hs->left = file.len;
  262. send_data(pcb, hs);
  263. tcp_sent(pcb, http_sent);
  264. }
  265. /* End reqest */
  266. post_data_count = 0;
  267. log_post_reqn = 0;
  268. }
  269. }
  270. /* request was fragmented before "\r\n\r\n" */
  271. else {
  272. //printf("no data found!\r\n");
  273. /* wait next packet */
  274. log_post_reqn++;
  275. /* wait max 2 requests */
  276. if (log_post_reqn > 1) {
  277. /* Redirect to login page */
  278. fs_open("/login.html", &file);
  279. hs->file = file.data;
  280. hs->left = file.len;
  281. send_data(pcb, hs);
  282. tcp_sent(pcb, http_sent);
  283. /* End reqest */
  284. post_data_count = 0;
  285. log_post_reqn = 0;
  286. }
  287. }
  288. }
  289. else {
  290. printf("Too long POST request!\r\n");
  291. /* Ignore request */
  292. post_data_count = 0;
  293. log_post_reqn = 0;
  294. /* Redirect to login page */
  295. fs_open("/login.html", &file);
  296. hs->file = file.data;
  297. hs->left = file.len;
  298. send_data(pcb, hs);
  299. tcp_sent(pcb, http_sent);
  300. }
  301. }
  302. else if (post_data_count > 0)
  303. {
  304. strncat(post_req_data, data, post_data_count);
  305. //printf("copied: %d\r\n", (int)post_data_count);
  306. //printf("post_req_data: %s\r\n", post_req_data);
  307. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) {
  308. hs->file = sendBuf;
  309. hs->left = sendBufLoadLen;
  310. send_data(pcb, hs);
  311. tcp_sent(pcb, http_sent);
  312. }
  313. else {
  314. /* Redirect to login page */
  315. fs_open("/login.html", &file);
  316. hs->file = file.data;
  317. hs->left = file.len;
  318. send_data(pcb, hs);
  319. tcp_sent(pcb, http_sent);
  320. }
  321. /* End reqest */
  322. post_data_count = 0;
  323. log_post_reqn = 0;
  324. }
  325. else
  326. {
  327. fs_open("/login.html", &file);
  328. hs->file = file.data;
  329. hs->left = file.len;
  330. send_data(pcb, hs);
  331. tcp_sent(pcb, http_sent);
  332. }
  333. }
  334. else if ( Authenticated == true ) {
  335. if (strncmp(data, "GET /main.css", 13) == 0) // +
  336. {
  337. fs_open("/main.css", &file);
  338. hs->file = file.data;
  339. hs->left = file.len;
  340. send_data(pcb, hs);
  341. tcp_sent(pcb, http_sent);
  342. }
  343. else if (strncmp(data, "GET /rotek.png", 14) == 0) // +
  344. {
  345. fs_open("/rotek.png", &file);
  346. hs->file = file.data;
  347. hs->left = file.len;
  348. send_data(pcb, hs);
  349. tcp_sent(pcb, http_sent);
  350. }
  351. else if (strncmp(data, "GET /favicon.ico", 16) == 0) // ?
  352. {
  353. fs_open("/favicon.ico", &file);
  354. hs->file = file.data;
  355. hs->left = file.len;
  356. send_data(pcb, hs);
  357. tcp_sent(pcb, http_sent);
  358. }
  359. else if (strncmp(data, "GET /main.js", 12) == 0) // +
  360. {
  361. fs_open("/main.js", &file);
  362. hs->file = file.data;
  363. hs->left = file.len;
  364. send_data(pcb, hs);
  365. tcp_sent(pcb, http_sent);
  366. }
  367. else if (strncmp(data, "GET /role.js", 12) == 0)
  368. {
  369. fs_open("/role.js", &file);
  370. hs->file = file.data;
  371. hs->left = file.len;
  372. send_data(pcb, hs);
  373. tcp_sent(pcb, http_sent);
  374. }
  375. else if (strncmp(data, "GET /settings.html", 18) == 0) // +
  376. {
  377. HTTP_UpdateUserLoginTime(user_id);
  378. if (seclevel == 0){
  379. fs_open("/settings.html", &file);
  380. hs->file = file.data;
  381. hs->left = file.len;
  382. send_data(pcb, hs);
  383. tcp_sent(pcb, http_sent);
  384. }
  385. else {
  386. fs_open("/index.html", &file);
  387. hs->file = file.data;
  388. hs->left = file.len;
  389. send_data(pcb, hs);
  390. tcp_sent(pcb, http_sent);
  391. }
  392. }
  393. else if (strncmp(data, "GET /info.html", 14) == 0) // +
  394. {
  395. HTTP_UpdateUserLoginTime(user_id);
  396. fs_open("/info.html", &file);
  397. hs->file = file.data;
  398. hs->left = file.len;
  399. send_data(pcb, hs);
  400. tcp_sent(pcb, http_sent);
  401. }
  402. else if (strncmp(data, "GET /history.html", 17) == 0)
  403. {
  404. HTTP_UpdateUserLoginTime(user_id);
  405. fs_open("/history.html", &file);
  406. hs->file = file.data;
  407. hs->left = file.len;
  408. send_data(pcb, hs);
  409. tcp_sent(pcb, http_sent);
  410. }
  411. else if (strncmp(data, "GET /ups_history.html", 21) == 0)
  412. {
  413. HTTP_UpdateUserLoginTime(user_id);
  414. fs_open("/ups_history.html", &file);
  415. hs->file = file.data;
  416. hs->left = file.len;
  417. send_data(pcb, hs);
  418. tcp_sent(pcb, http_sent);
  419. }
  420. else if (strncmp(data, "GET /getJson.cgi", 16) == 0) // +
  421. {
  422. HTTP_GetParamsPage1(sendBuf);
  423. hs->file = sendBuf;
  424. hs->left = strlen(sendBuf);
  425. send_data(pcb, hs);
  426. tcp_sent(pcb, http_sent);
  427. }
  428. else if (strncmp(data, "GET /settings.cgi", 17) == 0) // +
  429. {
  430. if (seclevel == 0) {
  431. SET_PAGE = SET_PAGE_PAGE2;
  432. if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  433. {
  434. hs->file = sendBuf;
  435. hs->left = sendBufLoadLen;
  436. send_data(pcb, hs);
  437. tcp_sent(pcb, http_sent);
  438. }
  439. /*else
  440. {
  441. fs_open("/settings.html", &file);
  442. hs->file = file.data;
  443. hs->left = file.len;
  444. send_data(pcb, hs);
  445. tcp_sent(pcb, http_sent);
  446. }*/
  447. }
  448. }
  449. else if (strncmp(data, "POST /settings.cgi", 18) == 0 || (DataFlag2 >= 1))
  450. {
  451. if (seclevel == 0) {
  452. DataOffset = 0;
  453. /* POST Packet received */
  454. if (DataFlag2 == 0)
  455. {
  456. BrowserFlag = 0;
  457. TotalReceived = 0;
  458. memset(sendBuf, 0, strlen(sendBuf));
  459. /* parse packet for Content-length field */
  460. size = Parse_Content_Length(data, p->tot_len);
  461. /* parse packet for the octet-stream field */
  462. for (i = 0; i < receivedBufLen; i++)
  463. {
  464. if (strncmp ((char*)(data+i), "managerIP", 8)==0)
  465. {
  466. DataOffset = i;
  467. break;
  468. }
  469. }
  470. /* case of MSIE8 : we do not receive data in the POST packet*/
  471. if (DataOffset == 0)
  472. {
  473. DataFlag2++;
  474. BrowserFlag = 1;
  475. pbuf_free(p);
  476. return ERR_OK;
  477. }
  478. /* case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
  479. else
  480. {
  481. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  482. TotalReceived = receivedBufLen - DataOffset;
  483. }
  484. }
  485. if (((DataFlag2 ==1)&&(BrowserFlag==1)) || ((DataFlag2 ==0)&&(BrowserFlag==0)))
  486. {
  487. if ((DataFlag2 ==0)&&(BrowserFlag==0))
  488. {
  489. DataFlag2++;
  490. }
  491. else if ((DataFlag2 ==1)&&(BrowserFlag==1))
  492. {
  493. /* parse packet for the octet-stream field */
  494. for (i = 0; i < receivedBufLen; i++)
  495. {
  496. if (strncmp ((char*)(data+i), "managerIP", 8)==0)
  497. {
  498. DataOffset = i;
  499. break;
  500. }
  501. }
  502. TotalReceived += receivedBufLen;
  503. DataFlag2++;
  504. }
  505. TotalData =0 ;
  506. }
  507. /* DataFlag >1 => the packet is data only */
  508. else
  509. {
  510. TotalReceived +=receivedBufLen;
  511. }
  512. ptr = (char*)(data + DataOffset);
  513. receivedBufLen-= DataOffset;
  514. /* update Total data received counter */
  515. TotalData +=receivedBufLen;
  516. /* check if last data packet */
  517. if (TotalReceived == size)
  518. {
  519. //DBG printf("State: Received %d bytes\r\n", (int)TotalReceived);
  520. strncat(&sendBuf, ptr, receivedBufLen);
  521. strncat(&sendBuf, " ", 1);
  522. //ВBG printf("receive %s /r/n", sendBuf);
  523. HTTP_SetSettings(sendBuf, strlen(&sendBuf));
  524. DataFlag2=0;
  525. memset(sendBuf, 0, size);
  526. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  527. strcat(sendBuf, "\r\n\r\n");
  528. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/settings.html\"/></head></html>\r\n\r\n");
  529. sendBufLoadLen = strlen(sendBuf);
  530. hs->file = sendBuf;
  531. hs->left = sendBufLoadLen;
  532. send_data(pcb, hs);
  533. /* Tell TCP that we wish be to informed of data that has been
  534. successfully sent by a call to the http_sent() function. */
  535. tcp_sent(pcb, http_sent);
  536. }
  537. /* not last data packet */
  538. else
  539. {
  540. /* write data in flash */
  541. if(receivedBufLen)
  542. {
  543. strncat(&sendBuf, ptr, receivedBufLen);
  544. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  545. }
  546. }
  547. }
  548. }
  549. else if (strncmp(data, "GET /info.cgi", 13) == 0) // +
  550. {
  551. if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  552. {
  553. hs->file = sendBuf;
  554. hs->left = sendBufLoadLen;
  555. send_data(pcb, hs);
  556. tcp_sent(pcb, http_sent);
  557. }
  558. /* else
  559. {
  560. fs_open("/info.html", &file);
  561. hs->file = file.data;
  562. hs->left = file.len;
  563. send_data(pcb, hs);
  564. tcp_sent(pcb, http_sent);
  565. }*/
  566. }
  567. else if (strncmp(data, "POST /info.cgi", 14) == 0)
  568. {
  569. //strncat(&receiveBuf, " ", 1);
  570. strncat(receiveBuf, " ", 1);
  571. HTTP_SetInfo(receiveBuf, receivedBufLen);
  572. memset(sendBuf, 0, SEND_BUF_MAX_LEN);
  573. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  574. strcat(sendBuf, "\r\n\r\n");
  575. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/info.html\"/></head></html>\r\n\r\n");
  576. sendBufLoadLen = strlen(sendBuf);
  577. hs->file = sendBuf;
  578. hs->left = sendBufLoadLen;
  579. send_data(pcb, hs);
  580. tcp_sent(pcb, http_sent);
  581. }
  582. else if (strncmp(data, "GET /history.cgi", 16) == 0)
  583. {
  584. int res;
  585. res = HTTP_HistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  586. if (res == SEND_REQUIRED_FILE)
  587. {
  588. hs->file = sendBuf;
  589. hs->left = sendBufLoadLen;
  590. send_data(pcb, hs);
  591. tcp_sent(pcb, http_sent_history);
  592. tcp_err(pcb, http_sent_log_err);
  593. }
  594. else if (res == SEND_REQUIRED_YES) {
  595. hs->file = sendBuf;
  596. hs->left = sendBufLoadLen;
  597. send_data(pcb, hs);
  598. tcp_sent(pcb, http_sent);
  599. }
  600. }
  601. else if (strncmp(data, "GET /ups_history.cgi", 19) == 0)
  602. {
  603. int res;
  604. res = HTTP_UpsHistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  605. if (res == SEND_REQUIRED_FILE)
  606. {
  607. hs->file = sendBuf;
  608. hs->left = sendBufLoadLen;
  609. send_data(pcb, hs);
  610. tcp_sent(pcb, http_sent_log);
  611. tcp_err(pcb, http_sent_log_err);
  612. }
  613. else if (res == SEND_REQUIRED_YES) {
  614. hs->file = sendBuf;
  615. hs->left = sendBufLoadLen;
  616. send_data(pcb, hs);
  617. tcp_sent(pcb, http_sent);
  618. }
  619. }
  620. /* Тест АКБ ИБП */
  621. else if (strncmp(data, "POST /bat_test.cgi", 18) == 0)
  622. {
  623. HTTP_UPSTest(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  624. hs->file = sendBuf;
  625. hs->left = sendBufLoadLen;
  626. send_data(pcb, hs);
  627. tcp_sent(pcb, http_sent);
  628. }
  629. /* Выключение ИБП */
  630. else if (strncmp(data, "POST /ups_power.cgi", 19) == 0)
  631. {
  632. HTTP_UPSshutdown(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  633. hs->file = sendBuf;
  634. hs->left = sendBufLoadLen;
  635. send_data(pcb, hs);
  636. tcp_sent(pcb, http_sent);
  637. }
  638. /* Сброс настроек и сохранине */
  639. else if (strncmp(data, "GET /reset.cgi", 14) == 0)
  640. {
  641. HTTP_ResetSettings();
  642. HTTP_SaveSettings();
  643. fs_open("/settings.html", &file);
  644. hs->file = file.data;
  645. hs->left = file.len;
  646. send_data(pcb, hs);
  647. tcp_sent(pcb, http_sent);
  648. }
  649. /* Перезагрузка контроллера */
  650. else if (strncmp(data, "GET /reboot.cgi", 15) == 0)
  651. {
  652. HTTP_Reboot();
  653. }
  654. /* Подтверждение новых сетевых настроек */
  655. else if (strncmp(data, "GET /confirm.cgi", 16) == 0)
  656. {
  657. SetWebReinitFlag(false);
  658. SetConfirmWebParamsFlag();
  659. fs_open("/index.html", &file);
  660. hs->file = file.data;
  661. hs->left = file.len;
  662. send_data(pcb, hs);
  663. tcp_sent(pcb, http_sent);
  664. }
  665. /* Проверка пароля, переход в bootloader */
  666. else if (strncmp(data, "GET /fw_update.cgi", 18) == 0)
  667. {
  668. HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  669. hs->file = sendBuf;
  670. hs->left = sendBufLoadLen;
  671. send_data(pcb, hs);
  672. tcp_sent(pcb, http_sent);
  673. }
  674. /* Смена пароля пользователя */
  675. else if (strncmp(data, "POST /changepwd.cgi", 19) == 0)
  676. {
  677. HTTP_ChangeUserPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  678. hs->file = sendBuf;
  679. hs->left = sendBufLoadLen;
  680. send_data(pcb, hs);
  681. tcp_sent(pcb, http_sent);
  682. }
  683. // На производстве
  684. else if (strncmp(data, "GET /setProdate.cgi", 19) == 0)
  685. {
  686. HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  687. hs->file = sendBuf;
  688. hs->left = sendBufLoadLen;
  689. send_data(pcb, hs);
  690. tcp_sent(pcb, http_sent);
  691. }
  692. else
  693. {
  694. HTTP_UpdateUserLoginTime(user_id);
  695. fs_open("/index.html", &file); // +
  696. hs->file = file.data;
  697. hs->left = file.len;
  698. send_data(pcb, hs);
  699. tcp_sent(pcb, http_sent);
  700. }
  701. }
  702. }
  703. pbuf_free(p);
  704. // close_conn(pcb,hs);
  705. }
  706. /* if (err == ERR_OK && p == NULL)
  707. {
  708. close_conn(pcb, hs);
  709. }*/
  710. return ERR_OK;
  711. }
  712. /**
  713. * @brief Error callback for log file transfer
  714. */
  715. static void http_sent_log_err(void * arg, err_t err)
  716. {
  717. (void)err;
  718. (void)arg;
  719. /* Clear file transfer in progress flag */
  720. fLogTransInprog = false;
  721. }
  722. /**
  723. * @brief Sent callback for log file transfer (messages as is, not ordered)
  724. */
  725. static err_t http_sent_log(void *arg, struct tcp_pcb *pcb, u16_t len)
  726. {
  727. struct http_state *hs;
  728. uint32_t nbytes = 0;
  729. static bool start = true;
  730. (void)len;
  731. hs = arg;
  732. if (hs->left > 0)
  733. {
  734. send_data(pcb, hs);
  735. }
  736. else
  737. {
  738. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  739. if (log_ptr + FILE_BUF_MAX_LEN_LOG <= log_size) {
  740. nbytes = LOG_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN_LOG, start);
  741. }
  742. else if (log_ptr < log_size) {
  743. nbytes = LOG_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  744. }
  745. else {
  746. nbytes = 0;
  747. }
  748. log_ptr += nbytes;
  749. start = false;
  750. if (nbytes == 0) {
  751. /* File transfer finished. */
  752. start = true;
  753. close_conn(pcb, hs);
  754. /* Clear file transfer in progress flag */
  755. fLogTransInprog = false;
  756. return ERR_OK;
  757. }
  758. hs->file = logFileBuf;
  759. hs->left = nbytes;
  760. send_data(pcb, hs);
  761. tcp_sent(pcb, http_sent_log);
  762. }
  763. return ERR_OK;
  764. }
  765. /**
  766. * @brief Sent callback for log file transfer (messages as is, not ordered)
  767. */
  768. static err_t http_sent_history(void *arg, struct tcp_pcb *pcb, u16_t len)
  769. {
  770. struct http_state *hs;
  771. uint32_t nbytes = 0;
  772. static bool start = true;
  773. (void)len;
  774. hs = arg;
  775. if (hs->left > 0)
  776. {
  777. send_data(pcb, hs);
  778. }
  779. else
  780. {
  781. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  782. if (log_ptr + FILE_BUF_MAX_LEN <= log_size) {
  783. nbytes = History_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN, start);
  784. }
  785. else if (log_ptr < log_size) {
  786. nbytes = History_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  787. }
  788. else {
  789. nbytes = 0;
  790. }
  791. log_ptr += nbytes;
  792. start = false;
  793. if (nbytes == 0) {
  794. /* File transfer finished. */
  795. start = true;
  796. close_conn(pcb, hs);
  797. /* Clear file transfer in progress flag */
  798. fLogTransInprog = false;
  799. return ERR_OK;
  800. }
  801. hs->file = logFileBuf;
  802. hs->left = nbytes;
  803. send_data(pcb, hs);
  804. tcp_sent(pcb, http_sent_history);
  805. }
  806. return ERR_OK;
  807. }
  808. /**
  809. * @brief callback function for handling connection errors
  810. * @param arg: pointer to an argument to be passed to callback function
  811. * @param err: LwIP error code
  812. * @retval none
  813. */
  814. static void conn_err(void *arg, err_t err)
  815. {
  816. struct http_state *hs;
  817. hs = arg;
  818. mem_free(hs);
  819. }
  820. /**
  821. * @brief callback function called after a successfull TCP data packet transmission
  822. * @param arg: pointer to an argument to be passed to callback function
  823. * @param pcb: pointer on tcp_pcb structure
  824. * @param len
  825. * @retval err : LwIP error code
  826. */
  827. static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
  828. {
  829. struct http_state *hs;
  830. hs = arg;
  831. if (hs->left > 0)
  832. {
  833. send_data(pcb, hs);
  834. }
  835. else
  836. {
  837. close_conn(pcb, hs);
  838. }
  839. return ERR_OK;
  840. }
  841. /**
  842. * @brief sends data found in member "file" of a http_state struct
  843. * @param pcb: pointer to a tcp_pcb struct
  844. * @param hs: pointer to a http_state struct
  845. * @retval none
  846. */
  847. static void send_data(struct tcp_pcb *pcb, struct http_state *hs)
  848. {
  849. err_t err;
  850. u16_t len;
  851. /* We cannot send more data than space available in the send
  852. buffer */
  853. if (tcp_sndbuf(pcb) < hs->left)
  854. {
  855. len = tcp_sndbuf(pcb);
  856. }
  857. else
  858. {
  859. len = hs->left;
  860. }
  861. err = tcp_write(pcb, hs->file, len, 0);
  862. if (err == ERR_OK)
  863. {
  864. hs->file += len;
  865. hs->left -= len;
  866. }
  867. }
  868. /**
  869. * @brief tcp poll callback function
  870. * @param arg: pointer to an argument to be passed to callback function
  871. * @param pcb: pointer on tcp_pcb structure
  872. * @retval err_t
  873. */
  874. static err_t http_poll(void *arg, struct tcp_pcb *pcb)
  875. {
  876. if (arg == NULL)
  877. {
  878. tcp_close(pcb);
  879. }
  880. else
  881. {
  882. send_data(pcb, (struct http_state *)arg);
  883. }
  884. return ERR_OK;
  885. }
  886. /**
  887. * @brief callback function on TCP connection setup ( on port 80)
  888. * @param arg: pointer to an argument structure to be passed to callback function
  889. * @param pcb: pointer to a tcp_pcb structure
  890. * &param err: Lwip stack error code
  891. * @retval err
  892. */
  893. static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
  894. {
  895. struct http_state *hs;
  896. /* Allocate memory for the structure that holds the state of the connection */
  897. hs = mem_malloc(sizeof(struct http_state));
  898. if (hs == NULL)
  899. {
  900. return ERR_MEM;
  901. }
  902. /* Initialize the structure. */
  903. hs->file = NULL;
  904. hs->left = 0;
  905. /* Tell TCP that this is the structure we wish to be passed for our
  906. callbacks. */
  907. tcp_arg(pcb, hs);
  908. /* Tell TCP that we wish to be informed of incoming data by a call
  909. to the http_recv() function. */
  910. tcp_recv(pcb, http_recv);
  911. tcp_err(pcb, conn_err);
  912. tcp_poll(pcb, http_poll, 10);
  913. return ERR_OK;
  914. }
  915. /**
  916. * @brief Opens a file defined in fsdata.c ROM filesystem
  917. * @param name : pointer to a file name
  918. * @param file : pointer to a fs_file structure
  919. * @retval 1 if success, 0 if fail
  920. */
  921. static int fs_open(char *name, struct fs_file *file)
  922. {
  923. struct fsdata_file_noconst *f;
  924. for (f = (struct fsdata_file_noconst *)FS_ROOT; f != NULL; f = (struct fsdata_file_noconst *)f->next)
  925. {
  926. if (!strcmp(name, f->name))
  927. {
  928. file->data = f->data;
  929. file->len = f->len;
  930. return 1;
  931. }
  932. }
  933. return 0;
  934. }
  935. /**
  936. * @brief Initialize the HTTP server (start its thread)
  937. * @param none
  938. * @retval None
  939. */
  940. void HTTP_Init()
  941. {
  942. char buf[MAX_WEB_COOKIE_LEN];
  943. uint8_t user_id;
  944. //sys_thread_new("HTTP", http_server_netconn_thread, NULL, 3000, 2);
  945. struct tcp_pcb *pcb;
  946. /*create new pcb*/
  947. pcb = tcp_new();
  948. /* bind HTTP traffic to pcb */
  949. tcp_bind(pcb, IP_ADDR_ANY, 80);
  950. /* start listening on port 80 */
  951. pcb = tcp_listen(pcb);
  952. /* define callback function for TCP connection setup */
  953. tcp_accept(pcb, http_accept);
  954. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  955. /* Flush user cookie by random value */
  956. sprintf(buf, "%X", (unsigned int)GetRandomNumber());
  957. HTTP_SetUserCookie(buf, user_id);
  958. /* Create user logout timers */
  959. users[user_id].LogoutTimer =
  960. xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
  961. }
  962. }
  963. /**
  964. * @brief
  965. * @retval None
  966. */
  967. int HTTP_SettingsPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  968. {
  969. char tempStr[30];
  970. strncpy(tempStr, bufIn, 30);
  971. /* В запросе нет параметров, нужно формировать JSON ответ */
  972. if (strpbrk(tempStr,"?") == 0)
  973. {
  974. memset(bufOut, 0, SEND_BUF_MAX_LEN);
  975. HTTP_GetSettings(bufOut);
  976. //printf(bufOut);
  977. *lenBufOut = strlen(bufOut);
  978. return SEND_REQUIRED_YES;
  979. }
  980. /* В запросе есть параметры, нужно парсить и сохранять настройки */
  981. else
  982. {
  983. //HTTP_SetSettings(bufIn, lenBufIn);
  984. return SEND_REQUIRED_NO;
  985. }
  986. }
  987. /**
  988. * @brief
  989. * @retval None
  990. */
  991. int HTTP_InfoPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  992. {
  993. char tempStr[30];
  994. strncpy(tempStr, bufIn, 30);
  995. /* В запросе нет параметров, нужно формировать JSON ответ */
  996. if (strpbrk(tempStr,"?") == 0)
  997. {
  998. memset(bufOut, 0, SEND_BUF_MAX_LEN);
  999. HTTP_GetInfo(bufOut);
  1000. *lenBufOut = strlen(bufOut);
  1001. return SEND_REQUIRED_YES;
  1002. }
  1003. /* В запросе есть параметры, нужно парсить и сохранять настройки */
  1004. else
  1005. {
  1006. //HTTP_SetInfo(bufIn, lenBufIn);
  1007. return SEND_REQUIRED_NO;
  1008. /*
  1009. HTTP_SetSettings(bufIn, lenBufIn);
  1010. return SEND_REQUIRED_NO;
  1011. */
  1012. }
  1013. }
  1014. int HTTP_HistoryPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1015. {
  1016. uint8_t i, valueLen = 0;
  1017. char value[20];
  1018. uint32_t nbytes = 0;
  1019. (void)lenBufIn;
  1020. memset(bufOut, 0, FILE_BUF_MAX_LEN);
  1021. ClearParamString(bufIn);
  1022. memset(value, 0, 20);
  1023. GetParamValue(bufIn, "page=", value, &valueLen);
  1024. if (strcmp(value, "all") == 0)
  1025. {
  1026. if (!LOG_IsInit()) {
  1027. return SEND_REQUIRED_NO;
  1028. }
  1029. if (fLogTransInprog == false) {
  1030. // Send log as raw data
  1031. log_ptr = 0;
  1032. log_size = History_GetTotalSTRCount() * STRING_SIZE_HISTORY + sizeof(UTF8_BOM)-1;
  1033. sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Length:%lu\r\n\r\n%s", log_size, UTF8_BOM);
  1034. *lenBufOut = strlen(bufOut);
  1035. // Set file transfer in progress flag
  1036. fLogTransInprog = true;
  1037. return SEND_REQUIRED_FILE;
  1038. }
  1039. else {
  1040. // We send nothing if file transfer already in progress
  1041. return SEND_REQUIRED_NO;
  1042. }
  1043. }
  1044. else {
  1045. if (!LOG_IsInit()) {
  1046. return SEND_REQUIRED_NO;
  1047. }
  1048. else {
  1049. HTTP_GetHistoryPage(bufOut, atoi(value));
  1050. *lenBufOut = strlen(bufOut);
  1051. return SEND_REQUIRED_YES;
  1052. }
  1053. }
  1054. }
  1055. int HTTP_UpsHistoryPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1056. {
  1057. uint8_t i, valueLen = 0;
  1058. char value[20];
  1059. uint32_t nbytes = 0;
  1060. (void)lenBufIn;
  1061. memset(bufOut, 0, FILE_BUF_MAX_LEN);
  1062. ClearParamString(bufIn);
  1063. memset(value, 0, 20);
  1064. GetParamValue(bufIn, "page=", value, &valueLen);
  1065. if (strcmp(value, "all") == 0)
  1066. {
  1067. if (!LOG_IsInit()) {
  1068. return SEND_REQUIRED_NO;
  1069. }
  1070. if (fLogTransInprog == false) {
  1071. // Send log as raw data
  1072. log_ptr = 0;
  1073. log_size = LOG_GetTotalSTRCount() * STRING_SIZE + sizeof(UTF8_BOM)-1;
  1074. sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Length:%lu\r\n\r\n%s", log_size, UTF8_BOM);
  1075. *lenBufOut = strlen(bufOut);
  1076. // Set file transfer in progress flag
  1077. fLogTransInprog = true;
  1078. return SEND_REQUIRED_FILE;
  1079. }
  1080. else {
  1081. // We send nothing if file transfer already in progress
  1082. return SEND_REQUIRED_NO;
  1083. }
  1084. }
  1085. else {
  1086. if (!LOG_IsInit()) {
  1087. return SEND_REQUIRED_NO;
  1088. }
  1089. else {
  1090. HTTP_GetUpsHistoryPage(bufOut, atoi(value));
  1091. *lenBufOut = strlen(bufOut);
  1092. return SEND_REQUIRED_YES;
  1093. }
  1094. }
  1095. }
  1096. /**
  1097. * @brief Установка даты производства
  1098. */
  1099. // TODO Убрать заглушку!
  1100. void HTTP_Prodate(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1101. {
  1102. uint8_t valueLen = 0;
  1103. char value[20];
  1104. memset(bufOut, 0, SEND_BUF_MAX_LEN);
  1105. ClearParamString(bufIn);
  1106. memset(value, 0, 20);
  1107. GetParamValue(bufIn, "prodate=", value, &valueLen);
  1108. /*
  1109. printf("Prodate: ");
  1110. printf(value);
  1111. printf("\r\n");
  1112. */
  1113. /* Устанавливаем дату производства */
  1114. SETTINGS_SetProDate(value, valueLen);
  1115. /* Устанавливаем дату следующей профилактики +1 год */
  1116. RTC_SetProfTime(value);
  1117. /* Пока отправляем true */
  1118. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\nTrue");
  1119. *lenBufOut = strlen(bufOut);
  1120. // TEST_SetServerFlag();
  1121. }
  1122. /**
  1123. * @brief
  1124. * @retval None
  1125. */
  1126. void HTTP_SetSettings(char *buf, uint16_t lenBuf)
  1127. {
  1128. uint8_t valueLen = 0;
  1129. const uint8_t len = MAX_WEB_PARAM_LEN;
  1130. char value[MAX_WEB_PARAM_LEN];
  1131. char str[MAX_WEB_PARAM_LEN];
  1132. //printf(buf);
  1133. //ClearParamString(buf);
  1134. memset(value, 0, len);
  1135. memset(str, 0, MAX_WEB_PARAM_LEN);
  1136. /* SNMP */
  1137. GetParamValue(buf, "read_community=", value, &valueLen);
  1138. SetReadCommunity(value);
  1139. memset(value, 0, len);
  1140. GetParamValue(buf, "write_community=", value, &valueLen);
  1141. SetWriteCommunity(value);
  1142. memset(value, 0, len);
  1143. GetParamValue(buf, "managerIP=", value, &valueLen);
  1144. SetManagerIp(value);
  1145. memset(value, 0, len);
  1146. GetParamValue(buf, "managerIP2=", value, &valueLen);
  1147. SetManagerIp2(value);
  1148. memset(value, 0, len);
  1149. GetParamValue(buf, "managerIP3=", value, &valueLen);
  1150. SetManagerIp3(value);
  1151. memset(value, 0, len);
  1152. GetParamValue(buf, "managerIP4=", value, &valueLen);
  1153. SetManagerIp4(value);
  1154. memset(value, 0, len);
  1155. GetParamValue(buf, "managerIP5=", value, &valueLen);
  1156. SetManagerIp5(value);
  1157. memset(value, 0, len);
  1158. /* Сетевые параметры */
  1159. GetParamValue(buf, "dhcp=", value, &valueLen);
  1160. SetDhcpStateStr(value);
  1161. if (strncmp(value, "on", 2) != 0) // Если dhcp off устанавливаем параметры
  1162. {
  1163. memset(value, 0, len);
  1164. GetParamValue(buf, "ipaddr=", value, &valueLen);
  1165. SetIPStr(value);
  1166. memset(value, 0, len);
  1167. GetParamValue(buf, "gw=", value, &valueLen);
  1168. SetGatewayStr(value);
  1169. memset(value, 0, len);
  1170. GetParamValue(buf, "mask=", value, &valueLen);
  1171. SetMaskStr(value);
  1172. memset(value, 0, len);
  1173. }
  1174. memset(value, 0, len);
  1175. GetParamValue(buf, "swauth=", value, &valueLen);
  1176. SetAuthEnableStateStr(value);
  1177. if (strncmp(value, "on", 2) == 0){
  1178. /* параметры RADIUS*/
  1179. memset(value, 0, len);
  1180. GetParamValue(buf, "rs_enabled=", value, &valueLen);
  1181. SetRDSEnableStateStr(value);
  1182. if (strncmp(value, "on", 2) == 0) // Если raddius off устанавливаем параметры
  1183. {
  1184. memset(value, 0, len);
  1185. GetParamValue(buf, "rs_server=", value, &valueLen);
  1186. SetRDSIpStr(value);
  1187. memset(value, 0, len);
  1188. GetParamValue(buf, "rs_port=", value, &valueLen);
  1189. SetRDSPortStr(value);
  1190. memset(value, 0, len);
  1191. GetParamValue(buf, "rs_pwd=", value, &valueLen);
  1192. SetRDSPasswordkStr(value);
  1193. memset(value, 0, len);
  1194. GetParamValue(buf, "rs_key=", value, &valueLen);
  1195. SetRDSKeyAccesstStr(value);
  1196. memset(value, 0, len);
  1197. }
  1198. }
  1199. memset(value, 0, len);
  1200. // Параметры реле и сухих контактов
  1201. GetParamValue(buf, "di1=", value, &valueLen);
  1202. SetDINTypeActStr(value, 0);
  1203. memset(value, 0, len);
  1204. GetParamValue(buf, "ro1=", value, &valueLen);
  1205. SetROTypeActStr(value, 0);
  1206. memset(value, 0, len);
  1207. GetParamValue(buf, "ro2=", value, &valueLen);
  1208. SetROTypeActStr(value, 1);
  1209. memset(value, 0, len);
  1210. // Параметры даты и времени
  1211. GetParamValue(buf, "ntp=", value, &valueLen);
  1212. SetSntpStateStr(value);
  1213. if (strncmp(value, "1", 1) == 0) // Если ntp on устанавливаем параметры
  1214. {
  1215. memset(value, 0, len);
  1216. GetParamValue(buf, "ntpservip=", value, &valueLen);
  1217. SetSntpServerIpStr(value);
  1218. memset(value, 0, len);
  1219. }
  1220. else if (strncmp(value, "0", 1) == 0){
  1221. GetParamValue(buf, "date=", value, &valueLen);
  1222. SetDateStr(value);
  1223. memset(value, 0, len);
  1224. GetParamValue(buf, "time=", value, &valueLen);
  1225. url_decode(str, sizeof(str), value);
  1226. SetTimeStr(str);
  1227. memset(value, 0, len);
  1228. }
  1229. GetParamValue(buf, "utc=", value, &valueLen);
  1230. SetSntpTimeZoneStr(value);
  1231. memset(value, 0, len);
  1232. /* Если параметры WEB изменились выставляем флаг, сохраняем настройки и перезагружаемся */
  1233. if (GetStateWebReinit() == true)
  1234. {
  1235. SetWebReinitFlag(true);
  1236. HTTP_SaveSettings();
  1237. /* Блокируем управление ключем на тау секунд*/
  1238. //IO_KeyBlockOn();
  1239. vTaskDelay(1010);
  1240. Reboot();
  1241. }
  1242. HTTP_SaveSettings();
  1243. }
  1244. /**
  1245. * @brief
  1246. * @retval None
  1247. */
  1248. void HTTP_SetInfo(char *buf, uint16_t lenBuf)
  1249. {
  1250. uint8_t valueLen = 0;
  1251. const uint8_t len = 110;
  1252. char value[110];
  1253. char str[110];
  1254. // ClearParamString(buf);
  1255. memset(value, 0, len);
  1256. /* Владелец */
  1257. GetParamValue(buf, "owner=", value, &valueLen);
  1258. url_decode(str, sizeof(str), value);
  1259. SetOwner(str);
  1260. memset(value, 0, len);
  1261. /* Владелец */
  1262. GetParamValue(buf, "sysLocation=", value, &valueLen);
  1263. url_decode(str, sizeof(str), value);
  1264. SetLocation(str);
  1265. memset(value, 0, len);
  1266. /* Комментарий */
  1267. GetParamValue(buf, "comment=", value, &valueLen);
  1268. url_decode(str, sizeof(str), value);
  1269. SetComment(str);
  1270. memset(value, 0, len);
  1271. HTTP_SaveSettings();
  1272. }
  1273. /**
  1274. * @brief Запуск/останов теста UPS
  1275. */
  1276. void HTTP_UPSTest(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1277. {
  1278. char *value = 0;
  1279. char *valueLen = 0;
  1280. char tempValue[20];
  1281. char tempValue2[20];
  1282. int8_t res = 0;
  1283. memset(tempValue, 0, 20);
  1284. memset(tempValue2, 0, 20);
  1285. strcpy(bufOut, HTTP_200_OK);
  1286. value = strstr(bufIn, "func");
  1287. valueLen = strpbrk(value, ":");
  1288. strncpy(tempValue, (valueLen+1), (strlen(valueLen)-2));
  1289. if (strcmp(tempValue, "\"stop\"") == 0){
  1290. res = ups_metac_service_pdu(ups_cancel_test);
  1291. if(res == 1 || res == 0){
  1292. strcat(bufOut, "Тест остановлен!");
  1293. log_event_data(LOG_TEST_UPS, "Администратор(Останов)");
  1294. }
  1295. if(res == -1)
  1296. strcat(bufOut, "Тест не удалось остановить!");
  1297. *lenBufOut = strlen(bufOut);
  1298. }
  1299. else if (strcmp(tempValue, "\"discharge\"") == 0){
  1300. res = ups_metac_service_pdu(ups_test_low_bat);
  1301. if(res == 1 || res == 0){
  1302. strcat(bufOut, "Тест запущен!");
  1303. log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  1304. }
  1305. if(res == -1)
  1306. strcat(bufOut, "Тест не удалось запустить!");
  1307. *lenBufOut = strlen(bufOut);
  1308. }
  1309. else if (strncmp(tempValue, "\"time\"", 6) == 0){
  1310. valueLen = strpbrk(tempValue, ":");
  1311. strncpy(tempValue2, (valueLen+2), (strlen(valueLen)-2));
  1312. TimeParam = atoi(tempValue2);
  1313. res = ups_metac_service_pdu(ups_test_time);
  1314. if(res == 1 || res == 0){
  1315. strcat(bufOut, "Тест запущен!");
  1316. log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  1317. }
  1318. if(res == -1)
  1319. strcat(bufOut, "Тест не удалось запустить!");
  1320. *lenBufOut = strlen(bufOut);
  1321. }
  1322. }
  1323. /**
  1324. * @brief Выклюение UPS
  1325. */
  1326. void HTTP_UPSshutdown(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1327. {
  1328. char *value = 0;
  1329. char *valueLen = 0;
  1330. char *valueLenEnd = 0;
  1331. char tempValue[50];
  1332. char tempValue2[50];
  1333. int8_t res = 0;
  1334. memset(tempValue, 0, 50);
  1335. strcpy(bufOut, HTTP_200_OK);
  1336. value = strstr(bufIn, "func");
  1337. valueLen = strpbrk(value, ":");
  1338. strncpy(tempValue, (valueLen+1), (strlen(valueLen)-2));
  1339. if (strcmp(tempValue, "\"reboot\"") == 0){
  1340. res = ups_metac_service_pdu(ups_cancel_shut_down);
  1341. if(res == 1){
  1342. log_event_data(LOG_SHUTDOWN_UPS, "Администратор(Останов)");
  1343. strcat(bufOut, "Выключение нагрузки ИБП отменено!");
  1344. }
  1345. else
  1346. strcat(bufOut, "Выключение нагрузки ИБП не удалось отменить!");
  1347. *lenBufOut = strlen(bufOut);
  1348. }
  1349. else if (strncmp(tempValue, "\"off\"", 5) == 0){
  1350. value = strstr(tempValue, "after");
  1351. valueLen = strpbrk(value, ":");
  1352. memset(tempValue2, 0, 50);
  1353. strncpy(tempValue2, (valueLen+2), (strlen(valueLen) - 2));
  1354. TimeParamFloat = atof(tempValue2);
  1355. res = ups_metac_service_pdu(ups_shutdown);
  1356. if(res == 1){
  1357. strcat(bufOut, "Отключение нагрузки ИБП!");
  1358. log_event_data(LOG_SHUTDOWN_UPS, "Администратор");
  1359. }else
  1360. strcat(bufOut, "Отключение нагрузки ИБП не удалось!");
  1361. *lenBufOut = strlen(bufOut);
  1362. }
  1363. }
  1364. /**
  1365. * @brief Проверка пароля для перехода в режим bootloader
  1366. * @retval None
  1367. */
  1368. void HTTP_ConfirmBootPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1369. {
  1370. char tempStr[50];
  1371. strncpy(tempStr, bufIn, 50);
  1372. char value[20];
  1373. uint8_t valueLen;
  1374. memset(value, 0, 20);
  1375. //if (GetParamValue(tempStr, "password=", value, &valueLen))
  1376. {
  1377. //if (strcmp(BOOTLOADER_PASWORD, value) == 0)
  1378. {
  1379. // *bufOut = '1';
  1380. /* Запускаем задачу отложенной перезагрузки. Контроллер должен успеть
  1381. отправить ответ серверу о статусе пароля */
  1382. HTTP_StartResetTask(true);
  1383. }
  1384. /* else
  1385. *bufOut = '0';*/
  1386. //*lenBufOut = 1;
  1387. }
  1388. }
  1389. /**
  1390. * @brief Проверка пароля для входа в Web
  1391. * @retval None
  1392. */
  1393. int HTTP_ConfirmWebPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1394. {
  1395. char tempStr[50];
  1396. char login[20];
  1397. char password[20];
  1398. uint8_t valueLen, user_id;
  1399. char *strPtr = 0;
  1400. char WebPassword[MAX_WEB_PASSWD_LEN];
  1401. char WebLogin[MAX_WEB_LOGIN_LEN];
  1402. char buf[40];
  1403. memset(login, 0, 20);
  1404. memset(password, 0, 20);
  1405. memset(tempStr, 0, 50);
  1406. /* Get first 50 bytes of string */
  1407. strncpy(tempStr, bufIn, 49);
  1408. /* Add " " to the string in order GetParamValue() can be able to parse the param */
  1409. strcat(tempStr, " ");
  1410. if (GetParamValue(tempStr, "login=", login, &valueLen) &&
  1411. GetParamValue(tempStr, "password=", password, &valueLen))
  1412. {
  1413. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  1414. GetUserLogin(user_id, WebLogin, &valueLen);
  1415. GetUserPassword(user_id, WebPassword, &valueLen);
  1416. /* Check login and password */
  1417. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  1418. (strncmp(WebPassword, password, MAX_WEB_PASSWD_LEN) == 0)) {
  1419. /* Login and pass are valid */
  1420. /* Check user's login session */
  1421. /* If "user" has logged in */
  1422. if (user_id >= 1) {
  1423. }
  1424. /* TODO replace global flag with user-pass-cookie */
  1425. Authenticated = true;
  1426. /* Generate cookie */
  1427. sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
  1428. /* Set users cookie */
  1429. HTTP_SetUserCookie(tempStr, user_id);
  1430. HTTP_UpdateUserLoginTime(user_id);
  1431. /* Send login and cookie back */
  1432. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
  1433. strcat(bufOut, WebLogin);
  1434. strcat(bufOut, "\r\nSet-Cookie: id=");
  1435. strcat(bufOut, tempStr);
  1436. sprintf(tempStr, "%d", user_id);
  1437. strcat(bufOut, "\r\nSet-Cookie: role=");
  1438. strcat(bufOut, tempStr);
  1439. strcat(bufOut, "\r\n\r\n");
  1440. strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
  1441. *lenBufOut = strlen(bufOut);
  1442. switch (user_id) {
  1443. case 0:
  1444. snprintf(buf, sizeof(buf), "Администратор");
  1445. break;
  1446. case 1:
  1447. snprintf(buf, sizeof(buf), "Пользователь");
  1448. break;
  1449. default:
  1450. snprintf(buf, sizeof(buf), "", login);
  1451. break;
  1452. }
  1453. log_event_data(LOG_LOGIN, buf);
  1454. /* Запускаем задачу-таймер логаута. */
  1455. /* TODO отправить ответ серверу о статусе пароля */
  1456. return SEND_REQUIRED_YES;
  1457. }
  1458. /*
  1459. else {
  1460. continue;
  1461. }
  1462. */
  1463. }
  1464. }
  1465. /* No valid login and pass found */
  1466. /* TODO replace global flag with user-pass-cookie*/
  1467. Authenticated = false;
  1468. /* Wrong login or pass, return */
  1469. return SEND_REQUIRED_NO;
  1470. }
  1471. void HTTP_LOGIN(char *bufOut, uint16_t *lenBufOut)
  1472. {
  1473. char tempStr[50];
  1474. uint8_t valueLen;
  1475. char WebLogin[MAX_WEB_LOGIN_LEN];
  1476. GetUserLogin(ADMIN, WebLogin, &valueLen);
  1477. memset(tempStr, 0, 50);
  1478. /* TODO replace global flag with user-pass-cookie */
  1479. Authenticated = true;
  1480. /* Generate cookie */
  1481. sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
  1482. /* Set users cookie */
  1483. HTTP_SetUserCookie(tempStr, ADMIN);
  1484. HTTP_UpdateUserLoginTime(ADMIN);
  1485. /* Send login and cookie back */
  1486. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
  1487. strcat(bufOut, WebLogin);
  1488. strcat(bufOut, "\r\nSet-Cookie: id=");
  1489. strcat(bufOut, tempStr);
  1490. strcat(bufOut, "\r\nSet-Cookie: role=0");
  1491. strcat(bufOut, "\r\nSet-Cookie: auth=0");
  1492. strcat(bufOut, "\r\n\r\n");
  1493. strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
  1494. *lenBufOut = strlen(bufOut);
  1495. }
  1496. /**
  1497. * @brief
  1498. * @retval None
  1499. */
  1500. uint8_t GetParamValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1501. {
  1502. char *beginValue = 0;
  1503. char *endValue = 0;
  1504. int len = 0;
  1505. char *strPtr = 0;
  1506. strPtr = strstr(inStr, paramName);
  1507. if (strPtr != 0)
  1508. {
  1509. beginValue = strpbrk(strPtr,"=");
  1510. endValue = strpbrk(strPtr,"&");
  1511. if (endValue == 0)
  1512. endValue = strpbrk(strPtr," ");
  1513. len = endValue - beginValue - 1;
  1514. strncpy(paramValue, beginValue + 1, len);
  1515. *endValue = '0';
  1516. *beginValue = '0';
  1517. *paramLen = len;
  1518. return 1;
  1519. }
  1520. else
  1521. {
  1522. *paramLen = 0;
  1523. return 0;
  1524. }
  1525. }
  1526. /**
  1527. * @brief
  1528. * @retval None
  1529. */
  1530. uint8_t GetCookieValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1531. {
  1532. char *beginValue = 0;
  1533. char *endValue = 0;
  1534. int len = 0;
  1535. char *strPtr = 0;
  1536. strPtr = strstr(inStr, paramName);
  1537. if (strPtr != 0)
  1538. {
  1539. beginValue = strpbrk(strPtr,"=");
  1540. endValue = strpbrk(strPtr,";");
  1541. if (endValue == 0)
  1542. endValue = strpbrk(strPtr,"\n");
  1543. len = endValue - beginValue - 1;
  1544. strncpy(paramValue, beginValue + 1, len);
  1545. *endValue = '0';
  1546. *beginValue = '0';
  1547. *paramLen = len;
  1548. return 1;
  1549. }
  1550. else
  1551. {
  1552. *paramLen = 0;
  1553. return 0;
  1554. }
  1555. }
  1556. /**
  1557. * @brief
  1558. * @retval None
  1559. */
  1560. /*
  1561. uint8_t GetParamValueInEnd(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1562. {
  1563. char *beginValue = 0;
  1564. char *endValue = 0;
  1565. int len = 0;
  1566. char *strPtr = 0;
  1567. strPtr = strstr(inStr, paramName);
  1568. if (strPtr != 0)
  1569. {
  1570. beginValue = strpbrk(strPtr,"=");
  1571. endValue = strpbrk(strPtr," ");
  1572. len = endValue - beginValue - 1;
  1573. strncpy(paramValue, beginValue + 1, len);
  1574. *endValue = '0';
  1575. *beginValue = '0';
  1576. *paramLen = len;
  1577. return 1;
  1578. }
  1579. else
  1580. {
  1581. *paramLen = 0;
  1582. return 0;
  1583. }
  1584. }
  1585. */
  1586. void ClearParamString(char *inBuf)
  1587. {
  1588. uint16_t len;
  1589. char *str;
  1590. str = strstr(inBuf, "HTTP");
  1591. if (str != 0)
  1592. {
  1593. len = str - inBuf;
  1594. memset(str, 0, RECIVE_BUF_MAX_LEN - len - 1);
  1595. }
  1596. }
  1597. /**
  1598. * @brief Чтение Cookie пользователя
  1599. */
  1600. static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len)
  1601. {
  1602. sprintf(str, "%s", users[user_id].cookie);
  1603. *len = strlen(str);
  1604. }
  1605. /**
  1606. * @brief Установка Cookie пользователя
  1607. */
  1608. static void HTTP_SetUserCookie(char *str, uint8_t user_id)
  1609. {
  1610. strcpy(users[user_id].cookie, str);
  1611. }
  1612. /**
  1613. * @brief Обновление времени последней активности пользователя
  1614. */
  1615. static void HTTP_UpdateUserLoginTime(uint8_t user_id)
  1616. {
  1617. xTimerStart(users[user_id].LogoutTimer, 0);
  1618. }
  1619. /**
  1620. * @brief Extract the Content_Length data from HTML data
  1621. * @param data : pointer on receive packet buffer
  1622. * @param len : buffer length
  1623. * @retval size : Content_length in numeric format
  1624. */
  1625. static uint32_t Parse_Content_Length(char *data, uint32_t len)
  1626. {
  1627. uint32_t i=0,size=0, S=1;
  1628. int32_t j=0;
  1629. char sizestring[6], *ptr;
  1630. ContentLengthOffset =0;
  1631. /* find Content-Length data in packet buffer */
  1632. for (i=0;i<len;i++)
  1633. {
  1634. if (strncmp ((char*)(data+i), Content_Length, 16)==0)
  1635. {
  1636. ContentLengthOffset = i+16;
  1637. break;
  1638. }
  1639. }
  1640. /* read Content-Length value */
  1641. if (ContentLengthOffset)
  1642. {
  1643. i=0;
  1644. ptr = (char*)(data + ContentLengthOffset);
  1645. while(*(ptr+i)!=0x0d)
  1646. {
  1647. sizestring[i] = *(ptr+i);
  1648. i++;
  1649. ContentLengthOffset++;
  1650. }
  1651. if (i>0)
  1652. {
  1653. /* transform string data into numeric format */
  1654. for(j=i-1;j>=0;j--)
  1655. {
  1656. size += (sizestring[j]-0x30)*S;
  1657. S=S*10;
  1658. }
  1659. }
  1660. }
  1661. return size;
  1662. }
  1663. /**
  1664. * @brief Принудительный логаут пользователя
  1665. */
  1666. static void HTTP_ForceUserLogout(uint8_t user_id)
  1667. {
  1668. char cookie[MAX_WEB_COOKIE_LEN];
  1669. /* Flush user cookie by random value */
  1670. sprintf(cookie, "%X", (unsigned int)GetRandomNumber());
  1671. HTTP_SetUserCookie(cookie, user_id);
  1672. }
  1673. /**
  1674. * @brief >Callback таймера логаута пользователя
  1675. */
  1676. void LogoutTimerCallback(TimerHandle_t pxTimer) {
  1677. uint8_t user_id = (uint8_t)pvTimerGetTimerID( pxTimer );
  1678. if( sSettings.sRADIUS.Auth_enable )
  1679. HTTP_ForceUserLogout(user_id);
  1680. }
  1681. /**
  1682. * @brief Смена пароля пользователя
  1683. * @retval None
  1684. */
  1685. int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1686. {
  1687. char tempStr[110];
  1688. char value[20];
  1689. char login[20];
  1690. char password[20];
  1691. uint8_t valueLen, valueLen2, user_id;
  1692. char WebLogin[MAX_WEB_LOGIN_LEN];
  1693. (void)lenBufIn;
  1694. memset(login, 0, 20);
  1695. memset(password, 0, 20);
  1696. memset(tempStr, 0, 50);
  1697. memset(value, 0, 20);
  1698. ClearParamString(bufIn);
  1699. strncpy(tempStr, bufIn, 110);
  1700. strcpy(bufOut, HTTP_200_OK);
  1701. if (GetParamValue(tempStr, "username=", login, &valueLen) &&
  1702. GetParamValue(tempStr, "oldpass=", password, &valueLen))
  1703. {
  1704. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++)
  1705. {
  1706. memset(value, 0, 20);
  1707. memset(WebLogin, 0, MAX_WEB_LOGIN_LEN);
  1708. GetUserLogin(user_id, WebLogin, &valueLen);
  1709. GetUserPassword(user_id, value, &valueLen2);
  1710. /* Check login and password */
  1711. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  1712. (memcmp(password, value, 11) == 0))
  1713. {
  1714. memset(password, 0, 20);
  1715. if (GetParamValue(tempStr, "newpass=", password, &valueLen))
  1716. {
  1717. memcpy(sSettings.sAuth[user_id].password, password, 11);
  1718. HTTP_SaveSettings();
  1719. log_event_data(LOG_PSW_CHANGE, "Администратор");
  1720. strcat(bufOut, "Пароль успешно изменён");
  1721. *lenBufOut = strlen(bufOut);
  1722. return SEND_REQUIRED_YES;
  1723. }
  1724. else {
  1725. strcat(bufOut, "Введены некорректные данные!");
  1726. *lenBufOut = strlen(bufOut);
  1727. return SEND_REQUIRED_YES;
  1728. }
  1729. }
  1730. }
  1731. strcat(bufOut, "Введён неверный пароль!");
  1732. *lenBufOut = strlen(bufOut);
  1733. return SEND_REQUIRED_YES;
  1734. }
  1735. else {
  1736. strcat(bufOut, "Введены некорректные данные!");
  1737. *lenBufOut = strlen(bufOut);
  1738. return SEND_REQUIRED_YES;
  1739. }
  1740. }