2013-09-04 22:02:17 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
|
|
|
|
*/
|
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-03 23:33:36 -04:00
|
|
|
"asink"
|
2013-08-23 00:09:03 -04:00
|
|
|
"code.google.com/p/gopass"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-08-27 09:41:55 -04:00
|
|
|
"net/rpc"
|
2013-08-23 00:09:03 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type boolIsSetFlag struct {
|
|
|
|
Value bool
|
|
|
|
IsSet bool //true if explicitly set from the command-line, false otherwise
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBoolIsSetFlag(defaultValue bool) *boolIsSetFlag {
|
|
|
|
b := new(boolIsSetFlag)
|
|
|
|
b.Value = defaultValue
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolIsSetFlag) Set(value string) error {
|
|
|
|
v, err := strconv.ParseBool(value)
|
|
|
|
b.Value = v
|
|
|
|
b.IsSet = true
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:35:48 -04:00
|
|
|
const rpcSocketDefault = "/var/run/asink/asinkd.sock"
|
|
|
|
const rpcSocketDescription = "Socket to use to connect to the Asink server."
|
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
func (b *boolIsSetFlag) String() string { return fmt.Sprintf("%v", *b) }
|
|
|
|
|
|
|
|
func (b *boolIsSetFlag) IsBoolFlag() bool { return true }
|
|
|
|
|
|
|
|
func UserAdd(args []string) {
|
|
|
|
flags := flag.NewFlagSet("useradd", flag.ExitOnError)
|
|
|
|
admin := flags.Bool("admin", false, "User should be an administrator")
|
2013-09-02 23:35:48 -04:00
|
|
|
rpcSocket := flags.String("sock", rpcSocketDefault, rpcSocketDescription)
|
2013-08-23 00:09:03 -04:00
|
|
|
flags.Parse(args)
|
|
|
|
|
|
|
|
if flags.NArg() != 1 {
|
|
|
|
fmt.Println("Error: please supply a username (and only one)")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordOne, err := gopass.GetPass("Enter password for new user: ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
passwordTwo, err := gopass.GetPass("Enter the same password again: ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if passwordOne != passwordTwo {
|
|
|
|
fmt.Println("Error: Passwords do not match. Please try again.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
user := new(User)
|
2013-08-23 00:09:03 -04:00
|
|
|
|
|
|
|
if *admin {
|
2013-09-02 09:36:03 -04:00
|
|
|
user.Role = ADMIN
|
2013-08-23 00:09:03 -04:00
|
|
|
} else {
|
2013-09-02 09:36:03 -04:00
|
|
|
user.Role = NORMAL
|
2013-08-23 00:09:03 -04:00
|
|
|
}
|
|
|
|
user.Username = flags.Arg(0)
|
2013-09-02 09:36:03 -04:00
|
|
|
user.PWHash = HashPassword(passwordOne)
|
2013-08-23 00:09:03 -04:00
|
|
|
|
|
|
|
i := 99
|
2013-09-03 23:33:36 -04:00
|
|
|
err = asink.RPCCall(*rpcSocket, "UserModifier.AddUser", user, &i)
|
2013-08-23 00:09:03 -04:00
|
|
|
if err != nil {
|
2013-09-02 09:36:03 -04:00
|
|
|
if _, ok := err.(rpc.ServerError); ok && err.Error() == DuplicateUsernameErr.Error() {
|
2013-08-27 09:41:55 -04:00
|
|
|
fmt.Println("Error: " + err.Error())
|
2013-08-27 00:03:38 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-23 00:09:03 -04:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserDel(args []string) {
|
2013-09-02 23:35:48 -04:00
|
|
|
flags := flag.NewFlagSet("userdel", flag.ExitOnError)
|
|
|
|
rpcSocket := flags.String("sock", rpcSocketDefault, rpcSocketDescription)
|
|
|
|
flags.Parse(args)
|
|
|
|
|
|
|
|
if flags.NArg() != 1 {
|
2013-08-23 00:09:03 -04:00
|
|
|
fmt.Println("Error: please supply a username (and only one)")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
user := new(User)
|
2013-08-23 00:09:03 -04:00
|
|
|
user.Username = args[0]
|
|
|
|
|
|
|
|
i := 99
|
2013-09-03 23:33:36 -04:00
|
|
|
err := asink.RPCCall(*rpcSocket, "UserModifier.RemoveUser", user, &i)
|
2013-08-23 00:09:03 -04:00
|
|
|
if err != nil {
|
2013-09-02 09:36:03 -04:00
|
|
|
if _, ok := err.(rpc.ServerError); ok && err.Error() == NoUserErr.Error() {
|
2013-08-27 09:41:55 -04:00
|
|
|
fmt.Println("Error: " + err.Error())
|
2013-08-27 00:03:38 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-23 00:09:03 -04:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserMod(args []string) {
|
2013-09-02 09:36:03 -04:00
|
|
|
rpcargs := new(UserModifierArgs)
|
|
|
|
rpcargs.Current = new(User)
|
|
|
|
rpcargs.Updated = new(User)
|
2013-08-23 00:09:03 -04:00
|
|
|
|
|
|
|
admin := newBoolIsSetFlag(false)
|
|
|
|
|
|
|
|
flags := flag.NewFlagSet("usermod", flag.ExitOnError)
|
|
|
|
flags.Var(admin, "admin", "User should be an administrator")
|
|
|
|
flags.BoolVar(&rpcargs.UpdatePassword, "password", false, "Change the user's password")
|
|
|
|
flags.BoolVar(&rpcargs.UpdatePassword, "p", false, "Change the user's password (short version)")
|
|
|
|
flags.BoolVar(&rpcargs.UpdateLogin, "login", false, "Change the user's username")
|
|
|
|
flags.BoolVar(&rpcargs.UpdateLogin, "l", false, "Change the user's username (short version)")
|
2013-09-02 23:35:48 -04:00
|
|
|
rpcSocket := flags.String("sock", rpcSocketDefault, rpcSocketDescription)
|
2013-08-23 00:09:03 -04:00
|
|
|
flags.Parse(args)
|
|
|
|
|
|
|
|
if flags.NArg() != 1 {
|
|
|
|
fmt.Println("Error: please supply a username (and only one)")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
rpcargs.Current.Username = flags.Arg(0)
|
|
|
|
|
|
|
|
if rpcargs.UpdateLogin == true {
|
|
|
|
fmt.Print("New login: ")
|
|
|
|
fmt.Scanf("%s", &rpcargs.Updated.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rpcargs.UpdatePassword {
|
|
|
|
passwordOne, err := gopass.GetPass("Enter new password for user: ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
passwordTwo, err := gopass.GetPass("Enter the same password again: ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if passwordOne != passwordTwo {
|
|
|
|
fmt.Println("Error: Passwords do not match. Please try again.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-09-02 09:36:03 -04:00
|
|
|
rpcargs.Updated.PWHash = HashPassword(passwordOne)
|
2013-08-23 00:09:03 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 00:03:38 -04:00
|
|
|
//set the UpdateRole flag based on whether it was present on the command-line
|
2013-08-23 00:09:03 -04:00
|
|
|
rpcargs.UpdateRole = admin.IsSet
|
|
|
|
if admin.Value {
|
2013-09-02 09:36:03 -04:00
|
|
|
rpcargs.Updated.Role = ADMIN
|
2013-08-23 00:09:03 -04:00
|
|
|
} else {
|
2013-09-02 09:36:03 -04:00
|
|
|
rpcargs.Updated.Role = NORMAL
|
2013-08-23 00:09:03 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 00:03:38 -04:00
|
|
|
if !rpcargs.UpdateRole && !rpcargs.UpdateLogin && !rpcargs.UpdatePassword {
|
|
|
|
fmt.Println("What exactly are you modifying again?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
i := 99
|
2013-09-03 23:33:36 -04:00
|
|
|
err := asink.RPCCall(*rpcSocket, "UserModifier.ModifyUser", rpcargs, &i)
|
2013-08-23 00:09:03 -04:00
|
|
|
if err != nil {
|
2013-09-02 09:36:03 -04:00
|
|
|
if _, ok := err.(rpc.ServerError); ok && err.Error() == NoUserErr.Error() {
|
2013-08-27 09:41:55 -04:00
|
|
|
fmt.Println("Error: " + err.Error())
|
2013-08-27 00:03:38 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-23 00:09:03 -04:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|