ftp client若若一问!(在线给分)

xzhuang 2002-11-15 04:23:37
以下是一个简单的试验。通过socket 连接ftp server的21端口。然后USER anonymous , PASS adfo@eea.net试图登陆服务器(支持匿名登陆,用telent server 21,然后送出USER anonymous , PASS adfo@eea.net。可以得到希望的结果)
////////////////////////////////////
////
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pwd.h>
#include <sys/ioctl.h>


#define PORT 21
//struct ioctl_data_t d;


int main(int argc, char *argv[])
{
int socketfd , numreceive;
struct hostent* server;
struct sockaddr_in server_addr;
char buffer[5000];
char hostname[] = "real.nwu.edu.cn";

if ((server = gethostbyname(hostname)) == NULL){ //DNS
printf("Get Host error\n");
exit(-1);
}

if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ //initlization socket
printf("socket initliaztion error\n");
exit(-1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr*)server->h_addr);
bzero(&(server_addr.sin_zero) , 8);

if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ //connect;
printf("connect error\n");
exit(-1);
}

int i = 1;
while(i!=2){
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message error\n");
exit(-1);
}
i++;
}

int buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '\0';
printf("%s" , buffer);

char send_msg[] = "USER anonymous\n"; //send anonymous quest and receive server message;

if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message error\n");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message error\n");
exit(-1);
}

buffer[numreceive + 1] = '\0';
printf("%s\n", buffer);

char send_msg2[] = "PASS adfoa@245.net\n"; //send password and receive message from server

if ((numreceive = send(socketfd, send_msg2, sizeof(send_msg2), 0)) == -1){
printf ("send message error\n");
exit(-1);
}

if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message error\n");
exit(-1);
}

buffer[numreceive + 1] = '\0';
printf("%s\n" , buffer);
close(socketfd);
return 0;
}
//////////////////////////
运行后的提示如下:(faint.........)
///
220 real FTP server (Version wu-2.6.1(1) Wed Aug 9 05:54:50 EDT 2000) ready.
331 Guest login ok, send your complete e-mail address as password.
)
530 Please login with USER and PASS.

问题好像出在PASS *******那块。在SEND passwd时。是否需要加密(md5?)。可是我看了一些ftp client 的源码好像都不用。而且就算要加密。server也应当send一个
encrypt key 过来。
难道要用ioctl来转换么?没用过这个函数。有谁能说说。
...全文
55 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzhuang 2002-11-15
  • 打赏
  • 举报
回复
up
xzhuang 2002-11-15
  • 打赏
  • 举报
回复
up
xzhuang 2002-11-15
  • 打赏
  • 举报
回复
x86(大雪):首先谢谢你。不过我觉得应该不用crypt的。因为我用telnet登陆21端口是可以的。
而且在rfc959中有:
PASSWORD (PASS)
The argument field is a Telnet string specifying the user’s
password. This command must be immediately preceded by the
user name command, and, for some sites, completes the user’s
identification for access control. Since password
information is quite sensitive, it is desirable in general
to "mask" it or suppress typeout. It appears that the
server has no foolproof way to achieve this. It is
therefore the responsibility of the user-FTP process to hide
the sensitive password information.
x86 2002-11-15
  • 打赏
  • 举报
回复
没写过,提些建议。
你可以用crypt后的密文:
crypt(passwd, salt);
passwd是加密前的明文,salt是两个字符的字符串,你可以给任意字符。
比如:
char salt[] = "ab";

你也可以直接试一下shadow文件中相应用户加密后的密码

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧