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