| 1 | structure PassGen :> PASSGEN = struct |
| 2 | |
| 3 | open Config Sql |
| 4 | structure C = PgClient |
| 5 | |
| 6 | val db = ref (NONE : C.conn option) |
| 7 | fun getDb () = valOf (!db) |
| 8 | |
| 9 | fun begin () = |
| 10 | let |
| 11 | val c = C.conn passgenDbstring |
| 12 | in |
| 13 | db := SOME c; |
| 14 | ignore (C.dml c "BEGIN") |
| 15 | end |
| 16 | |
| 17 | fun commit () = |
| 18 | let |
| 19 | val db = getDb () |
| 20 | in |
| 21 | C.dml db "COMMIT"; |
| 22 | C.close db |
| 23 | end |
| 24 | |
| 25 | fun gen () = |
| 26 | let |
| 27 | val db = getDb () |
| 28 | |
| 29 | val id = case C.oneRow db "SELECT nextval('PassSeq')" of |
| 30 | [id] => C.intFromSql id |
| 31 | | _ => raise Fail "Bad nextval() return" |
| 32 | |
| 33 | val proc = Unix.execute ("/usr/bin/apg", ["/usr/bin/apg", "-d", "-n", "1", "-M", "NCL"]) |
| 34 | val inf = Unix.textInstreamOf proc |
| 35 | |
| 36 | val pass = case TextIO.inputLine inf of |
| 37 | NONE => raise Fail "No apg output" |
| 38 | | SOME line => String.substring (line, 0, size line - 1) |
| 39 | in |
| 40 | ignore (Unix.reap proc); |
| 41 | ignore (C.dml db ($`INSERT INTO Pass (id, pass) VALUES (^(C.intToSql id), ^(C.stringToSql pass))`)); |
| 42 | (id, pass) |
| 43 | end |
| 44 | |
| 45 | end |