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