Skip to content

Instantly share code, notes, and snippets.

@kimboslice99
Created November 7, 2022 18:11
Show Gist options
  • Save kimboslice99/78bbc86af2cea0793a2aab973a05f534 to your computer and use it in GitHub Desktop.
Save kimboslice99/78bbc86af2cea0793a2aab973a05f534 to your computer and use it in GitHub Desktop.
Authenticate OpenVPN against a DB
# Script requires ODBC Connector MySql/MariaDB
$data = Get-Content "$args"
$username = $data[0]
$password = $data[1]
#connection - pwsh.exe Get-OdbcDriver
$driver = "MariaDB ODBC 3.1 Driver"
$Server = "127.0.0.1"
$DBName = "openvpn"
$User = "openvpn"
$PW = "abcd1234"
# Connect to the database
$Connection = New-Object System.Data.ODBC.ODBCConnection
$Connection.connectionstring = "DRIVER={$driver};" +
"Server = $Server;" +
"Database = $DBName;" +
"UID = $User;" +
"PWD= $PW;" +
"Option = 3"
$Connection.Open()
# Hint, create a DB with two varchar columns, one named password and another named user
$Query = "SELECT * FROM openvpn.users WHERE password = `"$password`" AND user = `"$username`""
$Command = New-Object System.Data.ODBC.ODBCCommand($Query, $Connection)
$DataTable = New-Object -TypeName System.Data.DataTable
$Reader = $Command.ExecuteReader()
$DataTable.Load($Reader)
if (@($DataTable).count -gt 0) { (Write-Host "ok"); exit 0 }
$Connection.Dispose()
Write-Host "not ok"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment