-
Notifications
You must be signed in to change notification settings - Fork 124
Expand allowedUsers email field to support comma-separated and domains #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
86c899c
05c9bfd
4132854
18f6726
0c25dab
cabf848
c980683
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,15 +36,43 @@ func connectionChecker(peer smtpd.Peer) error { | |||||
return smtpd.Error{Code: 421, Message: "Denied"} | ||||||
} | ||||||
|
||||||
func addrAllowed(addr string, allowedAddrs []string) bool { | ||||||
if allowedAddrs == nil { | ||||||
return true | ||||||
} | ||||||
|
||||||
domidx := strings.LastIndex(addr, "@") | ||||||
if domidx == -1 { | ||||||
return false | ||||||
} | ||||||
domain := strings.ToLower(addr[domidx+1:]) | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied (earlier in the function, so |
||||||
for _, allowedAddr := range allowedAddrs { | ||||||
allowedAddr = strings.ToLower(allowedAddr) | ||||||
|
||||||
if strings.Index(allowedAddr, "@") == -1 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now a conflict because the absence of @ can be because it's a domain or a local address without domain yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per below, I was unaware that "local" addresses could reach smtprelay. Updated (see below). |
||||||
if allowedAddr == domain { | ||||||
return true | ||||||
} | ||||||
} else { | ||||||
if allowedAddr == addr { | ||||||
return true | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return false | ||||||
} | ||||||
|
||||||
func senderChecker(peer smtpd.Peer, addr string) error { | ||||||
// check sender address from auth file if user is authenticated | ||||||
if *allowedUsers != "" && peer.Username != "" { | ||||||
_, email, err := AuthFetch(peer.Username) | ||||||
_, allowedAddrs, err := AuthFetch(peer.Username) | ||||||
if err != nil { | ||||||
return smtpd.Error{Code: 451, Message: "Bad sender address"} | ||||||
} | ||||||
|
||||||
if email != "" && strings.ToLower(addr) != strings.ToLower(email) { | ||||||
if !addrAllowed(addr, allowedAddrs) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've lost the strings.ToLower(addr) case but i think this should be done in the new addrAllowed() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. This was a result of the refactor. I've fixed that, and added a test (cabf848). |
||||||
return smtpd.Error{Code: 451, Message: "Bad sender address"} | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAddrAllowedNoDomain(t *testing.T) { | ||
allowedAddrs := []string{"[email protected]"} | ||
if addrAllowed("bob.com", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestAddrAllowedSingle(t *testing.T) { | ||
allowedAddrs := []string{"[email protected]"} | ||
|
||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestAddrAllowedMulti(t *testing.T) { | ||
allowedAddrs := []string{"[email protected]", "[email protected]"} | ||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestAddrAllowedSingleDomain(t *testing.T) { | ||
allowedAddrs := []string{"abc.com"} | ||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestAddrAllowedMixed(t *testing.T) { | ||
allowedAddrs := []string{"[email protected]", "appsrv.example.com"} | ||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if !addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
if addrAllowed("[email protected]", allowedAddrs) { | ||
t.FailNow() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since addr is the sender email it shoud be valid that the address does not contain a domain and @ yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per below, I was unaware that "local" addresses could reach smtprelay. Updated (see below).