int ftp_list(const char *cmd, const char *param, FILE *fp) { if(!cmd || !fp || !ftp_connected()) return -1; if(ftp->ssh_pid) return ssh_list(cmd, param, fp); reset_transfer_info(); foo_hookf = 0; #if 0 /* don't care about transfer type, binary should work well... */ ftp_type(tmAscii); #endif if(ftp_init_transfer() != 0) return -1; ftp_set_tmp_verbosity(vbNone); if(param) ftp_cmd("%s %s", cmd, param); else ftp_cmd("%s", cmd); if(ftp->code != ctPrelim) return -1; if(sock_accept(ftp->data, "r", ftp_is_passive()) != 0) { perror("accept()"); return -1; } if(FILE_recv_ascii(ftp->data->sin, fp) != 0) return -1; sock_destroy(ftp->data); ftp_read_reply(); ftp->data = 0; return ftp->code == ctComplete ? 0 : -1; }
/*-------------------------------------------------------------------------*\ * Tries to bind socket to (address, port) \*-------------------------------------------------------------------------*/ const char *inet_trybind(p_sock ps, const char *address, unsigned short port) { struct sockaddr_in local; int err; memset(&local, 0, sizeof(local)); /* address is either wildcard or a valid ip address */ local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_port = htons(port); local.sin_family = AF_INET; if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; err = sock_gethostbyname(address, &hp); if (err != IO_DONE) return sock_hoststrerror(err); addr = (struct in_addr **) hp->h_addr_list; memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); } err = sock_bind(ps, (SA *) &local, sizeof(local)); if (err != IO_DONE) sock_destroy(ps); return sock_strerror(err); }
/*-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/ const char *inet_tryconnect(p_sock ps, const char *address, unsigned short port, p_tm tm) { struct sockaddr_in remote; int err; memset(&remote, 0, sizeof(remote)); remote.sin_family = AF_INET; remote.sin_port = htons(port); if (strcmp(address, "*")) { if (!inet_aton(address, &remote.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; err = sock_gethostbyname(address, &hp); if (err != IO_DONE) return sock_hoststrerror(err); addr = (struct in_addr **) hp->h_addr_list; memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); } } else remote.sin_family = AF_UNSPEC; err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); if (err != IO_DONE) sock_destroy(ps); return sock_strerror(err); }
void DLLCALL xpms_destroy(struct xpms_set *xpms_set, void (*sock_destroy)(SOCKET, void *), void *cbdata) { int i; if(!xpms_set) return; for(i=0; i<xpms_set->sock_count; i++) { if(xpms_set->socks[i].sock != INVALID_SOCKET) { if(xpms_set->lprintf!=NULL) xpms_set->lprintf(LOG_INFO, "%04d closing %s socket on port %d" , xpms_set->socks[i].sock, xpms_set->socks[i].prot?xpms_set->socks[i].prot:"unknown" , xpms_set->socks[i].port); closesocket(xpms_set->socks[i].sock); if(sock_destroy) sock_destroy(xpms_set->socks[i].sock, cbdata); } xpms_set->socks[i].sock = INVALID_SOCKET; FREE_AND_NULL(xpms_set->socks[i].address); FREE_AND_NULL(xpms_set->socks[i].prot); } FREE_AND_NULL(xpms_set->socks); free(xpms_set); }
/*! * \brief Sends the notify message and returns a reply. * * \return on success returns UPNP_E_SUCCESS, otherwise returns a UPNP error. * * \note called by genaNotify */ static UPNP_INLINE int notify_send_and_recv( /*! [in] subscription callback URL (URL of the control point). */ uri_type *destination_url, /*! [in] Common HTTP headers. */ membuffer *mid_msg, /*! [in] The evented XML. */ char *propertySet, /*! [out] The response from the control point. */ http_parser_t *response) { uri_type url; SOCKET conn_fd; membuffer start_msg; int ret_code; int err_code; int timeout; SOCKINFO info; const char *CRLF = "\r\n"; /* connect */ UpnpPrintf(UPNP_ALL, GENA, __FILE__, __LINE__, "gena notify to: %.*s\n", (int)destination_url->hostport.text.size, destination_url->hostport.text.buff); conn_fd = http_Connect(destination_url, &url); if (conn_fd < 0) /* return UPNP error */ return UPNP_E_SOCKET_CONNECT; ret_code = sock_init(&info, conn_fd); if (ret_code) { sock_destroy(&info, SD_BOTH); return ret_code; } /* make start line and HOST header */ membuffer_init(&start_msg); if (http_MakeMessage( &start_msg, 1, 1, "q" "s", HTTPMETHOD_NOTIFY, &url, mid_msg->buf) != 0) { membuffer_destroy(&start_msg); sock_destroy(&info, SD_BOTH); return UPNP_E_OUTOF_MEMORY; } timeout = GENA_NOTIFICATION_SENDING_TIMEOUT; /* send msg (note: end of notification will contain "\r\n" twice) */ ret_code = http_SendMessage(&info, &timeout, "bbb", start_msg.buf, start_msg.length, propertySet, strlen(propertySet), CRLF, strlen(CRLF)); if (ret_code) { membuffer_destroy(&start_msg); sock_destroy(&info, SD_BOTH); return ret_code; } timeout = GENA_NOTIFICATION_ANSWERING_TIMEOUT; ret_code = http_RecvMessage(&info, response, HTTPMETHOD_NOTIFY, &timeout, &err_code); if (ret_code) { membuffer_destroy(&start_msg); sock_destroy(&info, SD_BOTH); httpmsg_destroy(&response->msg); return ret_code; } /* should shutdown completely when closing socket */ sock_destroy(&info, SD_BOTH); membuffer_destroy(&start_msg); return UPNP_E_SUCCESS; }
/** * Uninitialize IPC interface */ static void lrvm_ipc_done (void) { sock_destroy (sock); }
void ftp_reset_vars(void) { sock_destroy(ftp->data); ftp->data = NULL; sock_destroy(ftp->ctrl); ftp->ctrl = NULL; host_destroy(ftp->host); ftp->host = NULL; #ifdef HAVE_LIBSSH if (ftp->session) { sftp_free(ftp->sftp_session); ssh_disconnect(ftp->session); ssh_free(ftp->session); ftp->sftp_session = NULL; ftp->session = NULL; } #endif url_destroy(ftp->url); ftp->url = NULL; ftp->connected = false; ftp->loggedin = false; ftp->has_mdtm_command = true; ftp->has_size_command = true; ftp->has_pasv_command = true; ftp->has_stou_command = true; ftp->has_site_chmod_command = true; ftp->has_site_idle_command = true; ftp->has_mlsd_command = true; list_free(ftp->dirs_to_flush); ftp->dirs_to_flush = list_new((listfunc)free); list_free(ftp->cache); ftp->cache = list_new((listfunc)rdir_destroy); /* don't assume server is in ascii mode initially even if RFC says so */ ftp->prev_type = '?'; ftp->code = ctNone; ftp->fullcode = 0; ftp->reply_timeout = gvCommandTimeout; free(ftp->last_mkpath); ftp->last_mkpath = 0; #ifdef SECFTP sec_end(); ftp->request_data_prot = 0; ftp->buffer_size = 0; #endif ftp->LIST_type = ltUnknown; list_free(ftp->taglist); ftp->taglist = list_new((listfunc)rfile_destroy); }
/*-------------------------------------------------------------------------*\ * Closes socket used by object \*-------------------------------------------------------------------------*/ static int meth_close(lua_State *L) { p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); sock_destroy(&udp->sock); lua_pushnumber(L, 1); return 1; }
static int ftp_send(const char *path, FILE *fp, putmode_t how, transfer_mode_t mode, ftp_transfer_func hookf) { int r; long rp = ftp->restart_offset; ftp->restart_offset = 0L; if(how == putUnique && !ftp->has_stou_command) return -1; if (how == putTryUnique && !ftp->has_stou_command) how = putNormal; reset_transfer_info(); ftp->ti.transfer_is_put = true; if(ftp_init_transfer() != 0) return -1; ftp_type(mode); if(rp > 0) { /* fp is assumed to be fseek'd already */ ftp_cmd("REST %ld", rp); if(ftp->code != ctContinue) return -1; ftp->ti.size = rp; ftp->ti.restart_size = rp; } ftp_set_tmp_verbosity(vbError); switch (how) { case putAppend: ftp_cmd("APPE %s", path); break; case putTryUnique: case putUnique: ftp_cmd("STOU %s", path); if (ftp->fullcode == 502 || ftp->fullcode == 504) { ftp->has_stou_command = false; if (how == putTryUnique) how = putNormal; else break; } else break; default: ftp_cmd("STOR %s", path); break; } if(ftp->code != ctPrelim) return -1; if(how == putUnique) { /* try to figure out remote filename */ char *e = strstr(ftp->reply, " for "); if(e) { int l; e += 5; l = strlen(e); if(l) { free(ftp->ti.local_name); if(*e == '\'') ftp->ti.local_name = xstrndup(e+1, l-3); else ftp->ti.local_name = xstrndup(e, l-1); ftp_trace("parsed unique filename as '%s'\n", ftp->ti.local_name); } } } if(!sock_accept(ftp->data, "w", ftp_is_passive())) { ftp_err(_("data connection not accepted\n")); return -1; } ftp_cache_flush_mark_for(path); if(mode == tmBinary) r = FILE_send_binary(fp, ftp->data); else r = FILE_send_ascii(fp, ftp->data); sock_flush(ftp->data); sock_destroy(ftp->data); ftp->data = 0; if(r == 0) { transfer_finished(); ftp_read_reply(); ftp->ti.ioerror = (ftp->code != ctComplete); if(ftp->code != ctComplete) { ftp_trace("transfer failed\n"); return -1; } } else transfer_finished(); return 0; }
static int ftp_init_transfer(void) { if (!ftp_connected()) return -1; if (!sock_dup(ftp->ctrl, &ftp->data)) return -1; if (ftp_is_passive()) { ftp_trace("Initializing passive connection.\n"); struct sockaddr_storage sa; memcpy(&sa, sock_remote_addr(ftp->ctrl), sizeof(struct sockaddr_storage)); unsigned char pac[6] = { 0 }; unsigned short ipv6_port = { 0 }; if (!ftp_pasv(sa.ss_family != AF_INET, pac, &ipv6_port)) { ftp_trace("PASV/EPSV failed.\n"); sock_destroy(ftp->data); ftp->data = NULL; return -1; } socklen_t len = sizeof(struct sockaddr_in); if (sa.ss_family == AF_INET) { memcpy(&((struct sockaddr_in*)&sa)->sin_addr, pac, (size_t)4); memcpy(&((struct sockaddr_in*)&sa)->sin_port, pac+4, (size_t)2); } #ifdef HAVE_IPV6 else if (sa.ss_family == AF_INET6) { ((struct sockaddr_in6*)&sa)->sin6_port = htons(ipv6_port); len = sizeof(struct sockaddr_in6); } #endif else { ftp_trace("Do not know how to handle family %d.\n", sa.ss_family); sock_destroy(ftp->data); ftp->data = NULL; return -1; } struct sockaddr_storage tmp; memcpy(&tmp, sock_remote_addr(ftp->ctrl), sizeof(struct sockaddr_storage)); if (is_reserved((struct sockaddr*) &sa) || is_multicast((struct sockaddr*) &sa) || (is_private((struct sockaddr*) &sa) != is_private((struct sockaddr*) &tmp)) || (is_loopback((struct sockaddr*) &sa) != is_loopback((struct sockaddr*) &tmp))) { // Invalid address returned by PASV. Replace with address from control // socket. ftp_err(_("Address returned by PASV seems to be incorrect.\n")); ((struct sockaddr_in*)&sa)->sin_addr = ((struct sockaddr_in*)&tmp)->sin_addr; } if (!sock_connect_addr(ftp->data, (struct sockaddr*) &sa, len)) { ftp_trace("Could not connect to address from PASV/EPSV.\n"); perror("connect()"); sock_destroy(ftp->data); ftp->data = NULL; return -1; } } else { ftp_trace("Initializing active connection.\n"); const struct sockaddr* local = sock_local_addr(ftp->data); sock_listen(ftp->data, local->sa_family); if (local->sa_family == AF_INET) { struct sockaddr_in* tmp = (struct sockaddr_in*)local; unsigned char* a = (unsigned char *)&tmp->sin_addr; unsigned char* p = (unsigned char *)&tmp->sin_port; ftp_set_tmp_verbosity(vbError); ftp_cmd("PORT %d,%d,%d,%d,%d,%d", a[0], a[1], a[2], a[3], p[0], p[1]); } #ifdef HAVE_IPV6 else if (local->sa_family == AF_INET6) { char* addr = printable_address(local); ftp_set_tmp_verbosity(vbError); ftp_cmd("EPRT |2|%s|%u|", addr, ntohs(((struct sockaddr_in6*)local)->sin6_port)); free(addr); } #endif else { ftp_trace("Do not know how to handle family %d.\n", local->sa_family); sock_destroy(ftp->data); ftp->data = NULL; return -1; } if(ftp->code != ctComplete) { ftp_trace("PORT/EPRT not successful\n"); sock_destroy(ftp->data); ftp->data = NULL; return -1; } } sock_throughput(ftp->data); return 0; }
static int ftp_init_transfer(void) { struct sockaddr_storage sa; unsigned char *a, *p; if(!ftp_connected()) return -1; if (!(ftp->data = sock_create())) { return -1; } sock_copy(ftp->data, ftp->ctrl); if (ftp_is_passive()) { memcpy(&sa, sock_remote_addr(ftp->ctrl), sizeof(struct sockaddr_storage)); unsigned char pac[6]; unsigned short ipv6_port; if (!ftp_pasv(sa.ss_family != AF_INET, pac, &ipv6_port)) goto err1; socklen_t len = sizeof(struct sockaddr_in); if (sa.ss_family == AF_INET) { memcpy(&((struct sockaddr_in*)&sa)->sin_addr, pac, (size_t)4); memcpy(&((struct sockaddr_in*)&sa)->sin_port, pac+4, (size_t)2); } #ifdef HAVE_IPV6 else if (sa.ss_family == AF_INET6) { ((struct sockaddr_in6*)&sa)->sin6_port = htons(ipv6_port); len = sizeof(struct sockaddr_in6); } #endif else return -1; struct sockaddr_storage tmp; memcpy(&tmp, sock_remote_addr(ftp->ctrl), sizeof(struct sockaddr_storage)); if (is_reserved((struct sockaddr*) &sa) || is_multicast((struct sockaddr*) &sa) || (is_private((struct sockaddr*) &sa) != is_private((struct sockaddr*) &tmp)) || (is_loopback((struct sockaddr*) &sa) != is_loopback((struct sockaddr*) &tmp))) { // Invalid address returned by PASV. Replace with address from control // socket. ftp_err(_("Address returned by PASV seems to be incorrect.\n")); ((struct sockaddr_in*)&sa)->sin_addr = ((struct sockaddr_in*)&tmp)->sin_addr; } if (!sock_connect_addr(ftp->data, (struct sockaddr*) &sa, len)) { perror("connect()"); goto err1; } } else { const struct sockaddr* local = sock_local_addr(ftp->data); sock_listen(ftp->data, local->sa_family); if (local->sa_family == AF_INET) { struct sockaddr_in* tmp = (struct sockaddr_in*)local; a = (unsigned char *)&tmp->sin_addr; p = (unsigned char *)&tmp->sin_port; ftp_set_tmp_verbosity(vbError); ftp_cmd("PORT %d,%d,%d,%d,%d,%d", a[0], a[1], a[2], a[3], p[0], p[1]); } #ifdef HAVE_IPV6 else if (local->sa_family == AF_INET6) { char* addr = printable_address(local); ftp_set_tmp_verbosity(vbError); ftp_cmd("EPRT |2|%s|%u", addr, ntohs(((struct sockaddr_in6*)local)->sin6_port)); free(addr); } #endif else goto err1; if(ftp->code != ctComplete) goto err1; } sock_throughput(ftp->data); return 0; err1: sock_destroy(ftp->data); ftp->data = 0; return -1; }
int main (int argc, char *argv[]) { int fd = -1; char *log_identity = argv[0]; int log_priority = LOG_INFO; int log_options = LOG_OPT_PRIORITY; #ifndef NDEBUG log_priority = LOG_DEBUG; log_options |= LOG_OPT_TIMESTAMP; #endif /* NDEBUG */ log_open_file (stderr, log_identity, log_priority, log_options); disable_core_dumps (); conf = create_conf (); parse_cmdline (conf, argc, argv); auth_recv_init (conf->auth_server_dir, conf->auth_client_dir, conf->got_force); if (!conf->got_foreground) { fd = daemonize_init (argv[0]); if (conf->got_syslog) { log_open_file (NULL, NULL, 0, 0); log_open_syslog (log_identity, LOG_DAEMON); } else { open_logfile (conf->logfile_name, log_priority, conf->got_force); } } handle_signals (); lookup_ip_addr (conf); write_pidfile (conf->pidfile_name, conf->got_force); if (conf->got_mlockall) { lock_memory (); } crypto_init (); if (random_init (conf->seed_name) < 0) { if (conf->seed_name) { free (conf->seed_name); conf->seed_name = NULL; } } create_subkeys (conf); conf->gids = gids_create (conf->gids_update_secs, conf->got_group_stat); replay_init (); timer_init (); sock_create (conf); if (!conf->got_foreground) { daemonize_fini (fd); } log_msg (LOG_NOTICE, "Starting %s daemon (pid %d)", META_ALIAS, (int) getpid ()); job_accept (conf); sock_destroy (conf); timer_fini (); replay_fini (); gids_destroy (conf->gids); random_fini (conf->seed_name); crypto_fini (); destroy_conf (conf); log_msg (LOG_NOTICE, "Stopping %s daemon (pid %d)", META_ALIAS, (int) getpid ()); exit (EMUNGE_SUCCESS); }