private static bool IsFbaEnabled(string server, string localMetabasePath) { IMSAdminBase iisAdmin = IMSAdminBaseHelper.Create(server); string metabasePath = "/LM" + localMetabasePath; OwaIsapiFilter.FormsAuthPropertyFlags formsAuthPropertyFlags; int flags = OwaIsapiFilter.GetFlags(iisAdmin, metabasePath, out formsAuthPropertyFlags); if (flags == -2146646015) { return(false); } if (flags == -2147024891) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationIsEnabledAccessDeniedException(metabasePath, 45054), flags); } if (flags == -2147024893) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationIsEnabledPathNotFoundException(metabasePath, 45054), flags); } if (flags < 0) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationIsEnabledUnknownErrorException(metabasePath, 45054), flags); } return((formsAuthPropertyFlags & OwaIsapiFilter.FormsAuthPropertyFlags.FbaEnabled) != OwaIsapiFilter.FormsAuthPropertyFlags.None); }
static Probe() { METADATA_MASTER_ROOT_HANDLE = new IntPtr(0); MSAdminBase adminBase = new MSAdminBase(); baseInterface = (IMSAdminBase)adminBase; }
private static void SetFlags(string server, string path, OwaIsapiFilter.FormsAuthPropertyFlags flags) { IMSAdminBase imsadminBase = IMSAdminBaseHelper.Create(server); string text = "/LM" + path; SafeMetadataHandle safeMetadataHandle; int num = IMSAdminBaseHelper.OpenKey(imsadminBase, SafeMetadataHandle.MetadataMasterRootHandle, text, MBKeyAccess.Write, 15000, out safeMetadataHandle); using (safeMetadataHandle) { if (num == -2147024748) { throw new FormsAuthenticationErrorPathBusyException(text); } if (num == -2147024893) { throw new FormsAuthenticationMarkPathErrorPathNotFoundException(text); } if (num < 0) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationMarkPathErrorUnknownOpenError(text), num); } MetadataRecord metadataRecord = OwaIsapiFilter.CreateFormsRecord(MBAttributes.Inherit); using (metadataRecord) { Marshal.WriteInt32(metadataRecord.DataBuf.DangerousGetHandle(), (int)flags); num = imsadminBase.SetData(safeMetadataHandle, string.Empty, metadataRecord); } if (num == -2147024891) { throw new FormsAuthenticationMarkPathAccessDeniedException(text, 45054); } if (num == -2147024888) { throw new OutOfMemoryException(); } if (num == -2147024893) { throw new FormsAuthenticationMarkPathErrorPathNotFoundException(text); } if (num == -2146646008) { throw new FormsAuthenticationMarkPathCannotMarkSecureAttributeException(text, 45054); } if (num < 0) { throw new FormsAuthenticationMarkPathUnknownSetError(text, 45054, num); } } num = IisUtility.CommitMetabaseChanges(server); if (num < 0) { throw new OwaIsapiFilterException(Strings.CommitMetabaseChangesException(server), num); } }
private static int GetFlags(IMSAdminBase iisAdmin, string metabasePath, out OwaIsapiFilter.FormsAuthPropertyFlags flags) { flags = OwaIsapiFilter.FormsAuthPropertyFlags.None; int result = 0; MetadataRecord metadataRecord = OwaIsapiFilter.CreateFormsRecord(MBAttributes.None); using (metadataRecord) { int num; result = iisAdmin.GetData(SafeMetadataHandle.MetadataMasterRootHandle, metabasePath, metadataRecord, out num); flags = (OwaIsapiFilter.FormsAuthPropertyFlags)Marshal.ReadInt32(metadataRecord.DataBuf.DangerousGetHandle()); } return(result); }
internal static int OpenKey(IMSAdminBase adminBase, SafeMetadataHandle handle, string Path, MBKeyAccess AccessRequested, int TimeOut, out SafeMetadataHandle newHandle) { IntPtr handle2; int num = adminBase.OpenKey(handle, Path, AccessRequested, TimeOut, out handle2); if (num == 0) { newHandle = new SafeMetadataHandle(handle2, adminBase); } else { newHandle = null; } return(num); }
private static bool CheckPermission(IMSAdminBase iisAdmin) { MetadataRecord metadataRecord = new MetadataRecord(0); bool result; using (metadataRecord) { metadataRecord.Identifier = MBIdentifier.KeyType; metadataRecord.Attributes = MBAttributes.None; metadataRecord.UserType = MBUserType.Server; metadataRecord.DataType = MBDataType.String; SafeMetadataHandle key = new SafeMetadataHandle(IntPtr.Zero, null); int num; int data = iisAdmin.GetData(key, IMSAdminBaseHelper.W3SVC, metadataRecord, out num); result = (data != -2147024891); } return(result); }
internal static int GetDataPaths(IMSAdminBase iisAdmin, string searchPath, MBIdentifier propertyID, MBDataType dataType, ref List <string> paths) { int num = 4096; int num2 = 0; char[] array; int dataPaths; do { array = new char[num]; dataPaths = iisAdmin.GetDataPaths(SafeMetadataHandle.MetadataMasterRootHandle, searchPath, propertyID, dataType, num, array, out num); }while (dataPaths == -2147024774 && num2 < 5); if (dataPaths == -2147024774 && num2 == 5) { throw new IMSAdminHelperGetDataPathsCouldNotAllocateException(); } if (dataPaths == -2147024888) { throw new OutOfMemoryException(); } if (dataPaths < 0) { return(dataPaths); } int num3 = 0; while (array[num3] != '\0') { int num4 = num3; while (array[num4] != '\0') { num4++; } int num5 = num4 - num3; while (num5 > 1 && array[num3 + num5 - 1] == '/') { num5--; } paths.Add(new string(array, num3, num5)); num3 = num4 + 1; } return(0); }
public MetabaseReader() { uint num; this.currentBufferSize = 0x400; lock (syncRoot) { if (adminBase == null) { adminBase = (IMSAdminBase) new MSAdminBase(); } } uint num2 = adminBase.OpenKey(0, "/LM", 1, 0x7530, out num); this.mdHandle = (int) num; if (num2 != 0) { throw FxTrace.Exception.AsError(new COMException(System.ServiceModel.Activation.SR.Hosting_MetabaseAccessError, (int) num2)); } this.bufferHandle = SafeHGlobalHandleCritical.AllocHGlobal(this.currentBufferSize); }
private static int GetMarkedPathCount(string server, string webSitePath) { int num = 0; IMSAdminBase iisAdmin = IMSAdminBaseHelper.Create(server); webSitePath = "/LM" + webSitePath; List <string> list = new List <string>(); int num2 = IMSAdminBaseHelper.GetDataPaths(iisAdmin, webSitePath, MBIdentifier.FormsAuthenticationEnabledProperty, MBDataType.Dword, ref list); if (num2 == -2147024893) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationDeleteMarksIfUnusedPathNotFoundException(webSitePath), num2); } if (num2 < 0) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationDeleteMarksIfUnusedUnknownErrorException(webSitePath, 45054), num2); } int[] array = new int[list.Count]; for (int i = 0; i < list.Count; i++) { OwaIsapiFilter.FormsAuthPropertyFlags formsAuthPropertyFlags; num2 = OwaIsapiFilter.GetFlags(iisAdmin, list[i], out formsAuthPropertyFlags); if (num2 == -2147024891) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationDeleteMarksIfUnusedCheckMarkAccessDeniedException(list[i]), num2); } if (num2 != -2147024893 && num2 != -2146646015) { if (num2 < 0) { throw new OwaIsapiFilterException(Strings.FormsAuthenticationDeleteMarksIfUnusedUnknownCheckErrorException(list[i]), num2); } num++; } } if (num == 1 && string.Compare(list[array[0]], webSitePath, true, CultureInfo.InvariantCulture) == 0) { num = 0; } return(num); }
public MetabaseReader() { uint num; this.currentBufferSize = 0x400; lock (syncRoot) { if (adminBase == null) { adminBase = (IMSAdminBase) new MSAdminBase(); } } uint num2 = adminBase.OpenKey(0, "/LM", 1, 0x7530, out num); this.mdHandle = (int)num; if (num2 != 0) { throw FxTrace.Exception.AsError(new COMException(System.ServiceModel.Activation.SR.Hosting_MetabaseAccessError, (int)num2)); } this.bufferHandle = SafeHGlobalHandleCritical.AllocHGlobal(this.currentBufferSize); }
internal static IMSAdminBase Create(string server) { if (server == null) { MSAdminBase msadminBase = new MSAdminBase(); return((IMSAdminBase)msadminBase); } Type typeFromCLSID = Type.GetTypeFromCLSID(IMSAdminBaseHelper.MSAdminBaseGuid, server, false); IMSAdminBase imsadminBase = (IMSAdminBase)Activator.CreateInstance(typeFromCLSID); if (IMSAdminBaseHelper.CheckPermission(imsadminBase)) { return(imsadminBase); } object obj = null; if (COMHelper.Create(IMSAdminBaseHelper.MSAdminBaseGuid, IMSAdminBaseHelper.IIMSAdminBaseGuid, server, ref obj) == 0U) { return((IMSAdminBase)obj); } return(imsadminBase); }
public MetabaseReader() { lock (syncRoot) { if (adminBase == null) { adminBase = (IMSAdminBase)new MSAdminBase(); } } uint handle; uint hResult = adminBase.OpenKey(MSAdminBase.METADATA_MASTER_ROOT_HANDLE, LMPath, MSAdminBase.METADATA_PERMISSION_READ, MSAdminBase.DEFAULT_METABASE_TIMEOUT, out handle); mdHandle = (int)handle; if (hResult != 0) { throw FxTrace.Exception.AsError(new COMException(SR.Hosting_MetabaseAccessError, (int)hResult)); } bufferHandle = SafeHGlobalHandleCritical.AllocHGlobal(currentBufferSize); }
public MetabaseReader() { lock (syncRoot) { if (adminBase == null) { adminBase = (IMSAdminBase) new MSAdminBase(); } } uint handle; uint hResult = adminBase.OpenKey(MSAdminBase.METADATA_MASTER_ROOT_HANDLE, LMPath, MSAdminBase.METADATA_PERMISSION_READ, MSAdminBase.DEFAULT_METABASE_TIMEOUT, out handle); mdHandle = (int)handle; if (hResult != 0) { throw FxTrace.Exception.AsError(new COMException(SR.Hosting_MetabaseAccessError, (int)hResult)); } bufferHandle = SafeHGlobalHandleCritical.AllocHGlobal(currentBufferSize); }
internal SafeMetadataHandle(IntPtr handle, IMSAdminBase adminBase) : base(true) { base.SetHandle(handle); this.adminBase = adminBase; }
internal unsafe static string GetPickupDirectory() { int hr; UInt32 reqLength = 0; Int32 serverState; string pickupDirectory = string.Empty; IMSAdminBase adminBase = null; IntPtr ptrKey = IntPtr.Zero; StringBuilder keySuffix = new StringBuilder(MetadataMaxNameLen); uint bufferLen = MaxPathSize * 4; byte[] buffer = new byte[bufferLen]; try { adminBase = new MSAdminBase() as IMSAdminBase; hr = adminBase.OpenKey(IntPtr.Zero, "LM/SmtpSvc", MBKeyAccess.Read, InfiniteTimeout, ref ptrKey); if (hr < 0) { Console.WriteLine("Failed to open IIS Metabase LM/SmtpSvc"); goto Exit; } MetadataRecord rec = new MetadataRecord(); fixed(byte *bufferPtr = buffer) { for (int index = 0; ; index++) { Console.WriteLine($"Metabase record index: {index}"); hr = adminBase.EnumKeys(ptrKey, "", keySuffix, index); if (hr == unchecked ((int)MBErrors.NoMoreItems)) { Console.WriteLine("No more metabase entries to parse"); break; } if (hr < 0) { Console.WriteLine($"Error attempting to query entry {index}: hr={hr}"); goto Exit; } rec.Identifier = (UInt32)PropertyName.ServerState; rec.Attributes = 0; rec.UserType = (UInt32)MBUserType.Server; rec.DataType = (UInt32)MBDataType.Dword; rec.DataTag = 0; rec.DataBuf = (IntPtr)bufferPtr; rec.DataLen = bufferLen; hr = adminBase.GetData(ptrKey, keySuffix.ToString(), ref rec, ref reqLength); if (hr < 0) { if (hr == unchecked ((int)MBErrors.DataNotFound) || hr == unchecked ((int)MBErrors.AccessDenied)) { if (hr == unchecked ((int)MBErrors.DataNotFound)) { Console.WriteLine("Unable to retrieve server data: MBErrors.DataNotFound"); } if (hr == unchecked ((int)MBErrors.AccessDenied)) { Console.WriteLine("Unable to retrieve server data: MBErrors.AccessDenied"); } continue; } else { Console.WriteLine($"Unable to retrieve server data (check MBErrors enum for more info): hr={hr}"); goto Exit; } } serverState = Marshal.ReadInt32((IntPtr)bufferPtr); if (serverState == (Int32)ServerState.Started) { rec.Identifier = (UInt32)PropertyName.PickupDirectory; rec.Attributes = 0; rec.UserType = (UInt32)MBUserType.Server; rec.DataType = (UInt32)MBDataType.String; rec.DataTag = 0; rec.DataBuf = (IntPtr)bufferPtr; rec.DataLen = bufferLen; hr = adminBase.GetData(ptrKey, keySuffix.ToString(), ref rec, ref reqLength); if (hr < 0) { Console.WriteLine($"Error when retrieving data for server at index {index} from metabase: hr={hr}"); goto Exit; } pickupDirectory = Marshal.PtrToStringUni((IntPtr)bufferPtr); break; } else { Console.WriteLine($"SMTP server at index {index} is not started"); } } if (hr == unchecked ((int)MBErrors.NoMoreItems)) { for (int index = 0; ; index++) { hr = adminBase.EnumKeys(ptrKey, "", keySuffix, index); if (hr == unchecked ((int)MBErrors.NoMoreItems)) { break; } if (hr < 0) { Console.WriteLine($"Unexpected error from EnumKeys call (check MBErrors enum for more info): hr={hr}"); goto Exit; } rec.Identifier = (UInt32)PropertyName.PickupDirectory; rec.Attributes = 0; rec.UserType = (UInt32)MBUserType.Server; rec.DataType = (UInt32)MBDataType.String; rec.DataTag = 0; rec.DataBuf = (IntPtr)bufferPtr; rec.DataLen = bufferLen; hr = adminBase.GetData(ptrKey, keySuffix.ToString(), ref rec, ref reqLength); if (hr < 0) { if (hr == unchecked ((int)MBErrors.DataNotFound) || hr == unchecked ((int)MBErrors.AccessDenied)) { if (hr == unchecked ((int)MBErrors.DataNotFound)) { Console.WriteLine("MBErrors.DataNotFound"); } if (hr == unchecked ((int)MBErrors.AccessDenied)) { Console.WriteLine("MBErrors.AccessDenied"); } continue; } else { Console.WriteLine($"Unexpected error from GetData call (check MBErrors enum for more info): hr={hr}"); goto Exit; } } pickupDirectory = Marshal.PtrToStringUni((IntPtr)bufferPtr); Console.WriteLine($"Testing Pickup directory: {pickupDirectory}"); if (Directory.Exists(pickupDirectory)) { break; } else { pickupDirectory = string.Empty; } } } } Exit: ; } catch (Exception exception) { Console.WriteLine($"Exception: {exception.Message}"); } finally { if (adminBase != null) { if (ptrKey != IntPtr.Zero) { adminBase.CloseKey(ptrKey); } } } if (String.IsNullOrEmpty(pickupDirectory)) { Console.WriteLine("Could not determine Pickup Directory"); } else { Console.WriteLine($"Pickup directory: {pickupDirectory}"); } return(pickupDirectory); }
internal unsafe static string GetPickupDirectory() { int hr; UInt32 reqLength = 0; Int32 serverState; string pickupDirectory = string.Empty; IMSAdminBase adminBase = null; IntPtr ptrKey = IntPtr.Zero; StringBuilder keySuffix = new StringBuilder(MetadataMaxNameLen); uint bufferLen = MaxPathSize * 4; byte[] buffer = new byte[bufferLen]; try { adminBase = new MSAdminBase() as IMSAdminBase; hr = adminBase.OpenKey(IntPtr.Zero, "LM/SmtpSvc", MBKeyAccess.Read, InfiniteTimeout, ref ptrKey); if (hr < 0) { goto Exit; } MetadataRecord rec = new MetadataRecord(); fixed(byte *bufferPtr = buffer) { for (int index = 0; ; index++) { hr = adminBase.EnumKeys(ptrKey, "", keySuffix, index); if (hr == unchecked ((int)MBErrors.NoMoreItems)) { break; } if (hr < 0) { goto Exit; } rec.Identifier = (UInt32)PropertyName.ServerState; rec.Attributes = 0; rec.UserType = (UInt32)MBUserType.Server; rec.DataType = (UInt32)MBDataType.Dword; rec.DataTag = 0; rec.DataBuf = (IntPtr)bufferPtr; rec.DataLen = bufferLen; hr = adminBase.GetData(ptrKey, keySuffix.ToString(), ref rec, ref reqLength); if (hr < 0) { if (hr == unchecked ((int)MBErrors.DataNotFound) || hr == unchecked ((int)MBErrors.AccessDenied)) { continue; } else { goto Exit; } } serverState = Marshal.ReadInt32((IntPtr)bufferPtr); if (serverState == (Int32)ServerState.Started) { rec.Identifier = (UInt32)PropertyName.PickupDirectory; rec.Attributes = 0; rec.UserType = (UInt32)MBUserType.Server; rec.DataType = (UInt32)MBDataType.String; rec.DataTag = 0; rec.DataBuf = (IntPtr)bufferPtr; rec.DataLen = bufferLen; hr = adminBase.GetData(ptrKey, keySuffix.ToString(), ref rec, ref reqLength); if (hr < 0) { goto Exit; } pickupDirectory = Marshal.PtrToStringUni((IntPtr)bufferPtr); break; } } if (hr == unchecked ((int)MBErrors.NoMoreItems)) { for (int index = 0; ; index++) { hr = adminBase.EnumKeys(ptrKey, "", keySuffix, index); if (hr == unchecked ((int)MBErrors.NoMoreItems)) { break; } if (hr < 0) { goto Exit; } rec.Identifier = (UInt32)PropertyName.PickupDirectory; rec.Attributes = 0; rec.UserType = (UInt32)MBUserType.Server; rec.DataType = (UInt32)MBDataType.String; rec.DataTag = 0; rec.DataBuf = (IntPtr)bufferPtr; rec.DataLen = bufferLen; hr = adminBase.GetData(ptrKey, keySuffix.ToString(), ref rec, ref reqLength); if (hr < 0) { if (hr == unchecked ((int)MBErrors.DataNotFound) || hr == unchecked ((int)MBErrors.AccessDenied)) { continue; } else { goto Exit; } } pickupDirectory = Marshal.PtrToStringUni((IntPtr)bufferPtr); if (Directory.Exists(pickupDirectory)) { break; } else { pickupDirectory = string.Empty; } } } } Exit: ; } catch (Exception exception) { if (exception is SecurityException || exception is AuthenticationException || exception is SmtpException) { throw; } throw new SmtpException(SR.GetString(SR.SmtpGetIisPickupDirectoryFailed)); } finally { if (adminBase != null) { if (ptrKey != IntPtr.Zero) { adminBase.CloseKey(ptrKey); } } } if (pickupDirectory == string.Empty) { throw new SmtpException(SR.GetString(SR.SmtpGetIisPickupDirectoryFailed)); } return(pickupDirectory); }
internal static int GetDataPaths(string searchPath, MBIdentifier propertyID, MBDataType dataType, ref List <string> paths) { IMSAdminBase iisAdmin = IMSAdminBaseHelper.Create(); return(IMSAdminBaseHelper.GetDataPaths(iisAdmin, searchPath, propertyID, dataType, ref paths)); }
internal static unsafe string GetPickupDirectory() { uint requiredDataLen = 0; string path = string.Empty; IMSAdminBase base2 = null; IntPtr zero = IntPtr.Zero; StringBuilder builder = new StringBuilder(0x100); uint num4 = 0x410; byte[] buffer = new byte[num4]; try { base2 = new MSAdminBase() as IMSAdminBase; if (base2.OpenKey(IntPtr.Zero, "LM/SmtpSvc", MBKeyAccess.Read, -1, ref zero) >= 0) { MetadataRecord data = new MetadataRecord(); try { fixed(byte *numRef = buffer) { int num; int enumKeyIndex = 0; Label_0080: num = base2.EnumKeys(zero, "", builder, enumKeyIndex); if (num == -2147024637) { goto Label_01AE; } if (num < 0) { goto Label_02D0; } data.Identifier = 0x3f8; data.Attributes = 0; data.UserType = 1; data.DataType = 1; data.DataTag = 0; data.DataBuf = (IntPtr)numRef; data.DataLen = num4; num = base2.GetData(zero, builder.ToString(), ref data, ref requiredDataLen); if (num < 0) { if ((num == -2146646015) || (num == -2147024891)) { goto Label_01A3; } goto Label_02D0; } if (Marshal.ReadInt32((IntPtr)numRef) == 2) { data.Identifier = 0x9010; data.Attributes = 0; data.UserType = 1; data.DataType = 2; data.DataTag = 0; data.DataBuf = (IntPtr)numRef; data.DataLen = num4; num = base2.GetData(zero, builder.ToString(), ref data, ref requiredDataLen); if (num < 0) { goto Label_02D0; } path = Marshal.PtrToStringUni((IntPtr)numRef); goto Label_01AE; } Label_01A3: enumKeyIndex++; goto Label_0080; Label_01AE: if (num != -2147024637) { goto Label_02D0; } int num6 = 0; Label_01BC: num = base2.EnumKeys(zero, "", builder, num6); if ((num == -2147024637) || (num < 0)) { goto Label_02D0; } data.Identifier = 0x9010; data.Attributes = 0; data.UserType = 1; data.DataType = 2; data.DataTag = 0; data.DataBuf = (IntPtr)numRef; data.DataLen = num4; num = base2.GetData(zero, builder.ToString(), ref data, ref requiredDataLen); if (num < 0) { if ((num == -2146646015) || (num == -2147024891)) { goto Label_026E; } goto Label_02D0; } path = Marshal.PtrToStringUni((IntPtr)numRef); if (Directory.Exists(path)) { goto Label_02D0; } path = string.Empty; Label_026E: num6++; goto Label_01BC; } } finally { numRef = null; } } } catch (Exception exception) { if (((exception is SecurityException) || (exception is AuthenticationException)) || (exception is SmtpException)) { throw; } throw new SmtpException(SR.GetString("SmtpGetIisPickupDirectoryFailed")); } finally { if ((base2 != null) && (zero != IntPtr.Zero)) { base2.CloseKey(zero); } } Label_02D0: if (path == string.Empty) { throw new SmtpException(SR.GetString("SmtpGetIisPickupDirectoryFailed")); } return(path); }