1
0
Fork 0
mirror of synced 2024-11-22 09:05:34 -05:00
openvpn-user/openvpn-user.go

157 lines
6.1 KiB
Go
Raw Permalink Normal View History

2021-02-05 01:37:12 -05:00
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"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.")
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.")
revokeCommandUserFlag = revokeCommand.Flag("user", "Username.").Short('u').Required().String()
2021-02-05 01:37:12 -05:00
restoreCommand = kingpin.Command("restore", "Restore user.")
restoreCommandUserFlag = restoreCommand.Flag("user", "Username.").Short('u').Required().String()
2021-02-05 01:37:12 -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.")
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.")
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")
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")
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")
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()
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
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
)
func main() {
2022-11-14 05:33:43 -05:00
args := kingpin.Parse()
2021-02-05 01:37:12 -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
openvpnUser := src.OpenvpnUser{Database: db}
2022-11-14 05:33:43 -05:00
kingpin.Version(version).VersionFlag.Short('v')
2022-11-14 05:33:43 -05:00
switch args {
2021-02-05 01:37:12 -05:00
case createCommand.FullCommand():
wrap(openvpnUser.CreateUser(*createCommandUserFlag, *createCommandPasswordFlag))
2021-02-05 01:37:12 -05:00
case deleteCommand.FullCommand():
wrap(openvpnUser.DeleteUser(*deleteCommandUserFlag, *deleteCommandUserForceFlag))
2021-02-05 01:37:12 -05:00
case revokeCommand.FullCommand():
wrap(openvpnUser.RevokedUser(*revokeCommandUserFlag))
2021-02-05 01:37:12 -05:00
case restoreCommand.FullCommand():
wrap(openvpnUser.RestoreUser(*restoreCommandUserFlag))
2021-02-05 01:37:12 -05:00
case listCommand.FullCommand():
openvpnUser.PrintUsers(*listCommandAllFlag)
2021-02-05 13:19:54 -05:00
case checkCommand.FullCommand():
_ = 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 {
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-12-02 06:12:43 -05:00
2022-11-14 05:33:43 -05:00
} else {
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():
wrap(openvpnUser.ChangeUserPassword(*changePasswordCommandUserFlag, *changePasswordCommandPasswordFlag))
2022-11-14 05:33:43 -05:00
case updateSecretCommand.FullCommand():
wrap(openvpnUser.RegisterOtpSecret(*updateSecretCommandUserFlag, *updateSecretCommandSecretFlag))
2022-11-14 05:33:43 -05:00
case registerAppCommand.FullCommand():
wrap(openvpnUser.RegisterOtpApplication(*registerAppCommandUserFlag, *registerAppCommandTotpFlag))
2022-11-29 10:22:39 -05:00
case resetAppCommand.FullCommand():
wrap(openvpnUser.ResetOtpApplication(*resetAppCommandUserFlag))
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-14 05:33:43 -05:00
case getSecretCommand.FullCommand():
wrap(openvpnUser.GetUserOtpSecret(*getSecretCommandUserFlag))
2021-02-05 01:37:12 -05:00
case dbInitCommand.FullCommand():
openvpnUser.InitDb()
2021-02-05 01:37:12 -05:00
case dbMigrateCommand.FullCommand():
openvpnUser.MigrateDb()
2021-02-08 04:53:09 -05:00
}
2021-02-05 01:37:12 -05:00
}
func wrap(msg string, err error) {
2022-11-14 05:33:43 -05:00
if err != nil {
kingpin.Fatalf(err.Error())
2021-02-05 01:37:12 -05:00
} else {
fmt.Println(msg)
2021-02-05 01:37:12 -05:00
}
}