OpenVAS Scanner 23.40.3
ftp_funcs.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 1998 Renaud Deraison
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "ftp_funcs.h"
8
9#include "network.h"
10
11#include <netinet/in.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16int
17ftp_log_in (int soc, char *username, char *passwd)
18{
19 char buf[1024];
20 int n;
21 int counter;
22
23 buf[sizeof (buf) - 1] = '\0';
24 n = recv_line (soc, buf, sizeof (buf) - 1);
25 if (n <= 0)
26 return (1);
27
28 if (strncmp (buf, "220", 3) != 0)
29 {
30 return 1;
31 }
32
33 counter = 0;
34 while (buf[3] == '-' && n > 0 && counter < 1024)
35 {
36 n = recv_line (soc, buf, sizeof (buf) - 1);
37 counter++;
38 }
39
40 if (counter >= 1024)
41 return 1; /* Rogue FTP server */
42
43 if (n <= 0)
44 return 1;
45
46 snprintf (buf, sizeof (buf), "USER %s\r\n", username);
47 write_stream_connection (soc, buf, strlen (buf));
48 n = recv_line (soc, buf, sizeof (buf) - 1);
49 if (n <= 0)
50 return 1;
51 if (strncmp (buf, "230", 3) == 0)
52 {
53 counter = 0;
54 while (buf[3] == '-' && n > 0 && counter < 1024)
55 {
56 n = recv_line (soc, buf, sizeof (buf) - 1);
57 counter++;
58 }
59 return 0;
60 }
61
62 if (strncmp (buf, "331", 3) != 0)
63 {
64 return 1;
65 }
66
67 counter = 0;
68 n = 1;
69 while (buf[3] == '-' && n > 0 && counter < 1024)
70 {
71 n = recv_line (soc, buf, sizeof (buf) - 1);
72 counter++;
73 }
74
75 if (counter >= 1024)
76 return 1;
77
78 snprintf (buf, sizeof (buf), "PASS %s\r\n", passwd);
79 write_stream_connection (soc, buf, strlen (buf));
80 n = recv_line (soc, buf, sizeof (buf) - 1);
81 if (n <= 0)
82 return 1;
83
84 if (strncmp (buf, "230", 3) != 0)
85 {
86 return 1;
87 }
88
89 counter = 0;
90 n = 1;
91 while (buf[3] == '-' && n > 0 && counter < 1024)
92 {
93 n = recv_line (soc, buf, sizeof (buf) - 1);
94 counter++;
95 }
96
97 return 0;
98}
99
100int
101ftp_get_pasv_address (int soc, struct sockaddr_in *addr)
102{
103 char buf[512];
104 char *t, *s;
105 unsigned char l[6];
106 uint32_t *a;
107 unsigned short *p;
108
109 snprintf (buf, 7, "PASV\r\n");
110 write_stream_connection (soc, buf, strlen (buf));
111 bzero (buf, sizeof (buf));
112 bzero (addr, sizeof (struct sockaddr_in));
113 if (recv_line (soc, buf, sizeof (buf) - 1) < 0)
114 return 1;
115
116 if (strncmp (buf, "227", 3) != 0)
117 return 1;
118
119 t = strchr (buf, '(');
120 if (t == NULL)
121 return 1;
122 t++;
123 s = strchr (t, ',');
124 if (s == NULL)
125 return 1;
126
127 s[0] = '\0';
128
129 l[0] = (unsigned char) atoi (t);
130 s++;
131 t = strchr (s, ',');
132 if (t == NULL)
133 return 1;
134 t[0] = 0;
135 l[1] = (unsigned char) atoi (s);
136 t++;
137 s = strchr (t, ',');
138 if (s == NULL)
139 return 1;
140 s[0] = 0;
141 l[2] = (unsigned char) atoi (t);
142 s++;
143 t = strchr (s, ',');
144 if (t == NULL)
145 return 1;
146 t[0] = 0;
147 l[3] = (unsigned char) atoi (s);
148 t++;
149 s = strchr (t, ',');
150 if (s == NULL)
151 return 1;
152 s[0] = 0;
153 l[4] = (unsigned char) atoi (t);
154 s++;
155 t = strchr (s, ')');
156 if (t == NULL)
157 return 1;
158 t[0] = 0;
159 l[5] = (unsigned char) atoi (s);
160 a = (uint32_t *) l;
161 p = (unsigned short *) (l + 4);
162
163 addr->sin_addr.s_addr = *a;
164 addr->sin_port = *p;
165 addr->sin_family = AF_INET;
166 return 0;
167}
int ftp_get_pasv_address(int soc, struct sockaddr_in *addr)
Definition ftp_funcs.c:101
int ftp_log_in(int soc, char *username, char *passwd)
Definition ftp_funcs.c:17
Header file for module ftp_funcs.
static uint32 counter
Definition genrand.c:48
int write_stream_connection(int fd, void *buf0, int n)
Definition network.c:1583
int recv_line(int soc, char *buf, size_t bufsiz)
Reads a text from the socket stream into the argument buffer, always.
Definition network.c:2040
Header file for module network.