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
|
/*
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 <errno.h>
#include <sys/types.h>
#include "ldapdiff.h"
#include "base64.h"
static int ldifparsebase64(char* s,char **val,size_t *val_len)
{
*val = LDALLOC(strlen(s) * 4,sizeof(char));
if((*val_len = b64_pton(s,(u_char*)*val,strlen(s) * 4)) == -1){
fprintf(stderr,"b64_pton() failed: file: %s, line: %d\n",__FILE__,__LINE__);
return -1;
}
return 0;
}
static int ldifparseurl(char* s,char **val,size_t *val_len)
{
if(strncasecmp("file://",s,strlen("file://")) == 0){
FILE *f;
char *fname;
char *buf[1024];
size_t bcount;
size_t bsum;
fname = s+strlen("file://");
if((f = fopen(fname,"r")) == NULL){
fprintf(stderr,"fopen() of %s failed: file: %s, line: %d\n",fname,__FILE__,__LINE__);
return -1;
}
bsum = 0;
*val = NULL;
while((bcount = fread(buf,sizeof(char),1024,f)) > 0){
if((*val = realloc(*val,sizeof(char)*(bsum+bcount))) == NULL){
fprintf(stderr,"realloc() failed file:%s line:%d %s\n",__FILE__,__LINE__,strerror(errno));
exit(-1);
}
memcpy(*val+bsum,buf,bcount);
bsum += bcount;
}
ldiflog(LOG2,"ldifread: file [%s] with %d bytes read",fname,bsum);
fclose(f);
*val_len = bsum;
return 0;
}
else{
fprintf(stderr,"ldifparseurl() failed (\"file://\") file: %s line: %d\n",__FILE__,__LINE__);
return -1;
}
}
int ldifparse(char *s,char **var,char **val,size_t *val_len,teattrtype *val_type)
{
char *valtmp;
if((valtmp = strchr(s,':')) == NULL){
return -1;
}
*var = LDALLOC(valtmp - s + 1,sizeof(char));
strncpy(*var,s,valtmp - s);
(*var)[valtmp-s] = '\0';
/*
not rfc2849 conform, but useful
*/
ldiftrim(*var);
if(strlen(*var) == 0){
return -1;
}
/* base64 encoded ldif attribute detected */
if(strncmp(valtmp+1,": ",2) == 0){
ldiflog(LOG2,"ldifread: base64 attribute detected attribute: %s",*var);
*val_type = ATTRBIN;
return ldifparsebase64(valtmp+3,val,val_len);
}
/* attributed with file:/// url detected */
if(strncmp(valtmp+1,"< ",2) == 0){
ldiflog(LOG2,"ldifread: url attribute detected attribute: %s",*var);
*val_type = ATTRBIN;
return ldifparseurl(valtmp+3,val,val_len);
}
*val = LDALLOC(strlen(valtmp+2)*2,sizeof(char));
if(strcmp(ldifgetgconf(CONFICONV),"yes") == 0){
char *itmp;
char *otmp;
itmp = valtmp+2;
otmp = *val;
if(ldificonv(&itmp,&otmp) == -1){
return -1;
}
}
else{
sprintf(*val,"%s",valtmp+2);
}
*val_len = strlen(*val);
*val_type = ATTRASC;
if(strlen(*val) == 0){
return -1;
}
return 0;
}
|