xref: /freebsd/sbin/ipf/libipf/facpri.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
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 
24 
25 typedef	struct	table	{
26 	char	*name;
27 	int	value;
28 } table_t;
29 
30 table_t	facs[] = {
31 	{ "kern", LOG_KERN },	{ "user", LOG_USER },
32 	{ "mail", LOG_MAIL },	{ "daemon", LOG_DAEMON },
33 	{ "auth", LOG_AUTH },	{ "syslog", LOG_SYSLOG },
34 	{ "lpr", LOG_LPR },	{ "news", LOG_NEWS },
35 	{ "uucp", LOG_UUCP },
36 #if LOG_CRON == LOG_CRON2
37 	{ "cron2", LOG_CRON1 },
38 #else
39 	{ "cron", LOG_CRON1 },
40 #endif
41 #ifdef LOG_FTP
42 	{ "ftp", LOG_FTP },
43 #endif
44 #ifdef LOG_AUTHPRIV
45 	{ "authpriv", LOG_AUTHPRIV },
46 #endif
47 #ifdef	LOG_AUDIT
48 	{ "audit", LOG_AUDIT },
49 #endif
50 #ifdef	LOG_LFMT
51 	{ "logalert", LOG_LFMT },
52 #endif
53 #if LOG_CRON == LOG_CRON1
54 	{ "cron", LOG_CRON2 },
55 #else
56 	{ "cron2", LOG_CRON2 },
57 #endif
58 #ifdef	LOG_SECURITY
59 	{ "security", LOG_SECURITY },
60 #endif
61 	{ "local0", LOG_LOCAL0 },	{ "local1", LOG_LOCAL1 },
62 	{ "local2", LOG_LOCAL2 },	{ "local3", LOG_LOCAL3 },
63 	{ "local4", LOG_LOCAL4 },	{ "local5", LOG_LOCAL5 },
64 	{ "local6", LOG_LOCAL6 },	{ "local7", LOG_LOCAL7 },
65 	{ NULL, 0 }
66 };
67 
68 
69 /*
70  * map a facility number to its name
71  */
72 char *
73 fac_toname(int facpri)
74 {
75 	int	i, j, fac;
76 
77 	fac = facpri & LOG_FACMASK;
78 	j = fac >> 3;
79 	if (j < (sizeof(facs)/sizeof(facs[0]))) {
80 		if (facs[j].value == fac)
81 			return (facs[j].name);
82 	}
83 	for (i = 0; facs[i].name; i++)
84 		if (fac == facs[i].value)
85 			return (facs[i].name);
86 
87 	return (NULL);
88 }
89 
90 
91 /*
92  * map a facility name to its number
93  */
94 int
95 fac_findname(char *name)
96 {
97 	int     i;
98 
99 	for (i = 0; facs[i].name; i++)
100 		if (!strcmp(facs[i].name, name))
101 			return (facs[i].value);
102 	return (-1);
103 }
104 
105 
106 table_t	pris[] = {
107 	{ "emerg", LOG_EMERG },		{ "alert", LOG_ALERT  },
108 	{ "crit", LOG_CRIT },		{ "err", LOG_ERR  },
109 	{ "warn", LOG_WARNING },	{ "notice", LOG_NOTICE  },
110 	{ "info", LOG_INFO },		{ "debug", LOG_DEBUG  },
111 	{ NULL, 0 }
112 };
113 
114 
115 /*
116  * map a facility name to its number
117  */
118 int
119 pri_findname(char *name)
120 {
121 	int     i;
122 
123 	for (i = 0; pris[i].name; i++)
124 		if (!strcmp(pris[i].name, name))
125 			return (pris[i].value);
126 	return (-1);
127 }
128 
129 
130 /*
131  * map a priority number to its name
132  */
133 char *
134 pri_toname(int facpri)
135 {
136 	int	i, pri;
137 
138 	pri = facpri & LOG_PRIMASK;
139 	if (pris[pri].value == pri)
140 		return (pris[pri].name);
141 	for (i = 0; pris[i].name; i++)
142 		if (pri == pris[i].value)
143 			return (pris[i].name);
144 	return (NULL);
145 }
146