annotate pst2ldif.cpp @ 11:bf12a9d4524c

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