| 1 |
/* |
| 2 |
|
| 3 |
libftp 1.0 (stable): this is an FTP library to simplify the work to a Developer |
| 4 |
who want to work with FTP servers (RFC 959). |
| 5 |
Copyright (C) 2007/2008 omnipresent - omnipresent[at]email.it |
| 6 |
|
| 7 |
This program is free software: you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation, either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
#include "ftp_main.h" |
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
/* Path of the Current Work Directory */ |
| 25 |
char path[1050]={'\0'}; |
| 26 |
|
| 27 |
/* |
| 28 |
* |
| 29 |
* strfnbytes: look for needle into big_str for n bytes |
| 30 |
* char *big_str: big string (where to look for needle) |
| 31 |
* char *needle: string that you need |
| 32 |
* int bytes: first n bytes |
| 33 |
* |
| 34 |
*/ |
| 35 |
|
| 36 |
|
| 37 |
int strfnbytes(char *big_str, char *needle, int bytes) { |
| 38 |
|
| 39 |
int i=0; |
| 40 |
int same=0; |
| 41 |
|
| 42 |
if (strlen(big_str)-1 < bytes) { |
| 43 |
printf("[-] Big String MUST BE major than %d\n", bytes); |
| 44 |
printf("Aborting...\n"); |
| 45 |
return (-1); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
if (strlen(needle)-1 >= bytes) { |
| 50 |
printf("[-] needle MUST BE the same as n bytes\n"); |
| 51 |
printf("Aborting...\n"); |
| 52 |
return (-1); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
while (big_str[i] != '\0') { |
| 57 |
|
| 58 |
if (i >= bytes) |
| 59 |
break; |
| 60 |
|
| 61 |
if (needle[i] == big_str[i]) { |
| 62 |
|
| 63 |
same++; |
| 64 |
} else { |
| 65 |
|
| 66 |
same=0; |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
i++; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
if (same != 0) |
| 75 |
return 0; |
| 76 |
else |
| 77 |
return -1; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* |
| 83 |
* find_pasv: find the ports in the PASV message reply |
| 84 |
* char *StrPasv: the reply string to working on it |
| 85 |
* int verbose: verbose mode |
| 86 |
* - 1 Yes |
| 87 |
* - 0 No |
| 88 |
* |
| 89 |
*/ |
| 90 |
|
| 91 |
int find_pasv (char *StrPasv, int verbose) |
| 92 |
{ |
| 93 |
char p1[100]; |
| 94 |
char p2[100]; |
| 95 |
|
| 96 |
int port1 = 0; |
| 97 |
int port2 = 0; |
| 98 |
int count = 0; |
| 99 |
int i = 0; |
| 100 |
int temp = 0; |
| 101 |
int tmp = 0; |
| 102 |
int sum = 0; |
| 103 |
|
| 104 |
extern int data_port1; |
| 105 |
extern int data_port2; |
| 106 |
|
| 107 |
|
| 108 |
int len = strlen(StrPasv); |
| 109 |
|
| 110 |
len = len-2; |
| 111 |
StrPasv[len] = '\0'; |
| 112 |
|
| 113 |
|
| 114 |
if (strstr(StrPasv, ")") == NULL) { //mmh an error occur.. |
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
while (StrPasv[i] != '\0') { |
| 119 |
if (StrPasv[i] == ')') { |
| 120 |
break; |
| 121 |
} |
| 122 |
|
| 123 |
i++; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
temp = i; |
| 128 |
i = 0; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
data_port1 = 0; |
| 134 |
data_port2 = 0; |
| 135 |
|
| 136 |
count = temp; |
| 137 |
|
| 138 |
count = count - 1; |
| 139 |
|
| 140 |
memset(p1, 0x0, 100); |
| 141 |
memset(p2, 0x0, 100); |
| 142 |
|
| 143 |
if (verbose == 1) { |
| 144 |
printf("--[ Server: %s\n", StrPasv); |
| 145 |
} |
| 146 |
|
| 147 |
while (StrPasv[count] != ',') { |
| 148 |
|
| 149 |
if (i >= 100) |
| 150 |
return -1; |
| 151 |
|
| 152 |
p1[i] = StrPasv[count]; |
| 153 |
|
| 154 |
count--; |
| 155 |
i++; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
p1[i] = '\0'; |
| 160 |
|
| 161 |
|
| 162 |
count--; |
| 163 |
i = 0; |
| 164 |
|
| 165 |
while (StrPasv[count] != ',') { |
| 166 |
|
| 167 |
p2[i] = StrPasv[count]; |
| 168 |
|
| 169 |
|
| 170 |
count--; |
| 171 |
i++; |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
p2[i] = '\0'; |
| 176 |
|
| 177 |
i = 0; |
| 178 |
count = 0; |
| 179 |
tmp = 0; |
| 180 |
sum = 0; |
| 181 |
|
| 182 |
while (p1[i] != '\0') { |
| 183 |
|
| 184 |
tmp = 0; |
| 185 |
sum = 0; |
| 186 |
|
| 187 |
if (i == 1) { |
| 188 |
|
| 189 |
tmp = (int)p1[i] - 48; |
| 190 |
sum = 10*tmp; |
| 191 |
|
| 192 |
port1 = port1 + sum; |
| 193 |
|
| 194 |
} else if (i == 2) { |
| 195 |
|
| 196 |
tmp = (int)p1[i] - 48; |
| 197 |
|
| 198 |
sum = 100*tmp; |
| 199 |
|
| 200 |
port1 = port1 + sum; |
| 201 |
|
| 202 |
} else if (i == 0) { |
| 203 |
|
| 204 |
tmp = (int)p1[i] - 48; |
| 205 |
|
| 206 |
port1 = port1 + tmp; |
| 207 |
|
| 208 |
} else if ( i > 2) { |
| 209 |
|
| 210 |
return -1; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
i++; |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
i = 0; |
| 219 |
count = 0; |
| 220 |
tmp = 0; |
| 221 |
sum = 0; |
| 222 |
|
| 223 |
while (p2[i] != '\0') { |
| 224 |
|
| 225 |
tmp = 0; |
| 226 |
sum = 0; |
| 227 |
|
| 228 |
if (i == 1) { |
| 229 |
|
| 230 |
tmp = (int)p2[i] - 48; |
| 231 |
sum = 10*tmp; |
| 232 |
|
| 233 |
port2 = port2 + sum; |
| 234 |
|
| 235 |
} else if (i == 2) { |
| 236 |
|
| 237 |
tmp = (int)p2[i] - 48; |
| 238 |
|
| 239 |
sum = 100*tmp; |
| 240 |
|
| 241 |
port2 = port2 + sum; |
| 242 |
|
| 243 |
} else if (i == 0) { |
| 244 |
|
| 245 |
tmp = (int)p2[i] - 48; |
| 246 |
|
| 247 |
port2 = port2 + tmp; |
| 248 |
|
| 249 |
} else if ( i > 2) { |
| 250 |
|
| 251 |
return -1; |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
i++; |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
data_port2 = port1; |
| 260 |
data_port1 = port2; |
| 261 |
|
| 262 |
|
| 263 |
return 0; |
| 264 |
|
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
/* |
| 269 |
* |
| 270 |
* ftp_DataPort: Make the right Data Port for transfer connections |
| 271 |
* int PortOne: First port in the PASV reply |
| 272 |
* int PortTwo: Second port in the PASV reply |
| 273 |
* |
| 274 |
*/ |
| 275 |
|
| 276 |
int ftp_DataPort (int PortOne, int PortTwo) { |
| 277 |
|
| 278 |
/*** HOWTO Data Transfer Connection: |
| 279 |
((port1 * 256) + port2) |
| 280 |
***/ |
| 281 |
|
| 282 |
extern int data_port; |
| 283 |
|
| 284 |
data_port = 0; |
| 285 |
|
| 286 |
data_port = PortOne * 256; |
| 287 |
data_port = data_port + PortTwo; |
| 288 |
|
| 289 |
return data_port; |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
* |
| 295 |
* ftp_path: Reply with the Current Directory |
| 296 |
* char *PathCurDir: Reply of the FTPD from the PWD command |
| 297 |
* |
| 298 |
*/ |
| 299 |
|
| 300 |
int ftp_path (char *PathCurDir) { |
| 301 |
|
| 302 |
int i = 0; |
| 303 |
int count = 0; |
| 304 |
|
| 305 |
if (strlen(PathCurDir) > 1024) |
| 306 |
return -1; |
| 307 |
|
| 308 |
while (PathCurDir[i] != '"') { |
| 309 |
|
| 310 |
if (i >= 1024) |
| 311 |
return -1; |
| 312 |
i++; |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
i++; |
| 317 |
|
| 318 |
while (PathCurDir[i] != '"') { |
| 319 |
|
| 320 |
if (i >= 1024) { |
| 321 |
path[i] = '\0'; |
| 322 |
break; |
| 323 |
} |
| 324 |
|
| 325 |
path[count] = PathCurDir[i]; |
| 326 |
i++; |
| 327 |
count++; |
| 328 |
} |
| 329 |
|
| 330 |
if (i >= 1024) { |
| 331 |
path[i] = '\0'; |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
path[count] = '\0'; |
| 336 |
|
| 337 |
return 0; |
| 338 |
|
| 339 |
} |