summary refs log blame commit diff stats
path: root/cmd/cetus/main_openbsd.go
blob: b978e7c2701a0456880e9abfb2ef848e2decd3bf (plain) (tree)




















                                          




                                               
 
                                                      




                                             
                                 
                                     
                                   
                                 


                                       


                                                                             




                                                                   





                                  



                              






                                     

                                      
                                                          



                                                                                
 




                                                                                     



                  
// +build openbsd

package main

import (
	"fmt"
	"log"
	"strings"

	"golang.org/x/sys/unix"
	"tildegit.org/andinus/cetus/cache"
)

func main() {
	unveil()
	app()
}

func unveil() {
	unveilL := make(map[string]string)

	// We unveil the whole cache directory.
	err = unix.Unveil(cache.Dir(), "rwc")
	if err != nil {
		log.Fatal(err)
	}

	unveilL["/dev/null"] = "rw" // required by feh
	unveilL["/etc/resolv.conf"] = "r"

	// ktrace output
	unveilL["/usr/libexec/ld.so"] = "r"
	unveilL["/var/run/ld.so.hints"] = "r"
	unveilL["/usr/lib"] = "r"
	unveilL["/dev/urandom"] = "r"
	unveilL["/etc/hosts"] = "r"
	unveilL["/etc/ssl"] = "r"

	for k, v := range unveilL {
		err = unix.Unveil(k, v)
		if err != nil && err.Error() == "no such file or directory" {
			log.Printf("WARN: Unveil failed on %s", k)
		} else if err != nil {
			log.Fatal(fmt.Sprintf("%s :: %s\n%s", k, v,
				err.Error()))
		}
	}

	err = unveilCmds([]string{
		"feh",
		"gsettings",
		"pcmanfm",
		"notify-send",
	})
	if err != nil {
		log.Fatal(err)
	}

	// Block further unveil calls
	err = unix.UnveilBlock()
	if err != nil {
		log.Fatal(err)
	}
}

// unveilCmds will unveil commands.
func unveilCmds(cmds []string) error {
	pathList := strings.Split(getEnv("PATH", ""), ":")
	// Unveil each command.
	for _, cmd := range cmds {
		for _, path := range pathList {
			err = unix.Unveil(fmt.Sprintf("%s/%s", path, cmd), "rx")

			if err != nil && err.Error() != "no such file or directory" {
				return fmt.Errorf("%s\n%s",
					cmd,
					err.Error())
			}
		}
	}
	return nil
}