~ubuntu-branches/debian/sid/openchange/sid

« back to all changes in this revision

Viewing changes to .pc/03_no_popt/utils/openchangepfadmin.c

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2012-04-12 20:07:57 UTC
  • mfrom: (11 sid)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20120412200757-k933d9trljmxj1l4
Tags: 1:1.0-4
* openchangeserver: Add dependency on openchangeproxy.
* Rebuild against newer version of Samba 4.
* Use dpkg-buildflags.
* Migrate to Git, update Vcs-Git header.
* Switch to debhelper 9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Public Folders Administration Tool
 
3
 
 
4
   OpenChange Project
 
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 "libmapiadmin/libmapiadmin.h"
 
24
#include <samba/popt.h>
 
25
#include <param.h>
 
26
 
 
27
#include "openchangepfadmin.h"
 
28
#include "openchange-tools.h"
 
29
 
 
30
static int32_t get_aclrights(const char *permission)
 
31
{
 
32
        uint32_t        i;
 
33
 
 
34
        if (!permission) return -1;
 
35
 
 
36
        for (i = 0; aclrights[i].name != NULL; i++) {
 
37
                if (!strcmp(permission, aclrights[i].name)) {
 
38
                        return aclrights[i].value;
 
39
                }
 
40
        }
 
41
        return -1;
 
42
}
 
43
 
 
44
static void list_aclrights(void)
 
45
{
 
46
        uint32_t        i;
 
47
 
 
48
        printf("Valid permissions:\n");
 
49
        for (i = 0; aclrights[i].name != NULL; i++) {
 
50
                printf("\t%s\n", aclrights[i].name);
 
51
        }
 
52
}
 
53
 
 
54
static int32_t check_IPF_class(const char *dirclass)
 
55
{
 
56
        uint32_t        i;
 
57
 
 
58
        if (!dirclass) return -1;
 
59
 
 
60
        for (i = 0; IPF_class[i]; i++) {
 
61
                if (!strcmp(dirclass, IPF_class[i])) {
 
62
                        return 0;
 
63
                }
 
64
        }
 
65
 
 
66
        return -1;
 
67
}
 
68
 
 
69
static void list_IPF_class(void)
 
70
{
 
71
        uint32_t        i;
 
72
 
 
73
        printf("Valid IPF Classes:\n");
 
74
        for (i = 0; IPF_class[i] != NULL; i++) {
 
75
                printf("\t%s\n", IPF_class[i]);
 
76
        }
 
77
}
 
78
 
 
79
static bool get_child_folders_pf(TALLOC_CTX *mem_ctx, mapi_object_t *parent, mapi_id_t folder_id, int count)
 
80
{
 
81
        enum MAPISTATUS         retval;
 
82
        bool                    ret;
 
83
        mapi_object_t           obj_folder;
 
84
        mapi_object_t           obj_htable;
 
85
        struct SPropTagArray    *SPropTagArray;
 
86
        struct SRowSet          rowset;
 
87
        const char              *name;
 
88
        const uint32_t          *child;
 
89
        uint32_t                index;
 
90
        const uint64_t          *fid;
 
91
        int                     i;
 
92
 
 
93
        mapi_object_init(&obj_folder);
 
94
        retval = OpenFolder(parent, folder_id, &obj_folder);
 
95
        if (retval != MAPI_E_SUCCESS) return false;
 
96
 
 
97
        mapi_object_init(&obj_htable);
 
98
        retval = GetHierarchyTable(&obj_folder, &obj_htable, 0, NULL);
 
99
        if (retval != MAPI_E_SUCCESS) return false;
 
100
 
 
101
        SPropTagArray = set_SPropTagArray(mem_ctx, 0x3,
 
102
                                          PR_DISPLAY_NAME_UNICODE,
 
103
                                          PR_FID,
 
104
                                          PR_FOLDER_CHILD_COUNT);
 
105
        retval = SetColumns(&obj_htable, SPropTagArray);
 
106
        MAPIFreeBuffer(SPropTagArray);
 
107
        if (retval != MAPI_E_SUCCESS) return false;
 
108
        
 
109
        while (((retval = QueryRows(&obj_htable, 0x32, TBL_ADVANCE, &rowset)) != MAPI_E_NOT_FOUND) && rowset.cRows) {
 
110
                for (index = 0; index < rowset.cRows; index++) {
 
111
                        fid = (const uint64_t *)find_SPropValue_data(&rowset.aRow[index], PR_FID);
 
112
                        name = (const char *)find_SPropValue_data(&rowset.aRow[index], PR_DISPLAY_NAME_UNICODE);
 
113
                        child = (const uint32_t *)find_SPropValue_data(&rowset.aRow[index], PR_FOLDER_CHILD_COUNT);
 
114
 
 
115
                        for (i = 0; i < count; i++) {
 
116
                                printf("|   ");
 
117
                        }
 
118
                        printf("|---+ %-15s\n", name);
 
119
                        if (*child) {
 
120
                                ret = get_child_folders_pf(mem_ctx, &obj_folder, *fid, count + 1);
 
121
                                if (ret == false) return ret;
 
122
                        }
 
123
                        
 
124
                }
 
125
        }
 
126
        return true;
 
127
}
 
128
 
 
129
static enum MAPISTATUS openchangepfadmin_getdir(TALLOC_CTX *mem_ctx, 
 
130
                                                mapi_object_t *obj_container,
 
131
                                                mapi_object_t *obj_child,
 
132
                                                const char *folder)
 
133
{
 
134
        enum MAPISTATUS         retval;
 
135
        struct SPropTagArray    *SPropTagArray;
 
136
        struct SRowSet          rowset;
 
137
        mapi_object_t           obj_htable;
 
138
        const char              *name;
 
139
        const uint64_t          *fid;
 
140
        uint32_t                index;
 
141
 
 
142
        mapi_object_init(&obj_htable);
 
143
        retval = GetHierarchyTable(obj_container, &obj_htable, 0, NULL);
 
144
        MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
145
 
 
146
        SPropTagArray = set_SPropTagArray(mem_ctx, 0x2,
 
147
                                          PR_DISPLAY_NAME_UNICODE,
 
148
                                          PR_FID);
 
149
        retval = SetColumns(&obj_htable, SPropTagArray);
 
150
        MAPIFreeBuffer(SPropTagArray);
 
151
        if (retval != MAPI_E_SUCCESS) return MAPI_E_NOT_FOUND;
 
152
        
 
153
        while (((retval = QueryRows(&obj_htable, 0x32, TBL_ADVANCE, &rowset)) != MAPI_E_NOT_FOUND) && rowset.cRows) {
 
154
                for (index = 0; index < rowset.cRows; index++) {
 
155
                        fid = (const uint64_t *)find_SPropValue_data(&rowset.aRow[index], PR_FID);
 
156
                        name = (const char *)find_SPropValue_data(&rowset.aRow[index], PR_DISPLAY_NAME_UNICODE);
 
157
 
 
158
                        if (name && fid && !strcmp(name, folder)) {
 
159
                                retval = OpenFolder(obj_container, *fid, obj_child);
 
160
                                MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
161
                                return MAPI_E_SUCCESS;
 
162
                        }
 
163
                }
 
164
        }
 
165
 
 
166
        errno = MAPI_E_NOT_FOUND;
 
167
        return MAPI_E_NOT_FOUND;
 
168
}
 
169
 
 
170
static enum MAPISTATUS openchangepfadmin_mkdir(mapi_object_t *obj_container, 
 
171
                                               const char *name, 
 
172
                                               const char *comment, 
 
173
                                               const char *type)
 
174
{
 
175
        enum MAPISTATUS         retval;
 
176
        mapi_object_t           obj_folder;
 
177
        struct SPropValue       props[1];
 
178
        uint32_t                prop_count = 0;
 
179
 
 
180
        mapi_object_init(&obj_folder);
 
181
        retval = CreateFolder(obj_container, FOLDER_GENERIC, name, comment, OPEN_IF_EXISTS, &obj_folder);
 
182
        MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
183
 
 
184
        if (type) {
 
185
                set_SPropValue_proptag(&props[0], PR_CONTAINER_CLASS, (const void *) type);
 
186
                prop_count++;
 
187
 
 
188
                retval = SetProps(&obj_folder, 0, props, prop_count);
 
189
                mapi_object_release(&obj_folder);
 
190
                MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
191
        }
 
192
        
 
193
        return MAPI_E_SUCCESS;
 
194
}
 
195
 
 
196
static enum MAPISTATUS openchangepfadmin_rmdir(TALLOC_CTX *mem_ctx,
 
197
                                               mapi_object_t *obj_container,
 
198
                                               const char *name)
 
199
{
 
200
        enum MAPISTATUS         retval;
 
201
        mapi_object_t           obj_folder;
 
202
 
 
203
        mapi_object_init(&obj_folder);
 
204
 
 
205
        retval = openchangepfadmin_getdir(mem_ctx, obj_container, &obj_folder, name);
 
206
        MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
207
 
 
208
        retval = EmptyFolder(&obj_folder);
 
209
        MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
210
 
 
211
        retval = DeleteFolder(obj_container, mapi_object_get_id(&obj_folder),
 
212
                              DEL_FOLDERS, NULL);
 
213
        MAPI_RETVAL_IF(retval, GetLastError(), NULL);
 
214
 
 
215
        mapi_object_release(&obj_folder);
 
216
 
 
217
        return MAPI_E_SUCCESS;
 
218
}
 
219
 
 
220
int main(int argc, const char *argv[])
 
221
{
 
222
        TALLOC_CTX              *mem_ctx;
 
223
        enum MAPISTATUS         retval;
 
224
        struct mapi_context     *mapi_ctx;
 
225
        struct mapi_session     *session = NULL;
 
226
        mapi_object_t           obj_store;
 
227
        mapi_object_t           obj_ipm_subtree;
 
228
        mapi_object_t           obj_folder;
 
229
        mapi_id_t               id_pf;
 
230
        poptContext             pc;
 
231
        int                     opt;
 
232
        const char              *opt_profdb = NULL;
 
233
        char                    *opt_profname = NULL;
 
234
        const char              *opt_password = NULL;
 
235
        const char              *opt_comment = NULL;
 
236
        const char              *opt_dirclass = NULL;
 
237
        const char              *opt_adduser = NULL;
 
238
        const char              *opt_rmuser = NULL;
 
239
        const char              *opt_apassword = NULL;
 
240
        const char              *opt_adesc = NULL;
 
241
        const char              *opt_acomment = NULL;
 
242
        const char              *opt_afullname = NULL;
 
243
        const char              *opt_addright = NULL;
 
244
        bool                    opt_rmright = false;
 
245
        const char              *opt_modright = NULL;
 
246
        const char              *opt_folder = NULL;
 
247
        const char              *opt_debug = NULL;
 
248
        const char              *opt_username = NULL;   
 
249
        int32_t                 opt_permission = -1;
 
250
        bool                    opt_ipm_list = false;
 
251
        bool                    opt_mkdir = false;
 
252
        bool                    opt_rmdir = false;
 
253
        bool                    opt_dumpdata = false;
 
254
 
 
255
        enum {OPT_PROFILE_DB=1000, OPT_PROFILE, OPT_PASSWORD, OPT_IPM_LIST, 
 
256
              OPT_MKDIR, OPT_RMDIR, OPT_COMMENT, OPT_DIRCLASS, OPT_ACL, 
 
257
              OPT_ADDUSER, OPT_RMUSER, OPT_DEBUG, OPT_APASSWORD, OPT_ADESC, 
 
258
              OPT_ACOMMENT, OPT_AFULLNAME, OPT_ADDRIGHT, OPT_RMRIGHT, 
 
259
              OPT_MODRIGHT, OPT_USERNAME, OPT_FOLDER, OPT_DUMPDATA};
 
260
 
 
261
        struct poptOption long_options[] = {
 
262
                POPT_AUTOHELP
 
263
                {"database", 'f', POPT_ARG_STRING, NULL, OPT_PROFILE_DB, "set the profile database path", "PATH"},
 
264
                {"profile", 'p', POPT_ARG_STRING, NULL, OPT_PROFILE, "set the profile name", "PROFILE"},
 
265
                {"password", 'P', POPT_ARG_STRING, NULL, OPT_PASSWORD, "set the profile password", "PASSWORD"},
 
266
                {"apassword", 0, POPT_ARG_STRING, NULL, OPT_APASSWORD, "set the account password", "PASSWORD"},
 
267
                {"adesc", 0, POPT_ARG_STRING, NULL, OPT_ADESC, "set the account description", "DESCRIPTION"},
 
268
                {"acomment", 0, POPT_ARG_STRING, NULL, OPT_ACOMMENT, "set the account comment", "COMMENT"},
 
269
                {"afullname", 0, POPT_ARG_STRING, NULL, OPT_AFULLNAME, "set the account full name", "NAME"},
 
270
                {"list",0, POPT_ARG_NONE, NULL, OPT_IPM_LIST, "list IPM subtree directories", NULL},
 
271
                {"mkdir", 0, POPT_ARG_NONE, NULL, OPT_MKDIR, "create a Public Folder directory", NULL},
 
272
                {"rmdir", 0, POPT_ARG_NONE, NULL, OPT_RMDIR, "delete a Public Folder directory", NULL},
 
273
                {"comment", 0, POPT_ARG_STRING, NULL, OPT_COMMENT, "set the folder comment", "COMMENT"},
 
274
                {"dirclass", 0, POPT_ARG_STRING, NULL, OPT_DIRCLASS, "set the folder class", "CLASS"},
 
275
                {"adduser", 0, POPT_ARG_STRING, NULL, OPT_ADDUSER, "add Exchange user", "USERNAME"},
 
276
                {"rmuser", 0, POPT_ARG_STRING, NULL, OPT_RMUSER, "delete Exchange user", "USERNAME"},
 
277
                {"addright", 0, POPT_ARG_STRING, NULL, OPT_ADDRIGHT, "add MAPI permissions to PF folder", "RIGHT"},
 
278
                {"rmright", 0, POPT_ARG_NONE, NULL, OPT_RMRIGHT, "remove MAPI permissions to PF folder", NULL},
 
279
                {"modright", 0, POPT_ARG_STRING, NULL, OPT_MODRIGHT, "modify MAPI permissions to PF folder", "RIGHT"},
 
280
                {"debuglevel", 0, POPT_ARG_STRING, NULL, OPT_DEBUG, "set debug level", "LEVEL"},
 
281
                {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMPDATA, "Dump the hex data", NULL},
 
282
                {"folder", 0, POPT_ARG_STRING, NULL, OPT_FOLDER, "specify the Public Folder directory", "FOLDER"},
 
283
                {"username", 0, POPT_ARG_STRING, NULL, OPT_USERNAME, "specify the username to use", "USERNAME"},
 
284
                POPT_OPENCHANGE_VERSION
 
285
                { NULL, 0, POPT_ARG_NONE, NULL, 0, NULL, NULL }
 
286
        };
 
287
 
 
288
        mem_ctx = talloc_named(NULL, 0, "openchangepfadmin");
 
289
 
 
290
        pc = poptGetContext("openchangepfadmin", argc, argv, long_options, 0);
 
291
 
 
292
        while ((opt = poptGetNextOpt(pc)) != -1) {
 
293
                switch (opt) {
 
294
                case OPT_DEBUG:
 
295
                        opt_debug = poptGetOptArg(pc);
 
296
                        break;
 
297
                case OPT_DUMPDATA:
 
298
                        opt_dumpdata = true;
 
299
                        break;
 
300
                case OPT_PROFILE_DB:
 
301
                        opt_profdb = poptGetOptArg(pc);
 
302
                        break;
 
303
                case OPT_PROFILE:
 
304
                        opt_profname = talloc_strdup(mem_ctx, poptGetOptArg(pc));
 
305
                        break;
 
306
                case OPT_PASSWORD:
 
307
                        opt_password = poptGetOptArg(pc);
 
308
                        break;
 
309
                case OPT_IPM_LIST:
 
310
                        opt_ipm_list = true;
 
311
                        break;
 
312
                case OPT_MKDIR:
 
313
                        opt_mkdir = true;
 
314
                        break;
 
315
                case OPT_RMDIR:
 
316
                        opt_rmdir = true;
 
317
                        break;
 
318
                case OPT_COMMENT:
 
319
                        opt_comment = poptGetOptArg(pc);
 
320
                        break;
 
321
                case OPT_DIRCLASS:
 
322
                        opt_dirclass = poptGetOptArg(pc);
 
323
                        break;
 
324
                case OPT_ADDUSER:
 
325
                        opt_adduser = poptGetOptArg(pc);
 
326
                        break;
 
327
                case OPT_RMUSER:
 
328
                        opt_rmuser = poptGetOptArg(pc);
 
329
                        break;
 
330
                case OPT_APASSWORD:
 
331
                        opt_apassword = poptGetOptArg(pc);
 
332
                        break;
 
333
                case OPT_ADESC:
 
334
                        opt_adesc = poptGetOptArg(pc);
 
335
                        break;
 
336
                case OPT_ACOMMENT:
 
337
                        opt_acomment = poptGetOptArg(pc);
 
338
                        break;
 
339
                case OPT_AFULLNAME:
 
340
                        opt_afullname = poptGetOptArg(pc);
 
341
                        break;
 
342
                case OPT_ADDRIGHT:
 
343
                        opt_addright = poptGetOptArg(pc);
 
344
                        break;
 
345
                case OPT_RMRIGHT:
 
346
                        opt_rmright = true;
 
347
                        break;
 
348
                case OPT_MODRIGHT:
 
349
                        opt_modright = poptGetOptArg(pc);
 
350
                        break;
 
351
                case OPT_FOLDER:
 
352
                        opt_folder = poptGetOptArg(pc);
 
353
                        break;
 
354
                case OPT_USERNAME:
 
355
                        opt_username = poptGetOptArg(pc);
 
356
                        break;
 
357
                default:
 
358
                        break;
 
359
                };
 
360
        }
 
361
 
 
362
        /* Sanity check on options */
 
363
 
 
364
        if ((opt_mkdir == true && !opt_folder) || (opt_rmdir == true && !opt_folder) ||
 
365
            (opt_addright && !opt_folder) || (opt_rmright == true && !opt_folder) || 
 
366
            (opt_modright && !opt_folder)) {
 
367
                printf("You need to specify a directory with --folder option\n");
 
368
                return (-1);
 
369
        }
 
370
 
 
371
        if ((opt_addright && !opt_username) || (opt_rmright == true && !opt_username) ||
 
372
            (opt_modright && !opt_username)) {
 
373
                printf("You need to specify a username with --username option\n");
 
374
                return (-1);
 
375
        }
 
376
 
 
377
        /* dirclass sanity check */
 
378
        if (opt_dirclass) {
 
379
                if (check_IPF_class(opt_dirclass) == -1) {
 
380
                        list_IPF_class();
 
381
                        return (-1);
 
382
                }
 
383
        }
 
384
 
 
385
        if (!opt_profdb) {
 
386
                opt_profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB, getenv("HOME"));
 
387
        }
 
388
 
 
389
        /**
 
390
         * Initialize MAPI subsystem
 
391
         */
 
392
        retval = MAPIInitialize(&mapi_ctx, opt_profdb);
 
393
        if (retval != MAPI_E_SUCCESS) {
 
394
                mapi_errstr("MAPIInitialize", GetLastError());
 
395
                exit (1);
 
396
        }
 
397
 
 
398
        /* debug options */
 
399
        SetMAPIDumpData(mapi_ctx, opt_dumpdata);
 
400
 
 
401
        if (opt_debug) {
 
402
                SetMAPIDebugLevel(mapi_ctx, atoi(opt_debug));
 
403
        }
 
404
 
 
405
        /**
 
406
         * If no profile is specified, try to load the default one
 
407
         * from the database
 
408
         */
 
409
        if (!opt_profname) {
 
410
                retval = GetDefaultProfile(mapi_ctx, &opt_profname);
 
411
                if (retval != MAPI_E_SUCCESS) {
 
412
                        mapi_errstr("GetDefaultProfile", GetLastError());
 
413
                        exit (1);
 
414
                }
 
415
        }
 
416
 
 
417
        retval = MapiLogonEx(mapi_ctx, &session, opt_profname, opt_password);
 
418
        talloc_free(opt_profname);
 
419
        if (retval != MAPI_E_SUCCESS) {
 
420
                mapi_errstr("MapiLogonEx", GetLastError());
 
421
                exit (1);
 
422
        }
 
423
 
 
424
        /**
 
425
         * User management operations
 
426
         */
 
427
 
 
428
        if (opt_adduser) {
 
429
                struct mapiadmin_ctx    *mapiadmin_ctx;
 
430
 
 
431
                mapiadmin_ctx = mapiadmin_init(session);
 
432
                mapiadmin_ctx->username = opt_adduser;
 
433
                mapiadmin_ctx->password = opt_apassword;
 
434
                mapiadmin_ctx->description = opt_adesc;
 
435
                mapiadmin_ctx->comment = opt_acomment;
 
436
                mapiadmin_ctx->fullname = opt_afullname;
 
437
 
 
438
                retval = mapiadmin_user_add(mapiadmin_ctx);
 
439
                mapi_errstr("mapiadmin_user_add", GetLastError());
 
440
                if (retval != MAPI_E_SUCCESS) {
 
441
                        exit (1);
 
442
                }
 
443
                printf("username: %s\n", mapiadmin_ctx->username);
 
444
                printf("password: %s\n", mapiadmin_ctx->password);
 
445
        }
 
446
 
 
447
        if (opt_rmuser) {
 
448
                struct mapiadmin_ctx *mapiadmin_ctx;
 
449
                
 
450
                mapiadmin_ctx = mapiadmin_init(session);
 
451
                mapiadmin_ctx->username = opt_rmuser;
 
452
 
 
453
                retval = mapiadmin_user_del(mapiadmin_ctx);
 
454
                mapi_errstr("mapiadmin_user_del", GetLastError());
 
455
        }
 
456
 
 
457
        /**
 
458
         * Open Public Folder Store
 
459
         */
 
460
 
 
461
        mapi_object_init(&obj_store);
 
462
        retval = OpenPublicFolder(session, &obj_store);
 
463
        if (retval != MAPI_E_SUCCESS) {
 
464
                mapi_errstr("OpenPublicFolder", GetLastError());
 
465
                goto end;
 
466
        }
 
467
 
 
468
        /* Open IPM Subtree (All Public Folders) */
 
469
        retval = GetDefaultPublicFolder(&obj_store, &id_pf, olFolderPublicIPMSubtree);
 
470
        if (retval != MAPI_E_SUCCESS) {
 
471
                goto end;
 
472
        }
 
473
 
 
474
        mapi_object_init(&obj_ipm_subtree);
 
475
        retval = OpenFolder(&obj_store, id_pf, &obj_ipm_subtree);
 
476
        if (retval != MAPI_E_SUCCESS) {
 
477
                mapi_errstr("OpenFolder", GetLastError());
 
478
                goto end;
 
479
        }
 
480
 
 
481
        /* create directory */
 
482
        if (opt_mkdir == true) {
 
483
                retval = openchangepfadmin_mkdir(&obj_ipm_subtree, opt_folder, opt_comment, opt_dirclass);
 
484
                if (retval != MAPI_E_SUCCESS) {
 
485
                        mapi_errstr("mkdir", GetLastError());
 
486
                        goto end;
 
487
                }
 
488
        }
 
489
 
 
490
        /* remove directory */
 
491
        if (opt_rmdir == true) {
 
492
                retval = openchangepfadmin_rmdir(mem_ctx, &obj_ipm_subtree, opt_folder);
 
493
                if (retval != MAPI_E_SUCCESS) {
 
494
                        mapi_errstr("rmdir", GetLastError());
 
495
                        goto end;
 
496
                }
 
497
                
 
498
        }
 
499
 
 
500
        /* add permissions */
 
501
        if (opt_addright) {
 
502
                if ((opt_permission = get_aclrights(opt_addright)) == -1) {
 
503
                        printf("Invalid permission\n");
 
504
                        list_aclrights();
 
505
                        goto end;
 
506
                } else {
 
507
                        mapi_object_init(&obj_folder);
 
508
                        retval = openchangepfadmin_getdir(mem_ctx, &obj_ipm_subtree, &obj_folder, opt_folder);
 
509
                        if (retval != MAPI_E_SUCCESS) {
 
510
                                printf("Invalid directory\n");
 
511
                                goto end;
 
512
                        }
 
513
                        retval = AddUserPermission(&obj_folder, opt_username, opt_permission);
 
514
                        if (retval != MAPI_E_SUCCESS) {
 
515
                                mapi_errstr("AddUserPermission", GetLastError());
 
516
                                mapi_object_release(&obj_folder);
 
517
                                goto end;
 
518
                        }
 
519
                        mapi_object_release(&obj_folder);
 
520
                }
 
521
                printf("Permission %s added for %s on folder %s\n", opt_addright, opt_username, opt_folder);
 
522
        }
 
523
 
 
524
        /* remove permissions */
 
525
        if (opt_rmright == true) {
 
526
                mapi_object_init(&obj_folder);
 
527
                retval = openchangepfadmin_getdir(mem_ctx, &obj_ipm_subtree, &obj_folder, opt_folder);
 
528
                if (retval != MAPI_E_SUCCESS) {
 
529
                        printf("Invalid directory\n");
 
530
                        goto end;
 
531
                }
 
532
                retval = RemoveUserPermission(&obj_folder, opt_username);
 
533
                if (retval != MAPI_E_SUCCESS) {
 
534
                        mapi_errstr("RemoveUserPermission", GetLastError());
 
535
                        mapi_object_release(&obj_folder);
 
536
                        goto end;
 
537
                }
 
538
                mapi_object_release(&obj_folder);
 
539
                printf("Permission removed for %s on folder %s\n", opt_username, opt_folder);
 
540
        }
 
541
 
 
542
        /* modify permissions */
 
543
        if (opt_modright) {
 
544
                opt_permission = get_aclrights(opt_modright);
 
545
                if (opt_permission == -1) {
 
546
                        printf("Invalid permission\n");
 
547
                        list_aclrights();
 
548
                        goto end;
 
549
                } else {
 
550
                        mapi_object_init(&obj_folder);
 
551
                        retval = openchangepfadmin_getdir(mem_ctx, &obj_ipm_subtree, &obj_folder, opt_folder);
 
552
                        if (retval != MAPI_E_SUCCESS) {
 
553
                                printf("Invalid directory\n");
 
554
                                goto end;
 
555
                        }
 
556
                        retval = ModifyUserPermission(&obj_folder, opt_username, opt_permission);
 
557
                        if (retval != MAPI_E_SUCCESS) {
 
558
                                mapi_errstr("ModifyUserPermission", GetLastError());
 
559
                                mapi_object_release(&obj_folder);
 
560
                                goto end;
 
561
                        }
 
562
                        mapi_object_release(&obj_folder);
 
563
                }
 
564
                printf("Permission changed to %s for %s on folder %s\n", opt_modright, opt_username, opt_folder);
 
565
        }
 
566
 
 
567
        /* list directories */
 
568
        if (opt_ipm_list == true) {
 
569
                printf("+ All Public Folders\n");
 
570
                get_child_folders_pf(mem_ctx, &obj_ipm_subtree, id_pf, 0);
 
571
        }
 
572
 
 
573
        /**
 
574
         * Uninitialize MAPI subsystem
 
575
         */
 
576
end:
 
577
        mapi_object_release(&obj_store);
 
578
 
 
579
        MAPIUninitialize(mapi_ctx);
 
580
        talloc_free(mem_ctx);
 
581
        return 0;
 
582
}