/* Send data through socket * @param socket: file descriptor of client * @param filename: name of the file * @param fdout: file descriptor to write * * @return: 0 on success. Error code otherwise */ int send_data(int socket, char filename[], int fdout) { struct stat file_stat; char type = END; if (stat(filename, &file_stat) < 0) return -1; if (file_stat.st_mode & S_IFDIR) { send_dir(socket, filename, search_root(filename), fdout); write(socket, &type, sizeof(char)); } else if (file_stat.st_mode & S_IFREG || file_stat.st_mode & S_IFLNK) { send_file(socket, filename, search_root(filename), fdout); write(socket, &type, sizeof(char)); } else { dprintf(fdout, "It can't be sended that kind of file\n"); dprintf(fdout, "Quitting...\n"); return -1; } dprintf(fdout, "\nSe han enviado: - %d directorios\n", DIR_S); dprintf(fdout, " - %d archivos\n", FILES_S); dprintf(fdout, "\nTransferencia realizada con éxito.\n"); write_to(socket, &DIR_S, sizeof(int)); write_to(socket, &FILES_S, sizeof(int)); return 0; }
/* Send file through socket * @param socket: file descriptor of client * @param filename: name of the file * @param position: index of the filename in absolute path * @param fdout: file descriptor to write * * @return: 0 on success. Error code otherwise */ int send_dir(int socket, char *dir_name, int position, int fdout) { DIR *d = NULL; send_file(socket, dir_name, position, fdout); /* Check it was opened. -1 on error */ if (!(d = opendir (dir_name))) { fprintf (stderr, "Cannot open directory '%s': %s\n", \ dir_name, strerror (errno)); return -1; } else { while (TRUE) { struct dirent *d_struct; char name[PATH_MAX]; /* readdir gets subsequent entries from "d" until the end (NULL) */ if ((d_struct = readdir(d))) { sprintf(name, "%s%s", dir_name, d_struct->d_name); if (d_struct->d_type == DT_DIR) { if (strcmp(d_struct->d_name, ".") && \ strcmp(d_struct->d_name, "..")) { sprintf(name, "%s%s/", dir_name, d_struct->d_name); send_dir(socket, name, position, fdout); } else /* SEE THIS */ sprintf(name, "%s%s", dir_name, d_struct->d_name); } else send_file(socket, name, position, fdout); } else break; } if (closedir(d)) { fprintf (stderr, "Could not close '%s': %s\n", \ dir_name, strerror (errno)); exit (EXIT_FAILURE); } } return 0; }
void send_node(per_request *reqInfo) { struct stat finfo; register x = 0; int allow; char allow_options; int ErrReturn = 0; exit_callback = NULL; /* It is no longer necessary to move all but one of the trailing slashes * to the path_info string, since all multiple slashes are now compressed * to one as a security precaution. */ if(stat(reqInfo->filename,&finfo) == -1) { ErrReturn = extract_path_info(reqInfo,&finfo); } evaluate_access(reqInfo,&finfo,&allow,&allow_options); if (ErrReturn) { if(ErrReturn == ENOENT) { log_reason(reqInfo,"file does not exist",reqInfo->filename); die(reqInfo,SC_NOT_FOUND,reqInfo->url); /* Check for AFS/NFS problems, and send back an unavailable message instead * Larry Schwimmer ([email protected]) */ } else if ((ErrReturn == ETIMEDOUT) || (ErrReturn == ENODEV)) { log_reason(reqInfo, "file temporarily unavailable", reqInfo->filename); die(reqInfo,SC_SERVICE_UNAVAIL,reqInfo->url); } else { log_reason(reqInfo,"(3) file permissions deny server access", reqInfo->filename); die(reqInfo,SC_FORBIDDEN,reqInfo->url); } } if(!allow) { log_reason(reqInfo,"client denied by server configuration", reqInfo->filename); die(reqInfo,SC_FORBIDDEN,reqInfo->url); } if (S_ISDIR(finfo.st_mode)) { send_dir(reqInfo,&finfo,allow_options); } else if (S_ISREG(finfo.st_mode)) { x = strlen(reqInfo->filename); /* Remove the trailing slash if its not a directory */ if (reqInfo->filename[x-1] == '/') { if (reqInfo->path_info[0] == '\0') { reqInfo->path_info[0] = '/'; reqInfo->path_info[1] = '\0'; } reqInfo->filename[x-1] = '\0'; } probe_content_type(reqInfo,reqInfo->filename); if (!strcmp(reqInfo->outh_content_type, CGI_MAGIC_TYPE)) send_cgi(reqInfo,&finfo,allow_options); #ifdef IMAGEMAP_SUPPORT else if (!strcmp(reqInfo->outh_content_type, IMAGEMAP_MAGIC_TYPE)) send_imagemap(reqInfo,&finfo,allow_options); #endif /* IMAGEMAP_SUPPORT */ #ifdef FCGI_SUPPORT else if (!strcmp(reqInfo->outh_content_type, FCGI_MAGIC_TYPE)) FastCgiHandler(reqInfo); #endif /* FCGI_SUPPORT */ else send_file(reqInfo,&finfo,allow_options); } else { log_reason(reqInfo,"improper file type",reqInfo->filename); /* device driver or pipe, no permission */ die(reqInfo,SC_FORBIDDEN,reqInfo->url); } }
int main(int argc , char* argv[])//exe ip port { signal(13,SIG_IGN); signal(17,sig_handle); if(argc != 3) { printf("usge : exe ip port"); exit(-1); } int fd_server ,fd_client; struct sockaddr_in client_addr ; int addrlen = sizeof(client_addr); fd_server = listenfd_init(argv[1],argv[2]); while( (fd_client = accept(fd_server,(struct sockaddr*)&client_addr,&addrlen) ) > 0 ) { if(fd_client == -1) { if(errno == EINTR) { continue ; } } printf("a client connect :%s:%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); if(fork() == 0) { close(fd_server); char comd[1024]; //circle recv msg while(1) { fflush(stdout); char sign[32]; bzero(comd, 1024); recv_command(fd_client,comd); printf("comd:%s\n", comd); if(strncmp(comd, "ls", 2) == 0) { send_dir(fd_client); } else if(strncmp(comd, "download", 8) == 0) { ul_file(fd_client); } else if(strncmp(comd, "cd", 2) == 0) { send_path(fd_client, comd); } else if(strncmp(comd, "rm", 2) == 0) { system(comd); } else if(strncmp(comd, "upload", 6) == 0) { dl_file(fd_client); } else if(strncmp(comd, "cl", 2) == 0) { system("clear"); } } exit(0); } } }