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