comparison src/pst2ldif.cpp @ 16:c508ee15dfca

switch to automake/autoconf
author carl
date Sun, 19 Feb 2006 18:47:46 -0800
parents
children d64a9df0dc02
comparison
equal deleted inserted replaced
15:ac98f448b6ab 16:c508ee15dfca
1 /*
2
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
4 the GPL version 2 or any later version at your choice available at
5 http://www.fsf.org/licenses/gpl.txt
6
7 Based on readpst.c by David Smith
8
9 */
10
11 using namespace std;
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <time.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <limits.h>
19 #include <errno.h>
20
21 #ifndef _WIN32
22 # include <unistd.h>
23 # include <sys/stat.h> //mkdir
24
25 // for reading of directory and clearing in function mk_separate_dir
26 # include <sys/types.h>
27 # include <dirent.h>
28 #else
29 # include <direct.h>
30 # define chdir _chdir
31 # define int32_t __int32
32 #endif
33
34 #ifndef __GNUC__
35 # include "XGetopt.h"
36 #endif
37
38 // needed for std c++ collections
39 #include <set>
40
41 extern "C" {
42 #include "libstrfunc.h" // for base64_encoding
43 #include "define.h"
44 #include "libpst.h"
45 #include "common.h"
46 #include "timeconv.h"
47 #include "lzfu.h"
48 }
49
50 int32_t usage();
51 int32_t version();
52 char *my_stristr(char *haystack, char *needle);
53 char *check_filename(char *fname);
54 char *single(char *str);
55 char *folded(char *str);
56 void multi(char *fmt, char *str);
57 char *rfc2426_escape(char *str);
58 int32_t chr_count(char *str, char x);
59
60 char *prog_name;
61 pst_file pstfile;
62 char *ldap_base = NULL; // 'o=some.domain.tld, c=US'
63 char *ldap_class = NULL; // 'newPerson'
64 char *ldap_org = NULL; // 'o=some.domain.tld', computed from ldap_base
65
66
67 ////////////////////////////////////////////////
68 // define our ordering
69 struct ltstr {
70 bool operator()(char* s1, char* s2) const {
71 return strcasecmp(s1, s2) < 0;
72 }
73 };
74 // define our set
75 typedef set<char *, ltstr> string_set;
76 // make a static set to hold the cn values
77 static string_set all_strings;
78
79
80 ////////////////////////////////////////////////
81 // helper to register a string in a string set
82 //
83 static char* register_string(string_set &s, char *name);
84 static char* register_string(string_set &s, char *name) {
85 string_set::iterator i = s.find(name);
86 if (i != s.end()) return *i;
87 char *x = strdup(name);
88 s.insert(x);
89 return x;
90 }
91
92 ////////////////////////////////////////////////
93 // register a global string
94 //
95 static char* register_string(char *name);
96 static char* register_string(char *name) {
97 return register_string(all_strings, name);
98 }
99
100
101 ////////////////////////////////////////////////
102 // make a unique string
103 //
104 static char* unique_string(char *name);
105 static char* unique_string(char *name) {
106 int unique = 2;
107 string_set::iterator i = all_strings.find(name);
108 if (i == all_strings.end()) return register_string(name);
109 while (true) {
110 char n[strlen(name)+10];
111 snprintf(n, sizeof(n), "%s %d", name, unique++);
112 string_set::iterator i = all_strings.find(n);
113 if (i == all_strings.end()) return register_string(n);
114 }
115 }
116
117
118 ////////////////////////////////////////////////
119 // remove leading and trailing blanks
120 //
121 static char *trim(char *name);
122 static char *trim(char *name) {
123 char *p;
124 while (*name == ' ') name++;
125 p = name + strlen(name) - 1;
126 while ((p >= name) && (*p == ' ')) *p-- = '\0';
127 return name;
128 }
129
130
131 static void process(pst_desc_ll *d_ptr);
132 static void process(pst_desc_ll *d_ptr) {
133 pst_item *item = NULL;
134 while (d_ptr) {
135 if (d_ptr->desc) {
136 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr);
137 DEBUG_INFO(("item pointer is %p\n", item));
138 if (item) {
139 if (item->message_store) {
140 // there should only be one message_store, and we have already done it
141 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
142 }
143
144 if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) {
145 //if this is a non-empty folder other than deleted items, we want to recurse into it
146 fprintf(stderr, "entering folder %s\n", item->file_as);
147 process(d_ptr->child);
148 } else if (item->contact) {
149 // deal with a contact
150 if (item->type != PST_TYPE_CONTACT) {
151 DIE(("type should be contact\n"));
152 }
153 else if (item->contact == NULL) { // this is an incorrect situation. Inform user
154 DIE(("null item contact\n"));
155 } else {
156 char cn[1000];
157 snprintf(cn, sizeof(cn), "%s %s %s %s",
158 single(item->contact->display_name_prefix),
159 single(item->contact->first_name),
160 single(item->contact->surname),
161 single(item->contact->suffix));
162 if (strcmp(cn, " ")) {
163 // fprintf(stderr, "\n\n\n");
164 // fprintf(stderr, "access_method %s\n", item->contact->access_method);
165 // fprintf(stderr, "account_name %s\n", item->contact->account_name);
166 // fprintf(stderr, "address1 %s\n", item->contact->address1);
167 // fprintf(stderr, "address1_desc %s\n", item->contact->address1_desc);
168 // fprintf(stderr, "address1_transport %s\n", item->contact->address1_transport);
169 // fprintf(stderr, "address2 %s\n", item->contact->address2);
170 // fprintf(stderr, "address2_desc %s\n", item->contact->address2_desc);
171 // fprintf(stderr, "address2_transport %s\n", item->contact->address2_transport);
172 // fprintf(stderr, "address3 %s\n", item->contact->address3);
173 // fprintf(stderr, "address3_desc %s\n", item->contact->address3_desc);
174 // fprintf(stderr, "address3_transport %s\n", item->contact->address3_transport);
175 // fprintf(stderr, "assistant_name %s\n", item->contact->assistant_name);
176 // fprintf(stderr, "assistant_phone %s\n", item->contact->assistant_phone);
177 // fprintf(stderr, "billing_information %s\n", item->contact->billing_information);
178 // fprintf(stderr, "business_address %s\n", item->contact->business_address);
179 // fprintf(stderr, "business_city %s\n", item->contact->business_city);
180 // fprintf(stderr, "business_country %s\n", item->contact->business_country);
181 // fprintf(stderr, "business_fax %s\n", item->contact->business_fax);
182 // fprintf(stderr, "business_homepage %s\n", item->contact->business_homepage);
183 // fprintf(stderr, "business_phone %s\n", item->contact->business_phone);
184 // fprintf(stderr, "business_phone2 %s\n", item->contact->business_phone2);
185 // fprintf(stderr, "business_po_box %s\n", item->contact->business_po_box);
186 // fprintf(stderr, "business_postal_code %s\n", item->contact->business_postal_code);
187 // fprintf(stderr, "business_state %s\n", item->contact->business_state);
188 // fprintf(stderr, "business_street %s\n", item->contact->business_street);
189 // fprintf(stderr, "callback_phone %s\n", item->contact->callback_phone);
190 // fprintf(stderr, "car_phone %s\n", item->contact->car_phone);
191 // fprintf(stderr, "company_main_phone %s\n", item->contact->company_main_phone);
192 // fprintf(stderr, "company_name %s\n", item->contact->company_name);
193 // fprintf(stderr, "computer_name %s\n", item->contact->computer_name);
194 // fprintf(stderr, "customer_id %s\n", item->contact->customer_id);
195 // fprintf(stderr, "def_postal_address %s\n", item->contact->def_postal_address);
196 // fprintf(stderr, "department %s\n", item->contact->department);
197 // fprintf(stderr, "display_name_prefix %s\n", item->contact->display_name_prefix);
198 // fprintf(stderr, "first_name %s\n", item->contact->first_name);
199 // fprintf(stderr, "followup %s\n", item->contact->followup);
200 // fprintf(stderr, "free_busy_address %s\n", item->contact->free_busy_address);
201 // fprintf(stderr, "ftp_site %s\n", item->contact->ftp_site);
202 // fprintf(stderr, "fullname %s\n", item->contact->fullname);
203 // fprintf(stderr, "gov_id %s\n", item->contact->gov_id);
204 // fprintf(stderr, "hobbies %s\n", item->contact->hobbies);
205 // fprintf(stderr, "home_address %s\n", item->contact->home_address);
206 // fprintf(stderr, "home_city %s\n", item->contact->home_city);
207 // fprintf(stderr, "home_country %s\n", item->contact->home_country);
208 // fprintf(stderr, "home_fax %s\n", item->contact->home_fax);
209 // fprintf(stderr, "home_phone %s\n", item->contact->home_phone);
210 // fprintf(stderr, "home_phone2 %s\n", item->contact->home_phone2);
211 // fprintf(stderr, "home_po_box %s\n", item->contact->home_po_box);
212 // fprintf(stderr, "home_postal_code %s\n", item->contact->home_postal_code);
213 // fprintf(stderr, "home_state %s\n", item->contact->home_state);
214 // fprintf(stderr, "home_street %s\n", item->contact->home_street);
215 // fprintf(stderr, "initials %s\n", item->contact->initials);
216 // fprintf(stderr, "isdn_phone %s\n", item->contact->isdn_phone);
217 // fprintf(stderr, "job_title %s\n", item->contact->job_title);
218 // fprintf(stderr, "keyword %s\n", item->contact->keyword);
219 // fprintf(stderr, "language %s\n", item->contact->language);
220 // fprintf(stderr, "location %s\n", item->contact->location);
221 // fprintf(stderr, "manager_name %s\n", item->contact->manager_name);
222 // fprintf(stderr, "middle_name %s\n", item->contact->middle_name);
223 // fprintf(stderr, "mileage %s\n", item->contact->mileage);
224 // fprintf(stderr, "mobile_phone %s\n", item->contact->mobile_phone);
225 // fprintf(stderr, "nickname %s\n", item->contact->nickname);
226 // fprintf(stderr, "office_loc %s\n", item->contact->office_loc);
227 // fprintf(stderr, "org_id %s\n", item->contact->org_id);
228 // fprintf(stderr, "other_address %s\n", item->contact->other_address);
229 // fprintf(stderr, "other_city %s\n", item->contact->other_city);
230 // fprintf(stderr, "other_country %s\n", item->contact->other_country);
231 // fprintf(stderr, "other_phone %s\n", item->contact->other_phone);
232 // fprintf(stderr, "other_po_box %s\n", item->contact->other_po_box);
233 // fprintf(stderr, "other_postal_code %s\n", item->contact->other_postal_code);
234 // fprintf(stderr, "other_state %s\n", item->contact->other_state);
235 // fprintf(stderr, "other_street %s\n", item->contact->other_street);
236 // fprintf(stderr, "pager_phone %s\n", item->contact->pager_phone);
237 // fprintf(stderr, "personal_homepage %s\n", item->contact->personal_homepage);
238 // fprintf(stderr, "pref_name %s\n", item->contact->pref_name);
239 // fprintf(stderr, "primary_fax %s\n", item->contact->primary_fax);
240 // fprintf(stderr, "primary_phone %s\n", item->contact->primary_phone);
241 // fprintf(stderr, "profession %s\n", item->contact->profession);
242 // fprintf(stderr, "radio_phone %s\n", item->contact->radio_phone);
243 // fprintf(stderr, "spouse_name %s\n", item->contact->spouse_name);
244 // fprintf(stderr, "suffix %s\n", item->contact->suffix);
245 // fprintf(stderr, "surname %s\n", item->contact->surname);
246 // fprintf(stderr, "telex %s\n", item->contact->telex);
247 // fprintf(stderr, "transmittable_display_name %s\n", item->contact->transmittable_display_name);
248 // fprintf(stderr, "ttytdd_phone %s\n", item->contact->ttytdd_phone);
249 // have a valid cn
250 char *ucn = unique_string(folded(trim(cn)));
251 printf("dn: cn=%s, %s\n", ucn, ldap_base);
252 printf("cn: %s\n", ucn);
253 if (item->contact->first_name) {
254 snprintf(cn, sizeof(cn), "%s %s",
255 single(item->contact->display_name_prefix),
256 single(item->contact->first_name));
257 printf("givenName: %s\n", trim(cn));
258 }
259 if (item->contact->surname) {
260 snprintf(cn, sizeof(cn), "%s %s",
261 single(item->contact->surname),
262 single(item->contact->suffix));
263 printf("sn: %s\n", trim(cn));
264 }
265 else if (item->contact->company_name) {
266 printf("sn: %s\n", single(item->contact->company_name));
267 }
268 else
269 printf("sn: %s\n", ucn); // use cn as sn if we cannot find something better
270
271 if (item->contact->job_title)
272 printf("personalTitle: %s\n", single(item->contact->job_title));
273 if (item->contact->company_name)
274 printf("company: %s\n", single(item->contact->company_name));
275 if (item->contact->address1 && *item->contact->address1)
276 printf("mail: %s\n", single(item->contact->address1));
277 if (item->contact->address2 && *item->contact->address2)
278 printf("mail: %s\n", single(item->contact->address2));
279 if (item->contact->address3 && *item->contact->address3)
280 printf("mail: %s\n", single(item->contact->address3));
281 if (item->contact->address1a && *item->contact->address1a)
282 printf("mail: %s\n", single(item->contact->address1a));
283 if (item->contact->address2a && *item->contact->address2a)
284 printf("mail: %s\n", single(item->contact->address2a));
285 if (item->contact->address3a && *item->contact->address3a)
286 printf("mail: %s\n", single(item->contact->address3a));
287 if (item->contact->business_address) {
288 if (item->contact->business_po_box)
289 printf("postalAddress: %s\n", single(item->contact->business_po_box));
290 if (item->contact->business_street)
291 multi("postalAddress: %s\n", item->contact->business_street);
292 if (item->contact->business_city)
293 printf("l: %s\n", single(item->contact->business_city));
294 if (item->contact->business_state)
295 printf("st: %s\n", single(item->contact->business_state));
296 if (item->contact->business_postal_code)
297 printf("postalCode: %s\n", single(item->contact->business_postal_code));
298 }
299 else if (item->contact->home_address) {
300 if (item->contact->home_po_box)
301 printf("postalAddress: %s\n", single(item->contact->home_po_box));
302 if (item->contact->home_street)
303 multi("postalAddress: %s\n", item->contact->home_street);
304 if (item->contact->home_city)
305 printf("l: %s\n", single(item->contact->home_city));
306 if (item->contact->home_state)
307 printf("st: %s\n", single(item->contact->home_state));
308 if (item->contact->home_postal_code)
309 printf("postalCode: %s\n", single(item->contact->home_postal_code));
310 }
311 else if (item->contact->other_address) {
312 if (item->contact->other_po_box)
313 printf("postalAddress: %s\n", single(item->contact->other_po_box));
314 if (item->contact->other_street)
315 multi("postalAddress: %s\n", item->contact->other_street);
316 if (item->contact->other_city)
317 printf("l: %s\n", single(item->contact->other_city));
318 if (item->contact->other_state)
319 printf("st: %s\n", single(item->contact->other_state));
320 if (item->contact->other_postal_code)
321 printf("postalCode: %s\n", single(item->contact->other_postal_code));
322 }
323 if (item->contact->business_fax)
324 printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax));
325 else if (item->contact->home_fax)
326 printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax));
327
328 if (item->contact->business_phone)
329 printf("telephoneNumber: %s\n", single(item->contact->business_phone));
330 if (item->contact->home_phone)
331 printf("homePhone: %s\n", single(item->contact->home_phone));
332
333 if (item->contact->car_phone)
334 printf("mobile: %s\n", single(item->contact->car_phone));
335 else if (item->contact->mobile_phone)
336 printf("mobile: %s\n", single(item->contact->mobile_phone));
337 else if (item->contact->other_phone)
338 printf("mobile: %s\n", single(item->contact->other_phone));
339
340
341 if (item->comment)
342 printf("description: %s\n", single(item->comment));
343
344 printf("objectClass: %s\n\n", ldap_class);
345 }
346 }
347 }
348 else {
349 DEBUG_INFO(("item is not a contact\n"));
350 }
351 }
352 _pst_freeItem(item);
353 }
354 d_ptr = d_ptr->next;
355 }
356 }
357
358
359 int main(int argc, char** argv) {
360 pst_desc_ll *d_ptr;
361 char *fname = NULL;
362 char *temp = NULL; //temporary char pointer
363 char c;
364 prog_name = argv[0];
365 pst_item *item = NULL;
366
367 while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) {
368 switch (c) {
369 case 'b':
370 ldap_base = optarg;
371 temp = strchr(ldap_base, ',');
372 if (temp) {
373 *temp = '\0';
374 ldap_org = strdup(ldap_base+2); // assume first 2 chars are o=
375 *temp = ',';
376 }
377 break;
378 case 'c':
379 ldap_class = optarg;
380 break;
381 case 'h':
382 usage();
383 exit(0);
384 break;
385 case 'V':
386 version();
387 exit(0);
388 break;
389 default:
390 usage();
391 exit(1);
392 break;
393 }
394 }
395
396 if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) {
397 fname = argv[optind];
398 } else {
399 usage();
400 exit(2);
401 }
402
403 #ifdef DEBUG_ALL
404 DEBUG_INIT("pst2ldif.log");
405 DEBUG_REGISTER_CLOSE();
406 #endif
407 DEBUG_ENT("main");
408 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
409 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
410
411 pst_load_extended_attributes(&pstfile);
412
413 d_ptr = pstfile.d_head; // first record is main record
414 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr);
415 if (!item || !item->message_store) {
416 DEBUG_RET();
417 DIE(("main: Could not get root record\n"));
418 }
419
420 d_ptr = pst_getTopOfFolders(&pstfile, item);
421 if (!d_ptr) {
422 DEBUG_RET();
423 DIE(("Top of folders record not found. Cannot continue\n"));
424 }
425
426 _pst_freeItem(item);
427
428 // write the ldap header
429 printf("dn: %s\n", ldap_base);
430 printf("o: %s\n", ldap_org);
431 printf("objectClass: organization\n\n");
432 printf("dn: cn=root, %s\n", ldap_base);
433 printf("cn: root\n");
434 printf("objectClass: %s\n\n", ldap_class);
435
436 process(d_ptr->child); // do the children of TOPF
437 pst_close(&pstfile);
438 DEBUG_RET();
439 return 0;
440 }
441
442
443 int usage() {
444 version();
445 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
446 printf("OPTIONS:\n");
447 printf("\t-h\t- Help. This screen\n");
448 printf("\t-V\t- Version. Display program version\n");
449 printf("\t-b ldapbase\t- set the ldap base value\n");
450 printf("\t-c class \t- set the class of the ldap objects\n");
451 return 0;
452 }
453
454
455 int version() {
456 printf("pst2ldif v%s\n", VERSION);
457 #if BYTE_ORDER == BIG_ENDIAN
458 printf("Big Endian implementation being used.\n");
459 #elif BYTE_ORDER == LITTLE_ENDIAN
460 printf("Little Endian implementation being used.\n");
461 #else
462 # error "Byte order not supported by this library"
463 #endif
464 #ifdef __GNUC__
465 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
466 #endif
467 return 0;
468 }
469
470
471 // my_stristr varies from strstr in that its searches are case-insensitive
472 char * my_stristr(char *haystack, char *needle) {
473 char *x=haystack, *y=needle, *z = NULL;
474 if (haystack == NULL || needle == NULL)
475 return NULL;
476 while (*y != '\0' && *x != '\0') {
477 if (tolower(*y) == tolower(*x)) {
478 // move y on one
479 y++;
480 if (z == NULL) {
481 z = x; // store first position in haystack where a match is made
482 }
483 } else {
484 y = needle; // reset y to the beginning of the needle
485 z = NULL; // reset the haystack storage point
486 }
487 x++; // advance the search in the haystack
488 }
489 return z;
490 }
491
492
493 char *check_filename(char *fname) {
494 char *t = fname;
495 if (t == NULL) {
496 return fname;
497 }
498 while ((t = strpbrk(t, "/\\:"))) {
499 // while there are characters in the second string that we don't want
500 *t = '_'; //replace them with an underscore
501 }
502 return fname;
503 }
504
505
506 char *single(char *str) {
507 if (!str) return "";
508 char *ret = rfc2426_escape(str);
509 char *n = strchr(ret, '\n');
510 if (n) *n = '\0';
511 return ret;
512 }
513
514
515 char *folded(char *str) {
516 if (!str) return "";
517 char *ret = rfc2426_escape(str);
518 char *n = ret;
519 while (n = strchr(n, '\n')) {
520 *n = ' ';
521 }
522 n = ret;
523 while (n = strchr(n, ',')) {
524 *n = ' ';
525 }
526 return ret;
527 }
528
529
530 void multi(char *fmt, char *str) {
531 if (!str) return;
532 char *ret = rfc2426_escape(str);
533 char *n = ret;
534 while (n = strchr(ret, '\n')) {
535 *n = '\0';
536 printf(fmt, ret);
537 ret = n+1;
538 }
539 if (*ret) printf(fmt, ret);
540 }
541
542
543 char *rfc2426_escape(char *str) {
544 static char* buf = NULL;
545 char *ret, *a, *b;
546 int x = 0, y, z;
547 if (str == NULL)
548 ret = str;
549 else {
550
551 // calculate space required to escape all the following characters
552 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2));
553 z = chr_count(str, '\r');
554 if (y == 0 && z == 0)
555 // there isn't any extra space required
556 ret = str;
557 else {
558 buf = (char*) realloc(buf, x+1);
559 a = str;
560 b = buf;
561 while (*a != '\0') {
562 switch(*a) {
563 // case ',' :
564 case '\\':
565 case ';' :
566 // case '\n':
567 *(b++)='\\';
568 *b=*a;
569 break;
570 case '\r':
571 break;
572 default:
573 *b=*a;
574 }
575 b++;
576 a++;
577 }
578 *b = '\0';
579 ret = buf;
580 }
581 }
582 return ret;
583 }
584
585
586 int chr_count(char *str, char x) {
587 int r = 0;
588 while (*str != '\0') {
589 if (*str == x)
590 r++;
591 str++;
592 }
593 return r;
594 }
595