suricata
app-layer-ftp.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22  * \author Eric Leblond <eric@regit.org>
23  * \author Jeff Lucovsky <jlucovsky@oisf.net>
24  *
25  * App Layer Parser for FTP
26  */
27 
28 #include "suricata-common.h"
29 #include "app-layer-ftp.h"
30 #include "app-layer.h"
31 #include "app-layer-parser.h"
32 #include "app-layer-expectation.h"
33 #include "app-layer-detect-proto.h"
34 
35 #include "rust.h"
36 
37 #include "util-misc.h"
38 #include "util-mpm.h"
39 #include "util-validate.h"
40 
41 typedef struct FTPThreadCtx_ {
45 
46 #define FTP_MPM mpm_default_matcher
47 
48 static MpmCtx *ftp_mpm_ctx = NULL;
49 
50 uint64_t ftp_config_memcap = 0;
51 uint32_t ftp_config_maxtx = 1024;
52 uint32_t ftp_max_line_len = 4096;
53 
54 SC_ATOMIC_DECLARE(uint64_t, ftp_memuse);
55 SC_ATOMIC_DECLARE(uint64_t, ftp_memcap);
56 
57 static FTPTransaction *FTPGetOldestTx(const FtpState *, FTPTransaction *);
58 
59 static void FTPParseMemcap(void)
60 {
61  SCFTPGetConfigValues(&ftp_config_memcap, &ftp_config_maxtx, &ftp_max_line_len);
62 
63  SC_ATOMIC_INIT(ftp_memuse);
64  SC_ATOMIC_INIT(ftp_memcap);
65 }
66 
67 static void FTPIncrMemuse(uint64_t size)
68 {
69  (void)SC_ATOMIC_ADD(ftp_memuse, size);
70 }
71 
72 static void FTPDecrMemuse(uint64_t size)
73 {
74  (void)SC_ATOMIC_SUB(ftp_memuse, size);
75 }
76 
77 uint64_t FTPMemuseGlobalCounter(void)
78 {
79  uint64_t tmpval = SC_ATOMIC_GET(ftp_memuse);
80  return tmpval;
81 }
82 
83 uint64_t FTPMemcapGlobalCounter(void)
84 {
85  uint64_t tmpval = SC_ATOMIC_GET(ftp_memcap);
86  return tmpval;
87 }
88 
89 int FTPSetMemcap(uint64_t size)
90 {
91  if ((uint64_t)SC_ATOMIC_GET(ftp_memcap) < size) {
92  SC_ATOMIC_SET(ftp_memcap, size);
93  return 1;
94  }
95 
96  return 0;
97 }
98 
99 /**
100  * \brief Check if alloc'ing "size" would mean we're over memcap
101  *
102  * \retval 1 if in bounds
103  * \retval 0 if not in bounds
104  */
105 static int FTPCheckMemcap(uint64_t size)
106 {
107  if (ftp_config_memcap == 0 || size + SC_ATOMIC_GET(ftp_memuse) <= ftp_config_memcap)
108  return 1;
109  (void) SC_ATOMIC_ADD(ftp_memcap, 1);
110  return 0;
111 }
112 
113 static void *FTPCalloc(size_t n, size_t size)
114 {
115  if (FTPCheckMemcap((uint32_t)(n * size)) == 0) {
117  return NULL;
118  }
119 
120  void *ptr = SCCalloc(n, size);
121 
122  if (unlikely(ptr == NULL)) {
124  return NULL;
125  }
126 
127  FTPIncrMemuse((uint64_t)(n * size));
128  return ptr;
129 }
130 
131 static void *FTPRealloc(void *ptr, size_t orig_size, size_t size)
132 {
133  if (FTPCheckMemcap((uint32_t)(size - orig_size)) == 0) {
135  return NULL;
136  }
137 
138  void *rptr = SCRealloc(ptr, size);
139  if (rptr == NULL) {
141  return NULL;
142  }
143 
144  if (size > orig_size) {
145  FTPIncrMemuse(size - orig_size);
146  } else {
147  FTPDecrMemuse(orig_size - size);
148  }
149 
150  return rptr;
151 }
152 
153 static void FTPFree(void *ptr, size_t size)
154 {
155  SCFree(ptr);
156 
157  FTPDecrMemuse((uint64_t)size);
158 }
159 
160 static FTPResponseWrapper *FTPResponseWrapperAlloc(FTPResponseLine *response)
161 {
162  FTPResponseWrapper *wrapper = FTPCalloc(1, sizeof(FTPResponseWrapper));
163  if (likely(wrapper)) {
164  FTPIncrMemuse(response->total_size);
165  wrapper->response = response;
166  }
167  return wrapper;
168 }
169 
170 static void FTPResponseWrapperFree(FTPResponseWrapper *wrapper)
171 {
172  if (wrapper->response) {
173  FTPDecrMemuse(wrapper->response->total_size);
174  SCFTPFreeResponseLine(wrapper->response);
175  }
176 
177  FTPFree(wrapper, sizeof(FTPResponseWrapper));
178 }
179 
180 static void *FTPLocalStorageAlloc(void)
181 {
182  /* needed by the mpm */
183  FTPThreadCtx *td = SCCalloc(1, sizeof(*td));
184  if (td == NULL) {
185  exit(EXIT_FAILURE);
186  }
187 
188  td->pmq = SCCalloc(1, sizeof(*td->pmq));
189  if (td->pmq == NULL) {
190  exit(EXIT_FAILURE);
191  }
192  PmqSetup(td->pmq);
193 
194  td->ftp_mpm_thread_ctx = SCCalloc(1, sizeof(MpmThreadCtx));
195  if (unlikely(td->ftp_mpm_thread_ctx == NULL)) {
196  exit(EXIT_FAILURE);
197  }
199  return td;
200 }
201 
202 static void FTPLocalStorageFree(void *ptr)
203 {
204  FTPThreadCtx *td = ptr;
205  if (td != NULL) {
206  if (td->pmq != NULL) {
207  PmqFree(td->pmq);
208  SCFree(td->pmq);
209  }
210 
211  if (td->ftp_mpm_thread_ctx != NULL) {
214  }
215 
216  SCFree(td);
217  }
218 }
219 static FTPTransaction *FTPTransactionCreate(FtpState *state)
220 {
221  SCEnter();
222  FTPTransaction *firsttx = TAILQ_FIRST(&state->tx_list);
223  if (firsttx && state->tx_cnt - firsttx->tx_id > ftp_config_maxtx) {
224  // FTP does not set events yet...
225  return NULL;
226  }
227  FTPTransaction *tx = FTPCalloc(1, sizeof(*tx));
228  if (tx == NULL) {
229  return NULL;
230  }
231 
232  TAILQ_INSERT_TAIL(&state->tx_list, tx, next);
233  tx->tx_id = state->tx_cnt++;
234 
235  TAILQ_INIT(&tx->response_list);
236 
237  SCLogDebug("new transaction %p (state tx cnt %"PRIu64")", tx, state->tx_cnt);
238  return tx;
239 }
240 
241 static void FTPTransactionFree(FTPTransaction *tx)
242 {
243  SCEnter();
244 
245  SCAppLayerTxDataCleanup(&tx->tx_data);
246 
247  if (tx->request) {
248  FTPFree(tx->request, tx->request_length);
249  }
250 
251  FTPResponseWrapper *wrapper;
252  while ((wrapper = TAILQ_FIRST(&tx->response_list))) {
253  TAILQ_REMOVE(&tx->response_list, wrapper, next);
254  FTPResponseWrapperFree(wrapper);
255  }
256 
257  FTPFree(tx, sizeof(*tx));
258 }
259 
260 typedef struct FtpInput_ {
261  const uint8_t *buf;
262  int32_t consumed;
263  int32_t len;
264  int32_t orig_len;
266 
267 static AppLayerResult FTPGetLineForDirection(
268  FtpLineState *line, FtpInput *input, bool *current_line_truncated)
269 {
270  SCEnter();
271 
272  /* we have run out of input */
273  if (input->len <= 0)
274  return APP_LAYER_ERROR;
275 
276  uint8_t *lf_idx = memchr(input->buf + input->consumed, 0x0a, input->len);
277 
278  if (lf_idx == NULL) {
279  if (!(*current_line_truncated) && (uint32_t)input->len >= ftp_max_line_len) {
280  *current_line_truncated = true;
281  line->buf = input->buf;
282  line->len = ftp_max_line_len;
283  line->delim_len = 0;
284  input->len = 0;
286  }
287  SCReturnStruct(APP_LAYER_INCOMPLETE(input->consumed, input->len + 1));
288  } else if (*current_line_truncated) {
289  // Whatever came in with first LF should also get discarded
290  *current_line_truncated = false;
291  line->len = 0;
292  line->delim_len = 0;
293  input->len = 0;
295  } else {
296  // There could be one chunk of command data that has LF but post the line limit
297  // e.g. input_len = 5077
298  // lf_idx = 5010
299  // max_line_len = 4096
300  uint32_t o_consumed = input->consumed;
301  input->consumed = (uint32_t)(lf_idx - input->buf + 1);
302  line->len = input->consumed - o_consumed;
303  input->len -= line->len;
304  line->lf_found = true;
305  DEBUG_VALIDATE_BUG_ON((input->consumed + input->len) != input->orig_len);
306  line->buf = input->buf + o_consumed;
307  if (line->len >= ftp_max_line_len) {
308  *current_line_truncated = true;
309  line->len = ftp_max_line_len;
311  }
312  if (input->consumed >= 2 && input->buf[input->consumed - 2] == 0x0D) {
313  line->delim_len = 2;
314  line->len -= 2;
315  } else {
316  line->delim_len = 1;
317  line->len -= 1;
318  }
320  }
321 }
322 
323 /**
324  * \brief This function is called to determine and set which command is being
325  * transferred to the ftp server
326  * \param thread context
327  * \param input input line of the command
328  * \param len of the command
329  * \param cmd_descriptor when the command has been parsed
330  *
331  * \retval 1 when the command is parsed, 0 otherwise
332  */
333 static int FTPParseRequestCommand(
334  FTPThreadCtx *td, FtpLineState *line, FtpCommandInfo *cmd_descriptor)
335 {
336  SCEnter();
337 
338  /* I don't like this pmq reset here. We'll devise a method later, that
339  * should make the use of the mpm very efficient */
340  PmqReset(td->pmq);
341  int mpm_cnt = mpm_table[FTP_MPM].Search(
342  ftp_mpm_ctx, td->ftp_mpm_thread_ctx, td->pmq, line->buf, line->len);
343  if (mpm_cnt) {
344  uint8_t command_code;
345  if (SCGetFtpCommandInfo(td->pmq->rule_id_array[0], NULL, &command_code, NULL)) {
346  cmd_descriptor->command_code = command_code;
347  /* FTP command indices are expressed in Rust as a u8 */
348  cmd_descriptor->command_index = (uint8_t)td->pmq->rule_id_array[0];
349  SCReturnInt(1);
350  } else {
351  /* Where is out command? */
353  }
354 #ifdef DEBUG
355  if (SCLogDebugEnabled()) {
356  const char *command_name = NULL;
357  (void)SCGetFtpCommandInfo(td->pmq->rule_id_array[0], &command_name, NULL, NULL);
358  SCLogDebug("matching FTP command is %s [code: %d, index %d]", command_name,
359  command_code, td->pmq->rule_id_array[0]);
360  }
361 #endif
362  }
363 
364  cmd_descriptor->command_code = FTP_COMMAND_UNKNOWN;
365  SCReturnInt(0);
366 }
367 
368 static void FtpTransferCmdFree(void *data)
369 {
370  FtpTransferCmd *cmd = (FtpTransferCmd *)data;
371  if (cmd == NULL)
372  return;
373  if (cmd->file_name) {
374  FTPFree((void *)cmd->file_name, cmd->file_len + 1);
375  }
376  SCFTPTransferCmdFree(cmd);
377  FTPDecrMemuse((uint64_t)sizeof(FtpTransferCmd));
378 }
379 
380 static uint32_t CopyCommandLine(uint8_t **dest, FtpLineState *line)
381 {
382  if (likely(line->len)) {
383  uint8_t *where = FTPCalloc(line->len + 1, sizeof(char));
384  if (unlikely(where == NULL)) {
385  return 0;
386  }
387  memcpy(where, line->buf, line->len);
388 
389  /* Remove trailing newlines/carriage returns */
390  while (line->len && isspace((unsigned char)where[line->len - 1])) {
391  line->len--;
392  }
393 
394  where[line->len] = '\0';
395  *dest = where;
396  }
397  /* either 0 or actual */
398  return line->len ? line->len + 1 : 0;
399 }
400 
401 #include "util-print.h"
402 
403 /**
404  * \brief This function is called to retrieve a ftp request
405  * \param ftp_state the ftp state structure for the parser
406  *
407  * \retval APP_LAYER_OK when input was process successfully
408  * \retval APP_LAYER_ERROR when a unrecoverable error was encountered
409  */
410 static AppLayerResult FTPParseRequest(Flow *f, void *ftp_state, AppLayerParserState *pstate,
411  StreamSlice stream_slice, void *local_data)
412 {
413  FTPThreadCtx *thread_data = local_data;
414 
415  SCEnter();
416  /* PrintRawDataFp(stdout, input,input_len); */
417 
418  FtpState *state = (FtpState *)ftp_state;
419  void *ptmp;
420 
421  const uint8_t *input = StreamSliceGetData(&stream_slice);
422  uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
423 
424  if (input == NULL && AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS)) {
426  } else if (input == NULL || input_len == 0) {
428  }
429 
430  FtpInput ftpi = { .buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
431  FtpLineState line = { .buf = NULL, .len = 0, .delim_len = 0, .lf_found = false };
432 
433  uint8_t direction = STREAM_TOSERVER;
434  AppLayerResult res;
435  while (1) {
436  res = FTPGetLineForDirection(&line, &ftpi, &state->current_line_truncated_ts);
437  if (res.status == 1) {
438  return res;
439  } else if (res.status == -1) {
440  break;
441  }
442 
443  FtpCommandInfo cmd_descriptor;
444  if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
445  state->command = FTP_COMMAND_UNKNOWN;
446  continue;
447  }
448 
449  state->command = cmd_descriptor.command_code;
450  FTPTransaction *tx = FTPTransactionCreate(state);
451  if (unlikely(tx == NULL))
453  state->curr_tx = tx;
454 
455  tx->command_descriptor = cmd_descriptor;
456  tx->request_length = CopyCommandLine(&tx->request, &line);
458 
459  if (line.lf_found) {
460  state->current_line_truncated_ts = false;
461  }
462  if (tx->request_truncated) {
463  AppLayerDecoderEventsSetEventRaw(&tx->tx_data.events, FtpEventRequestCommandTooLong);
464  }
465 
466  /* change direction (default to server) so expectation will handle
467  * the correct message when expectation will match.
468  * For ftp active mode, data connection direction is opposite to
469  * control direction.
470  */
471  if ((state->active && state->command == FTP_COMMAND_STOR) ||
472  (!state->active && state->command == FTP_COMMAND_RETR)) {
473  direction = STREAM_TOCLIENT;
474  }
475 
476  switch (state->command) {
477  case FTP_COMMAND_EPRT:
478  // fallthrough
479  case FTP_COMMAND_PORT:
480  if (line.len + 1 > state->port_line_size) {
481  /* Allocate an extra byte for a NULL terminator */
482  ptmp = FTPRealloc(state->port_line, state->port_line_size, line.len + 1);
483  if (ptmp == NULL) {
484  if (state->port_line) {
485  FTPFree(state->port_line, state->port_line_size);
486  state->port_line = NULL;
487  state->port_line_size = 0;
488  state->port_line_len = 0;
489  }
491  }
492  state->port_line = ptmp;
493  state->port_line_size = line.len + 1;
494  }
495  memcpy(state->port_line, line.buf, line.len);
496  state->port_line_len = line.len;
497  break;
498  case FTP_COMMAND_RETR:
499  // fallthrough
500  case FTP_COMMAND_STOR: {
501  /* Ensure that there is a negotiated dyn port and a file
502  * name -- need more than 5 chars: cmd [4], space, <filename>
503  */
504  if (state->dyn_port == 0 || line.len < 6) {
506  }
507  FtpTransferCmd *data = SCFTPTransferCmdNew();
508  if (data == NULL)
510  FTPIncrMemuse((uint64_t)(sizeof *data));
511  data->data_free = FtpTransferCmdFree;
512 
513  /*
514  * Min size has been checked in FTPParseRequestCommand
515  * SC_FILENAME_MAX includes the null
516  */
517  uint32_t file_name_len = MIN(SC_FILENAME_MAX - 1, line.len - 5);
518 #if SC_FILENAME_MAX > UINT16_MAX
519 #error SC_FILENAME_MAX is greater than UINT16_MAX
520 #endif
521  data->file_name = FTPCalloc(file_name_len + 1, sizeof(char));
522  if (data->file_name == NULL) {
523  FtpTransferCmdFree(data);
525  }
526  data->file_name[file_name_len] = 0;
527  data->file_len = (uint16_t)file_name_len;
528  memcpy(data->file_name, line.buf + 5, file_name_len);
529  data->cmd = state->command;
530  data->flow_id = FlowGetId(f);
531  data->direction = direction;
532  int ret = AppLayerExpectationCreate(f, direction,
533  0, state->dyn_port, ALPROTO_FTPDATA, data);
534  if (ret == -1) {
535  FtpTransferCmdFree(data);
536  SCLogDebug("No expectation created.");
538  } else {
539  SCLogDebug("Expectation created [direction: %s, dynamic port %"PRIu16"].",
540  state->active ? "to server" : "to client",
541  state->dyn_port);
542  }
543 
544  /* reset the dyn port to avoid duplicate */
545  state->dyn_port = 0;
546  /* reset active/passive indicator */
547  state->active = false;
548  } break;
549  default:
550  break;
551  }
552  if (line.len >= ftp_max_line_len) {
553  ftpi.consumed = ftpi.len + 1;
554  break;
555  }
556  }
557 
559 }
560 
561 static int FTPParsePassiveResponse(FtpState *state, const uint8_t *input, uint32_t input_len)
562 {
563  uint16_t dyn_port = rs_ftp_pasv_response(input, input_len);
564  if (dyn_port == 0) {
565  return -1;
566  }
567  SCLogDebug("FTP passive mode (v4): dynamic port %"PRIu16"", dyn_port);
568  state->active = false;
569  state->dyn_port = dyn_port;
570  state->curr_tx->dyn_port = dyn_port;
571  state->curr_tx->active = false;
572 
573  return 0;
574 }
575 
576 static int FTPParsePassiveResponseV6(FtpState *state, const uint8_t *input, uint32_t input_len)
577 {
578  uint16_t dyn_port = rs_ftp_epsv_response(input, input_len);
579  if (dyn_port == 0) {
580  return -1;
581  }
582  SCLogDebug("FTP passive mode (v6): dynamic port %"PRIu16"", dyn_port);
583  state->active = false;
584  state->dyn_port = dyn_port;
585  state->curr_tx->dyn_port = dyn_port;
586  state->curr_tx->active = false;
587  return 0;
588 }
589 
590 /**
591  * \brief Handle preliminary replies -- keep tx open
592  * \retval bool True for a positive preliminary reply; false otherwise
593  *
594  * 1yz Positive Preliminary reply
595  *
596  * The requested action is being initiated; expect another
597  * reply before proceeding with a new command
598  */
599 static inline bool FTPIsPPR(const uint8_t *input, uint32_t input_len)
600 {
601  return input_len >= 4 && isdigit(input[0]) && input[0] == '1' &&
602  isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
603 }
604 
605 /**
606  * \brief This function is called to retrieve a ftp response
607  * \param ftp_state the ftp state structure for the parser
608  * \param input input line of the command
609  * \param input_len length of the request
610  * \param output the resulting output
611  *
612  * \retval 1 when the command is parsed, 0 otherwise
613  */
614 static AppLayerResult FTPParseResponse(Flow *f, void *ftp_state, AppLayerParserState *pstate,
615  StreamSlice stream_slice, void *local_data)
616 {
617  FtpState *state = (FtpState *)ftp_state;
618 
619  const uint8_t *input = StreamSliceGetData(&stream_slice);
620  uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
621 
622  if (unlikely(input_len == 0)) {
624  }
625  FtpInput ftpi = { .buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
626  FtpLineState line = { .buf = NULL, .len = 0, .delim_len = 0, .lf_found = false };
627 
628  FTPTransaction *lasttx = TAILQ_FIRST(&state->tx_list);
629  AppLayerResult res;
630  while (1) {
631  res = FTPGetLineForDirection(&line, &ftpi, &state->current_line_truncated_tc);
632  if (res.status == 1) {
633  return res;
634  } else if (res.status == -1) {
635  break;
636  }
637  FTPTransaction *tx = FTPGetOldestTx(state, lasttx);
638  if (tx == NULL) {
639  tx = FTPTransactionCreate(state);
640  }
641  if (unlikely(tx == NULL)) {
643  }
644  lasttx = tx;
645  tx->tx_data.updated_tc = true;
646  if (state->command == FTP_COMMAND_UNKNOWN) {
647  /* unknown */
648  tx->command_descriptor.command_code = FTP_COMMAND_UNKNOWN;
649  }
650 
651  state->curr_tx = tx;
652  uint16_t dyn_port;
653  switch (state->command) {
654  case FTP_COMMAND_AUTH_TLS:
655  if (line.len >= 4 && SCMemcmp("234 ", line.buf, 4) == 0) {
657  }
658  break;
659 
660  case FTP_COMMAND_EPRT:
661  dyn_port = rs_ftp_active_eprt(state->port_line, state->port_line_len);
662  if (dyn_port == 0) {
663  goto tx_complete;
664  }
665  state->dyn_port = dyn_port;
666  state->active = true;
667  tx->dyn_port = dyn_port;
668  tx->active = true;
669  SCLogDebug("FTP active mode (v6): dynamic port %" PRIu16 "", dyn_port);
670  break;
671 
672  case FTP_COMMAND_PORT:
673  dyn_port = rs_ftp_active_port(state->port_line, state->port_line_len);
674  if (dyn_port == 0) {
675  goto tx_complete;
676  }
677  state->dyn_port = dyn_port;
678  state->active = true;
679  tx->dyn_port = state->dyn_port;
680  tx->active = true;
681  SCLogDebug("FTP active mode (v4): dynamic port %" PRIu16 "", dyn_port);
682  break;
683 
684  case FTP_COMMAND_PASV:
685  if (line.len >= 4 && SCMemcmp("227 ", line.buf, 4) == 0) {
686  FTPParsePassiveResponse(ftp_state, line.buf, line.len);
687  }
688  break;
689 
690  case FTP_COMMAND_EPSV:
691  if (line.len >= 4 && SCMemcmp("229 ", line.buf, 4) == 0) {
692  FTPParsePassiveResponseV6(ftp_state, line.buf, line.len);
693  }
694  break;
695  default:
696  break;
697  }
698 
699  if (likely(line.len)) {
700  FTPResponseLine *response = SCFTPParseResponseLine((const char *)line.buf, line.len);
701  if (likely(response)) {
702  FTPResponseWrapper *wrapper = FTPResponseWrapperAlloc(response);
703  if (likely(wrapper)) {
704  response->truncated = state->current_line_truncated_tc;
705  if (response->truncated) {
707  &tx->tx_data.events, FtpEventResponseCommandTooLong);
708  }
709  if (line.lf_found) {
710  state->current_line_truncated_tc = false;
711  }
712  TAILQ_INSERT_TAIL(&tx->response_list, wrapper, next);
713  } else {
714  SCFTPFreeResponseLine(response);
715  }
716  } else {
717  SCLogDebug("unable to parse FTP response line \"%s\"", line.buf);
718  }
719  }
720 
721  /* Handle preliminary replies -- keep tx open */
722  if (FTPIsPPR(line.buf, line.len)) {
723  continue;
724  }
725  tx_complete:
726  tx->done = true;
727 
728  if (line.len >= ftp_max_line_len) {
729  ftpi.consumed = ftpi.len + 1;
730  break;
731  }
732  }
733 
735 }
736 
737 
738 #ifdef DEBUG
739 static SCMutex ftp_state_mem_lock = SCMUTEX_INITIALIZER;
740 static uint64_t ftp_state_memuse = 0;
741 static uint64_t ftp_state_memcnt = 0;
742 #endif
743 
744 static void *FTPStateAlloc(void *orig_state, AppProto proto_orig)
745 {
746  void *s = FTPCalloc(1, sizeof(FtpState));
747  if (unlikely(s == NULL))
748  return NULL;
749 
750  FtpState *ftp_state = (FtpState *) s;
751  TAILQ_INIT(&ftp_state->tx_list);
752 
753 #ifdef DEBUG
754  SCMutexLock(&ftp_state_mem_lock);
755  ftp_state_memcnt++;
756  ftp_state_memuse+=sizeof(FtpState);
757  SCMutexUnlock(&ftp_state_mem_lock);
758 #endif
759  return s;
760 }
761 
762 static void FTPStateFree(void *s)
763 {
764  FtpState *fstate = (FtpState *) s;
765  if (fstate->port_line != NULL)
766  FTPFree(fstate->port_line, fstate->port_line_size);
767 
768  FTPTransaction *tx = NULL;
769  while ((tx = TAILQ_FIRST(&fstate->tx_list))) {
770  TAILQ_REMOVE(&fstate->tx_list, tx, next);
771 #ifdef DEBUG
772  if (SCLogDebugEnabled()) {
773  const char *command_name = NULL;
774  (void)SCGetFtpCommandInfo(
775  tx->command_descriptor.command_index, &command_name, NULL, NULL);
776  SCLogDebug("[%s] state %p id %" PRIu64 ", Freeing %d bytes at %p",
777  command_name != NULL ? command_name : "n/a", s, tx->tx_id, tx->request_length,
778  tx->request);
779  }
780 #endif
781 
782  FTPTransactionFree(tx);
783  }
784 
785  FTPFree(s, sizeof(FtpState));
786 #ifdef DEBUG
787  SCMutexLock(&ftp_state_mem_lock);
788  ftp_state_memcnt--;
789  ftp_state_memuse-=sizeof(FtpState);
790  SCMutexUnlock(&ftp_state_mem_lock);
791 #endif
792 }
793 
794 /**
795  * \brief This function returns the oldest open transaction; if none
796  * are open, then the oldest transaction is returned
797  * \param ftp_state the ftp state structure for the parser
798  * \param starttx the ftp transaction where to start looking
799  *
800  * \retval transaction pointer when a transaction was found; NULL otherwise.
801  */
802 static FTPTransaction *FTPGetOldestTx(const FtpState *ftp_state, FTPTransaction *starttx)
803 {
804  if (unlikely(!ftp_state)) {
805  SCLogDebug("NULL state object; no transactions available");
806  return NULL;
807  }
808  FTPTransaction *tx = starttx;
809  FTPTransaction *lasttx = NULL;
810  while(tx != NULL) {
811  /* Return oldest open tx */
812  if (!tx->done) {
813  SCLogDebug("Returning tx %p id %"PRIu64, tx, tx->tx_id);
814  return tx;
815  }
816  /* save for the end */
817  lasttx = tx;
818  tx = TAILQ_NEXT(tx, next);
819  }
820  /* All tx are closed; return last element */
821  if (lasttx)
822  SCLogDebug("Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->tx_id);
823  return lasttx;
824 }
825 
826 static void *FTPGetTx(void *state, uint64_t tx_id)
827 {
828  FtpState *ftp_state = (FtpState *)state;
829  if (ftp_state) {
830  FTPTransaction *tx = NULL;
831 
832  if (ftp_state->curr_tx == NULL)
833  return NULL;
834  if (ftp_state->curr_tx->tx_id == tx_id)
835  return ftp_state->curr_tx;
836 
837  TAILQ_FOREACH(tx, &ftp_state->tx_list, next) {
838  if (tx->tx_id == tx_id)
839  return tx;
840  }
841  }
842  return NULL;
843 }
844 
845 static AppLayerTxData *FTPGetTxData(void *vtx)
846 {
847  FTPTransaction *tx = (FTPTransaction *)vtx;
848  return &tx->tx_data;
849 }
850 
851 static AppLayerStateData *FTPGetStateData(void *vstate)
852 {
853  FtpState *s = (FtpState *)vstate;
854  return &s->state_data;
855 }
856 
857 static void FTPStateTransactionFree(void *state, uint64_t tx_id)
858 {
859  FtpState *ftp_state = state;
860  FTPTransaction *tx = NULL;
861  TAILQ_FOREACH(tx, &ftp_state->tx_list, next) {
862  if (tx_id < tx->tx_id)
863  break;
864  else if (tx_id > tx->tx_id)
865  continue;
866 
867  if (tx == ftp_state->curr_tx)
868  ftp_state->curr_tx = NULL;
869  TAILQ_REMOVE(&ftp_state->tx_list, tx, next);
870  FTPTransactionFree(tx);
871  break;
872  }
873 }
874 
875 static uint64_t FTPGetTxCnt(void *state)
876 {
877  uint64_t cnt = 0;
878  FtpState *ftp_state = state;
879  if (ftp_state) {
880  cnt = ftp_state->tx_cnt;
881  }
882  SCLogDebug("returning state %p %"PRIu64, state, cnt);
883  return cnt;
884 }
885 
886 static int FTPGetAlstateProgress(void *vtx, uint8_t direction)
887 {
888  SCLogDebug("tx %p", vtx);
889  FTPTransaction *tx = vtx;
890 
891  if (!tx->done) {
892  if (direction == STREAM_TOSERVER &&
893  tx->command_descriptor.command_code == FTP_COMMAND_PORT) {
894  return FTP_STATE_PORT_DONE;
895  }
896  return FTP_STATE_IN_PROGRESS;
897  }
898 
899  return FTP_STATE_FINISHED;
900 }
901 
902 static AppProto FTPUserProbingParser(
903  Flow *f, uint8_t direction, const uint8_t *input, uint32_t len, uint8_t *rdir)
904 {
905  if (f->alproto_tc == ALPROTO_POP3) {
906  // POP traffic begins by same "USER" pattern as FTP
907  return ALPROTO_FAILED;
908  }
909  return ALPROTO_FTP;
910 }
911 
912 static AppProto FTPServerProbingParser(
913  Flow *f, uint8_t direction, const uint8_t *input, uint32_t len, uint8_t *rdir)
914 {
915  // another check for minimum length
916  if (len < 5) {
917  return ALPROTO_UNKNOWN;
918  }
919  // begins by 220
920  if (input[0] != '2' || input[1] != '2' || input[2] != '0') {
921  return ALPROTO_FAILED;
922  }
923  // followed by space or hypen
924  if (input[3] != ' ' && input[3] != '-') {
925  return ALPROTO_FAILED;
926  }
927  if (f->alproto_ts == ALPROTO_FTP || (f->todstbytecnt > 4 && f->alproto_ts == ALPROTO_UNKNOWN)) {
928  // only validates FTP if client side was FTP
929  // or if client side is unknown despite having received bytes
930  if (memchr(input + 4, '\n', len - 4) != NULL) {
931  return ALPROTO_FTP;
932  }
933  }
934  return ALPROTO_UNKNOWN;
935 }
936 
937 static int FTPRegisterPatternsForProtocolDetection(void)
938 {
940  IPPROTO_TCP, ALPROTO_FTP, "220 (", 5, 0, STREAM_TOCLIENT) < 0) {
941  return -1;
942  }
944  IPPROTO_TCP, ALPROTO_FTP, "FEAT", 4, 0, STREAM_TOSERVER) < 0) {
945  return -1;
946  }
947  if (AppLayerProtoDetectPMRegisterPatternCSwPP(IPPROTO_TCP, ALPROTO_FTP, "USER ", 5, 0,
948  STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
949  return -1;
950  }
952  IPPROTO_TCP, ALPROTO_FTP, "PASS ", 5, 0, STREAM_TOSERVER) < 0) {
953  return -1;
954  }
956  IPPROTO_TCP, ALPROTO_FTP, "PORT ", 5, 0, STREAM_TOSERVER) < 0) {
957  return -1;
958  }
959  // Only check FTP on known ports as the banner has nothing special beyond
960  // the response code shared with SMTP.
962  "tcp", IPPROTO_TCP, "ftp", ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
963  // STREAM_TOSERVER here means use 21 as flow destination port
964  // and NULL, FTPServerProbingParser means use probing parser to client
965  AppLayerProtoDetectPPRegister(IPPROTO_TCP, "21", ALPROTO_FTP, 0, 5, STREAM_TOSERVER, NULL,
966  FTPServerProbingParser);
967  }
968  return 0;
969 }
970 
971 
973 
974 /**
975  * \brief This function is called to retrieve a ftp request
976  * \param ftp_state the ftp state structure for the parser
977  * \param output the resulting output
978  *
979  * \retval 1 when the command is parsed, 0 otherwise
980  */
981 static AppLayerResult FTPDataParse(Flow *f, FtpDataState *ftpdata_state,
982  AppLayerParserState *pstate, StreamSlice stream_slice, void *local_data, uint8_t direction)
983 {
984  const uint8_t *input = StreamSliceGetData(&stream_slice);
985  uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
986  const bool eof = (direction & STREAM_TOSERVER)
989 
990  SCTxDataUpdateFileFlags(&ftpdata_state->tx_data, ftpdata_state->state_data.file_flags);
991  if (ftpdata_state->tx_data.file_tx == 0)
992  ftpdata_state->tx_data.file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
993  if (direction & STREAM_TOSERVER) {
994  ftpdata_state->tx_data.updated_ts = true;
995  } else {
996  ftpdata_state->tx_data.updated_tc = true;
997  }
998  /* we depend on detection engine for file pruning */
999  const uint16_t flags = FileFlowFlagsToFlags(ftpdata_state->tx_data.file_flags, direction);
1000  int ret = 0;
1001 
1002  SCLogDebug("FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len, flags, direction,
1003  (direction & STREAM_TOSERVER) ? "toserver" : "toclient", eof ? "true" : "false");
1004 
1005  SCLogDebug("FTP-DATA flags %04x dir %d", flags, direction);
1006  if (input_len && ftpdata_state->files == NULL) {
1007  FtpTransferCmd *data =
1008  (FtpTransferCmd *)FlowGetStorageById(f, AppLayerExpectationGetFlowId());
1009  if (data == NULL) {
1011  }
1012 
1013  /* we shouldn't get data in the wrong dir. Don't set things up for this dir */
1014  if ((direction & data->direction) == 0) {
1015  // TODO set event for data in wrong direction
1016  SCLogDebug("input %u not for our direction (%s): %s/%s", input_len,
1017  (direction & STREAM_TOSERVER) ? "toserver" : "toclient",
1018  data->cmd == FTP_COMMAND_STOR ? "STOR" : "RETR",
1019  (data->direction & STREAM_TOSERVER) ? "toserver" : "toclient");
1021  }
1022 
1023  ftpdata_state->files = FileContainerAlloc();
1024  if (ftpdata_state->files == NULL) {
1027  }
1028 
1029  ftpdata_state->file_name = data->file_name;
1030  ftpdata_state->file_len = data->file_len;
1031  data->file_name = NULL;
1032  data->file_len = 0;
1033  f->parent_id = data->flow_id;
1034  ftpdata_state->command = data->cmd;
1035  switch (data->cmd) {
1036  case FTP_COMMAND_STOR:
1037  ftpdata_state->direction = data->direction;
1038  SCLogDebug("STOR data to %s",
1039  (ftpdata_state->direction & STREAM_TOSERVER) ? "toserver" : "toclient");
1040  break;
1041  case FTP_COMMAND_RETR:
1042  ftpdata_state->direction = data->direction;
1043  SCLogDebug("RETR data to %s",
1044  (ftpdata_state->direction & STREAM_TOSERVER) ? "toserver" : "toclient");
1045  break;
1046  default:
1047  break;
1048  }
1049 
1050  /* open with fixed track_id 0 as we can have just one
1051  * file per ftp-data flow. */
1052  if (FileOpenFileWithId(ftpdata_state->files, &sbcfg,
1053  0ULL, (uint8_t *) ftpdata_state->file_name,
1054  ftpdata_state->file_len,
1055  input, input_len, flags) != 0) {
1056  SCLogDebug("Can't open file");
1057  ret = -1;
1058  }
1060  ftpdata_state->tx_data.files_opened = 1;
1061  } else {
1062  if (ftpdata_state->state == FTPDATA_STATE_FINISHED) {
1063  SCLogDebug("state is already finished");
1064  DEBUG_VALIDATE_BUG_ON(input_len); // data after state finished is a bug.
1066  }
1067  if ((direction & ftpdata_state->direction) == 0) {
1068  if (input_len) {
1069  // TODO set event for data in wrong direction
1070  }
1071  SCLogDebug("input %u not for us (%s): %s/%s", input_len,
1072  (direction & STREAM_TOSERVER) ? "toserver" : "toclient",
1073  ftpdata_state->command == FTP_COMMAND_STOR ? "STOR" : "RETR",
1074  (ftpdata_state->direction & STREAM_TOSERVER) ? "toserver" : "toclient");
1076  }
1077  if (input_len != 0) {
1078  ret = FileAppendData(ftpdata_state->files, &sbcfg, input, input_len);
1079  if (ret == -2) {
1080  ret = 0;
1081  SCLogDebug("FileAppendData() - file no longer being extracted");
1082  goto out;
1083  } else if (ret < 0) {
1084  SCLogDebug("FileAppendData() failed: %d", ret);
1085  ret = -2;
1086  goto out;
1087  }
1088  }
1089  }
1090 
1091  BUG_ON((direction & ftpdata_state->direction) == 0); // should be unreachable
1092  if (eof) {
1093  ret = FileCloseFile(ftpdata_state->files, &sbcfg, NULL, 0, flags);
1094  ftpdata_state->state = FTPDATA_STATE_FINISHED;
1095  SCLogDebug("closed because of eof: state now FTPDATA_STATE_FINISHED");
1096  }
1097 out:
1098  if (ret < 0) {
1100  }
1102 }
1103 
1104 static AppLayerResult FTPDataParseRequest(Flow *f, void *ftp_state, AppLayerParserState *pstate,
1105  StreamSlice stream_slice, void *local_data)
1106 {
1107  return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1108 }
1109 
1110 static AppLayerResult FTPDataParseResponse(Flow *f, void *ftp_state, AppLayerParserState *pstate,
1111  StreamSlice stream_slice, void *local_data)
1112 {
1113  return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1114 }
1115 
1116 #ifdef DEBUG
1117 static SCMutex ftpdata_state_mem_lock = SCMUTEX_INITIALIZER;
1118 static uint64_t ftpdata_state_memuse = 0;
1119 static uint64_t ftpdata_state_memcnt = 0;
1120 #endif
1121 
1122 static void *FTPDataStateAlloc(void *orig_state, AppProto proto_orig)
1123 {
1124  void *s = FTPCalloc(1, sizeof(FtpDataState));
1125  if (unlikely(s == NULL))
1126  return NULL;
1127 
1128  FtpDataState *state = (FtpDataState *) s;
1129  state->state = FTPDATA_STATE_IN_PROGRESS;
1130 
1131 #ifdef DEBUG
1132  SCMutexLock(&ftpdata_state_mem_lock);
1133  ftpdata_state_memcnt++;
1134  ftpdata_state_memuse+=sizeof(FtpDataState);
1135  SCMutexUnlock(&ftpdata_state_mem_lock);
1136 #endif
1137  return s;
1138 }
1139 
1140 static void FTPDataStateFree(void *s)
1141 {
1142  FtpDataState *fstate = (FtpDataState *) s;
1143 
1144  SCAppLayerTxDataCleanup(&fstate->tx_data);
1145 
1146  if (fstate->file_name != NULL) {
1147  FTPFree(fstate->file_name, fstate->file_len + 1);
1148  }
1149 
1150  FileContainerFree(fstate->files, &sbcfg);
1151 
1152  FTPFree(s, sizeof(FtpDataState));
1153 #ifdef DEBUG
1154  SCMutexLock(&ftpdata_state_mem_lock);
1155  ftpdata_state_memcnt--;
1156  ftpdata_state_memuse-=sizeof(FtpDataState);
1157  SCMutexUnlock(&ftpdata_state_mem_lock);
1158 #endif
1159 }
1160 
1161 static AppLayerTxData *FTPDataGetTxData(void *vtx)
1162 {
1163  FtpDataState *ftp_state = (FtpDataState *)vtx;
1164  return &ftp_state->tx_data;
1165 }
1166 
1167 static AppLayerStateData *FTPDataGetStateData(void *vstate)
1168 {
1169  FtpDataState *ftp_state = (FtpDataState *)vstate;
1170  return &ftp_state->state_data;
1171 }
1172 
1173 static void FTPDataStateTransactionFree(void *state, uint64_t tx_id)
1174 {
1175  /* do nothing */
1176 }
1177 
1178 static void *FTPDataGetTx(void *state, uint64_t tx_id)
1179 {
1180  FtpDataState *ftp_state = (FtpDataState *)state;
1181  return ftp_state;
1182 }
1183 
1184 static uint64_t FTPDataGetTxCnt(void *state)
1185 {
1186  /* ftp-data is single tx */
1187  return 1;
1188 }
1189 
1190 static int FTPDataGetAlstateProgress(void *tx, uint8_t direction)
1191 {
1192  FtpDataState *ftpdata_state = (FtpDataState *)tx;
1193  if (direction == ftpdata_state->direction)
1194  return ftpdata_state->state;
1195  else
1196  return FTPDATA_STATE_FINISHED;
1197 }
1198 
1199 static AppLayerGetFileState FTPDataStateGetTxFiles(void *tx, uint8_t direction)
1200 {
1201  FtpDataState *ftpdata_state = (FtpDataState *)tx;
1202  AppLayerGetFileState files = { .fc = NULL, .cfg = &sbcfg };
1203 
1204  if (direction == ftpdata_state->direction)
1205  files.fc = ftpdata_state->files;
1206 
1207  return files;
1208 }
1209 
1210 static void FTPSetMpmState(void)
1211 {
1212  ftp_mpm_ctx = SCCalloc(1, sizeof(MpmCtx));
1213  if (unlikely(ftp_mpm_ctx == NULL)) {
1214  exit(EXIT_FAILURE);
1215  }
1216  MpmInitCtx(ftp_mpm_ctx, FTP_MPM);
1217 
1218  SCFTPSetMpmState(ftp_mpm_ctx);
1219  mpm_table[FTP_MPM].Prepare(NULL, ftp_mpm_ctx);
1220 }
1221 
1222 static void FTPFreeMpmState(void)
1223 {
1224  if (ftp_mpm_ctx != NULL) {
1225  mpm_table[FTP_MPM].DestroyCtx(ftp_mpm_ctx);
1226  SCFree(ftp_mpm_ctx);
1227  ftp_mpm_ctx = NULL;
1228  }
1229 }
1230 
1231 /** \brief FTP tx iterator, specialized for its linked list
1232  *
1233  * \retval txptr or NULL if no more txs in list
1234  */
1235 static AppLayerGetTxIterTuple FTPGetTxIterator(const uint8_t ipproto, const AppProto alproto,
1236  void *alstate, uint64_t min_tx_id, uint64_t max_tx_id, AppLayerGetTxIterState *state)
1237 {
1238  FtpState *ftp_state = (FtpState *)alstate;
1239  AppLayerGetTxIterTuple no_tuple = { NULL, 0, false };
1240  if (ftp_state) {
1241  FTPTransaction *tx_ptr;
1242  if (state->un.ptr == NULL) {
1243  tx_ptr = TAILQ_FIRST(&ftp_state->tx_list);
1244  } else {
1245  tx_ptr = (FTPTransaction *)state->un.ptr;
1246  }
1247  if (tx_ptr) {
1248  while (tx_ptr->tx_id < min_tx_id) {
1249  tx_ptr = TAILQ_NEXT(tx_ptr, next);
1250  if (!tx_ptr) {
1251  return no_tuple;
1252  }
1253  }
1254  if (tx_ptr->tx_id >= max_tx_id) {
1255  return no_tuple;
1256  }
1257  state->un.ptr = TAILQ_NEXT(tx_ptr, next);
1258  AppLayerGetTxIterTuple tuple = {
1259  .tx_ptr = tx_ptr,
1260  .tx_id = tx_ptr->tx_id,
1261  .has_next = (state->un.ptr != NULL),
1262  };
1263  return tuple;
1264  }
1265  }
1266  return no_tuple;
1267 }
1268 
1270 {
1271  const char *proto_name = "ftp";
1272  const char *proto_data_name = "ftp-data";
1273 
1274  /** FTP */
1275  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
1277  if (FTPRegisterPatternsForProtocolDetection() < 0 )
1278  return;
1280  }
1281 
1282  if (AppLayerParserConfParserEnabled("tcp", proto_name)) {
1283  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_FTP, STREAM_TOSERVER,
1284  FTPParseRequest);
1285  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_FTP, STREAM_TOCLIENT,
1286  FTPParseResponse);
1287  AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_FTP, FTPStateAlloc, FTPStateFree);
1288  AppLayerParserRegisterParserAcceptableDataDirection(IPPROTO_TCP, ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1289 
1290  AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_FTP, FTPStateTransactionFree);
1291 
1292  AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_FTP, FTPGetTx);
1293  AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_FTP, FTPGetTxData);
1294  AppLayerParserRegisterGetTxIterator(IPPROTO_TCP, ALPROTO_FTP, FTPGetTxIterator);
1295  AppLayerParserRegisterStateDataFunc(IPPROTO_TCP, ALPROTO_FTP, FTPGetStateData);
1296 
1297  AppLayerParserRegisterLocalStorageFunc(IPPROTO_TCP, ALPROTO_FTP, FTPLocalStorageAlloc,
1298  FTPLocalStorageFree);
1299  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_FTP, FTPGetTxCnt);
1300 
1301  AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_FTP, FTPGetAlstateProgress);
1302 
1304  ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1305 
1307  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_FTPDATA, STREAM_TOSERVER,
1308  FTPDataParseRequest);
1309  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_FTPDATA, STREAM_TOCLIENT,
1310  FTPDataParseResponse);
1311  AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataStateAlloc, FTPDataStateFree);
1312  AppLayerParserRegisterParserAcceptableDataDirection(IPPROTO_TCP, ALPROTO_FTPDATA, STREAM_TOSERVER | STREAM_TOCLIENT);
1313  AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataStateTransactionFree);
1314 
1315  AppLayerParserRegisterGetTxFilesFunc(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataStateGetTxFiles);
1316 
1317  AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataGetTx);
1318  AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataGetTxData);
1319  AppLayerParserRegisterStateDataFunc(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataGetStateData);
1320 
1321  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataGetTxCnt);
1322 
1323  AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_FTPDATA, FTPDataGetAlstateProgress);
1324 
1326  ALPROTO_FTPDATA, FTPDATA_STATE_FINISHED, FTPDATA_STATE_FINISHED);
1327 
1328  AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_FTP, ftp_get_event_info);
1329  AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_FTP, ftp_get_event_info_by_id);
1330 
1333 
1334  sbcfg.buf_size = 4096;
1335  sbcfg.Calloc = FTPCalloc;
1336  sbcfg.Realloc = FTPRealloc;
1337  sbcfg.Free = FTPFree;
1338 
1339  FTPParseMemcap();
1340  } else {
1341  SCLogInfo("Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1342  }
1343 
1344  FTPSetMpmState();
1345 
1346 #ifdef UNITTESTS
1348 #endif
1349 }
1350 
1351 /*
1352  * \brief Returns the ending offset of the next line from a multi-line buffer.
1353  *
1354  * "Buffer" refers to a FTP response in a single buffer containing multiple lines.
1355  * Here, "next line" is defined as terminating on
1356  * - Newline character
1357  * - Null character
1358  *
1359  * \param buffer Contains zero or more characters.
1360  * \param len Size, in bytes, of buffer.
1361  *
1362  * \retval Offset from the start of buffer indicating the where the
1363  * next "line ends". The characters between the input buffer and this
1364  * value comprise the line.
1365  *
1366  * NULL is found first or a newline isn't found, then UINT16_MAX is returned.
1367  */
1368 uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
1369 {
1370  if (!buffer || *buffer == '\0') {
1371  return UINT16_MAX;
1372  }
1373 
1374  char *c = strchr(buffer, '\n');
1375  return c == NULL ? len : (uint16_t)(c - buffer + 1);
1376 }
1377 
1378 bool EveFTPDataAddMetadata(void *vtx, SCJsonBuilder *jb)
1379 {
1380  const FtpDataState *ftp_state = (FtpDataState *)vtx;
1381  SCJbOpenObject(jb, "ftp_data");
1382 
1383  if (ftp_state->file_name) {
1384  SCJbSetStringFromBytes(jb, "filename", ftp_state->file_name, ftp_state->file_len);
1385  }
1386  switch (ftp_state->command) {
1387  case FTP_COMMAND_STOR:
1388  JB_SET_STRING(jb, "command", "STOR");
1389  break;
1390  case FTP_COMMAND_RETR:
1391  JB_SET_STRING(jb, "command", "RETR");
1392  break;
1393  default:
1394  break;
1395  }
1396  SCJbClose(jb);
1397  return true;
1398 }
1399 
1400 /**
1401  * \brief Free memory allocated for global FTP parser state.
1402  */
1404 {
1405  FTPFreeMpmState();
1406 }
1407 
1408 /* UNITTESTS */
1409 #ifdef UNITTESTS
1410 #include "stream-tcp.h"
1411 
1412 /** \test Send a get request in one chunk. */
1413 static int FTPParserTest01(void)
1414 {
1415  Flow f;
1416  uint8_t ftpbuf[] = "PORT 192,168,1,1,0,80\r\n";
1417  uint32_t ftplen = sizeof(ftpbuf) - 1; /* minus the \0 */
1418  TcpSession ssn;
1420 
1421  memset(&f, 0, sizeof(f));
1422  memset(&ssn, 0, sizeof(ssn));
1423 
1424  f.protoctx = (void *)&ssn;
1425  f.proto = IPPROTO_TCP;
1426  f.alproto = ALPROTO_FTP;
1427 
1428  StreamTcpInitConfig(true);
1429 
1430  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1431  STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1432  FAIL_IF(r != 0);
1433 
1434  FtpState *ftp_state = f.alstate;
1435  FAIL_IF_NULL(ftp_state);
1436  FAIL_IF(ftp_state->command != FTP_COMMAND_PORT);
1437 
1439  StreamTcpFreeConfig(true);
1440  PASS;
1441 }
1442 
1443 /** \test Supply RETR without a filename */
1444 static int FTPParserTest11(void)
1445 {
1446  Flow f;
1447  uint8_t ftpbuf1[] = "PORT 192,168,1,1,0,80\r\n";
1448  uint8_t ftpbuf2[] = "RETR\r\n";
1449  uint8_t ftpbuf3[] = "227 OK\r\n";
1450  TcpSession ssn;
1451 
1453 
1454  memset(&f, 0, sizeof(f));
1455  memset(&ssn, 0, sizeof(ssn));
1456 
1457  f.protoctx = (void *)&ssn;
1458  f.proto = IPPROTO_TCP;
1459  f.alproto = ALPROTO_FTP;
1460 
1461  StreamTcpInitConfig(true);
1462 
1463  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1464  STREAM_TOSERVER | STREAM_START, ftpbuf1,
1465  sizeof(ftpbuf1) - 1);
1466  FAIL_IF(r != 0);
1467 
1468  /* Response */
1469  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1470  STREAM_TOCLIENT,
1471  ftpbuf3,
1472  sizeof(ftpbuf3) - 1);
1473  FAIL_IF(r != 0);
1474 
1475  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1476  STREAM_TOSERVER, ftpbuf2,
1477  sizeof(ftpbuf2) - 1);
1478  FAIL_IF(r == 0);
1479 
1480  FtpState *ftp_state = f.alstate;
1481  FAIL_IF_NULL(ftp_state);
1482 
1483  FAIL_IF(ftp_state->command != FTP_COMMAND_RETR);
1484 
1486  StreamTcpFreeConfig(true);
1487  PASS;
1488 }
1489 
1490 /** \test Supply STOR without a filename */
1491 static int FTPParserTest12(void)
1492 {
1493  Flow f;
1494  uint8_t ftpbuf1[] = "PORT 192,168,1,1,0,80\r\n";
1495  uint8_t ftpbuf2[] = "STOR\r\n";
1496  uint8_t ftpbuf3[] = "227 OK\r\n";
1497  TcpSession ssn;
1498 
1500 
1501  memset(&f, 0, sizeof(f));
1502  memset(&ssn, 0, sizeof(ssn));
1503 
1504  f.protoctx = (void *)&ssn;
1505  f.proto = IPPROTO_TCP;
1506  f.alproto = ALPROTO_FTP;
1507 
1508  StreamTcpInitConfig(true);
1509 
1510  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1511  STREAM_TOSERVER | STREAM_START, ftpbuf1,
1512  sizeof(ftpbuf1) - 1);
1513  FAIL_IF(r != 0);
1514 
1515  /* Response */
1516  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1517  STREAM_TOCLIENT,
1518  ftpbuf3,
1519  sizeof(ftpbuf3) - 1);
1520  FAIL_IF(r != 0);
1521 
1522  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_FTP,
1523  STREAM_TOSERVER, ftpbuf2,
1524  sizeof(ftpbuf2) - 1);
1525  FAIL_IF(r == 0);
1526 
1527  FtpState *ftp_state = f.alstate;
1528  FAIL_IF_NULL(ftp_state);
1529 
1530  FAIL_IF(ftp_state->command != FTP_COMMAND_STOR);
1531 
1533  StreamTcpFreeConfig(true);
1534  PASS;
1535 }
1536 #endif /* UNITTESTS */
1537 
1539 {
1540 #ifdef UNITTESTS
1541  UtRegisterTest("FTPParserTest01", FTPParserTest01);
1542  UtRegisterTest("FTPParserTest11", FTPParserTest11);
1543  UtRegisterTest("FTPParserTest12", FTPParserTest12);
1544 #endif /* UNITTESTS */
1545 }
1546 
MpmInitThreadCtx
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
Definition: util-mpm.c:195
FTPTransaction_::request_truncated
bool request_truncated
Definition: app-layer-ftp.h:69
PmqReset
void PmqReset(PrefilterRuleStore *pmq)
Reset a Pmq for reusage. Meant to be called after a single search.
Definition: util-prefilter.c:102
AppLayerParserRegisterGetStateProgressFunc
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
Definition: app-layer-parser.c:499
AppLayerProtoDetectPPParseConfPorts
int AppLayerProtoDetectPPParseConfPorts(const char *ipproto_name, uint8_t ipproto, const char *alproto_name, AppProto alproto, uint16_t min_depth, uint16_t max_depth, ProbingParserFPtr ProbingParserTs, ProbingParserFPtr ProbingParserTc)
Definition: app-layer-detect-proto.c:1567
len
uint8_t len
Definition: app-layer-dnp3.h:2
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
FTPTransaction_::request
uint8_t * request
Definition: app-layer-ftp.h:68
AppLayerGetTxIterState::ptr
void * ptr
Definition: app-layer-parser.h:123
AppLayerProtoDetectPMRegisterPatternCI
int AppLayerProtoDetectPMRegisterPatternCI(uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
Registers a case-insensitive pattern for protocol detection.
Definition: app-layer-detect-proto.c:1668
FTPThreadCtx_
Definition: app-layer-ftp.c:41
StreamingBufferConfig_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:66
AppLayerParserRegisterLocalStorageFunc
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
Definition: app-layer-parser.c:459
TAILQ_INIT
#define TAILQ_INIT(head)
Definition: queue.h:262
FtpState_::active
bool active
Definition: app-layer-ftp.h:88
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:314
FileContainerAlloc
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition: util-file.c:497
FtpDataState_::state
uint8_t state
Definition: app-layer-ftp.h:116
StreamingBufferConfig_::Calloc
void *(* Calloc)(size_t n, size_t size)
Definition: util-streaming-buffer.h:69
MpmThreadCtx_
Definition: util-mpm.h:46
stream-tcp.h
FtpState
struct FtpState_ FtpState
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
AppLayerRequestProtocolTLSUpgrade
bool AppLayerRequestProtocolTLSUpgrade(Flow *f)
request applayer to wrap up this protocol and rerun protocol detection with expectation of TLS....
Definition: app-layer-detect-proto.c:1854
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
EveFTPDataAddMetadata
bool EveFTPDataAddMetadata(void *vtx, SCJsonBuilder *jb)
Definition: app-layer-ftp.c:1378
AppLayerExpectationGetFlowId
FlowStorageId AppLayerExpectationGetFlowId(void)
Definition: app-layer-expectation.c:288
PrefilterRuleStore_
structure for storing potential rule matches
Definition: util-prefilter.h:34
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FtpCommandInfo_
Definition: app-layer-ftp.h:55
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
Flow_::proto
uint8_t proto
Definition: flow.h:378
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
FtpState_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:97
AppLayerParserConfParserEnabled
int AppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
check if a parser is enabled in the config Returns enabled always if: were running unittests
Definition: app-layer-parser.c:343
ALPROTO_POP3
@ ALPROTO_POP3
Definition: app-layer-protos.h:71
STREAMING_BUFFER_CONFIG_INITIALIZER
#define STREAMING_BUFFER_CONFIG_INITIALIZER
Definition: util-streaming-buffer.h:74
FileContainerFree
void FileContainerFree(FileContainer *ffc, const StreamingBufferConfig *cfg)
Free a FileContainer.
Definition: util-file.c:533
Flow_
Flow data structure.
Definition: flow.h:356
FTPTransaction_::done
bool done
Definition: app-layer-ftp.h:75
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:332
AppLayerParserRegisterStateProgressCompletionStatus
void AppLayerParserRegisterStateProgressCompletionStatus(AppProto alproto, const int ts, const int tc)
Definition: app-layer-parser.c:547
AppLayerParserRegisterParserAcceptableDataDirection
void AppLayerParserRegisterParserAcceptableDataDirection(uint8_t ipproto, AppProto alproto, uint8_t direction)
Definition: app-layer-parser.c:413
FtpState_::current_line_truncated_tc
bool current_line_truncated_tc
Definition: app-layer-ftp.h:95
AppLayerParserRegisterTxFreeFunc
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
Definition: app-layer-parser.c:509
FTPTransaction_::tx_data
AppLayerTxData tx_data
Definition: app-layer-ftp.h:64
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
FtpDataState_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:115
FTPMemuseGlobalCounter
uint64_t FTPMemuseGlobalCounter(void)
Definition: app-layer-ftp.c:77
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:322
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
rust.h
MIN
#define MIN(x, y)
Definition: suricata-common.h:400
FtpLineState_::buf
const uint8_t * buf
Definition: app-layer-ftp.h:38
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:37
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
FtpDataState_::file_len
int16_t file_len
Definition: app-layer-ftp.h:114
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
FTPResponseWrapper_
Definition: app-layer-ftp.h:44
app-layer-ftp.h
APP_LAYER_PARSER_EOF_TS
#define APP_LAYER_PARSER_EOF_TS
Definition: app-layer-parser.h:39
Flow_::protoctx
void * protoctx
Definition: flow.h:441
FtpDataState_::direction
uint8_t direction
Definition: app-layer-ftp.h:117
FtpInput_::len
int32_t len
Definition: app-layer-ftp.c:263
SC_ELIMIT
@ SC_ELIMIT
Definition: util-error.h:31
SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, ftp_memuse)
SC_ENOMEM
@ SC_ENOMEM
Definition: util-error.h:29
FTPParserCleanup
void FTPParserCleanup(void)
Free memory allocated for global FTP parser state.
Definition: app-layer-ftp.c:1403
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:209
AppLayerProtoDetectPPRegister
void AppLayerProtoDetectPPRegister(uint8_t ipproto, const char *portstr, AppProto alproto, uint16_t min_depth, uint16_t max_depth, uint8_t direction, ProbingParserFPtr ProbingParser1, ProbingParserFPtr ProbingParser2)
register parser at a port
Definition: app-layer-detect-proto.c:1527
ftp_max_line_len
uint32_t ftp_max_line_len
Definition: app-layer-ftp.c:52
FtpState_
Definition: app-layer-ftp.h:87
app-layer-expectation.h
app-layer-detect-proto.h
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:488
APP_LAYER_INCOMPLETE
#define APP_LAYER_INCOMPLETE(c, n)
Definition: app-layer-parser.h:75
TAILQ_REMOVE
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:312
MpmDestroyThreadCtx
void MpmDestroyThreadCtx(MpmThreadCtx *mpm_thread_ctx, const uint16_t matcher)
Definition: util-mpm.c:202
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:26
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
FtpState_::port_line
uint8_t * port_line
Definition: app-layer-ftp.h:101
AppLayerParserState_
Definition: app-layer-parser.c:133
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
FTPThreadCtx_::pmq
PrefilterRuleStore * pmq
Definition: app-layer-ftp.c:43
SC_FILENAME_MAX
#define SC_FILENAME_MAX
Definition: util-file.h:62
FtpState_::curr_tx
FTPTransaction * curr_tx
Definition: app-layer-ftp.h:90
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
FtpDataState_::state_data
AppLayerStateData state_data
Definition: app-layer-ftp.h:119
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:22
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:490
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
AppLayerParserRegisterStateFuncs
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void *, AppProto), void(*StateFree)(void *))
Definition: app-layer-parser.c:434
AppLayerProtoDetectPMRegisterPatternCSwPP
int AppLayerProtoDetectPMRegisterPatternCSwPP(uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction, ProbingParserFPtr PPFunc, uint16_t pp_min_depth, uint16_t pp_max_depth)
Definition: app-layer-detect-proto.c:1654
FtpLineState_::lf_found
bool lf_found
Definition: app-layer-ftp.h:41
app-layer-parser.h
Flow_::todstbytecnt
uint64_t todstbytecnt
Definition: flow.h:497
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:309
AppLayerParserRegisterGetEventInfo
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, uint8_t *event_id, AppLayerEventType *event_type))
Definition: app-layer-parser.c:594
FtpState_::state_data
AppLayerStateData state_data
Definition: app-layer-ftp.h:105
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:341
FTPParserRegisterTests
void FTPParserRegisterTests(void)
Definition: app-layer-ftp.c:1538
RegisterFTPParsers
void RegisterFTPParsers(void)
Definition: app-layer-ftp.c:1269
FTPTransaction_::command_descriptor
FtpCommandInfo command_descriptor
Definition: app-layer-ftp.h:72
AppLayerParserRegisterProtocolUnittests
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
Definition: app-layer-parser.c:1849
AppLayerExpectationCreate
int AppLayerExpectationCreate(Flow *f, int direction, Port src, Port dst, AppProto alproto, void *data)
Definition: app-layer-expectation.c:219
AppLayerGetTxIterState
Definition: app-layer-parser.h:121
FtpCommandInfo_::command_code
FtpRequestCommand command_code
Definition: app-layer-ftp.h:57
APP_LAYER_PARSER_EOF_TC
#define APP_LAYER_PARSER_EOF_TC
Definition: app-layer-parser.h:40
ftp_config_memcap
uint64_t ftp_config_memcap
Definition: app-layer-ftp.c:50
AppLayerRegisterExpectationProto
void AppLayerRegisterExpectationProto(uint8_t proto, AppProto alproto)
Definition: app-layer-detect-proto.c:2140
FTPResponseWrapper_::response
FTPResponseLine * response
Definition: app-layer-ftp.h:45
FileOpenFileWithId
int FileOpenFileWithId(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id, const uint8_t *name, uint16_t name_len, const uint8_t *data, uint32_t data_len, uint16_t flags)
Open a new File.
Definition: util-file.c:984
AppLayerParserRegisterGetTxFilesFunc
void AppLayerParserRegisterGetTxFilesFunc(uint8_t ipproto, AppProto alproto, AppLayerGetFileState(*GetTxFiles)(void *, uint8_t))
Definition: app-layer-parser.c:471
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1763
FtpDataState
struct FtpDataState_ FtpDataState
FileAppendData
int FileAppendData(FileContainer *ffc, const StreamingBufferConfig *sbcfg, const uint8_t *data, uint32_t data_len)
Store/handle a chunk of file data in the File structure The last file in the FileContainer will be us...
Definition: util-file.c:783
FTPMemcapGlobalCounter
uint64_t FTPMemcapGlobalCounter(void)
Definition: app-layer-ftp.c:83
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:177
FtpInput
struct FtpInput_ FtpInput
FtpDataState_::tx_data
AppLayerTxData tx_data
Definition: app-layer-ftp.h:118
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
FlowGetStorageById
void * FlowGetStorageById(const Flow *f, FlowStorageId id)
Definition: flow-storage.c:40
AppLayerParserRegisterParser
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
Definition: app-layer-parser.c:401
FtpInput_::orig_len
int32_t orig_len
Definition: app-layer-ftp.c:264
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:295
AppLayerParserRegisterGetTx
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
Definition: app-layer-parser.c:529
APP_LAYER_OK
#define APP_LAYER_OK
Definition: app-layer-parser.h:63
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1466
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
SCReturnStruct
#define SCReturnStruct(x)
Definition: util-debug.h:291
FTPTransaction_::request_length
uint32_t request_length
Definition: app-layer-ftp.h:67
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
util-mpm.h
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool quiet)
Definition: stream-tcp.c:859
flags
uint8_t flags
Definition: decode-gre.h:0
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1300
FtpLineState_
Definition: app-layer-ftp.h:35
suricata-common.h
ftp_config_maxtx
uint32_t ftp_config_maxtx
Definition: app-layer-ftp.c:51
AppLayerDecoderEventsSetEventRaw
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
Definition: app-layer-events.c:94
FtpState_::port_line_len
uint32_t port_line_len
Definition: app-layer-ftp.h:99
FtpState_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:103
TAILQ_NEXT
#define TAILQ_NEXT(elm, field)
Definition: queue.h:307
ALPROTO_FTPDATA
@ ALPROTO_FTPDATA
Definition: app-layer-protos.h:53
AppLayerParserRegisterStateDataFunc
void AppLayerParserRegisterStateDataFunc(uint8_t ipproto, AppProto alproto, AppLayerStateData *(*GetStateData)(void *state))
Definition: app-layer-parser.c:615
AppLayerParserRegisterTxDataFunc
void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto, AppLayerTxData *(*GetTxData)(void *tx))
Definition: app-layer-parser.c:605
Flow_::parent_id
int64_t parent_id
Definition: flow.h:430
FtpDataState_
Definition: app-layer-ftp.h:109
util-validate.h
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
FTPSetMemcap
int FTPSetMemcap(uint64_t size)
Definition: app-layer-ftp.c:89
FtpLineState_::delim_len
uint8_t delim_len
Definition: app-layer-ftp.h:40
AppLayerParserRegisterGetTxIterator
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
Definition: app-layer-parser.c:539
MpmTableElmt_::Prepare
int(* Prepare)(MpmConfig *, struct MpmCtx_ *)
Definition: util-mpm.h:174
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:154
FlowFreeStorageById
void FlowFreeStorageById(Flow *f, FlowStorageId id)
Definition: flow-storage.c:55
FileCloseFile
int FileCloseFile(FileContainer *ffc, const StreamingBufferConfig *sbcfg, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition: util-file.c:1079
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Flow_::alproto_ts
AppProto alproto_ts
Definition: flow.h:451
Flow_::alstate
void * alstate
Definition: flow.h:479
StreamingBufferConfig_::Free
void(* Free)(void *ptr, size_t size)
Definition: util-streaming-buffer.h:71
FtpCommandInfo_::command_index
uint8_t command_index
Definition: app-layer-ftp.h:56
FtpState_::tx_cnt
uint64_t tx_cnt
Definition: app-layer-ftp.h:92
FtpState_::current_line_truncated_ts
bool current_line_truncated_ts
Definition: app-layer-ftp.h:94
sc_errno
thread_local SCError sc_errno
Definition: util-error.c:31
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:33
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
FtpLineState_::len
uint32_t len
Definition: app-layer-ftp.h:39
AppLayerParserRegisterGetTxCnt
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
Definition: app-layer-parser.c:519
APP_LAYER_ERROR
#define APP_LAYER_ERROR
Definition: app-layer-parser.h:67
FtpState_::port_line_size
uint32_t port_line_size
Definition: app-layer-ftp.h:100
PmqFree
void PmqFree(PrefilterRuleStore *pmq)
Cleanup and free a Pmq.
Definition: util-prefilter.c:126
FTP_MPM
#define FTP_MPM
Definition: app-layer-ftp.c:46
FTPTransaction_::tx_id
uint64_t tx_id
Definition: app-layer-ftp.h:62
FTPTransaction_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:74
FTPTransaction_::active
bool active
Definition: app-layer-ftp.h:76
AppLayerParserRegisterGetEventInfoById
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(uint8_t event_id, const char **event_name, AppLayerEventType *event_type))
Definition: app-layer-parser.c:562
FtpDataState_::files
FileContainer * files
Definition: app-layer-ftp.h:112
likely
#define likely(expr)
Definition: util-optimize.h:32
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:58
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
MpmCtx_
Definition: util-mpm.h:93
TcpSession_
Definition: stream-tcp-private.h:283
JsonGetNextLineFromBuffer
uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
Definition: app-layer-ftp.c:1368
util-misc.h
FileFlowFlagsToFlags
uint16_t FileFlowFlagsToFlags(const uint16_t flow_file_flags, uint8_t direction)
Definition: util-file.c:233
FTPTransaction_
Definition: app-layer-ftp.h:60
FTPThreadCtx
struct FTPThreadCtx_ FTPThreadCtx
AppLayerParserStateIssetFlag
uint16_t AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag)
Definition: app-layer-parser.c:1838
Flow_::alproto_tc
AppProto alproto_tc
Definition: flow.h:452
FtpInput_::buf
const uint8_t * buf
Definition: app-layer-ftp.c:261
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
SCMemcmp
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:290
AppLayerProtoDetectConfProtoDetectionEnabled
int AppLayerProtoDetectConfProtoDetectionEnabled(const char *ipproto, const char *alproto)
Given a protocol name, checks if proto detection is enabled in the conf file.
Definition: app-layer-detect-proto.c:1964
SCMutex
#define SCMutex
Definition: threads-debug.h:114
FTPThreadCtx_::ftp_mpm_thread_ctx
MpmThreadCtx * ftp_mpm_thread_ctx
Definition: app-layer-ftp.c:42
SCLogDebugEnabled
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
Definition: util-debug.c:767
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
FtpInput_::consumed
int32_t consumed
Definition: app-layer-ftp.c:262
PmqSetup
int PmqSetup(PrefilterRuleStore *pmq)
Setup a pmq.
Definition: util-prefilter.c:37
StreamingBufferConfig_::Realloc
void *(* Realloc)(void *ptr, size_t orig_size, size_t size)
Definition: util-streaming-buffer.h:70
FtpDataState_::file_name
uint8_t * file_name
Definition: app-layer-ftp.h:111
FtpInput_
Definition: app-layer-ftp.c:260
AppLayerGetTxIterState::un
union AppLayerGetTxIterState::@7 un
app-layer.h
PrefilterRuleStore_::rule_id_array
SigIntId * rule_id_array
Definition: util-prefilter.h:38