Files

69 lines
4.1 KiB
PowerShell

$ErrorActionPreference = 'Stop'
[Console]::InputEncoding = New-Object System.Text.UTF8Encoding($false)
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
try {
$orderSmbRequest = [Console]::In.ReadToEnd() | ConvertFrom-Json
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public static class OrderSmbNative {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHARE_INFO_1 { public string name; public uint type; public string remark; }
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
public static extern int NetShareEnum(string server, int level, out IntPtr buffer, int length, out int read, out int total, ref int resume);
[DllImport("Netapi32.dll")] public static extern int NetApiBufferFree(IntPtr buffer);
public static string[] ListShares(string server) {
var names = new System.Collections.Generic.List<string>(); int resume = 0, code;
do {
IntPtr buffer; int read, total;
code = NetShareEnum(server, 1, out buffer, 16384, out read, out total, ref resume);
try {
if (code != 0 && code != 234) throw new System.ComponentModel.Win32Exception(code);
int stride = Marshal.SizeOf(typeof(SHARE_INFO_1));
for (int i = 0; i < read && names.Count < 501; i++) {
var item = (SHARE_INFO_1)Marshal.PtrToStructure(IntPtr.Add(buffer, i * stride), typeof(SHARE_INFO_1));
if ((item.type & 255) == 0 && !item.name.EndsWith("$")) names.Add(item.name);
}
} finally { if (buffer != IntPtr.Zero) NetApiBufferFree(buffer); }
} while (code == 234 && names.Count < 501);
return names.ToArray();
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NETRESOURCE {
public int scope, type, displayType, usage;
public string localName, remoteName, comment, provider;
}
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
public static extern int WNetAddConnection2W(ref NETRESOURCE resource, string password, string username, int flags);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool MoveFileExW(string existingName, string newName, int flags);
}
'@
if ($orderSmbRequest.operation -eq 'shares') {
if ($orderSmbRequest.authMode -eq 'credentials') {
$orderSmbResource = New-Object OrderSmbNative+NETRESOURCE
$orderSmbResource.type = 0
$orderSmbResource.remoteName = '\\' + $orderSmbRequest.host + '\IPC$'
$orderSmbCode = [OrderSmbNative]::WNetAddConnection2W([ref]$orderSmbResource, $orderSmbRequest.password, $orderSmbRequest.username, 4)
if ($orderSmbCode -ne 0) { @{ok=$false;code=$orderSmbCode} | ConvertTo-Json -Compress; exit }
}
$orderSmbNames = @([OrderSmbNative]::ListShares('\\' + $orderSmbRequest.host))
@{ok=$true;code=0;shares=$orderSmbNames} | ConvertTo-Json -Compress -Depth 4
exit
} elseif ($orderSmbRequest.operation -eq 'connect') {
$orderSmbResource = New-Object OrderSmbNative+NETRESOURCE
$orderSmbResource.type = 1
$orderSmbResource.remoteName = $orderSmbRequest.remote
$orderSmbCode = [OrderSmbNative]::WNetAddConnection2W([ref]$orderSmbResource, $orderSmbRequest.password, $orderSmbRequest.username, 4)
} elseif ($orderSmbRequest.operation -eq 'publish') {
$orderSmbMoved = [OrderSmbNative]::MoveFileExW($orderSmbRequest.source, $orderSmbRequest.target, 8)
$orderSmbCode = if ($orderSmbMoved) { 0 } else { [Runtime.InteropServices.Marshal]::GetLastWin32Error() }
} else { throw 'Unsupported operation' }
@{ ok = ($orderSmbCode -eq 0); code = $orderSmbCode } | ConvertTo-Json -Compress
} catch {
$orderSmbCause = $_.Exception
while ($orderSmbCause.InnerException) { $orderSmbCause = $orderSmbCause.InnerException }
$orderSmbErrorCode = if ($orderSmbCause -is [ComponentModel.Win32Exception]) { $orderSmbCause.NativeErrorCode } else { -1 }
@{ ok = $false; code = $orderSmbErrorCode } | ConvertTo-Json -Compress
}