~thomas-reith/ldapdiff/main

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
/*
    ldapdiff
    Copyright (C) 2000-2008 Thomas.Reith@rhoen.de

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <lber.h>
#include <ldap.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ldapdiff.h"

void ldifread(struct s_ldif **sldif,char *filename)
{
 FILE          *f;
 char           line[MAXATTRLEN];
 char           folderline[MAXATTRLEN];
 char          *pline;
 char          *pstrip;
 char          *var = NULL;
 char          *val = NULL;
 size_t         val_len;
 teattrtype     val_type;
 struct s_ldif *psldif;
 int            clines = 0;
 int            centrs = 0;
 int            cattrs = 0;
 int            cempty = 0;
 int            cerrs  = 0;
 int            evalid = 0;

 if(filename != NULL){
  if((f = fopen(filename,"r")) == NULL){
   ldiflog(LOG0,"fopen() of %s failed: file: %s, line: %d",filename,__FILE__,__LINE__);
   exit(EXITLDERROR);
  }
 }
 else{
  f = stdin;
 }

 if(strcmp(ldifgetgconf(CONFICONV),"yes") == 0){
  ldificonvopen();
 }

 psldif = *sldif;
 while(fgets(line,sizeof(line),f) != NULL){
  clines++;

  pline = LDDUP(line);
  /* 
  rfc2849: '#', are valid for comment in the first column only 
  */
  /* strip comment */
  if(*pline == '#'){
    *pline = '\0';
  }
  /* strip \n */
  if((pstrip = strchr(pline,'\n')) != NULL){
   *pstrip = '\0';
  }
  /* strip \r\n */
  if((pstrip = strstr(pline,"\r\n")) != NULL){
   *pstrip = '\0';
  }

  if(strlen(pline) != 0){

   for(;;){
    char nextch = getc(f);

    ungetc(nextch, f);
    if(nextch != ' ' && nextch != '\t'){
     break;
    }
    if(fgets(folderline,sizeof(folderline),f) == NULL){
     break;
    }
    if((pstrip = strchr(folderline,'\n')) != NULL){
     *pstrip = '\0';
    }
    /* strip \r\n */
    if((pstrip = strstr(folderline,"\r\n")) != NULL){
     *pstrip = '\0';
    }
    clines++;
    if(strlen(line) + strlen(folderline+1) > MAXATTRLEN){
     ldiflog(LOG0,"error: attribute size too big: file:%s line:%d",__FILE__,__LINE__);
     exit(EXITLDERROR);
    }
    pline = realloc(pline,strlen(pline)+1 + strlen(folderline));
    strcat(pline,folderline+1);
   }

   if(ldifparse(pline,&var,&val,&val_len,&val_type) == -1){
    ldiflog(LOG0,"read: parse error in line %d: [%s]",clines,pline);
    exit(EXITLDERROR);
   }

   if(val_type == ATTREMPTY){
    ldiflog(LOG2,"read: warning, empty attribute ignored in line %d:",clines);
    cempty++;
   }
   else{
    if(strcasecmp(var,DNNAME) == 0){
     char *nval;

     nval = ldifnormalizedn(val);
     if(ldifcheckbase(nval) != 0){
      ldiflog(LOG0,"read: warning: unknown basedn ignored %d:",clines);
      ldiflog(LOG0,"read: [%s]",pline);
      cerrs++;
      evalid = 0;
     }
     else{
      centrs++;
      psldif = ldifadd(sldif,var,nval);
      evalid = 1;
     }
     free(nval);
    }
    else{
     if(evalid == 1){
      cattrs++;
      ldifaddentry(psldif,var,val,val_len,val_type);
     }
     else{
      ldiflog(LOG0,"fread: warning, unexpected attribute in line %d:",clines);
      ldiflog(LOG0,"read: [%s]",pline);
      cerrs++;
     }
    }
    /* we don't need var and val anymore*/
    free(var);
    free(val);
   }
  }
  free(pline);
 }

 if(strcmp(ldifgetgconf(CONFICONV),"yes") == 0){
  ldificonvclose();
 }

 ldiflog(LOG1,"read:   %10d lines      processed",clines);
 ldiflog(LOG1,"read:   %10d entries    processed",centrs);
 ldiflog(LOG1,"read:   %10d attributes processed",cattrs);
 ldiflog(LOG1,"read:   %10d empty attr processed",cempty);
 ldiflog(LOG1,"read:   %10d errors     proceesed",cerrs);

 if(filename != NULL){
  fclose(f);
 }
}