File: util.c

package info (click to toggle)
gnarwl 3.6.dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 856 kB
  • ctags: 288
  • sloc: ansic: 1,163; makefile: 119; sh: 70; php: 44
file content (218 lines) | stat: -rw-r--r-- 4,442 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>

#include <conf.h>

#ifdef HAVE_ICONV
#include <iconv.h>
#endif

#include "config.h"
#include "util.h"

#ifndef __LCLINT__
#include <sys/syslog.h>
#endif


extern int verbose;
extern struct conf cfg;

void expandVars(char** txt, char* s,char *r) {
  int x;
  int ns;
  int sl;
  char *tmp;
  char *tmp2=NULL;
  
  if (((*txt)==NULL) || (s==NULL)) return;
  if (r==NULL) r="";
  
  // Quickfix: If s is/contains a substring of r, bail out before going into
  // an infinite loop
  for(x=0;x<(int)strlen(r);x++) {
    if (!strcasecmp(r+x,s)) {
      return;
    }
  }
  
  cpyStr(&tmp2,*txt);
  
  sl=strlen(tmp2);
  for (x=0;x<sl;x++) {
    if (tmp2[x]==s[0]) {
      if (!strncasecmp(tmp2+x,s,strlen(s))) {
        ns=sl+strlen(r)-strlen(s)+1;
        tmp=(char*)calloc(ns,sizeof(char));
        if (tmp==NULL) oom();
        strncpy(tmp,tmp2,x);
        strcpy(tmp+x,r);
        strcpy(tmp+strlen(tmp),tmp2+x+strlen(s));
        free(tmp2);
        tmp2=(char*)calloc(strlen(tmp)+1,sizeof(char));
        if (tmp2==NULL) oom();
        strcpy(tmp2,tmp);
        sl=strlen(tmp2);
        free(tmp);
      }
    }
  }
  *txt=tmp2;
}


void oom() {
  syslog(LOG_MAIL+LOG_WARNING,"CRIT/SYS Out of memory");
  closelog();
  exit(EXIT_FAILURE);
}


char **splitString(const char* str, int idx, char delim) {
  int x=0;
  int y=0;
  int z=0;
  int len;
  char* tmp;
  char** ret;
  
  for(x=0;x<(int)strlen(str);x++) {
    while(str[x]==delim && x<(int)strlen(str)) x++;
    while(str[x]!=delim && x<(int)strlen(str)) x++;
    z++;
  }
  
  z++;
  ret=(char**)calloc((size_t)z,sizeof(char*));
  if (ret==NULL) oom();
  z=0;
  
  len=strlen(str);
  while(str[len-1]==delim) len--;
  
  for(x=0;x<len;x++) {
    while(str[x]==delim && x<len) x++;
    y=x;
    if(z==idx && idx>-1) x=len;
    while(str[x]!=delim && x<len) x++;
    
    tmp=(char*)calloc((size_t)x-y+3,sizeof(char));
    if (tmp==NULL) oom();
    memcpy(tmp,str+y*sizeof(char),(size_t)x-y);
    ret[z]=tmp;
    z++;
  }
  return ret;
}

char* readFile(char* fname) {
  
  int fd;
  struct stat fs;
  char *buf;
  
  fd=open(fname,O_RDONLY);
  if ( (fd==-1) && (verbose==LVL_WARN) ) {
    syslog(LOG_MAIL+LOG_INFO,"WARN/IO %s",fname);
    return NULL;
  }
  
  if (fstat(fd,&fs)==-1) {
    syslog(LOG_MAIL+LOG_INFO,"WARN/IO %s",fname);
    return NULL;
  }
  
  buf=(char*)calloc((size_t)fs.st_size+1,sizeof(char));
  
  if (buf==NULL) oom();
  if (fs.st_size!=read(fd,buf,(size_t)fs.st_size)) {
    syslog(LOG_MAIL+LOG_INFO,"WARN/IO %s",fname);
    return NULL;
  }
  return buf;
}

void cleanAddress(char** d) {
  int m=0;
  int r=-1;
  int l=-1;
  
  char* s=NULL;
  char** tmp;
  
  s=(char*)calloc(strlen(*d)+1,sizeof(char));
  if (s==NULL) oom();
  strcpy(s,*d);
  
  for(m=0;m<(int)strlen(s);m++)  if (s[m]=='<') { l=m+1; break; }
  for(m=strlen(s);m>=0;m--) if (s[m]=='>') { r=m-1; break; }
    
  for(m=0;m<(int)strlen(s);m++) {
    if (r>-1 && l>-1) {
      if (m<l || m>r) s[m]='*';
    }
    else {
      if (! ( ((s[m]>47)&&(s[m]<58)) || ((s[m]>63)&&(s[m]<91)) || ((s[m]>96)&&(s[m]<123)) || s[m]=='.' || s[m]=='-' || s[m]=='_' || s[m]=='+' || s[m]=='=') ) s[m]='*';
    }
  }
  
  tmp=splitString(s,1,'*');
  m=1;
  while (tmp[m]!=NULL) {free(tmp[m]); m++;}
  free(*d);
  *d=tmp[0];
  if (tmp[0]!=NULL)
    for(m=0;m<(int)strlen(tmp[0]);m++) tmp[0][m]=(char)tolower(tmp[0][m]);
}


void cpyStr(char** dest, const char* src) {
  if (*dest!=NULL) free(*dest);
  *dest=(char*)malloc((strlen(src)+1)*sizeof(char));
  if (*dest==NULL) oom();
  strcpy(*dest,src);
}

void translateString(char** txt) {
#ifdef HAVE_ICONV
  char* in=*txt;
  char* out;
  size_t inlen;
  size_t outlen;
  iconv_t conv;
  char* to;
#endif
    
  if (cfg.charset==NULL) return;

#ifdef HAVE_ICONV
  conv=iconv_open(cfg.charset, LDAP_CHARSET);
  if (conv != (iconv_t) -1) {
    
    
    inlen=strlen(in);
    outlen=inlen;
    to=(char*)calloc(inlen+1,sizeof(char));
    if (to==NULL) oom();
    out=to;
    
    while(inlen > 0 && outlen > 0) {
      if(iconv(conv, &in, &inlen, &out, &outlen) != 0) { in++; inlen--; }
    }
    iconv_close(conv);
    *out='\0';
    free(*txt);
    *txt=to;
  }
  else {
    syslog(LOG_MAIL|LOG_WARNING,"WARN/MAIL charset conversion failed (errno: %s)",strerror(errno));
  }
#endif
}