smtpd(n) | Tcl SMTP Server Package | smtpd(n) |
smtpd - Tcl SMTP server implementation
package require Tcl 8.3
package require smtpd ?1.4?
::smtpd::start ?myaddr? ?port?
::smtpd::stop
::smptd::configure ?option value? ?option value ...?
::smtpd::cget ?option?
The smtpd package provides a simple Tcl-only server library for the Simple Mail Transfer Protocol as described in RFC 821 (http://www.rfc-editor.org/rfc/rfc821.txt) and RFC 2821 (http://www.rfc-editor.org/rfc/rfc2821.txt). By default the server will bind to the default network address and the standard SMTP port (25).
This package was designed to permit testing of Mail User Agent code from a developers workstation. It does not attempt to deliver mail to your mailbox. Instead users of this package are expected to write a procedure that will be called when mail arrives. Once this procedure returns, the server has nothing further to do with the mail.
On Unix platforms binding to the SMTP port requires root privileges. I would not recommend running any script-based server as root unless there is some method for dropping root privileges immediately after the socket is bound. Under Windows platforms, it is not necessary to have root or administrator privileges to bind low numbered sockets. However, security on these platforms is weak anyway.
In short, this code should probably not be used as a permanently running Mail Transfer Agent on an Internet connected server, even though we are careful not to evaluate remote user input. There are many other well tested and security audited programs that can be used as mail servers for internet connected hosts.
set sock [::smtpd::start [info hostname] 0]
will bind to the hosts internet interface on the first available port.
At present the package only supports a single instance of a SMTP server. This could be changed if required at the cost of making the package a little more complicated to read. If there is a good reason for running multiple SMTP services then it will only be necessary to fix the options array and the ::smtpd::stopped variable usage.
As the server code uses fileevent(n) handlers to process the input on sockets you will need to run the event loop. This means either you should be running from within wish(1) or you should vwait(n) on the ::smtpd::stopped variable which is set when the server is stopped.
It should be noted that stopping the server does not disconnect any currently active sessions as these are operating over an independent channel. Only explicitly tracking and closing these sessions, or exiting the server process will close down all the running sessions. This is similar to the usual unix daemon practice where the server performs a fork(2) and the client session continues on the child process.
proc validate_host {ipnum} { if {[string match "192.168.1.*" $ipnum]} { error "go away!" } }
If access is denied the client will receive a standard message that includes the text of your error, such as:
550 Access denied: I hate you.
As per the SMTP protocol, the connection is not closed but we wait for the client to send a QUIT command. Any other commands cause a 503 Bad Sequence error.
proc validate_sender {address} { eval array set addr [mime::parseaddress $address] if {[string match "denied" $addr(local)]} { error "mailbox $addr(local) denied" } return }
The content of any error message will not be passed back to the client.
proc deliverMIME {token} { set sender [lindex [mime::getheader $token From] 0] set recipients [lindex [mime::getheader $token To] 0] set mail "From $sender [clock format [clock seconds]]" append mail "\n" [mime::buildmessage $token] puts $mail }
proc deliver {sender recipients data} { set mail "From $sender [clock format [clock seconds]]" append mail "\n" [join $data "\n"] puts "$mail" }
Note that the DATA command will return an error if no sender or recipient has yet been defined.
Written by Pat Thoyts mailto:patthoyts@users.sourceforge.net.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file "license.terms" for more details.
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category smtpd of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation.
rfc 2821, rfc 821, services, smtp, smtpd, socket, vwait
Networking
Copyright (c) Pat Thoyts <patthoyts@users.sourceforge.net>
1.4 | smtpd |