99ab3cd83efe18af6220c00b1e2c77f13cc5a2e8
[jelmer/openchange.git] / torture / mapi_permissions.c
1 /*
2    OpenChange MAPI torture suite implementation.
3
4    Test MAPI Permissions
5
6    Copyright (C) Julien Kerihuel 2007.
7
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.
12    
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.
17    
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/>.
20  */
21
22 #include <libmapi/libmapi.h>
23 #include <gen_ndr/ndr_exchange.h>
24 #include <param.h>
25 #include <credentials.h>
26 #include <torture/mapi_torture.h>
27 #include <torture.h>
28 #include <torture/torture_proto.h>
29 #include <samba/popt.h>
30
31 bool torture_rpc_mapi_permissions(struct torture_context *torture)
32 {
33         NTSTATUS                ntstatus;
34         enum MAPISTATUS         retval;
35         struct dcerpc_pipe      *p;
36         TALLOC_CTX              *mem_ctx;
37         struct mapi_session     *session;
38         mapi_object_t           obj_store;
39         mapi_object_t           obj_inbox;
40         mapi_object_t           obj_table;
41         mapi_id_t               id_inbox;
42         struct SRowSet          SRowSet;
43         struct SPropTagArray    *proptags;
44         uint32_t                i;
45         const char              *operation = lp_parm_string(torture->lp_ctx, NULL, "mapi", "operation");
46         const char              *role = lp_parm_string(torture->lp_ctx, NULL, "mapi", "role");
47         uint32_t                permission;
48         const char              *username = lp_parm_string(torture->lp_ctx, NULL, "mapi", "username");
49
50         /* init torture */
51         mem_ctx = talloc_init("torture_rpc_mapi_permissions");
52         ntstatus = torture_rpc_connection(torture, &p, &ndr_table_exchange_emsmdb);
53         if (!NT_STATUS_IS_OK(ntstatus)) {
54                 talloc_free(mem_ctx);
55                 return false;
56         }
57
58         /* init mapi */
59         if ((session = torture_init_mapi(mem_ctx, torture->lp_ctx)) == NULL) return false;
60
61         mapi_object_init(&obj_store);
62         retval = OpenMsgStore(session, &obj_store);
63         mapi_errstr("OpenMsgStore", GetLastError());
64         if (retval != MAPI_E_SUCCESS) return false;
65
66         /* Retrieve the default inbox folder id */
67         retval = GetDefaultFolder(&obj_store, &id_inbox, olFolderInbox);
68         mapi_errstr("GetDefaultFolder", GetLastError());
69         if (retval != MAPI_E_SUCCESS) return false;
70
71         mapi_object_init(&obj_inbox);
72         retval = OpenFolder(&obj_store, id_inbox, &obj_inbox);
73         mapi_errstr("OpenFolder", GetLastError());
74         if (retval != MAPI_E_SUCCESS) return false;
75
76         if (!strncasecmp(operation, "add", strlen(operation))) {
77                 permission = get_permission_from_name(role);
78                 retval = AddUserPermission(&obj_inbox, username, permission);
79                 mapi_errstr("AddUserPermission", GetLastError());
80                 if (retval != MAPI_E_SUCCESS) return false;
81         }
82         if (!strncasecmp(operation, "modify", strlen(operation))) {
83                 permission = get_permission_from_name(role);
84                 retval = ModifyUserPermission(&obj_inbox, username, permission);
85                 mapi_errstr("ModifyUserPermission", GetLastError());
86                 if (retval != MAPI_E_SUCCESS) return false;
87         }
88         if (!strncasecmp(operation, "remove", strlen(operation))) {
89                 retval = RemoveUserPermission(&obj_inbox, username);
90                 mapi_errstr("RemoveUserPermission", GetLastError());
91                 if (retval != MAPI_E_SUCCESS) return false;
92         }
93         if (!strncasecmp(operation, "list", strlen(operation))) {
94                 mapi_object_init(&obj_table);
95                 retval = GetTable(&obj_inbox, &obj_table);
96                 mapi_errstr("GetTable", GetLastError());
97                 if (retval != MAPI_E_SUCCESS) return false;
98
99                 proptags = set_SPropTagArray(mem_ctx, 4,
100                                              PR_MEMBER_ID,
101                                              PR_MEMBER_NAME,
102                                              PR_MEMBER_RIGHTS,
103                                              PR_ENTRYID);
104                 retval = SetColumns(&obj_table, proptags);
105                 mapi_errstr("SetColumns", GetLastError());
106                 MAPIFreeBuffer(proptags);
107                 if (retval != MAPI_E_SUCCESS) return false;
108                 
109                 retval = QueryRows(&obj_table, 0x32, TBL_ADVANCE, &SRowSet);
110                 mapi_errstr("QueryRows", GetLastError());
111                 if (retval != MAPI_E_SUCCESS) return false;
112                 
113                 for (i = 0; i < SRowSet.cRows; i++) {
114                         struct SPropValue *lpProp;
115                         
116                         lpProp = get_SPropValue_SRow(&(SRowSet.aRow[i]), PR_MEMBER_NAME);
117                         printf("    %-25s: %s\n", "Username", lpProp->value.lpszA ? lpProp->value.lpszA : "Default");
118                         lpProp = get_SPropValue_SRow(&(SRowSet.aRow[i]), PR_MEMBER_RIGHTS);
119                         ndr_print_debug((ndr_print_fn_t)ndr_print_ACLRIGHTS, "Rights", (void *)lpProp->value.l);
120                         printf("\n");
121                 }
122                 
123                 mapi_object_release(&obj_table);
124         }
125         mapi_object_release(&obj_inbox);
126         mapi_object_release(&obj_store);
127
128         MAPIUninitialize();
129         talloc_free(mem_ctx);
130         return true;
131 }