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