OpenVAS Scanner  7.0.1~git
ftp_funcs.c
Go to the documentation of this file.
1 /* Portions Copyright (C) 2009-2019 Greenbone Networks GmbH
2  * Based on work Copyright (C) 1998 Renaud Deraison
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include "network.h"
22 
23 #include <netinet/in.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 int
29 ftp_log_in (int soc, char *username, char *passwd)
30 {
31  char buf[1024];
32  int n;
33  int counter;
34 
35  buf[sizeof (buf) - 1] = '\0';
36  n = recv_line (soc, buf, sizeof (buf) - 1);
37  if (n <= 0)
38  return 1;
39 
40  if (strncmp (buf, "220", 3) != 0)
41  {
42  return 1;
43  }
44 
45  counter = 0;
46  while (buf[3] == '-' && n > 0 && counter < 1024)
47  {
48  n = recv_line (soc, buf, sizeof (buf) - 1);
49  counter++;
50  }
51 
52  if (counter >= 1024)
53  return 1; /* Rogue FTP server */
54 
55  if (n <= 0)
56  return 1;
57 
58  snprintf (buf, sizeof (buf), "USER %s\r\n", username);
59  write_stream_connection (soc, buf, strlen (buf));
60  n = recv_line (soc, buf, sizeof (buf) - 1);
61  if (n <= 0)
62  return 1;
63  if (strncmp (buf, "230", 3) == 0)
64  {
65  counter = 0;
66  while (buf[3] == '-' && n > 0 && counter < 1024)
67  {
68  n = recv_line (soc, buf, sizeof (buf) - 1);
69  counter++;
70  }
71  return 0;
72  }
73 
74  if (strncmp (buf, "331", 3) != 0)
75  {
76  return 1;
77  }
78 
79  counter = 0;
80  n = 1;
81  while (buf[3] == '-' && n > 0 && counter < 1024)
82  {
83  n = recv_line (soc, buf, sizeof (buf) - 1);
84  counter++;
85  }
86 
87  if (counter >= 1024)
88  return 1;
89 
90  snprintf (buf, sizeof (buf), "PASS %s\r\n", passwd);
91  write_stream_connection (soc, buf, strlen (buf));
92  n = recv_line (soc, buf, sizeof (buf) - 1);
93  if (n <= 0)
94  return 1;
95 
96  if (strncmp (buf, "230", 3) != 0)
97  {
98  return 1;
99  }
100 
101  counter = 0;
102  n = 1;
103  while (buf[3] == '-' && n > 0 && counter < 1024)
104  {
105  n = recv_line (soc, buf, sizeof (buf) - 1);
106  counter++;
107  }
108 
109  return 0;
110 }
111 
112 int
113 ftp_get_pasv_address (int soc, struct sockaddr_in *addr)
114 {
115  char buf[512];
116  char *t, *s;
117  unsigned char l[6];
118  unsigned long *a;
119  unsigned short *p;
120 
121  snprintf (buf, 7, "PASV\r\n");
122  write_stream_connection (soc, buf, strlen (buf));
123  bzero (buf, sizeof (buf));
124  bzero (addr, sizeof (struct sockaddr_in));
125  if (recv_line (soc, buf, sizeof (buf) - 1) < 0)
126  return 1;
127 
128  if (strncmp (buf, "227", 3) != 0)
129  return 1;
130 
131  t = strchr (buf, '(');
132  if (t == NULL)
133  return 1;
134  t++;
135  s = strchr (t, ',');
136  if (s == NULL)
137  return 1;
138 
139  s[0] = '\0';
140 
141  l[0] = (unsigned char) atoi (t);
142  s++;
143  t = strchr (s, ',');
144  if (t == NULL)
145  return 1;
146  t[0] = 0;
147  l[1] = (unsigned char) atoi (s);
148  t++;
149  s = strchr (t, ',');
150  if (s == NULL)
151  return 1;
152  s[0] = 0;
153  l[2] = (unsigned char) atoi (t);
154  s++;
155  t = strchr (s, ',');
156  if (t == NULL)
157  return 1;
158  t[0] = 0;
159  l[3] = (unsigned char) atoi (s);
160  t++;
161  s = strchr (t, ',');
162  if (s == NULL)
163  return 1;
164  s[0] = 0;
165  l[4] = (unsigned char) atoi (t);
166  s++;
167  t = strchr (s, ')');
168  if (t == NULL)
169  return 1;
170  t[0] = 0;
171  l[5] = (unsigned char) atoi (s);
172  a = (unsigned long *) l;
173  p = (unsigned short *) (l + 4);
174 
175  addr->sin_addr.s_addr = *a;
176  addr->sin_port = *p;
177  addr->sin_family = AF_INET;
178  return 0;
179 }
recv_line
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:1846
ftp_get_pasv_address
int ftp_get_pasv_address(int soc, struct sockaddr_in *addr)
Definition: ftp_funcs.c:113
counter
static uint32 counter
Definition: genrand.c:62
ftp_log_in
int ftp_log_in(int soc, char *username, char *passwd)
Definition: ftp_funcs.c:29
network.h
Header file for module network.
write_stream_connection
int write_stream_connection(int fd, void *buf0, int n)
Definition: network.c:1396