|
|
|
@ -42,62 +42,63 @@ int smtp_checkuser(struct tlsport *p, struct ejabber_msg *m, const char *host)
|
|
|
|
|
int len, mlen, r;
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if(!m || !m->user) return -1;
|
|
|
|
|
if(!m->domain || !m->password) return -1;
|
|
|
|
|
if(!p || !p->ssl) return -1;
|
|
|
|
|
if(!host) return -1;
|
|
|
|
|
if(!m || !m->user) return 0;
|
|
|
|
|
if(!m->domain || !m->password) return 0;
|
|
|
|
|
if(!p || !p->ssl) return 0;
|
|
|
|
|
if(!host) return 0;
|
|
|
|
|
|
|
|
|
|
/* clear stack buffers */
|
|
|
|
|
len = mlen = r = 0;
|
|
|
|
|
len = mlen = 0;
|
|
|
|
|
memset(buf, 0, SMTPLINE_LENGTH);
|
|
|
|
|
r = 1;
|
|
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
if(len < 3) return 0;
|
|
|
|
|
else if(strncmp(buf, "220", 3)) return 0;
|
|
|
|
|
memset(buf, 0, len + sizeof(char));
|
|
|
|
|
|
|
|
|
|
/* start with hello */
|
|
|
|
|
mlen = strlen(host) + sizeof(char)*6;
|
|
|
|
|
if(mlen > SMTPLINE_LENGTH - sizeof(char)) return -1; /* too long message */
|
|
|
|
|
if(mlen > SMTPLINE_LENGTH - sizeof(char)) return 0; /* too long message */
|
|
|
|
|
snprintf(buf, mlen + sizeof(char), "HELO %s\n", host);
|
|
|
|
|
len = tls_io(p, buf, mlen, TLSIO_WRITE);
|
|
|
|
|
|
|
|
|
|
if(len < 0) return -1;
|
|
|
|
|
if(len < 0) return 0;
|
|
|
|
|
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;
|
|
|
|
|
if(len < 4) return 0;
|
|
|
|
|
else if(strncmp(buf, "250 ", 4)) return 0;
|
|
|
|
|
memset(buf, 0, len + sizeof(char));
|
|
|
|
|
|
|
|
|
|
/* set mail from */
|
|
|
|
|
mlen = strlen(m->user) + strlen(m->domain) + 14*sizeof(char);
|
|
|
|
|
if(mlen > SMTPLINE_LENGTH - sizeof(char)) return -1; /* too long message */
|
|
|
|
|
if(mlen > SMTPLINE_LENGTH - sizeof(char)) return 0; /* too long message */
|
|
|
|
|
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;
|
|
|
|
|
if(len < 0) return 0;
|
|
|
|
|
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;
|
|
|
|
|
if(len < 4) return 0;
|
|
|
|
|
else if(strncmp(buf, "250 ", 4)) return 0;
|
|
|
|
|
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) + 12*sizeof(char);
|
|
|
|
|
if(mlen > SMTPLINE_LENGTH - sizeof(char)) return -1; /* too long message */
|
|
|
|
|
if(mlen > SMTPLINE_LENGTH - sizeof(char)) return 0; /* too long message */
|
|
|
|
|
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;
|
|
|
|
|
if(len < 0) return 0;
|
|
|
|
|
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;
|
|
|
|
|
if(len < 4) return 0;
|
|
|
|
|
else if(strncmp(buf, "250 ", 4)) r = 0;
|
|
|
|
|
|
|
|
|
|
/* gracefully end smtp session */
|
|
|
|
|
mlen = strlen("quit\n");
|
|
|
|
|