1 /*
2     ldapdiff
3     Copyright (C) 2000-2008 Thomas.Reith@rhoen.de
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <lber.h>
20 #include <ldap.h>
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <syslog.h>
28 #include <ctype.h>
29 
30 #include "ldapdiff.h"
31 
ldiflogstdout(char * msg)32 static void ldiflogstdout(char *msg)
33 {
34  time_t       t;
35 
36  t = time(NULL);
37  printf("%s: %s\n",strtok(ctime(&t),"\n"),msg);
38 }
39 
ldiflogfile(char * msg)40 static void ldiflogfile(char *msg)
41 {
42  extern char *logfile;
43  FILE        *f;
44  time_t       t;
45 
46  if((f = fopen(logfile,"a")) == NULL){
47   ldiflog(LOG0,"fopen() of %s failed: file: %s, line: %d",logfile,__FILE__,__LINE__
48 );
49   exit(EXITLDERROR);
50  }
51 
52  t = time(NULL);
53  fprintf(f,"%s: %s\n",strtok(ctime(&t),"\n"),msg);
54 
55  fclose(f);
56 }
57 
ldiflogsyslog(int aloglevel,char * msg)58 static void ldiflogsyslog(int aloglevel, char *msg)
59 {
60  extern int facility;
61  int        level = LOG_ERR;
62 
63  openlog(SYSLOGNAME,LOG_ODELAY,facility);
64  switch(aloglevel){
65   case LOG0:  level = LOG_ERR;
66        break;
67   case LOG1:  level = LOG_INFO;
68        break;
69   case LOG2 : level = LOG_DEBUG;
70        break;
71  }
72  syslog(level,msg);
73  closelog();
74 }
75 
ldiflogval(teloglevel aloglevel,char * s,char * var,char * val,size_t val_len)76 void ldiflogval(teloglevel aloglevel,char *s,char* var,char *val,size_t val_len)
77 {
78  char *tval;
79 
80  tval = LDALLOC(1,val_len + 1);
81  if(val != NULL){
82   memcpy(tval,val,val_len);
83  }
84  ldiflog(aloglevel,s,var,tval);
85  free(tval);
86 }
87 
ldiflog(teloglevel aloglevel,char * s,...)88 void ldiflog(teloglevel aloglevel,char *s,...)
89 {
90  extern int   facility;
91  extern int   loglevel;
92  extern char *logfile;
93  int          i = 0;
94  int          j = 0;
95 
96  char buf[MAXATTRLEN];
97  char pbuf[MAXATTRLEN];
98 
99  va_list ap;
100 
101  if(aloglevel > loglevel){
102   return;
103  }
104 
105  va_start(ap,s);
106  vsprintf(buf,s,ap);
107  va_end(ap);
108 
109  for(i=0;i<strlen(buf);i++){
110   if(isalnum(buf[i]) || ispunct(buf[i]) || isspace(buf[i])){
111    pbuf[j++] = buf[i];
112   }
113   else{
114    sprintf(pbuf+j,"[%3.3o]",(unsigned char)buf[i]);
115    j += 5;
116   }
117 
118   if(j >= MAXLOGLEN){
119    sprintf(pbuf+j,"[...]");
120    j += 5;
121    break;
122   }
123  }
124  pbuf[j] = '\0';
125 
126  if(facility != -1){
127   ldiflogsyslog(aloglevel,pbuf);
128  }
129  else if(logfile != NULL){
130   ldiflogfile(pbuf);
131  }
132  else{
133   ldiflogstdout(pbuf);
134  }
135 }
136