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 1 - (show annotations) (download) (as text)
Wed Feb 18 08:06:53 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpauth.c
File MIME type: text/x-csrc
File size: 4616 byte(s)
Initial import. original is libOftp_1_0.tar.gz.
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 if (strlen(usr) > 50) {
62
63 return USR_TOO_LONG;
64
65 }
66
67 char *auth = malloc(strlen("USER ")+strlen(usr)+2);
68 char buf[1024]={'\0'};
69 int n = 0;
70
71 sprintf(auth, "%s %s\n", "USER", usr);
72
73 if (write(sck, auth, strlen(auth)) == -1) {
74
75 if (verbose) {
76
77 if (errno == EBADF) {
78
79 fprintf(stderr, "Socket Descriptor Not Valid!\n");
80 free(auth);
81 return -1;
82 } else if (errno == EIO) {
83
84 fprintf(stderr, "I/O Error!\n");
85 free(auth);
86
87 return -1;
88 } else if (errno == EINTR) {
89
90 fprintf(stderr, "Signal Interrupted The write() Function\n");
91 free(auth);
92 return -1;
93 }
94
95 } else {
96
97 if (errno == EBADF) {
98 free(auth);
99 return -1;
100 } else if (errno == EIO) {
101 free(auth);
102
103 return -1;
104 } else if (errno == EINTR) {
105 free(auth);
106 return -1;
107 }
108
109 }
110
111 free(auth);
112
113 return -1;
114
115 }
116
117 free(auth);
118
119 buf[1023] = '\0';
120
121 struct timeval tm; //tv_sec - tv_usec
122 fd_set readfds;
123 tm.tv_sec = 3;
124 tm.tv_usec = 0;
125
126 FD_ZERO(&readfds);
127
128 FD_SET(sck, &readfds);
129 sleep(1); //wait a little bit
130
131 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
132 if (verbose) {
133 perror("select()");
134 }
135 return -1;
136 } else {
137
138 if (FD_ISSET(sck, &readfds)) {
139
140 if ( (n = recv(sck, buf, 1022, 0)) < 0) {
141 if (verbose) {
142 perror("recv()");
143 }
144 return -1;
145 }
146
147 if (verbose)
148 printf("[Server] %s\n", buf);
149
150 }
151 else {
152 if (verbose)
153 printf("[-] Timeout\n");
154
155 return -1;
156 }
157 }
158
159 buf[n] = '\0';
160
161 if (ftp_login_handler(buf, 0) != 2) { //2? Yes.. Need a Password
162 return -1;
163 }
164
165 memset(buf, 0x0, 1024);
166
167 if (strlen(pwd) > 50) {
168
169 return PWD_TOO_LONG;
170
171 }
172
173 auth = malloc(strlen("PASS ")+strlen(pwd)+2);
174
175 sprintf(auth, "%s %s\n", "PASS", pwd);
176
177 if (write(sck, auth, strlen(auth)) == -1) {
178
179 if (verbose) {
180 if (errno == EBADF) {
181 fprintf(stderr, "Socket Descriptor Not Valid!\n");
182 free(auth);
183
184 return -1;
185
186 } else if (errno == EIO) {
187
188 fprintf(stderr, "I/O Error!\n");
189 free(auth);
190
191 return -1;
192
193 } else if (errno == EINTR) {
194
195 fprintf(stderr, "Signal Interrupted The write() Function\n");
196 free(auth);
197
198 return -1;
199 }
200
201 } else {
202
203 if (errno == EBADF) {
204 free(auth);
205
206 return -1;
207
208 } else if (errno == EIO) {
209 free(auth);
210
211 return -1;
212
213 } else if (errno == EINTR) {
214 free(auth);
215
216 return -1;
217 }
218
219 }
220
221 free(auth);
222
223 return -1;
224
225 }
226
227 pwd_len = strlen(pwd) - 1;
228
229 free(auth);
230
231
232 FD_ZERO(&readfds);
233
234 FD_SET(sck, &readfds);
235
236 sleep(1);
237
238 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
239 if (verbose) {
240 perror("select()");
241 }
242 return -1;
243 } else {
244
245 if (FD_ISSET(sck, &readfds)) {
246
247 if ( (n= recv(sck, buf, 1022, 0)) < 0) {
248 if (verbose) {
249 perror("recv()");
250 }
251 return -1;
252 }
253
254 if (verbose)
255 printf("[Server] %s\n", buf);
256
257 }
258 else {
259 if (verbose)
260 printf("[-] Timeout\n");
261
262 return -1;
263 }
264 }
265
266 buf[n] = '\0';
267
268 if (ftp_login_handler(buf, 0) != 0) {
269
270 return -1;
271 }
272
273 memset(buf, 0x0, 1024);
274
275 return 0;
276
277 }

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