-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx-ad-auth-go.go
192 lines (171 loc) · 6.53 KB
/
nginx-ad-auth-go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) 2022-2024 Ronan LE MEILLAT
// This program is licensed under the AGPLv3 license.
package main
import (
"crypto/tls"
_ "embed"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"github.com/go-ldap/ldap/v3"
)
var (
port int
ldapURI string
ldapBase string
adDomain string
mailServer string
mailServerPort int
)
//go:embed "not-found.html"
var notFoundHTML []byte
// init initializes the application by parsing command line flags and checking environment variables.
// It sets the values for port, ldapURI, ldapBase, adDomain, mailServer, and mailServerPort.
// If any of these values are missing or invalid, it logs a fatal error.
func init() {
flag.IntVar(&port, "port", 8080, "Port to listen on")
flag.StringVar(&ldapURI, "ldap-uri", "", "LDAP URI")
flag.StringVar(&ldapBase, "ldap-base", "", "LDAP base")
flag.StringVar(&adDomain, "ad-domain", "", "AD domain")
flag.StringVar(&mailServer, "mail-server", "", "Mail server")
flag.IntVar(&mailServerPort, "mail-server-port", 0, "Mail server port")
flag.Parse()
// Check environment variables
if envPort := os.Getenv("NGINX_AUTH_PORT"); envPort != "" {
port, _ = strconv.Atoi(envPort)
}
if envLDAPURI := os.Getenv("NGINX_AUTH_LDAP_URI"); envLDAPURI != "" {
ldapURI = envLDAPURI
}
if envLDAPBase := os.Getenv("NGINX_AUTH_LDAP_BASE"); envLDAPBase != "" {
ldapBase = envLDAPBase
}
if envADDomain := os.Getenv("NGINX_AUTH_AD_DOMAIN"); envADDomain != "" {
adDomain = envADDomain
}
if envMailServer := os.Getenv("NGINX_AUTH_MAIL_SERVER"); envMailServer != "" {
mailServer = envMailServer
}
if envMailServerPort := os.Getenv("NGINX_AUTH_MAIL_SERVER_PORT"); envMailServerPort != "" {
mailServerPort, _ = strconv.Atoi(envMailServerPort)
}
if ldapURI == "" || ldapBase == "" || adDomain == "" || mailServer == "" || mailServerPort == 0 {
log.Fatal("LDAP URI, LDAP base, AD domain, mail server and mail server port are required")
}
}
// If user hits any other endpoint, return a 404 error with the content of the file not-found.html
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Not found: %s, IP: %s", r.URL.Path, r.RemoteAddr)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusNotFound)
w.Write(notFoundHTML)
}
func main() {
http.HandleFunc("/auth", authHandler)
http.HandleFunc("/", notFoundHandler)
log.Printf("Starting server on port %d", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
// Get hostname from IP address like dig -x
func getHostnameFromIP(ip string) string {
hostname, err := net.LookupAddr(ip)
if err != nil {
return ip
}
return hostname[0]
}
// authHandler is a function that handles authentication requests.
// It takes in an http.ResponseWriter and an http.Request as parameters.
// The function retrieves the user, password, and protocol from the request headers.
// If either the user or password is empty, it returns an "Auth-Status: No login or password" error response.
// If the user authentication fails, it returns an "Auth-Status: Invalid login or password" error response.
// The function determines the authentication port based on the protocol and sets the appropriate headers.
// Finally, it sets the "Auth-Status", "Auth-Server", and "Auth-Port" headers and writes a successful response.
func authHandler(w http.ResponseWriter, r *http.Request) {
user := r.Header.Get("Auth-User")
pass := r.Header.Get("Auth-Pass")
client_ip := r.Header.Get("Client-IP")
client_hostname := r.Header.Get("Client-Host")
protocol := r.Header.Get("Auth-Protocol")
if client_hostname == "" {
client_hostname = getHostnameFromIP(client_ip)
}
if user == "" || pass == "" {
log.Printf("No login or password, nginx IP: %s, client IP: %s, client hostname: %s", r.RemoteAddr, client_ip, client_hostname)
http.Error(w, "Auth-Status: No login or password", http.StatusOK)
return
}
authenticated, err := authenticateUser(user, pass)
if err != nil {
log.Printf("Error authenticating user: %v", err)
http.Error(w, "Auth-Status: Internal server error", http.StatusInternalServerError)
return
}
if !authenticated {
log.Printf("Invalid login or password, nginx IP: %s, client IP: %s, client hostname: %s", r.RemoteAddr, client_ip, client_hostname)
http.Error(w, "Auth-Status: Invalid login or password", http.StatusOK)
return
}
authPort := mailServerPort
switch protocol {
case "imap":
authPort = 143
case "imaps":
authPort = 993
case "pop3":
authPort = 110
case "pop3s":
authPort = 995
case "smtp":
authPort = 25
case "smtps":
authPort = 465
}
w.Header().Set("Auth-Status", "OK")
w.Header().Set("Auth-Server", mailServer)
w.Header().Set("Auth-Port", strconv.Itoa(authPort))
w.WriteHeader(http.StatusOK)
log.Printf("Authenticated user: %s, nginx IP: %s, client IP: %s, client hostname: %s", user, r.RemoteAddr, client_ip, client_hostname)
}
// authenticateUser is a function that authenticates a user against an Active Directory server.
// It takes a username and password as parameters and returns a boolean value indicating whether the authentication was successful or not, along with an error if any.
// The function establishes a connection with the LDAP server using the specified ldapURI and binds the user's credentials.
// It then performs a search in the LDAP directory to check if the user exists.
// The search is based on the sAMAccountName attribute, which is the username attribute in Active Directory.
// If the search returns exactly one entry, it means the user exists and the function returns true.
// Otherwise, it returns false.
// If there is any error during the authentication process, it is returned as an error.
func authenticateUser(username, password string) (bool, error) {
l, err := ldap.DialURL(ldapURI)
if err != nil {
return false, fmt.Errorf("failed to connect to LDAP server: %w", err)
}
defer l.Close()
// Reconnect with TLS
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
// If the server does not support StartTLS, return an error
return false, fmt.Errorf("failed to start TLS: %w", err)
}
err = l.Bind(fmt.Sprintf("%s\\%s", adDomain, username), password)
if err != nil {
log.Printf("Failed to bind to LDAP: %v", err)
return false, nil
}
searchRequest := ldap.NewSearchRequest(
ldapBase,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(sAMAccountName=%s)", ldap.EscapeFilter(username)),
[]string{"dn"},
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
return false, fmt.Errorf("failed to search LDAP: %w", err)
}
return len(sr.Entries) == 1, nil
}