2 OpenChange MAPI torture suite implementation.
4 Send attachments to an Exchange server
6 Copyright (C) Julien Kerihuel 2007.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <libmapi/libmapi.h>
23 #include <gen_ndr/ndr_exchange.h>
25 #include <credentials.h>
26 #include <torture/mapi_torture.h>
28 #include <torture/torture_proto.h>
29 #include <samba/popt.h>
34 bool torture_rpc_mapi_sendmail_html(struct torture_context *torture)
36 enum MAPISTATUS retval;
39 const char *subject = lp_parm_string(torture->lp_ctx, NULL, "mapi", "subject");
40 const char *body = lp_parm_string(torture->lp_ctx, NULL, "mapi", "body");
41 const char *filename = lp_parm_string(torture->lp_ctx, NULL, "mapi", "html");
42 const char **usernames;
43 const char **usernames_to;
44 const char **usernames_cc;
45 const char **usernames_bcc;
46 struct mapi_session *session;
47 mapi_object_t obj_store;
48 mapi_object_t obj_outbox;
49 mapi_object_t obj_message;
51 struct SRowSet *SRowSet = NULL;
52 struct SPropTagArray *flaglist = NULL;
54 struct SPropTagArray *SPropTagArray;
55 struct SBinary_short html;
56 struct SPropValue SPropValue;
57 struct SPropValue props[3];
61 mem_ctx = talloc_init("torture_rpc_mapi_sendmail");
63 /* get the attachment filename */
65 DEBUG(0, ("No filename specified with mapi:html\n"));
70 if ((session = torture_init_mapi(mem_ctx, torture->lp_ctx)) == NULL) return false;
73 mapi_object_init(&obj_store);
74 mapi_object_init(&obj_outbox);
75 mapi_object_init(&obj_message);
78 if (subject == 0) subject = "";
79 if (body == 0) body = "";
81 /* session::OpenMsgStore() */
82 retval = OpenMsgStore(session, &obj_store);
83 mapi_errstr("OpenMsgStore", GetLastError());
84 if (retval != MAPI_E_SUCCESS) return false;
86 /* id_outbox = store->GeOutboxFolder() */
87 retval = GetOutboxFolder(&obj_store, &id_outbox);
88 mapi_errstr("GetOutboxFodler", GetLastError());
89 if (retval != MAPI_E_SUCCESS) return false;
91 /* outbox = store->OpenFolder(id_outbox) */
92 retval = OpenFolder(&obj_store, id_outbox, &obj_outbox);
93 if (retval != MAPI_E_SUCCESS) return false;
95 /* message = outbox->CreateMessage() */
96 retval = CreateMessage(&obj_outbox, &obj_message);
97 mapi_errstr("CreateMessage", GetLastError());
98 if (retval != MAPI_E_SUCCESS) return false;
100 SPropTagArray = set_SPropTagArray(mem_ctx, 0x6,
103 PR_7BIT_DISPLAY_NAME_UNICODE,
104 PR_DISPLAY_NAME_UNICODE,
105 PR_SMTP_ADDRESS_UNICODE,
106 PR_GIVEN_NAME_UNICODE);
108 usernames_to = get_cmdline_recipients(mem_ctx, "to");
109 usernames_cc = get_cmdline_recipients(mem_ctx, "cc");
110 usernames_bcc = get_cmdline_recipients(mem_ctx, "bcc");
111 usernames = collapse_recipients(mem_ctx, usernames_to, usernames_cc, usernames_bcc);
113 retval = ResolveNames(mapi_object_get_session(&obj_store), (const char **)usernames,
114 SPropTagArray, &SRowSet, &flaglist, MAPI_UNICODE);
115 mapi_errstr("ResolveNames", GetLastError());
116 if (retval != MAPI_E_SUCCESS) return false;
119 SRowSet = talloc_zero(mem_ctx, struct SRowSet);
122 set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames_to, flaglist, MAPI_TO);
123 set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames_cc, flaglist, MAPI_CC);
124 set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames_bcc, flaglist, MAPI_BCC);
127 SPropValue.ulPropTag = PR_SEND_INTERNET_ENCODING;
128 SPropValue.value.l = 0;
129 SRowSet_propcpy(mem_ctx, &SRowSet[0], SPropValue);
131 /* message->ModifyRecipients() */
132 retval = ModifyRecipients(&obj_message, SRowSet);
133 mapi_errstr("ModifyRecipients", GetLastError());
134 if (retval != MAPI_E_SUCCESS) return false;
136 retval = MAPIFreeBuffer(SRowSet);
137 mapi_errstr("MAPIFreeBuffer: SRowSet", GetLastError());
139 retval = MAPIFreeBuffer(flaglist);
140 mapi_errstr("MAPIFreeBuffer: flaglist", GetLastError());
142 /* message->SetProps()
149 if (stat(filename, &sb) != 0) return false;
150 if ((fd = open(filename, O_RDONLY)) == -1) {
151 DEBUG(0, ("Error while opening %s\n", filename));
154 html.lpb = talloc_size(mem_ctx, sb.st_size);
155 html.cb = read(fd, html.lpb, sb.st_size);
159 msgflag = MSGFLAG_UNSENT;
160 set_SPropValue_proptag(&props[0], PR_SUBJECT, (const void *)subject);
161 set_SPropValue_proptag(&props[1], PR_HTML, (const void *)&html);
162 set_SPropValue_proptag(&props[2], PR_MESSAGE_FLAGS, (const void *)&msgflag);
163 retval = SetProps(&obj_message, props, 3);
164 mapi_errstr("SetProps", GetLastError());
166 /* message->SubmitMessage() */
167 retval = SubmitMessage(&obj_message);
168 mapi_errstr("SubmitMessage", GetLastError());
169 if (retval != MAPI_E_SUCCESS) return false;
171 /* objects->Release()
173 mapi_object_release(&obj_message);
174 mapi_object_release(&obj_outbox);
175 mapi_object_release(&obj_store);
177 /* session::Uninitialize()
181 talloc_free(mem_ctx);