Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.6  
changed lines
  Added in v.7

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