Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (hide annotations) (download) (as text)
Fri Feb 20 12:10:40 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpauth.c
File MIME type: text/x-csrc
File size: 4806 byte(s)
moved variable declaration  for traditional compilers. such as QNX6.2.

1 hirohitohigashi 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 hirohitohigashi 7 - 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 hirohitohigashi 1 ***/
56    
57    
58    
59 hirohitohigashi 7 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 hirohitohigashi 1
67 hirohitohigashi 7
68     if (strlen(usr) > 50) {
69 hirohitohigashi 1
70     return USR_TOO_LONG;
71    
72 hirohitohigashi 7 }
73 hirohitohigashi 1
74 hirohitohigashi 7 auth = malloc(strlen("USER ")+strlen(usr)+2);
75 hirohitohigashi 1
76 hirohitohigashi 7 sprintf(auth, "%s %s\n", "USER", usr);
77 hirohitohigashi 1
78 hirohitohigashi 7 if (write(sck, auth, strlen(auth)) == -1) {
79 hirohitohigashi 1
80 hirohitohigashi 7 if (verbose) {
81 hirohitohigashi 1
82 hirohitohigashi 7 if (errno == EBADF) {
83 hirohitohigashi 1
84     fprintf(stderr, "Socket Descriptor Not Valid!\n");
85     free(auth);
86     return -1;
87 hirohitohigashi 7 } else if (errno == EIO) {
88 hirohitohigashi 1
89     fprintf(stderr, "I/O Error!\n");
90     free(auth);
91    
92     return -1;
93 hirohitohigashi 7 } else if (errno == EINTR) {
94 hirohitohigashi 1
95     fprintf(stderr, "Signal Interrupted The write() Function\n");
96     free(auth);
97     return -1;
98 hirohitohigashi 7 }
99 hirohitohigashi 1
100 hirohitohigashi 7 } else {
101 hirohitohigashi 1
102 hirohitohigashi 7 if (errno == EBADF) {
103 hirohitohigashi 1 free(auth);
104     return -1;
105 hirohitohigashi 7 } else if (errno == EIO) {
106 hirohitohigashi 1 free(auth);
107    
108     return -1;
109 hirohitohigashi 7 } else if (errno == EINTR) {
110 hirohitohigashi 1 free(auth);
111     return -1;
112 hirohitohigashi 7 }
113 hirohitohigashi 1
114 hirohitohigashi 7 }
115 hirohitohigashi 1
116 hirohitohigashi 7 free(auth);
117 hirohitohigashi 1
118 hirohitohigashi 7 return -1;
119 hirohitohigashi 1
120 hirohitohigashi 7 }
121 hirohitohigashi 1
122 hirohitohigashi 7 free(auth);
123 hirohitohigashi 1
124 hirohitohigashi 7 buf[1023] = '\0';
125 hirohitohigashi 1
126 hirohitohigashi 7 tm.tv_sec = 3;
127     tm.tv_usec = 0;
128 hirohitohigashi 1
129 hirohitohigashi 7 FD_ZERO(&readfds);
130 hirohitohigashi 1
131 hirohitohigashi 7 FD_SET(sck, &readfds);
132     sleep(1); //wait a little bit
133 hirohitohigashi 1
134 hirohitohigashi 7 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 hirohitohigashi 1 if (verbose) {
145 hirohitohigashi 7 perror("recv()");
146 hirohitohigashi 1 }
147     return -1;
148 hirohitohigashi 7 }
149 hirohitohigashi 1
150 hirohitohigashi 7 if (verbose)
151     printf("[Server] %s\n", buf);
152 hirohitohigashi 1
153 hirohitohigashi 7 }
154     else {
155     if (verbose)
156     printf("[-] Timeout\n");
157 hirohitohigashi 1
158 hirohitohigashi 7 return -1;
159 hirohitohigashi 1 }
160 hirohitohigashi 7 }
161 hirohitohigashi 1
162 hirohitohigashi 7 buf[n] = '\0';
163 hirohitohigashi 1
164 hirohitohigashi 7 if (ftp_login_handler(buf, 0) != 2) { //2? Yes.. Need a Password
165     return -1;
166     }
167 hirohitohigashi 1
168 hirohitohigashi 7 memset(buf, 0x0, 1024);
169 hirohitohigashi 1
170 hirohitohigashi 7 if (strlen(pwd) > 50) {
171 hirohitohigashi 1
172 hirohitohigashi 7 return PWD_TOO_LONG;
173 hirohitohigashi 1
174 hirohitohigashi 7 }
175 hirohitohigashi 1
176 hirohitohigashi 7 auth = malloc(strlen("PASS ")+strlen(pwd)+2);
177 hirohitohigashi 1
178 hirohitohigashi 7 sprintf(auth, "%s %s\n", "PASS", pwd);
179 hirohitohigashi 1
180 hirohitohigashi 7 if (write(sck, auth, strlen(auth)) == -1) {
181 hirohitohigashi 1
182 hirohitohigashi 7 if (verbose) {
183     if (errno == EBADF) {
184 hirohitohigashi 1 fprintf(stderr, "Socket Descriptor Not Valid!\n");
185     free(auth);
186    
187     return -1;
188    
189 hirohitohigashi 7 } else if (errno == EIO) {
190 hirohitohigashi 1
191     fprintf(stderr, "I/O Error!\n");
192     free(auth);
193    
194     return -1;
195    
196 hirohitohigashi 7 } else if (errno == EINTR) {
197 hirohitohigashi 1
198     fprintf(stderr, "Signal Interrupted The write() Function\n");
199     free(auth);
200    
201     return -1;
202 hirohitohigashi 7 }
203 hirohitohigashi 1
204 hirohitohigashi 7 } else {
205 hirohitohigashi 1
206 hirohitohigashi 7 if (errno == EBADF) {
207 hirohitohigashi 1 free(auth);
208    
209     return -1;
210    
211 hirohitohigashi 7 } else if (errno == EIO) {
212 hirohitohigashi 1 free(auth);
213    
214     return -1;
215    
216 hirohitohigashi 7 } else if (errno == EINTR) {
217 hirohitohigashi 1 free(auth);
218    
219     return -1;
220 hirohitohigashi 7 }
221 hirohitohigashi 1
222 hirohitohigashi 7 }
223 hirohitohigashi 1
224     free(auth);
225    
226     return -1;
227    
228 hirohitohigashi 7 }
229 hirohitohigashi 1
230 hirohitohigashi 7 pwd_len = strlen(pwd) - 1;
231 hirohitohigashi 1
232 hirohitohigashi 7 free(auth);
233 hirohitohigashi 1
234    
235 hirohitohigashi 7 FD_ZERO(&readfds);
236 hirohitohigashi 1
237 hirohitohigashi 7 FD_SET(sck, &readfds);
238 hirohitohigashi 1
239 hirohitohigashi 7 sleep(1);
240 hirohitohigashi 1
241 hirohitohigashi 7 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 hirohitohigashi 1 if (verbose) {
252 hirohitohigashi 7 perror("recv()");
253 hirohitohigashi 1 }
254     return -1;
255 hirohitohigashi 7 }
256     buf[n] = '\0';
257 hirohitohigashi 1
258 hirohitohigashi 7 if (verbose)
259     printf("[Server] %s\n", buf);
260 hirohitohigashi 1
261 hirohitohigashi 7 }
262     else {
263     if (verbose)
264     printf("[-] Timeout\n");
265 hirohitohigashi 1
266 hirohitohigashi 7 return -1;
267 hirohitohigashi 1 }
268 hirohitohigashi 7 }
269 hirohitohigashi 1
270 hirohitohigashi 7 if (ftp_login_handler(buf, 0) != 0) {
271 hirohitohigashi 1
272     return -1;
273 hirohitohigashi 7 }
274 hirohitohigashi 1
275 hirohitohigashi 7 memset(buf, 0x0, 1024);
276 hirohitohigashi 1
277 hirohitohigashi 7 return 0;
278 hirohitohigashi 1
279     }

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