1 #include "policyd.h"
2
3
4 /*
5 *
6 *
7 * Policy Daemon
8 *
9 * policy daemon is used in conjuction with postfix to combat spam.
10 *
11 * Copyright (C) 2004 Cami Sardinha (cami@mweb.co.za)
12 *
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 *
29 *
30 */
31
32 /*
33 * SYSLOG FACILITIES
34 */
35 static struct syslog_priority {
36 char *str;
37 int num;
38 } syslog_priorities[] = {
39
40 /* priorities */
41 #ifdef LOG_EMERG
42 { "LOG_EMERG", LOG_EMERG },
43 #endif
44 #ifdef LOG_ALERT
45 { "LOG_ALERT", LOG_ALERT },
46 #endif
47 #ifdef LOG_CRIT
48 { "LOG_CRIT", LOG_CRIT },
49 #endif
50 #ifdef LOG_ERR
51 { "LOG_ERR", LOG_ERR },
52 #endif
53 #ifdef LOG_WARNING
54 { "LOG_WARNING", LOG_WARNING },
55 #endif
56 #ifdef LOG_NOTICE
57 { "LOG_NOTICE", LOG_NOTICE },
58 #endif
59 #ifdef LOG_INFO
60 { "LOG_INFO", LOG_INFO },
61 #endif
62 #ifdef LOG_DEBUG
63 { "LOG_DEBUG", LOG_DEBUG },
64 #endif
65
66
67 /* facilities */
68 #ifdef LOG_KERN
69 { "LOG_KERN", LOG_KERN },
70 #endif
71 #ifdef LOG_USER
72 { "LOG_USER", LOG_USER },
73 #endif
74 #ifdef LOG_MAIL
75 { "LOG_MAIL", LOG_MAIL },
76 #endif
77 #ifdef LOG_DAEMON
78 { "LOG_DAEMON", LOG_DAEMON },
79 #endif
80 #ifdef LOG_AUTH
81 { "LOG_AUTH", LOG_AUTH },
82 #endif
83 #ifdef LOG_SYSLOG
84 { "LOG_SYSLOG", LOG_SYSLOG },
85 #endif
86 #ifdef LOG_AUTHPRIV
87 { "LOG_AUTHPRIV", LOG_AUTHPRIV },
88 #endif
89 #ifdef LOG_LOCAL0
90 { "LOG_LOCAL0", LOG_LOCAL0 },
91 #endif
92 #ifdef LOG_LOCAL1
93 { "LOG_LOCAL1", LOG_LOCAL1 },
94 #endif
95 #ifdef LOG_LOCAL2
96 { "LOG_LOCAL2", LOG_LOCAL2 },
97 #endif
98 #ifdef LOG_LOCAL3
99 { "LOG_LOCAL3", LOG_LOCAL3 },
100 #endif
101 #ifdef LOG_LOCAL4
102 { "LOG_LOCAL4", LOG_LOCAL4 },
103 #endif
104 #ifdef LOG_LOCAL5
105 { "LOG_LOCAL5", LOG_LOCAL5 },
106 #endif
107 #ifdef LOG_LOCAL6
108 { "LOG_LOCAL6", LOG_LOCAL6 },
109 #endif
110 #ifdef LOG_LOCAL7
111 { "LOG_LOCAL7", LOG_LOCAL7 },
112 #endif
113 { NULL, 0 }
114 };
115
116
117
118
119 /*
120 * function: parse_syslog_priority
121 * purpose: split up tokens and ensure they are syslog facilities/priorities
122 * return: syslog facility
123 */
124 int
parse_syslog_priority(char * str)125 parse_syslog_priority(char *str)
126 {
127 char *token;
128 int n = -1;
129
130 token = (char *) strtok (str, "|");
131 if (token == NULL)
132 {
133 logmessage("fatal: error parsing (1st) syslog string: %s from %s\n", token, str);
134 exit(-1);
135 }
136
137 /* ensure priority/facility is supported */
138 syslog_token_set (token, &n);
139
140 /* ensure priority/facility is supported */
141 while ((token = (char *) strtok (NULL, "|")) != NULL)
142 syslog_token_set (token, &n);
143
144 if (n == -1)
145 {
146 logmessage("fatal: error parsing (2st) syslog string: %s from %s\n", token, str);
147 exit(-1);
148 }
149
150 /* return priority */
151 return (n);
152 }
153
154
155
156
157 /*
158 * function: syslog_token_set
159 * purpose: check token against struct of priorities/facilities
160 * return: nada
161 */
162 void
syslog_token_set(char * token,int * value)163 syslog_token_set(char *token, int *value)
164 {
165 unsigned int i;
166 token = strip_space (token);
167
168 for (i = 0; syslog_priorities[i].str != NULL; i++)
169 {
170 if(DEBUG > 3)
171 logmessage("DEBUG: token = %s, got = %s\n",
172 token, syslog_priorities[i].str);
173
174 if (!strcasecmp (token, syslog_priorities[i].str))
175 {
176 if (*value == -1)
177 {
178 if(DEBUG > 3)
179 logmessage("DEBUG: initial set: %s, n = 0x%08lx\n",
180 token, syslog_priorities[i].num);
181
182 *value = syslog_priorities[i].num;
183 } else {
184
185 if(DEBUG > 3)
186 logmessage("DEBUG: subsequent OR: %s, n = 0x%08lx\n",
187 token, *value | syslog_priorities[i].num);
188
189 *value = *value | syslog_priorities[i].num;
190 }
191
192 return;
193 }
194 }
195
196 logmessage("fatal: didn't find priority '%s', exiting\n", token);
197 exit (-1);
198 }
199
200
201
202
203 /*
204 * function: strip_space
205 * purpose: remove whitespace
206 * return: cleaned string
207 */
208 char
strip_space(char * str)209 *strip_space (char *str)
210 {
211 char *p;
212 unsigned int i = 0;
213
214 if (strlen (str) == 0)
215 return (str);
216
217 for (i = 0; isspace (str[i]); i++)
218 ;
219
220 strcpy (str, str + i);
221
222 p = str + strlen (str);
223 while ((p--) != str && isspace (*p))
224 *p = 0;
225
226 return (str);
227 }
228
229
230 /* EOF */
231