2021-02-05 01:37:12 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-11-18 07:47:42 -05:00
|
|
|
"github.com/pashcovich/openvpn-user/src"
|
2021-02-05 01:37:12 -05:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
const (
|
2022-12-02 11:34:21 -05:00
|
|
|
version = "1.0.9"
|
2021-02-08 04:53:09 -05:00
|
|
|
)
|
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
var (
|
|
|
|
dbPath = kingpin.Flag("db.path", "path do openvpn-user db").Default("./openvpn-user.db").String()
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
dbInitCommand = kingpin.Command("db-init", "Init db.")
|
|
|
|
dbMigrateCommand = kingpin.Command("db-migrate", "STUB: Migrate db.")
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
createCommand = kingpin.Command("create", "Create user.")
|
|
|
|
createCommandUserFlag = createCommand.Flag("user", "Username.").Required().String()
|
|
|
|
createCommandPasswordFlag = createCommand.Flag("password", "Password.").Required().String()
|
|
|
|
|
2022-05-23 09:21:54 -04:00
|
|
|
deleteCommand = kingpin.Command("delete", "Delete user.")
|
2022-11-18 07:47:42 -05:00
|
|
|
deleteCommandUserForceFlag = deleteCommand.Flag("force", "delete from db.").Short('f').Default("false").Bool()
|
|
|
|
deleteCommandUserFlag = deleteCommand.Flag("user", "Username.").Short('u').Required().String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
revokeCommand = kingpin.Command("revoke", "Revoke user.")
|
2022-11-18 07:47:42 -05:00
|
|
|
revokeCommandUserFlag = revokeCommand.Flag("user", "Username.").Short('u').Required().String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
restoreCommand = kingpin.Command("restore", "Restore user.")
|
2022-11-18 07:47:42 -05:00
|
|
|
restoreCommandUserFlag = restoreCommand.Flag("user", "Username.").Short('u').Required().String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
listCommand = kingpin.Command("list", "List active users.")
|
|
|
|
listCommandAllFlag = listCommand.Flag("all", "Show all users include revoked and deleted.").Short('a').Default("false").Bool()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
checkCommand = kingpin.Command("check", "check user existent.")
|
2022-11-18 07:47:42 -05:00
|
|
|
checkCommandUserFlag = checkCommand.Flag("user", "Username.").Short('u').Required().String()
|
2021-02-05 13:19:54 -05:00
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
authCommand = kingpin.Command("auth", "Auth user.")
|
2022-11-18 07:47:42 -05:00
|
|
|
authCommandUserFlag = authCommand.Flag("user", "Username.").Short('u').Required().String()
|
|
|
|
authCommandPasswordFlag = authCommand.Flag("password", "Password.").Short('p').String()
|
|
|
|
authCommandTotpFlag = authCommand.Flag("totp", "TOTP code.").Short('t').String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
changePasswordCommand = kingpin.Command("change-password", "Change password")
|
2022-11-18 07:47:42 -05:00
|
|
|
changePasswordCommandUserFlag = changePasswordCommand.Flag("user", "Username.").Short('u').Required().String()
|
|
|
|
changePasswordCommandPasswordFlag = changePasswordCommand.Flag("password", "Password.").Short('p').Required().String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
updateSecretCommand = kingpin.Command("update-secret", "update OTP secret")
|
2022-11-18 07:47:42 -05:00
|
|
|
updateSecretCommandUserFlag = updateSecretCommand.Flag("user", "Username.").Short('u').Required().String()
|
|
|
|
updateSecretCommandSecretFlag = updateSecretCommand.Flag("secret", "Secret.").Short('s').Default("generate").String()
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-14 11:26:34 -05:00
|
|
|
registerAppCommand = kingpin.Command("register-app", "register 2FA application")
|
2022-11-18 07:47:42 -05:00
|
|
|
registerAppCommandUserFlag = registerAppCommand.Flag("user", "Username.").Short('u').Required().String()
|
|
|
|
registerAppCommandTotpFlag = registerAppCommand.Flag("totp", "TOTP.").Short('t').Required().String()
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-29 10:22:39 -05:00
|
|
|
resetAppCommand = kingpin.Command("reset-app", "register 2FA application")
|
|
|
|
resetAppCommandUserFlag = resetAppCommand.Flag("user", "Username.").Short('u').Required().String()
|
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
checkAppCommand = kingpin.Command("check-app", "check 2FA application")
|
|
|
|
checkAppCommandUserFlag = checkAppCommand.Flag("user", "Username.").Short('u').Required().String()
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
getSecretCommand = kingpin.Command("get-secret", "get OTP secret")
|
|
|
|
getSecretCommandUserFlag = getSecretCommand.Flag("user", "Username.").Short('u').Required().String()
|
2021-02-05 01:37:12 -05:00
|
|
|
)
|
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
func main() {
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
args := kingpin.Parse()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
db, err := sql.Open("sqlite3", *dbPath)
|
|
|
|
if err != nil {
|
|
|
|
kingpin.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer func(db *sql.DB) {
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
kingpin.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}(db)
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
openvpnUser := src.OpenvpnUser{Database: db}
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
kingpin.Version(version).VersionFlag.Short('v')
|
2022-11-14 05:33:43 -05:00
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
switch args {
|
2021-02-05 01:37:12 -05:00
|
|
|
case createCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.CreateUser(*createCommandUserFlag, *createCommandPasswordFlag))
|
2021-02-05 01:37:12 -05:00
|
|
|
case deleteCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.DeleteUser(*deleteCommandUserFlag, *deleteCommandUserForceFlag))
|
2021-02-05 01:37:12 -05:00
|
|
|
case revokeCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.RevokedUser(*revokeCommandUserFlag))
|
2021-02-05 01:37:12 -05:00
|
|
|
case restoreCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.RestoreUser(*restoreCommandUserFlag))
|
2021-02-05 01:37:12 -05:00
|
|
|
case listCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
openvpnUser.PrintUsers(*listCommandAllFlag)
|
2021-02-05 13:19:54 -05:00
|
|
|
case checkCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
_ = openvpnUser.CheckUserExistent(*checkCommandUserFlag)
|
2021-02-05 01:37:12 -05:00
|
|
|
case authCommand.FullCommand():
|
2022-11-14 05:33:43 -05:00
|
|
|
provideAuthType := 0
|
|
|
|
if *authCommandPasswordFlag != "" {
|
|
|
|
provideAuthType += 1
|
|
|
|
}
|
|
|
|
if *authCommandTotpFlag != "" {
|
|
|
|
provideAuthType += 1
|
|
|
|
}
|
|
|
|
if provideAuthType == 1 {
|
2022-11-18 07:47:42 -05:00
|
|
|
authSuccessful, authErr := openvpnUser.AuthUser(*authCommandUserFlag, *authCommandPasswordFlag, *authCommandTotpFlag)
|
|
|
|
if authErr != nil {
|
|
|
|
kingpin.Fatalf(authErr.Error())
|
|
|
|
} else if authSuccessful {
|
|
|
|
fmt.Println("Authorization successful")
|
2022-12-02 06:12:43 -05:00
|
|
|
} else {
|
|
|
|
fmt.Println("Authorization failed")
|
2022-11-18 07:47:42 -05:00
|
|
|
}
|
2022-12-02 06:12:43 -05:00
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
} else {
|
2022-11-18 07:47:42 -05:00
|
|
|
fmt.Println("Please provide only one type of auth flag")
|
2022-11-14 05:33:43 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
case changePasswordCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.ChangeUserPassword(*changePasswordCommandUserFlag, *changePasswordCommandPasswordFlag))
|
2022-11-14 05:33:43 -05:00
|
|
|
case updateSecretCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.RegisterOtpSecret(*updateSecretCommandUserFlag, *updateSecretCommandSecretFlag))
|
2022-11-14 05:33:43 -05:00
|
|
|
case registerAppCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.RegisterOtpApplication(*registerAppCommandUserFlag, *registerAppCommandTotpFlag))
|
2022-11-29 10:22:39 -05:00
|
|
|
case resetAppCommand.FullCommand():
|
|
|
|
wrap(openvpnUser.ResetOtpApplication(*resetAppCommandUserFlag))
|
2022-11-18 07:47:42 -05:00
|
|
|
case checkAppCommand.FullCommand():
|
|
|
|
appConfigured, appErr := openvpnUser.IsSecondFactorEnabled(*checkAppCommandUserFlag)
|
|
|
|
if appErr != nil {
|
|
|
|
kingpin.Fatalf(appErr.Error())
|
|
|
|
} else if appConfigured {
|
|
|
|
fmt.Println("App configured")
|
2022-12-02 06:12:43 -05:00
|
|
|
} else {
|
|
|
|
fmt.Println("App not configured yet")
|
2022-11-18 07:47:42 -05:00
|
|
|
}
|
2022-11-14 05:33:43 -05:00
|
|
|
case getSecretCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
wrap(openvpnUser.GetUserOtpSecret(*getSecretCommandUserFlag))
|
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
case dbInitCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
openvpnUser.InitDb()
|
2021-02-05 01:37:12 -05:00
|
|
|
case dbMigrateCommand.FullCommand():
|
2022-11-18 07:47:42 -05:00
|
|
|
openvpnUser.MigrateDb()
|
2021-02-08 04:53:09 -05:00
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
2022-11-18 07:47:42 -05:00
|
|
|
func wrap(msg string, err error) {
|
2022-11-14 05:33:43 -05:00
|
|
|
if err != nil {
|
2022-11-18 07:47:42 -05:00
|
|
|
kingpin.Fatalf(err.Error())
|
2021-02-05 01:37:12 -05:00
|
|
|
} else {
|
2022-11-18 07:47:42 -05:00
|
|
|
fmt.Println(msg)
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
}
|