annotate pst2ldif.c @ 5:3e9cea0af4a1

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