masqmail

diff src/log.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/log.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,211 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 1999-2001 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +#include "masqmail.h"
    1.23 +
    1.24 +#include "sysexits.h"
    1.25 +
    1.26 +static char *_sysexit_strings[] = {
    1.27 +  "command line usage error",
    1.28 +  "data format error",
    1.29 +  "cannot open input",
    1.30 +  "addressee unknown",
    1.31 +  "host name unknown",
    1.32 +  "service unavailable",
    1.33 +  "internal software error",
    1.34 +  "system error (e.g., can't fork)",
    1.35 +  "critical OS file missing",
    1.36 +  "can't create (user) output file",
    1.37 +  "input/output error",
    1.38 +  "temp failure; user is invited to retry",
    1.39 +  "remote error in protocol",
    1.40 +  "permission denied",
    1.41 +  "configuration error"
    1.42 +};
    1.43 +
    1.44 +gchar *ext_strerror(int err)
    1.45 +{
    1.46 +  if(err < 1024)
    1.47 +    return strerror(err);
    1.48 +  else
    1.49 +    if(err > 1024 + EX__BASE &&
    1.50 +       (err - 1024 - EX__BASE < sizeof(_sysexit_strings)/sizeof(_sysexit_strings[0])))
    1.51 +      return _sysexit_strings[err - 1024 - EX__BASE];
    1.52 +
    1.53 +  return "unknown error";
    1.54 +}
    1.55 +
    1.56 +static FILE *logfile = NULL;
    1.57 +static FILE *debugfile = NULL;
    1.58 +
    1.59 +gboolean logopen()
    1.60 +{
    1.61 +  gchar *filename;
    1.62 +  mode_t saved_mode = umask(066);
    1.63 +
    1.64 +  if(conf.use_syslog){
    1.65 +    openlog(PACKAGE, LOG_PID, LOG_MAIL);
    1.66 +  }else{
    1.67 +    uid_t saved_uid;
    1.68 +    gid_t saved_gid;
    1.69 +    
    1.70 +    saved_gid = setegid(conf.mail_gid);
    1.71 +    saved_uid = seteuid(conf.mail_uid);
    1.72 +
    1.73 +    filename = g_strdup_printf("%s/masqmail.log", conf.log_dir);
    1.74 +    logfile = fopen(filename, "a");
    1.75 +    if(!logfile){
    1.76 +      fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno));
    1.77 +      return FALSE;
    1.78 +    }
    1.79 +    g_free(filename);
    1.80 +
    1.81 +    seteuid(saved_uid);
    1.82 +    setegid(saved_gid);
    1.83 +  }
    1.84 +
    1.85 +#ifdef ENABLE_DEBUG
    1.86 +  if(conf.debug_level > 0){
    1.87 +    filename = g_strdup_printf("%s/debug.log", conf.log_dir);
    1.88 +    debugfile = fopen(filename, "a");
    1.89 +    if(!debugfile){
    1.90 +      fprintf(stderr, "could not open debug log '%s'\n", filename);
    1.91 +      return FALSE;
    1.92 +    }
    1.93 +    g_free(filename);
    1.94 +  }
    1.95 +#endif
    1.96 +  umask(saved_mode);
    1.97 +  return TRUE;
    1.98 +}
    1.99 +
   1.100 +void logclose()
   1.101 +{
   1.102 +  if(conf.use_syslog)
   1.103 +    closelog();
   1.104 +  else
   1.105 +    if(logfile) fclose(logfile);
   1.106 +  if(debugfile) fclose(debugfile);
   1.107 +}
   1.108 +
   1.109 +void vlogwrite(int pri, const char *fmt, va_list args)
   1.110 +{
   1.111 +  if((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) || (pri == LOG_WARNING)){
   1.112 +    va_list args_copy;
   1.113 +    va_copy(args_copy, args);
   1.114 +    vfprintf(stdout, fmt, args_copy);
   1.115 +    va_end(args_copy);
   1.116 +    fflush(stdout); /* is this necessary? */
   1.117 +  }
   1.118 +
   1.119 +  pri &= ~LOG_VERBOSE;
   1.120 +  if(pri){
   1.121 +
   1.122 +    if(conf.use_syslog)
   1.123 +      vsyslog(pri, fmt, args);
   1.124 +    else{
   1.125 +      if(pri <= conf.log_max_pri){
   1.126 +	FILE *file = logfile ? logfile : stderr;
   1.127 +	time_t now = time(NULL);
   1.128 +	struct tm *t = localtime(&now);
   1.129 +	gchar buf[24];
   1.130 +	uid_t saved_uid;
   1.131 +	gid_t saved_gid;
   1.132 +
   1.133 +	saved_gid = setegid(conf.mail_gid);
   1.134 +	saved_uid = seteuid(conf.mail_uid);
   1.135 +
   1.136 +	strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
   1.137 +	fprintf(file, "%s [%d] ", buf, getpid());
   1.138 +	
   1.139 +	vfprintf(file, fmt, args);
   1.140 +	fflush(file);
   1.141 +
   1.142 +	seteuid(saved_uid);
   1.143 +	setegid(saved_gid);
   1.144 +      }
   1.145 +    }
   1.146 +  }
   1.147 +}  
   1.148 +
   1.149 +#ifdef ENABLE_DEBUG
   1.150 +void vdebugwrite(int pri, const char *fmt, va_list args)
   1.151 +{
   1.152 +  time_t now = time(NULL);
   1.153 +  struct tm *t = localtime(&now);
   1.154 +  gchar buf[24];
   1.155 +  strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
   1.156 +
   1.157 +  if(debugfile){
   1.158 +    fprintf(debugfile, "%s [%d] ", buf, getpid());
   1.159 +  
   1.160 +    vfprintf(debugfile, fmt, args);
   1.161 +    fflush(debugfile);
   1.162 +  }else{
   1.163 +    fprintf(stderr, "no debug file, msg was:\n");
   1.164 +    vfprintf(stderr, fmt, args);
   1.165 +  }
   1.166 +}
   1.167 +#endif
   1.168 +
   1.169 +void logwrite(int pri, const char *fmt, ...)
   1.170 +{
   1.171 +  va_list args, args_copy;
   1.172 +  int saved_errno = errno; /* somewhere this is changed to EBADF */
   1.173 +
   1.174 +  va_start(args, fmt);
   1.175 +#ifdef ENABLE_DEBUG
   1.176 +  va_copy(args_copy, args);
   1.177 +#endif
   1.178 +  vlogwrite(pri, fmt, args);
   1.179 +#ifdef ENABLE_DEBUG
   1.180 +  if(debugfile)
   1.181 +    vdebugwrite(pri, fmt, args_copy);
   1.182 +  va_end(args_copy);
   1.183 +#endif
   1.184 +  va_end(args);
   1.185 +
   1.186 +  errno = saved_errno;
   1.187 +}
   1.188 +
   1.189 +#ifdef ENABLE_DEBUG
   1.190 +void debugf(const char *fmt, ...)
   1.191 +{
   1.192 +  va_list args;
   1.193 +  va_start(args, fmt);
   1.194 +
   1.195 +  vdebugwrite(LOG_DEBUG, fmt, args);
   1.196 +
   1.197 +  va_end(args);
   1.198 +}
   1.199 +
   1.200 +void vdebugf(const char *fmt, va_list args)
   1.201 +{
   1.202 +  vdebugwrite(LOG_DEBUG, fmt, args);
   1.203 +}
   1.204 +#endif
   1.205 +
   1.206 +void maillog(const char *fmt, ...)
   1.207 +{
   1.208 +  va_list args;
   1.209 +  va_start(args, fmt);
   1.210 +
   1.211 +  vlogwrite(LOG_NOTICE, fmt, args);
   1.212 +
   1.213 +  va_end(args);
   1.214 +}