guichet/main.go

143 lines
3.3 KiB
Go
Raw Normal View History

2023-07-21 04:23:03 +00:00
/*
Guichet provides a user-management system around an LDAP Directory
Oriniated with deuxfleurs.fr and advanced by resdigita.com
*/
2020-02-09 13:46:52 +00:00
package main
import (
2020-02-09 14:01:20 +00:00
"crypto/rand"
2020-02-09 14:44:18 +00:00
"crypto/tls"
2023-07-21 04:23:03 +00:00
// "encoding/json"
2020-02-09 15:46:26 +00:00
"flag"
2023-07-21 04:23:03 +00:00
// "fmt"
2020-02-09 14:44:18 +00:00
"html/template"
2023-07-21 04:23:03 +00:00
// "io/ioutil"
2020-02-09 15:46:26 +00:00
"log"
"net/http"
2023-07-21 04:23:03 +00:00
// "os"
2020-02-09 15:46:26 +00:00
"strings"
2020-02-09 13:46:52 +00:00
2020-02-09 14:44:18 +00:00
"github.com/go-ldap/ldap/v3"
2020-02-09 21:06:33 +00:00
"github.com/gorilla/mux"
2020-02-09 22:04:37 +00:00
"github.com/gorilla/sessions"
2020-02-09 13:46:52 +00:00
)
2020-02-09 14:44:18 +00:00
const SESSION_NAME = "guichet_session"
2022-12-01 22:05:59 +00:00
var staticPath = "./static"
var templatePath = "./templates"
2020-02-09 15:46:26 +00:00
var store sessions.Store = nil
2022-12-01 22:05:59 +00:00
func getTemplate(name string) *template.Template {
2023-04-19 13:07:46 +00:00
return template.Must(template.New("layout.html").Funcs(template.FuncMap{
"contains": strings.Contains,
}).ParseFiles(
templatePath+"/layout.html",
templatePath+"/"+name,
))
2022-12-01 22:05:59 +00:00
}
2020-02-09 13:46:52 +00:00
func main() {
2020-02-09 14:01:20 +00:00
flag.Parse()
2020-02-09 13:46:52 +00:00
2020-02-09 14:44:18 +00:00
config_file := readConfig()
config = &config_file
2020-02-10 14:26:02 +00:00
session_key := make([]byte, 32)
n, err := rand.Read(session_key)
if err != nil || n != 32 {
log.Fatal(err)
}
store = sessions.NewCookieStore(session_key)
2020-02-09 14:01:20 +00:00
2020-02-09 21:06:33 +00:00
r := mux.NewRouter()
r.HandleFunc("/", handleHome)
r.HandleFunc("/logout", handleLogout)
2020-02-09 21:06:33 +00:00
r.HandleFunc("/profile", handleProfile)
r.HandleFunc("/passwd", handlePasswd)
r.HandleFunc("/picture/{name}", handleDownloadPicture)
2021-08-16 14:27:20 +00:00
r.HandleFunc("/directory/search", handleDirectorySearch)
2021-07-21 19:21:46 +00:00
r.HandleFunc("/directory", handleDirectory)
2023-04-19 13:07:46 +00:00
r.HandleFunc("/garage/key", handleGarageKey)
r.HandleFunc("/garage/website", handleGarageWebsiteList)
r.HandleFunc("/garage/website/new", handleGarageWebsiteNew)
r.HandleFunc("/garage/website/b/{bucket}", handleGarageWebsiteInspect)
2023-04-18 17:37:51 +00:00
2020-02-14 17:57:25 +00:00
r.HandleFunc("/invite/new_account", handleInviteNewAccount)
r.HandleFunc("/invite/send_code", handleInviteSendCode)
2020-02-14 20:58:34 +00:00
r.HandleFunc("/invitation/{code}", handleInvitationCode)
2020-02-14 17:57:25 +00:00
2020-02-09 21:06:33 +00:00
r.HandleFunc("/admin/users", handleAdminUsers)
r.HandleFunc("/admin/groups", handleAdminGroups)
r.HandleFunc("/admin/mailing", handleAdminMailing)
r.HandleFunc("/admin/mailing/{id}", handleAdminMailingList)
2020-02-09 21:06:33 +00:00
r.HandleFunc("/admin/ldap/{dn}", handleAdminLDAP)
2020-02-09 22:04:27 +00:00
r.HandleFunc("/admin/create/{template}/{super_dn}", handleAdminCreate)
2020-02-09 17:28:42 +00:00
2022-12-01 22:05:59 +00:00
staticfiles := http.FileServer(http.Dir(staticPath))
2020-02-09 21:06:33 +00:00
r.Handle("/static/{file:.*}", http.StripPrefix("/static/", staticfiles))
2020-02-09 14:44:18 +00:00
2023-07-20 08:21:51 +00:00
// log.Printf("Starting HTTP server on %s", config.HttpBindAddr)
2020-02-10 14:26:02 +00:00
err = http.ListenAndServe(config.HttpBindAddr, logRequest(r))
2020-02-09 13:46:52 +00:00
if err != nil {
log.Fatal("Cannot start http server: ", err)
}
}
2020-02-09 14:01:20 +00:00
2020-02-09 14:44:18 +00:00
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2023-07-20 08:21:51 +00:00
// log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
2020-02-09 14:44:18 +00:00
handler.ServeHTTP(w, r)
})
}
func ldapOpen(w http.ResponseWriter) *ldap.Conn {
l, err := ldap.DialURL(config.LdapServerAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
if config.LdapTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
}
return l
}
2020-02-09 14:01:20 +00:00
// Page handlers ----
type HomePageData struct {
2020-02-14 20:58:34 +00:00
Login *LoginStatus
BaseDN string
2023-07-21 04:23:03 +00:00
Org string
}
2020-02-09 14:01:20 +00:00
func handleHome(w http.ResponseWriter, r *http.Request) {
2022-12-01 22:05:59 +00:00
templateHome := getTemplate("home.html")
2020-02-09 15:46:26 +00:00
2020-02-09 14:44:18 +00:00
login := checkLogin(w, r)
if login == nil {
return
}
2020-02-10 14:26:02 +00:00
data := &HomePageData{
2020-02-14 20:58:34 +00:00
Login: login,
BaseDN: config.BaseDN,
2023-07-21 04:23:03 +00:00
Org: config.Org,
2020-02-10 14:26:02 +00:00
}
templateHome.Execute(w, data)
2020-02-09 14:44:18 +00:00
}