-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
445 lines (390 loc) · 11.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <MAPIX.h>
#include <MAPIUtil.h>
#include <string>
#include <AcctMgmt.h>
#include <AccountHelper.h>
#define pbGlobalProfileSectionGuid "\x13\xDB\xB0\xC8\xAA\x05\x10\x1A\x9B\xB0\x00\xAA\x00\x2F\xC4\x5A"
// This helper struct handles initializing and uninitializing COM for us
// Since it is in the global scope, it gets created before any of our main
// routine executes.
struct StartOle {
StartOle() { CoInitialize(nullptr); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
std::wstring GetProfileName(LPMAPISESSION lpSession);
void IterateAllProps(LPOLKACCOUNT lpAccount);
HRESULT EnumerateAccounts(LPMAPISESSION lpSession, LPCWSTR lpwszProfile, bool bIterateAllProps);
HRESULT DisplayAccountList(LPMAPISESSION lpSession, LPCWSTR lpwszProfile, ULONG ulFlags);
void PrintBinary(DWORD cb, const BYTE* lpb);
// Execute a function, log and return the HRESULT
// Does not modify or reference existing hRes
#define EC_H(fnx) \
[&]() -> HRESULT { \
auto __hRes = (fnx); \
if (FAILED(__hRes)) printf("0x%08X from %s\n @ %s + %d\n", __hRes, #fnx, __FILE__, __LINE__); \
return __hRes; \
}()
void LogPropString(LPOLKACCOUNT lpAccount, ULONG propTag, LPCSTR propName)
{
auto pProp = ACCT_VARIANT();
auto hRes = lpAccount->GetProp(propTag, &pProp);
if (SUCCEEDED(hRes) && pProp.Val.pwsz)
{
printf("%hs = \"%ws\"\n", propName, pProp.Val.pwsz);
(void)lpAccount->FreeMemory(reinterpret_cast<LPBYTE>(pProp.Val.pwsz));
}
}
#define LOGPROPSTRING(lpAccount, propTag) LogPropString((lpAccount), (propTag), #propTag)
void LogPropLong(LPOLKACCOUNT lpAccount, ULONG propTag, LPCSTR propName)
{
auto pProp = ACCT_VARIANT();
auto hRes = lpAccount->GetProp(propTag, &pProp);
if (SUCCEEDED(hRes) && pProp.Val.pwsz)
{
printf("%hs = 0x%08lX\n", propName, pProp.Val.dw);
}
}
#define LOGPROPLONG(lpAccount, propTag) LogPropLong((lpAccount), (propTag), #propTag)
void LogPropBinary(LPOLKACCOUNT lpAccount, ULONG propTag, LPCSTR propName)
{
auto pProp = ACCT_VARIANT();
auto hRes = lpAccount->GetProp(propTag, &pProp);
if (SUCCEEDED(hRes) && pProp.Val.pwsz)
{
printf("%hs = ", propName);
PrintBinary(pProp.Val.bin.cb, pProp.Val.bin.pb);
printf("\n");
(void)lpAccount->FreeMemory(pProp.Val.bin.pb);
}
}
#define LOGPROPBINARY(lpAccount, propTag) LogPropBinary((lpAccount), (propTag), #propTag)
void IterateAllProps(LPOLKACCOUNT lpAccount)
{
if (!lpAccount) return;
printf("Iterating all properties\n");
auto hRes = S_OK;
ULONG i = 0;
ACCT_VARIANT pProp = { 0 };
for (i = 0; i < 0x8000; i++)
{
memset(&pProp, 0, sizeof(ACCT_VARIANT));
hRes = lpAccount->GetProp(PROP_TAG(PT_LONG, i), &pProp);
if (SUCCEEDED(hRes))
{
printf("Prop = 0x%08lX, Type = PT_LONG, Value = 0x%08lX\n", PROP_TAG(PT_LONG, i), pProp.Val.dw);
}
hRes = lpAccount->GetProp(PROP_TAG(PT_UNICODE, i), &pProp);
if (SUCCEEDED(hRes))
{
printf("Prop = 0x%08lX, Type = PT_UNICODE, Value = %ws\n", PROP_TAG(PT_UNICODE, i), pProp.Val.pwsz);
(void)lpAccount->FreeMemory(reinterpret_cast<LPBYTE>(pProp.Val.pwsz));
}
hRes = lpAccount->GetProp(PROP_TAG(PT_BINARY, i), &pProp);
if (SUCCEEDED(hRes))
{
printf("Prop = 0x%08lX, Type = PT_BINARY, Value = ", PROP_TAG(PT_BINARY, i));
PrintBinary(pProp.Val.bin.cb, pProp.Val.bin.pb);
printf("\n");
(void)lpAccount->FreeMemory(pProp.Val.bin.pb);
}
}
printf("Done iterating all properties\n");
}
HRESULT EnumerateAccounts(LPMAPISESSION lpSession, LPCWSTR lpwszProfile, bool bIterateAllProps)
{
auto hRes = S_OK;
LPOLKACCOUNTMANAGER lpAcctMgr = nullptr;
hRes = EC_H(CoCreateInstance(CLSID_OlkAccountManager,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IOlkAccountManager,
reinterpret_cast<LPVOID*>(&lpAcctMgr)));
if (SUCCEEDED(hRes) && lpAcctMgr)
{
auto pMyAcctHelper = new CAccountHelper(lpwszProfile, lpSession);
if (pMyAcctHelper)
{
LPOLKACCOUNTHELPER lpAcctHelper = nullptr;
hRes = EC_H(pMyAcctHelper->QueryInterface(IID_IOlkAccountHelper, reinterpret_cast<LPVOID*>(&lpAcctHelper)));
if (SUCCEEDED(hRes) && lpAcctHelper)
{
hRes = EC_H(lpAcctMgr->Init(lpAcctHelper, ACCT_INIT_NOSYNCH_MAPI_ACCTS));
if (SUCCEEDED(hRes))
{
LPOLKENUM lpAcctEnum = nullptr;
hRes = EC_H(lpAcctMgr->EnumerateAccounts(&CLSID_OlkMail,
nullptr,
OLK_ACCOUNT_NO_FLAGS,
&lpAcctEnum));
if (SUCCEEDED(hRes) && lpAcctEnum)
{
DWORD cAccounts = 0;
hRes = EC_H(lpAcctEnum->GetCount(&cAccounts));
if (SUCCEEDED(hRes) && cAccounts)
{
hRes = EC_H(lpAcctEnum->Reset());
if (SUCCEEDED(hRes))
{
DWORD i = 0;
for (i = 0; i < cAccounts; i++)
{
if (i > 0) printf("\n");
printf("Account #%lu\n", i + 1);
LPUNKNOWN lpUnk = nullptr;
hRes = EC_H(lpAcctEnum->GetNext(&lpUnk));
if (SUCCEEDED(hRes) && lpUnk)
{
LPOLKACCOUNT lpAccount = nullptr;
hRes = EC_H(lpUnk->QueryInterface(IID_IOlkAccount, reinterpret_cast<LPVOID*>(&lpAccount)));
if (lpAccount)
{
LOGPROPSTRING(lpAccount, PROP_ACCT_NAME);
LOGPROPSTRING(lpAccount, PROP_ACCT_USER_DISPLAY_NAME);
LOGPROPSTRING(lpAccount, PROP_ACCT_USER_EMAIL_ADDR);
LOGPROPSTRING(lpAccount, PROP_ACCT_STAMP);
LOGPROPSTRING(lpAccount, PROP_ACCT_SEND_STAMP);
LOGPROPLONG(lpAccount, PROP_ACCT_IS_EXCH);
LOGPROPLONG(lpAccount, PROP_ACCT_ID);
LOGPROPBINARY(lpAccount, PROP_ACCT_DELIVERY_FOLDER);
LOGPROPBINARY(lpAccount, PROP_ACCT_DELIVERY_STORE);
LOGPROPBINARY(lpAccount, PROP_ACCT_SENTITEMS_EID);
LOGPROPBINARY(lpAccount, PR_NEXT_SEND_ACCT);
LOGPROPBINARY(lpAccount, PR_PRIMARY_SEND_ACCT);
LOGPROPBINARY(lpAccount, PROP_MAPI_IDENTITY_ENTRYID);
LOGPROPBINARY(lpAccount, PROP_MAPI_TRANSPORT_FLAGS);
if (bIterateAllProps) IterateAllProps(lpAccount);
lpAccount->Release();
}
}
if (lpUnk) lpUnk->Release();
lpUnk = nullptr;
}
}
}
}
if (lpAcctEnum) lpAcctEnum->Release();
}
}
if (lpAcctHelper) lpAcctHelper->Release();
pMyAcctHelper->Release();
}
}
if (lpAcctMgr) lpAcctMgr->Release();
return hRes;
}
HRESULT DisplayAccountList(LPMAPISESSION lpSession, LPCWSTR lpwszProfile, ULONG ulFlags)
{
auto hRes = S_OK;
LPOLKACCOUNTMANAGER lpAcctMgr = nullptr;
hRes = EC_H(CoCreateInstance(CLSID_OlkAccountManager,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IOlkAccountManager,
reinterpret_cast<LPVOID*>(&lpAcctMgr)));
if (SUCCEEDED(hRes) && lpAcctMgr)
{
auto pMyAcctHelper = new CAccountHelper(lpwszProfile, lpSession);
if (pMyAcctHelper)
{
LPOLKACCOUNTHELPER lpAcctHelper = nullptr;
hRes = EC_H(pMyAcctHelper->QueryInterface(IID_IOlkAccountHelper, reinterpret_cast<LPVOID*>(&lpAcctHelper)));
if (SUCCEEDED(hRes) && lpAcctHelper)
{
hRes = EC_H(lpAcctMgr->Init(lpAcctHelper, ACCT_INIT_NOSYNCH_MAPI_ACCTS));
if (SUCCEEDED(hRes))
{
hRes = EC_H(lpAcctMgr->DisplayAccountList(
nullptr, // hwnd
ulFlags, // dwFlags
nullptr, // wszTitle
NULL, // cCategories
nullptr, // rgclsidCategories
nullptr)); // pclsidType
}
}
if (lpAcctHelper) lpAcctHelper->Release();
pMyAcctHelper->Release();
}
}
if (lpAcctMgr) lpAcctMgr->Release();
return hRes;
}
std::wstring GetProfileName(LPMAPISESSION lpSession)
{
LPPROFSECT lpProfSect = nullptr;
std::wstring profileName;
if (!lpSession) return profileName;
auto hRes = EC_H(lpSession->OpenProfileSection(
LPMAPIUID(pbGlobalProfileSectionGuid),
nullptr,
0,
&lpProfSect));
if (SUCCEEDED(hRes) && lpProfSect)
{
LPSPropValue lpProfileName = nullptr;
hRes = EC_H(HrGetOneProp(lpProfSect, PR_PROFILE_NAME_W, &lpProfileName));
if (SUCCEEDED(hRes) && lpProfileName && lpProfileName->ulPropTag == PR_PROFILE_NAME_W)
{
profileName = std::wstring(lpProfileName->Value.lpszW);
}
MAPIFreeBuffer(lpProfileName);
}
if (lpProfSect)
lpProfSect->Release();
return profileName;
}
void PrintBinary(const DWORD cb, const BYTE* lpb)
{
if (!cb || !lpb) return;
LPSTR lpszHex = nullptr;
ULONG i = 0;
ULONG iBinPos = 0;
lpszHex = new CHAR[1 + 2 * cb];
if (lpszHex)
{
for (i = 0; i < cb; i++)
{
const auto bLow = static_cast<BYTE>(lpb[i] & 0xf);
const auto bHigh = static_cast<BYTE>(lpb[i] >> 4 & 0xf);
const auto szLow = static_cast<CHAR>(bLow <= 0x9 ? '0' + bLow : 'A' + bLow - 0xa);
const auto szHigh = static_cast<CHAR>(bHigh <= 0x9 ? '0' + bHigh : 'A' + bHigh - 0xa);
lpszHex[iBinPos] = szHigh;
lpszHex[iBinPos + 1] = szLow;
iBinPos += 2;
}
lpszHex[iBinPos] = '\0';
printf("%hs", lpszHex);
delete[] lpszHex;
}
}
void DisplayUsage()
{
printf("EnumAccounts - Exercise the account management API\n");
printf("\n");
printf("Usage:\n");
printf(" EnumAccounts [-E] [-W] [-F]\n");
printf(" EnumAccounts -?\n");
printf("\n");
printf(" -E Enumerate accounts\n");
printf(" -I Iterate through all prop numbers looking for data (implies -E)\n");
printf(" -W Display wizard (DisplayAccountList)\n");
printf(" -F Indicates specific flags to pass to the DisplayAccountList.\n");
printf(" If not specified, 0x500 = ACCTUI_NO_WARNING | ACCTUI_SHOW_ACCTWIZARD is used.\n");
printf(" Available values are (these may be OR'ed together):\n");
printf(" ACCTUI_NO_WARNING 0x0100\n");
printf(" ACCTUI_SHOW_DATA_TAB 0x0200\n");
printf(" ACCTUI_SHOW_ACCTWIZARD 0x0400\n");
printf(" -? Print this message\n");
printf("\n");
printf("Examples\n");
printf(" EnumAccounts\n");
printf("\n");
printf(" EnumAccounts -e\n");
printf("\n");
printf(" EnumAccounts -w -f 0x500\n");
printf("\n");
}
struct MYOPTIONS
{
BOOL bDoWizard;
BOOL bDoEnum;
BOOL bIterateAllProps;
ULONG ulWizardFlags;
};
BOOL ParseArgs(int argc, char * argv[], MYOPTIONS * pRunOpts)
{
if (!pRunOpts) return false;
// Clear our options list
ZeroMemory(pRunOpts, sizeof(MYOPTIONS));
BOOL bFlagsSet = false;
// Initialize non-null default values
pRunOpts->ulWizardFlags = ACCTUI_NO_WARNING | ACCTUI_SHOW_ACCTWIZARD;
for (auto i = 1; i < argc; i++)
{
switch (argv[i][0])
{
case '-':
case '/':
case '\\':
{
if (0 == argv[i][1])
{
// Bad argument - get out of here
return false;
}
switch (tolower(argv[i][1]))
{
case 'i':
pRunOpts->bIterateAllProps = true;
pRunOpts->bDoEnum = true;
break;
case 'e':
pRunOpts->bDoEnum = true;
break;
case 'w':
pRunOpts->bDoWizard = true;
break;
case 'f':
if (argc <= i + 1) return false;
if (bFlagsSet) return false;
{
LPSTR szEndPtr = nullptr;
pRunOpts->ulWizardFlags = strtoul(argv[++i], &szEndPtr, 16);
bFlagsSet = true;
}
break;
case '?':
case 'h':
default:
// display help
return FALSE;
}
}
break;
default:
break;
}
}
if (pRunOpts->bDoEnum && pRunOpts->bDoWizard) return false;
if (pRunOpts->bIterateAllProps && pRunOpts->bDoWizard) return false;
if (!pRunOpts->bDoEnum && !pRunOpts->bDoWizard) return false;
// Didn't fail - return true
return true;
}
void main(int argc, char * argv[])
{
MYOPTIONS ProgOpts{};
if (!ParseArgs(argc, argv, &ProgOpts))
{
DisplayUsage();
return;
}
auto hRes = EC_H(MAPIInitialize(nullptr));
if (SUCCEEDED(hRes))
{
LPMAPISESSION lpSession = nullptr;
hRes = EC_H(MAPILogonEx(0,
nullptr,
nullptr,
fMapiUnicode | MAPI_EXTENDED | MAPI_EXPLICIT_PROFILE |
MAPI_NEW_SESSION | MAPI_NO_MAIL | MAPI_LOGON_UI,
&lpSession));
if (SUCCEEDED(hRes) && lpSession)
{
auto profileName = GetProfileName(lpSession);
if (!profileName.empty())
{
if (ProgOpts.bDoWizard)
{
(void)DisplayAccountList(lpSession, profileName.c_str(), ProgOpts.ulWizardFlags);
}
else if (ProgOpts.bDoEnum)
{
(void)EnumerateAccounts(lpSession, profileName.c_str(), ProgOpts.bIterateAllProps);
}
}
}
if (lpSession) lpSession->Release();
MAPIUninitialize();
}
}