| 1 |
hirohitohigashi |
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 |
|
|
|
| 22 |
|
|
#include <stdio.h> |
| 23 |
|
|
#include <errno.h> |
| 24 |
|
|
#include <stdlib.h> |
| 25 |
|
|
#include <unistd.h> |
| 26 |
|
|
#include <string.h> |
| 27 |
|
|
#include <sys/types.h> |
| 28 |
|
|
#include <sys/socket.h> |
| 29 |
|
|
#include <sys/stat.h> |
| 30 |
|
|
#include <netinet/in.h> |
| 31 |
|
|
#include <netdb.h> |
| 32 |
|
|
|
| 33 |
|
|
#include "ftp_main.h" |
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
#define CMD_OK "200" |
| 37 |
|
|
#define CMD_NI "202" |
| 38 |
|
|
#define SYS_STATUS "211" |
| 39 |
|
|
#define HLP_MSG "214" |
| 40 |
|
|
#define NAME_SYS "215" |
| 41 |
|
|
#define READY "220" |
| 42 |
|
|
#define CLOSE "221" |
| 43 |
|
|
#define NO_TRANSFER "225" |
| 44 |
|
|
#define CLOSE_CON "226" |
| 45 |
|
|
#define PASSIVE_MOD "227" |
| 46 |
|
|
#define PATH_DONE "257" |
| 47 |
|
|
#define LOGGED "230" |
| 48 |
|
|
#define REQ_FILE_OK "250" |
| 49 |
|
|
#define PATH_CREATED "257" |
| 50 |
|
|
#define USER_OK "331" |
| 51 |
|
|
#define NEED_ACCT "332" |
| 52 |
|
|
#define ACTION_PEND "350" //350 Requested file action pending further information. |
| 53 |
|
|
#define SERVICE_NA "421" |
| 54 |
|
|
#define NOT_OPEN_DT "425" |
| 55 |
|
|
#define CON_CLOSED "426" |
| 56 |
|
|
#define FILE_NOT_TAKEN "450" //File not available: eg busy |
| 57 |
|
|
#define ACT_ABORTED "451" |
| 58 |
|
|
#define ACT_NOT_TAKEN "452" |
| 59 |
|
|
#define SYNTAX_ERR "500" //Command unrecognized |
| 60 |
|
|
#define SYNTAX_ERR_P "501" //Syntax error in parameters or arguments |
| 61 |
|
|
#define CMD_NOT_IMPL "502" |
| 62 |
|
|
#define BAD_SEQ_CMD "503" |
| 63 |
|
|
#define NOT_LOGGED "530" |
| 64 |
|
|
#define ACCT_FOR_FILE "532" |
| 65 |
|
|
#define FILE_NOT_FOUND "550" //File not found, no access to file |
| 66 |
|
|
#define ACTION_ABORTED "551" |
| 67 |
|
|
#define ALLOCATION_ERR "552" |
| 68 |
|
|
#define FILE_ERR_NAME "553" |
| 69 |
|
|
|
| 70 |
|
|
/*** |
| 71 |
|
|
200 Command okay. |
| 72 |
|
|
202 Command not implemented, superfluous at this site. |
| 73 |
|
|
211 System status, or system help reply. |
| 74 |
|
|
212 Directory status. |
| 75 |
|
|
213 File status. |
| 76 |
|
|
214 Help message. |
| 77 |
|
|
On how to use the server or the meaning of a particular |
| 78 |
|
|
non-standard command. This reply is useful only to the |
| 79 |
|
|
human user. |
| 80 |
|
|
215 NAME system type. |
| 81 |
|
|
|
| 82 |
|
|
Where NAME is an official system name from the list in the |
| 83 |
|
|
Assigned Numbers document. |
| 84 |
|
|
220 Service ready for new user. |
| 85 |
|
|
221 Service closing control connection. |
| 86 |
|
|
Logged out if appropriate. |
| 87 |
|
|
225 Data connection open; no transfer in progress. |
| 88 |
|
|
226 Closing data connection. |
| 89 |
|
|
Requested file action successful (for example, file |
| 90 |
|
|
transfer or file abort). |
| 91 |
|
|
227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). |
| 92 |
|
|
230 User logged in, proceed. |
| 93 |
|
|
250 Requested file action okay, completed. |
| 94 |
|
|
257 "PATHNAME" created. |
| 95 |
|
|
|
| 96 |
|
|
331 User name okay, need password. |
| 97 |
|
|
332 Need account for login. |
| 98 |
|
|
350 Requested file action pending further information. |
| 99 |
|
|
|
| 100 |
|
|
421 Service not available, closing control connection. |
| 101 |
|
|
This may be a reply to any command if the service knows it |
| 102 |
|
|
must shut down. |
| 103 |
|
|
425 Can't open data connection. |
| 104 |
|
|
426 Connection closed; transfer aborted. |
| 105 |
|
|
450 Requested file action not taken. |
| 106 |
|
|
File unavailable (e.g., file busy). |
| 107 |
|
|
451 Requested action aborted: local error in processing. |
| 108 |
|
|
452 Requested action not taken. |
| 109 |
|
|
Insufficient storage space in system. |
| 110 |
|
|
500 Syntax error, command unrecognized. |
| 111 |
|
|
This may include errors such as command line too long. |
| 112 |
|
|
501 Syntax error in parameters or arguments. |
| 113 |
|
|
502 Command not implemented. |
| 114 |
|
|
503 Bad sequence of commands. |
| 115 |
|
|
504 Command not implemented for that parameter. |
| 116 |
|
|
530 Not logged in. |
| 117 |
|
|
532 Need account for storing files. |
| 118 |
|
|
550 Requested action not taken. |
| 119 |
|
|
File unavailable (e.g., file not found, no access). |
| 120 |
|
|
551 Requested action aborted: page type unknown. |
| 121 |
|
|
552 Requested file action aborted. |
| 122 |
|
|
Exceeded storage allocation (for current directory or |
| 123 |
|
|
dataset). |
| 124 |
|
|
553 Requested action not taken. |
| 125 |
|
|
File name not allowed. |
| 126 |
|
|
|
| 127 |
|
|
***/ |
| 128 |
|
|
|
| 129 |
|
|
/*** |
| 130 |
|
|
error_handler: |
| 131 |
|
|
- int sck => Socket Descriptor |
| 132 |
|
|
- int connected => Value 0 or 1: |
| 133 |
|
|
. 0 System Connected |
| 134 |
|
|
. 1 System Not Connected |
| 135 |
|
|
- int verbose => Print Inforations: |
| 136 |
|
|
. 0 Do not Print |
| 137 |
|
|
. 1 Print |
| 138 |
|
|
***/ |
| 139 |
|
|
|
| 140 |
|
|
int ftp_login_handler (char *cmd_reply, int verbose) { |
| 141 |
|
|
|
| 142 |
|
|
if (verbose == 1) { |
| 143 |
|
|
|
| 144 |
|
|
if (strfnbytes(cmd_reply, NEED_ACCT, 3) == 0) { |
| 145 |
|
|
printf("--[ Server: Need an Account to Login.\n"); |
| 146 |
|
|
return -1; |
| 147 |
|
|
} else if (strfnbytes(cmd_reply, LOGGED, 3) == 0) { |
| 148 |
|
|
printf("--[ Server: User Logged in.\n"); |
| 149 |
|
|
return 0; |
| 150 |
|
|
} else if (strfnbytes(cmd_reply, USER_OK, 3) == 0) { |
| 151 |
|
|
printf("--[ Server: User Okay, need a Password.\n"); |
| 152 |
|
|
return 2; |
| 153 |
|
|
} else if (strfnbytes(cmd_reply, NOT_LOGGED, 3) == 0) { |
| 154 |
|
|
printf("--[ Server: User not logged in.\n"); |
| 155 |
|
|
return -1; |
| 156 |
|
|
} else { |
| 157 |
|
|
printf("--[ Server: Unknown reply at the login process.\n"); |
| 158 |
|
|
return -1; |
| 159 |
|
|
} |
| 160 |
|
|
|
| 161 |
|
|
} else { |
| 162 |
|
|
|
| 163 |
|
|
if (strfnbytes(cmd_reply, NEED_ACCT, 3) == 0) { |
| 164 |
|
|
return -1; |
| 165 |
|
|
} else if (strfnbytes(cmd_reply, LOGGED, 3) == 0) { |
| 166 |
|
|
return 0; |
| 167 |
|
|
} else if (strfnbytes(cmd_reply, USER_OK, 3) == 0) { |
| 168 |
|
|
return 2; |
| 169 |
|
|
} else if (strfnbytes(cmd_reply, NOT_LOGGED, 3) == 0) { |
| 170 |
|
|
return -1; |
| 171 |
|
|
} else { |
| 172 |
|
|
return -1; |
| 173 |
|
|
} |
| 174 |
|
|
|
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
int ftp_dir_handler (char *cmd_reply, int verbose) { |
| 180 |
|
|
|
| 181 |
|
|
if (verbose == 1) { |
| 182 |
|
|
|
| 183 |
|
|
if (strfnbytes(cmd_reply, CMD_OK, 3) == 0) { |
| 184 |
|
|
printf("--[ Server: Command Ok.\n"); |
| 185 |
|
|
return 0; |
| 186 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 187 |
|
|
printf("--[ Server: Command successful.\n"); |
| 188 |
|
|
return 0; |
| 189 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 190 |
|
|
printf("--[ Server: Command successful.\n"); |
| 191 |
|
|
return 0; |
| 192 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 193 |
|
|
printf("--[ Server: Command successful.\n"); |
| 194 |
|
|
return 0; |
| 195 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR, 3) == 0) { |
| 196 |
|
|
printf("--[ Server: Syntax Error.\n"); |
| 197 |
|
|
return 1; |
| 198 |
|
|
} else if (strfnbytes(cmd_reply, PATH_CREATED, 3) == 0) { |
| 199 |
|
|
printf("--[ Server: Directory Successfully Created.\n"); |
| 200 |
|
|
return 0; |
| 201 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR_P, 3) == 0) { |
| 202 |
|
|
printf("--[ Server: Syntax Error in Parameters.\n"); |
| 203 |
|
|
return 1; |
| 204 |
|
|
} else if (strfnbytes(cmd_reply, FILE_NOT_FOUND, 3) == 0) { |
| 205 |
|
|
printf("--[ Server: File Not Found.\n"); |
| 206 |
|
|
return 1; |
| 207 |
|
|
} else if (strfnbytes(cmd_reply, ACTION_ABORTED, 3) == 0) { |
| 208 |
|
|
printf("--[ Server: Requested action aborted.\n"); |
| 209 |
|
|
return 1; |
| 210 |
|
|
} else if (strfnbytes(cmd_reply, FILE_ERR_NAME, 3) == 0) { |
| 211 |
|
|
printf("--[ Server: File Name not allowed.\n"); |
| 212 |
|
|
return 1; |
| 213 |
|
|
} else { |
| 214 |
|
|
printf("--[ Server: Unknown reply.\n"); |
| 215 |
|
|
return 1; |
| 216 |
|
|
} |
| 217 |
|
|
|
| 218 |
|
|
} else { |
| 219 |
|
|
|
| 220 |
|
|
if (strfnbytes(cmd_reply, CMD_OK, 3) == 0) { |
| 221 |
|
|
return 0; |
| 222 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 223 |
|
|
return 0; |
| 224 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 225 |
|
|
return 0; |
| 226 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 227 |
|
|
return 0; |
| 228 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR, 3) == 0) { |
| 229 |
|
|
return 1; |
| 230 |
|
|
} else if (strfnbytes(cmd_reply, PATH_CREATED, 3) == 0) { |
| 231 |
|
|
return 0; |
| 232 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR_P, 3) == 0) { |
| 233 |
|
|
return 1; |
| 234 |
|
|
} else if (strfnbytes(cmd_reply, FILE_NOT_FOUND, 3) == 0) { |
| 235 |
|
|
return 1; |
| 236 |
|
|
} else if (strfnbytes(cmd_reply, ACTION_ABORTED, 3) == 0) { |
| 237 |
|
|
return 1; |
| 238 |
|
|
} else if (strfnbytes(cmd_reply, FILE_ERR_NAME, 3) == 0) { |
| 239 |
|
|
return 1; |
| 240 |
|
|
} else { |
| 241 |
|
|
return 1; |
| 242 |
|
|
} |
| 243 |
|
|
|
| 244 |
|
|
} |
| 245 |
|
|
|
| 246 |
|
|
} |
| 247 |
|
|
|
| 248 |
|
|
int ftp_file_handler (char *cmd_reply, int verbose) { |
| 249 |
|
|
|
| 250 |
|
|
if (verbose == 1) { |
| 251 |
|
|
|
| 252 |
|
|
if (strfnbytes(cmd_reply, CMD_OK, 3) == 0) { |
| 253 |
|
|
printf("--[ Server: Command Ok.\n"); |
| 254 |
|
|
return 0; |
| 255 |
|
|
} else if (strfnbytes(cmd_reply, ACTION_PEND, 3) == 0) { |
| 256 |
|
|
printf("--[ Server: Command successful.\n"); |
| 257 |
|
|
return 0; |
| 258 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 259 |
|
|
printf("--[ Server: Requested file action completed.\n"); |
| 260 |
|
|
return 0; |
| 261 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR, 3) == 0) { |
| 262 |
|
|
printf("--[ Server: Syntax Error.\n"); |
| 263 |
|
|
return 1; |
| 264 |
|
|
} else if (strfnbytes(cmd_reply, FILE_NOT_FOUND, 3) == 0) { |
| 265 |
|
|
printf("--[ Server: File Not Found.\n"); |
| 266 |
|
|
return 1; |
| 267 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR_P, 3) == 0) { |
| 268 |
|
|
printf("--[ Server: Syntax Error in Parameters.\n"); |
| 269 |
|
|
return 1; |
| 270 |
|
|
} else if (strfnbytes(cmd_reply, BAD_SEQ_CMD, 3) == 0) { |
| 271 |
|
|
printf("--[ Server: File Not Found.\n"); |
| 272 |
|
|
return 1; |
| 273 |
|
|
} else if (strfnbytes(cmd_reply, ACTION_ABORTED, 3) == 0) { |
| 274 |
|
|
printf("--[ Server: Requested action aborted.\n"); |
| 275 |
|
|
return 1; |
| 276 |
|
|
} else if (strfnbytes(cmd_reply, FILE_NOT_TAKEN, 3) == 0) { |
| 277 |
|
|
printf("--[ Server: File busy.\n"); |
| 278 |
|
|
return 1; |
| 279 |
|
|
} else if (strfnbytes(cmd_reply, FILE_ERR_NAME, 3) == 0) { |
| 280 |
|
|
printf("--[ Server: File Name not allowed.\n"); |
| 281 |
|
|
return 1; |
| 282 |
|
|
} else { |
| 283 |
|
|
printf("--[ Server: Unknown reply.\n"); |
| 284 |
|
|
return 1; |
| 285 |
|
|
} |
| 286 |
|
|
|
| 287 |
|
|
|
| 288 |
|
|
|
| 289 |
|
|
} else { |
| 290 |
|
|
|
| 291 |
|
|
if (strfnbytes(cmd_reply, CMD_OK, 3) == 0) { |
| 292 |
|
|
return 0; |
| 293 |
|
|
} else if (strfnbytes(cmd_reply, ACTION_PEND, 3) == 0) { |
| 294 |
|
|
return 0; |
| 295 |
|
|
} else if (strfnbytes(cmd_reply, REQ_FILE_OK, 3) == 0) { |
| 296 |
|
|
return 0; |
| 297 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR, 3) == 0) { |
| 298 |
|
|
return 1; |
| 299 |
|
|
} else if (strfnbytes(cmd_reply, FILE_NOT_FOUND, 3) == 0) { |
| 300 |
|
|
return 1; |
| 301 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR_P, 3) == 0) { |
| 302 |
|
|
return 1; |
| 303 |
|
|
} else if (strfnbytes(cmd_reply, BAD_SEQ_CMD, 3) == 0) { |
| 304 |
|
|
return 1; |
| 305 |
|
|
} else if (strfnbytes(cmd_reply, ACTION_ABORTED, 3) == 0) { |
| 306 |
|
|
return 1; |
| 307 |
|
|
} else if (strfnbytes(cmd_reply, FILE_NOT_TAKEN, 3) == 0) { |
| 308 |
|
|
return 1; |
| 309 |
|
|
} else if (strfnbytes(cmd_reply, FILE_ERR_NAME, 3) == 0) { |
| 310 |
|
|
return 1; |
| 311 |
|
|
} else { |
| 312 |
|
|
return 1; |
| 313 |
|
|
} |
| 314 |
|
|
|
| 315 |
|
|
|
| 316 |
|
|
} |
| 317 |
|
|
|
| 318 |
|
|
} |
| 319 |
|
|
|
| 320 |
|
|
int ftp_port_handler (char *cmd_reply, int verbose) { |
| 321 |
|
|
|
| 322 |
|
|
if (verbose == 1) { |
| 323 |
|
|
|
| 324 |
|
|
if (strfnbytes(cmd_reply, SYNTAX_ERR, 3) == 0) { |
| 325 |
|
|
printf("--[ Server: Need an Account to Login.\n"); |
| 326 |
|
|
return -1; |
| 327 |
|
|
} else if (strfnbytes(cmd_reply, CMD_OK, 3) == 0) { |
| 328 |
|
|
printf("--[ Server: OK!\n"); |
| 329 |
|
|
return 0; |
| 330 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR_P, 3) == 0) { |
| 331 |
|
|
printf("--[ Server: User Okay, need a Password.\n"); |
| 332 |
|
|
return -1; |
| 333 |
|
|
} else if (strfnbytes(cmd_reply, SERVICE_NA, 3) == 0) { |
| 334 |
|
|
printf("--[ Server: User not logged in.\n"); |
| 335 |
|
|
return -1; |
| 336 |
|
|
} else { |
| 337 |
|
|
printf("--[ Server: Unknown reply at the login process.\n"); |
| 338 |
|
|
return -1; |
| 339 |
|
|
} |
| 340 |
|
|
|
| 341 |
|
|
} else { |
| 342 |
|
|
|
| 343 |
|
|
if (strfnbytes(cmd_reply, SYNTAX_ERR, 3) == 0) { |
| 344 |
|
|
return -1; |
| 345 |
|
|
} else if (strfnbytes(cmd_reply, CMD_OK, 3) == 0) { |
| 346 |
|
|
return 0; |
| 347 |
|
|
} else if (strfnbytes(cmd_reply, SYNTAX_ERR_P, 3) == 0) { |
| 348 |
|
|
return -1; |
| 349 |
|
|
} else if (strfnbytes(cmd_reply, SERVICE_NA, 3) == 0) { |
| 350 |
|
|
return -1; |
| 351 |
|
|
} else { |
| 352 |
|
|
return -1; |
| 353 |
|
|
} |
| 354 |
|
|
|
| 355 |
|
|
} |
| 356 |
|
|
|
| 357 |
|
|
} |