comparison pst2ldif.cpp @ 9:8c724896a28a

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