Compare commits

..

1 Commits

Author SHA1 Message Date
Genva
c820c1e741
Merge 5b5d1301c9 into 676dd7b85c 2025-06-16 10:22:49 +02:00
4 changed files with 80 additions and 63 deletions

View File

@ -9,12 +9,17 @@ echo "Building BitBetter for BitWarden version $BW_VERSION"
# If there aren't any keys, generate them first.
[ -e "$DIR/.keys/cert.cert" ] || "$DIR/.keys/generate-keys.sh"
# Prepare Bitwarden server repository
# Prepare Bitwarden repository
rm -rf $DIR/server
git clone --branch "v${BW_VERSION}" --depth 1 https://github.com/bitwarden/server.git $DIR/server
old_thumbprint=$(openssl x509 -fingerprint -noout -in $DIR/server/src/Core/licensing.cer | cut -d= -f2 | tr -d ':')
new_thumbprint=$(openssl x509 -fingerprint -noout -in $DIR/.keys/cert.cert | cut -d= -f2 | tr -d ':')
cp $DIR/.keys/cert.cert $DIR/server/src/Core/licensing.cer
# Optional, has actually no effect
sed -i -e "s/$old_thumbprint/$new_thumbprint/g" $DIR/server/src/Core/Services/Implementations/LicensingService.cs
# Enable loose files for API, so Core.dll is accessible
sed -i -e 's/PublishSingleFile=true/PublishSingleFile=false/g' $DIR/server/src/Api/Dockerfile
# Build docker images
docker build --no-cache --label com.bitwarden.product="bitbetter" $DIR/server -f $DIR/server/src/Api/Dockerfile -t bitbetter/api
docker build --no-cache --label com.bitwarden.product="bitbetter" $DIR/server -f $DIR/server/src/Identity/Dockerfile -t bitbetter/identity

View File

@ -14,4 +14,4 @@ FROM bitbetter/api
COPY --from=build /licenseGen/bin/Release/net8.0/publish/* /app/
ENTRYPOINT [ "dotnet", "/app/licenseGen.dll", "--core", "/app/Core.dll", "--executable", "/app/Api", "--cert", "/cert.pfx" ]
ENTRYPOINT [ "dotnet", "/app/licenseGen.dll", "--core", "/app/Core.dll", "--cert", "/cert.pfx" ]

View File

@ -1,40 +1,38 @@
namespace BitwardenSelfLicensor
{
using Microsoft.Extensions.CommandLineUtils;
using Newtonsoft.Json;
using SingleFileExtractor.Core;
using System;
using System.IO;
using System.Runtime.Loader;
using System.Security.Cryptography.X509Certificates;
using System;
using System.IO;
using System.Linq;
using System.Runtime.Loader;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.CommandLineUtils;
using Newtonsoft.Json;
public static class Program
namespace bitwardenSelfLicensor
{
class Program
{
public static int Main(string[] args)
static int Main(string[] args)
{
var app = new CommandLineApplication();
var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication();
var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue);
var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue);
var exec = app.Option("--executable", "path to Bitwarden single file executable", CommandOptionType.SingleValue);
bool ExecExists() => File.Exists(exec.Value());
bool CertExists() => File.Exists(cert.Value());
bool CoreExists() => File.Exists(coreDll.Value());
bool VerifyTopOptions() =>
!string.IsNullOrWhiteSpace(cert.Value()) &&
(!string.IsNullOrWhiteSpace(coreDll.Value()) || !string.IsNullOrWhiteSpace(exec.Value())) &&
CertExists() &&
(CoreExists() || ExecExists());
string GetExtractedDll()
bool certExists()
{
var coreDllPath = Path.Combine("extract", "Core.dll");
var reader = new ExecutableReader(exec.Value());
reader.ExtractToDirectory("extract");
var fileInfo = new FileInfo(coreDllPath);
return fileInfo.FullName;
return File.Exists(cert.Value());
}
string GetCoreDllPath() => CoreExists() ? coreDll.Value() : GetExtractedDll();
bool coreExists()
{
return File.Exists(coreDll.Value());
}
bool verifyTopOptions()
{
return !string.IsNullOrWhiteSpace(cert.Value()) &&
!string.IsNullOrWhiteSpace(coreDll.Value()) &&
certExists() && coreExists();
}
app.Command("interactive", config =>
{
string buff="", licensetype="", name="", email="", businessname="";
@ -45,11 +43,11 @@ namespace BitwardenSelfLicensor
config.OnExecute(() =>
{
if (!VerifyTopOptions())
if (!verifyTopOptions())
{
if (!ExecExists() && !string.IsNullOrWhiteSpace(exec.Value())) config.Error.WriteLine($"Cant find single file executable at: {exec.Value()}");
if (!CoreExists() && !string.IsNullOrWhiteSpace(coreDll.Value())) config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
if (!CertExists()) config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
if (!coreExists()) config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
if (!certExists()) config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
config.ShowHelp();
return 1;
}
@ -94,7 +92,7 @@ namespace BitwardenSelfLicensor
WriteLineOver("Please enter a business name, default is BitBetter. [Business Name]:");
buff = Console.ReadLine();
if (buff == "") businessname = "BitBetter";
else if (CheckBusinessName(buff)) businessname = buff;
else if (checkBusinessName(buff)) businessname = buff;
}
}
else
@ -107,14 +105,14 @@ namespace BitwardenSelfLicensor
{
WriteLineOver("Please provide the username this license will be registered to. [username]:");
buff = Console.ReadLine();
if ( CheckUsername(buff) ) name = buff;
if ( checkUsername(buff) ) name = buff;
}
while (email == "")
{
WriteLineOver("Please provide the email address for the user " + name + ". [email]");
buff = Console.ReadLine();
if ( CheckEmail(buff) ) email = buff;
if ( checkEmail(buff) ) email = buff;
}
while (storage == 0)
@ -127,7 +125,7 @@ namespace BitwardenSelfLicensor
}
else
{
if (CheckStorage(buff)) storage = short.Parse(buff);
if (checkStorage(buff)) storage = short.Parse(buff);
}
}
@ -137,7 +135,7 @@ namespace BitwardenSelfLicensor
buff = Console.ReadLine();
if ( buff == "" || buff == "y" || buff == "Y" )
{
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), GetCoreDllPath(), name, email, storage, guid, null);
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, guid, null);
}
else
{
@ -151,7 +149,7 @@ namespace BitwardenSelfLicensor
buff = Console.ReadLine();
if ( buff == "" || buff == "y" || buff == "Y" )
{
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), GetCoreDllPath(), name, email, storage, installid, businessname, null);
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, installid, businessname, null);
}
else
{
@ -175,11 +173,17 @@ namespace BitwardenSelfLicensor
config.OnExecute(() =>
{
if (!VerifyTopOptions())
if (!verifyTopOptions())
{
if (!ExecExists() && !string.IsNullOrWhiteSpace(exec.Value())) config.Error.WriteLine($"Cant find single file executable at: {exec.Value()}");
if (!CoreExists() && !string.IsNullOrWhiteSpace(coreDll.Value())) config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
if (!CertExists()) config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
if (!coreExists())
{
config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
}
if (!certExists())
{
config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
}
config.ShowHelp();
return 1;
}
@ -210,7 +214,7 @@ namespace BitwardenSelfLicensor
storageShort = (short) parsedStorage;
}
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), GetCoreDllPath(), name.Value, email.Value, storageShort, userId, key.Value);
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, storageShort, userId, key.Value);
return 0;
});
@ -227,11 +231,17 @@ namespace BitwardenSelfLicensor
config.OnExecute(() =>
{
if (!VerifyTopOptions())
if (!verifyTopOptions())
{
if (!ExecExists() && !string.IsNullOrWhiteSpace(exec.Value())) config.Error.WriteLine($"Cant find single file executable at: {exec.Value()}");
if (!CoreExists() && !string.IsNullOrWhiteSpace(coreDll.Value())) config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
if (!CertExists()) config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
if (!coreExists())
{
config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
}
if (!certExists())
{
config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
}
config.ShowHelp();
return 1;
}
@ -265,7 +275,7 @@ namespace BitwardenSelfLicensor
storageShort = (short) parsedStorage;
}
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), GetCoreDllPath(), name.Value, email.Value, storageShort, installationId, businessName.Value, key.Value);
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, storageShort, installationId, businessName.Value, key.Value);
return 0;
});
@ -291,7 +301,7 @@ namespace BitwardenSelfLicensor
}
// checkUsername Checks that the username is a valid username
private static bool CheckUsername(string s)
static bool checkUsername(string s)
{
if ( string.IsNullOrWhiteSpace(s) ) {
WriteLineOver("The username provided doesn't appear to be valid.\n");
@ -301,7 +311,7 @@ namespace BitwardenSelfLicensor
}
// checkBusinessName Checks that the Business Name is a valid username
private static bool CheckBusinessName(string s)
static bool checkBusinessName(string s)
{
if ( string.IsNullOrWhiteSpace(s) ) {
WriteLineOver("The Business Name provided doesn't appear to be valid.\n");
@ -311,7 +321,7 @@ namespace BitwardenSelfLicensor
}
// checkEmail Checks that the email address is a valid email address
private static bool CheckEmail(string s)
static bool checkEmail(string s)
{
if ( string.IsNullOrWhiteSpace(s) ) {
WriteLineOver("The email provided doesn't appear to be valid.\n");
@ -321,7 +331,7 @@ namespace BitwardenSelfLicensor
}
// checkStorage Checks that the storage is in a valid range
private static bool CheckStorage(string s)
static bool checkStorage(string s)
{
if (string.IsNullOrWhiteSpace(s))
{
@ -337,16 +347,19 @@ namespace BitwardenSelfLicensor
}
// WriteLineOver Writes a new line to console over last line.
private static void WriteLineOver(string s)
static void WriteLineOver(string s)
{
Console.SetCursorPosition(0, Console.CursorTop -1);
Console.WriteLine(s);
}
// WriteLine This wrapper is just here so that console writes all look similar.
private static void WriteLine(string s) => Console.WriteLine(s);
static void WriteLine(string s)
{
Console.WriteLine(s);
}
private static void GenerateUserLicense(X509Certificate2 cert, string corePath, string userName, string email, short storage, Guid userId, string key)
static void GenerateUserLicense(X509Certificate2 cert, string corePath, string userName, string email, short storage, Guid userId, string key)
{
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
@ -379,7 +392,7 @@ namespace BitwardenSelfLicensor
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
}
private static void GenerateOrgLicense(X509Certificate2 cert, string corePath, string userName, string email, short storage, Guid instalId, string businessName, string key)
static void GenerateOrgLicense(X509Certificate2 cert, string corePath, string userName, string email, short storage, Guid instalId, string businessName, string key)
{
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -7,8 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SingleFileExtractor.Core" Version="2.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>