2 * log.c Logging module.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2001 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
23 * Copyright 2001 Chad Miller <cmiller@surfsouth.com>
26 static const char rcsid[] = "$Id$";
29 #include "libradius.h"
36 #include <sys/types.h>
46 static int r_mkdir(const char *);
49 static int r_mkdir(const char *part) {
50 char *ptr, parentdir[500];
53 if (stat(part, &st) == 0)
56 ptr = strrchr(part, '/');
61 snprintf(parentdir, (ptr - part)+1, "%s", part);
63 if (r_mkdir(parentdir) != 0)
66 if (mkdir(part, 0770) != 0) {
67 fprintf(stderr, "mkdir(%s) error: %s\n", part, strerror(errno));
71 fprintf(stderr, "Created directory %s\n", part);
77 int radlogdir_iswritable(const char *effectiveuser) {
80 if (radlog_dir[0] != '/')
83 if (r_mkdir(radlog_dir) != 0)
86 /* FIXME: do we have this function? */
87 if (strstr(radlog_dir, "radius") == NULL)
90 /* we have a logdir that mentions 'radius', so it's probably
91 * safe to chown the immediate directory to be owned by the normal
92 * process owner. we gotta do it before we give up root. -chad
99 pwent = getpwnam(effectiveuser);
101 if (pwent == NULL) /* uh oh! */
104 if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
112 * Log the message to the logfile. Include the severity and
115 static int do_log(int lvl, const char *fmt, va_list ap)
118 const char *s = ": ";
124 * NOT debugging, and trying to log debug messages.
126 * Throw the message away.
128 if (!debug_flag && (lvl == L_DBG)) {
133 || (radlog_dest == RADLOG_STDOUT)
134 || (radlog_dir == NULL)) {
137 } else if (radlog_dest == RADLOG_STDERR) {
140 } else if (radlog_dest != RADLOG_SYSLOG) {
142 sprintf(buffer, "%.1000s/%.1000s", radlog_dir, RADIUS_LOG);
143 if ((msgfd = fopen(buffer, "a")) == NULL) {
144 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
145 progname, buffer, strerror(errno));
147 fprintf(stderr, " (");
148 vfprintf(stderr, fmt, ap); /* the message that caused the log */
149 fprintf(stderr, ")\n");
156 if (radlog_dest == RADLOG_SYSLOG) {
163 timeval = time(NULL);
164 ctime_r(&timeval, buffer);
166 switch(lvl & ~L_CONS) {
184 len = strlen(buffer);
187 #ifdef HAVE_VSNPRINTF
188 vsnprintf(buffer + len, sizeof(buffer) - len -1, fmt, ap);
190 vsprintf(buffer + len, fmt, ap);
191 if (strlen(buffer) >= sizeof(buffer) - 1)
192 /* What can we do? */
197 * Filter out characters not in Latin-1.
199 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
200 if (*p == '\r' || *p == '\n')
202 else if (*p < 32 || (*p >= 128 && *p <= 160))
205 strcat(buffer, "\n");
208 * If we're debugging, for small values of debug, then
209 * we don't do timestamps.
211 if ((debug_flag == 1) || (debug_flag == 2)) {
216 * No debugging, or lots of debugging. Print
223 if (radlog_dest != RADLOG_SYSLOG)
227 if (msgfd == stdout) {
229 } else if (msgfd == stderr) {
236 else { /* it was syslog */
237 switch(lvl & ~L_CONS) {
254 syslog(lvl, "%s", buffer + len); /* don't print timestamp */
261 int log_debug(const char *msg, ...)
267 r = do_log(L_DBG, msg, ap);
273 int radlog(int lvl, const char *msg, ...)
279 r = do_log(lvl, msg, ap);