Ejemplo n.º 1
0
func TestSvr(t *testing.T) {
	dbfile := "d:\\temp\\1.db"
	tablename := "testuser"
	t.Log("start to test InitDB\n")
	passdb, err := passsql.InitDB(dbfile)
	if err != nil {
		t.Fatal(err)
		t.Fatalf("Failed to init db:%q", dbfile)
	}
	t.Log("start to test IniTable\n")
	passdb.InitTable(tablename)
	svr, err := NewClientAPISVR("127.0.0.1", 9000, "testuser", []byte("user123"), *passdb, tablename)
	if err != nil {
		t.Fatal(err)
	}
	err = tlsvr.ListenWithConfig(svr.HttpSvr)
	if err != nil {
		t.Fatal(err)
	}
	var c chan int
	<-c
}
Ejemplo n.º 2
0
func main() {
	version_str := "Manpass ver1.0 - server daemon"
	fmt.Println(version_str)
	var uname = flag.String("username", "", "specify the username")
	var svr_port = flag.Int("svrport", 9000, "specify the server listening port")
	var svr_ip = flag.String("svrip", "127.0.0.1", "specify the server listening IP address")
	var pipe_pass = flag.Bool("pipepass", false, "enable to use pipe to pass password")
	var createuser = flag.Bool("create", false, "create a new user")
	flag.Parse()
	if *uname == "" || !common.GoodUname(*uname) {
		fmt.Println("Error: Missing username or invalid username")
		flag.PrintDefaults()
		return
	}

	confDir := common.GetConfDir(*uname)

	fi, err := os.Stat(confDir)
	var upass []byte
	if (err != nil || !fi.IsDir()) && *createuser {
		//if user directory doesn't exist
		if *pipe_pass == false {
			fmt.Printf("user %s does not exisit! Creating a new user:%s\n", *uname, *uname)
			upass = common.InputNewPassword(*uname)
		} else {
			var pass_str string
			_, err := fmt.Scan(&pass_str)
			if err != nil {
				log.Println(err)
				log.Fatal("invalid input")
			}
			upass = []byte(pass_str)
		}
		os.Remove(confDir)
		fmt.Println("creating needed files...\n")

		err = os.MkdirAll(confDir, 0700)
		if err != nil {
			log.Fatalf("Failed to create directory:%s\n", confDir)
		}
		err = pki.GenerateCAandEEFiles(*uname, upass)
		if err != nil {
			log.Fatalf("Failed to generate CA and EE cert/keys:%s\n", err)
		}
		err = passsql.InitPassDB(*uname)
		if err != nil {
			log.Fatalf("Failed to init PassDB:%s\n", err)
		}
		fmt.Println("new user created.\n")
	} else {
		//load exisiting user directory
		if *pipe_pass == false {
			fmt.Printf("Input password for user %s:", *uname)
			upass, err = terminal.ReadPassword(int(os.Stdin.Fd()))
			if err != nil {
				log.Println(err)
				log.Fatal("invalid input")

			}
		} else {
			var pass_str string
			_, err := fmt.Scan(&pass_str)
			if err != nil {
				log.Println(err)
				log.Fatal("invalid input")
			}
			upass = []byte(pass_str)

		}

	}
	log.Println("Starting server...")
	dbfile := filepath.Join(confDir, *uname+".db")
	passdb, err := passsql.LoadDB(dbfile)
	if err != nil {
		log.Fatalf("Fatal Error: Failed to load db %s, %s", dbfile, err)
	}
	svr, err := api.NewClientAPISVR(*svr_ip, *svr_port, *uname, upass, *passdb, *uname)
	if err != nil {
		log.Fatal("Fatal Error: " + string(err.Error()))
	}
	err = tlsvr.ListenWithConfig(svr.HttpSvr)
	if err != nil {
		log.Fatal("Fatal Error: " + string(err.Error()))
	}
	//	httpsvr, err := api.NewProvisionSVR(*svr_ip, *svr_port+1, *uname, upass)
	//	httpsvr.Serve()
	log.Println("Server started.")
	sigs := make(chan os.Signal, 1)
	done := make(chan bool, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	processSignal(sigs, done, passdb)
	<-done

}