prepared for packnl embedding
[rrq/hourglass.git] / manager / basic_login.lsp
1 ;;
2 ;; This script is written with the expectation that it is an embedded
3 ;; newlisp executable invked via .cgi links from within the
4 ;; subdirectory www
5 ;;
6 ;; Its (main-args 0) has a final path component X.cgi that links to
7 ;; this file as a packnl embedding that will use that X part of its
8 ;; name to determine th actual script. That lookup will also use the
9 ;; "role base" as declared in "roles.txt"
10
11 (unless (ends-with (main-args 0) ".cgi")
12   (if (exists file? (list (main-args 1)
13                           (string (main-args 1) ".lsp")))
14       (load $it)
15     (write-line 2 (string "Unknown command " (main-args 1))))
16   (exit 0))
17
18 ;; This script is executed in (protected) subdirectory www
19 (change-dir "..")
20
21 ;; Needs an HTTP_AUTHORIZATION environment variable
22 (unless (setf AUTH (env "HTTP_AUTHORIZATION"))
23   (write-line 2 (read-file "tmpl/unauthorized.http"))
24   (exit 0))
25
26 ;; Must find that value in .htpasswd
27 (unless (ref (6 AUTH) (parse (read-file ".htpasswd") "\n"))
28   (write-line 1 (read-file "tmpl/unauthorized.http"))
29   (exit 0))
30
31 (setf
32  ROLES (map (fn (x) (parse x ":")) (parse (read-file "roles.txt") "\n"))
33  REMOTE_USER (and (regex "([^:]+):" (base64-dec (6 AUTH)) 0) $1)
34  ROLE (or (lookup REMOTE_USER ROLSE) "child")
35  SCRIPT (format "%s/%s" ROLE (main-args 1))
36  )
37 (env "REMOTE_USER" REMOTE_USER)
38 (env "ROLE" ROLE)
39
40 (unless (file? SCRIPT)
41   (write 1 "\nBroken.\n")
42   (exit 0))
43
44 (load SCRIPT)
45 (exit 0)