| 1 |
/* |
| 2 |
liboftp: this is an FTP library to simplify the work to a Developer |
| 3 |
who want to work with FTP servers (RFC 959). |
| 4 |
|
| 5 |
Copyright (c) 2009 hirohito higashi. All rights reserved. |
| 6 |
This file is distributed under BSD license. |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
/***** Feature test switches ************************************************/ |
| 11 |
/***** System headers *******************************************************/ |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
|
| 16 |
/***** Local headers ********************************************************/ |
| 17 |
#include "liboftp.h" |
| 18 |
#include "sub.h" |
| 19 |
|
| 20 |
|
| 21 |
/***** Constat values *******************************************************/ |
| 22 |
/***** Macros ***************************************************************/ |
| 23 |
/***** Typedefs *************************************************************/ |
| 24 |
/***** Function prototypes **************************************************/ |
| 25 |
/***** Local variables ******************************************************/ |
| 26 |
/***** Global variables *****************************************************/ |
| 27 |
/***** Signal catching functions ********************************************/ |
| 28 |
/***** Local functions ******************************************************/ |
| 29 |
/***** Global functions *****************************************************/ |
| 30 |
|
| 31 |
/****************************************************************************/ |
| 32 |
/*! ��������������� |
| 33 |
* |
| 34 |
*@param ftp LIBOFTP��������������������� |
| 35 |
*@param user ������������ |
| 36 |
*@param pass ��������������� |
| 37 |
*@retval int ������������������ |
| 38 |
*@note |
| 39 |
* ������������������������������������������������������ |
| 40 |
*/ |
| 41 |
int ftp_user( LIBOFTP *ftp, const char *user, const char *pass ) |
| 42 |
{ |
| 43 |
char str1[256]; |
| 44 |
int res; |
| 45 |
|
| 46 |
/* |
| 47 |
* send user name. |
| 48 |
*/ |
| 49 |
snprintf( str1, sizeof(str1)-1, "USER %s\r\n", user ); |
| 50 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 51 |
DEBUGPRINT1( "user command sending error.%s\n", "" ); |
| 52 |
return LIBOFTP_ERROR_OS; |
| 53 |
} |
| 54 |
|
| 55 |
res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) ); |
| 56 |
if( res == 230 ) { /* 230: User logged in, proceed. */ |
| 57 |
goto PROCEED; |
| 58 |
} |
| 59 |
if( res != 331 ) { /* 331: User name okay, need password. */ |
| 60 |
DEBUGPRINT1( "USER command response error. %d\n", res ); |
| 61 |
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* send password. |
| 66 |
*/ |
| 67 |
snprintf( str1, sizeof(str1)-1, "PASS %s\r\n", pass ); |
| 68 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 69 |
DEBUGPRINT1( "pass command sending error.%s\n", "" ); |
| 70 |
return LIBOFTP_ERROR_OS; |
| 71 |
} |
| 72 |
|
| 73 |
res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) ); |
| 74 |
if( res != 230 ) { /* 230: User logged in, proceed. */ |
| 75 |
DEBUGPRINT1( "user authentication error.%s\n", "" ); |
| 76 |
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 77 |
} |
| 78 |
|
| 79 |
PROCEED: |
| 80 |
/* |
| 81 |
* system type. |
| 82 |
*/ |
| 83 |
snprintf( str1, sizeof(str1)-1, "SYST\r\n" ); |
| 84 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 85 |
DEBUGPRINT1( "SYST command sending error.%s\n", "" ); |
| 86 |
return LIBOFTP_ERROR_OS; |
| 87 |
} |
| 88 |
|
| 89 |
res = ftp_receive_response( ftp, str1, sizeof(str1) ); |
| 90 |
if( res == 215 ) { |
| 91 |
if( strstr( str1, "UNIX" ) != 0 ) { |
| 92 |
ftp->system_type = UNIX; |
| 93 |
} else if( strstr( str1, "Windows_NT" ) != 0 ) { |
| 94 |
ftp->system_type = Windows_NT; |
| 95 |
} |
| 96 |
DEBUGPRINT1( "system type is %d\n", ftp->system_type ); |
| 97 |
} |
| 98 |
|
| 99 |
return LIBOFTP_NOERROR; |
| 100 |
} |
| 101 |
|
| 102 |
|