Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a firewall rule for Distributed Transaction Coordinator (msdtc.exe)

I tried to use firewallAPI.dll to add a rule. It works fine for calc.exe (or some other files) as described bellow but fails for msdtc.exe with the following exception:

System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'

Example:

static void Main(string[] args)
{
    var manager = GetFirewallManager();
    if (manager.LocalPolicy.CurrentProfile.FirewallEnabled)
    {
        var path = @"C:\Windows\System32\calc.exe";
        //var path = @"C:\Windows\System32\msdtc.exe"; // System.IO.FileNotFoundException: 'The system cannot find the file specified.
        AuthorizeApplication("Test", path, NET_FW_SCOPE_.NET_FW_SCOPE_ALL, NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);
    }
}

private const string CLSID_FIREWALL_MANAGER =
    "{304CE942-6E39-40D8-943A-B913C40C9CD4}";

private static NetFwTypeLib.INetFwMgr GetFirewallManager()
{
    Type objectType = Type.GetTypeFromCLSID(
        new Guid(CLSID_FIREWALL_MANAGER));
    return Activator.CreateInstance(objectType)
        as NetFwTypeLib.INetFwMgr;
}

private const string PROGID_AUTHORIZED_APPLICATION =
    "HNetCfg.FwAuthorizedApplication";
public static bool AuthorizeApplication(string title, string applicationPath,
    NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion)
{
    // Create the type from prog id
    Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION);
    INetFwAuthorizedApplication auth = Activator.CreateInstance(type)
        as INetFwAuthorizedApplication;
    auth.Name = title;
    auth.ProcessImageFileName = applicationPath;
    auth.Scope = scope;
    auth.IpVersion = ipVersion;
    auth.Enabled = true;

    INetFwMgr manager = GetFirewallManager();
    manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth);
    return true;
}

Note: I checked the folder and see the file is located properly... Could anybody help to add firewall rule for Distributed Transaction Coordinator? Maybe I should try to add another file to firewall (not msdtc.exe)?

like image 506
Serg046 Avatar asked Jun 02 '17 22:06

Serg046


People also ask

How do I enable Microsoft Distributed Transaction Coordinator?

On the MSDTC tab, click Security Configuration under Transaction Configuration, click to select the Network DTC Access check box under Security Settings, and then click to select the following check boxes under Transaction Manager Communication: Allow Inbound. Allow Outbound.

What ports need to be open for MSDTC?

All ports must be in the range of 1024 to 65535. If any port is outside this range or if any string is invalid, RPC will treat the entire configuration as invalid. Microsoft recommends that you open up ports from 5000 and up, and that you open a minimum of 15 to 20 ports.

How do I install MSDTC roles?

To enable MSDTC service: Choose Start > (All) Programs > Administrative tools > Component Services. Expand Computers and choose My Computer. Right-click My Computer, choose Distributed Transaction Coordinator, right-click Local DTC and choose Properties > Security.


1 Answers

Project > Properties > Build tab, untick the "Prefer 32-bit" checkbox. You don't prefer it, there is no 32-bit version of msdtc.exe.

Why the file system redirector caused the FileNotFoundException is explained well in this MSDN article.

like image 143
Hans Passant Avatar answered Oct 05 '22 12:10

Hans Passant