diff --git a/src/smtp.c b/src/smtp.c index 97617bc..33ef9f4 100644 --- a/src/smtp.c +++ b/src/smtp.c @@ -51,45 +51,57 @@ int smtp_checkuser(struct tlsport *p, struct ejabber_msg *m, const char *host) len = mlen = r = 0; memset(buf, 0, SMTPLINE_LENGTH); + /* read greetings, shall be 220 for error code */ + len = tls_io(p, buf, SMTPLINE_LENGTH - sizeof(char), TLSIO_READ); + if(len < 3) return -1; + else if(strncmp(buf, "220", 3)) return -1; + memset(buf, 0, len + sizeof(char)); + /* start with hello */ - mlen = strlen(host) + sizeof(char)*5; + mlen = strlen(host) + sizeof(char)*6; if(mlen > SMTPLINE_LENGTH - sizeof(char)) return -1; /* too long message */ - snprintf(buf, mlen + sizeof(char), "HELO %s", host); + snprintf(buf, mlen + sizeof(char), "HELO %s\n", host); len = tls_io(p, buf, mlen, TLSIO_WRITE); + if(len < 0) return -1; else memset(buf, 0, mlen); + /* check the reply if any */ len = tls_io(p, buf, SMTPLINE_LENGTH - sizeof(char), TLSIO_READ); if(len < 4) return -1; else if(strncmp(buf, "250 ", 4)) return -1; + memset(buf, 0, len + sizeof(char)); /* set mail from */ - mlen = strlen(m->user) + strlen(m->domain) + 15*sizeof(char); + mlen = strlen(m->user) + strlen(m->domain) + 14*sizeof(char); if(mlen > SMTPLINE_LENGTH - sizeof(char)) return -1; /* too long message */ - snprintf(buf, mlen + sizeof(char), "mail from:<%s@%s>", m->user, m->domain); + snprintf(buf, mlen + sizeof(char), "mail from:<%s@%s>\n", m->user, m->domain); len = tls_io(p, buf, mlen, TLSIO_WRITE); + if(len < 0) return -1; else memset(buf, 0, mlen); /* check the reply if any */ len = tls_io(p, buf, SMTPLINE_LENGTH - sizeof(char), TLSIO_READ); if(len < 4) return -1; else if(strncmp(buf, "250 ", 4)) return -1; + memset(buf, 0, len + sizeof(char)); /* set recepient, if it's ok - user exists, if it's not - no such user */ - mlen = strlen(m->user) + strlen(m->domain) + 11*sizeof(char); + mlen = strlen(m->user) + strlen(m->domain) + 12*sizeof(char); if(mlen > SMTPLINE_LENGTH - sizeof(char)) return -1; /* too long message */ - snprintf(buf, mlen + sizeof(char), "rcpt to:<%s@%s>", m->user, m->domain); + snprintf(buf, mlen + sizeof(char), "rcpt to:<%s@%s>\n", m->user, m->domain); len = tls_io(p, buf, mlen, TLSIO_WRITE); if(len < 0) return -1; else memset(buf, 0, mlen); + /* check the reply if any */ len = tls_io(p, buf, SMTPLINE_LENGTH - sizeof(char), TLSIO_READ); if(len < 4) return -1; else if(strncmp(buf, "250 ", 4)) r = -1; /* gracefully end smtp session */ - mlen = strlen("quit"); - snprintf(buf, mlen + sizeof(char), "quit"); + mlen = strlen("quit\n"); + snprintf(buf, mlen + sizeof(char), "quit\n"); tls_io(p, buf, mlen, TLSIO_WRITE); return r;