static Connection * ServerSocketInit(int port) { Connection *conn = ConnAlloc(FALSE); if (conn) { conn->socketAddress.sin_family = AF_INET; conn->socketAddress.sin_port = htons(port); conn->socketAddress.sin_addr.s_addr = MsgGetAgentBindIPAddress(); /* Get root privs back for the bind. It's ok if this fails - * the user might not need to be root to bind to the port */ XplSetEffectiveUserId(0); conn->socket = ConnServerSocket(conn, 2048); if (XplSetEffectiveUser(MsgGetUnprivilegedUser()) < 0) { Log(LOG_ERROR, "Could not drop to unprivileged user '%s'.", MsgGetUnprivilegedUser()); ConnFree(conn); return NULL; } if (conn->socket == -1) { Log(LOG_ERROR, "Could not bind to port %d.", port); ConnFree(conn); return NULL; } } else { Log(LOG_ERROR, "Could not allocate connection."); return NULL; } return conn; }
static BOOL ApplyObjects(ItipAgentClient *client, BongoSList *users, BongoCalObject *cal, char *invitation) { BongoSList *l; for (l = users; l != NULL; l = l->next) { const char *user = l->data; Connection *conn; // FIXME: don't assume localhost. conn = NMAPConnect("127.0.0.1", NULL); if (!conn || !NMAPAuthenticateThenUserAndStore(conn, user)) { continue; } if (cal) { ApplyCalObject(client, conn, cal); } if (invitation) { SaveCalendarInvitation(client, conn, invitation); } NMAPQuit(conn); ConnFree(conn); } return TRUE; }
int FileRepConnServer_CreateConnection() { int status = STATUS_OK; port = (Port *) calloc(1, sizeof(Port)); if (port == NULL) { ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), (errmsg("not enough memory to create connection")))); return status; } status = StreamConnection(listenSocket[0], port); if (status != STATUS_OK) { ereport(WARNING, (errcode_for_socket_access(), errmsg("could not accept connection: %m"), FileRep_errcontext())); if (port->sock >= 0) { StreamClose(port->sock); } ConnFree(); } else { /* * MPP-14225: On NIC failure, filerep receiver process's recv() system * call will take hours to timeout, depending on the TCP timeout. Add * SO_RCVTIMEO timeout to filerep receiver process's socket to avoid * this. */ struct timeval tv; tv.tv_sec = file_rep_socket_timeout; tv.tv_usec = 0; /* Not initializing this can cause strange * errors */ if (setsockopt(port->sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(struct timeval)) == -1) ereport(WARNING, (errcode_for_socket_access(), errmsg("could not set receive timeout on socket"))); /* set TCP keep-alive parameters for FileRep connection */ (void) pq_setkeepalivesidle(gp_filerep_tcp_keepalives_idle, port); (void) pq_setkeepalivesinterval(gp_filerep_tcp_keepalives_interval, port); (void) pq_setkeepalivescount(gp_filerep_tcp_keepalives_count, port); MyProcPort = port; } return status; }
void FileRepConnServer_CloseConnection(void) { if (port != NULL) { secure_close(port); if (port->sock >= 0) { /* to close socket (file descriptor) */ StreamClose(port->sock); port->sock = -1; } ConnFree(); } }
BOOL VirusCheck(AVirusClient *client, const char *queueID, BOOL hasFlags, unsigned long msgFlags, unsigned long senderIp, char *senderUserName) { int ccode; long size; BOOL infected = FALSE; Connection *conn; conn = ConnAddressPoolConnect(&(AVirus.clamd.hosts), AVirus.clamd.connectionTimeout); if (conn) { Connection *data; unsigned short port; ConnWrite(conn, "STREAM\r\n", strlen("STREAM\r\n")); ConnFlush(conn); ccode = ConnReadAnswer(conn, client->line, CONN_BUFSIZE); if (!ccode || strncmp(client->line, "PORT ", strlen("PORT ")) != 0 || (port = atoi(client->line + strlen("PORT "))) == 0) { ConnFree(conn); return -1; } data = ConnAlloc(TRUE); if (!data) { ConnFree(conn); return -1; } memcpy(&data->socketAddress, &conn->socketAddress, sizeof(struct sockaddr_in)); data->socketAddress.sin_port = htons(port); if (ConnConnectEx(data, NULL, 0, NULL, client->conn->trace.destination) < 0) { ConnFree(conn); ConnFree(data); return -1; } size = 0; if (((ccode = NMAPSendCommandF(client->conn, "QRETR %s MESSAGE\r\n", queueID)) != -1) && ((ccode = NMAPReadAnswer(client->conn, client->line, CONN_BUFSIZE, TRUE)) != -1) && ccode == 2023) { char *ptr; ptr = strchr (client->line, ' '); if (ptr) { *ptr = '\0'; } size = atol(client->line); } if (size == 0) { ConnFree(conn); ConnFree(data); return -1; } ccode = ConnReadToConn(client->conn, data, size); ConnFree(data); if ((ccode == -1) || ((ccode = NMAPReadAnswer(client->conn, client->line, CONN_BUFSIZE, TRUE)) != 1000)) { Log(LOG_DEBUG, "result: %d", ccode); ConnFree(conn); return -1; } while ((ccode = ConnReadAnswer(conn, client->line, CONN_BUFSIZE)) != -1) { char *ptr; ptr = strrchr(client->line, ' '); if (XplStrCaseCmp(ptr + 1, "FOUND") == 0) { *ptr = '\0'; ptr = client->line + strlen("stream: "); #if 0 if(client->foundViruses.used == client->foundViruses.allocated) { client->foundViruses.names = MemRealloc(client->foundViruses.names, sizeof(char*) * (client->foundViruses.allocated + MIME_REALLOC_SIZE)); } client->foundViruses.names[client->foundViruses.used++] = MemStrdup(ptr); XplSafeIncrement(AVirus.stats.viruses); #endif infected = TRUE; } } ConnFree(conn); } return infected; }