login-validator: Can receive shadow files
This commit is contained in:
parent
41a38f99d3
commit
d896911077
1
pkg/login-validator/.gitignore
vendored
1
pkg/login-validator/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
validator
|
validator
|
||||||
|
login-validator
|
@ -102,6 +102,7 @@ func main() {
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("/login", lc)
|
mux.Handle("/login", lc)
|
||||||
mux.HandleFunc("/logout", logout)
|
mux.HandleFunc("/logout", logout)
|
||||||
|
mux.HandleFunc("/passwd", passwd)
|
||||||
http.HandleFunc("/", mux.ServeHTTP)
|
http.HandleFunc("/", mux.ServeHTTP)
|
||||||
|
|
||||||
// Serve content
|
// Serve content
|
||||||
|
64
pkg/login-validator/cmd/passwd.go
Normal file
64
pkg/login-validator/cmd/passwd.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
func passwd(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
|
||||||
|
r.RemoteAddr = addr
|
||||||
|
}
|
||||||
|
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
|
||||||
|
// Check request type and size
|
||||||
|
if r.Method != "POST" {
|
||||||
|
http.Error(w,
|
||||||
|
"Invalid request",
|
||||||
|
http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
} else if r.ContentLength < 0 || r.ContentLength > 655360 {
|
||||||
|
http.Error(w,
|
||||||
|
"Request entity too large",
|
||||||
|
http.StatusRequestEntityTooLarge)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Authenticate the request
|
||||||
|
|
||||||
|
// Retrieve the file
|
||||||
|
file, _, err := r.FormFile("shadow")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error when retrieving shadow file from", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Unable to read your passwd file: something is wrong in your request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Save the file
|
||||||
|
fd, err := os.Create(path.Join(tftpDir, fmt.Sprintf("%s.shadow", r.RemoteAddr)))
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error when creating shadow file from", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Unable to treat your passwd file, please try again later", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer fd.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(fd, file)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error when saving shadow file from", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Unable to save your passwd file, please try again later", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate the new cpio archive
|
||||||
|
|
||||||
|
log.Println("Registered shadow from", r.RemoteAddr)
|
||||||
|
http.Error(w, "Success", http.StatusOK)
|
||||||
|
}
|
Reference in New Issue
Block a user