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