| 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 <string.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <sys/socket.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <netdb.h> |
| 33 |
#include <fcntl.h> |
| 34 |
|
| 35 |
#include "ftp_main.h" |
| 36 |
|
| 37 |
#define ASCIITYPE 1 //ASCII Type |
| 38 |
#define NPRINTYPE 2 //Non-Print Type |
| 39 |
#define IMAGETYPE 3 //Image Type |
| 40 |
#define EBCDIC 4 //EBCDIC Type |
| 41 |
#define TELNET 5 //Telnet Type |
| 42 |
|
| 43 |
/*** |
| 44 |
ftp_type: |
| 45 |
- int sck => Socket Descriptor |
| 46 |
- int t_type => What kind of TYPE? ASCII(1), IMAGE(3)... |
| 47 |
- int verbose => Verbose? |
| 48 |
. 0 Do not Print |
| 49 |
. 1 Print |
| 50 |
- ret => Success? |
| 51 |
. 0 Success |
| 52 |
.-1 Error |
| 53 |
***/ |
| 54 |
|
| 55 |
int ftp_type(int sck, int t_type, int verbose) { |
| 56 |
|
| 57 |
|
| 58 |
char cmd_type[10]={'\0'}; |
| 59 |
char *buf; |
| 60 |
int n = 0; |
| 61 |
buf = (char *)malloc(1024*sizeof(char)); |
| 62 |
|
| 63 |
|
| 64 |
switch(t_type) { |
| 65 |
|
| 66 |
case ASCIITYPE: |
| 67 |
sprintf(cmd_type, "TYPE A\n"); |
| 68 |
break; |
| 69 |
|
| 70 |
case NPRINTYPE: |
| 71 |
sprintf(cmd_type, "TYPE N\n"); |
| 72 |
break; |
| 73 |
|
| 74 |
case IMAGETYPE: |
| 75 |
sprintf(cmd_type, "TYPE I\n"); |
| 76 |
break; |
| 77 |
|
| 78 |
case EBCDIC: |
| 79 |
sprintf(cmd_type, "TYPE E\n"); |
| 80 |
break; |
| 81 |
|
| 82 |
case TELNET: |
| 83 |
sprintf(cmd_type, "TYPE T\n"); |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
free(buf); |
| 88 |
|
| 89 |
return -1; |
| 90 |
break; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
memset(buf, 0x0, 1024); |
| 95 |
|
| 96 |
|
| 97 |
if (write(sck, cmd_type, strlen(cmd_type)) == -1) { |
| 98 |
|
| 99 |
if (verbose) { |
| 100 |
if (errno == EBADF) { |
| 101 |
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
| 102 |
free(buf); |
| 103 |
|
| 104 |
return -1; |
| 105 |
} else if (errno == EIO) { |
| 106 |
fprintf(stderr, "I/O Error!\n"); |
| 107 |
free(buf); |
| 108 |
|
| 109 |
return -1; |
| 110 |
} else if (errno == EINTR) { |
| 111 |
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
| 112 |
free(buf); |
| 113 |
|
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
} else { |
| 118 |
|
| 119 |
if (errno == EBADF) { |
| 120 |
free(buf); |
| 121 |
|
| 122 |
return -1; |
| 123 |
} else if (errno == EIO) { |
| 124 |
free(buf); |
| 125 |
|
| 126 |
return -1; |
| 127 |
} else if (errno == EINTR) { |
| 128 |
free(buf); |
| 129 |
|
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
free(buf); |
| 136 |
|
| 137 |
return -1; |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
struct timeval tm; //tv_sec - tv_usec |
| 142 |
fd_set readfds; |
| 143 |
tm.tv_sec = 3; |
| 144 |
tm.tv_usec = 0; |
| 145 |
|
| 146 |
FD_ZERO(&readfds); |
| 147 |
|
| 148 |
FD_SET(sck, &readfds); |
| 149 |
|
| 150 |
if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) { |
| 151 |
if (verbose) { |
| 152 |
perror("select()"); |
| 153 |
} |
| 154 |
free(buf); |
| 155 |
|
| 156 |
return -1; |
| 157 |
} else { |
| 158 |
|
| 159 |
if (FD_ISSET(sck, &readfds)) { |
| 160 |
|
| 161 |
if ( (n = recv(sck, buf, 1022, 0)) < 0) { |
| 162 |
if (verbose) { |
| 163 |
perror("recv()"); |
| 164 |
} |
| 165 |
free(buf); |
| 166 |
|
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
if (verbose) |
| 171 |
printf("[Server] %s\n", buf); |
| 172 |
|
| 173 |
} |
| 174 |
else { |
| 175 |
if (verbose) |
| 176 |
printf("[-] Timeout\n"); |
| 177 |
|
| 178 |
return -1; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
buf[n] = '\0'; |
| 183 |
|
| 184 |
memset(buf, 0x0, 1024); |
| 185 |
|
| 186 |
free(buf); |
| 187 |
|
| 188 |
|
| 189 |
return 0; //0 returned; operation sucessfull! |
| 190 |
|
| 191 |
} |