/***************************************************************************
* connections.c
*
* Copyright 2005 Dimitur Kirov
* dkirov@gmail.com
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <assert.h>
#include <netdb.h>
#include <errno.h>
#include <dirent.h>
#include "defines.h"
#include "cmdparser.h"
#include "fileutils.h"
int open_connections;
bool max_limit_notify;
int raiseerr(int err_code) {
printf("Error %d\n",err_code);
return -1;
}
/**
* This is neccessary for future use of glib and gettext based localization.
*/
const char * _(const char* message) {
return message;
}
/**
* Guess the transfer type, given the client requested type.
* Actually in unix there is no difference between binary and
* ascii mode when we work with file descriptors.
* If #type is not recognized as a valid client request, -1 is returned.
*/
int get_type(const char *type) {
if(type==NULL)
return -1;
int len = strlen(type);
if(len==0)
return -1;
switch(type[0]) {
case 'I':
return 1;
case 'A':
return 2;
case 'L':
if(len<3)
return -1;
if(type[2]=='7')
return 3;
if(type[2]=='8')
return 4;
}
return -1;
}
/**
* Create a new connection to a (address,port) tuple, retrieved from
* the PORT command. This connection will be used for data transfers
* in commands like "LIST","STOR","RETR"
*/
int make_client_connection(int sock_fd,int client_port,const char* client_addr) {
if(client_port<1) {
send_repl(sock_fd,REPL_425);
return -1;
}
int sock=-1;
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(client_addr);
servaddr.sin_port = htons (client_port);
if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
send_repl(sock,REPL_425);
raiseerr(15);
return -1;
}
int status = connect (sock, (struct sockaddr *)&servaddr, sizeof (servaddr));
if(status!=0) {
send_repl(sock,REPL_425);
return -1;
}
return sock;
}
/**
* Close the connection to the client and exit the child proccess.
* Although it is the same as close(sock_fd), in the future it can be used for
* logging some stats about active and closed sessions.
*/
void close_conn(int sock_fd) {
if (close(sock_fd) < 0) {
raiseerr (5);
}
exit(0);
}
/**
* Get the next command from the client socket.
*/
int get_command(int conn_fd,char *read_buff1,char *data_buff) {
char read_buff[RCVBUFSIZE];
memset((char *)&read_buff, 0, RCVBUFSIZE);
read_buff[0]='\0';
char *rcv=read_buff;
int cmd_status = -1;
int recvbuff = recv(conn_fd,read_buff,RCVBUFSIZE,0);
if(recvbuff<1) {
return CMD_CLOSE;
}
if(recvbuff==RCVBUFSIZE) {
return CMD_UNKNOWN;
}
// printf("Received:%s\n",rcv);
cmd_status = parse_input(rcv,data_buff);
return cmd_status;
}
/**
* A handler, which is called on child proccess exit.
*/
void sig_chld_handler(void) {
open_connections--;
while (waitpid(-1, NULL, WNOHANG) > 0);
}
/**
* Send reply to the client socket, given the reply.
*/
int send_repl(int send_sock,char *msg) {
if (send(send_sock, msg, strlen(msg),0) < 0) {
raiseerr (4);
close(send_sock);
exit(0);
}
return 0;
}
/**
* Send single reply to the client socket, given the reply and its length.
*/
int send_repl_client_len(int send_sock,char *msg,int len) {
if (send(send_sock, msg, len,0) < 0) {
raiseerr (4);
close(send_sock);
}
return 0;
}
/*
Izprashtane na edinishen otgovor do dopulnitelnia socket za transfer
*/
int send_repl_client(int send_sock,char *msg) {
send_repl_client_len(send_sock,msg,strlen(msg));
return 0;
}
/**
* Send single reply to the additional transfer socket, given the raply and its length.
*/
int send_repl_len(int send_sock,char *msg,int len) {
if (send(send_sock, msg, len,0) < 0) {
raiseerr (4);
close(send_sock);
exit(0);
}
return 0;
}
/**
* Parses the results from the PORT command, writes the
* address in "client_addt" and returnes the port
*/
int parse_port_data(char *data_buff,char *client_addr) {
client_addr[0]='\0';
int len=0;
int port=0;
int _toint=0;
char *result;
result = strtok(data_buff, PORTDELIM);
_toint=toint(result,FALSE);
if(_toint<1 || _toint>254)
return -1;
len += strlen(result);
strcpy(client_addr,result);
client_addr[len]='\0';
strcat(client_addr,".");
len++;
result = strtok(NULL, PORTDELIM);
_toint=toint(result,FALSE);
if(_toint<0 || _toint>254)
return -1;
len += strlen(result);
strcat(client_addr,result);
client_addr[len]='\0';
strcat(client_addr,".");
len++;
result = strtok(NULL, PORTDELIM);
if(_toint<0 || _toint>254)
return -1;
len += strlen(result);
strcat(client_addr,result);
client_addr[len]='\0';
strcat(client_addr,".");
len++;
result = strtok(NULL, PORTDELIM);
if(_toint<0 || _toint>254)
return -1;
len += strlen(result);
strcat(client_addr,result);
client_addr[len]='\0';
result = strtok(NULL, PORTDELIM);
len = toint(result,FALSE);
if(_toint<0 || _toint>255)
return -1;
port = 256*len;
result = strtok(NULL, PORTDELIM);
len = toint(result,FALSE);
if(_toint<0 || _toint>255)
return -1;
port +=len;
return port;
}
void print_help(int sock) {
send_repl(sock," Some help message.\r\n Probably nobody needs help from telnet.\r\n See rfc959.\r\n");
}
/**
* Main cycle for client<->server communication.
* This is done synchronously. On each client message, it is parsed and recognized,
* certain action is performed. After that we wait for the next client message
*
*/
int interract(int conn_fd,cmd_opts *opts) {
static int BANNER_LEN = strlen(REPL_220);
int userid = opts->userid;
int client_fd=-1;
int len;
int _type ;
int type = 2; // ASCII TYPE by default
if(userid>0) {
int status = setreuid(userid,userid);
if(status != 0) {
switch(errno) {
case EPERM:
break;
case EAGAIN:
break;
default:
break;
}
close_conn(conn_fd);
}
}
if(max_limit_notify) {
send_repl(conn_fd,REPL_120);
close_conn(conn_fd);
}
char current_dir[MAXPATHLEN];
char parent_dir[MAXPATHLEN];
char virtual_dir[MAXPATHLEN];
char reply[SENDBUFSIZE];
char data_buff[DATABUFSIZE];
char read_buff[RCVBUFSIZE];
char *str;
bool is_loged = FALSE;
bool state_user = FALSE;
char rename_from[MAXPATHLEN];
memset((char *)¤t_dir, 0, MAXPATHLEN);
strcpy(current_dir,opts->chrootdir);
strcpy(parent_dir,opts->chrootdir);
free(opts);
chdir(current_dir);
if((getcwd(current_dir,MAXPATHLEN)==NULL)) {
raiseerr(19);
close_conn(conn_fd);
}
memset((char *)&data_buff, 0, DATABUFSIZE);
memset((char *)&read_buff, 0, RCVBUFSIZE);
reply[0]='\0';
int client_port = 0;
char client_addr[ADDRBUFSIZE];
send_repl_len(conn_fd,REPL_220,BANNER_LEN);
while(1) {
data_buff[0]='\0';
int result = get_command(conn_fd,read_buff,data_buff);
if(result != CMD_RNFR && result != CMD_RNTO && result != CMD_NOOP)
rename_from[0]='\0';
switch(result) {
case CMD_UNKNOWN:
case -1:
send_repl(conn_fd,REPL_500);
break;
case CMD_EMPTY:
ca
评论1
最新资源