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