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

« back to all changes in this revision

Viewing changes to .pc/03_no_popt/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:
 
1
/*
 
2
   Wrapper over the MAPI Profile API
 
3
 
 
4
   OpenChange Project
 
5
 
 
6
   Copyright (C) Julien Kerihuel 2007-2009
 
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 <samba/popt.h>
 
24
#include <param.h>
 
25
#include "openchange-tools.h"
 
26
 
 
27
#include <sys/stat.h>
 
28
#include <sys/types.h>
 
29
#include <stdlib.h>
 
30
#include <signal.h>
 
31
 
 
32
#define DEFAULT_DIR                     "%s/.openchange"
 
33
#define DEFAULT_PROFDB                  "%s/.openchange/profiles.ldb"
 
34
#define DEFAULT_EXCHANGE_VERSION        "2000"
 
35
 
 
36
/**
 
37
   MAPI version:
 
38
   #- 0: use EcDoConnect (0x0) / EcDoRpc (0x2) RPC calls
 
39
   #- 1: use EcDoConnectEx (0xA) / EcDoRpcExt2 (0xB) RPC calls
 
40
   #- 2: Same as 1, but require sealed pipe
 
41
 */
 
42
struct exchange_version {
 
43
        const char      *name;
 
44
        uint8_t         version;
 
45
};
 
46
 
 
47
static const struct exchange_version exchange_version[] = {
 
48
        { DEFAULT_EXCHANGE_VERSION,     0 },
 
49
        { "2003",                       1 },
 
50
        { "2007",                       1 },
 
51
        { "2010",                       2 },
 
52
        { NULL,                         0 }
 
53
};
 
54
 
 
55
static bool mapiprofile_createdb(const char *profdb, const char *ldif_path)
 
56
{
 
57
        enum MAPISTATUS retval;
 
58
 
 
59
        if (access(profdb, F_OK) == 0) {
 
60
                fprintf(stderr, "[ERROR] mapiprofile: %s already exists\n", profdb);
 
61
                return false;
 
62
        }
 
63
 
 
64
        retval = CreateProfileStore(profdb, ldif_path);
 
65
        if (retval != MAPI_E_SUCCESS) {
 
66
                mapi_errstr("CreateProfileStore", retval);
 
67
                return false;
 
68
        }
 
69
 
 
70
        return true;
 
71
}
 
72
 
 
73
static uint32_t callback(struct SRowSet *rowset, void *private)
 
74
{
 
75
        uint32_t                i;
 
76
        struct SPropValue       *lpProp;
 
77
        FILE                    *fd;
 
78
        uint32_t                index;
 
79
        char                    entry[10];
 
80
        const char              *label = (const char *)private;
 
81
 
 
82
        printf("%s:\n", label);
 
83
        for (i = 0; i < rowset->cRows; i++) {
 
84
                lpProp = get_SPropValue_SRow(&(rowset->aRow[i]), PR_DISPLAY_NAME);
 
85
                if (lpProp && lpProp->value.lpszA) {
 
86
                        printf("\t[%u] %s\n", i, lpProp->value.lpszA);
 
87
                }
 
88
        }
 
89
        printf("\t[%u] cancel operation\n", i);
 
90
        fd = fdopen(0, "r");
 
91
getentry:
 
92
        printf("Enter username id [0]: ");
 
93
        if (fgets(entry, 10, fd) == NULL) {
 
94
                printf("Failed to read string\n");
 
95
                exit(1);
 
96
        }
 
97
 
 
98
        index = atoi(entry);
 
99
        if (index > i) {
 
100
                printf("Invalid id - Must be a value between 0 and %u\n", i);
 
101
                goto getentry;
 
102
        }
 
103
        
 
104
        fclose(fd);
 
105
        return (index);
 
106
}
 
107
 
 
108
const char *g_profname;
 
109
struct mapi_context *g_mapi_ctx;
 
110
 
 
111
static void signal_delete_profile(void)
 
112
{
 
113
        enum MAPISTATUS retval;
 
114
 
 
115
        fprintf(stderr, "CTRL-C caught ... Deleting profile\n");
 
116
        if ((retval = DeleteProfile(g_mapi_ctx, g_profname)) != MAPI_E_SUCCESS) {
 
117
                mapi_errstr("DeleteProfile", retval);
 
118
        }
 
119
 
 
120
        (void) signal(SIGINT, SIG_DFL);
 
121
        exit (1);
 
122
}
 
123
 
 
124
static bool mapiprofile_create(struct mapi_context *mapi_ctx,
 
125
                               const char *profdb, const char *profname,
 
126
                               const char *pattern, const char *username, 
 
127
                               const char *password, const char *address, 
 
128
                               const char *language, const char *workstation,
 
129
                               const char *domain, const char *realm,
 
130
                               uint32_t flags, bool seal,
 
131
                               bool opt_dumpdata, const char *opt_debuglevel,
 
132
                               uint8_t exchange_version, const char *kerberos)
 
133
{
 
134
        enum MAPISTATUS         retval;
 
135
        struct mapi_session     *session = NULL;
 
136
        TALLOC_CTX              *mem_ctx;
 
137
        struct mapi_profile     profile;
 
138
        const char              *locale;
 
139
        uint32_t                cpid = 0;
 
140
        uint32_t                lcid = 0;
 
141
        char                    *exchange_version_str;
 
142
        char                    *cpid_str;
 
143
        char                    *lcid_str;
 
144
 
 
145
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_create");
 
146
 
 
147
        /* catch CTRL-C */
 
148
        g_profname = profname;
 
149
        g_mapi_ctx = mapi_ctx;
 
150
 
 
151
#if defined (__FreeBSD__)
 
152
        (void) signal(SIGINT, (sig_t) signal_delete_profile);
 
153
#elif defined (__SunOS)
 
154
        (void) signal(SIGINT, signal_delete_profile);
 
155
#else
 
156
        (void) signal(SIGINT, (sighandler_t) signal_delete_profile);
 
157
#endif
 
158
 
 
159
        /* debug options */
 
160
        SetMAPIDumpData(mapi_ctx, opt_dumpdata);
 
161
 
 
162
        if (opt_debuglevel) {
 
163
                SetMAPIDebugLevel(mapi_ctx, atoi(opt_debuglevel));
 
164
        }
 
165
 
 
166
        /* Sanity check */
 
167
        retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
 
168
        if (retval == MAPI_E_SUCCESS) {
 
169
                fprintf(stderr, "[ERROR] mapiprofile: profile \"%s\" already exists\n", profname);
 
170
                talloc_free(mem_ctx);
 
171
                return false;
 
172
        }
 
173
 
 
174
        retval = CreateProfile(mapi_ctx, profname, username, password, flags);
 
175
        if (retval != MAPI_E_SUCCESS) {
 
176
                mapi_errstr("CreateProfile", retval);
 
177
                talloc_free(mem_ctx);
 
178
                return false;
 
179
        }
 
180
 
 
181
        mapi_profile_add_string_attr(mapi_ctx, profname, "binding", address);
 
182
        mapi_profile_add_string_attr(mapi_ctx, profname, "workstation", workstation);
 
183
        mapi_profile_add_string_attr(mapi_ctx, profname, "domain", domain);
 
184
        mapi_profile_add_string_attr(mapi_ctx, profname, "seal", (seal == true) ? "true" : "false");
 
185
 
 
186
        exchange_version_str = talloc_asprintf(mem_ctx, "%d", exchange_version);
 
187
        mapi_profile_add_string_attr(mapi_ctx, profname, "exchange_version", exchange_version_str);
 
188
        talloc_free(exchange_version_str);
 
189
 
 
190
        if (realm) {
 
191
                mapi_profile_add_string_attr(mapi_ctx, profname, "realm", realm);
 
192
        }
 
193
 
 
194
        locale = (const char *) (language) ? mapi_get_locale_from_language(language) : mapi_get_system_locale();
 
195
 
 
196
        if (locale) {
 
197
                cpid = mapi_get_cpid_from_locale(locale);
 
198
                lcid = mapi_get_lcid_from_locale(locale);
 
199
        }
 
200
 
 
201
        if (!locale || !cpid || !lcid) {
 
202
                printf("Invalid Language supplied or unknown system language '%s\n'", language);
 
203
                printf("Deleting profile\n");
 
204
                if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
 
205
                        mapi_errstr("DeleteProfile", retval);
 
206
                }
 
207
                talloc_free(mem_ctx);
 
208
                return false;
 
209
        }
 
210
 
 
211
        cpid_str = talloc_asprintf(mem_ctx, "%d", cpid);
 
212
        lcid_str = talloc_asprintf(mem_ctx, "%d", lcid);
 
213
 
 
214
        mapi_profile_add_string_attr(mapi_ctx, profname, "codepage", cpid_str);
 
215
        mapi_profile_add_string_attr(mapi_ctx, profname, "language", lcid_str);
 
216
        mapi_profile_add_string_attr(mapi_ctx, profname, "method", lcid_str);
 
217
 
 
218
        talloc_free(cpid_str);
 
219
        talloc_free(lcid_str);
 
220
 
 
221
        if (kerberos) {
 
222
                mapi_profile_add_string_attr(mapi_ctx, profname, "kerberos", kerberos);
 
223
        }
 
224
 
 
225
        retval = MapiLogonProvider(mapi_ctx, &session, profname, password, PROVIDER_ID_NSPI);
 
226
        if (retval != MAPI_E_SUCCESS) {
 
227
                mapi_errstr("MapiLogonProvider", retval);
 
228
                printf("Deleting profile\n");
 
229
                if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
 
230
                        mapi_errstr("DeleteProfile", retval);
 
231
                }
 
232
                talloc_free(mem_ctx);
 
233
                return false;
 
234
        }
 
235
 
 
236
        if (pattern) {
 
237
                username = pattern;
 
238
        }
 
239
 
 
240
        retval = ProcessNetworkProfile(session, username, (mapi_profile_callback_t) callback, "Select a user id");
 
241
        if (retval != MAPI_E_SUCCESS && retval != 0x1) {
 
242
                mapi_errstr("ProcessNetworkProfile", retval);
 
243
                printf("Deleting profile\n");
 
244
                if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
 
245
                        mapi_errstr("DeleteProfile", retval);
 
246
                }
 
247
                talloc_free(mem_ctx);
 
248
                return false;
 
249
        }
 
250
 
 
251
        printf("Profile %s completed and added to database %s\n", profname, profdb);
 
252
 
 
253
        talloc_free(mem_ctx);
 
254
 
 
255
        return true;
 
256
}
 
257
 
 
258
static void mapiprofile_delete(struct mapi_context *mapi_ctx, const char *profdb, const char *profname)
 
259
{
 
260
        enum MAPISTATUS         retval;
 
261
 
 
262
        if ((retval = DeleteProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
 
263
                mapi_errstr("DeleteProfile", retval);
 
264
                exit (1);
 
265
        }
 
266
 
 
267
        printf("Profile %s deleted from database %s\n", profname, profdb);
 
268
}
 
269
 
 
270
 
 
271
static void mapiprofile_rename(struct mapi_context *mapi_ctx, const char *profdb, const char *old_profname, const char *new_profname)
 
272
{
 
273
        TALLOC_CTX              *mem_ctx;
 
274
        enum MAPISTATUS         retval;
 
275
        char                    *profname;
 
276
 
 
277
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_rename");
 
278
 
 
279
        if (!old_profname) {
 
280
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
 
281
                        mapi_errstr("GetDefaultProfile", retval);
 
282
                        talloc_free(mem_ctx);
 
283
                        exit (1);
 
284
                }
 
285
        } else {
 
286
                profname = talloc_strdup(mem_ctx, old_profname);
 
287
        }
 
288
 
 
289
        if ((retval = RenameProfile(mapi_ctx, profname, new_profname)) != MAPI_E_SUCCESS) {
 
290
                mapi_errstr("RenameProfile", retval);
 
291
                talloc_free(profname);
 
292
                talloc_free(mem_ctx);
 
293
                exit (1);
 
294
        }
 
295
 
 
296
        talloc_free(profname);
 
297
        talloc_free(mem_ctx);
 
298
}
 
299
 
 
300
 
 
301
static void mapiprofile_set_default(struct mapi_context *mapi_ctx, const char *profdb, const char *profname)
 
302
{
 
303
        enum MAPISTATUS         retval;
 
304
 
 
305
        if ((retval = SetDefaultProfile(mapi_ctx, profname)) != MAPI_E_SUCCESS) {
 
306
                mapi_errstr("SetDefaultProfile", retval);
 
307
                exit (1);
 
308
        }
 
309
 
 
310
        printf("Profile %s is now set the default one\n", profname);
 
311
}
 
312
 
 
313
static void mapiprofile_get_default(struct mapi_context *mapi_ctx, const char *profdb)
 
314
{
 
315
        enum MAPISTATUS         retval;
 
316
        char                    *profname;
 
317
 
 
318
        if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
 
319
                mapi_errstr("GetDefaultProfile", retval);
 
320
                exit (1);
 
321
        }
 
322
 
 
323
        printf("Default profile is set to %s\n", profname);
 
324
 
 
325
        talloc_free(profname);
 
326
}
 
327
 
 
328
static void mapiprofile_get_fqdn(struct mapi_context *mapi_ctx,
 
329
                                 const char *profdb,
 
330
                                 const char *opt_profname,
 
331
                                 const char *password,
 
332
                                 bool opt_dumpdata)
 
333
{
 
334
        TALLOC_CTX              *mem_ctx;
 
335
        enum MAPISTATUS         retval;
 
336
        struct mapi_session     *session = NULL;
 
337
        const char              *serverFQDN;
 
338
        char                    *profname;
 
339
 
 
340
        SetMAPIDumpData(mapi_ctx, opt_dumpdata);
 
341
 
 
342
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_get_fqdn");
 
343
 
 
344
        if (!opt_profname) {
 
345
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
 
346
                        mapi_errstr("GetDefaultProfile", retval);
 
347
                        talloc_free(mem_ctx);
 
348
                        exit (1);
 
349
                }
 
350
        } else {
 
351
                profname = talloc_strdup(mem_ctx, (char *)opt_profname);
 
352
        }
 
353
 
 
354
        retval = MapiLogonProvider(mapi_ctx, &session, profname, password, PROVIDER_ID_NSPI);
 
355
        talloc_free(profname);
 
356
        talloc_free(mem_ctx);
 
357
        if (retval != MAPI_E_SUCCESS) {
 
358
                mapi_errstr("MapiLogonProvider", retval);
 
359
                exit (1);
 
360
        }
 
361
 
 
362
        retval = RfrGetFQDNFromLegacyDN(mapi_ctx, session, &serverFQDN);
 
363
        if (retval != MAPI_E_SUCCESS) {
 
364
                mapi_errstr("RfrGetFQDNFromLegacyDN", retval);
 
365
                exit (1);
 
366
        }
 
367
 
 
368
        printf("%s is at %s\n", mapi_ctx->session->profile->homemdb, serverFQDN);
 
369
}
 
370
 
 
371
static void mapiprofile_list(struct mapi_context *mapi_ctx, const char *profdb)
 
372
{
 
373
        enum MAPISTATUS         retval;
 
374
        struct SRowSet          proftable;
 
375
        uint32_t                count = 0;
 
376
 
 
377
        memset(&proftable, 0, sizeof (struct SRowSet));
 
378
        if ((retval = GetProfileTable(mapi_ctx, &proftable)) != MAPI_E_SUCCESS) {
 
379
                mapi_errstr("GetProfileTable", retval);
 
380
                exit (1);
 
381
        }
 
382
 
 
383
        printf("We have %u profiles in the database:\n", proftable.cRows);
 
384
 
 
385
        for (count = 0; count != proftable.cRows; count++) {
 
386
                const char      *name = NULL;
 
387
                uint32_t        dflt = 0;
 
388
 
 
389
                name = proftable.aRow[count].lpProps[0].value.lpszA;
 
390
                dflt = proftable.aRow[count].lpProps[1].value.l;
 
391
 
 
392
                if (dflt) {
 
393
                        printf("\tProfile = %s [default]\n", name);
 
394
                } else {
 
395
                        printf("\tProfile = %s\n", name);
 
396
                }
 
397
 
 
398
        }
 
399
}
 
400
 
 
401
static void mapiprofile_dump(struct mapi_context *mapi_ctx, const char *profdb, const char *opt_profname)
 
402
{
 
403
        TALLOC_CTX              *mem_ctx;
 
404
        enum MAPISTATUS         retval;
 
405
        struct mapi_profile     profile;
 
406
        char                    *profname;
 
407
        char                    *exchange_version = NULL;
 
408
 
 
409
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_dump");
 
410
 
 
411
        if (!opt_profname) {
 
412
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
 
413
                        mapi_errstr("GetDefaultProfile", retval);
 
414
                        talloc_free(mem_ctx);
 
415
                        exit (1);
 
416
                }
 
417
        } else {
 
418
                profname = talloc_strdup(mem_ctx, (const char *)opt_profname);
 
419
        }
 
420
 
 
421
        retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
 
422
        talloc_free(profname);
 
423
 
 
424
        if (retval && (retval != MAPI_E_INVALID_PARAMETER)) {
 
425
                talloc_free(mem_ctx);
 
426
                mapi_errstr("OpenProfile", retval);
 
427
                exit (1);
 
428
        }
 
429
 
 
430
        switch (profile.exchange_version) {
 
431
        case 0x0:
 
432
                exchange_version = talloc_strdup(mem_ctx, "exchange 2000");
 
433
                break;
 
434
        case 0x1:
 
435
                exchange_version = talloc_strdup(mem_ctx, "exchange 2003/2007");
 
436
                break;
 
437
        case 0x2:
 
438
                exchange_version = talloc_strdup(mem_ctx, "exchange 2010");
 
439
                break;
 
440
        default:
 
441
                printf("Error: unknown Exchange server\n");
 
442
                goto end;
 
443
        }
 
444
 
 
445
        printf("Profile: %s\n", profile.profname);
 
446
        printf("\texchange server == %s\n", exchange_version);
 
447
        printf("\tencryption      == %s\n", (profile.seal == true) ? "yes" : "no");
 
448
        printf("\tusername        == %s\n", profile.username);
 
449
        printf("\tpassword        == %s\n", profile.password);
 
450
        printf("\tmailbox         == %s\n", profile.mailbox);
 
451
        printf("\tworkstation     == %s\n", profile.workstation);
 
452
        printf("\tdomain          == %s\n", profile.domain);
 
453
        printf("\tserver          == %s\n", profile.server);
 
454
 
 
455
end:
 
456
        talloc_free(mem_ctx);
 
457
}
 
458
 
 
459
static void mapiprofile_attribute(struct mapi_context *mapi_ctx, const char *profdb, const char *opt_profname, 
 
460
                                  const char *attribute)
 
461
{
 
462
        TALLOC_CTX              *mem_ctx;
 
463
        enum MAPISTATUS         retval;
 
464
        struct mapi_profile     profile;
 
465
        char                    *profname = NULL;
 
466
        char                    **value = NULL;
 
467
        unsigned int            count = 0;
 
468
        unsigned int            i;
 
469
 
 
470
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_attribute");
 
471
 
 
472
        if (!opt_profname) {
 
473
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
 
474
                        mapi_errstr("GetDefaultProfile", retval);
 
475
                        exit (1);
 
476
                }
 
477
        } else {
 
478
                profname = talloc_strdup(mem_ctx, (const char *)opt_profname);
 
479
        }
 
480
 
 
481
        retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
 
482
        if (retval && (retval != MAPI_E_INVALID_PARAMETER)) {
 
483
                mapi_errstr("OpenProfile", retval);
 
484
                talloc_free(profname);
 
485
                talloc_free(mem_ctx);
 
486
                exit (1);
 
487
        }
 
488
 
 
489
        if ((retval = GetProfileAttr(&profile, attribute, &count, &value))) {
 
490
                mapi_errstr("ProfileGetAttr", retval);
 
491
                talloc_free(profname);
 
492
                talloc_free(mem_ctx);
 
493
                exit (1);
 
494
        }
 
495
 
 
496
        printf("Profile %s: results(%u)\n", profname, count);
 
497
        for (i = 0; i < count; i++) {
 
498
                printf("\t%s = %s\n", attribute, value[i]);
 
499
        }
 
500
        MAPIFreeBuffer(value);
 
501
        talloc_free(profname);
 
502
        talloc_free(mem_ctx);
 
503
}
 
504
 
 
505
static void show_help(poptContext pc, const char *param)
 
506
{
 
507
        printf("%s argument missing\n", param);
 
508
        poptPrintUsage(pc, stderr, 0);
 
509
        exit (1);
 
510
}
 
511
 
 
512
int main(int argc, const char *argv[])
 
513
{
 
514
        TALLOC_CTX      *mem_ctx;
 
515
        struct mapi_context *mapi_ctx = NULL;
 
516
        int             error;
 
517
        poptContext     pc;
 
518
        int             opt;
 
519
        int             i;
 
520
        char            *default_path;
 
521
        bool            found = false;
 
522
        bool            create = false;
 
523
        bool            delete = false;
 
524
        bool            list = false;
 
525
        bool            listlangs = false;
 
526
        bool            dump = false;
 
527
        bool            newdb = false;
 
528
        bool            setdflt = false;
 
529
        bool            getdflt = false;
 
530
        bool            getfqdn = false;
 
531
        bool            opt_dumpdata = false;
 
532
        bool            opt_seal = false;
 
533
        const char      *opt_debuglevel = NULL;
 
534
        const char      *ldif = NULL;
 
535
        const char      *address = NULL;
 
536
        const char      *workstation = NULL;
 
537
        const char      *domain = NULL;
 
538
        const char      *realm = NULL;
 
539
        const char      *username = NULL;
 
540
        const char      *language = NULL;
 
541
        const char      *pattern = NULL;
 
542
        const char      *password = NULL;
 
543
        const char      *profdb = NULL;
 
544
        const char      *profname = NULL;
 
545
        const char      *rename = NULL;
 
546
        const char      *attribute = NULL;
 
547
        const char      *opt_tmp = NULL;
 
548
        const char      *opt_krb = NULL;
 
549
        const char      *version = NULL;
 
550
        uint32_t        nopass = 0;
 
551
        char            hostname[256];
 
552
        int             retcode = EXIT_SUCCESS;
 
553
 
 
554
        enum {OPT_PROFILE_DB=1000, OPT_PROFILE, OPT_ADDRESS, OPT_WORKSTATION,
 
555
              OPT_DOMAIN, OPT_REALM, OPT_USERNAME, OPT_LANGUAGE, OPT_PASSWORD, 
 
556
              OPT_CREATE_PROFILE, OPT_DELETE_PROFILE, OPT_LIST_PROFILE, OPT_DUMP_PROFILE, 
 
557
              OPT_DUMP_ATTR, OPT_PROFILE_NEWDB, OPT_PROFILE_LDIF, OPT_LIST_LANGS,
 
558
              OPT_PROFILE_SET_DFLT, OPT_PROFILE_GET_DFLT, OPT_PATTERN, OPT_GETFQDN,
 
559
              OPT_NOPASS, OPT_RENAME_PROFILE, OPT_DUMPDATA, OPT_DEBUGLEVEL,
 
560
              OPT_ENCRYPT_CONN, OPT_EXCHANGE_VERSION, OPT_KRB};
 
561
 
 
562
        struct poptOption long_options[] = {
 
563
                POPT_AUTOHELP
 
564
                {"ldif", 'L', POPT_ARG_STRING, NULL, OPT_PROFILE_LDIF, "set the ldif path", "PATH"},
 
565
                {"getdefault", 'G', POPT_ARG_NONE, NULL, OPT_PROFILE_GET_DFLT, "get the default profile", NULL},
 
566
                {"default", 'S', POPT_ARG_NONE, NULL, OPT_PROFILE_SET_DFLT, "set the default profile", NULL},
 
567
                {"newdb", 'n', POPT_ARG_NONE, NULL, OPT_PROFILE_NEWDB, "create a new profile store", NULL},
 
568
                {"database", 'f', POPT_ARG_STRING, NULL, OPT_PROFILE_DB, "set the profile database path", "PATH"},
 
569
                {"profile", 'P', POPT_ARG_STRING, NULL, OPT_PROFILE, "set the profile name", "PROFILE"},
 
570
                {"address", 'I', POPT_ARG_STRING, NULL, OPT_ADDRESS, "set the exchange server IP address", "xxx.xxx.xxx.xxx"},
 
571
                {"workstation", 'M', POPT_ARG_STRING, NULL, OPT_WORKSTATION, "set the workstation", "WORKSTATION_NAME"},
 
572
                {"domain", 'D', POPT_ARG_STRING, NULL, OPT_DOMAIN, "set the domain/workgroup", "DOMAIN"},
 
573
                {"realm", 'R', POPT_ARG_STRING, NULL, OPT_REALM, "set the realm", "REALM"},
 
574
                {"encrypt", 'E', POPT_ARG_NONE, NULL, OPT_ENCRYPT_CONN, "enable encryption with Exchange server", NULL },
 
575
                {"exchange-version", 'v', POPT_ARG_STRING, NULL, OPT_EXCHANGE_VERSION, "specify Exchange server version", "2000" },
 
576
                {"username", 'u', POPT_ARG_STRING, NULL, OPT_USERNAME, "set the profile username", "USERNAME"},
 
577
                {"language", 'C', POPT_ARG_STRING, NULL, OPT_LANGUAGE, "set the user's language (if different from system one)", "LANGUAGE"},
 
578
                {"pattern", 's', POPT_ARG_STRING, NULL, OPT_PATTERN, "username to search for", "USERNAME"},
 
579
                {"password", 'p', POPT_ARG_STRING, NULL, OPT_PASSWORD, "set the profile password", "PASSWORD"},
 
580
                {"nopass", 0, POPT_ARG_NONE, NULL, OPT_NOPASS, "do not save password in the profile", NULL},
 
581
                {"create", 'c', POPT_ARG_NONE, NULL, OPT_CREATE_PROFILE, "create a profile in the database", NULL},
 
582
                {"delete", 'r', POPT_ARG_NONE, NULL, OPT_DELETE_PROFILE, "delete a profile in the database", NULL},
 
583
                {"rename", 'R', POPT_ARG_STRING, NULL, OPT_RENAME_PROFILE, "rename a profile in the database", NULL},
 
584
                {"list", 'l', POPT_ARG_NONE, NULL, OPT_LIST_PROFILE, "list existing profiles in the database", NULL},
 
585
                {"listlangs", 0, POPT_ARG_NONE, NULL, OPT_LIST_LANGS, "list all recognised languages", NULL},
 
586
                {"dump", 0, POPT_ARG_NONE, NULL, OPT_DUMP_PROFILE, "dump a profile entry", NULL},
 
587
                {"attr", 'a', POPT_ARG_STRING, NULL, OPT_DUMP_ATTR, "print an attribute value", "VALUE"},
 
588
                {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMPDATA, "dump the hex data", NULL},
 
589
                {"debuglevel", 'd', POPT_ARG_STRING, NULL, OPT_DEBUGLEVEL, "set the debug level", "LEVEL"},
 
590
                {"getfqdn", 0, POPT_ARG_NONE, NULL, OPT_GETFQDN, "returns the DNS FQDN of the NSPI server matching the legacyDN", NULL},
 
591
                {"kerberos", 'k', POPT_ARG_STRING, NULL, OPT_KRB, "specify kerberos behavior (guess by default)", "{yes|no}"},
 
592
                POPT_OPENCHANGE_VERSION
 
593
                { NULL, 0, POPT_ARG_NONE, NULL, 0, NULL, NULL }
 
594
        };
 
595
 
 
596
        mem_ctx = talloc_named(NULL, 0, "mapiprofile");
 
597
 
 
598
        pc = poptGetContext("mapiprofile", argc, argv, long_options, 0);
 
599
 
 
600
        while ((opt = poptGetNextOpt(pc)) != -1) {
 
601
                switch(opt) {
 
602
                case OPT_DUMPDATA:
 
603
                        opt_dumpdata = true;
 
604
                        break;
 
605
                case OPT_DEBUGLEVEL:
 
606
                        opt_debuglevel = poptGetOptArg(pc);
 
607
                        break;
 
608
                case OPT_PROFILE_LDIF:
 
609
                        opt_tmp = poptGetOptArg(pc);
 
610
                        ldif = talloc_strdup(mem_ctx, opt_tmp);
 
611
                        free((void*)opt_tmp);
 
612
                        opt_tmp = NULL;
 
613
                        break;
 
614
                case OPT_PROFILE_NEWDB:
 
615
                        newdb = true;
 
616
                        break;
 
617
                case OPT_PROFILE_SET_DFLT:
 
618
                        setdflt = true;
 
619
                        break;
 
620
                case OPT_PROFILE_GET_DFLT:
 
621
                        getdflt = true;
 
622
                        break;
 
623
                case OPT_PROFILE_DB:
 
624
                        opt_tmp = poptGetOptArg(pc);
 
625
                        profdb = talloc_strdup(mem_ctx, opt_tmp); 
 
626
                        free((void*)opt_tmp);
 
627
                        opt_tmp = NULL;
 
628
                        break;
 
629
                case OPT_PROFILE:
 
630
                        profname = poptGetOptArg(pc);
 
631
                        break;
 
632
                case OPT_ADDRESS:
 
633
                        address = poptGetOptArg(pc);
 
634
                        break;
 
635
                case OPT_WORKSTATION:
 
636
                        opt_tmp = poptGetOptArg(pc);
 
637
                        workstation = talloc_strdup(mem_ctx, opt_tmp);
 
638
                        free((void*)opt_tmp);
 
639
                        opt_tmp = NULL;
 
640
                        break;
 
641
                case OPT_DOMAIN:
 
642
                        domain = poptGetOptArg(pc);
 
643
                        break;
 
644
                case OPT_REALM:
 
645
                        realm = poptGetOptArg(pc);
 
646
                        break;
 
647
                case OPT_ENCRYPT_CONN:
 
648
                        opt_seal = true;
 
649
                        break;
 
650
                case OPT_EXCHANGE_VERSION:
 
651
                        opt_tmp = poptGetOptArg(pc);
 
652
                        version = talloc_strdup(mem_ctx, opt_tmp);
 
653
                        free((void *)opt_tmp);
 
654
                        opt_tmp = NULL;
 
655
                        break;
 
656
                case OPT_USERNAME:
 
657
                        username = poptGetOptArg(pc);
 
658
                        break;
 
659
                case OPT_LANGUAGE:
 
660
                        opt_tmp = poptGetOptArg(pc);
 
661
                        language = talloc_strdup(mem_ctx, opt_tmp);
 
662
                        free((void*)opt_tmp);
 
663
                        opt_tmp = NULL;
 
664
                        break;
 
665
                case OPT_PATTERN:
 
666
                        pattern = poptGetOptArg(pc);
 
667
                        break;
 
668
                case OPT_PASSWORD:
 
669
                        password = poptGetOptArg(pc);
 
670
                        break;
 
671
                case OPT_NOPASS:
 
672
                        nopass = 1;
 
673
                        break;
 
674
                case OPT_CREATE_PROFILE:
 
675
                        create = true;
 
676
                        break;
 
677
                case OPT_DELETE_PROFILE:
 
678
                        delete = true;
 
679
                        break;
 
680
                case OPT_RENAME_PROFILE:
 
681
                        rename = poptGetOptArg(pc);
 
682
                        break;
 
683
                case OPT_LIST_PROFILE:
 
684
                        list = true;
 
685
                        break;
 
686
                case OPT_LIST_LANGS:
 
687
                        listlangs = true;
 
688
                        break;
 
689
                case OPT_DUMP_PROFILE:
 
690
                        dump = true;
 
691
                        break;
 
692
                case OPT_DUMP_ATTR:
 
693
                        attribute = poptGetOptArg(pc);
 
694
                        break;
 
695
                case OPT_GETFQDN:
 
696
                        getfqdn = true;
 
697
                        break;
 
698
                case OPT_KRB:
 
699
                        opt_krb = poptGetOptArg(pc);
 
700
                        break;
 
701
                }
 
702
        }
 
703
 
 
704
        /* Sanity check on options */
 
705
        if (!profdb) {
 
706
                default_path = talloc_asprintf(mem_ctx, DEFAULT_DIR, getenv("HOME"));
 
707
                error = mkdir(default_path, 0700);
 
708
                talloc_free(default_path);
 
709
                if ((error == -1) && (errno != EEXIST)) {
 
710
                        perror("mkdir");
 
711
                        retcode = EXIT_FAILURE;
 
712
                        goto cleanup;
 
713
                }
 
714
                profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB, 
 
715
                                         getenv("HOME"));
 
716
        }
 
717
 
 
718
        if ((list == false) && (getfqdn == false) && (newdb == false) && (listlangs == false)
 
719
            && (getdflt == false) && (dump == false) && (rename == NULL) && 
 
720
            (!attribute) && (!profname || !profdb)) {
 
721
                poptPrintUsage(pc, stderr, 0);
 
722
                retcode = EXIT_FAILURE;
 
723
                goto cleanup;
 
724
        }
 
725
 
 
726
        if (newdb == true) {
 
727
                if (!ldif) {
 
728
                        ldif = talloc_strdup(mem_ctx, mapi_profile_get_ldif_path());
 
729
                }
 
730
                if (!ldif) show_help(pc, "ldif");
 
731
                if (!mapiprofile_createdb(profdb, ldif)) {
 
732
                        retcode = EXIT_FAILURE;
 
733
                        goto cleanup;
 
734
                }
 
735
        }
 
736
 
 
737
        if (MAPIInitialize(&mapi_ctx, profdb) != MAPI_E_SUCCESS) {
 
738
                mapi_errstr("MAPIInitialize", GetLastError());
 
739
                exit (1);
 
740
        }
 
741
 
 
742
        if (opt_krb) {
 
743
                if (strncmp(opt_krb, "yes", 3) && strncmp(opt_krb, "no", 2)) {
 
744
                        fprintf(stderr,
 
745
                                "kerberos value must be 'yes' or 'no'\n");
 
746
                        poptPrintUsage(pc, stderr, 0);
 
747
                        retcode = EXIT_FAILURE;
 
748
                        goto cleanup;
 
749
                }
 
750
        }
 
751
 
 
752
        /* Process the code here */
 
753
 
 
754
        if (!workstation) {
 
755
                gethostname(hostname, sizeof(hostname) - 1);
 
756
                hostname[sizeof(hostname) - 1] = 0;
 
757
                workstation = hostname;
 
758
        }
 
759
 
 
760
        if (create == true) {
 
761
                if (!profname) show_help(pc, "profile");
 
762
                if (!password && !nopass) show_help(pc, "password");
 
763
                if (!username) show_help(pc, "username");
 
764
                if (!address) show_help(pc, "address");
 
765
 
 
766
                if (!version) {
 
767
                        version = talloc_strdup(mem_ctx, DEFAULT_EXCHANGE_VERSION);
 
768
                }
 
769
 
 
770
                for (i = 0; exchange_version[i].name; i++) {
 
771
                        if (!strcasecmp(version, exchange_version[i].name)) {
 
772
                                version = talloc_strdup(mem_ctx, exchange_version[i].name);
 
773
                                found = true;
 
774
                                break;
 
775
                        }
 
776
                }
 
777
                if (found == false) {
 
778
                        printf("Invalid Exchange server version. Possible values are:\n");
 
779
                        for (i = 0; exchange_version[i].name; i++) {
 
780
                                printf("\t[*] %s\n", exchange_version[i].name);
 
781
                        }
 
782
                        goto cleanup;
 
783
                }
 
784
 
 
785
                /* Force encrypt parameter if exchange2010 is specified */
 
786
                if (!strcasecmp(version, "2010")) {
 
787
                        opt_seal = true;
 
788
                }
 
789
                talloc_free((void *)version);
 
790
 
 
791
                if (! mapiprofile_create(mapi_ctx, profdb, profname, pattern, username, password, address,
 
792
                                         language, workstation, domain, realm, nopass, opt_seal, 
 
793
                                         opt_dumpdata, opt_debuglevel,
 
794
                                         exchange_version[i].version,
 
795
                                         opt_krb)) {
 
796
                        retcode = EXIT_FAILURE;
 
797
                        goto cleanup;
 
798
                }
 
799
        }
 
800
 
 
801
        if (rename) {
 
802
                mapiprofile_rename(mapi_ctx, profdb, profname, rename);
 
803
        }
 
804
 
 
805
        if (getfqdn == true) {
 
806
                mapiprofile_get_fqdn(mapi_ctx, profdb, profname, password, opt_dumpdata);
 
807
        }
 
808
 
 
809
        if (listlangs == true) {
 
810
                mapidump_languages_list();
 
811
        }
 
812
 
 
813
        if (setdflt == true) {
 
814
                mapiprofile_set_default(mapi_ctx, profdb, profname);
 
815
        }
 
816
 
 
817
        if (getdflt == true) {
 
818
                mapiprofile_get_default(mapi_ctx, profdb);
 
819
        }
 
820
 
 
821
        if (delete == true) {
 
822
                mapiprofile_delete(mapi_ctx, profdb, profname);
 
823
        }
 
824
 
 
825
        if (list == true) {
 
826
                mapiprofile_list(mapi_ctx, profdb);
 
827
        }
 
828
 
 
829
        if (dump == true) {
 
830
                mapiprofile_dump(mapi_ctx, profdb, profname);
 
831
        }
 
832
 
 
833
        if (attribute) {
 
834
                mapiprofile_attribute(mapi_ctx, profdb, profname, attribute);
 
835
        }
 
836
 
 
837
cleanup:
 
838
        free((void*)opt_debuglevel);
 
839
        free((void*)profname);
 
840
        free((void*)address);
 
841
        free((void*)domain);
 
842
        free((void*)realm);
 
843
        free((void*)username);
 
844
        free((void*)pattern);
 
845
        free((void*)password);
 
846
        free((void*)rename);
 
847
        free((void*)attribute);
 
848
 
 
849
        if (mapi_ctx)
 
850
                MAPIUninitialize(mapi_ctx);
 
851
        poptFreeContext(pc);
 
852
        talloc_free(mem_ctx);
 
853
 
 
854
        return retcode;
 
855
}