| 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 <time.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <netinet/in.h> |
| 33 |
#include <netdb.h> |
| 34 |
#include <fcntl.h> |
| 35 |
|
| 36 |
#include "ftp_main.h" |
| 37 |
|
| 38 |
|
| 39 |
/*** |
| 40 |
ftp_put: |
| 41 |
- int sck => Socket Descriptor |
| 42 |
- char *FileName => The file name that you want to upload |
| 43 |
- int verbose => Verbose? |
| 44 |
. 0 Do not Print |
| 45 |
. 1 Print |
| 46 |
- ret => Success? |
| 47 |
. 0 Success |
| 48 |
.-1 Error |
| 49 |
***/ |
| 50 |
|
| 51 |
#define DATA_CONNECTION_ERROR -3 |
| 52 |
#define WRONG_DATA_P -3 |
| 53 |
#define USR_TOO_LONG -2 |
| 54 |
|
| 55 |
time_t res_put; |
| 56 |
|
| 57 |
int ftp_put (int sck, char *FileName, int verbose) |
| 58 |
{ |
| 59 |
char *buffer; |
| 60 |
int n = 0; |
| 61 |
int flags; |
| 62 |
time_t start, end; |
| 63 |
int sock; |
| 64 |
FILE *fd; |
| 65 |
int con_err = 0; |
| 66 |
char buff[4096]; |
| 67 |
int n_bytes; |
| 68 |
int n_send; |
| 69 |
|
| 70 |
extern int data_port; |
| 71 |
extern struct sockaddr_in ftp_connection; |
| 72 |
|
| 73 |
|
| 74 |
buffer = (char *)malloc(4092*sizeof(char)); |
| 75 |
|
| 76 |
if (buffer == NULL) { |
| 77 |
printf("Unable to alloc 4092 bytes to buffer (ftp_put)\n"); |
| 78 |
return -1; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (data_port == 0) { |
| 83 |
|
| 84 |
free(buffer); |
| 85 |
|
| 86 |
return WRONG_DATA_P; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
if (strlen(FileName) > 1024) { |
| 91 |
|
| 92 |
free(buffer); |
| 93 |
|
| 94 |
return USR_TOO_LONG; |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
if (access(FileName, F_OK) != 0) { |
| 99 |
if (verbose) { |
| 100 |
printf("--[ Error: File Name '%s' not here\n", FileName); |
| 101 |
} |
| 102 |
|
| 103 |
free(buffer); |
| 104 |
|
| 105 |
|
| 106 |
return 1; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
sprintf(buffer, "STOR %s\r\n", FileName); |
| 111 |
|
| 112 |
if (write(sck, buffer, strlen(buffer)) == -1) { |
| 113 |
|
| 114 |
if (verbose) { |
| 115 |
|
| 116 |
if (errno == EBADF) { |
| 117 |
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
| 118 |
free(buffer); |
| 119 |
|
| 120 |
return -1; |
| 121 |
|
| 122 |
} else if (errno == EIO) { |
| 123 |
|
| 124 |
fprintf(stderr, "I/O Error!\n"); |
| 125 |
free(buffer); |
| 126 |
return -1; |
| 127 |
|
| 128 |
} else if (errno == EINTR) { |
| 129 |
|
| 130 |
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
| 131 |
free(buffer); |
| 132 |
|
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
} else { |
| 137 |
|
| 138 |
if (errno == EBADF) { |
| 139 |
free(buffer); |
| 140 |
|
| 141 |
return -1; |
| 142 |
|
| 143 |
} else if (errno == EIO) { |
| 144 |
free(buffer); |
| 145 |
|
| 146 |
return -1; |
| 147 |
|
| 148 |
} else if (errno == EINTR) { |
| 149 |
free(buffer); |
| 150 |
|
| 151 |
return -1; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
free(buffer); |
| 158 |
|
| 159 |
return -1; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
sock = socket(AF_INET, SOCK_STREAM, 0); |
| 164 |
if (sock == -1) { |
| 165 |
free(buffer); |
| 166 |
if (verbose) { |
| 167 |
perror("Socket"); |
| 168 |
} |
| 169 |
return -1; |
| 170 |
} |
| 171 |
|
| 172 |
ftp_connection.sin_port = htons(data_port); |
| 173 |
|
| 174 |
if ( (con_err = connect(sock, (struct sockaddr *)&ftp_connection, sizeof(struct sockaddr)) ) != 0) { |
| 175 |
|
| 176 |
data_port = 0; |
| 177 |
n = recv(sck, buffer, 1024, 0); |
| 178 |
memset(buffer, 0x0, 1024); |
| 179 |
free(buffer); |
| 180 |
|
| 181 |
if (verbose) |
| 182 |
printf("[-] Data Connection error (PUT)\n"); |
| 183 |
|
| 184 |
return DATA_CONNECTION_ERROR; |
| 185 |
} |
| 186 |
|
| 187 |
if ( (flags=fcntl(sock, F_GETFL,0)) < 0) { |
| 188 |
printf("--[ fcntl get flags error.\n"); |
| 189 |
exit(1); |
| 190 |
} |
| 191 |
|
| 192 |
flags |= O_NONBLOCK; |
| 193 |
|
| 194 |
if ( fcntl (sock, F_SETFL, flags ) < 0 ) { |
| 195 |
printf("--[ Non Blocking Socket Error.\n"); |
| 196 |
exit(1); |
| 197 |
} |
| 198 |
|
| 199 |
buff[4095] = '\0'; |
| 200 |
|
| 201 |
fd = fopen(FileName, "rb"); |
| 202 |
|
| 203 |
if (fd == NULL) { |
| 204 |
|
| 205 |
if (verbose) { |
| 206 |
printf("--[ Error: Unable to open '%s' in ftp_put\n", FileName); |
| 207 |
} |
| 208 |
|
| 209 |
return -1; |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
while (!feof(fd)) { |
| 214 |
|
| 215 |
n_bytes = fread(buff, sizeof(char), 4094, fd); |
| 216 |
n_send = send(sock, buff, n_bytes, 0); |
| 217 |
usleep(150); |
| 218 |
memset(buff, 0x0, n_send); |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
fclose(fd); |
| 223 |
close(sock); |
| 224 |
|
| 225 |
|
| 226 |
time(&start); |
| 227 |
|
| 228 |
while (1) { |
| 229 |
|
| 230 |
sleep(1); |
| 231 |
n = recv(sck, buffer, 4092, MSG_DONTWAIT); |
| 232 |
if (n == 0) |
| 233 |
break; |
| 234 |
|
| 235 |
if (n < 0) { |
| 236 |
|
| 237 |
if ( (errno == ESPIPE) || (errno == EAGAIN) ) { |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
if (verbose) { |
| 242 |
printf("--[ Error: Error in ftp_put() while putting on the file\n"); |
| 243 |
} |
| 244 |
|
| 245 |
free(buffer); |
| 246 |
|
| 247 |
return -1; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
buffer[n] = '\0'; |
| 252 |
|
| 253 |
n = 0; |
| 254 |
memset(buffer, 0x0, 4092); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
memset(buffer, 0x0, 4092); |
| 259 |
|
| 260 |
time(&end); |
| 261 |
|
| 262 |
res_put = (int)(end - start); |
| 263 |
|
| 264 |
free(buffer); |
| 265 |
|
| 266 |
|
| 267 |
return 0; |
| 268 |
} |