Branch data Line data Source code
1 : : /*
2 : : *****************************************************************************
3 : : *
4 : : * File: log_msg.c
5 : : *
6 : : * Purpose: General logging routine that can write to syslog and/or stderr
7 : : * and can take varibale number of args.
8 : : *
9 : : * Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
10 : : * Copyright (C) 2009-2014 fwknop developers and contributors. For a full
11 : : * list of contributors, see the file 'CREDITS'.
12 : : *
13 : : * License (GNU General Public License):
14 : : *
15 : : * This program is free software; you can redistribute it and/or
16 : : * modify it under the terms of the GNU General Public License
17 : : * as published by the Free Software Foundation; either version 2
18 : : * of the License, or (at your option) any later version.
19 : : *
20 : : * This program is distributed in the hope that it will be useful,
21 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 : : * GNU General Public License for more details.
24 : : *
25 : : * You should have received a copy of the GNU General Public License
26 : : * along with this program; if not, write to the Free Software
27 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 : : * USA
29 : : *
30 : : *****************************************************************************
31 : : */
32 : : #include "fwknopd_common.h"
33 : : #include "utils.h"
34 : : #include "log_msg.h"
35 : :
36 : : /* The default log facility (can be overridden via config file directive).
37 : : */
38 : : static int syslog_fac = LOG_DAEMON;
39 : :
40 : : /* This value is or'ed with the log level on all logging calls. This allows
41 : : * for force log to stderr instead of syslog simply be setting this to the
42 : : * appropriate value (which is done at init_logging().
43 : : */
44 : : static int static_log_flag = LOG_STDERR_ONLY;
45 : :
46 : : /* The name to use for ID in log messages. This defaults to fwknopd.
47 : : */
48 : : static char *log_name = NULL;
49 : :
50 : : /* The value of the default verbosity used by the log module */
51 : : static int verbosity = LOG_DEFAULT_VERBOSITY;
52 : :
53 : : /* Free resources allocated for logging.
54 : : */
55 : : void
56 : 3492 : free_logging(void)
57 : : {
58 [ + + ]: 3492 : if(log_name != NULL)
59 : 1678 : free(log_name);
60 : 3492 : }
61 : :
62 : : /* Initialize logging sets the name used for syslog.
63 : : */
64 : : void
65 : 1678 : init_logging(fko_srv_options_t *opts) {
66 : 1678 : char *my_name = NULL;
67 : 1678 : int is_syslog = 0;
68 : :
69 : : /* In case this is a re-init.
70 : : */
71 : 1678 : free_logging();
72 : :
73 : : /* Allocate memory for the log_name and set the my_name to point to the
74 : : * appropriate name. The name should already be set in the config struct
75 : : * but if it is not, fallback to the default as defined by 'MY_NAME'.
76 : : */
77 [ + - ]: 1678 : if(opts->config[CONF_SYSLOG_IDENTITY] != NULL
78 [ + - ]: 1678 : && opts->config[CONF_SYSLOG_IDENTITY][0] != '\0')
79 : : {
80 : 1678 : my_name = opts->config[CONF_SYSLOG_IDENTITY];
81 : 1678 : log_name = calloc(1, strlen(opts->config[CONF_SYSLOG_IDENTITY])+1);
82 : 1678 : is_syslog = 1;
83 : : }
84 : : else
85 : : {
86 : 0 : my_name = (char*)&MY_NAME;
87 : 0 : log_name = calloc(1, strlen(MY_NAME)+1);
88 : : }
89 : :
90 [ - + ]: 1678 : if(log_name == NULL)
91 : : {
92 : 0 : fprintf(stderr, "Memory allocation error setting log_name!\n");
93 : 0 : clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
94 : : }
95 : :
96 : : /* Set our name.
97 : : */
98 [ + - ]: 1678 : if (is_syslog)
99 : 1678 : strlcpy(log_name, my_name, strlen(opts->config[CONF_SYSLOG_IDENTITY])+1);
100 : : else
101 : 0 : strlcpy(log_name, my_name, strlen(MY_NAME)+1);
102 : :
103 : 1678 : static_log_flag = LOG_SYSLOG_ONLY;
104 : :
105 : : /* If we are running in the foreground or performing firewall operations,
106 : : * all logging will go to stderr.
107 : : */
108 [ - + ]: 1678 : if(opts->foreground != 0
109 [ # # ]: 0 : || opts->fw_flush != 0
110 : : || opts->fw_list != 0
111 [ # # ]: 0 : || opts->fw_list_all != 0)
112 : 1678 : static_log_flag = LOG_STDERR_ONLY;
113 : :
114 : : /* If the user forces syslog using --syslog-enable, we remove the
115 : : * LOG_WITHOUT_SYSLOG flag. It means all messages will go through syslog */
116 [ - + ]: 1678 : if (opts->syslog_enable != 0)
117 : 0 : static_log_flag &= ~LOG_WITHOUT_SYSLOG;
118 : :
119 : : /* Parse the log facility as specified in the config struct. If, for some
120 : : * reason, it is not, fac will already be set to LOG_DAEMON.
121 : : */
122 [ + - ]: 1678 : if(opts->config[CONF_SYSLOG_FACILITY] != NULL
123 [ + - ]: 1678 : && opts->config[CONF_SYSLOG_FACILITY][0] != '\0')
124 : : {
125 [ + + ]: 1678 : if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_DAEMON"))
126 : 1629 : syslog_fac = LOG_DAEMON;
127 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL0"))
128 : 0 : syslog_fac = LOG_LOCAL0;
129 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL1"))
130 : 0 : syslog_fac = LOG_LOCAL1;
131 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL2"))
132 : 0 : syslog_fac = LOG_LOCAL2;
133 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL3"))
134 : 0 : syslog_fac = LOG_LOCAL3;
135 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL4"))
136 : 0 : syslog_fac = LOG_LOCAL4;
137 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL5"))
138 : 0 : syslog_fac = LOG_LOCAL5;
139 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL6"))
140 : 0 : syslog_fac = LOG_LOCAL6;
141 [ - + ]: 49 : else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL7"))
142 : 0 : syslog_fac = LOG_LOCAL7;
143 : : else
144 : : {
145 : 49 : fprintf(stderr, "Invalid SYSLOG_FACILITY setting '%s'\n",
146 : : opts->config[CONF_SYSLOG_FACILITY]);
147 : 49 : clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
148 : : }
149 : : }
150 : :
151 : 1629 : verbosity = LOG_DEFAULT_VERBOSITY + opts->verbose;
152 : 1629 : }
153 : :
154 : : /* Syslog message function. It uses default set at intialization, and also
155 : : * takes variable args to accomodate printf-like formatting and expansion.
156 : : */
157 : : void
158 : 137958 : log_msg(int level, char* msg, ...)
159 : : {
160 : : va_list ap, apse;
161 : :
162 : : /* Make sure the level is in the right range */
163 [ + - ]: 137958 : if ((level & LOG_VERBOSITY_MASK) > verbosity)
164 : 137958 : return;
165 : :
166 : 137958 : va_start(ap, msg);
167 : :
168 : 137958 : level |= static_log_flag;
169 : :
170 : : /* Print msg to stderr if the level was or'ed with LOG_STDERR
171 : : */
172 [ + - ]: 137958 : if(LOG_STDERR & level)
173 : : {
174 : : /* Need to make a copy of our va_list so we don't screw
175 : : * up the message going to syslog after we print it to stderr.
176 : : */
177 : 137958 : va_copy(apse, ap);
178 : :
179 : 137958 : vfprintf(stderr, msg, apse);
180 : 137958 : fprintf(stderr, "\n");
181 : 137958 : fflush(stderr);
182 : :
183 : 137958 : va_end(apse);
184 : : }
185 : :
186 : : /* If the message has not to be printed to the syslog, we return */
187 [ + - ]: 137958 : if (LOG_WITHOUT_SYSLOG & level)
188 : : {
189 : 137958 : va_end(ap);
190 : 137958 : return;
191 : : }
192 : :
193 : : /* Remove the static log flags from the level */
194 : 0 : level &= LOG_VERBOSITY_MASK;
195 : :
196 : : /* Send the message to syslog.
197 : : */
198 : 0 : openlog(log_name, LOG_PID, syslog_fac);
199 : :
200 : : vsyslog(level, msg, ap);
201 : :
202 : 0 : va_end(ap);
203 : : }
204 : :
205 : : /**
206 : : * Set the verbosity level for the current context of the log module.
207 : : *
208 : : * The verbosity levels used by the module are defined by the syslog module.
209 : : *
210 : : * @param level verbosity level to set (LOG_INFO, LOG_NOTICE ...)
211 : : */
212 : : void
213 : 1629 : log_set_verbosity(int level)
214 : : {
215 : 1629 : verbosity = level;
216 : 1629 : }
217 : :
218 : : /***EOF***/
|