1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2007-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 3 of the License, or (at your
7    option) any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <syslog.h>
21 #include <string.h>
22 #include <mailutils/diag.h>
23 #include <mailutils/kwd.h>
24 #include <mailutils/syslog.h>
25 #include <mailutils/cstr.h>
26 
27 #ifndef LOG_AUTHPRIV
28 # define LOG_AUTHPRIV
29 #endif
30 
31 static mu_kwd_t kw_facility[] = {
32   { "USER",    LOG_USER },
33   { "DAEMON",  LOG_DAEMON },
34   { "AUTH",    LOG_AUTH },
35   { "AUTHPRIV",LOG_AUTHPRIV },
36   { "MAIL",    LOG_MAIL },
37   { "CRON",    LOG_CRON },
38   { "LOCAL0",  LOG_LOCAL0 },
39   { "LOCAL1",  LOG_LOCAL1 },
40   { "LOCAL2",  LOG_LOCAL2 },
41   { "LOCAL3",  LOG_LOCAL3 },
42   { "LOCAL4",  LOG_LOCAL4 },
43   { "LOCAL5",  LOG_LOCAL5 },
44   { "LOCAL6",  LOG_LOCAL6 },
45   { "LOCAL7",  LOG_LOCAL7 },
46   { NULL }
47 };
48 
49 static int
syslog_to_n(mu_kwd_t * kw,const char * str,int * pint)50 syslog_to_n (mu_kwd_t *kw, const char *str, int *pint)
51 {
52   if (mu_c_strncasecmp (str, "LOG_", 4) == 0)
53     str += 4;
54   return mu_kwd_xlat_name_ci (kw, str, pint);
55 }
56 
57 int
mu_string_to_syslog_facility(const char * str,int * pfacility)58 mu_string_to_syslog_facility (const char *str, int *pfacility)
59 {
60   return syslog_to_n (kw_facility, str, pfacility);
61 }
62 
63 const char *
mu_syslog_facility_to_string(int n)64 mu_syslog_facility_to_string (int n)
65 {
66   const char *res = NULL;
67   mu_kwd_xlat_tok (kw_facility, n, &res);
68   return res;
69 }
70 
71 static mu_kwd_t kw_prio[] = {
72   { "EMERG", LOG_EMERG },
73   { "ALERT", LOG_ALERT },
74   { "CRIT", LOG_CRIT },
75   { "ERR", LOG_ERR },
76   { "WARNING", LOG_WARNING },
77   { "NOTICE", LOG_NOTICE },
78   { "INFO", LOG_INFO },
79   { "DEBUG", LOG_DEBUG },
80   { NULL }
81 };
82 
83 int
mu_string_to_syslog_priority(const char * str,int * pprio)84 mu_string_to_syslog_priority (const char *str, int *pprio)
85 {
86   return syslog_to_n (kw_prio, str, pprio);
87 }
88 
89 const char *
mu_syslog_priority_to_string(int n)90 mu_syslog_priority_to_string (int n)
91 {
92   const char *res = NULL;
93   mu_kwd_xlat_tok (kw_prio, n, &res);
94   return res;
95 }
96 
97 int
mu_diag_level_to_syslog(mu_log_level_t level)98 mu_diag_level_to_syslog (mu_log_level_t level)
99 {
100   switch (level)
101     {
102     case MU_DIAG_EMERG:
103       return LOG_EMERG;
104 
105     case MU_DIAG_ALERT:
106       return LOG_ALERT;
107 
108     case MU_DIAG_CRIT:
109       return LOG_CRIT;
110 
111     case MU_DIAG_ERROR:
112       return LOG_ERR;
113 
114     case MU_DIAG_WARNING:
115       return LOG_WARNING;
116 
117     case MU_DIAG_NOTICE:
118       return LOG_NOTICE;
119 
120     case MU_DIAG_INFO:
121       return LOG_INFO;
122 
123     case MU_DIAG_DEBUG:
124       return LOG_DEBUG;
125     }
126   return LOG_EMERG;
127 }
128 
129 
130 
131 int mu_log_syslog = 0;
132 int mu_log_facility = LOG_FACILITY;
133 char *mu_log_tag = NULL;
134 int mu_log_print_severity = 0;
135 unsigned mu_log_severity_threshold = MU_LOG_DEBUG;
136 int mu_log_session_id;
137