module Main where

import Network.CGI
import Text.XHtml
import Text.Regex.Posix
import Process

main = runCGI $ handleErrors cgiMain

cgiMain = do
    format <- getInput "format"
    let fmt = maybe "PEM" id format
    let command = "/usr/bin/openssl x509 -in ../ca-cert.pem -inform PEM -outform " ++ safe_fmt fmt
    converted <- liftIO $ run command
    setHeader "Content-Type" "application/octet-stream"
    output $ converted

-- set format to PEM if anyone attempts something funny
safe_fmt fmt =
    if fmt =~ ".*`.*" || fmt =~ ".*;.*" || fmt =~ ".*\\$.*"
        then "PEM"
        else fmt
