//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

package main

import (
	"os"

	"humungus.tedunangst.com/r/gonix"
)

func unveil(what, how string) {
	err := gonix.Unveil(what, how)
	if err != nil {
		errx("unveil(%s, %s) failure (%d)", what, how, err)
	}
}
func pledge(promises string) {
	err := gonix.Pledge(promises)
	if err != nil {
		errx("pledge(%s) failure (%d)", promises, err)
	}
}

func securitizeweb() {
	unveil("/etc/ssl", "r")
	if viewDir != dataDir {
		unveil(viewDir, "r")
	}
	unveil(dataDir, "rwc")
	unveil(os.Args[0], "rx")
	gonix.UnveilEnd()
	pledge("stdio rpath wpath cpath flock dns inet unix")
}

func securitizebackend() {
	gonix.UnveilEnd()
	pledge("stdio unix")
}
