1 /*
2  * SNOOPY LOGGER
3  *
4  * File: misc.c
5  *
6  * Copyright (c) 2014-2015 Bostjan Skufca <bostjan@a2o.si>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 
23 
24 
25 /*
26  * Includes order: from local to global
27  */
28 #include "misc.h"
29 
30 #include "snoopy.h"
31 #include "configuration.h"
32 #include "error.h"
33 #include "inputdatastorage.h"
34 #ifdef SNOOPY_CONF_THREAD_SAFETY_ENABLED
35 #include "tsrm.h"
36 #endif
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 
43 
44 
45 /*
46  * snoopy_init
47  *
48  * Description:
49  *     Handles Snoopy initialization/startup specifics.
50  *     This method must be called when initializing anything that is
51  *     Snoopy-based. This is especially true for thread-safe Snoopy builds.
52  *
53  * Params:
54  *     (none)
55  *
56  * Return:
57  *     void
58  */
snoopy_init()59 void snoopy_init ()
60 {
61 #ifdef SNOOPY_CONF_THREAD_SAFETY_ENABLED
62     snoopy_tsrm_ctor();
63 #endif
64     snoopy_configuration_ctor();
65     snoopy_inputdatastorage_ctor();
66 }
67 
68 
69 
70 /*
71  * snoopy_cleanup
72  *
73  * Description:
74  *     Handles all Snoopy deinitialization/shutdown specifics
75  *
76  * Params:
77  *     (none)
78  *
79  * Return:
80  *     void
81  */
snoopy_cleanup()82 void snoopy_cleanup ()
83 {
84     /* Reverse order from ctor */
85     snoopy_inputdatastorage_dtor();
86     snoopy_configuration_dtor();
87 #ifdef SNOOPY_CONF_THREAD_SAFETY_ENABLED
88     snoopy_tsrm_dtor();
89 #endif
90 }
91 
92 
93 
94 /*
95  * snoopy_string_append
96  *
97  * Description:
98  *     Appends content to the end of string, watching for
99  *     buffer overrun.
100  *
101  * Params:
102  *     destString:            string container to append to
103  *     appendThis:            content to append to destString
104  *     destStringMaxLength:   maximum length of dest string, including \0
105  *
106  * Return:
107  *     void
108  */
snoopy_string_append(char * destString,const char * appendThis,int destStringMaxLength)109 void snoopy_string_append (
110     char *destString,
111     const char *appendThis,
112     int   destStringMaxLength
113 ) {
114     int   destStringSize          = -1;
115     int   destStringSizeRemaining = -1;
116     int   appendThisSize          = -1;
117 
118     /* Verify the limits */
119     destStringSize          = (int) strlen(destString);
120     appendThisSize          = (int) strlen(appendThis);
121     destStringSizeRemaining = destStringMaxLength - destStringSize;
122     if (destStringSizeRemaining < appendThisSize) {
123         snoopy_error_handler("Maximum destination string size exceeded");
124     }
125 
126     /* Copy to the destination string */
127     strcat(&destString[destStringSize], appendThis);
128 }
129 
130 
131 
132 /*
133  * snoopy_string_countChars
134  *
135  * Description:
136  *     Counts number of occurrences of specified character in a given string.
137  *
138  * Params:
139  *     stringToSearch:   string to look into
140  *     characterToCount: search for this character
141  *
142  * Return:
143  *     int   Number of occurrences
144  */
snoopy_string_countChars(const char * stringToSearch,char characterToCount)145 int  snoopy_string_countChars (const char *stringToSearch, char characterToCount)
146 {
147     const char *strPtr = stringToSearch;
148     int charCount      = 0;
149 
150     while ('\0' != *strPtr) {
151         if (*strPtr == characterToCount) {
152             charCount++;
153         }
154         strPtr++;
155     }
156 
157     return charCount;
158 }
159 
160 
161 
162 /*
163  * snoopy_syslog_convert_facilityToInt
164  *
165  * Description:
166  *     Returns corresponding integer for each syslog facility, or -1 on failure.
167  *
168  * Params:
169  *     facilityStr   Syslog facility string to convert
170  *
171  * Return:
172  *     int           Corresponding syslog facility id, or -1 if not found
173  */
snoopy_syslog_convert_facilityToInt(const char * facilityStr)174 int snoopy_syslog_convert_facilityToInt (
175     const char *facilityStr
176 ) {
177     const char *facilityStrAdj;
178     int   facilityInt;
179 
180     facilityStrAdj = facilityStr;
181 
182     // If there is LOG_ prefix, loose it.
183     if ('_' == facilityStr[3]) {
184         facilityStrAdj = &facilityStr[4];
185     }
186 
187     // Evaluate
188     if      (strcmp(facilityStrAdj, "AUTH")     == 0) { facilityInt = LOG_AUTH;     }
189     else if (strcmp(facilityStrAdj, "AUTHPRIV") == 0) { facilityInt = LOG_AUTHPRIV; }
190     else if (strcmp(facilityStrAdj, "CRON")     == 0) { facilityInt = LOG_CRON;     }
191     else if (strcmp(facilityStrAdj, "DAEMON")   == 0) { facilityInt = LOG_DAEMON;   }
192     else if (strcmp(facilityStrAdj, "FTP")      == 0) { facilityInt = LOG_FTP;      }
193     else if (strcmp(facilityStrAdj, "KERN")     == 0) { facilityInt = LOG_KERN;     }
194     else if (strcmp(facilityStrAdj, "LOCAL0")   == 0) { facilityInt = LOG_LOCAL0;   }
195     else if (strcmp(facilityStrAdj, "LOCAL1")   == 0) { facilityInt = LOG_LOCAL1;   }
196     else if (strcmp(facilityStrAdj, "LOCAL2")   == 0) { facilityInt = LOG_LOCAL2;   }
197     else if (strcmp(facilityStrAdj, "LOCAL3")   == 0) { facilityInt = LOG_LOCAL3;   }
198     else if (strcmp(facilityStrAdj, "LOCAL4")   == 0) { facilityInt = LOG_LOCAL4;   }
199     else if (strcmp(facilityStrAdj, "LOCAL5")   == 0) { facilityInt = LOG_LOCAL5;   }
200     else if (strcmp(facilityStrAdj, "LOCAL6")   == 0) { facilityInt = LOG_LOCAL6;   }
201     else if (strcmp(facilityStrAdj, "LOCAL7")   == 0) { facilityInt = LOG_LOCAL7;   }
202     else if (strcmp(facilityStrAdj, "LPR")      == 0) { facilityInt = LOG_LPR;      }
203     else if (strcmp(facilityStrAdj, "MAIL")     == 0) { facilityInt = LOG_MAIL;     }
204     else if (strcmp(facilityStrAdj, "NEWS")     == 0) { facilityInt = LOG_NEWS;     }
205     else if (strcmp(facilityStrAdj, "SYSLOG")   == 0) { facilityInt = LOG_SYSLOG;   }
206     else if (strcmp(facilityStrAdj, "USER")     == 0) { facilityInt = LOG_USER;     }
207     else if (strcmp(facilityStrAdj, "UUCP")     == 0) { facilityInt = LOG_UUCP;     }
208     else {
209         facilityInt = -1;
210     }
211 
212     return facilityInt;
213 }
214 
215 
216 
217 /*
218  * snoopy_syslog_convert_facilityToStr
219  *
220  * Description:
221  *     Convert syslog facility from integer code to corresponding string.
222  *
223  * Params:
224  *     facilityInt   Syslog facility to convert
225  *
226  * Return:
227  *     const char*   Corresponding syslog facility string
228  */
snoopy_syslog_convert_facilityToStr(int facilityInt)229 const char* snoopy_syslog_convert_facilityToStr (
230     int   facilityInt
231 ) {
232     const char *facilityStr;
233 
234     // Evaluate and set return value
235     if      (LOG_AUTH     == facilityInt) { facilityStr = "AUTH";     }
236     else if (LOG_AUTHPRIV == facilityInt) { facilityStr = "AUTHPRIV"; }
237     else if (LOG_CRON     == facilityInt) { facilityStr = "CRON";     }
238     else if (LOG_DAEMON   == facilityInt) { facilityStr = "DAEMON";   }
239     else if (LOG_FTP      == facilityInt) { facilityStr = "FTP";      }
240     else if (LOG_KERN     == facilityInt) { facilityStr = "KERN";     }
241     else if (LOG_LOCAL0   == facilityInt) { facilityStr = "LOCAL0";   }
242     else if (LOG_LOCAL1   == facilityInt) { facilityStr = "LOCAL1";   }
243     else if (LOG_LOCAL2   == facilityInt) { facilityStr = "LOCAL2";   }
244     else if (LOG_LOCAL3   == facilityInt) { facilityStr = "LOCAL3";   }
245     else if (LOG_LOCAL4   == facilityInt) { facilityStr = "LOCAL4";   }
246     else if (LOG_LOCAL5   == facilityInt) { facilityStr = "LOCAL5";   }
247     else if (LOG_LOCAL6   == facilityInt) { facilityStr = "LOCAL6";   }
248     else if (LOG_LOCAL7   == facilityInt) { facilityStr = "LOCAL7";   }
249     else if (LOG_LPR      == facilityInt) { facilityStr = "LPR";      }
250     else if (LOG_MAIL     == facilityInt) { facilityStr = "MAIL";     }
251     else if (LOG_NEWS     == facilityInt) { facilityStr = "NEWS";     }
252     else if (LOG_SYSLOG   == facilityInt) { facilityStr = "SYSLOG";   }
253     else if (LOG_USER     == facilityInt) { facilityStr = "USER";     }
254     else if (LOG_UUCP     == facilityInt) { facilityStr = "UUCP";     }
255     else {
256         facilityStr = "(invalid)";
257     }
258 
259     return facilityStr;
260 }
261 
262 
263 
264 /*
265  * snoopy_syslog_convert_levelToInt
266  *
267  * Description:
268  *     Returns corresponding integer for each syslog level, or -1 on failure.
269  *
270  * Params:
271  *     levelStr   Syslog level string to convert
272  *
273  * Return:
274  *     int        Corresponding syslog level id, or -1 if not found
275  */
snoopy_syslog_convert_levelToInt(const char * levelStr)276 int snoopy_syslog_convert_levelToInt (
277     const char *levelStr
278 ) {
279     const char *levelStrAdj;
280     int   levelInt;
281 
282     levelStrAdj = levelStr;
283 
284     // If there is LOG_ prefix, loose it.
285     if ('_' == levelStr[3]) {
286         levelStrAdj = &levelStr[4];
287     }
288 
289     // Evaluate and set return value
290     if      (strcmp(levelStrAdj, "EMERG")   == 0) { levelInt = LOG_EMERG;   }
291     else if (strcmp(levelStrAdj, "ALERT")   == 0) { levelInt = LOG_ALERT;   }
292     else if (strcmp(levelStrAdj, "CRIT")    == 0) { levelInt = LOG_CRIT;    }
293     else if (strcmp(levelStrAdj, "ERR")     == 0) { levelInt = LOG_ERR;     }
294     else if (strcmp(levelStrAdj, "WARNING") == 0) { levelInt = LOG_WARNING; }
295     else if (strcmp(levelStrAdj, "NOTICE")  == 0) { levelInt = LOG_NOTICE;  }
296     else if (strcmp(levelStrAdj, "INFO")    == 0) { levelInt = LOG_INFO;    }
297     else if (strcmp(levelStrAdj, "DEBUG")   == 0) { levelInt = LOG_DEBUG;   }
298     else {
299         levelInt = SNOOPY_SYSLOG_LEVEL;
300     }
301 
302     return levelInt;
303 }
304 
305 
306 
307 /*
308  * snoopy_syslog_convert_levelToStr
309  *
310  * Description:
311  *     Convert syslog level from integer code to corresponding string.
312  *
313  * Params:
314  *     levelInt      Syslog level to convert
315  *
316  * Return:
317  *     const char*   Corresponding syslog facility string
318  */
snoopy_syslog_convert_levelToStr(int levelInt)319 const char* snoopy_syslog_convert_levelToStr (
320     int   levelInt
321 ) {
322     const char *levelStr;
323 
324     // Evaluate and set return value
325     if      (LOG_EMERG   == levelInt) { levelStr = "EMERG";   }
326     else if (LOG_ALERT   == levelInt) { levelStr = "ALERT";   }
327     else if (LOG_CRIT    == levelInt) { levelStr = "CRIT";    }
328     else if (LOG_ERR     == levelInt) { levelStr = "ERR";     }
329     else if (LOG_WARNING == levelInt) { levelStr = "WARNING"; }
330     else if (LOG_NOTICE  == levelInt) { levelStr = "NOTICE";  }
331     else if (LOG_INFO    == levelInt) { levelStr = "INFO";    }
332     else if (LOG_DEBUG   == levelInt) { levelStr = "DEBUG";   }
333     else {
334         levelStr = "(invalid)";
335     }
336 
337     return levelStr;
338 }
339