2 OpenChange MAPI torture suite implementation.
4 Common MAPI and torture related operations
6 Copyright (C) Julien Kerihuel 2007.
7 Copyright (C) Fabien Le Mentec 2007.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <libmapi/libmapi.h>
24 #include <torture/mapi_torture.h>
26 #include <torture/torture_proto.h>
27 #include <torture/mapi_torture.h>
30 const char *get_filename(const char *filename)
34 substr = rindex(filename, '/');
35 if (substr) return substr;
40 const char **get_cmdline_recipients(TALLOC_CTX *mem_ctx, const char *type)
42 const char **usernames;
43 const char *recipients;
51 recipients = lp_parm_string(global_mapi_ctx->lp_ctx, NULL, "mapi", type);
54 if (recipients == 0) {
55 printf("no recipients specified for %s\n", type);
59 if ((tmp = strtok((char *)recipients, ",")) == NULL) {
60 DEBUG(2, ("Invalid mapi:%s string format\n", type));
64 usernames = talloc_array(mem_ctx, const char *, 2);
65 usernames[0] = strdup(tmp);
67 for (j = 1; (tmp = strtok(NULL, ",")) != NULL; j++) {
68 usernames = talloc_realloc(mem_ctx, usernames, const char *, j+2);
69 usernames[j] = strdup(tmp);
76 const char **collapse_recipients(TALLOC_CTX *mem_ctx, const char **to, const char **cc, const char **bcc)
80 const char **usernames;
82 if (!to && !cc && !bcc) return NULL;
85 for (i = 0; to && to[i]; i++, count++);
86 for (i = 0; cc && cc[i]; i++, count++);
87 for (i = 0; bcc && bcc[i]; i++, count++);
89 usernames = talloc_array(mem_ctx, const char *, count + 1);
92 for (i = 0; to && to[i]; i++, count++) {
93 usernames[count] = talloc_strdup(mem_ctx, to[i]);
96 for (i = 0; cc && cc[i]; i++, count++) {
97 usernames[count] = talloc_strdup(mem_ctx, cc[i]);
100 for (i = 0; bcc && bcc[i]; i++, count++) {
101 usernames[count] = talloc_strdup(mem_ctx, bcc[i]);
104 usernames[count++] = 0;
109 static bool set_external_recipients(TALLOC_CTX *mem_ctx, struct SRowSet *SRowSet, const char *username, enum ulRecipClass RecipClass)
112 struct SPropValue SPropValue;
114 SRowSet->aRow = talloc_realloc(mem_ctx, SRowSet->aRow, struct SRow, SRowSet->cRows + 2);
115 last = SRowSet->cRows;
116 SRowSet->aRow[last].cValues = 0;
117 SRowSet->aRow[last].lpProps = talloc_zero(mem_ctx, struct SPropValue);
120 SPropValue.ulPropTag = PR_OBJECT_TYPE;
121 SPropValue.value.l = MAPI_MAILUSER;
122 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
124 /* PR_DISPLAY_TYPE */
125 SPropValue.ulPropTag = PR_DISPLAY_TYPE;
126 SPropValue.value.l = 0;
127 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
130 SPropValue.ulPropTag = PR_GIVEN_NAME;
131 SPropValue.value.lpszA = username;
132 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
134 /* PR_DISPLAY_NAME */
135 SPropValue.ulPropTag = PR_DISPLAY_NAME;
136 SPropValue.value.lpszA = username;
137 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
139 /* PR_7BIT_DISPLAY_NAME */
140 SPropValue.ulPropTag = PR_7BIT_DISPLAY_NAME;
141 SPropValue.value.lpszA = username;
142 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
144 /* PR_SMTP_ADDRESS */
145 SPropValue.ulPropTag = PR_SMTP_ADDRESS;
146 SPropValue.value.lpszA = username;
147 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
150 SPropValue.ulPropTag = PR_ADDRTYPE;
151 SPropValue.value.lpszA = "SMTP";
152 SRow_addprop(&(SRowSet->aRow[last]), SPropValue);
154 SetRecipientType(&(SRowSet->aRow[last]), RecipClass);
160 bool set_usernames_RecipientType(TALLOC_CTX *mem_ctx, uint32_t *index, struct SRowSet *rowset,
161 const char **usernames, struct SPropTagArray *flaglist,
162 enum ulRecipClass RecipClass)
165 uint32_t count = *index;
166 static uint32_t counter = 0;
168 if (count == 0) counter = 0;
169 if (!usernames) return false;
171 for (i = 0; usernames[i]; i++) {
172 if (flaglist->aulPropTag[count] == MAPI_UNRESOLVED) {
173 set_external_recipients(mem_ctx, rowset, usernames[i], RecipClass);
175 if (flaglist->aulPropTag[count] == MAPI_RESOLVED) {
176 SetRecipientType(&(rowset->aRow[counter]), RecipClass);
188 * Initialize MAPI and Load Profile
191 enum MAPISTATUS torture_load_profile(TALLOC_CTX *mem_ctx,
192 struct loadparm_context *lp_ctx,
193 struct mapi_session **s)
195 enum MAPISTATUS retval;
197 const char *profname;
198 struct mapi_session *session = NULL;
200 profdb = lp_parm_string(lp_ctx, NULL, "mapi", "profile_store");
202 profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB_PATH, getenv("HOME"));
204 DEBUG(0, ("Specify a valid MAPI profile store\n"));
205 return MAPI_E_NOT_FOUND;
209 retval = MAPIInitialize(profdb);
210 mapi_errstr("MAPIInitialize", GetLastError());
211 MAPI_RETVAL_IF(retval, retval, NULL);
213 profname = lp_parm_string(lp_ctx, NULL, "mapi", "profile");
215 retval = GetDefaultProfile(&profname);
216 MAPI_RETVAL_IF(retval, retval, NULL);
219 /* MapiLogonProvider returns MAPI_E_NO_SUPPORT: reset errno */
220 retval = MapiLogonProvider(&session, profname, NULL, PROVIDER_ID_NSPI);
223 retval = LoadProfile(session->profile);
224 MAPI_RETVAL_IF(retval, retval, NULL);
228 return MAPI_E_SUCCESS;
232 * Initialize MAPI and logs on the EMSMDB pipe with the default
236 struct mapi_session *torture_init_mapi(TALLOC_CTX *mem_ctx,
237 struct loadparm_context *lp_ctx)
239 enum MAPISTATUS retval;
240 struct mapi_session *session;
242 const char *profname;
243 const char *password;
245 profdb = lp_parm_string(lp_ctx, NULL, "mapi", "profile_store");
247 profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB_PATH, getenv("HOME"));
249 DEBUG(0, ("Specify a valid MAPI profile store\n"));
254 retval = MAPIInitialize(profdb);
255 mapi_errstr("MAPIInitialize", GetLastError());
256 if (retval != MAPI_E_SUCCESS) return NULL;
259 profname = lp_parm_string(lp_ctx, NULL, "mapi", "profile");
261 retval = GetDefaultProfile(&profname);
262 if (retval != MAPI_E_SUCCESS) {
263 DEBUG(0, ("Please specify a valid profile\n"));
268 password = lp_parm_string(lp_ctx, NULL, "mapi", "password");
269 retval = MapiLogonEx(&session, profname, password);
270 mapi_errstr("MapiLogonEx", GetLastError());
271 if (retval != MAPI_E_SUCCESS) return NULL;
276 enum MAPISTATUS torture_simplemail_fromme(struct loadparm_context *lp_ctx,
277 mapi_object_t *obj_parent,
278 const char *subject, const char *body,
282 enum MAPISTATUS retval;
283 mapi_object_t obj_message;
284 struct SPropTagArray *SPropTagArray = NULL;
285 struct SPropValue SPropValue;
286 struct SRowSet *SRowSet = NULL;
287 struct SPropTagArray *flaglist = NULL;
288 struct SPropValue props[3];
289 struct mapi_session *session = NULL;
290 const char **usernames;
293 mem_ctx = talloc_init("torture_simplemail");
295 session = mapi_object_get_session(obj_parent);
296 MAPI_RETVAL_IF(!session, MAPI_E_NOT_INITIALIZED, NULL);
298 mapi_object_init(&obj_message);
299 retval = CreateMessage(obj_parent, &obj_message);
300 MAPI_RETVAL_IF(retval, retval, mem_ctx);
302 SPropTagArray = set_SPropTagArray(mem_ctx, 0x6,
305 PR_7BIT_DISPLAY_NAME,
310 lp_set_cmdline(lp_ctx, "mapi:to", session->profile->username);
311 usernames = get_cmdline_recipients(mem_ctx, "to");
313 retval = ResolveNames(session, usernames, SPropTagArray, &SRowSet, &flaglist, 0);
314 MAPI_RETVAL_IF(retval, retval, mem_ctx);
317 SRowSet = talloc_zero(mem_ctx, struct SRowSet);
320 set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames, flaglist, MAPI_TO);
322 SPropValue.ulPropTag = PR_SEND_INTERNET_ENCODING;
323 SPropValue.value.l = 0;
324 SRowSet_propcpy(mem_ctx, SRowSet, SPropValue);
326 retval = ModifyRecipients(&obj_message, SRowSet);
327 MAPI_RETVAL_IF(retval, retval, mem_ctx);
329 retval = MAPIFreeBuffer(SRowSet);
330 MAPI_RETVAL_IF(retval, retval, mem_ctx);
331 retval = MAPIFreeBuffer(flaglist);
332 MAPI_RETVAL_IF(retval, retval, mem_ctx);
334 set_SPropValue_proptag(&props[0], PR_SUBJECT, (const void *)subject);
335 set_SPropValue_proptag(&props[1], PR_BODY, (const void *)body);
336 set_SPropValue_proptag(&props[2], PR_MESSAGE_FLAGS, (const void *)&flags);
337 retval = SetProps(&obj_message, props, 3);
338 MAPI_RETVAL_IF(retval, retval, mem_ctx);
340 retval = SaveChangesMessage(obj_parent, &obj_message, KeepOpenReadOnly);
341 MAPI_RETVAL_IF(retval, retval, mem_ctx);
343 mapi_object_release(&obj_message);
345 talloc_free(mem_ctx);
346 return MAPI_E_SUCCESS;
349 uint32_t get_permission_from_name(const char *role)
351 if (!role) return -1;
353 if (!strncasecmp(role, "RightsNone", strlen(role))) return 0x0;
354 if (!strncasecmp(role, "RightsReadItems", strlen(role))) return 0x1;
355 if (!strncasecmp(role, "RightsCreateItems", strlen(role))) return 0x2;
356 if (!strncasecmp(role, "RightsEditOwn", strlen(role))) return 0x8;
357 if (!strncasecmp(role, "RightsDeleteOwn", strlen(role))) return 0x10;
358 if (!strncasecmp(role, "RightsEditAll", strlen(role))) return 0x20;
359 if (!strncasecmp(role, "RightsDeleteAll", strlen(role))) return 0x40;
360 if (!strncasecmp(role, "RightsCreateSubfolders", strlen(role))) return 0x80;
361 if (!strncasecmp(role, "RightsFolderOwner", strlen(role))) return 0x100;
362 if (!strncasecmp(role, "RightsFolderContact", strlen(role))) return 0x200;
363 if (!strncasecmp(role, "RoleNone", strlen(role))) return 0x400;
364 if (!strncasecmp(role, "RoleReviewer", strlen(role))) return 0x401;
365 if (!strncasecmp(role, "RoleContributor", strlen(role))) return 0x402;
366 if (!strncasecmp(role, "RoleNoneditingAuthor", strlen(role))) return 0x413;
367 if (!strncasecmp(role, "RoleAuthor", strlen(role))) return 0x41B;
368 if (!strncasecmp(role, "RoleEditor", strlen(role))) return 0x47B;
369 if (!strncasecmp(role, "RolePublishAuthor", strlen(role))) return 0x49B;
370 if (!strncasecmp(role, "RolePublishEditor", strlen(role))) return 0x4FB;
371 if (!strncasecmp(role, "RightsAll", strlen(role))) return 0x5FB;
372 if (!strncasecmp(role, "RoleOwner", strlen(role))) return 0x7FB;