Atempo.Lina.psm1

<#
=========== MODULE STRUCTURE ===========
 
    This module contains the following file structure
        -- Atempo.Lina.psm1 : Global variables + Loading of functions (Common,Get,Set,New,Remove)
        -- Atempo.Lina.psd1 : Lina PowerShell Module manifest (description, release notes, icon, author etc)
        -- help.html : HTML Help file (usually opened using Get-LinaHelp)
        -- Functions/ : Folder containing all the functions/cmdlets
                |-- Common.ps1 : Connect/Disconnect and all internal functions
                |-- Get.ps1 : Get-Lina* / Listing elements
                |-- New.ps1 : New-Lina* / Creating elements
                |-- Remove.ps1 : Remove-Lina* / Deleting elements
                |-- Set.ps1 : Set-Lina* / Modifying elements
 
=========== TODO ===========
    Try & Catch error on connect => Exit
    Error returns
    Check global enable ssl certification validation
 
    Test Lina 5.2 FQDN return
    AutoDoc in common => needs to be finalized or removed
    New-LinaUserProfile from scratch ?
    AppVeyor Continuous integration + Docker on premise for testing ?
    Write-host green/red/orange
    All LinaUser cmdlet
    Manage disconnections error
    Use Invoke-WebRequest instead of RestMethod => uniformize returns inside functions and improve debug like in Miria module
    Use SecurePassword
    Clone a strategy ?
    Creation of Protections ?
    Managing LinaTenant auto-assignment rules
    manage list of replications
    managing all missing objects (paths, rules etc)
    Ability to pipe commands => should be done everywhere
#>


# Needed for URLEncode
Add-Type -AssemblyName System.Web

# Can be set to $False from calling script to enforce certificate checking
$global:GLOBAL_IGNORE_CERTIFICATES = $True

# Use TLS v1.2 By default to avoid connection issues and increase security
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Can be set to $True from calling script to enable verbose logging
$global:LINA_DEBUG_MODE=$False

# Can be set to $True from calling script to enable auto-documenting
$global:LINA_DOC_MODE=$False
$global:LINA_DOC_OUTPUT=""
$global:LINA_DOC_URL_DONE=@()

# ============ Internal Variables =============== #

# Path to the Functions dir containing all functions
$global:FUNCTIONS_DIR  = @( Get-ChildItem -Path $PSScriptRoot\Functions\*.ps1 -ErrorAction SilentlyContinue )

$global:LoggedSession = $null
$global:GLOBAL_LINA_SERVER = $null
$global:LINA_TRANSLATIONS = $null
$global:LINA_VERSION = $null
$global:INT_LINA_API_VERSION = $null

# Mapping PowerShell Lina Strategies Properties to Lina REST API properties
$global:INT_STRATEGIES_MAPPINGS= @{
    Name                        = "Name" ;
    RPOInMinutes                = "ParamSchedule" ;
    RetentionInDays             = "ParamDataAging" ;
    AlertAfterDays              = "ParamAlertTime" ;
    ThroughputLimitKBps         = "ParamThroughputLimit" ;
    AlwaysEncrypt               = "ParamEncryptionActivated" ;
    WanMode                     = "ParamWanActivated" ;
    CompressionAlgo             = "ParamCompression" ;
    ReplicationTargetID         = "ParamRepliTargetID";
    AllowClientRPO              = "ParamAllowClientRPO" ;
    AllowClientRules            = "ParamAllowClientRules" ;
    AllowClientPause            = "ParamAllowClientPause" ;
    AllowClientBoost            = "ParamAllowClientBoost" ;
    AllowClientNetworkParams    = "ParamAllowClientNetworkParams" ;
    AllowWebAccess              = "ParamAllowWebAccess" ;
    QuotaMaxProtSizeMB          = "ParamQuotaMaxProtSize" ;
    QuotaMaxProtObjs            = "ParamQuotaMaxProtObjs" ;
    QuotaMaxHistSizeMB          = "ParamQuotaMaxHistSize" ;
    QuotaMaxHistObjs            = "ParamQuotaMaxHistObjs" ;
    ReplicationTargets          = "RepliTarget" ;
    TenantID                    = "DomainID"
}
# Possible values for User Types (0, 3, 4 may not exist but it's to avoid crashes just in case)
$global:INT_USER_TYPES= @{ 
    1 = "Local";
    2 = "LDAP";
    3 = "LDAP Group";
    4 = "Unknown"
}

$global:INT_API_ERRORCODES= @(
        (0, "ASM_OK", "[0] - OK"),
        (1, "ASM_ERR",  "[1] - General error"),
        (2, "ASM_ERR_MEM", "[2] - Out of memory"),
        (3, "ASM_ERR_EOF", "[3] - End of file or end of stream"),
        (4, "ASM_ERR_READ", "[4] - Read error"),
        (5, "ASM_ERR_WRITE", "[5] - Write error"),
        (6, "ASM_ERR_RANGE", "[6] - Value out of range"),
        (7, "ASM_ERR_FORMAT", "[7] - Corrupt format"),
        (8, "ASM_ERR_PERM", "[8] - Permission denied"),
        (9, "ASM_ERR_ABORT", "[9] - Abort"),
        (10, "ASM_ERR_FNF", "[10] - File/Object not found"),
        (11, "ASM_ERR_EXIST", "[11] - Object/entity already exists"),
        (12, "ASM_ERR_NOT_EXIST", "[12] - Object/entity does not exist"),
        (13, "ASM_ERR_NO_ADMIN", "[13] - Access denied. Administration rights are required"),
        (14, "ASM_ERR_COMM", "[14] - Network error or broken connection"),
        (15, "ASM_ERR_BAD_CPR", "[15] - Block already compressed"),
        (16, "ASM_ERR_BAD_STREAM", "[16] - Stream is corrupt"),
        (17, "ASM_ERR_DELETED_STREAM", "[17] - Stream is not available because it is deleted"),
        (18, "ASM_ERR_HEADER_CORRUPTED", "[18] - Corrupt header"),
        (19, "ASM_ERR_TRAILER_CORRUPTED", "[19] - Corrupt trailer"),
        (20, "ASM_ERR_CHECKSUM", "[20] - Bad checksum/digest"),
        (21, "ASM_ERR_NOT_IMPLEMENTED", "[21] - Operation not yet implemented"),
        (22, "ASM_ERR_ILLEGAL_CASE", "[22] - Illegal case"),
        (23, "ASM_ERR_CREATE_THREAD", "[23] - Could not start a thread"),
        (24, "ASM_ERR_CPR", "[24] - Compress error"),
        (25, "ASM_ERR_UNCPR", "[25] - Uncompress error"),
        (26, "ASM_ERR_CANT_OPEN_FILE", "[26] - Could not open file (bad path or permission)"),
        (27, "ASM_ERR_PROG_INCONS", "[27] - Coding error"),
        (28, "ASM_ERR_HOTBACKUP_IN_PROGRESS", "[28] - Hot backup in progress. Operation will be delayed"),
        (29, "ASM_ERR_GET_INTEGER", "[29] - Receive an integer"),
        (30, "ASM_ERR_EXIT", "[30] - Exit"),
        (31, "ASM_ERR_CONFIG", "[31] - Configuration error"),
        (32, "ASM_ERR_LOG", "[32] - Fail to build or log event"),
        (33, "ASM_ERR_TIME_OUT", "[33] - No respond or operation takes too long time"),
        (34, "ASM_ERR_GO", "[34] - Begins the task"),
        (35, "ASM_ERR_INVALID_PARAM", "[35] - Invalid parameter"),
        (36, "ASM_ERR_DB", "[36] - Database operation failure"),
        (37, "ASM_ERR_DB_EXEC", "[37] - SQL request failed"),
        (38, "ASM_ERR_BAD_PASSWORD", "[38] - Wrong login or password"),
        (39, "ASM_ERR_FS_FULL", "[39] - Filesystem is full"),
        (40, "ASM_ERR_FS_NEAR_FULL", "[40] - Filesystem is nearly full"),
        (41, "ASM_ERR_NOT_REACHABLE", "[41] - Server not reachable"),
        (42, "ASM_ERR_NOTHING_DONE", "[42] - Nothing was done"),
        (43, "ASM_ERR_SERVICE_NOT_AVAILABLE", "[43] - Service not available or not enabled"),
        (44, "ASM_ERR_OLD_VERSION", "[44] - Client version is not compatible with server version"),
        (45, "ASM_ERR_FULL", "[45] - Container full or upper limit reached"),
        (46, "ASM_ERR_REQ", "[46] - Unable to perform request"),
        (47, "ASM_ERR_OS_NOT_SUPPORTED", "[47] - OS version not supported"),
        (48, "ASM_ERR_RETRY", "[48] - Operation can't be done now"),
        (49, "ASM_ERR_EMPTY", "[49] - Empty"),
        (50, "ASM_ERR_LICENSE", "[50] - License error"),
        (51, "ASM_ERR_INVALID_SESS", "[51] - Invalid session"),
        (52, "ASM_ERR_MAINTENANCE_MODE", "[52] - Maintenance mode"),
        (53, "ASM_ERR_BUFFER_TOO_SMALL", "[53] - Buffer too small"),
        (54, "ASM_ERR_SHUTDOWN", "[54] - Server is stopping"),
        (55, "ASM_ERR_CORRUPTED", "[55] - Block is corrupted"),
        (56, "ASM_ERR_REMOVED", "[56] - Removed"),
        (57, "ASM_ERR_REPLI_IN_PROGRESS", "[57] - Stream replication in progress"),
        (58, "ASM_ERR_INMEM_FULL", "[58] - InMemory DB is full"),
        (59, "ASM_ERR_PRODUCT_DISABLED", "[59] - Product is disabled"),
        (60, "ASM_ERR_ASK_ACK", "[60] - Need an ACK"),
        (61, "ASM_ERR_BLOCK_NOT_FOUND", "[61] - Block not found"),
        (62, "ASM_ERR_DEPRECATED", "[62] - Deprecated"),
        (63, "ASM_ERR_USER_DISABLED", "[63] - User disabled"),
        (64, "ASM_ERR_NO_COOKIE", "[64] - No cookie available"),
        (65, "ASM_ERR_PERM_ENFORCEMENT", "[65] - Forbidden to give higher permission to someone"),
        (66, "ASM_ERR_PERM_OWNER", "[66] - Forbidden to alter his own permission"),
        (67, "ASM_ERR_PERM_READONLY_TENANT", "[67] - Forbidden to alter somethings his this tenant"),
        (68, "ASM_ERR_NOT_ENOUGH_SPACE", "[68] - Not enough free space on disks"),
        (69, "ASM_ERR_NOT_EMPTY", "[69] - Not empty"),
        (70, "ASM_ERR_IN_USE", "[70] - In use"),
        (71, "ASM_ERR_BLOCK_SKIPPED", "[71] - Block out of format, skipped"),
        (72, "ASM_ERR_NEED_2FA", "[72] - Need double authentication code")
    )

# Checking PowerShell version because Get-LinaUserProfile is using Enum variables and requires PowerShell >= 5
if ($PSVersionTable.PSVersion.Major -ge 5) {
    $global:ROLES1_LIST = [flags()] Enum ROLES1_LIST {
        ViewAlerts = 0x1
        SetAlerts  = 0x2
        ViewSupport = 0x4
        ViewUserProfiles   = 0x8
        CreateUserProfiles = 0x10
        DeleteUserProfiles = 0x20
        SetUserProfiles    = 0x40
        ViewUsers   = 0x80
        CreateUsers = 0x100
        DeleteUsers = 0x200
        SetUsers    = 0x400
        ViewServices = 0x800
        SetServices  = 0x1000
        ViewReplications   = 0x2000
        CreateReplications = 0x4000
        DeleteReplications = 0x8000
        SetReplications    = 0x10000
        AllowCrossResto = 0x200000
        AllowWebResto   = 0x400000
        ViewBMR     = 0x800000
        SetBMR      = 0x1000000
        AllowBMRResto   = 0x2000000 
    }
    $global:ROLES2_LIST = [flags()] Enum ROLES2_LIST {
        ViewFiles = 0x1
        ViewLicense = 0x2
        SetLicense  = 0x4
        ViewSmtp = 0x8 
        SetSmtp  = 0x10 
        ViewLdap = 0x20
        SetLdap  = 0x40
        ViewTunables         = 0x80
        SetTunables          = 0x100
        ViewAdvancedTunables = 0x200
        ViewStreams   = 0x400
        DeleteStreams = 0x800
        ViewStatistics      = 0x1000
        RecomputeStatistics = 0x2000
        ViewConfig   = 0x4000
        SetConfig    = 0x8000
        # Added Service after Restart to work automatically in Translate
        RestartService   = 0x10000
        ViewSnapshot   = 0x20000
        CreateSnapshot = 0x40000
        DeleteSnapshot = 0x80000 
    }
    $global:ROLES3_LIST = [flags()] Enum ROLES3_LIST {
        ViewAlnAgent   = 0x1
        CreateAlnAgent = 0x2
        DeleteAlnAgent = 0x4
        SetAlnAgent    = 0x8
        
        ViewAlnStrategy   = 0x10
        CreateAlnStrategy = 0x20
        DeleteAlnStrategy = 0x40
        SetAlnStrategy    = 0x80
        
        ViewAlnProtection   = 0x100
        CreateAlnProtection = 0x200
        DeleteAlnProtection = 0x400
        SetAlnProtection    = 0x800
        
        ViewAlnProtectionRule   = 0x1000
        CreateAlnProtectionRule = 0x2000
        DeleteAlnProtectionRule = 0x4000
        SetAlnProtectionRule    = 0x8000
    }
    $global:ROLES4_LIST = [flags()] Enum ROLES4_LIST {
        ViewAlnProtectionZone   = 0x1
        CreateAlnProtectionZone = 0x2
        DeleteAlnProtectionZone = 0x4
        SetAlnProtectionZone    = 0x8
        
        ViewAlnFileCategory   = 0x10
        CreateAlnFileCategory = 0x20
        DeleteAlnFileCategory = 0x40
        SetAlnFileCategory    = 0x80
        
        SetAlnDefaultConfiguration = 0x100
        
        ViewDomains = 0x200
        CreateDomains = 0x400
        DeleteDomains = 0x800
        SetDomains = 0x1000
 
        SetAlnGlobalDefaultConfiguration = 0x2000
    }

    # Calculating max values for each role, used for builtin accounts with negative values
    $global:ROLES1_MAX=0  
    [enum]::GetNames([ROLES1_LIST]) | ForEach-Object {$ROLES1_MAX+=$([ROLES1_LIST]::$_.value__)}
    $global:ROLES2_MAX=0
    [enum]::GetNames([ROLES2_LIST]) | ForEach-Object {$ROLES2_MAX+=$([ROLES2_LIST]::$_.value__)}
    $global:ROLES3_MAX=0 
    [enum]::GetNames([ROLES3_LIST]) | ForEach-Object {$ROLES3_MAX+=$([ROLES3_LIST]::$_.value__)}
    $global:ROLES4_MAX=0  
    [enum]::GetNames([ROLES4_LIST]) | ForEach-Object {$ROLES4_MAX+=$([ROLES4_LIST]::$_.value__)}


}else {
    # Declaring them for PS < 5.0 but won't work. Needs a workaround to avoid using Enum + flags (bitmasks)
    # Trying to avoid some crashes
    $global:ROLES2_LIST=@()
    $global:ROLES3_LIST=@()
    $global:ROLES4_LIST=@()
    $global:ROLES1_MAX=0
    $global:ROLES2_MAX=0  
    $global:ROLES3_MAX=0  
    $global:ROLES4_MAX=0 
}

# Loading dynamically all scripts containing the functions in Functions/
Foreach($import in @($FUNCTIONS_DIR))
{
    Try
    {
        . $import.fullname
    }
    Catch
    {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}