diff --git a/ChangeLog b/ChangeLog index 5de14ea5e..9d97a55a7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,8 @@ pertaining to the SamDB. (MAPIStoreSamDBUserAttribute): new function that returns the value of a specified key from a user record matching one simple criteria. + (MAPIStoreInternalEntrydId, MAPIStoreExternalEntryId): moved + functions from MAPIStoreMessage.m. * OpenChange/MAPIStoreMessage.m (MAPIStoreInternalEntryId): now takes a ldb_context * parameter pointing to the samdb, which diff --git a/OpenChange/MAPIStoreAppointmentWrapper.m b/OpenChange/MAPIStoreAppointmentWrapper.m index 493c5b527..c1f0e8b90 100644 --- a/OpenChange/MAPIStoreAppointmentWrapper.m +++ b/OpenChange/MAPIStoreAppointmentWrapper.m @@ -37,8 +37,8 @@ #import #import -#import "MAPIStoreMessage.h" #import "MAPIStoreRecurrenceUtils.h" +#import "MAPIStoreSamDBUtils.h" #import "MAPIStoreTypes.h" #import "NSData+MAPIStore.h" #import "NSDate+MAPIStore.h" diff --git a/OpenChange/MAPIStoreMailMessage.m b/OpenChange/MAPIStoreMailMessage.m index 6c148f5b1..8c165cc6e 100644 --- a/OpenChange/MAPIStoreMailMessage.m +++ b/OpenChange/MAPIStoreMailMessage.m @@ -48,6 +48,7 @@ #import "MAPIStoreMailAttachment.h" #import "MAPIStoreMailFolder.h" #import "MAPIStoreMapping.h" +#import "MAPIStoreSamDBUtils.h" #import "MAPIStoreTypes.h" #import "MAPIStoreMailMessage.h" diff --git a/OpenChange/MAPIStoreMailVolatileMessage.m b/OpenChange/MAPIStoreMailVolatileMessage.m index b23eefd6d..5952eb405 100644 --- a/OpenChange/MAPIStoreMailVolatileMessage.m +++ b/OpenChange/MAPIStoreMailVolatileMessage.m @@ -54,6 +54,7 @@ #import "MAPIStoreMailFolder.h" #import "MAPIStoreMIME.h" #import "MAPIStoreMapping.h" +#import "MAPIStoreSamDBUtils.h" #import "MAPIStoreTypes.h" #import "NSData+MAPIStore.h" #import "NSObject+MAPIStore.h" diff --git a/OpenChange/MAPIStoreMessage.h b/OpenChange/MAPIStoreMessage.h index 9270fb973..83dfa57a2 100644 --- a/OpenChange/MAPIStoreMessage.h +++ b/OpenChange/MAPIStoreMessage.h @@ -35,13 +35,8 @@ @class MAPIStoreAttachmentTable; @class MAPIStoreFolder; -struct ldb_context; - #import "MAPIStoreObject.h" -extern NSData *MAPIStoreInternalEntryId (struct ldb_context *, NSString *username); -extern NSData *MAPIStoreExternalEntryId (NSString *cn, NSString *email); - @interface MAPIStoreMessage : MAPIStoreObject { NSArray *attachmentKeys; diff --git a/OpenChange/MAPIStoreMessage.m b/OpenChange/MAPIStoreMessage.m index b5c9d3fe7..f2c78814e 100644 --- a/OpenChange/MAPIStoreMessage.m +++ b/OpenChange/MAPIStoreMessage.m @@ -36,6 +36,7 @@ #import "MAPIStoreContext.h" #import "MAPIStoreFolder.h" #import "MAPIStorePropertySelectors.h" +#import "MAPIStoreSamDBUtils.h" #import "MAPIStoreTypes.h" #import "NSData+MAPIStore.h" #import "NSObject+MAPIStore.h" @@ -54,116 +55,6 @@ static NSString *resourcesDir = nil; -NSData * -MAPIStoreInternalEntryId (struct ldb_context *samCtx, NSString *username) -{ - NSMutableData *entryId = nil; - TALLOC_CTX *memCtx; - struct ldb_result *res = NULL; - const char * const attrs[] = { "legacyExchangeDN", NULL }; - const char *legacyDn; - int ret; - static const uint8_t const providerUid[] = { 0xdc, 0xa7, 0x40, 0xc8, - 0xc0, 0x42, 0x10, 0x1a, - 0xb4, 0xb9, 0x08, 0x00, - 0x2b, 0x2f, 0xe1, 0x82 }; - - /* structure: - flags: 32 - provideruid: 32 * 4 - version: 32 - type: 32 - X500DN: variable */ - - memCtx = talloc_zero(NULL, TALLOC_CTX); - - /* Search mapistoreURI given partial URI */ - ret = ldb_search (samCtx, memCtx, &res, ldb_get_default_basedn(samCtx), - LDB_SCOPE_SUBTREE, attrs, - "(&(objectClass=user)(sAMAccountName=%s))", - [username UTF8String]); - if (ret == LDB_SUCCESS && res->count == 1) - { - legacyDn = ldb_msg_find_attr_as_string (res->msgs[0], attrs[0], NULL); - if (legacyDn) - { - entryId = [NSMutableData dataWithCapacity: 256]; - [entryId appendUInt32: 0]; // flags - [entryId appendBytes: providerUid length: 16]; // provideruid - [entryId appendUInt32: 1]; // version - [entryId appendUInt32: 0]; // type (local mail user) - [entryId appendBytes: legacyDn length: strlen (legacyDn) + 1]; // x500dn - } - } - - talloc_free(memCtx); - - return entryId; -} - -NSData * -MAPIStoreExternalEntryId (NSString *cn, NSString *email) -{ - NSMutableData *entryId; - static uint8_t providerUid[] = { 0x81, 0x2b, 0x1f, 0xa4, - 0xbe, 0xa3, 0x10, 0x19, - 0x9d, 0x6e, 0x00, 0xdd, - 0x01, 0x0f, 0x54, 0x02 }; - uint8_t flags21, flags22; - - /* structure: - flags: 32 - provideruid: 32 * 4 - version: 16 - { - PaD: 1 - MAE: 2 - Format: 4 - M: 1 - U: 1 - R: 2 - L: 1 - Pad: 4 - } - DisplayName: variable - AddressType: variable - EmailAddress: variable */ - - entryId = [NSMutableData dataWithCapacity: 256]; - [entryId appendUInt32: 0]; // flags - [entryId appendBytes: providerUid length: 16]; // provideruid - [entryId appendUInt16: 0]; // version - - flags21 = 0; /* PaD, MAE, R, Pad = 0 */ - flags21 |= 0x16; /* Format: text and HTML */ - flags21 |= 0x01; /* M: mime format */ - - flags22 = 0x90; /* U: unicode, L: no lookup */ - [entryId appendUInt8: flags21]; - [entryId appendUInt8: flags22]; - - /* DisplayName */ - if (!cn) - cn = @""; - [entryId - appendData: [cn dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; - [entryId appendUInt16: 0]; - - /* AddressType */ - [entryId - appendData: [@"SMTP" dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; - [entryId appendUInt16: 0]; - - /* EMailAddress */ - if (!email) - email = @""; - [entryId - appendData: [email dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; - [entryId appendUInt16: 0]; - - return entryId; -} - /* rtf conversion via unrtf */ static int unrtf_data_output (void *data, const char *str, size_t str_len) diff --git a/OpenChange/MAPIStoreSamDBUtils.h b/OpenChange/MAPIStoreSamDBUtils.h index d8220992f..c58a12686 100644 --- a/OpenChange/MAPIStoreSamDBUtils.h +++ b/OpenChange/MAPIStoreSamDBUtils.h @@ -24,11 +24,14 @@ #define MAPISTORESAMDBUTILS_H @class NSString; + struct ldb_context; NSString *MAPIStoreSamDBUserAttribute (struct ldb_context *samCtx, NSString *userKey, NSString *value, NSString *attributeName); +NSData *MAPIStoreInternalEntryId (struct ldb_context *, NSString *username); +NSData *MAPIStoreExternalEntryId (NSString *cn, NSString *email); #endif /* MAPISTORESAMDBUTILS_H */ diff --git a/OpenChange/MAPIStoreSamDBUtils.m b/OpenChange/MAPIStoreSamDBUtils.m index ed8d6a0b5..4d7caff63 100644 --- a/OpenChange/MAPIStoreSamDBUtils.m +++ b/OpenChange/MAPIStoreSamDBUtils.m @@ -20,10 +20,13 @@ * Boston, MA 02111-1307, USA. */ +#import #import #include #include +#import "NSData+MAPIStore.h" + #import "MAPIStoreSamDBUtils.h" NSString * @@ -60,3 +63,103 @@ MAPIStoreSamDBUserAttribute (struct ldb_context *samCtx, return resultValue; } + +NSData * +MAPIStoreInternalEntryId (struct ldb_context *samCtx, NSString *username) +{ + static const uint8_t const providerUid[] = { 0xdc, 0xa7, 0x40, 0xc8, + 0xc0, 0x42, 0x10, 0x1a, + 0xb4, 0xb9, 0x08, 0x00, + 0x2b, 0x2f, 0xe1, 0x82 }; + NSMutableData *entryId; + NSData *legacyDNData; + NSString *legacyDN; + + /* structure: + flags: 32 + provideruid: 32 * 4 + version: 32 + type: 32 + X500DN: variable */ + + legacyDN = MAPIStoreSamDBUserAttribute (samCtx, @"sAMAccountName", username, + @"legacyExchangeDN"); + if (legacyDN) + { + entryId = [NSMutableData dataWithCapacity: 256]; + [entryId appendUInt32: 0]; // flags + [entryId appendBytes: providerUid length: 16]; // provideruid + [entryId appendUInt32: 1]; // version + [entryId appendUInt32: 0]; // type (local mail user) + legacyDNData = [legacyDN dataUsingEncoding: NSASCIIStringEncoding]; + [entryId appendData: legacyDNData]; // x500dn + [entryId appendUInt8: 0]; // end of string + } + else + entryId = nil; + + return entryId; +} + +NSData * +MAPIStoreExternalEntryId (NSString *cn, NSString *email) +{ + NSMutableData *entryId; + static uint8_t providerUid[] = { 0x81, 0x2b, 0x1f, 0xa4, + 0xbe, 0xa3, 0x10, 0x19, + 0x9d, 0x6e, 0x00, 0xdd, + 0x01, 0x0f, 0x54, 0x02 }; + uint8_t flags21, flags22; + + /* structure: + flags: 32 + provideruid: 32 * 4 + version: 16 + { + PaD: 1 + MAE: 2 + Format: 4 + M: 1 + U: 1 + R: 2 + L: 1 + Pad: 4 + } + DisplayName: variable + AddressType: variable + EmailAddress: variable */ + + entryId = [NSMutableData dataWithCapacity: 256]; + [entryId appendUInt32: 0]; // flags + [entryId appendBytes: providerUid length: 16]; // provideruid + [entryId appendUInt16: 0]; // version + + flags21 = 0; /* PaD, MAE, R, Pad = 0 */ + flags21 |= 0x16; /* Format: text and HTML */ + flags21 |= 0x01; /* M: mime format */ + + flags22 = 0x90; /* U: unicode, L: no lookup */ + [entryId appendUInt8: flags21]; + [entryId appendUInt8: flags22]; + + /* DisplayName */ + if (!cn) + cn = @""; + [entryId + appendData: [cn dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; + [entryId appendUInt16: 0]; + + /* AddressType */ + [entryId + appendData: [@"SMTP" dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; + [entryId appendUInt16: 0]; + + /* EMailAddress */ + if (!email) + email = @""; + [entryId + appendData: [email dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; + [entryId appendUInt16: 0]; + + return entryId; +}