PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sapi/litespeed/lsapilib.c

http://github.com/php/php-src
C | 4110 lines | 3365 code | 529 blank | 216 comment | 796 complexity | 41e936f77533c7a5e759089d70cf9b1b MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available at through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt. |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: George Wang <gwang@litespeedtech.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /*
  17. Copyright (c) 2002-2018, Lite Speed Technologies Inc.
  18. All rights reserved.
  19. Redistribution and use in source and binary forms, with or without
  20. modification, are permitted provided that the following conditions are
  21. met:
  22. * Redistributions of source code must retain the above copyright
  23. notice, this list of conditions and the following disclaimer.
  24. * Redistributions in binary form must reproduce the above
  25. copyright notice, this list of conditions and the following
  26. disclaimer in the documentation and/or other materials provided
  27. with the distribution.
  28. * Neither the name of the Lite Speed Technologies Inc nor the
  29. names of its contributors may be used to endorse or promote
  30. products derived from this software without specific prior
  31. written permission.
  32. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  35. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  36. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  38. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  39. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  40. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  42. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. #include <ctype.h>
  45. #include <dlfcn.h>
  46. #include <errno.h>
  47. #include <fcntl.h>
  48. #include <limits.h>
  49. #include <sys/stat.h>
  50. #include <sched.h>
  51. #include <signal.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <stdarg.h>
  55. #include <string.h>
  56. #include <sys/mman.h>
  57. #include <sys/resource.h>
  58. #include <sys/socket.h>
  59. #include <sys/time.h>
  60. #include <sys/uio.h>
  61. #include <sys/wait.h>
  62. #include <grp.h>
  63. #include <pwd.h>
  64. #include <time.h>
  65. #include <unistd.h>
  66. #include <arpa/inet.h>
  67. #include <netdb.h>
  68. #include <netinet/in.h>
  69. #include <netinet/tcp.h>
  70. #include <sys/un.h>
  71. #include "lsapilib.h"
  72. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  73. #include <sys/prctl.h>
  74. #endif
  75. #if defined(__FreeBSD__ ) || defined(__NetBSD__) || defined(__OpenBSD__) \
  76. || defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
  77. #include <sys/sysctl.h>
  78. #endif
  79. #include <inttypes.h>
  80. #ifndef uint32
  81. #define uint32 uint32_t
  82. #endif
  83. #include <Zend/zend_portability.h>
  84. struct lsapi_MD5Context {
  85. uint32 buf[4];
  86. uint32 bits[2];
  87. unsigned char in[64];
  88. };
  89. void lsapi_MD5Init(struct lsapi_MD5Context *context);
  90. void lsapi_MD5Update(struct lsapi_MD5Context *context, unsigned char const *buf,
  91. unsigned len);
  92. void lsapi_MD5Final(unsigned char digest[16], struct lsapi_MD5Context *context);
  93. /*
  94. * This is needed to make RSAREF happy on some MS-DOS compilers.
  95. */
  96. typedef struct lsapi_MD5Context lsapi_MD5_CTX;
  97. #define LSAPI_ST_REQ_HEADER 1
  98. #define LSAPI_ST_REQ_BODY 2
  99. #define LSAPI_ST_RESP_HEADER 4
  100. #define LSAPI_ST_RESP_BODY 8
  101. #define LSAPI_ST_BACKGROUND 16
  102. #define LSAPI_RESP_BUF_SIZE 8192
  103. #define LSAPI_INIT_RESP_HEADER_LEN 4096
  104. enum
  105. {
  106. LSAPI_STATE_IDLE,
  107. LSAPI_STATE_CONNECTED,
  108. LSAPI_STATE_ACCEPTING,
  109. };
  110. typedef struct _lsapi_child_status
  111. {
  112. int m_pid;
  113. long m_tmStart;
  114. volatile short m_iKillSent;
  115. volatile char m_inProcess;
  116. volatile char m_state;
  117. volatile int m_iReqCounter;
  118. volatile long m_tmWaitBegin;
  119. volatile long m_tmReqBegin;
  120. volatile long m_tmLastCheckPoint;
  121. }
  122. lsapi_child_status;
  123. static lsapi_child_status * s_worker_status = NULL;
  124. static int g_inited = 0;
  125. static int g_running = 1;
  126. static int s_ppid;
  127. static int s_restored_ppid = 0;
  128. static int s_pid = 0;
  129. static int s_slow_req_msecs = 0;
  130. static int s_keepListener = 0;
  131. static int s_dump_debug_info = 0;
  132. static int s_pid_dump_debug_info = 0;
  133. static int s_req_processed = 0;
  134. static int s_skip_write = 0;
  135. static int (*pthread_atfork_func)(void (*prepare)(void), void (*parent)(void),
  136. void (*child)(void)) = NULL;
  137. static int *s_busy_workers = NULL;
  138. static int *s_accepting_workers = NULL;
  139. static int *s_global_counter = &s_req_processed;
  140. static int s_max_busy_workers = -1;
  141. static char *s_stderr_log_path = NULL;
  142. static int s_stderr_is_pipe = 0;
  143. static int s_ignore_pid = -1;
  144. static size_t s_total_pages = 1;
  145. static size_t s_min_avail_pages = 256 * 1024;
  146. static size_t *s_avail_pages = &s_total_pages;
  147. LSAPI_Request g_req =
  148. { .m_fdListen = -1, .m_fd = -1 };
  149. static char s_secret[24];
  150. static LSAPI_On_Timer_pf s_proc_group_timer_cb = NULL;
  151. void Flush_RespBuf_r( LSAPI_Request * pReq );
  152. static int lsapi_reopen_stderr(const char *p);
  153. static const char *CGI_HEADERS[H_TRANSFER_ENCODING+1] =
  154. {
  155. "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET",
  156. "HTTP_ACCEPT_ENCODING",
  157. "HTTP_ACCEPT_LANGUAGE", "HTTP_AUTHORIZATION",
  158. "HTTP_CONNECTION", "CONTENT_TYPE",
  159. "CONTENT_LENGTH", "HTTP_COOKIE", "HTTP_COOKIE2",
  160. "HTTP_HOST", "HTTP_PRAGMA",
  161. "HTTP_REFERER", "HTTP_USER_AGENT",
  162. "HTTP_CACHE_CONTROL",
  163. "HTTP_IF_MODIFIED_SINCE", "HTTP_IF_MATCH",
  164. "HTTP_IF_NONE_MATCH",
  165. "HTTP_IF_RANGE",
  166. "HTTP_IF_UNMODIFIED_SINCE",
  167. "HTTP_KEEP_ALIVE",
  168. "HTTP_RANGE",
  169. "HTTP_X_FORWARDED_FOR",
  170. "HTTP_VIA",
  171. "HTTP_TRANSFER_ENCODING"
  172. };
  173. static int CGI_HEADER_LEN[H_TRANSFER_ENCODING+1] =
  174. { 11, 19, 20, 20, 18, 15, 12, 14, 11, 12, 9, 11, 12, 15, 18,
  175. 22, 13, 18, 13, 24, 15, 10, 20, 8, 22 };
  176. static const char *HTTP_HEADERS[H_TRANSFER_ENCODING+1] =
  177. {
  178. "Accept", "Accept-Charset",
  179. "Accept-Encoding",
  180. "Accept-Language", "Authorization",
  181. "Connection", "Content-Type",
  182. "Content-Length", "Cookie", "Cookie2",
  183. "Host", "Pragma",
  184. "Referer", "User-Agent",
  185. "Cache-Control",
  186. "If-Modified-Since", "If-Match",
  187. "If-None-Match",
  188. "If-Range",
  189. "If-Unmodified-Since",
  190. "Keep-Alive",
  191. "Range",
  192. "X-Forwarded-For",
  193. "Via",
  194. "Transfer-Encoding"
  195. };
  196. static int HTTP_HEADER_LEN[H_TRANSFER_ENCODING+1] =
  197. { 6, 14, 15, 15, 13, 10, 12, 14, 6, 7, 4, 6, 7, 10, //user-agent
  198. 13,17, 8, 13, 8, 19, 10, 5, 15, 3, 17
  199. };
  200. static const char *s_log_level_names[8] =
  201. {
  202. "", "DEBUG","INFO", "NOTICE", "WARN", "ERROR", "CRIT", "FATAL"
  203. };
  204. void LSAPI_Log(int flag, const char * fmt, ...)
  205. {
  206. char buf[1024];
  207. char *p = buf;
  208. if ((flag & LSAPI_LOG_TIMESTAMP_BITS) &&
  209. !((flag & LSAPI_LOG_TIMESTAMP_STDERR) && s_stderr_is_pipe))
  210. {
  211. struct timeval tv;
  212. struct tm tm;
  213. gettimeofday(&tv, NULL);
  214. localtime_r(&tv.tv_sec, &tm);
  215. if (flag & LSAPI_LOG_TIMESTAMP_FULL)
  216. {
  217. p += snprintf(p, 1024, "%04d-%02d-%02d %02d:%02d:%02d.%06d ",
  218. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  219. tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec);
  220. }
  221. else if (flag & LSAPI_LOG_TIMESTAMP_HMS)
  222. {
  223. p += snprintf(p, 1024, "%02d:%02d:%02d ",
  224. tm.tm_hour, tm.tm_min, tm.tm_sec);
  225. }
  226. }
  227. int level = flag & LSAPI_LOG_LEVEL_BITS;
  228. if (level && level <= LSAPI_LOG_FLAG_FATAL)
  229. {
  230. p += snprintf(p, 100, "[%s] ", s_log_level_names[level]);
  231. }
  232. if (flag & LSAPI_LOG_PID)
  233. {
  234. p += snprintf(p, 100, "[%d] ", s_pid);
  235. }
  236. if (p > buf)
  237. fprintf(stderr, "%.*s", (int)(p - buf), buf);
  238. va_list ap;
  239. va_start(ap, fmt);
  240. vfprintf(stderr, fmt, ap);
  241. va_end(ap);
  242. }
  243. #ifdef LSAPI_DEBUG
  244. #define DBGLOG_FLAG (LSAPI_LOG_TIMESTAMP_FULL|LSAPI_LOG_FLAG_DEBUG|LSAPI_LOG_PID)
  245. #define lsapi_dbg(...) LSAPI_Log(DBGLOG_FLAG, __VA_ARGS__)
  246. #else
  247. #define lsapi_dbg(...)
  248. #endif
  249. #define lsapi_log(...) LSAPI_Log(LSAPI_LOG_TIMESTAMP_FULL|LSAPI_LOG_TIMESTAMP_STDERR|LSAPI_LOG_PID, __VA_ARGS__)
  250. static int lsapi_parent_dead()
  251. {
  252. // Return non-zero if the parent is dead. 0 if still alive.
  253. if (!s_ppid) {
  254. // not checking, so not dead
  255. return(0);
  256. }
  257. if (s_restored_ppid) {
  258. if (kill(s_restored_ppid,0) == -1) {
  259. if (errno == EPERM) {
  260. return(0); // no permission, but it's still there.
  261. }
  262. return(1); // Dead
  263. }
  264. return(0); // it worked, so it's not dead
  265. }
  266. return(s_ppid != getppid());
  267. }
  268. static void lsapi_sigpipe( int sig )
  269. {
  270. }
  271. static void lsapi_siguser1( int sig )
  272. {
  273. g_running = 0;
  274. }
  275. #ifndef sighandler_t
  276. typedef void (*sighandler_t)(int);
  277. #endif
  278. static void lsapi_signal(int signo, sighandler_t handler)
  279. {
  280. struct sigaction sa;
  281. sigaction(signo, NULL, &sa);
  282. if (sa.sa_handler == SIG_DFL)
  283. {
  284. sigemptyset(&sa.sa_mask);
  285. sa.sa_flags = 0;
  286. sa.sa_handler = handler;
  287. sigaction(signo, &sa, NULL);
  288. }
  289. }
  290. static int s_enable_core_dump = 0;
  291. static void lsapi_enable_core_dump(void)
  292. {
  293. #if defined(__FreeBSD__ ) || defined(__NetBSD__) || defined(__OpenBSD__) \
  294. || defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
  295. int mib[2];
  296. size_t len;
  297. len = 2;
  298. if ( sysctlnametomib("kern.sugid_coredump", mib, &len) == 0 )
  299. {
  300. len = sizeof(s_enable_core_dump);
  301. if (sysctl(mib, 2, NULL, 0, &s_enable_core_dump, len) == -1)
  302. perror( "sysctl: Failed to set 'kern.sugid_coredump', "
  303. "core dump may not be available!");
  304. }
  305. #endif
  306. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  307. if (prctl(PR_SET_DUMPABLE, s_enable_core_dump,0,0,0) == -1)
  308. perror( "prctl: Failed to set dumpable, "
  309. "core dump may not be available!");
  310. #endif
  311. }
  312. static inline void lsapi_buildPacketHeader( struct lsapi_packet_header * pHeader,
  313. char type, int len )
  314. {
  315. pHeader->m_versionB0 = LSAPI_VERSION_B0; /* LSAPI protocol version */
  316. pHeader->m_versionB1 = LSAPI_VERSION_B1;
  317. pHeader->m_type = type;
  318. pHeader->m_flag = LSAPI_ENDIAN;
  319. pHeader->m_packetLen.m_iLen = len;
  320. }
  321. static int lsapi_set_nblock( int fd, int nonblock )
  322. {
  323. int val = fcntl( fd, F_GETFL, 0 );
  324. if ( nonblock )
  325. {
  326. if (!( val & O_NONBLOCK ))
  327. {
  328. return fcntl( fd, F_SETFL, val | O_NONBLOCK );
  329. }
  330. }
  331. else
  332. {
  333. if ( val & O_NONBLOCK )
  334. {
  335. return fcntl( fd, F_SETFL, val &(~O_NONBLOCK) );
  336. }
  337. }
  338. return 0;
  339. }
  340. static int lsapi_close( int fd )
  341. {
  342. int ret;
  343. while( 1 )
  344. {
  345. ret = close( fd );
  346. if (( ret == -1 )&&( errno == EINTR )&&(g_running))
  347. continue;
  348. return ret;
  349. }
  350. }
  351. static void lsapi_close_connection(LSAPI_Request *pReq)
  352. {
  353. if (pReq->m_fd == -1)
  354. return;
  355. lsapi_close(pReq->m_fd);
  356. pReq->m_fd = -1;
  357. if (s_busy_workers)
  358. __sync_fetch_and_sub(s_busy_workers, 1);
  359. if (s_worker_status)
  360. __sync_lock_test_and_set(&s_worker_status->m_state, LSAPI_STATE_IDLE);
  361. }
  362. static inline ssize_t lsapi_read( int fd, void * pBuf, size_t len )
  363. {
  364. ssize_t ret;
  365. while( 1 )
  366. {
  367. ret = read( fd, (char *)pBuf, len );
  368. if (( ret == -1 )&&( errno == EINTR )&&(g_running))
  369. continue;
  370. return ret;
  371. }
  372. }
  373. /*
  374. static int lsapi_write( int fd, const void * pBuf, int len )
  375. {
  376. int ret;
  377. const char * pCur;
  378. const char * pEnd;
  379. if ( len == 0 )
  380. return 0;
  381. pCur = (const char *)pBuf;
  382. pEnd = pCur + len;
  383. while( g_running && (pCur < pEnd) )
  384. {
  385. ret = write( fd, pCur, pEnd - pCur );
  386. if ( ret >= 0)
  387. pCur += ret;
  388. else if (( ret == -1 )&&( errno != EINTR ))
  389. return ret;
  390. }
  391. return pCur - (const char *)pBuf;
  392. }
  393. */
  394. static int lsapi_writev( int fd, struct iovec ** pVec, int count, int totalLen )
  395. {
  396. int ret;
  397. int left = totalLen;
  398. int n = count;
  399. if (s_skip_write)
  400. return totalLen;
  401. while(( left > 0 )&&g_running )
  402. {
  403. ret = writev( fd, *pVec, n );
  404. if ( ret > 0 )
  405. {
  406. left -= ret;
  407. if (( left <= 0)||( !g_running ))
  408. return totalLen - left;
  409. while( ret > 0 )
  410. {
  411. if ( (*pVec)->iov_len <= (unsigned int )ret )
  412. {
  413. ret -= (*pVec)->iov_len;
  414. ++(*pVec);
  415. }
  416. else
  417. {
  418. (*pVec)->iov_base = (char *)(*pVec)->iov_base + ret;
  419. (*pVec)->iov_len -= ret;
  420. break;
  421. }
  422. }
  423. }
  424. else if ( ret == -1 )
  425. {
  426. if ( errno == EAGAIN )
  427. {
  428. if ( totalLen - left > 0 )
  429. return totalLen - left;
  430. else
  431. return -1;
  432. }
  433. else if ( errno != EINTR )
  434. return ret;
  435. }
  436. }
  437. return totalLen - left;
  438. }
  439. /*
  440. static int getTotalLen( struct iovec * pVec, int count )
  441. {
  442. struct iovec * pEnd = pVec + count;
  443. int total = 0;
  444. while( pVec < pEnd )
  445. {
  446. total += pVec->iov_len;
  447. ++pVec;
  448. }
  449. return total;
  450. }
  451. */
  452. static inline int allocateBuf( LSAPI_Request * pReq, int size )
  453. {
  454. char * pBuf = (char *)realloc( pReq->m_pReqBuf, size );
  455. if ( pBuf )
  456. {
  457. pReq->m_pReqBuf = pBuf;
  458. pReq->m_reqBufSize = size;
  459. pReq->m_pHeader = (struct lsapi_req_header *)pReq->m_pReqBuf;
  460. return 0;
  461. }
  462. return -1;
  463. }
  464. static int allocateIovec( LSAPI_Request * pReq, int n )
  465. {
  466. struct iovec * p = (struct iovec *)realloc(
  467. pReq->m_pIovec, sizeof(struct iovec) * n );
  468. if ( !p )
  469. return -1;
  470. pReq->m_pIovecToWrite = p + ( pReq->m_pIovecToWrite - pReq->m_pIovec );
  471. pReq->m_pIovecCur = p + ( pReq->m_pIovecCur - pReq->m_pIovec );
  472. pReq->m_pIovec = p;
  473. pReq->m_pIovecEnd = p + n;
  474. return 0;
  475. }
  476. static int allocateRespHeaderBuf( LSAPI_Request * pReq, int size )
  477. {
  478. char * p = (char *)realloc( pReq->m_pRespHeaderBuf, size );
  479. if ( !p )
  480. return -1;
  481. pReq->m_pRespHeaderBufPos = p + ( pReq->m_pRespHeaderBufPos - pReq->m_pRespHeaderBuf );
  482. pReq->m_pRespHeaderBuf = p;
  483. pReq->m_pRespHeaderBufEnd = p + size;
  484. return 0;
  485. }
  486. static inline int verifyHeader( struct lsapi_packet_header * pHeader, char pktType )
  487. {
  488. if (( LSAPI_VERSION_B0 != pHeader->m_versionB0 )||
  489. ( LSAPI_VERSION_B1 != pHeader->m_versionB1 )||
  490. ( pktType != pHeader->m_type ))
  491. return -1;
  492. if ( LSAPI_ENDIAN != (pHeader->m_flag & LSAPI_ENDIAN_BIT ))
  493. {
  494. register char b;
  495. b = pHeader->m_packetLen.m_bytes[0];
  496. pHeader->m_packetLen.m_bytes[0] = pHeader->m_packetLen.m_bytes[3];
  497. pHeader->m_packetLen.m_bytes[3] = b;
  498. b = pHeader->m_packetLen.m_bytes[1];
  499. pHeader->m_packetLen.m_bytes[1] = pHeader->m_packetLen.m_bytes[2];
  500. pHeader->m_packetLen.m_bytes[2] = b;
  501. }
  502. return pHeader->m_packetLen.m_iLen;
  503. }
  504. static int allocateEnvList( struct LSAPI_key_value_pair ** pEnvList,
  505. int *curSize, int newSize )
  506. {
  507. struct LSAPI_key_value_pair * pBuf;
  508. if ( *curSize >= newSize )
  509. return 0;
  510. if ( newSize > 8192 )
  511. return -1;
  512. pBuf = (struct LSAPI_key_value_pair *)realloc( *pEnvList, newSize *
  513. sizeof(struct LSAPI_key_value_pair) );
  514. if ( pBuf )
  515. {
  516. *pEnvList = pBuf;
  517. *curSize = newSize;
  518. return 0;
  519. }
  520. else
  521. return -1;
  522. }
  523. static inline int isPipe( int fd )
  524. {
  525. char achPeer[128];
  526. socklen_t len = 128;
  527. if (( getpeername( fd, (struct sockaddr *)achPeer, &len ) != 0 )&&
  528. ( errno == ENOTCONN ))
  529. return 0;
  530. else
  531. return 1;
  532. }
  533. static int parseEnv( struct LSAPI_key_value_pair * pEnvList, int count,
  534. char **pBegin, char * pEnd )
  535. {
  536. struct LSAPI_key_value_pair * pEnvEnd;
  537. int keyLen = 0, valLen = 0;
  538. if ( count > 8192 )
  539. return -1;
  540. pEnvEnd = pEnvList + count;
  541. while( pEnvList != pEnvEnd )
  542. {
  543. if ( pEnd - *pBegin < 4 )
  544. return -1;
  545. keyLen = *((unsigned char *)((*pBegin)++));
  546. keyLen = (keyLen << 8) + *((unsigned char *)((*pBegin)++));
  547. valLen = *((unsigned char *)((*pBegin)++));
  548. valLen = (valLen << 8) + *((unsigned char *)((*pBegin)++));
  549. if ( *pBegin + keyLen + valLen > pEnd )
  550. return -1;
  551. if (( !keyLen )||( !valLen ))
  552. return -1;
  553. pEnvList->pKey = *pBegin;
  554. *pBegin += keyLen;
  555. pEnvList->pValue = *pBegin;
  556. *pBegin += valLen;
  557. pEnvList->keyLen = keyLen - 1;
  558. pEnvList->valLen = valLen - 1;
  559. ++pEnvList;
  560. }
  561. if ( memcmp( *pBegin, "\0\0\0\0", 4 ) != 0 )
  562. return -1;
  563. *pBegin += 4;
  564. return 0;
  565. }
  566. static inline void swapIntEndian( int * pInteger )
  567. {
  568. char * p = (char *)pInteger;
  569. register char b;
  570. b = p[0];
  571. p[0] = p[3];
  572. p[3] = b;
  573. b = p[1];
  574. p[1] = p[2];
  575. p[2] = b;
  576. }
  577. static inline void fixEndian( LSAPI_Request * pReq )
  578. {
  579. struct lsapi_req_header *p= pReq->m_pHeader;
  580. swapIntEndian( &p->m_httpHeaderLen );
  581. swapIntEndian( &p->m_reqBodyLen );
  582. swapIntEndian( &p->m_scriptFileOff );
  583. swapIntEndian( &p->m_scriptNameOff );
  584. swapIntEndian( &p->m_queryStringOff );
  585. swapIntEndian( &p->m_requestMethodOff );
  586. swapIntEndian( &p->m_cntUnknownHeaders );
  587. swapIntEndian( &p->m_cntEnv );
  588. swapIntEndian( &p->m_cntSpecialEnv );
  589. }
  590. static void fixHeaderIndexEndian( LSAPI_Request * pReq )
  591. {
  592. int i;
  593. for( i = 0; i < H_TRANSFER_ENCODING; ++i )
  594. {
  595. if ( pReq->m_pHeaderIndex->m_headerOff[i] )
  596. {
  597. register char b;
  598. char * p = (char *)(&pReq->m_pHeaderIndex->m_headerLen[i]);
  599. b = p[0];
  600. p[0] = p[1];
  601. p[1] = b;
  602. swapIntEndian( &pReq->m_pHeaderIndex->m_headerOff[i] );
  603. }
  604. }
  605. if ( pReq->m_pHeader->m_cntUnknownHeaders > 0 )
  606. {
  607. struct lsapi_header_offset * pCur, *pEnd;
  608. pCur = pReq->m_pUnknownHeader;
  609. pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders;
  610. while( pCur < pEnd )
  611. {
  612. swapIntEndian( &pCur->nameOff );
  613. swapIntEndian( &pCur->nameLen );
  614. swapIntEndian( &pCur->valueOff );
  615. swapIntEndian( &pCur->valueLen );
  616. ++pCur;
  617. }
  618. }
  619. }
  620. static int validateHeaders( LSAPI_Request * pReq )
  621. {
  622. int totalLen = pReq->m_pHeader->m_httpHeaderLen;
  623. int i;
  624. for(i = 0; i < H_TRANSFER_ENCODING; ++i)
  625. {
  626. if ( pReq->m_pHeaderIndex->m_headerOff[i] )
  627. {
  628. if (pReq->m_pHeaderIndex->m_headerOff[i] > totalLen
  629. || pReq->m_pHeaderIndex->m_headerLen[i]
  630. + pReq->m_pHeaderIndex->m_headerOff[i] > totalLen)
  631. return -1;
  632. }
  633. }
  634. if (pReq->m_pHeader->m_cntUnknownHeaders > 0)
  635. {
  636. struct lsapi_header_offset * pCur, *pEnd;
  637. pCur = pReq->m_pUnknownHeader;
  638. pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders;
  639. while( pCur < pEnd )
  640. {
  641. if (pCur->nameOff > totalLen
  642. || pCur->nameOff + pCur->nameLen > totalLen
  643. || pCur->valueOff > totalLen
  644. || pCur->valueOff + pCur->valueLen > totalLen)
  645. return -1;
  646. ++pCur;
  647. }
  648. }
  649. return 0;
  650. }
  651. static uid_t s_uid = 0;
  652. static uid_t s_defaultUid; //web server need set this
  653. static gid_t s_defaultGid;
  654. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  655. #define LSAPI_LVE_DISABLED 0
  656. #define LSAPI_LVE_ENABLED 1
  657. #define LSAPI_CAGEFS_ENABLED 2
  658. #define LSAPI_CAGEFS_NO_SUEXEC 3
  659. struct liblve;
  660. static int s_enable_lve = LSAPI_LVE_DISABLED;
  661. static struct liblve * s_lve = NULL;
  662. static void *s_liblve;
  663. static int (*fp_lve_is_available)(void) = NULL;
  664. static int (*fp_lve_instance_init)(struct liblve *) = NULL;
  665. static int (*fp_lve_destroy)(struct liblve *) = NULL;
  666. static int (*fp_lve_enter)(struct liblve *, uint32_t, int32_t, int32_t, uint32_t *) = NULL;
  667. static int (*fp_lve_leave)(struct liblve *, uint32_t *) = NULL;
  668. static int (*fp_lve_jail)( struct passwd *, char *) = NULL;
  669. static int lsapi_load_lve_lib(void)
  670. {
  671. s_liblve = DL_LOAD("liblve.so.0");
  672. if (s_liblve)
  673. {
  674. fp_lve_is_available = dlsym(s_liblve, "lve_is_available");
  675. if (dlerror() == NULL)
  676. {
  677. if ( !(*fp_lve_is_available)() )
  678. {
  679. int uid = getuid();
  680. if ( uid )
  681. {
  682. setreuid( s_uid, uid );
  683. if ( !(*fp_lve_is_available)() )
  684. s_enable_lve = 0;
  685. setreuid( uid, s_uid );
  686. }
  687. }
  688. }
  689. }
  690. else
  691. {
  692. s_enable_lve = LSAPI_LVE_DISABLED;
  693. }
  694. return (s_liblve)? 0 : -1;
  695. }
  696. static int init_lve_ex(void)
  697. {
  698. int rc;
  699. if ( !s_liblve )
  700. return -1;
  701. fp_lve_instance_init = dlsym(s_liblve, "lve_instance_init");
  702. fp_lve_destroy = dlsym(s_liblve, "lve_destroy");
  703. fp_lve_enter = dlsym(s_liblve, "lve_enter");
  704. fp_lve_leave = dlsym(s_liblve, "lve_leave");
  705. if ( s_enable_lve >= LSAPI_CAGEFS_ENABLED )
  706. fp_lve_jail = dlsym(s_liblve, "jail" );
  707. if ( s_lve == NULL )
  708. {
  709. rc = (*fp_lve_instance_init)(NULL);
  710. s_lve = malloc(rc);
  711. }
  712. rc = (*fp_lve_instance_init)(s_lve);
  713. if (rc != 0)
  714. {
  715. perror( "LSAPI: Unable to initialize LVE" );
  716. free( s_lve );
  717. s_lve = NULL;
  718. return -1;
  719. }
  720. return 0;
  721. }
  722. #endif
  723. static int readSecret( const char * pSecretFile )
  724. {
  725. struct stat st;
  726. int fd = open( pSecretFile, O_RDONLY , 0600 );
  727. if ( fd == -1 )
  728. {
  729. lsapi_log("LSAPI: failed to open secret file: %s!\n", pSecretFile );
  730. return -1;
  731. }
  732. if ( fstat( fd, &st ) == -1 )
  733. {
  734. lsapi_log("LSAPI: failed to check state of file: %s!\n", pSecretFile );
  735. close( fd );
  736. return -1;
  737. }
  738. /*
  739. if ( st.st_uid != s_uid )
  740. {
  741. lsapi_log("LSAPI: file owner check failure: %s!\n", pSecretFile );
  742. close( fd );
  743. return -1;
  744. }
  745. */
  746. if ( st.st_mode & 0077 )
  747. {
  748. lsapi_log("LSAPI: file permission check failure: %s\n", pSecretFile );
  749. close( fd );
  750. return -1;
  751. }
  752. if ( read( fd, s_secret, 16 ) < 16 )
  753. {
  754. lsapi_log("LSAPI: failed to read secret from secret file: %s\n", pSecretFile );
  755. close( fd );
  756. return -1;
  757. }
  758. close( fd );
  759. return 0;
  760. }
  761. int LSAPI_is_suEXEC_Daemon(void)
  762. {
  763. if (( !s_uid )&&( s_secret[0] ))
  764. return 1;
  765. else
  766. return 0;
  767. }
  768. static int LSAPI_perror_r( LSAPI_Request * pReq, const char * pErr1, const char *pErr2 )
  769. {
  770. char achError[4096];
  771. int n = snprintf(achError, sizeof(achError), "[%d] %s:%s: %s\n", getpid(),
  772. pErr1, (pErr2)?pErr2:"", strerror(errno));
  773. if (n > (int)sizeof(achError))
  774. n = sizeof(achError);
  775. if ( pReq )
  776. LSAPI_Write_Stderr_r( pReq, achError, n );
  777. else
  778. write( STDERR_FILENO, achError, n );
  779. return 0;
  780. }
  781. static int lsapi_lve_error( LSAPI_Request * pReq )
  782. {
  783. static const char * headers[] =
  784. {
  785. "Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0",
  786. "Pragma: no-cache",
  787. "Retry-After: 60",
  788. "Content-Type: text/html",
  789. NULL
  790. };
  791. static const char achBody[] =
  792. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  793. "<HTML><HEAD>\n<TITLE>508 Resource Limit Is Reached</TITLE>\n"
  794. "</HEAD><BODY>\n" "<H1>Resource Limit Is Reached</H1>\n"
  795. "The website is temporarily unable to service your request as it exceeded resource limit.\n"
  796. "Please try again later.\n"
  797. "<HR>\n"
  798. "</BODY></HTML>\n";
  799. LSAPI_ErrResponse_r( pReq, 508, headers, achBody, sizeof( achBody ) - 1 );
  800. return 0;
  801. }
  802. static int lsapi_enterLVE( LSAPI_Request * pReq, uid_t uid )
  803. {
  804. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  805. if ( s_lve && uid ) //root user should not do that
  806. {
  807. uint32_t cookie;
  808. int ret = -1;
  809. ret = (*fp_lve_enter)(s_lve, uid, -1, -1, &cookie);
  810. if ( ret < 0 )
  811. {
  812. lsapi_log("enter LVE (%d) : result: %d !\n", uid, ret );
  813. LSAPI_perror_r(pReq, "LSAPI: lve_enter() failure, reached resource limit.", NULL );
  814. lsapi_lve_error( pReq );
  815. return -1;
  816. }
  817. }
  818. #endif
  819. return 0;
  820. }
  821. static int lsapi_jailLVE( LSAPI_Request * pReq, uid_t uid, struct passwd * pw )
  822. {
  823. int ret = 0;
  824. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  825. char error_msg[1024] = "";
  826. ret = (*fp_lve_jail)( pw, error_msg );
  827. if ( ret < 0 )
  828. {
  829. lsapi_log("LSAPI: LVE jail(%d) result: %d, error: %s !\n",
  830. uid, ret, error_msg );
  831. LSAPI_perror_r( pReq, "LSAPI: jail() failure.", NULL );
  832. return -1;
  833. }
  834. #endif
  835. return ret;
  836. }
  837. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  838. static int lsapi_initLVE(void)
  839. {
  840. const char * pEnv;
  841. if ( (pEnv = getenv( "LSAPI_LVE_ENABLE" ))!= NULL )
  842. {
  843. s_enable_lve = atol( pEnv );
  844. pEnv = NULL;
  845. }
  846. else if ( (pEnv = getenv( "LVE_ENABLE" ))!= NULL )
  847. {
  848. s_enable_lve = atol( pEnv );
  849. pEnv = NULL;
  850. }
  851. if ( s_enable_lve && !s_uid )
  852. {
  853. lsapi_load_lve_lib();
  854. if ( s_enable_lve )
  855. {
  856. return init_lve_ex();
  857. }
  858. }
  859. return 0;
  860. }
  861. #endif
  862. static int setUID_LVE(LSAPI_Request * pReq, uid_t uid, gid_t gid, const char * pChroot)
  863. {
  864. int rv;
  865. struct passwd * pw;
  866. pw = getpwuid( uid );
  867. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  868. if ( s_lve )
  869. {
  870. if( lsapi_enterLVE( pReq, uid ) == -1 )
  871. return -1;
  872. if ( pw && fp_lve_jail)
  873. {
  874. rv = lsapi_jailLVE( pReq, uid, pw );
  875. if ( rv == -1 )
  876. return -1;
  877. if (( rv == 1 )&&(s_enable_lve == LSAPI_CAGEFS_NO_SUEXEC )) //this mode only use cageFS, does not use suEXEC
  878. {
  879. uid = s_defaultUid;
  880. gid = s_defaultGid;
  881. pw = getpwuid( uid );
  882. }
  883. }
  884. }
  885. #endif
  886. //if ( !uid || !gid ) //do not allow root
  887. //{
  888. // return -1;
  889. //}
  890. #if defined(__FreeBSD__ ) || defined(__NetBSD__) || defined(__OpenBSD__) \
  891. || defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
  892. if ( s_enable_core_dump )
  893. lsapi_enable_core_dump();
  894. #endif
  895. rv = setgid(gid);
  896. if (rv == -1)
  897. {
  898. LSAPI_perror_r(pReq, "LSAPI: setgid()", NULL);
  899. return -1;
  900. }
  901. if ( pw && (pw->pw_gid == gid ))
  902. {
  903. rv = initgroups( pw->pw_name, gid );
  904. if (rv == -1)
  905. {
  906. LSAPI_perror_r(pReq, "LSAPI: initgroups()", NULL);
  907. return -1;
  908. }
  909. }
  910. else
  911. {
  912. rv = setgroups(1, &gid);
  913. if (rv == -1)
  914. {
  915. LSAPI_perror_r(pReq, "LSAPI: setgroups()", NULL);
  916. }
  917. }
  918. if ( pChroot )
  919. {
  920. rv = chroot( pChroot );
  921. if ( rv == -1 )
  922. {
  923. LSAPI_perror_r(pReq, "LSAPI: chroot()", NULL);
  924. return -1;
  925. }
  926. }
  927. rv = setuid(uid);
  928. if (rv == -1)
  929. {
  930. LSAPI_perror_r(pReq, "LSAPI: setuid()", NULL);
  931. return -1;
  932. }
  933. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
  934. if ( s_enable_core_dump )
  935. lsapi_enable_core_dump();
  936. #endif
  937. return 0;
  938. }
  939. static int lsapi_suexec_auth( LSAPI_Request *pReq,
  940. char * pAuth, int len, char * pUgid, int ugidLen )
  941. {
  942. lsapi_MD5_CTX md5ctx;
  943. unsigned char achMD5[16];
  944. if ( len < 32 )
  945. return -1;
  946. memmove( achMD5, pAuth + 16, 16 );
  947. memmove( pAuth + 16, s_secret, 16 );
  948. lsapi_MD5Init( &md5ctx );
  949. lsapi_MD5Update( &md5ctx, (unsigned char *)pAuth, 32 );
  950. lsapi_MD5Update( &md5ctx, (unsigned char *)pUgid, 8 );
  951. lsapi_MD5Final( (unsigned char *)pAuth + 16, &md5ctx);
  952. if ( memcmp( achMD5, pAuth + 16, 16 ) == 0 )
  953. return 0;
  954. return 1;
  955. }
  956. static int lsapi_changeUGid( LSAPI_Request * pReq )
  957. {
  958. int uid = s_defaultUid;
  959. int gid = s_defaultGid;
  960. const char *pStderrLog;
  961. const char *pChroot = NULL;
  962. struct LSAPI_key_value_pair * pEnv;
  963. struct LSAPI_key_value_pair * pAuth;
  964. int i;
  965. if ( s_uid )
  966. return 0;
  967. //with special ID 0x00
  968. //authenticate the suEXEC request;
  969. //first one should be MD5( nonce + lscgid secret )
  970. //remember to clear the secret after verification
  971. //it should be set at the end of special env
  972. i = pReq->m_pHeader->m_cntSpecialEnv - 1;
  973. if ( i >= 0 )
  974. {
  975. pEnv = pReq->m_pSpecialEnvList + i;
  976. if (( *pEnv->pKey == '\000' )&&
  977. ( strcmp( pEnv->pKey+1, "SUEXEC_AUTH" ) == 0 ))
  978. {
  979. --pReq->m_pHeader->m_cntSpecialEnv;
  980. pAuth = pEnv--;
  981. if (( *pEnv->pKey == '\000' )&&
  982. ( strcmp( pEnv->pKey+1, "SUEXEC_UGID" ) == 0 ))
  983. {
  984. --pReq->m_pHeader->m_cntSpecialEnv;
  985. uid = *(uint32_t *)pEnv->pValue;
  986. gid = *(((uint32_t *)pEnv->pValue) + 1 );
  987. //lsapi_log("LSAPI: SUEXEC_UGID set UID: %d, GID: %d\n", uid, gid );
  988. }
  989. else
  990. {
  991. lsapi_log("LSAPI: missing SUEXEC_UGID env, use default user!\n" );
  992. pEnv = NULL;
  993. }
  994. if ( pEnv&& lsapi_suexec_auth( pReq, pAuth->pValue, pAuth->valLen, pEnv->pValue, pEnv->valLen ) == 0 )
  995. {
  996. //read UID, GID from specialEnv
  997. }
  998. else
  999. {
  1000. //authentication error
  1001. lsapi_log("LSAPI: SUEXEC_AUTH authentication failed, use default user!\n" );
  1002. uid = 0;
  1003. }
  1004. }
  1005. else
  1006. {
  1007. //lsapi_log("LSAPI: no SUEXEC_AUTH env, use default user!\n" );
  1008. }
  1009. }
  1010. if ( !uid )
  1011. {
  1012. uid = s_defaultUid;
  1013. gid = s_defaultGid;
  1014. }
  1015. //change uid
  1016. if ( setUID_LVE( pReq, uid, gid, pChroot ) == -1 )
  1017. {
  1018. return -1;
  1019. }
  1020. s_uid = uid;
  1021. if ( pReq->m_fdListen != -1 )
  1022. {
  1023. close( pReq->m_fdListen );
  1024. pReq->m_fdListen = -1;
  1025. }
  1026. pStderrLog = LSAPI_GetEnv_r( pReq, "LSAPI_STDERR_LOG");
  1027. if (pStderrLog)
  1028. lsapi_reopen_stderr(pStderrLog);
  1029. return 0;
  1030. }
  1031. static int parseContentLenFromHeader(LSAPI_Request * pReq)
  1032. {
  1033. const char * pContentLen = LSAPI_GetHeader_r( pReq, H_CONTENT_LENGTH );
  1034. if ( pContentLen )
  1035. pReq->m_reqBodyLen = strtoll( pContentLen, NULL, 10 );
  1036. return 0;
  1037. }
  1038. static int parseRequest( LSAPI_Request * pReq, int totalLen )
  1039. {
  1040. int shouldFixEndian;
  1041. char * pBegin = pReq->m_pReqBuf + sizeof( struct lsapi_req_header );
  1042. char * pEnd = pReq->m_pReqBuf + totalLen;
  1043. shouldFixEndian = ( LSAPI_ENDIAN != (
  1044. pReq->m_pHeader->m_pktHeader.m_flag & LSAPI_ENDIAN_BIT ) );
  1045. if ( shouldFixEndian )
  1046. {
  1047. fixEndian( pReq );
  1048. }
  1049. if ( (pReq->m_specialEnvListSize < pReq->m_pHeader->m_cntSpecialEnv )&&
  1050. allocateEnvList( &pReq->m_pSpecialEnvList,
  1051. &pReq->m_specialEnvListSize,
  1052. pReq->m_pHeader->m_cntSpecialEnv ) == -1 )
  1053. return -1;
  1054. if ( (pReq->m_envListSize < pReq->m_pHeader->m_cntEnv )&&
  1055. allocateEnvList( &pReq->m_pEnvList, &pReq->m_envListSize,
  1056. pReq->m_pHeader->m_cntEnv ) == -1 )
  1057. return -1;
  1058. if ( parseEnv( pReq->m_pSpecialEnvList,
  1059. pReq->m_pHeader->m_cntSpecialEnv,
  1060. &pBegin, pEnd ) == -1 )
  1061. return -1;
  1062. if ( parseEnv( pReq->m_pEnvList, pReq->m_pHeader->m_cntEnv,
  1063. &pBegin, pEnd ) == -1 )
  1064. return -1;
  1065. if (pReq->m_pHeader->m_scriptFileOff < 0
  1066. || pReq->m_pHeader->m_scriptFileOff >= totalLen
  1067. || pReq->m_pHeader->m_scriptNameOff < 0
  1068. || pReq->m_pHeader->m_scriptNameOff >= totalLen
  1069. || pReq->m_pHeader->m_queryStringOff < 0
  1070. || pReq->m_pHeader->m_queryStringOff >= totalLen
  1071. || pReq->m_pHeader->m_requestMethodOff < 0
  1072. || pReq->m_pHeader->m_requestMethodOff >= totalLen)
  1073. {
  1074. lsapi_log("Bad request header - ERROR#1\n");
  1075. return -1;
  1076. }
  1077. pReq->m_pScriptFile = pReq->m_pReqBuf + pReq->m_pHeader->m_scriptFileOff;
  1078. pReq->m_pScriptName = pReq->m_pReqBuf + pReq->m_pHeader->m_scriptNameOff;
  1079. pReq->m_pQueryString = pReq->m_pReqBuf + pReq->m_pHeader->m_queryStringOff;
  1080. pReq->m_pRequestMethod = pReq->m_pReqBuf + pReq->m_pHeader->m_requestMethodOff;
  1081. pBegin = pReq->m_pReqBuf + (( pBegin - pReq->m_pReqBuf + 7 ) & (~0x7));
  1082. pReq->m_pHeaderIndex = ( struct lsapi_http_header_index * )pBegin;
  1083. pBegin += sizeof( struct lsapi_http_header_index );
  1084. pReq->m_pUnknownHeader = (struct lsapi_header_offset *)pBegin;
  1085. pBegin += sizeof( struct lsapi_header_offset) *
  1086. pReq->m_pHeader->m_cntUnknownHeaders;
  1087. pReq->m_pHttpHeader = pBegin;
  1088. pBegin += pReq->m_pHeader->m_httpHeaderLen;
  1089. if ( pBegin != pEnd )
  1090. {
  1091. lsapi_log("Request header does match total size, total: %d, "
  1092. "real: %ld\n", totalLen, pBegin - pReq->m_pReqBuf );
  1093. return -1;
  1094. }
  1095. if ( shouldFixEndian )
  1096. {
  1097. fixHeaderIndexEndian( pReq );
  1098. }
  1099. if (validateHeaders(pReq) == -1)
  1100. {
  1101. lsapi_log("Bad request header - ERROR#2\n");
  1102. return -1;
  1103. }
  1104. pReq->m_reqBodyLen = pReq->m_pHeader->m_reqBodyLen;
  1105. if ( pReq->m_reqBodyLen == -2 )
  1106. {
  1107. parseContentLenFromHeader(pReq);
  1108. }
  1109. return 0;
  1110. }
  1111. //OPTIMIZATION
  1112. static char s_accept_notify = 0;
  1113. static char s_schedule_notify = 0;
  1114. static char s_notify_scheduled = 0;
  1115. static char s_notified_pid = 0;
  1116. static struct lsapi_packet_header s_ack = {'L', 'S',
  1117. LSAPI_REQ_RECEIVED, LSAPI_ENDIAN, {LSAPI_PACKET_HEADER_LEN} };
  1118. static struct lsapi_packet_header s_conn_close_pkt = {'L', 'S',
  1119. LSAPI_CONN_CLOSE, LSAPI_ENDIAN, {LSAPI_PACKET_HEADER_LEN} };
  1120. static inline int send_notification_pkt( int fd, struct lsapi_packet_header *pkt )
  1121. {
  1122. if ( write( fd, pkt, LSAPI_PACKET_HEADER_LEN ) < LSAPI_PACKET_HEADER_LEN )
  1123. return -1;
  1124. return 0;
  1125. }
  1126. static inline int send_req_received_notification( int fd )
  1127. {
  1128. return send_notification_pkt(fd, &s_ack);
  1129. }
  1130. static inline int send_conn_close_notification( int fd )
  1131. {
  1132. return send_notification_pkt(fd, &s_conn_close_pkt);
  1133. }
  1134. //static void lsapi_sigalarm( int sig )
  1135. //{
  1136. // if ( s_notify_scheduled )
  1137. // {
  1138. // s_notify_scheduled = 0;
  1139. // if ( g_req.m_fd != -1 )
  1140. // write_req_received_notification( g_req.m_fd );
  1141. // }
  1142. //}
  1143. static inline int lsapi_schedule_notify(void)
  1144. {
  1145. if ( !s_notify_scheduled )
  1146. {
  1147. alarm( 2 );
  1148. s_notify_scheduled = 1;
  1149. }
  1150. return 0;
  1151. }
  1152. static inline int notify_req_received( int fd )
  1153. {
  1154. if ( s_schedule_notify )
  1155. return lsapi_schedule_notify();
  1156. return send_req_received_notification( fd );
  1157. }
  1158. static inline int lsapi_notify_pid( int fd )
  1159. {
  1160. char achBuf[16];
  1161. lsapi_buildPacketHeader( (struct lsapi_packet_header *)achBuf, LSAPI_STDERR_STREAM,
  1162. 8 + LSAPI_PACKET_HEADER_LEN );
  1163. memmove( &achBuf[8], "\0PID", 4 );
  1164. *((int *)&achBuf[12]) = getpid();
  1165. if ( write( fd, achBuf, 16 ) < 16 )
  1166. return -1;
  1167. return 0;
  1168. }
  1169. static char s_conn_key_packet[16];
  1170. static inline int init_conn_key( int fd )
  1171. {
  1172. struct lsapi_packet_header * pHeader = (struct lsapi_packet_header *)s_conn_key_packet;
  1173. struct timeval tv;
  1174. int i;
  1175. gettimeofday( &tv, NULL );
  1176. srand( (tv.tv_sec % 0x1000 + tv.tv_usec) ^ rand() );
  1177. for( i = 8; i < 16; ++i )
  1178. {
  1179. s_conn_key_packet[i]=(int) (256.0*rand()/(RAND_MAX+1.0));
  1180. }
  1181. lsapi_buildPacketHeader( pHeader, LSAPI_REQ_RECEIVED,
  1182. 8 + LSAPI_PACKET_HEADER_LEN );
  1183. if ( write( fd, s_conn_key_packet, LSAPI_PACKET_HEADER_LEN+8 )
  1184. < LSAPI_PACKET_HEADER_LEN+8 )
  1185. return -1;
  1186. return 0;
  1187. }
  1188. static int readReq( LSAPI_Request * pReq )
  1189. {
  1190. int len;
  1191. int packetLen;
  1192. if ( !pReq )
  1193. return -1;
  1194. if ( pReq->m_reqBufSize < 8192 )
  1195. {
  1196. if ( allocateBuf( pReq, 8192 ) == -1 )
  1197. return -1;
  1198. }
  1199. while ( pReq->m_bufRead < LSAPI_PACKET_HEADER_LEN )
  1200. {
  1201. len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf, pReq->m_reqBufSize );
  1202. if ( len <= 0 )
  1203. return -1;
  1204. pReq->m_bufRead += len;
  1205. }
  1206. pReq->m_reqState = LSAPI_ST_REQ_HEADER;
  1207. packetLen = verifyHeader( &pReq->m_pHeader->m_pktHeader, LSAPI_BEGIN_REQUEST );
  1208. if ( packetLen < 0 )
  1209. {
  1210. lsapi_log("packetLen < 0\n");
  1211. return -1;
  1212. }
  1213. if ( packetLen > LSAPI_MAX_HEADER_LEN )
  1214. {
  1215. lsapi_log("packetLen > %d\n", LSAPI_MAX_HEADER_LEN );
  1216. return -1;
  1217. }
  1218. if ( packetLen + 1024 > pReq->m_reqBufSize )
  1219. {
  1220. if ( allocateBuf( pReq, packetLen + 1024 ) == -1 )
  1221. return -1;
  1222. }
  1223. while( packetLen > pReq->m_bufRead )
  1224. {
  1225. len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf + pReq->m_bufRead, packetLen - pReq->m_bufRead );
  1226. if ( len <= 0 )
  1227. return -1;
  1228. pReq->m_bufRead += len;
  1229. }
  1230. if ( parseRequest( pReq, packetLen ) < 0 )
  1231. {
  1232. lsapi_log("ParseRequest error\n");
  1233. return -1;
  1234. }
  1235. pReq->m_reqState = LSAPI_ST_REQ_BODY | LSAPI_ST_RESP_HEADER;
  1236. if ( !s_uid )
  1237. {
  1238. if ( lsapi_changeUGid( pReq ) )
  1239. return -1;
  1240. memset(s_secret, 0, sizeof(s_secret));
  1241. }
  1242. pReq->m_bufProcessed = packetLen;
  1243. //OPTIMIZATION
  1244. if ( !s_accept_notify && !s_notified_pid )
  1245. return notify_req_received( pReq->m_fd );
  1246. else
  1247. {
  1248. s_notified_pid = 0;
  1249. return 0;
  1250. }
  1251. }
  1252. int LSAPI_Init(void)
  1253. {
  1254. if ( !g_inited )
  1255. {
  1256. s_uid = geteuid();
  1257. s_secret[0] = 0;
  1258. lsapi_signal(SIGPIPE, lsapi_sigpipe);
  1259. lsapi_signal(SIGUSR1, lsapi_siguser1);
  1260. #if defined(SIGXFSZ) && defined(SIG_IGN)
  1261. signal(SIGXFSZ, SIG_IGN);
  1262. #endif
  1263. /* let STDOUT function as STDERR,
  1264. just in case writing to STDOUT directly */
  1265. dup2( 2, 1 );
  1266. if ( LSAPI_InitRequest( &g_req, LSAPI_SOCK_FILENO ) == -1 )
  1267. return -1;
  1268. g_inited = 1;
  1269. s_ppid = getppid();
  1270. void *pthread_lib = dlopen("libpthread.so", RTLD_LAZY);
  1271. if (pthread_lib)
  1272. pthread_atfork_func = dlsym(pthread_lib, "pthread_atfork");
  1273. }
  1274. return 0;
  1275. }
  1276. void LSAPI_Stop(void)
  1277. {
  1278. g_running = 0;
  1279. }
  1280. int LSAPI_IsRunning(void)
  1281. {
  1282. return g_running;
  1283. }
  1284. void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf cb)
  1285. {
  1286. s_proc_group_timer_cb = cb;
  1287. }
  1288. int LSAPI_InitRequest( LSAPI_Request * pReq, int fd )
  1289. {
  1290. int newfd;
  1291. if ( !pReq )
  1292. return -1;
  1293. memset( pReq, 0, sizeof( LSAPI_Request ) );
  1294. if ( allocateIovec( pReq, 16 ) == -1 )
  1295. return -1;
  1296. pReq->m_pRespBuf = pReq->m_pRespBufPos = (char *)malloc( LSAPI_RESP_BUF_SIZE );
  1297. if ( !pReq->m_pRespBuf )
  1298. return -1;
  1299. pReq->m_pRespBufEnd = pReq->m_pRespBuf + LSAPI_RESP_BUF_SIZE;
  1300. pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec + 1;
  1301. pReq->m_respPktHeaderEnd = &pReq->m_respPktHeader[5];
  1302. if ( allocateRespHeaderBuf( pReq, LSAPI_INIT_RESP_HEADER_LEN ) == -1 )
  1303. return -1;
  1304. if ( fd == STDIN_FILENO )
  1305. {
  1306. fd = dup( fd );
  1307. newfd = open( "/dev/null", O_RDWR );
  1308. dup2( newfd, STDIN_FILENO );
  1309. }
  1310. if ( isPipe( fd ) )
  1311. {
  1312. pReq->m_fdListen = -1;
  1313. pReq->m_fd = fd;
  1314. }
  1315. else
  1316. {
  1317. pReq->m_fdListen = fd;
  1318. pReq->m_fd = -1;
  1319. lsapi_set_nblock( fd, 1 );
  1320. }
  1321. return 0;
  1322. }
  1323. int LSAPI_Is_Listen( void )
  1324. {
  1325. return LSAPI_Is_Listen_r( &g_req );
  1326. }
  1327. int LSAPI_Is_Listen_r( LSAPI_Request * pReq)
  1328. {
  1329. return pReq->m_fdListen != -1;
  1330. }
  1331. int LSAPI_Accept_r( LSAPI_Request * pReq )
  1332. {
  1333. char achPeer[128];
  1334. socklen_t len;
  1335. int nodelay = 1;
  1336. if ( !pReq )
  1337. return -1;
  1338. if ( LSAPI_Finish_r( pReq ) == -1 )
  1339. return -1;
  1340. lsapi_set_nblock( pReq->m_fdListen , 0 );
  1341. while( g_running )
  1342. {
  1343. if ( pReq->m_fd == -1 )
  1344. {
  1345. if ( pReq->m_fdListen != -1)
  1346. {
  1347. len = sizeof( achPeer );
  1348. pReq->m_fd = accept( pReq->m_fdListen,
  1349. (struct sockaddr *)&achPeer, &len );
  1350. if ( pReq->m_fd == -1 )
  1351. {
  1352. if (( errno == EINTR )||( errno == EAGAIN))
  1353. continue;
  1354. else
  1355. return -1;
  1356. }
  1357. else
  1358. {
  1359. if (s_worker_status)
  1360. __sync_lock_test_and_set(&s_worker_status->m_state,
  1361. LSAPI_STATE_CONNECTED);
  1362. if (s_busy_workers)
  1363. __sync_fetch_and_add(s_busy_workers, 1);
  1364. lsapi_set_nblock( pReq->m_fd , 0 );
  1365. if (((struct sockaddr *)&achPeer)->sa_family == AF_INET )
  1366. {
  1367. setsockopt(pReq->m_fd, IPPROTO_TCP, TCP_NODELAY,
  1368. (char *)&nodelay, sizeof(nodelay));
  1369. }
  1370. //init_conn_key( pReq->m_fd );
  1371. //OPTIMIZATION
  1372. if ( s_accept_notify )
  1373. if ( notify_req_received( pReq->m_fd ) == -1 )
  1374. return -1;
  1375. }
  1376. }
  1377. else
  1378. return -1;
  1379. }
  1380. if ( !readReq( pReq ) )
  1381. break;
  1382. //abort();
  1383. lsapi_close_connection(pReq);
  1384. LSAPI_Reset_r( pReq );
  1385. }
  1386. return 0;
  1387. }
  1388. static struct lsapi_packet_header finish_close[2] =
  1389. {
  1390. {'L', 'S', LSAPI_RESP_END, LSAPI_ENDIAN, {LSAPI_PACKET_HEADER_LEN} },
  1391. {'L', 'S', LSAPI_CONN_CLOSE, LSAPI_ENDIAN, {LSAPI_PACKET_HEADER_LEN} }
  1392. };
  1393. int LSAPI_Finish_r( LSAPI_Request * pReq )
  1394. {
  1395. /* finish req body */
  1396. if ( !pReq )
  1397. return -1;
  1398. if (pReq->m_reqState)
  1399. {
  1400. if ( pReq->m_fd != -1 )
  1401. {
  1402. if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER )
  1403. {
  1404. LSAPI_FinalizeRespHeaders_r( pReq );
  1405. }
  1406. if ( pReq->m_pRespBufPos != pReq->m_pRespBuf )
  1407. {
  1408. Flush_RespBuf_r( pReq );
  1409. }
  1410. pReq->m_pIovecCur->iov_base = (void *)finish_close;
  1411. pReq->m_pIovecCur->iov_len = LSAPI_PACKET_HEADER_LEN;
  1412. pReq->m_totalLen += LSAPI_PACKET_HEADER_LEN;
  1413. ++pReq->m_pIovecCur;
  1414. LSAPI_Flush_r( pReq );
  1415. }
  1416. LSAPI_Reset_r( pReq );
  1417. }
  1418. return 0;
  1419. }
  1420. int LSAPI_End_Response_r(LSAPI_Request * pReq)
  1421. {
  1422. if (!pReq)
  1423. return -1;
  1424. if (pReq->m_reqState & LSAPI_ST_BACKGROUND)
  1425. return 0;
  1426. if (pReq->m_reqState)
  1427. {
  1428. if ( pReq->m_fd != -1 )
  1429. {
  1430. if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER )
  1431. {
  1432. if ( pReq->m_pRespHeaderBufPos <= pReq->m_pRespHeaderBuf )
  1433. return 0;
  1434. LSAPI_FinalizeRespHeaders_r( pReq );
  1435. }
  1436. if ( pReq->m_pRespBufPos != pReq->m_pRespBuf )
  1437. {
  1438. Flush_RespBuf_r( pReq );
  1439. }
  1440. pReq->m_pIovecCur->iov_base = (void *)finish_close;
  1441. pReq->m_pIovecCur->iov_len = LSAPI_PACKET_HEADER_LEN << 1;
  1442. pReq->m_totalLen += LSAPI_PACKET_HEADER_LEN << 1;
  1443. ++pReq->m_pIovecCur;
  1444. LSAPI_Flush_r( pReq );
  1445. lsapi_close_connection(pReq);
  1446. }
  1447. pReq->m_reqState |= LSAPI_ST_BACKGROUND;
  1448. }
  1449. return 0;
  1450. }
  1451. void LSAPI_Reset_r( LSAPI_Request * pReq )
  1452. {
  1453. pReq->m_pRespBufPos = pReq->m_pRespBuf;
  1454. pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec + 1;
  1455. pReq->m_pRespHeaderBufPos = pReq->m_pRespHeaderBuf;
  1456. memset( &pReq->m_pHeaderIndex, 0,
  1457. (char *)(pReq->m_respHeaderLen) - (char *)&pReq->m_pHeaderIndex );
  1458. }
  1459. int LSAPI_Release_r( LSAPI_Request * pReq )
  1460. {
  1461. if ( pReq->m_pReqBuf )
  1462. free( pReq->m_pReqBuf );
  1463. if ( pReq->m_pSpecialEnvList )
  1464. free( pReq->m_pSpecialEnvList );
  1465. if ( pReq->m_pEnvList )
  1466. free( pReq->m_pEnvList );
  1467. if ( pReq->m_pRespHeaderBuf )
  1468. free( pReq->m_pRespHeaderBuf );
  1469. return 0;
  1470. }
  1471. char * LSAPI_GetHeader_r( LSAPI_Request * pReq, int headerIndex )
  1472. {
  1473. int off;
  1474. if ( !pReq || ((unsigned int)headerIndex > H_TRANSFER_ENCODING) )
  1475. return NULL;
  1476. off = pReq->m_pHeaderIndex->m_headerOff[ headerIndex ];
  1477. if ( !off )
  1478. return NULL;
  1479. if ( *(pReq->m_pHttpHeader + off
  1480. + pReq->m_pHeaderIndex->m_headerLen[ headerIndex ]) )
  1481. {
  1482. *( pReq->m_pHttpHeader + off
  1483. + pReq->m_pHeaderIndex->m_headerLen[ headerIndex ]) = 0;
  1484. }
  1485. return pReq->m_pHttpHeader + off;
  1486. }
  1487. static int readBodyToReqBuf( LSAPI_Request * pReq )
  1488. {
  1489. off_t bodyLeft;
  1490. ssize_t len = pReq->m_bufRead - pReq->m_bufProcessed;
  1491. if ( len > 0 )
  1492. return len;
  1493. pReq->m_bufRead = pReq->m_bufProcessed = pReq->m_pHeader->m_pktHeader.m_packetLen.m_iLen;
  1494. bodyLeft = pReq->m_reqBodyLen - pReq->m_reqBodyRead;
  1495. len = pReq->m_reqBufSize - pReq->m_bufRead;
  1496. if ( len < 0 )
  1497. return -1;
  1498. if ( len > bodyLeft )
  1499. len = bodyLeft;
  1500. len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf + pReq->m_bufRead, len );
  1501. if ( len > 0 )
  1502. pReq->m_bufRead += len;
  1503. return len;
  1504. }
  1505. int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq )
  1506. {
  1507. if (!pReq || (pReq->m_fd ==-1) )
  1508. return EOF;
  1509. if ( pReq->m_bufProcessed >= pReq->m_bufRead )
  1510. {
  1511. if ( readBodyToReqBuf( pReq ) <= 0 )
  1512. return EOF;
  1513. }
  1514. ++pReq->m_reqBodyRead;
  1515. return (unsigned char)*(pReq->m_pReqBuf + pReq->m_bufProcessed++);
  1516. }
  1517. int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen, int *getLF )
  1518. {
  1519. ssize_t len;
  1520. ssize_t left;
  1521. char * pBufEnd = pBuf + bufLen - 1;
  1522. char * pBufCur = pBuf;
  1523. char * pCur;
  1524. char * p;
  1525. if (!pReq || pReq->m_fd == -1 || !pBuf || !getLF)
  1526. return -1;
  1527. *getLF = 0;
  1528. while( (left = pBufEnd - pBufCur ) > 0 )
  1529. {
  1530. len = pReq->m_bufRead - pReq->m_bufProcessed;
  1531. if ( len <= 0 )
  1532. {
  1533. if ( (len = readBodyToReqBuf( pReq )) <= 0 )
  1534. {
  1535. *getLF = 1;
  1536. break;
  1537. }
  1538. }
  1539. if ( len > left )
  1540. len = left;
  1541. pCur = pReq->m_pReqBuf + pReq->m_bufProcessed;
  1542. p = memchr( pCur, '\n', len );
  1543. if ( p )
  1544. len = p - pCur + 1;
  1545. memmove( pBufCur, pCur, len );
  1546. pBufCur += len;
  1547. pReq->m_bufProcessed += len;
  1548. pReq->m_reqBodyRead += len;
  1549. if ( p )
  1550. {
  1551. *getLF = 1;
  1552. break;
  1553. }
  1554. }
  1555. *pBufCur = 0;
  1556. return pBufCur - pBuf;
  1557. }
  1558. ssize_t LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen )
  1559. {
  1560. ssize_t len;
  1561. off_t total;
  1562. /* char *pOldBuf = pBuf; */
  1563. if (!pReq || pReq->m_fd == -1 || !pBuf || (ssize_t)bufLen < 0)
  1564. return -1;
  1565. total = pReq->m_reqBodyLen - pReq->m_reqBodyRead;
  1566. if ( total <= 0 )
  1567. return 0;
  1568. if ( total < (ssize_t)bufLen )
  1569. bufLen = total;
  1570. total = 0;
  1571. len = pReq->m_bufRead - pReq->m_bufProcessed;
  1572. if ( len > 0 )
  1573. {
  1574. if ( len > (ssize_t)bufLen )
  1575. len = bufLen;
  1576. memmove( pBuf, pReq->m_pReqBuf + pReq->m_bufProcessed, len );
  1577. pReq->m_bufProcessed += len;
  1578. total += len;
  1579. pBuf += len;
  1580. bufLen -= len;
  1581. }
  1582. while( bufLen > 0 )
  1583. {
  1584. len = lsapi_read( pReq->m_fd, pBuf, bufLen );
  1585. if ( len > 0 )
  1586. {
  1587. total += len;
  1588. pBuf += len;
  1589. bufLen -= len;
  1590. }
  1591. else if ( len <= 0 )
  1592. {
  1593. if ( !total)
  1594. return -1;
  1595. break;
  1596. }
  1597. }
  1598. pReq->m_reqBodyRead += total;
  1599. return total;
  1600. }
  1601. ssize_t LSAPI_Write_r( LSAPI_Request * pReq, const char * pBuf, size_t len )
  1602. {
  1603. struct lsapi_packet_header * pHeader;
  1604. const char * pEnd;
  1605. const char * p;
  1606. ssize_t bufLen;
  1607. ssize_t toWrite;
  1608. ssize_t packetLen;
  1609. int skip = 0;
  1610. if (!pReq || !pBuf)
  1611. return -1;
  1612. if (pReq->m_reqState & LSAPI_ST_BACKGROUND)
  1613. return len;
  1614. if (pReq->m_fd == -1)
  1615. return -1;
  1616. if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER )
  1617. {
  1618. LSAPI_

Large files files are truncated, but you can click here to view the full file