| 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 DIR_TOO_LONG 2 |
| 38 |
#define NAME_TOO_LONG 2 |
| 39 |
|
| 40 |
/*** |
| 41 |
ftp_rmd: |
| 42 |
- int sck => Socket Descriptor |
| 43 |
- int verbose => Verbose? |
| 44 |
. 0 Do not Print |
| 45 |
. 1 Print |
| 46 |
- ret => Success? |
| 47 |
. 0 Success |
| 48 |
.-1 Error |
| 49 |
. 2 Error |
| 50 |
***/ |
| 51 |
|
| 52 |
|
| 53 |
int ftp_rmd (int sck, char *Directory, int verbose) |
| 54 |
{ |
| 55 |
char buffer[1024]={'\0'}; |
| 56 |
char *buf; |
| 57 |
int n = 0; |
| 58 |
struct timeval tm; //tv_sec - tv_usec |
| 59 |
fd_set readfds; |
| 60 |
|
| 61 |
|
| 62 |
buf = (char *)malloc(1024*sizeof(char)); |
| 63 |
|
| 64 |
memset(buffer, 0x0, 1024); |
| 65 |
memset(buf, 0x0, 1024); |
| 66 |
|
| 67 |
|
| 68 |
if ( strlen(Directory) > 1015 ) { |
| 69 |
free(buf); |
| 70 |
|
| 71 |
return DIR_TOO_LONG; |
| 72 |
} |
| 73 |
|
| 74 |
sprintf(buffer, "RMD %s\r\n", Directory); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if (write(sck, buffer, strlen(buffer)) == -1) { |
| 79 |
|
| 80 |
|
| 81 |
if (verbose) { |
| 82 |
if (errno == EBADF) { |
| 83 |
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
| 84 |
free(buf); |
| 85 |
|
| 86 |
return -1; |
| 87 |
} else if (errno == EIO) { |
| 88 |
fprintf(stderr, "I/O Error!\n"); |
| 89 |
free(buf); |
| 90 |
|
| 91 |
return -1; |
| 92 |
} else if (errno == EINTR) { |
| 93 |
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
| 94 |
free(buf); |
| 95 |
|
| 96 |
return -1; |
| 97 |
} |
| 98 |
|
| 99 |
} else { |
| 100 |
|
| 101 |
if (errno == EBADF) { |
| 102 |
free(buf); |
| 103 |
|
| 104 |
return -1; |
| 105 |
} else if (errno == EIO) { |
| 106 |
free(buf); |
| 107 |
|
| 108 |
return -1; |
| 109 |
} else if (errno == EINTR) { |
| 110 |
free(buf); |
| 111 |
|
| 112 |
return -1; |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
free(buf); |
| 118 |
|
| 119 |
return -1; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
sleep(1); //wait.. |
| 125 |
tm.tv_sec = 3; |
| 126 |
tm.tv_usec = 0; |
| 127 |
|
| 128 |
FD_ZERO(&readfds); |
| 129 |
|
| 130 |
FD_SET(sck, &readfds); |
| 131 |
|
| 132 |
if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) { |
| 133 |
if (verbose) { |
| 134 |
perror("select()"); |
| 135 |
} |
| 136 |
free(buf); |
| 137 |
|
| 138 |
return -1; |
| 139 |
} else { |
| 140 |
|
| 141 |
if (FD_ISSET(sck, &readfds)) { |
| 142 |
|
| 143 |
if ( (n = recv(sck, buf, 1022, 0)) < 0) { |
| 144 |
if (verbose) { |
| 145 |
perror("recv()"); |
| 146 |
} |
| 147 |
free(buf); |
| 148 |
|
| 149 |
return -1; |
| 150 |
} |
| 151 |
|
| 152 |
if (verbose) |
| 153 |
printf("[Server] %s\n", buf); |
| 154 |
|
| 155 |
} |
| 156 |
else { |
| 157 |
if (verbose) |
| 158 |
printf("[-] Timeout\n"); |
| 159 |
|
| 160 |
return -1; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
buf[n] = '\0'; |
| 165 |
|
| 166 |
if (ftp_dir_handler(buf, 0) != 0) { |
| 167 |
free(buf); |
| 168 |
|
| 169 |
return -1; //Error |
| 170 |
} |
| 171 |
|
| 172 |
free(buf); |
| 173 |
|
| 174 |
return 0; //Removed! |
| 175 |
|
| 176 |
} |