static void s_SetDefaultReferer(SERV_ITER iter, SConnNetInfo* net_info) { char* str, *referer = 0; if (strcasecmp(iter->op->name, "DISPD") == 0) referer = ConnNetInfo_URL(net_info); else if ((str = strdup(iter->op->name)) != 0) { const char* host = net_info->client_host; const char* args = net_info->args; const char* name = iter->name; if (!*net_info->client_host && !SOCK_gethostbyaddr(0, net_info->client_host, sizeof(net_info->client_host))) { SOCK_gethostname(net_info->client_host, sizeof(net_info->client_host)); } if (!(referer = (char*) malloc(3 + 1 + 1 + 1 + 2*strlen(strlwr(str)) + strlen(host) + (args[0] ? strlen(args) : 8 + strlen(name))))) { return; } strcat(strcat(strcat(strcat(strcpy (referer, str), "://"), host), "/"), str); if (args[0]) strcat(strcat(referer, "?"), args); else strcat(strcat(referer, "?service="), name); free(str); } assert(!net_info->http_referer); net_info->http_referer = referer; }
static int/*bool*/ TEST_gethostbyaddr(unsigned int host) { const char* name; char buf[1024]; CORE_LOG(eLOG_Note, "------------"); name = SOCK_gethostbyaddr(host, buf, sizeof(buf)); if ( name ) { assert(name == buf); assert(0 < strlen(buf) && strlen(buf) < sizeof(buf)); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): \"%s\"", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host), name)); } else { CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): <not found>", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); return 0/*false*/; } host = SOCK_gethostbyname(name); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyname(\"%s\"): 0x%08X [%s]", name, (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); return 1/*true*/; }
static unsigned int TEST_gethostbyname(const char* name) { char buf[256]; unsigned int host; CORE_LOG(eLOG_Note, "------------"); host = SOCK_gethostbyname(name); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyname(\"%s\"): 0x%08X [%s]", name, (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); if ( host ) { name = SOCK_gethostbyaddr(host, buf, sizeof(buf)); if ( name ) { assert(name == buf); assert(0 < strlen(buf) && strlen(buf) < sizeof(buf)); CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): \"%s\"", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host), name)); } else { CORE_LOGF(eLOG_Note, ("SOCK_gethostbyaddr(0x%08X [%s]): <not found>", (unsigned int) SOCK_NetToHostLong(host), s_ntoa(host))); } } return host; }
struct hostent *gethostbyaddr(const char *addr, int len, int type) { struct SocketSettings *lss; switch (lss=_lx_get_socket_settings(),lss->lx_network_type) { case LX_AS225: return SOCK_gethostbyaddr(addr,len,type); case LX_AMITCP: return TCP_GetHostByAddr(addr,len,type); default: return NULL; } }
static void s_MakeFrom(char* buf, size_t buf_size) { size_t buf_len, hostname_len; if (!CORE_GetUsername(buf, buf_size) || !*buf) strncpy0(buf, "anonymous", buf_size - 1); buf_len = strlen(buf); hostname_len = buf_size - buf_len; if (hostname_len-- > 1) { buf[buf_len++] = '@'; if ((!SOCK_gethostbyaddr(0, &buf[buf_len], hostname_len) || !strchr(&buf[buf_len], '.')) && SOCK_gethostname(&buf[buf_len], hostname_len) != 0) { buf[--buf_len] = '\0'; } } }
const char* CORE_SendMailEx(const char* to, const char* subject, const char* body, const SSendMailInfo* uinfo) { static const STimeout zero = {0, 0}; const SSendMailInfo* info; SSendMailInfo ainfo; char buffer[1024]; SOCK sock = 0; info = uinfo ? uinfo : SendMailInfo_Init(&ainfo); if (info->magic_number != MX_MAGIC_NUMBER) SENDMAIL_RETURN(6, "Invalid magic number"); if ((!to || !*to) && (!info->cc || !*info->cc) && (!info->bcc || !*info->bcc)) { SENDMAIL_RETURN(7, "At least one message recipient must be specified"); } /* Open connection to sendmail */ if (SOCK_Create(info->mx_host, info->mx_port, &info->mx_timeout, &sock) != eIO_Success) { SENDMAIL_RETURN(8, "Cannot connect to sendmail"); } SOCK_SetTimeout(sock, eIO_ReadWrite, &info->mx_timeout); /* Follow the protocol conversation, RFC821 */ if (!SENDMAIL_READ_RESPONSE(220, 0, buffer)) SENDMAIL_RETURN2(9, "Protocol error in connection init", buffer); if ((!(info->mx_options & fSendMail_StripNonFQDNHost) || !SOCK_gethostbyaddr(0, buffer, sizeof(buffer))) && SOCK_gethostname(buffer, sizeof(buffer)) != 0) { SENDMAIL_RETURN(10, "Unable to get local host name"); } if (!s_SockWrite(sock, "HELO ", 0) || !s_SockWrite(sock, buffer, 0) || !s_SockWrite(sock, MX_CRLF, 2)) { SENDMAIL_RETURN(11, "Write error in HELO command"); } if (!SENDMAIL_READ_RESPONSE(250, 0, buffer)) SENDMAIL_RETURN2(12, "Protocol error in HELO command", buffer); if (!s_SockWrite(sock, "MAIL FROM: <", 0) || !s_SockWrite(sock, info->from, s_FromSize(info)) || !s_SockWrite(sock, ">" MX_CRLF, 1 + 2)) { SENDMAIL_RETURN(13, "Write error in MAIL command"); } if (!SENDMAIL_READ_RESPONSE(250, 0, buffer)) SENDMAIL_RETURN2(14, "Protocol error in MAIL command", buffer); if (to && *to) { const char* error = SENDMAIL_SENDRCPT("To", to, buffer); if (error) return error; } if (info->cc && *info->cc) { const char* error = SENDMAIL_SENDRCPT("Cc", info->cc, buffer); if (error) return error; } if (info->bcc && *info->bcc) { const char* error = SENDMAIL_SENDRCPT("Bcc", info->bcc, buffer); if (error) return error; } if (!s_SockWrite(sock, "DATA" MX_CRLF, 0)) SENDMAIL_RETURN(15, "Write error in DATA command"); if (!SENDMAIL_READ_RESPONSE(354, 0, buffer)) SENDMAIL_RETURN2(16, "Protocol error in DATA command", buffer); if (!(info->mx_options & fSendMail_NoMxHeader)) { /* Follow RFC822 to compose message headers. Note that * 'Date:'and 'From:' are both added by sendmail automagically. */ if (!s_SockWrite(sock, "Subject: ", 0) || (subject && !s_SockWrite(sock, subject, 0)) || !s_SockWrite(sock, MX_CRLF, 2)) SENDMAIL_RETURN(17, "Write error in sending subject"); if (to && *to) { if (!s_SockWrite(sock, "To: ", 0) || !s_SockWrite(sock, to, 0) || !s_SockWrite(sock, MX_CRLF, 2)) SENDMAIL_RETURN(18, "Write error in sending To"); } if (info->cc && *info->cc) { if (!s_SockWrite(sock, "Cc: ", 0) || !s_SockWrite(sock, info->cc, 0) || !s_SockWrite(sock, MX_CRLF, 2)) SENDMAIL_RETURN(19, "Write error in sending Cc"); } } else if (subject && *subject) CORE_LOG_X(2, eLOG_Warning, "[SendMail] Subject ignored in as-is messages"); if (!s_SockWrite(sock, "X-Mailer: CORE_SendMail (NCBI " NCBI_SENDMAIL_TOOLKIT " Toolkit)" MX_CRLF, 0)) { SENDMAIL_RETURN(20, "Write error in sending mailer information"); } assert(sizeof(buffer) > sizeof(MX_CRLF) && sizeof(MX_CRLF) >= 3); if (info->header && *info->header) { size_t n = 0, m = strlen(info->header); int/*bool*/ newline = 0/*false*/; while (n < m) { size_t k = 0; if (SOCK_Wait(sock, eIO_Read, &zero) != eIO_Timeout) break; while (k < sizeof(buffer) - sizeof(MX_CRLF)) { if (info->header[n] == '\n') { memcpy(&buffer[k], MX_CRLF, sizeof(MX_CRLF) - 1); k += sizeof(MX_CRLF) - 1; newline = 1/*true*/; } else { if (info->header[n] != '\r' || info->header[n+1] != '\n') buffer[k++] = info->header[n]; newline = 0/*false*/; } if (++n >= m) break; } buffer[k] = '\0'/*just in case*/; if (!s_SockWrite(sock, buffer, k)) SENDMAIL_RETURN(21, "Write error while sending custom header"); } if (n < m) SENDMAIL_RETURN(22, "Header write error"); if (!newline && !s_SockWrite(sock, MX_CRLF, 2)) SENDMAIL_RETURN(23, "Write error while finalizing custom header"); } if (body) { size_t n = 0, m = info->body_size ? info->body_size : strlen(body); int/*bool*/ newline = 0/*false*/; if (!(info->mx_options & fSendMail_NoMxHeader) && m) { if (!s_SockWrite(sock, MX_CRLF, 2)) SENDMAIL_RETURN(24, "Write error in message body delimiter"); } while (n < m) { size_t k = 0; if (SOCK_Wait(sock, eIO_Read, &zero) != eIO_Timeout) break; while (k < sizeof(buffer) - sizeof(MX_CRLF)) { if (body[n] == '\n') { memcpy(&buffer[k], MX_CRLF, sizeof(MX_CRLF) - 1); k += sizeof(MX_CRLF) - 1; newline = 1/*true*/; } else { if (body[n] != '\r' || (n+1 < m && body[n+1] != '\n')){ if (body[n] == '.' && (newline || !n)) { buffer[k++] = '.'; buffer[k++] = '.'; } else buffer[k++] = body[n]; } newline = 0/*false*/; } if (++n >= m) break; } buffer[k] = '\0'/*just in case*/; if (!s_SockWrite(sock, buffer, k)) SENDMAIL_RETURN(25, "Write error while sending message body"); } if (n < m) SENDMAIL_RETURN(26, "Body write error"); if ((!newline && m && !s_SockWrite(sock, MX_CRLF, 2)) || !s_SockWrite(sock, "." MX_CRLF, 1 + 2)) { SENDMAIL_RETURN(27, "Write error while finalizing message body"); } } else if (!s_SockWrite(sock, "." MX_CRLF, 1 + 2)) SENDMAIL_RETURN(28, "Write error while finalizing message"); if (!SENDMAIL_READ_RESPONSE(250, 0, buffer)) SENDMAIL_RETURN2(29, "Protocol error in sending message", buffer); if (!s_SockWrite(sock, "QUIT" MX_CRLF, 0)) SENDMAIL_RETURN(30, "Write error in QUIT command"); if (!SENDMAIL_READ_RESPONSE(221, 0, buffer)) SENDMAIL_RETURN2(31, "Protocol error in QUIT command", buffer); SOCK_Close(sock); return 0; }