Passgen script (normal user part)
authorAdam Chlipala <adamc@hcoop.net>
Tue, 24 Mar 2009 15:22:41 +0000 (15:22 +0000)
committerAdam Chlipala <adamc@hcoop.net>
Tue, 24 Mar 2009 15:22:41 +0000 (15:22 +0000)
13 files changed:
passgen/.cvsignore [new file with mode: 0644]
passgen/Makefile [new file with mode: 0644]
passgen/after.mlt [new file with mode: 0644]
passgen/before.mlt [new file with mode: 0644]
passgen/config.sig [new file with mode: 0644]
passgen/config.sml [new file with mode: 0644]
passgen/exn.mlt [new file with mode: 0644]
passgen/mlt.conf [new file with mode: 0644]
passgen/out/.cvsignore [new file with mode: 0644]
passgen/passgen.mlt.in [new file with mode: 0644]
passgen/passgen.sig [new file with mode: 0644]
passgen/passgen.sml [new file with mode: 0644]
passgen/tables.sql [new file with mode: 0644]

diff --git a/passgen/.cvsignore b/passgen/.cvsignore
new file mode 100644 (file)
index 0000000..635b5dc
--- /dev/null
@@ -0,0 +1,2 @@
+.cm
+passgen.mlt
diff --git a/passgen/Makefile b/passgen/Makefile
new file mode 100644 (file)
index 0000000..59ce744
--- /dev/null
@@ -0,0 +1,4 @@
+all: passgen.mlt
+
+passgen.mlt: /etc/hcoop.header /etc/hcoop.footer passgen.mlt.in
+       hcoop_html "HCoop Passgen" passgen.mlt.in >passgen.mlt
diff --git a/passgen/after.mlt b/passgen/after.mlt
new file mode 100644 (file)
index 0000000..ec09e9e
--- /dev/null
@@ -0,0 +1 @@
+<% PassGen.commit () %>
diff --git a/passgen/before.mlt b/passgen/before.mlt
new file mode 100644 (file)
index 0000000..18e4637
--- /dev/null
@@ -0,0 +1 @@
+<% PassGen.begin () %>
diff --git a/passgen/config.sig b/passgen/config.sig
new file mode 100644 (file)
index 0000000..b61e7c6
--- /dev/null
@@ -0,0 +1,3 @@
+signature CONFIG = sig
+    val dbstring : string
+end
diff --git a/passgen/config.sml b/passgen/config.sml
new file mode 100644 (file)
index 0000000..185526b
--- /dev/null
@@ -0,0 +1,5 @@
+structure Config :> CONFIG = struct
+
+val dbstring = "dbname=passgen"
+
+end
diff --git a/passgen/exn.mlt b/passgen/exn.mlt
new file mode 100644 (file)
index 0000000..8c13844
--- /dev/null
@@ -0,0 +1,28 @@
+<html><head>
+<title>Hcoop Passgen: Exception</title>
+</head><body>
+
+<h1>Exception</h1>
+
+<% switch Web.getExn () of
+     Fail msg => %>
+<b>Fail</b>: <% Web.htmlNl msg %>
+<% | OS.SysErr (name, NONE) => %>
+<b>System error</b>: <% Web.html name %>
+<% | OS.SysErr (name, SOME syserr) => %>
+<b>System error</b>: <% Web.html name %>: <% Web.html (OS.errorName syserr) %>: <% Web.htmlNl (OS.errorMsg syserr) %>
+<% | IO.Io {name, function, ...} => %>
+<b>IO error</b>: <% Web.html name %> for <% Web.html function %><br>
+<% | PassGen.C.Sql msg => %>
+<b>SQL</b>: <% Web.htmlNl msg %>
+<% | Web.Format s => %>
+<b>Format</b>: <% Web.htmlNl s %>
+
+<% | ex => %>
+<b>Unknown exception kind.</b> Backtrace:
+<% foreach s in SMLofNJ.exnHistory ex do %>
+<li> <% Web.html s %></li>
+<% end
+end %>
+
+</body></html>
diff --git a/passgen/mlt.conf b/passgen/mlt.conf
new file mode 100644 (file)
index 0000000..778f75d
--- /dev/null
@@ -0,0 +1,9 @@
+before before
+after  after
+exn    exn
+
+out    out
+pub    /afs/hcoop.net/user/h/hc/hcoop/public_html/cgi-bin
+
+cm     /usr/local/share/smlsql/smlsql.cm
+cm     /usr/local/share/smlsql/libpq/sources.cm
diff --git a/passgen/out/.cvsignore b/passgen/out/.cvsignore
new file mode 100644 (file)
index 0000000..72e8ffc
--- /dev/null
@@ -0,0 +1 @@
+*
diff --git a/passgen/passgen.mlt.in b/passgen/passgen.mlt.in
new file mode 100644 (file)
index 0000000..c946638
--- /dev/null
@@ -0,0 +1,6 @@
+<% val (id, pass) = PassGen.gen () %>
+
+<table>
+<tr><td align="right"><b>ID:</b></td> <td><% id %></td></tr>
+<tr><td align="right"><b>Password:</b></td> <td><% pass %></td></tr>
+</table>
diff --git a/passgen/passgen.sig b/passgen/passgen.sig
new file mode 100644 (file)
index 0000000..80ce6af
--- /dev/null
@@ -0,0 +1,8 @@
+signature PASSGEN = sig
+    structure C : SQL_CLIENT
+
+    val begin : unit -> unit
+    val commit : unit -> unit
+
+    val gen : unit -> int * string
+end
diff --git a/passgen/passgen.sml b/passgen/passgen.sml
new file mode 100644 (file)
index 0000000..4de2583
--- /dev/null
@@ -0,0 +1,45 @@
+structure PassGen :> PASSGEN = struct
+
+open Config Sql
+structure C = PgClient
+
+val db = ref (NONE : C.conn option)
+fun getDb () = valOf (!db)
+
+fun begin () =
+    let
+       val c = C.conn dbstring
+    in
+       db := SOME c;
+       ignore (C.dml c "BEGIN")
+    end
+
+fun commit () =
+    let
+       val db = getDb ()
+    in
+       C.dml db "COMMIT";
+       C.close db
+    end
+
+fun gen () =
+    let
+       val db = getDb ()
+
+       val id = case C.oneRow db "SELECT nextval('PassSeq')" of
+                    [id] => C.intFromSql id
+                  | _ => raise Fail "Bad nextval() return"
+
+       val proc = Unix.execute ("/usr/bin/apg", ["/usr/bin/apg", "-d", "-n", "1"])
+       val inf = Unix.textInstreamOf proc
+
+       val pass = case TextIO.inputLine inf of
+                      NONE => raise Fail "No apg output"
+                    | SOME line => String.substring (line, 0, size line - 1)
+    in
+       ignore (Unix.reap proc);
+       ignore (C.dml db ($`INSERT INTO Pass (id, pass) VALUES (^(C.intToSql id), ^(C.stringToSql pass))`));
+       (id, pass)
+    end
+
+end
diff --git a/passgen/tables.sql b/passgen/tables.sql
new file mode 100644 (file)
index 0000000..2333bf7
--- /dev/null
@@ -0,0 +1,5 @@
+CREATE SEQUENCE PassSeq;
+
+CREATE TABLE Pass(
+       id INTEGER PRIMARY KEY,
+       pass TEXT NOT NULL);