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

« back to all changes in this revision

Viewing changes to utils/mapiprofile.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:
20
20
*/
21
21
 
22
22
#include "libmapi/libmapi.h"
23
 
#include <samba/popt.h>
 
23
#include <popt.h>
24
24
#include <param.h>
25
25
#include "openchange-tools.h"
26
26
 
55
55
static bool mapiprofile_createdb(const char *profdb, const char *ldif_path)
56
56
{
57
57
        enum MAPISTATUS retval;
58
 
        
 
58
 
59
59
        if (access(profdb, F_OK) == 0) {
60
60
                fprintf(stderr, "[ERROR] mapiprofile: %s already exists\n", profdb);
61
61
                return false;
63
63
 
64
64
        retval = CreateProfileStore(profdb, ldif_path);
65
65
        if (retval != MAPI_E_SUCCESS) {
66
 
                mapi_errstr("CreateProfileStore", GetLastError());
 
66
                mapi_errstr("CreateProfileStore", retval);
67
67
                return false;
68
68
        }
 
69
 
69
70
        return true;
70
71
}
71
72
 
113
114
 
114
115
        fprintf(stderr, "CTRL-C caught ... Deleting profile\n");
115
116
        if ((retval = DeleteProfile(g_mapi_ctx, g_profname)) != MAPI_E_SUCCESS) {
116
 
                mapi_errstr("DeleteProfile", GetLastError());
 
117
                mapi_errstr("DeleteProfile", retval);
117
118
        }
118
119
 
119
120
        (void) signal(SIGINT, SIG_DFL);
120
121
        exit (1);
121
122
}
122
123
 
123
 
static bool mapiprofile_create(const char *profdb, const char *profname,
 
124
static bool mapiprofile_create(struct mapi_context *mapi_ctx,
 
125
                               const char *profdb, const char *profname,
124
126
                               const char *pattern, const char *username, 
125
127
                               const char *password, const char *address, 
126
128
                               const char *language, const char *workstation,
130
132
                               uint8_t exchange_version, const char *kerberos)
131
133
{
132
134
        enum MAPISTATUS         retval;
133
 
        struct mapi_context     *mapi_ctx;
134
135
        struct mapi_session     *session = NULL;
135
136
        TALLOC_CTX              *mem_ctx;
136
137
        struct mapi_profile     profile;
141
142
        char                    *cpid_str;
142
143
        char                    *lcid_str;
143
144
 
144
 
        mem_ctx = talloc_named(NULL, 0, "mapiprofile_create");
145
 
 
146
 
        retval = MAPIInitialize(&mapi_ctx, profdb);
147
 
        if (retval != MAPI_E_SUCCESS) {
148
 
                mapi_errstr("MAPIInitialize", GetLastError());
149
 
                talloc_free(mem_ctx);
150
 
                return false;
151
 
        }
 
145
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_create");
152
146
 
153
147
        /* catch CTRL-C */
154
148
        g_profname = profname;
173
167
        retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
174
168
        if (retval == MAPI_E_SUCCESS) {
175
169
                fprintf(stderr, "[ERROR] mapiprofile: profile \"%s\" already exists\n", profname);
176
 
                MAPIUninitialize(mapi_ctx);
177
170
                talloc_free(mem_ctx);
178
171
                return false;
179
172
        }
180
173
 
181
174
        retval = CreateProfile(mapi_ctx, profname, username, password, flags);
182
175
        if (retval != MAPI_E_SUCCESS) {
183
 
                mapi_errstr("CreateProfile", GetLastError());
 
176
                mapi_errstr("CreateProfile", retval);
184
177
                talloc_free(mem_ctx);
185
178
                return false;
186
179
        }
209
202
                printf("Invalid Language supplied or unknown system language '%s\n'", language);
210
203
                printf("Deleting profile\n");
211
204
                if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
212
 
                        mapi_errstr("DeleteProfile", GetLastError());
 
205
                        mapi_errstr("DeleteProfile", retval);
213
206
                }
214
207
                talloc_free(mem_ctx);
215
208
                return false;
231
224
 
232
225
        retval = MapiLogonProvider(mapi_ctx, &session, profname, password, PROVIDER_ID_NSPI);
233
226
        if (retval != MAPI_E_SUCCESS) {
234
 
                mapi_errstr("MapiLogonProvider", GetLastError());
 
227
                mapi_errstr("MapiLogonProvider", retval);
235
228
                printf("Deleting profile\n");
236
229
                if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
237
 
                        mapi_errstr("DeleteProfile", GetLastError());
 
230
                        mapi_errstr("DeleteProfile", retval);
238
231
                }
239
232
                talloc_free(mem_ctx);
240
233
                return false;
246
239
 
247
240
        retval = ProcessNetworkProfile(session, username, (mapi_profile_callback_t) callback, "Select a user id");
248
241
        if (retval != MAPI_E_SUCCESS && retval != 0x1) {
249
 
                mapi_errstr("ProcessNetworkProfile", GetLastError());
 
242
                mapi_errstr("ProcessNetworkProfile", retval);
250
243
                printf("Deleting profile\n");
251
244
                if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
252
 
                        mapi_errstr("DeleteProfile", GetLastError());
 
245
                        mapi_errstr("DeleteProfile", retval);
253
246
                }
254
247
                talloc_free(mem_ctx);
255
248
                return false;
259
252
 
260
253
        talloc_free(mem_ctx);
261
254
 
262
 
        MAPIUninitialize(mapi_ctx);
263
 
 
264
255
        return true;
265
256
}
266
257
 
267
 
static void mapiprofile_delete(const char *profdb, const char *profname)
 
258
static void mapiprofile_delete(struct mapi_context *mapi_ctx, const char *profdb, const char *profname)
268
259
{
269
260
        enum MAPISTATUS         retval;
270
 
        struct mapi_context     *mapi_ctx;
271
 
 
272
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
273
 
                mapi_errstr("MAPIInitialize", GetLastError());
274
 
                exit (1);
275
 
        }
276
261
 
277
262
        if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
278
 
                mapi_errstr("DeleteProfile", GetLastError());
 
263
                mapi_errstr("DeleteProfile", retval);
279
264
                exit (1);
280
265
        }
281
266
 
282
267
        printf("Profile %s deleted from database %s\n", profname, profdb);
283
 
 
284
 
        MAPIUninitialize(mapi_ctx);
285
268
}
286
269
 
287
270
 
288
 
static void mapiprofile_rename(const char *profdb, const char *old_profname, const char *new_profname)
 
271
static void mapiprofile_rename(struct mapi_context *mapi_ctx, const char *profdb, const char *old_profname, const char *new_profname)
289
272
{
290
273
        TALLOC_CTX              *mem_ctx;
291
274
        enum MAPISTATUS         retval;
292
 
        struct mapi_context     *mapi_ctx;
293
275
        char                    *profname;
294
276
 
295
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
296
 
                mapi_errstr("MAPIInitialize", retval);
297
 
                exit (1);
298
 
        }
299
 
 
300
 
        mem_ctx = talloc_named(NULL, 0, "mapiprofile_rename");
 
277
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_rename");
301
278
 
302
279
        if (!old_profname) {
303
280
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
318
295
 
319
296
        talloc_free(profname);
320
297
        talloc_free(mem_ctx);
321
 
        MAPIUninitialize(mapi_ctx);
322
298
}
323
299
 
324
300
 
325
 
static void mapiprofile_set_default(const char *profdb, const char *profname)
 
301
static void mapiprofile_set_default(struct mapi_context *mapi_ctx, const char *profdb, const char *profname)
326
302
{
327
303
        enum MAPISTATUS         retval;
328
 
        struct mapi_context     *mapi_ctx;
329
 
 
330
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
331
 
                mapi_errstr("MAPIInitialize", GetLastError());
332
 
                exit (1);
333
 
        }
334
304
 
335
305
        if ((retval = SetDefaultProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
336
 
                mapi_errstr("SetDefaultProfile", GetLastError());
 
306
                mapi_errstr("SetDefaultProfile", retval);
337
307
                exit (1);
338
308
        }
339
309
 
340
310
        printf("Profile %s is now set the default one\n", profname);
341
 
 
342
 
        MAPIUninitialize(mapi_ctx);
343
311
}
344
312
 
345
 
static void mapiprofile_get_default(const char *profdb)
 
313
static void mapiprofile_get_default(struct mapi_context *mapi_ctx, const char *profdb)
346
314
{
347
315
        enum MAPISTATUS         retval;
348
 
        struct mapi_context     *mapi_ctx;
349
316
        char                    *profname;
350
317
 
351
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
352
 
                mapi_errstr("MAPIInitialize", GetLastError());
353
 
                exit (1);
354
 
        }
355
 
        
356
318
        if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
357
 
                mapi_errstr("GetDefaultProfile", GetLastError());
 
319
                mapi_errstr("GetDefaultProfile", retval);
358
320
                exit (1);
359
321
        }
360
322
 
361
323
        printf("Default profile is set to %s\n", profname);
362
324
 
363
325
        talloc_free(profname);
364
 
        MAPIUninitialize(mapi_ctx);
365
326
}
366
327
 
367
 
static void mapiprofile_get_fqdn(const char *profdb, 
368
 
                                 const char *opt_profname, 
 
328
static void mapiprofile_get_fqdn(struct mapi_context *mapi_ctx,
 
329
                                 const char *profdb,
 
330
                                 const char *opt_profname,
369
331
                                 const char *password,
370
332
                                 bool opt_dumpdata)
371
333
{
372
334
        TALLOC_CTX              *mem_ctx;
373
335
        enum MAPISTATUS         retval;
374
 
        struct mapi_context     *mapi_ctx;
375
336
        struct mapi_session     *session = NULL;
376
337
        const char              *serverFQDN;
377
338
        char                    *profname;
378
339
 
379
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
380
 
                mapi_errstr("MAPIInitialize", GetLastError());
381
 
                exit (1);
382
 
        }
383
 
 
384
340
        SetMAPIDumpData(mapi_ctx, opt_dumpdata);
385
341
 
386
 
        mem_ctx = talloc_named(NULL, 0, "mapiprofile_get_fqdn");
 
342
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_get_fqdn");
387
343
 
388
344
        if (!opt_profname) {
389
345
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
390
 
                        mapi_errstr("GetDefaultProfile", GetLastError());
 
346
                        mapi_errstr("GetDefaultProfile", retval);
391
347
                        talloc_free(mem_ctx);
392
348
                        exit (1);
393
349
                }
399
355
        talloc_free(profname);
400
356
        talloc_free(mem_ctx);
401
357
        if (retval != MAPI_E_SUCCESS) {
402
 
                mapi_errstr("MapiLogonProvider", GetLastError());
 
358
                mapi_errstr("MapiLogonProvider", retval);
403
359
                exit (1);
404
360
        }
405
361
 
406
362
        retval = RfrGetFQDNFromLegacyDN(mapi_ctx, session, &serverFQDN);
407
363
        if (retval != MAPI_E_SUCCESS) {
408
 
                mapi_errstr("RfrGetFQDNFromLegacyDN", GetLastError());
 
364
                mapi_errstr("RfrGetFQDNFromLegacyDN", retval);
409
365
                exit (1);
410
366
        }
411
367
 
412
368
        printf("%s is at %s\n", mapi_ctx->session->profile->homemdb, serverFQDN);
413
 
 
414
 
        MAPIUninitialize(mapi_ctx);
415
369
}
416
370
 
417
 
static void mapiprofile_list(const char *profdb)
 
371
static void mapiprofile_list(struct mapi_context *mapi_ctx, const char *profdb)
418
372
{
419
373
        enum MAPISTATUS         retval;
420
 
        struct mapi_context     *mapi_ctx;
421
374
        struct SRowSet          proftable;
422
375
        uint32_t                count = 0;
423
376
 
424
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
425
 
                mapi_errstr("MAPIInitialize", GetLastError());
426
 
                exit (1);
427
 
        }
428
 
 
429
377
        memset(&proftable, 0, sizeof (struct SRowSet));
430
378
        if ((retval = GetProfileTable(mapi_ctx, &proftable)) != MAPI_E_SUCCESS) {
431
 
                mapi_errstr("GetProfileTable", GetLastError());
 
379
                mapi_errstr("GetProfileTable", retval);
432
380
                exit (1);
433
381
        }
434
382
 
448
396
                }
449
397
 
450
398
        }
451
 
 
452
 
        MAPIUninitialize(mapi_ctx);
453
399
}
454
400
 
455
 
static void mapiprofile_dump(const char *profdb, const char *opt_profname)
 
401
static void mapiprofile_dump(struct mapi_context *mapi_ctx, const char *profdb, const char *opt_profname)
456
402
{
457
403
        TALLOC_CTX              *mem_ctx;
458
404
        enum MAPISTATUS         retval;
459
 
        struct mapi_context     *mapi_ctx;
460
405
        struct mapi_profile     profile;
461
406
        char                    *profname;
462
407
        char                    *exchange_version = NULL;
463
408
 
464
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
465
 
                mapi_errstr("MAPIInitialize", GetLastError());
466
 
                exit (1);
467
 
        }
468
 
 
469
 
        mem_ctx = talloc_named(NULL, 0, "mapiprofile_dump");
 
409
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_dump");
470
410
 
471
411
        if (!opt_profname) {
472
412
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
473
 
                        mapi_errstr("GetDefaultProfile", GetLastError());
 
413
                        mapi_errstr("GetDefaultProfile", retval);
474
414
                        talloc_free(mem_ctx);
475
415
                        exit (1);
476
416
                }
483
423
 
484
424
        if (retval && (retval != MAPI_E_INVALID_PARAMETER)) {
485
425
                talloc_free(mem_ctx);
486
 
                mapi_errstr("OpenProfile", GetLastError());
 
426
                mapi_errstr("OpenProfile", retval);
487
427
                exit (1);
488
428
        }
489
429
 
514
454
 
515
455
end:
516
456
        talloc_free(mem_ctx);
517
 
        MAPIUninitialize(mapi_ctx);
518
457
}
519
458
 
520
 
static void mapiprofile_attribute(const char *profdb, const char *opt_profname, 
 
459
static void mapiprofile_attribute(struct mapi_context *mapi_ctx, const char *profdb, const char *opt_profname, 
521
460
                                  const char *attribute)
522
461
{
523
462
        TALLOC_CTX              *mem_ctx;
524
463
        enum MAPISTATUS         retval;
525
 
        struct mapi_context     *mapi_ctx;
526
464
        struct mapi_profile     profile;
527
465
        char                    *profname = NULL;
528
466
        char                    **value = NULL;
529
467
        unsigned int            count = 0;
530
468
        unsigned int            i;
531
469
 
532
 
        if ((retval = MAPIInitialize(&mapi_ctx, profdb)) != MAPI_E_SUCCESS) {
533
 
                mapi_errstr("MAPIInitialize", GetLastError());
534
 
                exit (1);
535
 
        }
536
 
 
537
 
        mem_ctx = talloc_named(NULL, 0, "mapiprofile_attribute");
 
470
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_attribute");
538
471
 
539
472
        if (!opt_profname) {
540
473
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
541
 
                        mapi_errstr("GetDefaultProfile", GetLastError());
 
474
                        mapi_errstr("GetDefaultProfile", retval);
542
475
                        exit (1);
543
476
                }
544
477
        } else {
547
480
 
548
481
        retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
549
482
        if (retval && (retval != MAPI_E_INVALID_PARAMETER)) {
550
 
                mapi_errstr("OpenProfile", GetLastError());
 
483
                mapi_errstr("OpenProfile", retval);
551
484
                talloc_free(profname);
552
485
                talloc_free(mem_ctx);
553
486
                exit (1);
554
487
        }
555
488
 
556
489
        if ((retval = GetProfileAttr(&profile, attribute, &count, &value))) {
557
 
                mapi_errstr("ProfileGetAttr", GetLastError());
 
490
                mapi_errstr("ProfileGetAttr", retval);
558
491
                talloc_free(profname);
559
492
                talloc_free(mem_ctx);
560
493
                exit (1);
567
500
        MAPIFreeBuffer(value);
568
501
        talloc_free(profname);
569
502
        talloc_free(mem_ctx);
570
 
 
571
 
        MAPIUninitialize(mapi_ctx);
572
503
}
573
504
 
574
505
static void show_help(poptContext pc, const char *param)
581
512
int main(int argc, const char *argv[])
582
513
{
583
514
        TALLOC_CTX      *mem_ctx;
 
515
        struct mapi_context *mapi_ctx = NULL;
584
516
        int             error;
585
517
        poptContext     pc;
586
518
        int             opt;
802
734
                }
803
735
        }
804
736
 
 
737
        if (MAPIInitialize(&mapi_ctx, profdb) != MAPI_E_SUCCESS) {
 
738
                mapi_errstr("MAPIInitialize", GetLastError());
 
739
                exit (1);
 
740
        }
 
741
 
805
742
        if (opt_krb) {
806
743
                if (strncmp(opt_krb, "yes", 3) && strncmp(opt_krb, "no", 2)) {
807
744
                        fprintf(stderr,
851
788
                }
852
789
                talloc_free((void *)version);
853
790
 
854
 
                if (! mapiprofile_create(profdb, profname, pattern, username, password, address,
 
791
                if (! mapiprofile_create(mapi_ctx, profdb, profname, pattern, username, password, address,
855
792
                                         language, workstation, domain, realm, nopass, opt_seal, 
856
793
                                         opt_dumpdata, opt_debuglevel,
857
794
                                         exchange_version[i].version,
862
799
        }
863
800
 
864
801
        if (rename) {
865
 
                mapiprofile_rename(profdb, profname, rename);
 
802
                mapiprofile_rename(mapi_ctx, profdb, profname, rename);
866
803
        }
867
804
 
868
805
        if (getfqdn == true) {
869
 
                mapiprofile_get_fqdn(profdb, profname, password, opt_dumpdata);
 
806
                mapiprofile_get_fqdn(mapi_ctx, profdb, profname, password, opt_dumpdata);
870
807
        }
871
808
 
872
809
        if (listlangs == true) {
874
811
        }
875
812
 
876
813
        if (setdflt == true) {
877
 
                mapiprofile_set_default(profdb, profname);
 
814
                mapiprofile_set_default(mapi_ctx, profdb, profname);
878
815
        }
879
816
 
880
817
        if (getdflt == true) {
881
 
                mapiprofile_get_default(profdb);
 
818
                mapiprofile_get_default(mapi_ctx, profdb);
882
819
        }
883
820
 
884
821
        if (delete == true) {
885
 
                mapiprofile_delete(profdb, profname);
 
822
                mapiprofile_delete(mapi_ctx, profdb, profname);
886
823
        }
887
824
 
888
825
        if (list == true) {
889
 
                mapiprofile_list(profdb);
 
826
                mapiprofile_list(mapi_ctx, profdb);
890
827
        }
891
828
 
892
829
        if (dump == true) {
893
 
                mapiprofile_dump(profdb, profname);
 
830
                mapiprofile_dump(mapi_ctx, profdb, profname);
894
831
        }
895
832
 
896
833
        if (attribute) {
897
 
                mapiprofile_attribute(profdb, profname, attribute);
 
834
                mapiprofile_attribute(mapi_ctx, profdb, profname, attribute);
898
835
        }
899
836
 
900
837
cleanup:
909
846
        free((void*)rename);
910
847
        free((void*)attribute);
911
848
 
 
849
        if (mapi_ctx)
 
850
                MAPIUninitialize(mapi_ctx);
912
851
        poptFreeContext(pc);
913
852
        talloc_free(mem_ctx);
914
853