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