summaryrefslogtreecommitdiffstats
path: root/ldif_parse.c
blob: a7aa8eca333736f1ea8f9c35696279e3053590a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#define _FILE_OFFSET_BITS 64
#include <alloca.h>
#include <buffer.h>
#include <scan.h>
#include <open.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include "mduptab.h"
#include "mstorage.h"
#include <str.h>
#include "ldif.h"
#include "byte.h"
#include "textcode.h"
#include "stralloc.h"
#include "uint32.h"

mduptab_t attributes,classes;
mstorage_t stringtable;
uint32_t dn, objectClass;
unsigned long lines;

/* this is called after each record.
 * If it returns -1, ldif_parse will exit immediately.
 * If it returns 0, ldif_parse will continue parsing and overwrite the
 *   current ldaprec.
 * If it returns 1, ldif_parse will allocate a new ldaprec and link it
 *   using the next pointer in the current ldaprec.
 * If the callback is NULL, a callback that always returns 1 is assumed.
 * */
int (*ldif_parse_callback)(struct ldaprec* l);
uint32_t (*ldif_addstring_callback)(const char* s,unsigned long len);

unsigned long ldifrecords;

static void addattribute(struct ldaprec** l,uint32_t name,uint32_t val) {
  if (name==dn) (*l)->dn=val; else
    if ((*l)->n<ATTRIBS) {
      (*l)->a[(*l)->n].name=name;
      (*l)->a[(*l)->n].value=val;
      ++(*l)->n;
    } else {
      buffer_puts(buffer_2,"\r\n\nLDIF parse error: too many attributes!: ");
      buffer_puts(buffer_2,attributes.Strings->root+name);
      buffer_puts(buffer_2," in line ");
      buffer_putulong(buffer_2,lines);
      buffer_putnlflush(buffer_2);
      exit(1);
    }
}

static size_t unbase64(char* buf) {
  size_t destlen;
  char temp[8192];
  size_t l=scan_base64(buf,temp,&destlen);
  if (buf[l] && buf[l]!='\n') return 0;
  byte_copy(buf,destlen,temp);
  return destlen;
}

uint32_t (*ldif_addstring_callback)(const char* s,unsigned long len);

static uint32_t addstring(const char* s,unsigned long len) {
  return mstorage_add(&stringtable,s,len);
}

static long commit_string_bin(const char* s,unsigned long n) {
  unsigned int i;
  static char zero;
  uint32_t x;
  char intbuf[4];
  if (n==0 || (n==1 && s[0]==0)) goto encodebinary;
  for (i=0; i<n-1; ++i)
    if (!s[i]) {
encodebinary:
      uint32_pack(intbuf,n);
      if ((x=ldif_addstring_callback(&zero,1))==(uint32_t)-1 || ldif_addstring_callback(intbuf,4)==(uint32_t)-1 || ldif_addstring_callback(s,n)==(uint32_t)-1) return -1;
      return x;
    }
  x=ldif_addstring_callback(s,n);
  if (s[n-1])
    if (ldif_addstring_callback(&zero,1)==(uint32_t)-1) return -1;
  return x;
}


static inline int add_normalized(const char* s,long len) {
  char* newdn=alloca(len+1);
  long val;
  if ((val=ldif_addstring_callback(newdn,normalize_dn(newdn,s,len)))<0) return -1;
  return val;
}

static int parserec(buffer* b, struct ldaprec** l,const char* filename) {
  char buf[8192];
  int n,i,eof=0,ofs=0;
  unsigned int i2;
  size_t len,base64,binary;
  stralloc payload={0,0,0};

  if (!(*l=malloc(sizeof(struct ldaprec)))) {
nomem:
    buffer_putsflush(buffer_2,"\r\n\nout of memory!\n");
    return 1;
  }
  (*l)->dn=-1;
  (*l)->next=0; (*l)->n=0;
  ldifrecords=0;
  do {
    uint32_t tmp, val;
    base64=binary=0;
    buf[ofs]=0;
    n=ofs+buffer_get_token(b,buf+ofs,8192-ofs,":\n",2);
    if (n==ofs) {
      if (buf[ofs]==0) eof=1;
      break;
    }
    if (buf[0]=='#') {	/* comment line */
      while (n>=8192-ofs || buf[n]==':') {
	/* if we got a partial line or the comment contained a colon, do over */
	ofs=0;
	n=buffer_get_token(b,buf,8192,"\n",1);
      }
      ++lines;
      continue;
    }
    i=scan_whitenskip(buf,n);
    if (buf[byte_chr(buf+i,n-i,'\n')]=='\n') {
      buffer_putm(buffer_2,"\r\n\n",filename,":");
      buffer_putulong(buffer_2,lines+1);
      buffer_putsflush(buffer_2,": error: no key:value found\n");
      exit(1);
    }
    buf[n]=0;
    if ((i2=str_chr(buf,';'))<(unsigned int)n) {
      buf[i2]=0;
      if (str_equal("binary",buf+i2+1)) binary=1;
    }
    if ((tmp=mduptab_adds(&attributes,buf+i))==(uint32_t)-1) {
//      write(2,"a",1);
      goto nomem;
    } if (!stralloc_copys(&payload,"")) {
//      write(2,"b",1);
      goto nomem;
    }
    {
      char dummy;
      int res;
      /* read line, skipping initial whitespace */
      for (n=0; (res=buffer_getc(b,&dummy))==1; ) {
	if (dummy=='\n') { ++lines; break; }
	if (!n && dummy==':' && base64==0) { base64=1; continue; }
	if (!n && (dummy==' ' || dummy=='\t')) continue;
	if (!stralloc_append(&payload,&dummy)) {
//	  write(2,"c",1);
	  goto nomem;
	}
	++n;
      }
      if (res==-1) return 1;
    }

lookagain:
    {
      char c;
      switch (buffer_getc(b,&c)) {
      case 0: eof=1; break;
      case -1: buffer_putsflush(buffer_2,"read error!\n"); return 1;
      }
      if (c==' ') {	/* continuation */
//	puts("continuation!");
	n=buffer_get_token(b,buf,8192,"\n",1);
	if (n==-1) return 1;
	if (!stralloc_catb(&payload,buf,n)) {
//	  write(2,"d",1);
	  goto nomem;
	}
	goto lookagain;
      } else if (c=='\n') {
	struct ldaprec* m;

	++lines;

	if (payload.len) {
	  if (!stralloc_0(&payload)) {
//	    write(2,"e",1);
	    goto nomem;
	  }
	  if (base64) {
	    len=unbase64(payload.s);
	    if (len==0) {
	      buffer_putm(buffer_2,"\r\n\n",filename,":");
	      buffer_putulong(buffer_2,lines+1);
	      buffer_putsflush(buffer_2,": error: base64 decoding failed\n");
	      exit(1);
	    }
	    if (!binary) { payload.s[len]=0; ++len; }
	  } else {
	    size_t sl;
	    len=n;
	    sl=scan_ldapescape(payload.s,payload.s,&len);
	    if (sl!=payload.len-1) {
	      buffer_putm(buffer_2,"\r\n\n",filename,":");
	      buffer_putulong(buffer_2,lines+1);
	      buffer_putsflush(buffer_2,": error: LDIF de-escaping failed\n");
	      exit(1);
	    }
	    payload.s[len]=0;
	    ++len;
	  }
	} else
	  len=0;

#if 0
	buffer_puts(buffer_2,"feld \"");
	buffer_puts(buffer_2,attributes.Strings->root+tmp);
	buffer_puts(buffer_2,"\", wert \"");
	buffer_put(buffer_2,payload.s,len);
	buffer_putsflush(buffer_2,"\".\n");
#endif

	if (tmp==objectClass) {
	  if ((val=mduptab_add(&classes,payload.s,len-1))==(uint32_t)-1) {
//	    write(2,"f",1);
	    goto nomem;
	  }
	} else if (tmp==dn) {
	  if ((val=add_normalized(payload.s,len))==(uint32_t)-1) {
//	    write(2,"g",1);
	    goto nomem;
	  }
	} else
	  if ((val=commit_string_bin(payload.s,len))==(uint32_t)-1) {
//	    write(2,"h",1);
	    goto nomem;
	  }
	addattribute(l,tmp,val);

	m=0;
	if (ldif_parse_callback) {
	  switch (ldif_parse_callback(*l)) {
	  case -1:
	    return -1;
	  case 0:
	    m=*l;
	    break;
#if 0
	  case 1:
	    m=0;
	    break;
#endif
	  }
	}
	if (!m) if (!(m=malloc(sizeof(struct ldaprec)))) return 2;

	(*l)->next=m;
	m->n=0; m->dn=-1; m->next=0;
	ofs=0;
//	dumprec(*l);
	if (*l!=m) l=&((*l)->next);
	++ldifrecords;
	continue;
      } else {
	ofs=1;
	buf[0]=c;
      }
    }
//    buf[n]=0;
#if 1

    if (payload.len) {
      if (!stralloc_0(&payload)) {
//	write(2,"i",1);
	goto nomem;
      }
      if (base64) {
	len=unbase64(payload.s);
	if (len==0) {
	  buffer_putm(buffer_2,"\r\n\n",filename,":");
	  buffer_putulong(buffer_2,lines+1);
	  buffer_putsflush(buffer_2,": error: base64 decoding failed\n");
	  exit(1);
	}
	if (!binary) { payload.s[len]=0; ++len; }
      } else {
	len=n;
	scan_ldapescape(payload.s,payload.s,&len);
	payload.s[len]=0;
	++len;
      }
    } else
      len=0;

#if 0
	buffer_puts(buffer_2,"feld \"");
	buffer_puts(buffer_2,attributes.Strings->root+tmp);
	buffer_puts(buffer_2,"\", wert \"");
	buffer_put(buffer_2,payload.s,len);
	buffer_putsflush(buffer_2,"\".\n");
#endif

    if (tmp==objectClass) {
      if ((val=mduptab_add(&classes,payload.s,len-1))==(uint32_t)-1) {
//	write(2,"j",1);
	goto nomem;
      }
    } else if (tmp==dn) {
      if ((val=add_normalized(payload.s,payload.len))==(uint32_t)-1) {
//	write(2,"k",1);
	goto nomem;
      }
    } else
      if ((val=commit_string_bin(payload.s,len))==(uint32_t)-1) {
//	write(2,"l",1);
	goto nomem;
      }
    addattribute(l,tmp,val);
#endif
  } while (!eof);
  if (!eof) {
    buffer_putm(buffer_2,"\r\n\n",filename,":");
    buffer_putulong(buffer_2,lines+1);
    buffer_putsflush(buffer_2,": error: parse error (maybe 2nd empty line?)\n");
    exit(1);
  }
  if ((*l)->dn==(uint32_t)-1) return 0;
  if (ldif_parse_callback && ldif_parse_callback(*l)==-1) return -1;
  if ((*l)->dn==(uint32_t)-1 && ((*l)->next)) {
    struct ldaprec* m=(*l)->next;
    free((*l));
    (*l)=m;
  }
  return 0;
}

struct ldaprec *first=0;

int ldif_parse(const char* filename,off_t fromofs,struct stat* ss) {
  char buf[4096];
  int fd;
  buffer in;
  buffer* tmp;
  mstorage_init(&stringtable);
  if (ldif_addstring_callback==0) ldif_addstring_callback=addstring;
  if (filename[0]=='-' && !filename[1]) {
    tmp=buffer_0;
    fd=-1;
  } else {
    fd=open_read(filename);
    if (fd<0) return 0;		// no journal file is permissible
    if (fromofs) lseek(fd,fromofs,SEEK_SET);
    buffer_init(&in,(void*)read,fd,buf,sizeof buf);
    tmp=&in;
  }
  dn=mduptab_adds(&attributes,"dn");
  objectClass=mduptab_adds(&attributes,"objectClass");
  lines=0;
  {
    int res=parserec(tmp,&first,filename);
    if (ss) {
      fstat(fd,ss);
      /* the file size may have changed between parserec hitting EOF and
       * us calling lstat, we we write the current file pointer position
       * to st_size */
      ss->st_size=lseek(fd,0,SEEK_CUR);
    }
    if (fd!=-1) close(fd);
    return res;
  }
}