xref: /freebsd/sbin/ipf/libipf/facpri.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <sys/types.h>
14 #if !defined(__SVR4) && !defined(__svr4__)
15 #include <strings.h>
16 #endif
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stddef.h>
20 #include <syslog.h>
21 #include "facpri.h"
22 
23 #if !defined(lint)
24 static const char rcsid[] = "@(#)$Id$";
25 #endif
26 
27 
28 typedef	struct	table	{
29 	char	*name;
30 	int	value;
31 } table_t;
32 
33 table_t	facs[] = {
34 	{ "kern", LOG_KERN },	{ "user", LOG_USER },
35 	{ "mail", LOG_MAIL },	{ "daemon", LOG_DAEMON },
36 	{ "auth", LOG_AUTH },	{ "syslog", LOG_SYSLOG },
37 	{ "lpr", LOG_LPR },	{ "news", LOG_NEWS },
38 	{ "uucp", LOG_UUCP },
39 #if LOG_CRON == LOG_CRON2
40 	{ "cron2", LOG_CRON1 },
41 #else
42 	{ "cron", LOG_CRON1 },
43 #endif
44 #ifdef LOG_FTP
45 	{ "ftp", LOG_FTP },
46 #endif
47 #ifdef LOG_AUTHPRIV
48 	{ "authpriv", LOG_AUTHPRIV },
49 #endif
50 #ifdef	LOG_AUDIT
51 	{ "audit", LOG_AUDIT },
52 #endif
53 #ifdef	LOG_LFMT
54 	{ "logalert", LOG_LFMT },
55 #endif
56 #if LOG_CRON == LOG_CRON1
57 	{ "cron", LOG_CRON2 },
58 #else
59 	{ "cron2", LOG_CRON2 },
60 #endif
61 #ifdef	LOG_SECURITY
62 	{ "security", LOG_SECURITY },
63 #endif
64 	{ "local0", LOG_LOCAL0 },	{ "local1", LOG_LOCAL1 },
65 	{ "local2", LOG_LOCAL2 },	{ "local3", LOG_LOCAL3 },
66 	{ "local4", LOG_LOCAL4 },	{ "local5", LOG_LOCAL5 },
67 	{ "local6", LOG_LOCAL6 },	{ "local7", LOG_LOCAL7 },
68 	{ NULL, 0 }
69 };
70 
71 
72 /*
73  * map a facility number to its name
74  */
75 char *
76 fac_toname(int facpri)
77 {
78 	int	i, j, fac;
79 
80 	fac = facpri & LOG_FACMASK;
81 	j = fac >> 3;
82 	if (j < (sizeof(facs)/sizeof(facs[0]))) {
83 		if (facs[j].value == fac)
84 			return (facs[j].name);
85 	}
86 	for (i = 0; facs[i].name; i++)
87 		if (fac == facs[i].value)
88 			return (facs[i].name);
89 
90 	return (NULL);
91 }
92 
93 
94 /*
95  * map a facility name to its number
96  */
97 int
98 fac_findname(char *name)
99 {
100 	int     i;
101 
102 	for (i = 0; facs[i].name; i++)
103 		if (!strcmp(facs[i].name, name))
104 			return (facs[i].value);
105 	return (-1);
106 }
107 
108 
109 table_t	pris[] = {
110 	{ "emerg", LOG_EMERG },		{ "alert", LOG_ALERT  },
111 	{ "crit", LOG_CRIT },		{ "err", LOG_ERR  },
112 	{ "warn", LOG_WARNING },	{ "notice", LOG_NOTICE  },
113 	{ "info", LOG_INFO },		{ "debug", LOG_DEBUG  },
114 	{ NULL, 0 }
115 };
116 
117 
118 /*
119  * map a facility name to its number
120  */
121 int
122 pri_findname(char *name)
123 {
124 	int     i;
125 
126 	for (i = 0; pris[i].name; i++)
127 		if (!strcmp(pris[i].name, name))
128 			return (pris[i].value);
129 	return (-1);
130 }
131 
132 
133 /*
134  * map a priority number to its name
135  */
136 char *
137 pri_toname(int facpri)
138 {
139 	int	i, pri;
140 
141 	pri = facpri & LOG_PRIMASK;
142 	if (pris[pri].value == pri)
143 		return (pris[pri].name);
144 	for (i = 0; pris[i].name; i++)
145 		if (pri == pris[i].value)
146 			return (pris[i].name);
147 	return (NULL);
148 }
149