1 ;; Super-basic bell-and-whistle-free SMTP server.
3 ;; Intended for a single-user system
14 (chicken process-context)
15 (chicken process-context posix)
17 srfi-1 srfi-13 matchable)
19 (define lambdamail-version "0.0.1")
23 host port spool-dir user group)
24 (define-record message to from text helo)
25 (define (make-empty-message) (make-message "" "" "" ""))
31 (define ((make-smtp in-port out-port config) type)
32 (if (eq? type 'get-line)
36 ((greeting) (conc "220 " (config-host config)
37 " LambdaMail v" lambdamail-version))
39 ((intermediate) "354 intermediate")
40 ((close) "221 closing transmission channel")
41 ((not-implemented) "502 command not implemented"))
45 ;;; Server initialization
48 (define (drop-privs config)
49 (let ((uid (config-user config))
50 (gid (config-group config)))
51 (if (not (null? gid)) ; Group first, since only root can switch groups.
52 (set! (current-group-id) gid))
54 (set! (current-user-id) uid))))
56 (define (run-server config)
57 (set-buffering-mode! (current-output-port) #:line)
58 (let ((listener (tcp-listen (config-port config) 10 "::")))
59 (print "LambdaMail v" lambdamail-version
60 " listening on port " (config-port config) " ...")
61 (print "(Host name: " (config-host config)
62 ", Spool dir: " (config-spool-dir config) ")")
64 (server-loop listener config)))
70 (define (server-loop listener config)
71 (let-values (((in-port out-port) (tcp-accept listener)))
72 (let-values (((local-ip remote-ip) (tcp-addresses in-port)))
73 (print "Accepted connection from " remote-ip " on " (seconds->string)))
75 (let ((smtp (make-smtp in-port out-port config)))
77 (process-smtp smtp config))
79 (print-error-message o)))
80 (print "Terminating connection.")
81 (close-input-port in-port)
82 (close-output-port out-port)
83 (server-loop listener config)))
85 ;;; SMTP processing loop
88 (define (process-smtp smtp config)
89 (let loop ((msg (make-empty-message))
90 (line-orig (smtp 'get-line)))
91 (if (string? line-orig)
92 (let ((line (string-downcase line-orig)))
95 ((string-prefix? "helo" line)
96 (message-helo-set! msg (string-drop line (string-length "helo")))
98 (loop msg (smtp 'get-line)))
99 ((string-prefix? "mail from:" line)
100 (message-from-set! msg (string-drop line (string-length "mail from:")))
102 (loop msg (smtp 'get-line)))
103 ((string-prefix? "rcpt to:" line)
104 (message-to-set! msg (string-drop line (string-length "rcpt to:")))
106 (loop msg (smtp 'get-line)))
107 ((string-prefix? "data" line)
109 (let text-loop ((text-line (smtp 'get-line))
111 (if (string=? "." text-line)
112 (message-text-set! msg text)
113 (text-loop (smtp 'get-line)
114 (conc text text-line "\n"))))
115 (process-message msg config)
117 (loop (make-empty-message)
119 ((string-prefix? "quit" line)
123 (loop msg (smtp 'get-line)))
125 (smtp 'not-implemented)
126 (loop msg (smtp 'get-line)))))
133 (define (get-to-addresses config)
134 (map (lambda (p) (cons
135 (conc "<" (car p) "@" (config-host config) ">")
137 (map (lambda (file) (cons (pathname-file file) file))
138 (glob (conc (config-spool-dir config) "/*")))))
140 (define (remove-angle-brackets addr)
141 (let ((left-idx (substring-index "<" addr))
142 (right-idx (substring-index ">" addr)))
143 (substring addr (+ left-idx 1) right-idx)))
145 (define (deliver-message-mbox msg dest-file)
146 (print "Delivering to mbox " dest-file)
147 (with-output-to-file dest-file
149 (print "\nFrom " (remove-angle-brackets (message-from msg)))
150 (print (message-text msg)))
153 (define (deliver-message-maildir msg dest-dir)
154 (print "Delivering to maildir " dest-dir)
155 (with-output-to-file (conc dest-dir "/" (current-seconds))
157 (print (message-text msg)))))
159 (define (process-message msg config)
160 (let ((dest (assoc (message-to msg) (get-to-addresses config))))
162 (let ((dest-file (cdr dest)))
163 (if (directory-exists? dest-file)
164 (deliver-message-maildir msg dest-file)
165 (deliver-message-mbox msg dest-file))
166 (print "Message DELIVERED:"))
167 (print "Message REJECTED:"))
168 (print " * From: " (message-from msg))
169 (print " * To: " (message-to msg))))
172 ;;; Command line argument parsing
175 (define (print-usage progname)
177 progname " -h/--help\n"
178 progname " [-u/--user UID] [-g/--group GID] hostname [[port [spooldir]]\n"
180 "The -u and -g options can be used to set the UID and GID of the process\n"
181 "following the creation of the TCP port listener (which often requires root)."))
184 (let ((progname (pathname-file (car (argv))))
185 (config (make-config "" 25 "/var/spool/mail" '() '())))
186 (if (null? (cdr (argv)))
187 (print-usage progname)
188 (let loop ((args (cdr (argv))))
189 (let ((this-arg (car args))
190 (rest-args (cdr args)))
191 (if (string-prefix? "-" this-arg)
193 ((or (equal? this-arg "-u")
194 (equal? this-arg "--user"))
195 (config-user-set! config (string->number (car rest-args)))
196 (loop (cdr rest-args)))
197 ((or (equal? this-arg "-g")
198 (equal? this-arg "--group"))
199 (config-group-set! config (string->number (car rest-args)))
200 (loop (cdr rest-args)))
201 ((or (equal? this-arg "-h")
202 (equal? this-arg "--help"))
203 (print-usage progname))
205 (print "Unknown option " this-arg "\n")
206 (print-usage progname)))
208 (config-host-set! config this-arg)
209 (unless (null? rest-args)
210 (config-port-set! config (string->number (car rest-args)))
211 (unless (null? (cdr rest-args))
212 (config-spool-dir-set! (cadr rest-args))))
213 (run-server config))))))))
217 ;; (run-server (make-config "thelambdalab.xyz" 2525 "/var/spool/mail"))