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 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 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; }
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); }