File: ldapread.c

package info (click to toggle)
ldapdiff 0.9.2-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 716 kB
  • ctags: 182
  • sloc: ansic: 2,392; sh: 768; makefile: 11
file content (159 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (2)
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
/*
    ldapdiff
    Copyright (C) 2000-2002 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 2 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#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            cerrs  = 0;
 int            evalid = 0;

 if(filename != NULL){
  if((f = fopen(filename,"r")) == NULL){
   fprintf(stderr,"fopen() of %s failed: file: %s, line: %d\n",filename,__FILE__,__LINE__);
   exit(-1);
  }
 }
 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){
   long int filepos;

   filepos = ftell(f);
   while(fgets(folderline,sizeof(folderline),f) != NULL){
    /* strip \n */
    if((pstrip = strchr(folderline,'\n')) != NULL){
     *pstrip = '\0';
    }
    /* strip \r\n */
    if((pstrip = strstr(folderline,"\r\n")) != NULL){
     *pstrip = '\0';
    }
    if(folderline[0] == ' ' || folderline[0] == '\t'){
     clines++;
     if(strlen(line) + strlen(folderline+1) > MAXATTRLEN){
      fprintf(stderr,"attribute size to big file:%s line:%d\n",__FILE__,__LINE__);
      exit(-1);
     }
     pline = realloc(pline,strlen(pline)+1 + strlen(folderline));
     strcat(pline,folderline+1);
     filepos = ftell(f);
    }
    else{
     break;
    }
   }
   fseek(f,filepos,SEEK_SET); 

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

   if(strcmp(var,DNNAME) == 0){
    if(ldifcheckbase(val) != 0){
     ldiflog(LOG0,"ldifread: warning, entrybase outside searchbase in line %d:",clines);
     ldiflog(LOG0,"ldifread: [%s]",pline);
     cerrs++;
     evalid = 0;
    }
    else{
     centrs++;
     psldif = ldifadd(sldif,var,val);
     evalid = 1;
    }
   }
   else{
    if(evalid == 1){
     cattrs++;
     ldifaddentry(psldif,var,val,val_len,val_type);
    }
    else{
     ldiflog(LOG0,"ldifread: warning, unexpected attribute in line %d:",clines);
     ldiflog(LOG0,"ldifread: [%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 errors     proceesed",cerrs);

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