File: ldapiter.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 (191 lines) | stat: -rw-r--r-- 6,118 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
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
/*
    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 <stdlib.h>
#include <string.h>

#include "ldapdiff.h"

void ldifiterldif(LDAP *ld,struct s_ldif *sldif,struct s_mod **smod,struct s_schema *sschema)
{
 int             cn;
 char           *dn;
 char            filter[MAXATTRLEN];
 LDAPMessage    *lr          = NULL;
 LDAPMessage    *lm          = NULL;
 BerElement     *berp; 
 struct s_ldif  *psldif      = NULL;
 struct s_ldif  *psldap      = NULL;
 struct berval **pberval     = NULL;

 psldif = sldif;
 while(psldif != NULL){
  int                  i;               /* generic counter                */
  int                  rc;
  char                *attr;            /* ldap attribute                 */

  /* find filter criteria */
  if(ldifgetfilter(psldif->attrlist,filter) == -1){
   ldiflog(LOG1,"warning: ignoring entry [%s]",psldif->dnval);
   ldiflog(LOG1,"         filter attribute [%s] doesn't exist",ldifgetpconf(CONFFILTER));
   psldif = psldif->next;
   continue;
  }

  if((rc = ldap_search_s(ld,ldifgetpconf(CONFBASEDN),LDAP_SCOPE_SUBTREE,filter,NULL,0,&lr)) == -1){
   fprintf(stderr,"ldap_search() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
   exit(-1);
  }

  if((cn = ldap_count_entries(ld,lr)) == -1){
   fprintf(stderr,"ldap_count_entries() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
   fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
   exit(-1);
  }
  if(cn == 0){
   ldifadddn(psldif,smod);
   if(ldap_msgfree(lr) == -1){
    fprintf(stderr,"ldap_msgfree() failed: file: %s, line: %d\n",__FILE__,__LINE__);
    exit(-1);
   }
   psldif = psldif->next;
   continue;
  }

  /* if cn > 1, the filter's match is not unique */
  if(cn > 1){
   fprintf(stderr,"filter [%s] with %d matches file: %s, line: %d\n",filter,cn,__FILE__,__LINE__);
   exit(-1);
  }

  if((lm = ldap_first_entry(ld,lr)) == NULL){
   fprintf(stderr,"ldap_first_entry() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
   fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
   exit(-1);
  }

  if((attr = ldap_first_attribute(ld,lm,&berp)) == NULL){
   fprintf(stderr,"ldap_first_attribute() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
   fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
   exit(-1);
  }

  if((dn = ldap_get_dn(ld,lm)) == NULL){
   fprintf(stderr,"ldap_get_dn() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
   fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
   exit(-1);
  }

  ldifadd(&psldap,DNNAME,dn);

  while(attr != NULL){
   if((pberval = ldap_get_values_len(ld,lm,attr)) != NULL){ 
    for(i=0;i<ldap_count_values_len(pberval);i++){ 
     ldifaddentry(psldap,attr,pberval[i]->bv_val,pberval[i]->bv_len,ATTRUNKNOWN);
    }
    ldap_value_free_len(pberval);
   }
   ldap_memfree(attr);
   attr = ldap_next_attribute(ld,lm,berp);
  }

  /* check ldif (sldif) versus ldap (sldap) and write ldap.diff */
  ldifcmp(psldif,psldap,smod,sschema);

  ldap_memfree(dn);
  ber_free(berp,0);

  ldiffree(&psldap);

  if(ldap_msgfree(lr) == -1){
   fprintf(stderr,"ldap_msgfree() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   exit(-1);
  }

  psldif = psldif->next;
 } 
}

void ldifiterldap(LDAP *ld,struct s_ldif *sldif,struct s_mod **smod,struct s_schema *sschema)
{
 int            cn;
 int            rc;
 char          *dn;
 LDAPMessage   *lr          = NULL;
 LDAPMessage   *lm          = NULL;
 struct s_ldif *psldif      = NULL;

 if((rc = ldap_search_s(ld,ldifgetpconf(CONFBASEDN),LDAP_SCOPE_SUBTREE,ldifgetpconf(CONFGROUP),NULL,0,&lr)) == -1){
  fprintf(stderr,"ldap_search() failed: file: %s, line: %d\n",__FILE__,__LINE__);
  fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
  exit(-1);
 }

 if((cn = ldap_count_entries(ld,lr)) == -1){
  fprintf(stderr,"ldap_count_entries() failed: file: %s, line: %d\n",__FILE__,__LINE__);
  ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
  fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
  exit(-1);
 }
 if(cn > 0){
  ldiflog(LOG2,"searching for ldap entries");
  ldiflog(LOG2,"%d object(s) found with filter [%s]",cn,ldifgetpconf(CONFGROUP));
  if((lm = ldap_first_entry(ld,lr)) == NULL){
   fprintf(stderr,"ldap_first_entry() failed: file: %s, line: %d\n",__FILE__,__LINE__);
   ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
   fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
   exit(-1);
  }
  while(lm != NULL){
   int found;
   if((dn = ldap_get_dn(ld,lm)) == NULL){
    fprintf(stderr,"ldap_get_dn() failed: file: %s, line: %d\n",__FILE__,__LINE__);
    ldap_get_option(ld,LDAP_OPT_ERROR_NUMBER,&rc);
    fprintf(stderr,"ldap_err2string(): %s",ldap_err2string(rc));
    exit(-1);
   }

   found = 0;
   psldif = sldif;
   while(psldif != NULL){
    if(strcmp(psldif->dnval,dn) == 0){
     found++;
    }
    psldif = psldif->next;
   }
   if(found == 0){
    ldifdeletedn(DNNAME,dn,smod);
   }
   lm = ldap_next_entry(ld,lm);
  }
 }
 if(ldap_msgfree(lr) == -1){
  fprintf(stderr,"ldap_msgfree() failed: file: %s, line: %d\n",__FILE__,__LINE__);
  exit(-1);
 }
}