Carriage Returns In SMTP Server Responses

Discussion to talk about software related topics only.
kackle123
Posts: 73
Joined: Tue Nov 23, 2010 1:35 pm

Carriage Returns In SMTP Server Responses

Post by kackle123 »

I made a commercial product based on a NetBurner - it has been selling for years. It uses email, but I've found that it doesn't work with the Microsoft Office 365 cloud-based email servers. After the EHLO command verb, their servers' multi-lined response has 2 carriage returns followed by a line feed after each line (CR CR LF, or 0x0D 0x0D 0x0A), which confuses NNDK's code (I altered the formatting here for clarity):

Code: Select all

/* From ssl_mailto.cpp... */

/*
code result is  nnn- xxxxx\r\n
until nnn is a space...
*/
int ReadMultiLineResponse( int fd,  char * buffer, int size, DWORD timeout ) {

    DWORD now=TimeTick;
    int count=0;
    char *  LastLine=NULL;
    int state=0;
    
    while(1) {

        int  rv = ReadWithTimeout( fd, buffer+count, size-count, timeout );
        
        if(rv>0) {

            SaveToMailLog(buffer+count, rv);
            if(LastLine==NULL) LastLine=buffer;

            for(int i=0; i<rv; i++) {

                if(buffer[count+i]=='\r')  // ONLY CONSIDERS ONE <CR>.
                    state=1;
                else
                    if ((buffer[count+i]=='\n') && (state==1)) { //We are reading \r\n

                        state=2;
                        //was the last line xxx- or xxx
                        char * cpe=buffer+count+i;
                        char * cpscan=LastLine;
  
                        while ((*cpscan>='0') && (*cpscan<='9') && (cpscan<cpe)) cpscan++;
      
                        if(*cpscan==' ') return  count+rv;
  
                    } else {

                        if(state==2) { LastLine=buffer+count+i; }
                        state=0;

                    }

            }

            count+=rv;
            if(count>=(size-1)) return count;

        } else  // if(!(rv>0))...
            return rv;

        if ((timeout) && ((now+timeout)<TimeTick)) return count;

    }  // End of while(1).

    return count;

}  // End of ReadMultiLineResponse().
RFC 5321, Section 4.2.1, demands only 1 CR and 1 LF from the server. When I telnet into the server from a Windows machine (while 'logging all' with terminal emulator software "putty" and reading its logs using the tool "HxD"), everything looks proper. So I assume the NNDK is adding a CR somewhere deep in its TCP code (since the Windows server is unable to discern the distant OS via only its socket?), but my admittedly brief attempt at a NNDK code search got me lost.

Microsoft support tells me that there are sometimes issues with "newlines" between disparate systems (like NNDK's GNU/GCC and Microsoft's Windows server). This seems to be supported by Wikipedia's newline article.

Before I start hacking at NNDK's internals to bandage it, I wanted others' takes on whether my thoughts are correct, and whether I should deal with it differently. Thank you!
Last edited by kackle123 on Mon Apr 04, 2016 11:32 am, edited 2 times in total.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Carriage Returns In SMTP Server Responses

Post by pbreed »

I vaguely remember a bug about this...
Make sure you try the 2.7.5 beta before doing any real work.
kackle123
Posts: 73
Joined: Tue Nov 23, 2010 1:35 pm

Re: Carriage Returns In SMTP Server Responses

Post by kackle123 »

I tried recompiling under NNDK 2.7.5 beta, but there was no difference.

Code: Select all

'SMTP server failed to reply correctly to "HELO".  Closing connection...'
'Any other thoughts?
kackle123
Posts: 73
Joined: Tue Nov 23, 2010 1:35 pm

Re: Carriage Returns In SMTP Server Responses

Post by kackle123 »

Anyone? Bueller?
User avatar
dciliske
Posts: 623
Joined: Mon Feb 06, 2012 9:37 am
Location: San Diego, CA
Contact:

Re: Carriage Returns In SMTP Server Responses

Post by dciliske »

Bueller is currently poking strange corners in printf. He'll take a look at this after the Porsche is back in the garage.

-Dan
Dan Ciliske
Project Engineer
Netburner, Inc
kackle123
Posts: 73
Joined: Tue Nov 23, 2010 1:35 pm

Re: Carriage Returns In SMTP Server Responses

Post by kackle123 »

Recalling the end of the movie, I'm not sure that's possible.
kackle123
Posts: 73
Joined: Tue Nov 23, 2010 1:35 pm

Re: Carriage Returns In SMTP Server Responses

Post by kackle123 »

I made a minor, code-reading mistake. The code above does seem to handle erroneous carriage returns (CR) by looping back around and then checking for a CR again. However, it is locking up here (timing out apparently since it doesn't see what it wants: "250 xxxx...").

I'm driving my damaged Ferrari to paid tech support for now, and will report back here when it's resolved.
Armin.Paya@eltek.com
Posts: 4
Joined: Thu Jun 11, 2015 4:13 pm

Re: Carriage Returns In SMTP Server Responses

Post by Armin.Paya@eltek.com »

I may be running into a similar issue in implementing a TL1 server using Telnet with Netburner. TL1 requires <cr><lf><lf> in a response, but I am giving <cr><cr><lf><cr><lf>. I have tried fputs, fputc, and fwrite but am getting no changes in the received output. When I look at the buffer feeding the fputs transmit, I have (0x0d, 0x0a, 0x0a, ...) which is exactly what should be there.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Carriage Returns In SMTP Server Responses

Post by rnixon »

The server might be doing something unexpected. What do you see in the data stream in a wireshark capture of the transmit?
Armin.Paya@eltek.com
Posts: 4
Joined: Thu Jun 11, 2015 4:13 pm

Re: Carriage Returns In SMTP Server Responses

Post by Armin.Paya@eltek.com »

I am seeing <cr><cr><lf><cr><lf> in the Wireshark transcript for Frame:4340 in response to "meas-vg::all:10::;" at the end of the dialog.
Attachments
TL1_Login_meas-vg.pcapng
I am seeing <cr><cr><lf><cr><lf> in the Wireshark transcript for Frame:4340 in response to "meas-vg::all:10::;"
(806.24 KiB) Downloaded 249 times
Post Reply