Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/REL-1.1/ftpauth.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations) (download) (as text)
Mon Feb 23 02:05:06 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpauth.c
File MIME type: text/x-csrc
File size: 4810 byte(s)
changed the terminator of the ftp command from CR to CRLF.
and solved race condition in ftp_get().

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 /* Global Variable used to store the password length */
38
39 int pwd_len = 0;
40
41 #define USR_TOO_LONG -2
42 #define PWD_TOO_LONG -2
43
44 /***
45 ftp_connect:
46 - char *host => HostName/IP
47 - int port => Port to connect to
48 - struct sockaddr_in * => pointer to sockaddr_in structure
49 - int verbose => Verbose?
50 . 0 Do not Print
51 . 1 Print
52 - ret => Success?
53 . sock Socket Descriptor Returned
54 .-1 Error
55 ***/
56
57
58
59 int ftp_auth(int sck, char *usr, char *pwd, int verbose)
60 {
61 char *auth;
62 char buf[1024]={'\0'};
63 int n = 0;
64 struct timeval tm; //tv_sec - tv_usec
65 fd_set readfds;
66
67
68 if (strlen(usr) > 50) {
69
70 return USR_TOO_LONG;
71
72 }
73
74 auth = malloc(strlen("USER ")+strlen(usr)+2);
75
76 sprintf(auth, "%s %s\r\n", "USER", usr);
77
78 if (write(sck, auth, strlen(auth)) == -1) {
79
80 if (verbose) {
81
82 if (errno == EBADF) {
83
84 fprintf(stderr, "Socket Descriptor Not Valid!\n");
85 free(auth);
86 return -1;
87 } else if (errno == EIO) {
88
89 fprintf(stderr, "I/O Error!\n");
90 free(auth);
91
92 return -1;
93 } else if (errno == EINTR) {
94
95 fprintf(stderr, "Signal Interrupted The write() Function\n");
96 free(auth);
97 return -1;
98 }
99
100 } else {
101
102 if (errno == EBADF) {
103 free(auth);
104 return -1;
105 } else if (errno == EIO) {
106 free(auth);
107
108 return -1;
109 } else if (errno == EINTR) {
110 free(auth);
111 return -1;
112 }
113
114 }
115
116 free(auth);
117
118 return -1;
119
120 }
121
122 free(auth);
123
124 buf[1023] = '\0';
125
126 tm.tv_sec = 3;
127 tm.tv_usec = 0;
128
129 FD_ZERO(&readfds);
130
131 FD_SET(sck, &readfds);
132 sleep(1); //wait a little bit
133
134 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
135 if (verbose) {
136 perror("select()");
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 return -1;
148 }
149
150 if (verbose)
151 printf("[Server] %s\n", buf);
152
153 }
154 else {
155 if (verbose)
156 printf("[-] Timeout\n");
157
158 return -1;
159 }
160 }
161
162 buf[n] = '\0';
163
164 if (ftp_login_handler(buf, 0) != 2) { //2? Yes.. Need a Password
165 return -1;
166 }
167
168 memset(buf, 0x0, 1024);
169
170 if (strlen(pwd) > 50) {
171
172 return PWD_TOO_LONG;
173
174 }
175
176 auth = malloc(strlen("PASS ")+strlen(pwd)+2);
177
178 sprintf(auth, "%s %s\r\n", "PASS", pwd);
179
180 if (write(sck, auth, strlen(auth)) == -1) {
181
182 if (verbose) {
183 if (errno == EBADF) {
184 fprintf(stderr, "Socket Descriptor Not Valid!\n");
185 free(auth);
186
187 return -1;
188
189 } else if (errno == EIO) {
190
191 fprintf(stderr, "I/O Error!\n");
192 free(auth);
193
194 return -1;
195
196 } else if (errno == EINTR) {
197
198 fprintf(stderr, "Signal Interrupted The write() Function\n");
199 free(auth);
200
201 return -1;
202 }
203
204 } else {
205
206 if (errno == EBADF) {
207 free(auth);
208
209 return -1;
210
211 } else if (errno == EIO) {
212 free(auth);
213
214 return -1;
215
216 } else if (errno == EINTR) {
217 free(auth);
218
219 return -1;
220 }
221
222 }
223
224 free(auth);
225
226 return -1;
227
228 }
229
230 pwd_len = strlen(pwd) - 1;
231
232 free(auth);
233
234
235 FD_ZERO(&readfds);
236
237 FD_SET(sck, &readfds);
238
239 sleep(1);
240
241 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
242 if (verbose) {
243 perror("select()");
244 }
245 return -1;
246 } else {
247
248 if (FD_ISSET(sck, &readfds)) {
249
250 if ( (n= recv(sck, buf, 1022, 0)) < 0) {
251 if (verbose) {
252 perror("recv()");
253 }
254 return -1;
255 }
256 buf[n] = '\0';
257
258 if (verbose)
259 printf("[Server] %s\n", buf);
260
261 }
262 else {
263 if (verbose)
264 printf("[-] Timeout\n");
265
266 return -1;
267 }
268 }
269
270 if (ftp_login_handler(buf, 0) != 0) {
271
272 return -1;
273 }
274
275 memset(buf, 0x0, 1024);
276
277 return 0;
278
279 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26