xref: /freebsd/sbin/ipf/libipf/facpri.c (revision 70fad8acae7324525df09b46e3ee2007f3ecf0fe)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  *
741edb306SCy Schubert  * $Id$
841edb306SCy Schubert  */
941edb306SCy Schubert 
1041edb306SCy Schubert #include <stdio.h>
1141edb306SCy Schubert #include <string.h>
1241edb306SCy Schubert #include <limits.h>
13*70fad8acSElyes Haouas #include <sys/param.h>
1441edb306SCy Schubert #if !defined(__SVR4) && !defined(__svr4__)
1541edb306SCy Schubert #include <strings.h>
1641edb306SCy Schubert #endif
1741edb306SCy Schubert #include <stdlib.h>
1841edb306SCy Schubert #include <unistd.h>
1941edb306SCy Schubert #include <stddef.h>
2041edb306SCy Schubert #include <syslog.h>
2141edb306SCy Schubert #include "facpri.h"
2241edb306SCy Schubert 
2341edb306SCy Schubert 
2441edb306SCy Schubert 
2541edb306SCy Schubert typedef	struct	table	{
2641edb306SCy Schubert 	char	*name;
2741edb306SCy Schubert 	int	value;
2841edb306SCy Schubert } table_t;
2941edb306SCy Schubert 
3041edb306SCy Schubert table_t	facs[] = {
3141edb306SCy Schubert 	{ "kern", LOG_KERN },	{ "user", LOG_USER },
3241edb306SCy Schubert 	{ "mail", LOG_MAIL },	{ "daemon", LOG_DAEMON },
3341edb306SCy Schubert 	{ "auth", LOG_AUTH },	{ "syslog", LOG_SYSLOG },
3441edb306SCy Schubert 	{ "lpr", LOG_LPR },	{ "news", LOG_NEWS },
3541edb306SCy Schubert 	{ "uucp", LOG_UUCP },
3641edb306SCy Schubert #if LOG_CRON == LOG_CRON2
3741edb306SCy Schubert 	{ "cron2", LOG_CRON1 },
3841edb306SCy Schubert #else
3941edb306SCy Schubert 	{ "cron", LOG_CRON1 },
4041edb306SCy Schubert #endif
4141edb306SCy Schubert #ifdef LOG_FTP
4241edb306SCy Schubert 	{ "ftp", LOG_FTP },
4341edb306SCy Schubert #endif
4441edb306SCy Schubert #ifdef LOG_AUTHPRIV
4541edb306SCy Schubert 	{ "authpriv", LOG_AUTHPRIV },
4641edb306SCy Schubert #endif
4741edb306SCy Schubert #ifdef	LOG_AUDIT
4841edb306SCy Schubert 	{ "audit", LOG_AUDIT },
4941edb306SCy Schubert #endif
5041edb306SCy Schubert #ifdef	LOG_LFMT
5141edb306SCy Schubert 	{ "logalert", LOG_LFMT },
5241edb306SCy Schubert #endif
5341edb306SCy Schubert #if LOG_CRON == LOG_CRON1
5441edb306SCy Schubert 	{ "cron", LOG_CRON2 },
5541edb306SCy Schubert #else
5641edb306SCy Schubert 	{ "cron2", LOG_CRON2 },
5741edb306SCy Schubert #endif
5841edb306SCy Schubert #ifdef	LOG_SECURITY
5941edb306SCy Schubert 	{ "security", LOG_SECURITY },
6041edb306SCy Schubert #endif
6141edb306SCy Schubert 	{ "local0", LOG_LOCAL0 },	{ "local1", LOG_LOCAL1 },
6241edb306SCy Schubert 	{ "local2", LOG_LOCAL2 },	{ "local3", LOG_LOCAL3 },
6341edb306SCy Schubert 	{ "local4", LOG_LOCAL4 },	{ "local5", LOG_LOCAL5 },
6441edb306SCy Schubert 	{ "local6", LOG_LOCAL6 },	{ "local7", LOG_LOCAL7 },
6541edb306SCy Schubert 	{ NULL, 0 }
6641edb306SCy Schubert };
6741edb306SCy Schubert 
6841edb306SCy Schubert 
6941edb306SCy Schubert /*
7041edb306SCy Schubert  * map a facility number to its name
7141edb306SCy Schubert  */
7241edb306SCy Schubert char *
73efeb8bffSCy Schubert fac_toname(int facpri)
7441edb306SCy Schubert {
7541edb306SCy Schubert 	int	i, j, fac;
7641edb306SCy Schubert 
7741edb306SCy Schubert 	fac = facpri & LOG_FACMASK;
7841edb306SCy Schubert 	j = fac >> 3;
79*70fad8acSElyes Haouas 	if (j < nitems(facs)) {
8041edb306SCy Schubert 		if (facs[j].value == fac)
812582ae57SCy Schubert 			return (facs[j].name);
8241edb306SCy Schubert 	}
8341edb306SCy Schubert 	for (i = 0; facs[i].name; i++)
8441edb306SCy Schubert 		if (fac == facs[i].value)
852582ae57SCy Schubert 			return (facs[i].name);
8641edb306SCy Schubert 
872582ae57SCy Schubert 	return (NULL);
8841edb306SCy Schubert }
8941edb306SCy Schubert 
9041edb306SCy Schubert 
9141edb306SCy Schubert /*
9241edb306SCy Schubert  * map a facility name to its number
9341edb306SCy Schubert  */
9441edb306SCy Schubert int
95efeb8bffSCy Schubert fac_findname(char *name)
9641edb306SCy Schubert {
9741edb306SCy Schubert 	int     i;
9841edb306SCy Schubert 
9941edb306SCy Schubert 	for (i = 0; facs[i].name; i++)
10041edb306SCy Schubert 		if (!strcmp(facs[i].name, name))
1012582ae57SCy Schubert 			return (facs[i].value);
1022582ae57SCy Schubert 	return (-1);
10341edb306SCy Schubert }
10441edb306SCy Schubert 
10541edb306SCy Schubert 
10641edb306SCy Schubert table_t	pris[] = {
10741edb306SCy Schubert 	{ "emerg", LOG_EMERG },		{ "alert", LOG_ALERT  },
10841edb306SCy Schubert 	{ "crit", LOG_CRIT },		{ "err", LOG_ERR  },
10941edb306SCy Schubert 	{ "warn", LOG_WARNING },	{ "notice", LOG_NOTICE  },
11041edb306SCy Schubert 	{ "info", LOG_INFO },		{ "debug", LOG_DEBUG  },
11141edb306SCy Schubert 	{ NULL, 0 }
11241edb306SCy Schubert };
11341edb306SCy Schubert 
11441edb306SCy Schubert 
11541edb306SCy Schubert /*
11641edb306SCy Schubert  * map a facility name to its number
11741edb306SCy Schubert  */
11841edb306SCy Schubert int
119efeb8bffSCy Schubert pri_findname(char *name)
12041edb306SCy Schubert {
12141edb306SCy Schubert 	int     i;
12241edb306SCy Schubert 
12341edb306SCy Schubert 	for (i = 0; pris[i].name; i++)
12441edb306SCy Schubert 		if (!strcmp(pris[i].name, name))
1252582ae57SCy Schubert 			return (pris[i].value);
1262582ae57SCy Schubert 	return (-1);
12741edb306SCy Schubert }
12841edb306SCy Schubert 
12941edb306SCy Schubert 
13041edb306SCy Schubert /*
13141edb306SCy Schubert  * map a priority number to its name
13241edb306SCy Schubert  */
13341edb306SCy Schubert char *
134efeb8bffSCy Schubert pri_toname(int facpri)
13541edb306SCy Schubert {
13641edb306SCy Schubert 	int	i, pri;
13741edb306SCy Schubert 
13841edb306SCy Schubert 	pri = facpri & LOG_PRIMASK;
13941edb306SCy Schubert 	if (pris[pri].value == pri)
1402582ae57SCy Schubert 		return (pris[pri].name);
14141edb306SCy Schubert 	for (i = 0; pris[i].name; i++)
14241edb306SCy Schubert 		if (pri == pris[i].value)
1432582ae57SCy Schubert 			return (pris[i].name);
1442582ae57SCy Schubert 	return (NULL);
14541edb306SCy Schubert }
146