SMTP authorization

Started by cormullion, August 11, 2006, 10:17:47 AM

Previous topic - Next topic

cormullion

The current newLISP SMTP module doesn't support any authorization - I tried to use it today, and I haven't got any accounts that work OK with it. Is it easy to add the necessary stuff?

Lutz

#1
The module is mainly used together with other newLISP cgi scripts on a server on localhost and with permissions where no authorization is required.



At the moment I have no plans to add authorization, but perhaps somebody else wants to step up to the plate ;-)



Lutz

newdep

#2
What authorization do you mean? because there where some authorization

mechanisms in smtp used back in 1995 but I can also rember that they did not survice

the security checks ;-)
-- (define? (Cornflakes))

cormullion

#3
It's this stuff - thought I'd try smtp.lsp but it wants more than I know how to give.


220 anchor-post-30.mail.med.net ESMTP Fri, 11 Aug 2006 20:31:42 +0000
sent: HELO frangipane.med.com
250 anchor-post-30.mail.med.net Hello dyn-26-45-23-135.dslaccess.com [26.45.23.135]
sent: MAIL FROM: <postmaster@frangipane.med.com>
250 OK
sent: RCPT TO: <postmaster@frangipane.med.com>
550 SMTP AUTH required from 26.45.23.135


Oh well, no probs - just having fun. :-)

newdep

#4
Mmmm im just currious if this is indeed an authentication problem. Sometimes

the message 500... is misused for other global errors too...



Do you use that server to send your regular email normaly? And if you use it

is it plain normal mail sending via smtp without ssl or any other exotic protocols?



Im trying to understand the problem, that way a workaround in the smtp.lsp from Lutz

can be inplanted easier ;-) (i hope..)
-- (define? (Cornflakes))

newdep

#5
Well i had a quick look at the server...



There is good news and other news...



The good news is that it can be done in newLISP ;-)

The other news is we dont have MD5 by default in newlisp .



So for now you can only login there without authentication if they allow that..

Or use MSoutlook or MacEmailsoftware for mail sending..



Though if you simply want to send mail via smtp you could try and find

an "open relay" server  on the internet. In other words an other smtp server

that grand access from anyone...



Norman.
-- (define? (Cornflakes))

cormullion

#6
I'm getting a bit out of my depth here, but...



I wanted to hack the smtp.lsp module so that I could send email from newLISP to my main email account. The SMTP server uses AUTH PLAIN authentication, so after a bit of noodling around (and trying to learn newlispdoc too!) i got as far as this. It might work on other servers using AUTH PLAIN authentication. On my server it appears to use base64 rather than MD5, so its not too hard...


;; @module smtp.lsp
;; @version 1.7 - comments redone for automatic documentation
;; @version 1.8 - 2006-10-08 cormullion: first attempt at implementing AUTH PLAIN authentication
;; @author Lutz Mueller 2001
;;
;; <h2>Routines for sending mail</h2>
;; This module implements routines to communicate with a SMTP mail server
;; for sending email. To use this module include the following 'load' statement
;; at the beginning of the program file:
;; <pre>
;; (load "/usr/share/newlisp/smtp.lsp")
;; </pre>
;; To see debugging information:
;; <pre>(set 'debug-flag true)</pre>

(context 'SMTP)

(set 'debug-flag nil)

;; @syntax (SMTP:send-mail <str> <str> <str> <str> <str> <str> <str>)
;; @param <str> the email address of the sender
;; @param <str> the email address of the recipient
;; @param <str> the subject line of the email
;; @param <str> the message part of the email
;; @param <str> the address of the SMTP server
;; @param <user> (optional) the user name for authentication
;; @param <password> (optional unless user-name supplied) the password for authentication
;; @return On success 'true', on failure 'nil'
;; In case the function fails returning 'nil', the function 'SMTP:get-error-text' can be used to receive the error text.
;;
;; @example
;;(SMTP:send-mail "jdoe@asite.com" "somebody@isp.com" "Greetings"
;;   "How are you today? - john doe -" "smtp.asite.com" "jdoe" "secret")

;; This logs in to the server, tries to authenticate using the username 'jdoe' and password 'secret' (if supplied),
;; and sends an email with the format:
;; <pre>
;;  From:    jdoe@asite.com
;;  To:      somebody@isp.com
;;  Subject: Greetings
;;  Message: How are you today? - John Doe -
;; </pre>

(define (send-mail mail-from mail-to mail-subject mail-body SMTP-server user-name password)
    (and
        (set 'from-hostname (nth 1 (parse mail-from "@")))
        (set 'socket (net-connect SMTP-server 25))
        (confirm-request "2")
        (if (and user-name password)
        (mail-authorize user-name password))
        (net-send-get-result (append "HELO " from-hostname) "2")
        (net-send-get-result (append "MAIL FROM: <" mail-from ">") "2")
        (net-send-get-result (append "RCPT TO: <" mail-to ">") "2")
        (net-send-get-result "DATA" "3")
        (mail-send-header)
        (mail-send-body)
        (confirm-request "2")
        (net-send-get-result "QUIT" "2")
        (or (net-close socket) true)))

(define (confirm-request conf)
   (and
    (net-receive socket 'recvbuff 256 "rn")
    (if debug-flag (println recvbuff) true)
    ; remember responses
    (push recvbuff responses -1)
    (starts-with recvbuff conf)))
 
(define (net-send-get-result str conf)
   (set 'send-str (append str "rn"))
   (if debug-flag (println "sent: " send-str))
   (net-send socket 'send-str)
   (if conf (confirm-request conf) true))

(define (mail-authorize user-name password)
; find out what server does
   (and
    (net-send-get-result (append "EHLO " from-hostname) "2")
    (while (net-select socket "read" 500)
    (confirm-request "250"))
    (find "250-AUTH PLAIN" (join responses ))
    (net-send-get-result
    (append "AUTH PLAIN "
    (base64-enc (append "00" user-name "00" password))) "235")))

(define (mail-send-header)
    (net-send-get-result (append "TO: " mail-to))
    (net-send-get-result (append "FROM: " mail-from))
    (net-send-get-result (append "SUBJECT: " mail-subject))
    (net-send-get-result (append "X-Mailer: newLISP v." (string (nth -2 (sys-info))))))

(define (mail-send-body )
    (net-send-get-result "")
    (dolist (lne (parse mail-body "rn"))
        (if (= lne ".")
            (net-send-get-result "..")
            (net-send-get-result lne)))
    (net-send-get-result "."))

;; @syntax (SMTP:get-error-text)
;; @return The text of the last error occurred.

(define (get-error-text)
    recvbuff)

(context 'MAIN)

; eof


Help welcome!