Commit e103487a authored by venaas's avatar venaas Committed by venaas

added logging to file and syslog (need to add support for specifying facility)

git-svn-id: https://svn.testnett.uninett.no/radsecproxy/trunk@72 e88ac4ed-0b26-0410-9574-a7f39faa03bf
parent f32f6b2e
...@@ -10,22 +10,101 @@ ...@@ -10,22 +10,101 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <syslog.h>
#include <errno.h>
#include "debug.h" #include "debug.h"
static char *debug_ident = NULL;
static uint8_t debug_level = DBG_WARN; static uint8_t debug_level = DBG_WARN;
static FILE *debug_file = NULL;
static int debug_syslogfacility = 0;
void debug_init(char *ident) {
debug_file = stderr;
setvbuf(debug_file, NULL, _IONBF, 0);
debug_ident = ident;
}
void debug_set_level(uint8_t level) { void debug_set_level(uint8_t level) {
debug_level = level; debug_level = level;
} }
uint8_t debug_get_level() {
return debug_level;
}
int debug_set_destination(char *dest) {
extern int errno;
if (!strncasecmp(dest, "file:///", 8)) {
debug_file = fopen(dest + 7, "a");
if (!debug_file)
debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
dest + 7, strerror(errno));
setvbuf(debug_file, NULL, _IONBF, 0);
return 1;
}
if (!strcasecmp(dest, "x-syslog://")) {
debug_syslogfacility = LOG_DAEMON;
openlog(debug_ident, LOG_PID, debug_syslogfacility);
return 1;
}
return 0;
}
void debug_logit(uint8_t level, const char *format, va_list ap) {
struct timeval now;
char *timebuf;
int priority;
if (debug_syslogfacility) {
switch (level) {
case DBG_INFO:
priority = LOG_INFO;
break;
case DBG_WARN:
priority = LOG_WARNING;
break;
case DBG_ERR:
priority = LOG_ERR;
break;
default:
priority = LOG_DEBUG;
}
vsyslog(priority, format, ap);
} else {
timebuf = malloc(256);
if (timebuf) {
gettimeofday(&now, NULL);
ctime_r(&now.tv_sec, timebuf);
timebuf[strlen(timebuf) - 1] = '\0';
fprintf(debug_file, "%s: ", timebuf);
free(timebuf);
}
vfprintf(debug_file, format, ap);
fprintf(debug_file, "\n");
}
}
void debug(uint8_t level, char *format, ...) { void debug(uint8_t level, char *format, ...) {
va_list ap;
if (level < debug_level)
return;
va_start(ap, format);
debug_logit(level, format, ap);
va_end(ap);
}
void debugx(int status, uint8_t level, char *format, ...) {
if (level >= debug_level) { if (level >= debug_level) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vfprintf(stderr, format, ap); debug_logit(level, format, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n");
} }
if (level >= DBG_ERR) exit(status);
exit(1);
} }
...@@ -10,5 +10,9 @@ ...@@ -10,5 +10,9 @@
#define DBG_WARN 32 #define DBG_WARN 32
#define DBG_ERR 64 #define DBG_ERR 64
void debug_init(char *ident);
void debug_set_level(uint8_t level); void debug_set_level(uint8_t level);
uint8_t debug_get_level();
void debug(uint8_t level, char *format, ...); void debug(uint8_t level, char *format, ...);
void debugx(int status, uint8_t level, char *format, ...);
int debug_set_destination(char *dest);
This diff is collapsed.
...@@ -18,3 +18,8 @@ TLSCertificateKeyPassword follow the white rabbit ...@@ -18,3 +18,8 @@ TLSCertificateKeyPassword follow the white rabbit
#listenUDP localhost #listenUDP localhost
#listenTCP 10.10.10.10:2084 #listenTCP 10.10.10.10:2084
#ListenTCP [2001:700:1:7:215:f2ff:fe35:307d]:2084 #ListenTCP [2001:700:1:7:215:f2ff:fe35:307d]:2084
# Optional log level. 2 is default, 1 is less, 3 is more
#LogLevel 2
#Optional LogDestinatinon, else stderr used for logging
#LogDestination x-syslog://
#LogDestination file:///tmp/rp.log
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
sizeof(struct sockaddr_in) : \ sizeof(struct sockaddr_in) : \
sizeof(struct sockaddr_in6)) sizeof(struct sockaddr_in6))
#define DEBUG_LEVEL DBG_INFO #define DEBUG_LEVEL DBG_WARN
#define CONFIG_MAIN "/etc/radsecproxy/radsecproxy.conf" #define CONFIG_MAIN "/etc/radsecproxy/radsecproxy.conf"
#define CONFIG_SERVERS "/etc/radsecproxy/servers.conf" #define CONFIG_SERVERS "/etc/radsecproxy/servers.conf"
...@@ -57,6 +57,8 @@ struct options { ...@@ -57,6 +57,8 @@ struct options {
char *tlscertificatekeypassword; char *tlscertificatekeypassword;
char *listenudp; char *listenudp;
char *listentcp; char *listentcp;
char *logdestination;
uint8_t loglevel;
uint8_t statusserver; uint8_t statusserver;
}; };
......
...@@ -13,10 +13,11 @@ ...@@ -13,10 +13,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include "debug.h" #include "debug.h"
#if 0
#include <errno.h>
void errx(char *format, ...) { void errx(char *format, ...) {
extern int errno; extern int errno;
...@@ -47,6 +48,7 @@ void err(char *format, ...) { ...@@ -47,6 +48,7 @@ void err(char *format, ...) {
} else } else
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
#endif
char *stringcopy(char *s, int len) { char *stringcopy(char *s, int len) {
char *r; char *r;
...@@ -78,7 +80,7 @@ char *addr2string(struct sockaddr *addr, socklen_t len) { ...@@ -78,7 +80,7 @@ char *addr2string(struct sockaddr *addr, socklen_t len) {
} }
if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]), if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
NULL, 0, NI_NUMERICHOST)) { NULL, 0, NI_NUMERICHOST)) {
err("getnameinfo"); debug(DBG_WARN, "getnameinfo failed");
return NULL; return NULL;
} }
return addr_buf[i]; return addr_buf[i];
...@@ -93,19 +95,19 @@ int connectport(int type, char *host, char *port) { ...@@ -93,19 +95,19 @@ int connectport(int type, char *host, char *port) {
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
if (getaddrinfo(host, port, &hints, &res0) != 0) { if (getaddrinfo(host, port, &hints, &res0) != 0) {
err("connectport: can't resolve host %s port %s", host, port); debug(DBG_ERR, "connectport: can't resolve host %s port %s", host, port);
return -1; return -1;
} }
for (res = res0; res; res = res->ai_next) { for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) { if (s < 0) {
err("connectport: socket failed"); debug(DBG_WARN, "connectport: socket failed");
continue; continue;
} }
if (connect(s, res->ai_addr, res->ai_addrlen) == 0) if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
break; break;
err("connectport: connect failed"); debug(DBG_WARN, "connectport: connect failed");
close(s); close(s);
s = -1; s = -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment