comparison pst2ldif.c @ 2:8dd68d722fa8

add ldif converters
author carl
date Sun, 11 Jul 2004 13:31:02 -0700
parents
children 28737b9480e4
comparison
equal deleted inserted replaced
0:6b1b602514db 2:8dd68d722fa8
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 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <limits.h>
17 #include <errno.h>
18
19 #ifndef _WIN32
20 # include <unistd.h>
21 # include <sys/stat.h> //mkdir
22
23 // for reading of directory and clearing in function mk_separate_dir
24 # include <sys/types.h>
25 # include <dirent.h>
26 #else
27 # include <direct.h>
28 # define chdir _chdir
29 # define int32_t __int32
30 #endif
31
32 #ifndef __GNUC__
33 # include "XGetopt.h"
34 #endif
35
36 #include "libstrfunc.h" // for base64_encoding
37
38 #include "define.h"
39 #include "libpst.h"
40 #include "common.h"
41 #include "timeconv.h"
42 #include "lzfu.h"
43
44 #define OUTPUT_TEMPLATE "%s"
45
46 #define VERSION "0.1"
47 // max size of the c_time char*. It will store the date of the email
48 #define C_TIME_SIZE 500
49 #define PERM_DIRS 0777
50
51 // macro used for creating directories
52 #ifndef WIN32
53 #define D_MKDIR(x) mkdir(x, PERM_DIRS)
54 #else
55 #define D_MKDIR(x) mkdir(x)
56 #endif
57
58 int32_t usage();
59 int32_t version();
60 char *my_stristr(char *haystack, char *needle);
61 char *check_filename(char *fname);
62 char *single(char *str);
63 char *multi(char *fmt, char *str);
64 char *rfc2426_escape(char *str);
65 int32_t chr_count(char *str, char x);
66
67 char *prog_name;
68 pst_file pstfile;
69 char *ldap_base = NULL; // 'o=some.domain.tld, c=US'
70 char *ldap_class = NULL; // 'newPerson'
71 char *ldap_org = NULL; // 'o=some.domain.tld', computed from ldap_base
72
73
74 char *trim(char *name);
75 char *trim(char *name) {
76 char *p;
77 while (*name == ' ') name++;
78 p = name + strlen(name) - 1;
79 while ((p >= name) && (*p == ' ')) *p-- = '\0';
80 return name;
81 }
82
83
84 void process(pst_desc_ll *d_ptr);
85 void process(pst_desc_ll *d_ptr) {
86 pst_item *item = NULL;
87 while (d_ptr) {
88 if (d_ptr->desc) {
89 item = _pst_parse_item(&pstfile, d_ptr);
90 if (item) {
91 if (item->message_store) {
92 // there should only be one message_store, and we have already done it
93 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
94 }
95
96 if (item->folder) { //if this is a folder, we want to recurse into it
97 process(d_ptr->child);
98 } else if (item->contact) {
99 // deal with a contact
100 if (item->type != PST_TYPE_CONTACT) {
101 DIE(("type should be contact\n"));
102 }
103 else if (item->contact == NULL) { // this is an incorrect situation. Inform user
104 DIE(("null item contact\n"));
105 } else {
106 char cn[1000];
107 snprintf(cn, sizeof(cn), "%s %s %s %s",
108 single(item->contact->display_name_prefix),
109 single(item->contact->first_name),
110 single(item->contact->surname),
111 single(item->contact->suffix));
112 if (strcmp(cn, " ")) {
113 // have a valid cn
114 printf("dn: cn=%s, %s\n", trim(cn), ldap_base);
115 printf("cn: %s\n", trim(cn));
116 if (item->contact->first_name) {
117 snprintf(cn, sizeof(cn), "%s %s",
118 single(item->contact->display_name_prefix),
119 single(item->contact->first_name));
120 printf("givenName: %s\n", trim(cn));
121 }
122 if (item->contact->surname) {
123 snprintf(cn, sizeof(cn), "%s %s",
124 single(item->contact->surname),
125 single(item->contact->suffix));
126 printf("sn: %s\n", trim(cn));
127 }
128 if (item->contact->job_title)
129 printf("personalTitle: %s\n", single(item->contact->job_title));
130 if (item->contact->company_name)
131 printf("company: %s\n", single(item->contact->company_name));
132 if (item->contact->address1)
133 printf("mail: %s\n", single(item->contact->address1));
134 if (item->contact->address2)
135 printf("mail: %s\n", single(item->contact->address2));
136 if (item->contact->address3)
137 printf("mail: %s\n", single(item->contact->address3));
138 if (item->contact->business_address) {
139 if (item->contact->business_po_box)
140 printf("postalAddress: %s\n", single(item->contact->business_po_box));
141 if (item->contact->business_street)
142 multi("postalAddress: %s\n", item->contact->business_street);
143 if (item->contact->business_city)
144 printf("l: %s\n", single(item->contact->business_city));
145 if (item->contact->business_state)
146 printf("st: %s\n", single(item->contact->business_state));
147 if (item->contact->business_postal_code)
148 printf("postalCode: %s\n", single(item->contact->business_postal_code));
149 }
150 else if (item->contact->home_address) {
151 if (item->contact->home_po_box)
152 printf("postalAddress: %s\n", single(item->contact->home_po_box));
153 if (item->contact->home_street)
154 multi("postalAddress: %s\n", item->contact->home_street);
155 if (item->contact->home_city)
156 printf("l: %s\n", single(item->contact->home_city));
157 if (item->contact->home_state)
158 printf("st: %s\n", single(item->contact->home_state));
159 if (item->contact->home_postal_code)
160 printf("postalCode: %s\n", single(item->contact->home_postal_code));
161 }
162 else if (item->contact->other_address) {
163 if (item->contact->other_po_box)
164 printf("postalAddress: %s\n", single(item->contact->other_po_box));
165 if (item->contact->other_street)
166 multi("postalAddress: %s\n", item->contact->other_street);
167 if (item->contact->other_city)
168 printf("l: %s\n", single(item->contact->other_city));
169 if (item->contact->other_state)
170 printf("st: %s\n", single(item->contact->other_state));
171 if (item->contact->other_postal_code)
172 printf("postalCode: %s\n", single(item->contact->other_postal_code));
173 }
174 if (item->contact->business_fax)
175 printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax));
176 else if (item->contact->home_fax)
177 printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax));
178
179 if (item->contact->business_phone)
180 printf("telephoneNumber: %s\n", single(item->contact->business_phone));
181 if (item->contact->home_phone)
182 printf("homePhone: %s\n", single(item->contact->home_phone));
183
184 if (item->contact->car_phone)
185 printf("mobile: %s\n", single(item->contact->car_phone));
186 else if (item->contact->mobile_phone)
187 printf("mobile: %s\n", single(item->contact->mobile_phone));
188 else if (item->contact->other_phone)
189 printf("mobile: %s\n", single(item->contact->other_phone));
190
191
192 if (item->comment)
193 printf("description: %s\n", single(item->comment));
194
195 printf("objectClass: %s\n\n", ldap_class);
196 }
197 }
198 }
199 }
200 _pst_freeItem(item);
201 }
202 d_ptr = d_ptr->next;
203 }
204 }
205
206
207 int main(int argc, char** argv) {
208 pst_desc_ll *d_ptr;
209 char *fname = NULL;
210 char *temp = NULL; //temporary char pointer
211 char c;
212 prog_name = argv[0];
213 pst_item *item = NULL;
214
215 while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) {
216 switch (c) {
217 case 'b':
218 ldap_base = optarg;
219 temp = strchr(ldap_base, ',');
220 if (temp) {
221 *temp = '\0';
222 ldap_org = strdup(ldap_base);
223 *temp = ',';
224 }
225 break;
226 case 'c':
227 ldap_class = optarg;
228 break;
229 case 'h':
230 usage();
231 exit(0);
232 break;
233 case 'V':
234 version();
235 exit(0);
236 break;
237 default:
238 usage();
239 exit(1);
240 break;
241 }
242 }
243
244 if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) {
245 fname = argv[optind];
246 } else {
247 usage();
248 exit(2);
249 }
250
251 DEBUG_INIT("pst2ldif.log");
252 DEBUG_REGISTER_CLOSE();
253 DEBUG_ENT("main");
254 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
255 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
256
257 pst_load_extended_attributes(&pstfile);
258
259 d_ptr = pstfile.d_head; // first record is main record
260 item = _pst_parse_item(&pstfile, d_ptr);
261 if (!item || !item->message_store) {
262 DIE(("main: Could not get root record\n"));
263 }
264
265 d_ptr = pst_getTopOfFolders(&pstfile, item);
266 if (!d_ptr) {
267 DIE(("Top of folders record not found. Cannot continue\n"));
268 }
269
270 _pst_freeItem(item);
271
272 // write the ldap header
273 printf("dn: %s\n", ldap_base);
274 printf("o: %s\n", ldap_org);
275 printf("objectClass: organization\n\n");
276 printf("dn: cn=root, %s\n", ldap_base);
277 printf("cn: root\n");
278 printf("objectClass: %s\n\n", ldap_class);
279
280 process(d_ptr->child); // do the children of TOPF
281 pst_close(&pstfile);
282 return 0;
283 }
284
285
286 int usage() {
287 version();
288 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
289 printf("OPTIONS:\n");
290 printf("\t-h\t- Help. This screen\n");
291 printf("\t-V\t- Version. Display program version\n");
292 printf("\t-b ldapbase\t- set the ldap base value\n");
293 printf("\t-b ldapbase\t- set the ldap base value\n");
294 return 0;
295 }
296
297
298 int version() {
299 printf("pst2ldif v%s using LibPST v%s\n", VERSION, PST_VERSION);
300 #if BYTE_ORDER == BIG_ENDIAN
301 printf("Big Endian implementation being used.\n");
302 #elif BYTE_ORDER == LITTLE_ENDIAN
303 printf("Little Endian implementation being used.\n");
304 #else
305 # error "Byte order not supported by this library"
306 #endif
307 #ifdef __GNUC__
308 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
309 #endif
310 return 0;
311 }
312
313
314 // my_stristr varies from strstr in that its searches are case-insensitive
315 char * my_stristr(char *haystack, char *needle) {
316 char *x=haystack, *y=needle, *z = NULL;
317 if (haystack == NULL || needle == NULL)
318 return NULL;
319 while (*y != '\0' && *x != '\0') {
320 if (tolower(*y) == tolower(*x)) {
321 // move y on one
322 y++;
323 if (z == NULL) {
324 z = x; // store first position in haystack where a match is made
325 }
326 } else {
327 y = needle; // reset y to the beginning of the needle
328 z = NULL; // reset the haystack storage point
329 }
330 x++; // advance the search in the haystack
331 }
332 return z;
333 }
334
335
336 char *check_filename(char *fname) {
337 char *t = fname;
338 if (t == NULL) {
339 return fname;
340 }
341 while ((t = strpbrk(t, "/\\:"))) {
342 // while there are characters in the second string that we don't want
343 *t = '_'; //replace them with an underscore
344 }
345 return fname;
346 }
347
348
349 char *single(char *str) {
350 if (!str) return "";
351 char *ret = rfc2426_escape(str);
352 char *n = strchr(ret, '\n');
353 if (n) *n = '\0';
354 return ret;
355 }
356
357
358 char *multi(char *fmt, char *str) {
359 if (!str) return "";
360 char *ret = rfc2426_escape(str);
361 char *n = ret;
362 while (n = strchr(ret, '\n')) {
363 *n = '\0';
364 printf(fmt, ret);
365 ret = n+1;
366 }
367 if (*ret) printf(fmt, ret);
368 }
369
370
371 char *rfc2426_escape(char *str) {
372 static char* buf = NULL;
373 char *ret, *a, *b;
374 int x = 0, y, z;
375 if (str == NULL)
376 ret = str;
377 else {
378
379 // calculate space required to escape all the following characters
380 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2));
381 z = chr_count(str, '\r');
382 if (y == 0 && z == 0)
383 // there isn't any extra space required
384 ret = str;
385 else {
386 buf = (char*) realloc(buf, x+1);
387 a = str;
388 b = buf;
389 while (*a != '\0') {
390 switch(*a) {
391 // case ',' :
392 case '\\':
393 case ';' :
394 // case '\n':
395 *(b++)='\\';
396 *b=*a;
397 break;
398 case '\r':
399 break;
400 default:
401 *b=*a;
402 }
403 b++;
404 a++;
405 }
406 *b = '\0';
407 ret = buf;
408 }
409 }
410 return ret;
411 }
412
413
414 int chr_count(char *str, char x) {
415 int r = 0;
416 while (*str != '\0') {
417 if (*str == x)
418 r++;
419 str++;
420 }
421 return r;
422 }
423