/*
ldapdiff
Copyright (C) 2000-2006 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 <sys/types.h>
#include "ldapdiff.h"
#include "base64.h"
static void ldifwriteval(FILE *f,char *val,size_t val_len,teattrtype val_type){
char *val_temp = NULL;
size_t len_temp = 0;
int freeval = 0;
int left;
int n;
if(f == NULL || val == NULL || val_len == 0){
ldiflog(LOG0,"warning ldifwriteval() unexpected parameter: file: %s, line: %d",__FILE__,__LINE__);
}
switch(val_type){
case ATTRUNKNOWN: /* in doubt, write binary */
case ATTRBIN:
val_temp = LDALLOC(1,val_len * 4 + 1);
if((len_temp = b64_ntop((u_char*)val,val_len,val_temp,val_len * 4 + 1)) == -1){
ldiflog(LOG0,"b64_ntop failed: file: %s, line: %d",__FILE__,__LINE__);
exit(EXITLDERROR);
}
freeval = 1;
fprintf(f,": ");
break;
case ATTRASC:
val_temp = val;
len_temp = val_len;
freeval = 0;
fprintf(f," ");
break;
default:
ldiflog(LOG0,"error unknown attribute type detected: file: %s, line: %d",__FILE__,__LINE__);
exit(EXITLDERROR);
break;
}
left = len_temp;
while(left > 0){
if(left < len_temp){
fprintf(f," ");
}
if(left - MAXLDIFLINELEN > 0){
n = MAXLDIFLINELEN;
}
else{
n = left;
}
fwrite(val_temp+len_temp-left,sizeof(char),n,f);
fprintf(f,"\n");
left -= n;
}
if(freeval != 0){
free(val_temp);
}
}
void ldifwrite(struct s_mod *smod,struct s_schema *sschema)
{
FILE *fmod;
FILE *fadd;
struct s_mod *psmod;
struct s_modentry *psmodentry;
struct s_ldifval *psmodentryval;
psmod = smod;
if((fmod = fopen(ldifgetgconf(CONFMODFILE),"w")) == NULL){
ldiflog(LOG0,"fopen() of %s failed: file: %s, line: %d",ldifgetgconf(CONFMODFILE),__FILE__,__LINE__);
exit(EXITLDERROR);
}
if((fadd = fopen(ldifgetgconf(CONFADDFILE),"w")) == NULL){
ldiflog(LOG0,"fopen() of %s failed: file: %s, line: %d",ldifgetgconf(CONFADDFILE),__FILE__,__LINE__);
exit(EXITLDERROR);
}
while(psmod != NULL){
switch(psmod->modtype){
case MODADD:
fprintf(fadd,"%s: %s\n",psmod->dnvar,psmod->dnval);
psmodentry = psmod->attrlist;
while(psmodentry != NULL){
psmodentryval = psmodentry->vallist;
while(psmodentryval != NULL){
fprintf(fadd,"%s:",psmodentry->var);
ldifwriteval(fadd,psmodentryval->val,psmodentryval->val_len,psmodentry->val_type);
psmodentryval = psmodentryval->next;
}
psmodentry = psmodentry->next;
}
fprintf(fadd,"\n");
break;
case MODMODIFY:
fprintf(fmod,"%s: %s\n",psmod->dnvar,psmod->dnval);
fprintf(fmod,"changetype: modify\n");
psmodentry = psmod->attrlist;
while(psmodentry != NULL){
switch(psmodentry->modtype){
case MODATTRADD:
fprintf(fmod,"add: %s\n",psmodentry->var);
psmodentryval = psmodentry->vallist;
while(psmodentryval != NULL){
fprintf(fmod,"%s:",psmodentry->var);
ldifwriteval(fmod,psmodentryval->val,psmodentryval->val_len,psmodentry->val_type);
psmodentryval = psmodentryval->next;
}
fprintf(fmod,"-\n");
break;
case MODATTRREPLACE:
fprintf(fmod,"replace: %s\n",psmodentry->var);
psmodentryval = psmodentry->vallist;
while(psmodentryval != NULL){
fprintf(fmod,"%s:",psmodentry->var);
ldifwriteval(fmod,psmodentryval->val,psmodentryval->val_len,psmodentry->val_type);
psmodentryval = psmodentryval->next;
}
fprintf(fmod,"-\n");
break;
case MODATTRDELETE:
fprintf(fmod,"delete: %s\n",psmodentry->var);
if(ldifschemaequal(sschema,psmodentry->var) == 1){
psmodentryval = psmodentry->vallist;
while(psmodentryval != NULL){
/*
Tod Thomas told me, that it is possible with iplanet (sunone) ldapserver,
that val_len == 0. to avoid problems with this situation, an if statement has
been placed here.
Thanks Tod
*/
if(psmodentryval->val_len > 0){
fprintf(fmod,"%s:",psmodentry->var);
ldifwriteval(fmod,psmodentryval->val,psmodentryval->val_len,psmodentry->val_type);
}
psmodentryval = psmodentryval->next;
}
}
fprintf(fmod,"-\n");
break;
}
psmodentry = psmodentry->next;
}
fprintf(fmod,"\n");
break;
case MODDELETE:
fprintf(fmod,"%s: %s\n",psmod->dnvar,psmod->dnval);
fprintf(fmod,"changetype: delete\n");
fprintf(fmod,"\n");
break;
}
psmod = psmod->next;
}
fclose(fadd);
fclose(fmod);
}
syntax highlighted by Code2HTML, v. 0.9.1