spsmtp.cpp 12.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/*
 * Copyright 2009 Stephen Liu
 * For license terms, see the file COPYING along with this library.
 */

#include <string.h>
#include <stdio.h>
#include <ctype.h>

#include "spsmtp.hpp"

#include "spbuffer.hpp"
#include "sphandler.hpp"
#include "sprequest.hpp"
#include "spresponse.hpp"
#include "spmsgdecoder.hpp"
#include "sputils.hpp"

SP_SmtpHandler :: ~SP_SmtpHandler()
{
}

void SP_SmtpHandler :: error()
{
}

void SP_SmtpHandler :: timeout()
{
}

int SP_SmtpHandler :: welcome( const char * clientIP, const char * serverIP, SP_Buffer * reply )
{
	reply->append( "220 SMTP service ready\n" );

	return eAccept;
}

int SP_SmtpHandler :: help( const char * args, SP_Buffer * reply )
{
	reply->append( "250 HELP HELO EHLO MAIL RCPT DATA NOOP RSET QUIT\r\n" );

	return eAccept;
}

int SP_SmtpHandler :: helo( const char * args, SP_Buffer * reply )
{
	reply->append( "250 OK\n" );

	return eAccept;
}

int SP_SmtpHandler :: ehlo( const char * args, SP_Buffer * reply )
{
	reply->append( "250-OK\n" );
	reply->append( "250-AUTH=LOGIN\n" );
	reply->append( "250 HELP\n" );

	return eAccept;
}

int SP_SmtpHandler :: auth( const char * user, const char * pass, SP_Buffer * reply )
{
	reply->append( "535 Authenticate failed\n" );

	return eReject;
}

int SP_SmtpHandler :: noop( const char * args, SP_Buffer * reply )
{
	reply->append( "250 OK\n" );

	return eAccept;
}

//---------------------------------------------------------

SP_SmtpHandlerList :: SP_SmtpHandlerList()
{
	mList = new SP_ArrayList();
}

SP_SmtpHandlerList :: ~SP_SmtpHandlerList()
{
	for( int i = 0; i < mList->getCount(); i++ ) {
		delete (SP_SmtpHandler*)mList->getItem(i);
	}

	delete mList, mList = NULL;
}

int SP_SmtpHandlerList :: getCount()
{
	return mList->getCount();
}

void SP_SmtpHandlerList :: append( SP_SmtpHandler * handler )
{
	mList->append( handler );
}

SP_SmtpHandler * SP_SmtpHandlerList :: getItem( int index )
{
	return (SP_SmtpHandler*)mList->getItem( index );
}

//---------------------------------------------------------

SP_SmtpHandlerFactory :: ~SP_SmtpHandlerFactory()
{
}

//---------------------------------------------------------

class SP_SmtpSession {
public:
	SP_SmtpSession( SP_SmtpHandlerFactory * handlerFactory );
	~SP_SmtpSession();

	enum { eStepOther = 0, eStepUser = 1, eStepPass = 2 };
	void setAuthStep( int step );
	int  getAuthStep();

	void setUser( const char * user );
	const char * getUser();

	void setPass( const char * pass );
	const char * getPass();

	void setSeenAuth( int seenAuth );
	int  getSeenAuth();

	void setSeenHelo( int seenHelo );
	int  getSeenHelo();

	void setSeenSender( int seenSender );
	int  getSeenSender();

	void addRcpt();
	int  getRcptCount();

	void setDataMode( int mode );
	int  getDataMode();

	int  getSeenData();

	void reset();

	SP_SmtpHandler * getHandler();

private:
	int mAuthStep;
	int mSeenAuth;

	char mUser[ 128 ], mPass[ 128 ];

	int mSeenHelo;
	int mSeenSender;
	int mRcptCount;
	int mSeenData;

	int mDataMode;

	SP_SmtpHandler * mHandler;

	SP_SmtpHandlerFactory * mHandlerFactory;
};

SP_SmtpSession :: SP_SmtpSession( SP_SmtpHandlerFactory * handlerFactory )
{
	mHandler = NULL;
	mHandlerFactory = handlerFactory;

	mSeenHelo = 0;
	mSeenAuth = 0;
	mAuthStep = eStepOther;

	memset( mUser, 0, sizeof( mUser ) );
	memset( mPass, 0, sizeof( mPass ) );

	reset();
}

SP_SmtpSession :: ~SP_SmtpSession()
{
	if( NULL != mHandler ) delete mHandler;
}

void SP_SmtpSession :: reset()
{
	mSeenSender = 0;
	mRcptCount = 0;
	mSeenData = 0;

	mDataMode = 0;
}

void SP_SmtpSession :: setAuthStep( int step )
{
	mAuthStep = step;
}

int  SP_SmtpSession :: getAuthStep()
{
	return mAuthStep;
}

void SP_SmtpSession :: setUser( const char * user )
{
	sp_strlcpy( mUser, user, sizeof( mUser ) );
}

const char * SP_SmtpSession :: getUser()
{
	return mUser;
}

void SP_SmtpSession :: setPass( const char * pass )
{
	sp_strlcpy( mPass, pass, sizeof( mPass ) );
}

const char * SP_SmtpSession :: getPass()
{
	return mPass;
}

void SP_SmtpSession :: setSeenAuth( int seenAuth )
{
	mSeenAuth = seenAuth;
}

int  SP_SmtpSession :: getSeenAuth()
{
	return mSeenAuth;
}

void SP_SmtpSession :: setSeenHelo( int seenHelo )
{
	mSeenHelo = seenHelo;
}

int  SP_SmtpSession :: getSeenHelo()
{
	return mSeenHelo;
}

void SP_SmtpSession :: setSeenSender( int seenSender )
{
	mSeenSender = seenSender;
}

int  SP_SmtpSession :: getSeenSender()
{
	return mSeenSender;
}

void SP_SmtpSession :: addRcpt()
{
	mRcptCount++;
}

int  SP_SmtpSession :: getRcptCount()
{
	return mRcptCount;
}

void SP_SmtpSession :: setDataMode( int mode )
{
	mDataMode = mode;

	if( 1 == mode ) mSeenData = 1;
}

int  SP_SmtpSession :: getDataMode()
{
	return mDataMode;
}

int  SP_SmtpSession :: getSeenData()
{
	return mSeenData;
}

SP_SmtpHandler * SP_SmtpSession :: getHandler()
{
	if( NULL == mHandler ) mHandler = mHandlerFactory->create();

	return mHandler;
}

//---------------------------------------------------------

class SP_SmtpHandlerAdapter : public SP_Handler {
public:
	SP_SmtpHandlerAdapter( SP_SmtpHandlerFactory * handlerFactory );

	virtual ~SP_SmtpHandlerAdapter();

	// return -1 : terminate session, 0 : continue
	virtual int start( SP_Request * request, SP_Response * response );

	// return -1 : terminate session, 0 : continue
	virtual int handle( SP_Request * request, SP_Response * response );

	virtual void error( SP_Response * response );

	virtual void timeout( SP_Response * response );

	virtual void close();

private:
	SP_SmtpSession * mSession;
};

SP_SmtpHandlerAdapter :: SP_SmtpHandlerAdapter( SP_SmtpHandlerFactory * handlerFactory )
{
	mSession = new SP_SmtpSession( handlerFactory );
}

SP_SmtpHandlerAdapter :: ~SP_SmtpHandlerAdapter()
{
	delete mSession;
}

int SP_SmtpHandlerAdapter :: start( SP_Request * request, SP_Response * response )
{
	SP_Buffer * reply = response->getReply()->getMsg();

	int ret = mSession->getHandler()->welcome( request->getClientIP(),
			request->getServerIP(), reply );

	if( NULL == reply->find( "\n", 1 ) ) reply->append( "\n" );

	request->setMsgDecoder( new SP_LineMsgDecoder() );

	return ret;
}

int SP_SmtpHandlerAdapter :: handle( SP_Request * request, SP_Response * response )
{
	int ret = SP_SmtpHandler::eAccept;

	SP_Buffer * reply = response->getReply()->getMsg();

	if( mSession->getDataMode() ) {
		SP_DotTermChunkMsgDecoder * decoder = (SP_DotTermChunkMsgDecoder*)request->getMsgDecoder();

		char * data = (char*)decoder->getMsg();
		ret = mSession->getHandler()->data( data, reply );
		free( data );

		mSession->setDataMode( 0 );
		request->setMsgDecoder( new SP_LineMsgDecoder() );

	} else if( SP_SmtpSession::eStepUser == mSession->getAuthStep() ) {
		const char * line = ((SP_LineMsgDecoder*)(request->getMsgDecoder()))->getMsg();
		mSession->setUser( line );
		mSession->setAuthStep( SP_SmtpSession::eStepPass );
		reply->append( "334 UGFzc3dvcmQ6\r\n" );

	} else if( SP_SmtpSession::eStepPass == mSession->getAuthStep() ) {
		const char * line = ((SP_LineMsgDecoder*)(request->getMsgDecoder()))->getMsg();
		mSession->setPass( line );
		mSession->setAuthStep( SP_SmtpSession::eStepOther );
		ret = mSession->getHandler()->auth( mSession->getUser(), mSession->getPass(), reply );
		if( SP_SmtpHandler::eAccept == ret ) mSession->setSeenAuth( 1 );

	} else {
		const char * line = ((SP_LineMsgDecoder*)(request->getMsgDecoder()))->getMsg();

		char cmd[ 128 ] = { 0 };
		const char * args = NULL;

		sp_strtok( line, 0, cmd, sizeof( cmd ), ' ', &args );

		if( 0 == strcasecmp( cmd, "EHLO" ) ) {
			if( NULL != args ) {
				if( 0 == mSession->getSeenHelo() ) {
					ret = mSession->getHandler()->ehlo( args, reply );
					if( SP_SmtpHandler::eAccept == ret ) mSession->setSeenHelo( 1 );
				} else {
					reply->append( "503 Duplicate EHLO\r\n" );
				}
			} else {
				reply->append( "501 Syntax: EHLO <hostname>\r\n" );
			}

		} else if( 0 == strcasecmp( cmd, "AUTH" ) ) {
			if( 0 == mSession->getSeenHelo() ) {
				reply->append( "503 Error: send EHLO first\r\n" );
			} else if( mSession->getSeenAuth() ) {
				reply->append( "503 Duplicate AUTH\r\n" );
			} else {
				if( NULL != args ) {
					if( 0 == strcasecmp( args, "LOGIN" ) ) {
						reply->append( "334 VXNlcm5hbWU6\r\n" );
						mSession->setAuthStep( SP_SmtpSession::eStepUser );
					} else {
						reply->append( "504 Unrecognized authentication type.\r\n" );
					}
				} else {
					reply->append( "501 Syntax: AUTH LOGIN\r\n" );
				}
			}

		} else if( 0 == strcasecmp( cmd, "HELO" ) ) {
			if( NULL != args ) {
				if( 0 == mSession->getSeenHelo() ) {
					ret = mSession->getHandler()->helo( args, reply );
					if( SP_SmtpHandler::eAccept == ret ) mSession->setSeenHelo( 1 );
				} else {
					reply->append( "503 Duplicate HELO\r\n" );
				}
			} else {
				reply->append( "501 Syntax: HELO <hostname>\r\n" );
			}

		} else if( 0 == strcasecmp( cmd, "MAIL" ) ) {
			if( 0 == mSession->getSeenHelo() ) {
				reply->append( "503 Error: send HELO first\r\n" );
			} else if( mSession->getSeenSender() ) {
				reply->append( "503 Sender already specified.\r\n" );
			} else {
				if( NULL != args ) {
					if( 0 == strncasecmp( args, "FROM:", 5 ) ) args += 5;
					for( ; isspace( *args ); ) args++;
					ret = mSession->getHandler()->from( args, reply );
					if( SP_SmtpHandler::eAccept == ret ) mSession->setSeenSender( 1 );
				} else {
					reply->append( "501 Syntax: MAIL FROM:<address>\r\n" );
				}
			}

		} else if( 0 == strcasecmp( cmd, "RCPT" ) ) {
			if( 0 == mSession->getSeenHelo() ) {
				reply->append( "503 Error: send HELO first\r\n" );
			} else if( 0 == mSession->getSeenSender() ) {
				reply->append( "503 Error: need MAIL command\r\n" );
			} else if( mSession->getSeenData() ) {
				reply->append( "503 Bad sequence of commands\r\n" );
			} else {
				if( NULL != args ) {
					if( 0 == strncasecmp( args, "TO:", 3 ) ) args += 3;
					for( ; isspace( *args ); ) args++;
					ret = mSession->getHandler()->rcpt( args, reply );
					if( SP_SmtpHandler::eAccept == ret ) mSession->addRcpt();
				} else {
					reply->append( "501 Syntax: RCPT TO:<address>\r\n" );
				}
			}

		} else if( 0 == strcasecmp( cmd, "DATA" ) ) {
			if( 0 == mSession->getSeenHelo() ) {
				reply->append( "503 Error: send HELO first\r\n" );
			} else if( 0 == mSession->getSeenSender() ) {
				reply->append( "503 Error: need MAIL command\r\n" );
			} else if( mSession->getRcptCount() <= 0 ) {
				reply->append( "503 Error: need RCPT command\r\n" );
			} else {
				request->setMsgDecoder( new SP_DotTermChunkMsgDecoder() );
				reply->append( "354 Start mail input; end with <CRLF>.<CRLF>\r\n" );
				mSession->setDataMode( 1 );
			}

		} else if( 0 == strcasecmp( cmd, "RSET" ) ) {
			ret = mSession->getHandler()->rset( reply );
			mSession->reset();

		} else if( 0 == strcasecmp( cmd, "NOOP" ) ) {
			ret = mSession->getHandler()->noop( args, reply );

		} else if( 0 == strcasecmp( cmd, "HELP" ) ) {
			ret = mSession->getHandler()->help( args, reply );

		} else if( 0 == strcasecmp( cmd, "QUIT" ) ) {
			reply->append( "221 Closing connection. Good bye.\r\n" );
			ret = SP_SmtpHandler::eClose;

		} else {
			reply->printf( "500 Syntax error, command unrecognized <%s>.\r\n", cmd );
		}
	}

	if( NULL == reply->find( "\n", 1 ) ) reply->append( "\n" );

	return SP_SmtpHandler::eClose == ret ? -1 : 0;
}

void SP_SmtpHandlerAdapter :: error( SP_Response * response )
{
	mSession->getHandler()->error();
}

void SP_SmtpHandlerAdapter :: timeout( SP_Response * response )
{
	mSession->getHandler()->timeout();
}

void SP_SmtpHandlerAdapter :: close()
{
}

//---------------------------------------------------------

SP_SmtpHandlerAdapterFactory :: SP_SmtpHandlerAdapterFactory( SP_SmtpHandlerFactory * factory )
{
	mFactory = factory;
}

SP_SmtpHandlerAdapterFactory :: ~SP_SmtpHandlerAdapterFactory()
{
	delete mFactory;
}

SP_Handler * SP_SmtpHandlerAdapterFactory :: create() const
{
	return new SP_SmtpHandlerAdapter( mFactory );
}