ftplistparser.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /**
  23. * Now implemented:
  24. *
  25. * 1) UNIX version 1
  26. * drwxr-xr-x 1 user01 ftp 512 Jan 29 23:32 prog
  27. * 2) UNIX version 2
  28. * drwxr-xr-x 1 user01 ftp 512 Jan 29 1997 prog
  29. * 3) UNIX version 3
  30. * drwxr-xr-x 1 1 1 512 Jan 29 23:32 prog
  31. * 4) UNIX symlink
  32. * lrwxr-xr-x 1 user01 ftp 512 Jan 29 23:32 prog -> prog2000
  33. * 5) DOS style
  34. * 01-29-97 11:32PM <DIR> prog
  35. */
  36. #include "curl_setup.h"
  37. #ifndef CURL_DISABLE_FTP
  38. #include <curl/curl.h>
  39. #include "urldata.h"
  40. #include "fileinfo.h"
  41. #include "llist.h"
  42. #include "strtoofft.h"
  43. #include "rawstr.h"
  44. #include "ftp.h"
  45. #include "ftplistparser.h"
  46. #include "curl_fnmatch.h"
  47. #define _MPRINTF_REPLACE /* use our functions only */
  48. #include <curl/mprintf.h>
  49. #include "curl_memory.h"
  50. /* The last #include file should be: */
  51. #include "memdebug.h"
  52. /* allocs buffer which will contain one line of LIST command response */
  53. #define FTP_BUFFER_ALLOCSIZE 160
  54. typedef enum {
  55. PL_UNIX_TOTALSIZE = 0,
  56. PL_UNIX_FILETYPE,
  57. PL_UNIX_PERMISSION,
  58. PL_UNIX_HLINKS,
  59. PL_UNIX_USER,
  60. PL_UNIX_GROUP,
  61. PL_UNIX_SIZE,
  62. PL_UNIX_TIME,
  63. PL_UNIX_FILENAME,
  64. PL_UNIX_SYMLINK
  65. } pl_unix_mainstate;
  66. typedef union {
  67. enum {
  68. PL_UNIX_TOTALSIZE_INIT = 0,
  69. PL_UNIX_TOTALSIZE_READING
  70. } total_dirsize;
  71. enum {
  72. PL_UNIX_HLINKS_PRESPACE = 0,
  73. PL_UNIX_HLINKS_NUMBER
  74. } hlinks;
  75. enum {
  76. PL_UNIX_USER_PRESPACE = 0,
  77. PL_UNIX_USER_PARSING
  78. } user;
  79. enum {
  80. PL_UNIX_GROUP_PRESPACE = 0,
  81. PL_UNIX_GROUP_NAME
  82. } group;
  83. enum {
  84. PL_UNIX_SIZE_PRESPACE = 0,
  85. PL_UNIX_SIZE_NUMBER
  86. } size;
  87. enum {
  88. PL_UNIX_TIME_PREPART1 = 0,
  89. PL_UNIX_TIME_PART1,
  90. PL_UNIX_TIME_PREPART2,
  91. PL_UNIX_TIME_PART2,
  92. PL_UNIX_TIME_PREPART3,
  93. PL_UNIX_TIME_PART3
  94. } time;
  95. enum {
  96. PL_UNIX_FILENAME_PRESPACE = 0,
  97. PL_UNIX_FILENAME_NAME,
  98. PL_UNIX_FILENAME_WINDOWSEOL
  99. } filename;
  100. enum {
  101. PL_UNIX_SYMLINK_PRESPACE = 0,
  102. PL_UNIX_SYMLINK_NAME,
  103. PL_UNIX_SYMLINK_PRETARGET1,
  104. PL_UNIX_SYMLINK_PRETARGET2,
  105. PL_UNIX_SYMLINK_PRETARGET3,
  106. PL_UNIX_SYMLINK_PRETARGET4,
  107. PL_UNIX_SYMLINK_TARGET,
  108. PL_UNIX_SYMLINK_WINDOWSEOL
  109. } symlink;
  110. } pl_unix_substate;
  111. typedef enum {
  112. PL_WINNT_DATE = 0,
  113. PL_WINNT_TIME,
  114. PL_WINNT_DIRORSIZE,
  115. PL_WINNT_FILENAME
  116. } pl_winNT_mainstate;
  117. typedef union {
  118. enum {
  119. PL_WINNT_TIME_PRESPACE = 0,
  120. PL_WINNT_TIME_TIME
  121. } time;
  122. enum {
  123. PL_WINNT_DIRORSIZE_PRESPACE = 0,
  124. PL_WINNT_DIRORSIZE_CONTENT
  125. } dirorsize;
  126. enum {
  127. PL_WINNT_FILENAME_PRESPACE = 0,
  128. PL_WINNT_FILENAME_CONTENT,
  129. PL_WINNT_FILENAME_WINEOL
  130. } filename;
  131. } pl_winNT_substate;
  132. /* This struct is used in wildcard downloading - for parsing LIST response */
  133. struct ftp_parselist_data {
  134. enum {
  135. OS_TYPE_UNKNOWN = 0,
  136. OS_TYPE_UNIX,
  137. OS_TYPE_WIN_NT
  138. } os_type;
  139. union {
  140. struct {
  141. pl_unix_mainstate main;
  142. pl_unix_substate sub;
  143. } UNIX;
  144. struct {
  145. pl_winNT_mainstate main;
  146. pl_winNT_substate sub;
  147. } NT;
  148. } state;
  149. CURLcode error;
  150. struct curl_fileinfo *file_data;
  151. unsigned int item_length;
  152. size_t item_offset;
  153. struct {
  154. size_t filename;
  155. size_t user;
  156. size_t group;
  157. size_t time;
  158. size_t perm;
  159. size_t symlink_target;
  160. } offsets;
  161. };
  162. struct ftp_parselist_data *Curl_ftp_parselist_data_alloc(void)
  163. {
  164. return calloc(1, sizeof(struct ftp_parselist_data));
  165. }
  166. void Curl_ftp_parselist_data_free(struct ftp_parselist_data **pl_data)
  167. {
  168. if(*pl_data)
  169. free(*pl_data);
  170. *pl_data = NULL;
  171. }
  172. CURLcode Curl_ftp_parselist_geterror(struct ftp_parselist_data *pl_data)
  173. {
  174. return pl_data->error;
  175. }
  176. #define FTP_LP_MALFORMATED_PERM 0x01000000
  177. static int ftp_pl_get_permission(const char *str)
  178. {
  179. int permissions = 0;
  180. /* USER */
  181. if(str[0] == 'r')
  182. permissions |= 1 << 8;
  183. else if(str[0] != '-')
  184. permissions |= FTP_LP_MALFORMATED_PERM;
  185. if(str[1] == 'w')
  186. permissions |= 1 << 7;
  187. else if(str[1] != '-')
  188. permissions |= FTP_LP_MALFORMATED_PERM;
  189. if(str[2] == 'x')
  190. permissions |= 1 << 6;
  191. else if(str[2] == 's') {
  192. permissions |= 1 << 6;
  193. permissions |= 1 << 11;
  194. }
  195. else if(str[2] == 'S')
  196. permissions |= 1 << 11;
  197. else if(str[2] != '-')
  198. permissions |= FTP_LP_MALFORMATED_PERM;
  199. /* GROUP */
  200. if(str[3] == 'r')
  201. permissions |= 1 << 5;
  202. else if(str[3] != '-')
  203. permissions |= FTP_LP_MALFORMATED_PERM;
  204. if(str[4] == 'w')
  205. permissions |= 1 << 4;
  206. else if(str[4] != '-')
  207. permissions |= FTP_LP_MALFORMATED_PERM;
  208. if(str[5] == 'x')
  209. permissions |= 1 << 3;
  210. else if(str[5] == 's') {
  211. permissions |= 1 << 3;
  212. permissions |= 1 << 10;
  213. }
  214. else if(str[5] == 'S')
  215. permissions |= 1 << 10;
  216. else if(str[5] != '-')
  217. permissions |= FTP_LP_MALFORMATED_PERM;
  218. /* others */
  219. if(str[6] == 'r')
  220. permissions |= 1 << 2;
  221. else if(str[6] != '-')
  222. permissions |= FTP_LP_MALFORMATED_PERM;
  223. if(str[7] == 'w')
  224. permissions |= 1 << 1;
  225. else if(str[7] != '-')
  226. permissions |= FTP_LP_MALFORMATED_PERM;
  227. if(str[8] == 'x')
  228. permissions |= 1;
  229. else if(str[8] == 't') {
  230. permissions |= 1;
  231. permissions |= 1 << 9;
  232. }
  233. else if(str[8] == 'T')
  234. permissions |= 1 << 9;
  235. else if(str[8] != '-')
  236. permissions |= FTP_LP_MALFORMATED_PERM;
  237. return permissions;
  238. }
  239. static void PL_ERROR(struct connectdata *conn, CURLcode err)
  240. {
  241. struct ftp_wc_tmpdata *tmpdata = conn->data->wildcard.tmp;
  242. struct ftp_parselist_data *parser = tmpdata->parser;
  243. if(parser->file_data)
  244. Curl_fileinfo_dtor(NULL, parser->file_data);
  245. parser->file_data = NULL;
  246. parser->error = err;
  247. }
  248. static bool ftp_pl_gettime(struct ftp_parselist_data *parser, char *string)
  249. {
  250. (void)parser;
  251. (void)string;
  252. /* TODO
  253. * There could be possible parse timestamp from server. Leaving unimplemented
  254. * for now.
  255. * If you want implement this, please add CURLFINFOFLAG_KNOWN_TIME flag to
  256. * parser->file_data->flags
  257. *
  258. * Ftp servers are giving usually these formats:
  259. * Apr 11 1998 (unknown time.. set it to 00:00:00?)
  260. * Apr 11 12:21 (unknown year -> set it to NOW() time?)
  261. * 08-05-09 02:49PM (ms-dos format)
  262. * 20100421092538 -> for MLST/MLSD response
  263. */
  264. return FALSE;
  265. }
  266. static CURLcode ftp_pl_insert_finfo(struct connectdata *conn,
  267. struct curl_fileinfo *finfo)
  268. {
  269. curl_fnmatch_callback compare;
  270. struct WildcardData *wc = &conn->data->wildcard;
  271. struct ftp_wc_tmpdata *tmpdata = wc->tmp;
  272. struct curl_llist *llist = wc->filelist;
  273. struct ftp_parselist_data *parser = tmpdata->parser;
  274. bool add = TRUE;
  275. /* move finfo pointers to b_data */
  276. char *str = finfo->b_data;
  277. finfo->filename = str + parser->offsets.filename;
  278. finfo->strings.group = parser->offsets.group ?
  279. str + parser->offsets.group : NULL;
  280. finfo->strings.perm = parser->offsets.perm ?
  281. str + parser->offsets.perm : NULL;
  282. finfo->strings.target = parser->offsets.symlink_target ?
  283. str + parser->offsets.symlink_target : NULL;
  284. finfo->strings.time = str + parser->offsets.time;
  285. finfo->strings.user = parser->offsets.user ?
  286. str + parser->offsets.user : NULL;
  287. /* get correct fnmatch callback */
  288. compare = conn->data->set.fnmatch;
  289. if(!compare)
  290. compare = Curl_fnmatch;
  291. /* filter pattern-corresponding filenames */
  292. if(compare(conn->data->set.fnmatch_data, wc->pattern,
  293. finfo->filename) == 0) {
  294. /* discard symlink which is containing multiple " -> " */
  295. if((finfo->filetype == CURLFILETYPE_SYMLINK) && finfo->strings.target &&
  296. (strstr(finfo->strings.target, " -> "))) {
  297. add = FALSE;
  298. }
  299. }
  300. else {
  301. add = FALSE;
  302. }
  303. if(add) {
  304. if(!Curl_llist_insert_next(llist, llist->tail, finfo)) {
  305. Curl_fileinfo_dtor(NULL, finfo);
  306. tmpdata->parser->file_data = NULL;
  307. return CURLE_OUT_OF_MEMORY;
  308. }
  309. }
  310. else {
  311. Curl_fileinfo_dtor(NULL, finfo);
  312. }
  313. tmpdata->parser->file_data = NULL;
  314. return CURLE_OK;
  315. }
  316. size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb,
  317. void *connptr)
  318. {
  319. size_t bufflen = size*nmemb;
  320. struct connectdata *conn = (struct connectdata *)connptr;
  321. struct ftp_wc_tmpdata *tmpdata = conn->data->wildcard.tmp;
  322. struct ftp_parselist_data *parser = tmpdata->parser;
  323. struct curl_fileinfo *finfo;
  324. unsigned long i = 0;
  325. CURLcode rc;
  326. if(parser->error) { /* error in previous call */
  327. /* scenario:
  328. * 1. call => OK..
  329. * 2. call => OUT_OF_MEMORY (or other error)
  330. * 3. (last) call => is skipped RIGHT HERE and the error is hadled later
  331. * in wc_statemach()
  332. */
  333. return bufflen;
  334. }
  335. if(parser->os_type == OS_TYPE_UNKNOWN && bufflen > 0) {
  336. /* considering info about FILE response format */
  337. parser->os_type = (buffer[0] >= '0' && buffer[0] <= '9') ?
  338. OS_TYPE_WIN_NT : OS_TYPE_UNIX;
  339. }
  340. while(i < bufflen) { /* FSM */
  341. char c = buffer[i];
  342. if(!parser->file_data) { /* tmp file data is not allocated yet */
  343. parser->file_data = Curl_fileinfo_alloc();
  344. if(!parser->file_data) {
  345. parser->error = CURLE_OUT_OF_MEMORY;
  346. return bufflen;
  347. }
  348. parser->file_data->b_data = malloc(FTP_BUFFER_ALLOCSIZE);
  349. if(!parser->file_data->b_data) {
  350. PL_ERROR(conn, CURLE_OUT_OF_MEMORY);
  351. return bufflen;
  352. }
  353. parser->file_data->b_size = FTP_BUFFER_ALLOCSIZE;
  354. parser->item_offset = 0;
  355. parser->item_length = 0;
  356. }
  357. finfo = parser->file_data;
  358. finfo->b_data[finfo->b_used++] = c;
  359. if(finfo->b_used >= finfo->b_size - 1) {
  360. /* if it is important, extend buffer space for file data */
  361. char *tmp = realloc(finfo->b_data,
  362. finfo->b_size + FTP_BUFFER_ALLOCSIZE);
  363. if(tmp) {
  364. finfo->b_size += FTP_BUFFER_ALLOCSIZE;
  365. finfo->b_data = tmp;
  366. }
  367. else {
  368. Curl_fileinfo_dtor(NULL, parser->file_data);
  369. parser->file_data = NULL;
  370. parser->error = CURLE_OUT_OF_MEMORY;
  371. PL_ERROR(conn, CURLE_OUT_OF_MEMORY);
  372. return bufflen;
  373. }
  374. }
  375. switch (parser->os_type) {
  376. case OS_TYPE_UNIX:
  377. switch (parser->state.UNIX.main) {
  378. case PL_UNIX_TOTALSIZE:
  379. switch(parser->state.UNIX.sub.total_dirsize) {
  380. case PL_UNIX_TOTALSIZE_INIT:
  381. if(c == 't') {
  382. parser->state.UNIX.sub.total_dirsize = PL_UNIX_TOTALSIZE_READING;
  383. parser->item_length++;
  384. }
  385. else {
  386. parser->state.UNIX.main = PL_UNIX_FILETYPE;
  387. /* start FSM again not considering size of directory */
  388. finfo->b_used = 0;
  389. i--;
  390. }
  391. break;
  392. case PL_UNIX_TOTALSIZE_READING:
  393. parser->item_length++;
  394. if(c == '\r') {
  395. parser->item_length--;
  396. finfo->b_used--;
  397. }
  398. else if(c == '\n') {
  399. finfo->b_data[parser->item_length - 1] = 0;
  400. if(strncmp("total ", finfo->b_data, 6) == 0) {
  401. char *endptr = finfo->b_data+6;
  402. /* here we can deal with directory size */
  403. while(ISSPACE(*endptr))
  404. endptr++;
  405. if(*endptr != 0) {
  406. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  407. return bufflen;
  408. }
  409. else {
  410. parser->state.UNIX.main = PL_UNIX_FILETYPE;
  411. finfo->b_used = 0;
  412. }
  413. }
  414. else {
  415. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  416. return bufflen;
  417. }
  418. }
  419. break;
  420. }
  421. break;
  422. case PL_UNIX_FILETYPE:
  423. switch (c) {
  424. case '-':
  425. finfo->filetype = CURLFILETYPE_FILE;
  426. break;
  427. case 'd':
  428. finfo->filetype = CURLFILETYPE_DIRECTORY;
  429. break;
  430. case 'l':
  431. finfo->filetype = CURLFILETYPE_SYMLINK;
  432. break;
  433. case 'p':
  434. finfo->filetype = CURLFILETYPE_NAMEDPIPE;
  435. break;
  436. case 's':
  437. finfo->filetype = CURLFILETYPE_SOCKET;
  438. break;
  439. case 'c':
  440. finfo->filetype = CURLFILETYPE_DEVICE_CHAR;
  441. break;
  442. case 'b':
  443. finfo->filetype = CURLFILETYPE_DEVICE_BLOCK;
  444. break;
  445. case 'D':
  446. finfo->filetype = CURLFILETYPE_DOOR;
  447. break;
  448. default:
  449. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  450. return bufflen;
  451. }
  452. parser->state.UNIX.main = PL_UNIX_PERMISSION;
  453. parser->item_length = 0;
  454. parser->item_offset = 1;
  455. break;
  456. case PL_UNIX_PERMISSION:
  457. parser->item_length++;
  458. if(parser->item_length <= 9) {
  459. if(!strchr("rwx-tTsS", c)) {
  460. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  461. return bufflen;
  462. }
  463. }
  464. else if(parser->item_length == 10) {
  465. unsigned int perm;
  466. if(c != ' ') {
  467. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  468. return bufflen;
  469. }
  470. finfo->b_data[10] = 0; /* terminate permissions */
  471. perm = ftp_pl_get_permission(finfo->b_data + parser->item_offset);
  472. if(perm & FTP_LP_MALFORMATED_PERM) {
  473. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  474. return bufflen;
  475. }
  476. parser->file_data->flags |= CURLFINFOFLAG_KNOWN_PERM;
  477. parser->file_data->perm = perm;
  478. parser->offsets.perm = parser->item_offset;
  479. parser->item_length = 0;
  480. parser->state.UNIX.main = PL_UNIX_HLINKS;
  481. parser->state.UNIX.sub.hlinks = PL_UNIX_HLINKS_PRESPACE;
  482. }
  483. break;
  484. case PL_UNIX_HLINKS:
  485. switch(parser->state.UNIX.sub.hlinks) {
  486. case PL_UNIX_HLINKS_PRESPACE:
  487. if(c != ' ') {
  488. if(c >= '0' && c <= '9') {
  489. parser->item_offset = finfo->b_used - 1;
  490. parser->item_length = 1;
  491. parser->state.UNIX.sub.hlinks = PL_UNIX_HLINKS_NUMBER;
  492. }
  493. else {
  494. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  495. return bufflen;
  496. }
  497. }
  498. break;
  499. case PL_UNIX_HLINKS_NUMBER:
  500. parser->item_length ++;
  501. if(c == ' ') {
  502. char *p;
  503. long int hlinks;
  504. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  505. hlinks = strtol(finfo->b_data + parser->item_offset, &p, 10);
  506. if(p[0] == '\0' && hlinks != LONG_MAX && hlinks != LONG_MIN) {
  507. parser->file_data->flags |= CURLFINFOFLAG_KNOWN_HLINKCOUNT;
  508. parser->file_data->hardlinks = hlinks;
  509. }
  510. parser->item_length = 0;
  511. parser->item_offset = 0;
  512. parser->state.UNIX.main = PL_UNIX_USER;
  513. parser->state.UNIX.sub.user = PL_UNIX_USER_PRESPACE;
  514. }
  515. else if(c < '0' || c > '9') {
  516. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  517. return bufflen;
  518. }
  519. break;
  520. }
  521. break;
  522. case PL_UNIX_USER:
  523. switch(parser->state.UNIX.sub.user) {
  524. case PL_UNIX_USER_PRESPACE:
  525. if(c != ' ') {
  526. parser->item_offset = finfo->b_used - 1;
  527. parser->item_length = 1;
  528. parser->state.UNIX.sub.user = PL_UNIX_USER_PARSING;
  529. }
  530. break;
  531. case PL_UNIX_USER_PARSING:
  532. parser->item_length++;
  533. if(c == ' ') {
  534. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  535. parser->offsets.user = parser->item_offset;
  536. parser->state.UNIX.main = PL_UNIX_GROUP;
  537. parser->state.UNIX.sub.group = PL_UNIX_GROUP_PRESPACE;
  538. parser->item_offset = 0;
  539. parser->item_length = 0;
  540. }
  541. break;
  542. }
  543. break;
  544. case PL_UNIX_GROUP:
  545. switch(parser->state.UNIX.sub.group) {
  546. case PL_UNIX_GROUP_PRESPACE:
  547. if(c != ' ') {
  548. parser->item_offset = finfo->b_used - 1;
  549. parser->item_length = 1;
  550. parser->state.UNIX.sub.group = PL_UNIX_GROUP_NAME;
  551. }
  552. break;
  553. case PL_UNIX_GROUP_NAME:
  554. parser->item_length++;
  555. if(c == ' ') {
  556. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  557. parser->offsets.group = parser->item_offset;
  558. parser->state.UNIX.main = PL_UNIX_SIZE;
  559. parser->state.UNIX.sub.size = PL_UNIX_SIZE_PRESPACE;
  560. parser->item_offset = 0;
  561. parser->item_length = 0;
  562. }
  563. break;
  564. }
  565. break;
  566. case PL_UNIX_SIZE:
  567. switch(parser->state.UNIX.sub.size) {
  568. case PL_UNIX_SIZE_PRESPACE:
  569. if(c != ' ') {
  570. if(c >= '0' && c <= '9') {
  571. parser->item_offset = finfo->b_used - 1;
  572. parser->item_length = 1;
  573. parser->state.UNIX.sub.size = PL_UNIX_SIZE_NUMBER;
  574. }
  575. else {
  576. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  577. return bufflen;
  578. }
  579. }
  580. break;
  581. case PL_UNIX_SIZE_NUMBER:
  582. parser->item_length++;
  583. if(c == ' ') {
  584. char *p;
  585. curl_off_t fsize;
  586. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  587. fsize = curlx_strtoofft(finfo->b_data+parser->item_offset, &p, 10);
  588. if(p[0] == '\0' && fsize != CURL_OFF_T_MAX &&
  589. fsize != CURL_OFF_T_MIN) {
  590. parser->file_data->flags |= CURLFINFOFLAG_KNOWN_SIZE;
  591. parser->file_data->size = fsize;
  592. }
  593. parser->item_length = 0;
  594. parser->item_offset = 0;
  595. parser->state.UNIX.main = PL_UNIX_TIME;
  596. parser->state.UNIX.sub.time = PL_UNIX_TIME_PREPART1;
  597. }
  598. else if(!ISDIGIT(c)) {
  599. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  600. return bufflen;
  601. }
  602. break;
  603. }
  604. break;
  605. case PL_UNIX_TIME:
  606. switch(parser->state.UNIX.sub.time) {
  607. case PL_UNIX_TIME_PREPART1:
  608. if(c != ' ') {
  609. if(ISALNUM(c)) {
  610. parser->item_offset = finfo->b_used -1;
  611. parser->item_length = 1;
  612. parser->state.UNIX.sub.time = PL_UNIX_TIME_PART1;
  613. }
  614. else {
  615. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  616. return bufflen;
  617. }
  618. }
  619. break;
  620. case PL_UNIX_TIME_PART1:
  621. parser->item_length++;
  622. if(c == ' ') {
  623. parser->state.UNIX.sub.time = PL_UNIX_TIME_PREPART2;
  624. }
  625. else if(!ISALNUM(c) && c != '.') {
  626. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  627. return bufflen;
  628. }
  629. break;
  630. case PL_UNIX_TIME_PREPART2:
  631. parser->item_length++;
  632. if(c != ' ') {
  633. if(ISALNUM(c)) {
  634. parser->state.UNIX.sub.time = PL_UNIX_TIME_PART2;
  635. }
  636. else {
  637. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  638. return bufflen;
  639. }
  640. }
  641. break;
  642. case PL_UNIX_TIME_PART2:
  643. parser->item_length++;
  644. if(c == ' ') {
  645. parser->state.UNIX.sub.time = PL_UNIX_TIME_PREPART3;
  646. }
  647. else if(!ISALNUM(c) && c != '.') {
  648. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  649. return bufflen;
  650. }
  651. break;
  652. case PL_UNIX_TIME_PREPART3:
  653. parser->item_length++;
  654. if(c != ' ') {
  655. if(ISALNUM(c)) {
  656. parser->state.UNIX.sub.time = PL_UNIX_TIME_PART3;
  657. }
  658. else {
  659. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  660. return bufflen;
  661. }
  662. }
  663. break;
  664. case PL_UNIX_TIME_PART3:
  665. parser->item_length++;
  666. if(c == ' ') {
  667. finfo->b_data[parser->item_offset + parser->item_length -1] = 0;
  668. parser->offsets.time = parser->item_offset;
  669. if(ftp_pl_gettime(parser, finfo->b_data + parser->item_offset)) {
  670. parser->file_data->flags |= CURLFINFOFLAG_KNOWN_TIME;
  671. }
  672. if(finfo->filetype == CURLFILETYPE_SYMLINK) {
  673. parser->state.UNIX.main = PL_UNIX_SYMLINK;
  674. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_PRESPACE;
  675. }
  676. else {
  677. parser->state.UNIX.main = PL_UNIX_FILENAME;
  678. parser->state.UNIX.sub.filename = PL_UNIX_FILENAME_PRESPACE;
  679. }
  680. }
  681. else if(!ISALNUM(c) && c != '.' && c != ':') {
  682. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  683. return bufflen;
  684. }
  685. break;
  686. }
  687. break;
  688. case PL_UNIX_FILENAME:
  689. switch(parser->state.UNIX.sub.filename) {
  690. case PL_UNIX_FILENAME_PRESPACE:
  691. if(c != ' ') {
  692. parser->item_offset = finfo->b_used - 1;
  693. parser->item_length = 1;
  694. parser->state.UNIX.sub.filename = PL_UNIX_FILENAME_NAME;
  695. }
  696. break;
  697. case PL_UNIX_FILENAME_NAME:
  698. parser->item_length++;
  699. if(c == '\r') {
  700. parser->item_length--;
  701. parser->state.UNIX.sub.filename = PL_UNIX_FILENAME_WINDOWSEOL;
  702. }
  703. else if(c == '\n') {
  704. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  705. parser->offsets.filename = parser->item_offset;
  706. parser->state.UNIX.main = PL_UNIX_FILETYPE;
  707. rc = ftp_pl_insert_finfo(conn, finfo);
  708. if(rc) {
  709. PL_ERROR(conn, rc);
  710. return bufflen;
  711. }
  712. }
  713. break;
  714. case PL_UNIX_FILENAME_WINDOWSEOL:
  715. if(c == '\n') {
  716. finfo->b_data[parser->item_offset + parser->item_length] = 0;
  717. parser->offsets.filename = parser->item_offset;
  718. parser->state.UNIX.main = PL_UNIX_FILETYPE;
  719. rc = ftp_pl_insert_finfo(conn, finfo);
  720. if(rc) {
  721. PL_ERROR(conn, rc);
  722. return bufflen;
  723. }
  724. }
  725. else {
  726. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  727. return bufflen;
  728. }
  729. break;
  730. }
  731. break;
  732. case PL_UNIX_SYMLINK:
  733. switch(parser->state.UNIX.sub.symlink) {
  734. case PL_UNIX_SYMLINK_PRESPACE:
  735. if(c != ' ') {
  736. parser->item_offset = finfo->b_used - 1;
  737. parser->item_length = 1;
  738. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_NAME;
  739. }
  740. break;
  741. case PL_UNIX_SYMLINK_NAME:
  742. parser->item_length++;
  743. if(c == ' ') {
  744. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_PRETARGET1;
  745. }
  746. else if(c == '\r' || c == '\n') {
  747. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  748. return bufflen;
  749. }
  750. break;
  751. case PL_UNIX_SYMLINK_PRETARGET1:
  752. parser->item_length++;
  753. if(c == '-') {
  754. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_PRETARGET2;
  755. }
  756. else if(c == '\r' || c == '\n') {
  757. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  758. return bufflen;
  759. }
  760. else {
  761. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_NAME;
  762. }
  763. break;
  764. case PL_UNIX_SYMLINK_PRETARGET2:
  765. parser->item_length++;
  766. if(c == '>') {
  767. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_PRETARGET3;
  768. }
  769. else if(c == '\r' || c == '\n') {
  770. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  771. return bufflen;
  772. }
  773. else {
  774. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_NAME;
  775. }
  776. break;
  777. case PL_UNIX_SYMLINK_PRETARGET3:
  778. parser->item_length++;
  779. if(c == ' ') {
  780. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_PRETARGET4;
  781. /* now place where is symlink following */
  782. finfo->b_data[parser->item_offset + parser->item_length - 4] = 0;
  783. parser->offsets.filename = parser->item_offset;
  784. parser->item_length = 0;
  785. parser->item_offset = 0;
  786. }
  787. else if(c == '\r' || c == '\n') {
  788. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  789. return bufflen;
  790. }
  791. else {
  792. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_NAME;
  793. }
  794. break;
  795. case PL_UNIX_SYMLINK_PRETARGET4:
  796. if(c != '\r' && c != '\n') {
  797. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_TARGET;
  798. parser->item_offset = finfo->b_used - 1;
  799. parser->item_length = 1;
  800. }
  801. else {
  802. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  803. return bufflen;
  804. }
  805. break;
  806. case PL_UNIX_SYMLINK_TARGET:
  807. parser->item_length ++;
  808. if(c == '\r') {
  809. parser->item_length --;
  810. parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_WINDOWSEOL;
  811. }
  812. else if(c == '\n') {
  813. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  814. parser->offsets.symlink_target = parser->item_offset;
  815. rc = ftp_pl_insert_finfo(conn, finfo);
  816. if(rc) {
  817. PL_ERROR(conn, rc);
  818. return bufflen;
  819. }
  820. parser->state.UNIX.main = PL_UNIX_FILETYPE;
  821. }
  822. break;
  823. case PL_UNIX_SYMLINK_WINDOWSEOL:
  824. if(c == '\n') {
  825. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  826. parser->offsets.symlink_target = parser->item_offset;
  827. rc = ftp_pl_insert_finfo(conn, finfo);
  828. if(rc) {
  829. PL_ERROR(conn, rc);
  830. return bufflen;
  831. }
  832. parser->state.UNIX.main = PL_UNIX_FILETYPE;
  833. }
  834. else {
  835. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  836. return bufflen;
  837. }
  838. break;
  839. }
  840. break;
  841. }
  842. break;
  843. case OS_TYPE_WIN_NT:
  844. switch(parser->state.NT.main) {
  845. case PL_WINNT_DATE:
  846. parser->item_length++;
  847. if(parser->item_length < 9) {
  848. if(!strchr("0123456789-", c)) { /* only simple control */
  849. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  850. return bufflen;
  851. }
  852. }
  853. else if(parser->item_length == 9) {
  854. if(c == ' ') {
  855. parser->state.NT.main = PL_WINNT_TIME;
  856. parser->state.NT.sub.time = PL_WINNT_TIME_PRESPACE;
  857. }
  858. else {
  859. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  860. return bufflen;
  861. }
  862. }
  863. else {
  864. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  865. return bufflen;
  866. }
  867. break;
  868. case PL_WINNT_TIME:
  869. parser->item_length++;
  870. switch(parser->state.NT.sub.time) {
  871. case PL_WINNT_TIME_PRESPACE:
  872. if(!ISSPACE(c)) {
  873. parser->state.NT.sub.time = PL_WINNT_TIME_TIME;
  874. }
  875. break;
  876. case PL_WINNT_TIME_TIME:
  877. if(c == ' ') {
  878. parser->offsets.time = parser->item_offset;
  879. finfo->b_data[parser->item_offset + parser->item_length -1] = 0;
  880. parser->state.NT.main = PL_WINNT_DIRORSIZE;
  881. parser->state.NT.sub.dirorsize = PL_WINNT_DIRORSIZE_PRESPACE;
  882. parser->item_length = 0;
  883. }
  884. else if(!strchr("APM0123456789:", c)) {
  885. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  886. return bufflen;
  887. }
  888. break;
  889. }
  890. break;
  891. case PL_WINNT_DIRORSIZE:
  892. switch(parser->state.NT.sub.dirorsize) {
  893. case PL_WINNT_DIRORSIZE_PRESPACE:
  894. if(c == ' ') {
  895. }
  896. else {
  897. parser->item_offset = finfo->b_used - 1;
  898. parser->item_length = 1;
  899. parser->state.NT.sub.dirorsize = PL_WINNT_DIRORSIZE_CONTENT;
  900. }
  901. break;
  902. case PL_WINNT_DIRORSIZE_CONTENT:
  903. parser->item_length ++;
  904. if(c == ' ') {
  905. finfo->b_data[parser->item_offset + parser->item_length - 1] = 0;
  906. if(strcmp("<DIR>", finfo->b_data + parser->item_offset) == 0) {
  907. finfo->filetype = CURLFILETYPE_DIRECTORY;
  908. finfo->size = 0;
  909. }
  910. else {
  911. char *endptr;
  912. finfo->size = curlx_strtoofft(finfo->b_data +
  913. parser->item_offset,
  914. &endptr, 10);
  915. if(!*endptr) {
  916. if(finfo->size == CURL_OFF_T_MAX ||
  917. finfo->size == CURL_OFF_T_MIN) {
  918. if(errno == ERANGE) {
  919. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  920. return bufflen;
  921. }
  922. }
  923. }
  924. else {
  925. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  926. return bufflen;
  927. }
  928. /* correct file type */
  929. parser->file_data->filetype = CURLFILETYPE_FILE;
  930. }
  931. parser->file_data->flags |= CURLFINFOFLAG_KNOWN_SIZE;
  932. parser->item_length = 0;
  933. parser->state.NT.main = PL_WINNT_FILENAME;
  934. parser->state.NT.sub.filename = PL_WINNT_FILENAME_PRESPACE;
  935. }
  936. break;
  937. }
  938. break;
  939. case PL_WINNT_FILENAME:
  940. switch (parser->state.NT.sub.filename) {
  941. case PL_WINNT_FILENAME_PRESPACE:
  942. if(c != ' ') {
  943. parser->item_offset = finfo->b_used -1;
  944. parser->item_length = 1;
  945. parser->state.NT.sub.filename = PL_WINNT_FILENAME_CONTENT;
  946. }
  947. break;
  948. case PL_WINNT_FILENAME_CONTENT:
  949. parser->item_length++;
  950. if(c == '\r') {
  951. parser->state.NT.sub.filename = PL_WINNT_FILENAME_WINEOL;
  952. finfo->b_data[finfo->b_used - 1] = 0;
  953. }
  954. else if(c == '\n') {
  955. parser->offsets.filename = parser->item_offset;
  956. finfo->b_data[finfo->b_used - 1] = 0;
  957. parser->offsets.filename = parser->item_offset;
  958. rc = ftp_pl_insert_finfo(conn, finfo);
  959. if(rc) {
  960. PL_ERROR(conn, rc);
  961. return bufflen;
  962. }
  963. parser->state.NT.main = PL_WINNT_DATE;
  964. parser->state.NT.sub.filename = PL_WINNT_FILENAME_PRESPACE;
  965. }
  966. break;
  967. case PL_WINNT_FILENAME_WINEOL:
  968. if(c == '\n') {
  969. parser->offsets.filename = parser->item_offset;
  970. rc = ftp_pl_insert_finfo(conn, finfo);
  971. if(rc) {
  972. PL_ERROR(conn, rc);
  973. return bufflen;
  974. }
  975. parser->state.NT.main = PL_WINNT_DATE;
  976. parser->state.NT.sub.filename = PL_WINNT_FILENAME_PRESPACE;
  977. }
  978. else {
  979. PL_ERROR(conn, CURLE_FTP_BAD_FILE_LIST);
  980. return bufflen;
  981. }
  982. break;
  983. }
  984. break;
  985. }
  986. break;
  987. default:
  988. return bufflen+1;
  989. }
  990. i++;
  991. }
  992. return bufflen;
  993. }
  994. #endif /* CURL_DISABLE_FTP */