protected override string BeforeJob(SockTcp client, List <byte[]> clientBuf) { Protocol = MailProxyProtocolKind.Smtp; //挨拶文をサーバに変わって送出する client.AsciiSend("220 SMTP-Proxy"); while (clientBuf.Count < 5) { var buf = client.LineRecv(Timeout, this); if (buf == null) { return(null);//タイムアウト } //Ver5.8.6 //var str = Inet.TrimCrlf(Encoding.ASCII.GetString(buf)); buf = Inet.TrimCrlf(buf); var str = Encoding.ASCII.GetString(buf); //Ver5,3,4 RESTコマンドは蓄積がプロトコル上できないのでサーバへは送らない if (str.ToUpper().IndexOf("RSET") != 0) { clientBuf.Add(buf); } if (str.ToUpper().IndexOf("QUIT") != -1) { return(null); } if (clientBuf.Count > 1) { if (str.ToUpper().IndexOf("MAIL FROM:") != -1) { var mailAddress = str.Substring(str.IndexOf(":") + 1); mailAddress = mailAddress.Trim(); mailAddress = mailAddress.Trim(new[] { '<', '>' }); return(mailAddress);//メールアドレス } } client.AsciiSend("250 OK"); } return(null); }
bool SendCmd(string cmdStr) { //AsciiSendは、内部でCRLFを追加する if (cmdStr.Length + 2 != _sockTcp.AsciiSend(cmdStr)) { SetLastError(String.Format("Faild in PopClient SendCmd({0})", cmdStr)); ConfirmConnect();//接続確認 return(false); } return(true); }
void AuthError(SockTcp sockTcp, string user, string pass) { Logger.Set(LogKind.Secure, sockTcp, 3, string.Format("user={0} pass={1}", user, pass)); // �F�̃G���[�͂����ɕԓ���Ԃ��Ȃ� var authTimeout = (int)Conf.Get("authTimeout"); for (int i = 0; i < (authTimeout * 10) && IsLife(); i++) { Thread.Sleep(100); } sockTcp.AsciiSend(string.Format("-ERR Password supplied for {0} is incorrect.", user)); }
protected override string BeforeJob(SockTcp client, List <byte[]> clientBuf) { Protocol = MailProxyProtocol.Pop3; //挨拶文をサーバに変わって送出する client.AsciiSend("+OK "); //USER コマンドを受け付けるまでループ(最大5回)する for (var i = 0; i < 5; i++) { var buf = client.LineRecv(Timeout, this); if (buf != null) { var str = Inet.TrimCrlf(Encoding.ASCII.GetString(buf)); if (str.ToUpper().IndexOf("USER") == 0) { clientBuf.Add(buf); var tmp = str.Split(' '); if (tmp.Length >= 2) { return(tmp[1]);//ユーザ名 } } else if (str.ToUpper().IndexOf("QUIT") == 0) { return(null); } else { client.AsciiSend("-ERR "); } } else { Thread.Sleep(300); } } return(null); }
bool Login(SockTcp sockTcp, ref Pop3LoginState mode, ref MessageList messageList, string user, Ip addr) { //var folder = Kernel.MailBox.Login(user, addr); if (!Kernel.MailBox.Login(user, addr)) { Logger.Set(LogKind.Secure, sockTcp, 1, string.Format("user={0}", user)); sockTcp.AsciiSend("-ERR Double login"); return(false); } var folder = string.Format("{0}\\{1}", Kernel.MailBox.Dir, user); messageList = new MessageList(folder);//������ //if (kernel.MailBox.Login(user, addr)) {//POP before SMTP�̂��߂ɁA�Ō�̃��O�C���A�h���X��ۑ����� mode = Pop3LoginState.Login; Logger.Set(LogKind.Normal, sockTcp, 2, string.Format("User {0} from {1}[{2}]", user, sockTcp.RemoteHostname, sockTcp.RemoteAddress.Address)); // LOGIN //dfList = kernel.MailBox.GetDfList(user); sockTcp.AsciiSend(string.Format("+OK {0} has {1} message ({2} octets).", user, messageList.Count, messageList.Size)); return(true); }
//通常はこれを使用する public bool Recv(SockTcp sockTcp, int sec, Logger logger, ILife iLife) { var dtLast = DateTime.Now; //受信が20秒無かった場合は、処理を中断する while (iLife.IsLife()) { if (dtLast.AddSeconds(sec) < DateTime.Now) { return(false); //タイムアウト } var len = sockTcp.Length(); if (len == 0) { continue; } var buf = sockTcp.Recv(len, sec, iLife); if (buf == null) { return(false); //切断された } dtLast = DateTime.Now; var recvStatus = Append(buf); if (recvStatus == RecvStatus.Limit) { //サイズ制限 if (logger != null) { logger.Set(LogKind.Secure, sockTcp, 7, string.Format("Limit:{0}KByte", _sizeLimit)); } sockTcp.AsciiSend("552 Requested mail action aborted: exceeded storage allocation"); return(false); } if (recvStatus == RecvStatus.Finish) { return(true); } } return(false); }
//�ڑ��P�ʂ̏��� override protected void OnSubThread(SockObj sockObj) { var timeout = (int)Conf.Get("timeOut"); var client = (SockTcp)sockObj; SockTcp server = null; var user = ""; //���[�U�� string pass; //�p�X���[�h var hostName = ""; //�z�X�g�� //*************************************************************** //�O�����i�ڑ���E���[�U���E�p�X���[�h�̎擾) //*************************************************************** { var str = string.Format("220 {0} {1}", Define.ApplicationName(), Define.Copyright()); client.AsciiSend(str); var cmdStr = ""; var paramStr = ""; //wait USER user@hostName if (!WaitLine(client, ref cmdStr, ref paramStr)) { goto end; } if (cmdStr.ToUpper() != "USER") { goto end; } //paramStr = "user@hostName" if (paramStr != null) { //string[] tmp = paramStr.Split('@'); //if(tmp.Length == 2) { // user = tmp[0];//���[�U���擾 // hostName = tmp[1];//�z�X�g���擾 //} var i = paramStr.LastIndexOf('@'); if (i != -1) { user = paramStr.Substring(0, i); //���[�U���擾 hostName = paramStr.Substring(i + 1); //�z�X�g���擾 } } if (hostName == "") { Logger.Set(LogKind.Error, sockObj, 8, ""); goto end; } client.AsciiSend("331 USER OK enter password"); //wait PASS password if (!WaitLine(client, ref cmdStr, ref paramStr)) { goto end; } if (cmdStr.ToUpper() != "PASS") { goto end; } //paramStr = "password" pass = paramStr;//�p�X���[�h�擾 } //*************************************************************** // �T�[�o�Ƃ̐ڑ� //*************************************************************** { const int port = 21; //var ipList = new List<Ip>{new Ip(hostName)}; //if (ipList[0].ToString() == "0.0.0.0") { // ipList = Kernel.DnsCache.Get(hostName); // if (ipList.Count == 0) { // goto end; // } //} var ipList = Kernel.GetIpList(hostName); if (ipList.Count == 0) { goto end; } Ssl ssl = null; foreach (var ip in ipList) { server = Inet.Connect(Kernel, ip, port, Timeout, ssl); if (server != null) { break; } } if (server == null) { goto end; } } //*************************************************************** //�㏈���i���[�U���E�p�X���[�h�̑��M) //*************************************************************** { var cmdStr = ""; var paramStr = ""; //wait 220 welcome while (cmdStr != "220") { if (!WaitLine(server, ref cmdStr, ref paramStr)) { goto end; } } server.AsciiSend(string.Format("USER {0}", user)); //wait 331 USER OK enter password while (cmdStr != "331") { if (!WaitLine(server, ref cmdStr, ref paramStr)) { goto end; } } server.AsciiSend(string.Format("PASS {0}", pass)); if (!WaitLine(server, ref cmdStr, ref paramStr)) { goto end; } client.AsciiSend(string.Format("{0} {1}", cmdStr, paramStr)); } //*************************************************************** // �p�C�v //*************************************************************** var ftpTunnel = new FtpTunnel(Kernel, Logger, (int)Conf.Get("idleTime"), _dataPort, timeout); //Ver5.0.5 //ftpTunnel.BytePipe(ref life, server,client); ftpTunnel.Pipe(server, client, this); _dataPort = ftpTunnel.Dispose(); if (_dataPort > DataPortMax) { _dataPort = DataPortMin; } end: client.Close(); if (server != null) { server.Close(); } }
//string esmtpUser��null�łȂ��ꍇ�ASMTP�F��g�p���� public SmtpClientResult Send(SockTcp sockTcp, string serverName, Mail mail, MailAddress from, MailAddress to, string authUser, string authPass, ILife iLife) { var state = State.Ehlo; const int timeout = 3; var result = SmtpClientResult.Faild; //AUTH_STATE authState = AUTH_STATE.LOGIN; var smtpAuthClient = new SmtpAuthClient(authUser, authPass); LastLog.Clear();//���M���s���̋L�^�̓N���A���� while (iLife.IsLife()) { //******************************************************************** // �T�[�o����̃��X�|���X�R�[�h(response)��M //******************************************************************** int response; //var recvBuf = sockTcp.LineRecv(timeout,OperateCrlf.No,ref life); //Ver5.7.3 �^�C���A�E�g���������āA�Ԏ��̒x���T�[�o�ŃG���[�ƂȂ��Ă��܂� var recvBuf = sockTcp.LineRecv(timeout + 30, iLife); if (recvBuf == null) { //���M���s���̍Ō�̑���M�L�^ LastLog.Add(sockTcp.LastLineSend); //LastLog.Add(recvStr); break; } if (recvBuf.Length == 0) { Thread.Sleep(10); continue; } recvBuf = Inet.TrimCrlf(recvBuf);//\r\n�̔r�� var recvStr = Encoding.ASCII.GetString(recvBuf); if (state == State.Ehlo) { smtpAuthClient.Ehlo(recvStr);//AUTH�̑Ή���擾 } if (recvStr[3] == '-') { //string paramStr = recvStr.Substring(4); continue; } if (recvStr.IndexOf(' ') == 3) { response = Convert.ToInt32(recvStr.Substring(0, 3)); } else { //���M���s���̍Ō�̑���M�L�^ LastLog.Add(sockTcp.LastLineSend); LastLog.Add(recvStr); break; } //******************************************************************** // ��M�������X�|���X�R�[�h(response)�ɂ����(mode)�̕ύX //******************************************************************** if (response == 220) { state = State.Ehlo; } else if (response == 221) { if (state == State.Quit) { break; } } else if (response == 250) { if (state == State.Ehlo || state == State.Helo) { state = State.Mail; } else if (state == State.Mail) { state = State.Rcpt; } else if (state == State.Rcpt) { state = State.Data; } else if (state == State.Send) { result = SmtpClientResult.Success;//���M���� state = State.Quit; } } else if (response == 354) { if (state == State.Data) { state = State.Send; } } else if (response / 100 == 5) { // �]����SMTP�F��K�v�Ƃ��Ȃ��ꍇ�AEHLO�Ɏ��s������HELO�ōĐڑ�����݂� //if (Mode == 1 && TryEhlo && SmtpAuthClient == NULL) { if (state == State.Ehlo) { state = State.Helo; //HELO��500�������ꍇ�̓G���[�����ɉ�� } else //���M���s //���M���s���̍Ō�̑���M�L�^ { LastLog.Add(sockTcp.LastLineSend); LastLog.Add(recvStr); result = SmtpClientResult.ErrorCode;//�G���[�R�[�h��M state = State.Quit; } } //SMTP�F�� var ret = smtpAuthClient.Set(recvStr); if (ret != null) { sockTcp.AsciiSend(ret); continue; } //******************************************************************** // ���(mode)���Ƃ̏��� //******************************************************************** if (state == State.Ehlo) { sockTcp.AsciiSend(string.Format("EHLO {0}", serverName)); } else if (state == State.Helo) { sockTcp.AsciiSend(string.Format("HELO {0}", serverName)); } else if (state == State.Mail) { //Ver5.0.0-a24 //sockTcp.AsciiSend(string.Format("MAIL From:{0}",from),OPERATE_CRLF.YES); sockTcp.AsciiSend(string.Format("MAIL From: <{0}>", from)); } else if (state == State.Rcpt) { //Ver5.0.0-a24 //sockTcp.AsciiSend(string.Format("RCPT To:{0}",to),OPERATE_CRLF.YES); sockTcp.AsciiSend(string.Format("RCPT To: <{0}>", to)); } else if (state == State.Data) { sockTcp.AsciiSend("DATA"); } else if (state == State.Send) { if (mail == null) { //���M���s���̍Ō�̑���M�L�^ LastLog.Add(sockTcp.LastLineSend); LastLog.Add(recvStr); break; //�G���[���� } const int count = -1; //count ���M����{���̍s���i-1�̏ꍇ�͑S���j if (!mail.Send(sockTcp, count)) { //_logger.Set(LogKind.Error, null, 9000058, ex.Message); //mail.GetLastError()�𖢏��� break;//�G���[���� } sockTcp.AsciiSend("."); } else if (state == State.Quit) { sockTcp.AsciiSend("QUIT"); } } return(result); }