comparison dcclib/str2type.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.27 $Revision$
38 */
39
40 #include "dcc_defs.h"
41 #include "dcc_xhdr.h"
42 #include <ctype.h>
43
44
45 static struct tbl {
46 char nm[DCC_XHDR_MAX_TYPE_LEN];
47 DCC_CK_TYPES type;
48 } tbl[] = {
49 {DCC_XHDR_TYPE_IP, DCC_CK_IP},
50 {DCC_XHDR_TYPE_ENV_FROM, DCC_CK_ENV_FROM},
51 {DCC_XHDR_TYPE_FROM, DCC_CK_FROM},
52 {DCC_XHDR_TYPE_SUB, DCC_CK_SUB},
53 {DCC_XHDR_TYPE_MESSAGE_ID, DCC_CK_MESSAGE_ID},
54 {DCC_XHDR_TYPE_RECEIVED, DCC_CK_RECEIVED},
55 {DCC_XHDR_TYPE_BODY, DCC_CK_BODY},
56 {DCC_XHDR_TYPE_FUZ1, DCC_CK_FUZ1},
57 {DCC_XHDR_TYPE_FUZ2, DCC_CK_FUZ2},
58 {DCC_XHDR_TYPE_GREY_MSG, DCC_CK_G_MSG_R_TOTAL},
59 {DCC_XHDR_TYPE_GREY_TRIPLE, DCC_CK_G_TRIPLE_R_BULK},
60 {DCC_XHDR_TYPE_REP_TOTAL, DCC_CK_G_MSG_R_TOTAL},
61 {DCC_XHDR_TYPE_REP_BULK, DCC_CK_G_TRIPLE_R_BULK},
62 {DCC_XHDR_TYPE_ENV_TO, DCC_CK_ENV_TO}, /* same as DCC_CK_FLOD_PATH */
63 {DCC_XHDR_TYPE_FLOD_PATH, DCC_CK_FLOD_PATH}, /* same as DCC_CK_ENV_TO */
64 {DCC_XHDR_TYPE_SRVR_ID, DCC_CK_SRVR_ID},
65
66 {"ALL", SET_ALL_THOLDS},
67 {"CMN", SET_CMN_THOLDS},
68 };
69
70
71
72 static DCC_CK_TYPES
73 dcc_str2type_base(const char *str,
74 int len0) /* length or -1 */
75 {
76 struct tbl *tp;
77 const char *tgtp, *nmp;
78 char tgtc, nmc, d;
79 int len, i;
80
81 /* ignore leading blanks */
82 i = strspn(str, DCC_WHITESPACE);
83 str += i;
84 if (len0 >= 0) {
85 len0 -= i;
86 if (len0 <= 0)
87 return DCC_CK_INVALID;
88 }
89
90 for (tp = tbl; tp <= LAST(tbl); ++tp) {
91 nmp = tp->nm;
92 tgtp = str;
93 len = len0;
94 do {
95 if (len >= 0 && --len < 0)
96 tgtc = '\0';
97 else
98 tgtc = *tgtp++;
99 nmc = *nmp++;
100 if (nmc == '\0') {
101 /* ignore trailing blanks and colons */
102 while (tgtc == ':' || tgtc == ' '
103 || tgtc == '\t' || tgtc == '\n') {
104 if (len >= 0 && --len < 0)
105 tgtc = '\0';
106 else
107 tgtc = *tgtp++;
108 }
109 if (tgtc == '\0')
110 return tp->type;
111 break;
112 }
113 d = (nmc ^ tgtc);
114 } while (d == 0 || d == ('A' ^ 'a')
115 || ((nmc == '-' || nmc == '.' || nmc == '_')
116 && (tgtc == '-' || tgtc == '.' || tgtc == '_')));
117 }
118
119 return DCC_CK_INVALID;
120 }
121
122
123
124 /* for whiteclnt files and dccsight */
125 DCC_CK_TYPES
126 dcc_str2type_wf(const char *str,
127 int len0) /* length or -1 */
128 {
129 DCC_CK_TYPES type;
130
131 type = dcc_str2type_base(str, len0);
132 if (!DCC_CK_THOLD_OK(type)
133 && type != DCC_CK_ENV_TO)
134 return DCC_CK_INVALID;
135 return type;
136 }
137
138
139
140 /* types that can be deleted in database */
141 DCC_CK_TYPES
142 dcc_str2type_del(const char *str,
143 int len0) /* length or -1 */
144 {
145 DCC_CK_TYPES type;
146
147 type = dcc_str2type_base(str, len0);
148 if (!DCC_CK_THOLD_OK(type)
149 && type != DCC_CK_SRVR_ID)
150 return DCC_CK_INVALID;
151 return type;
152 }
153
154
155
156 /* for `dbclean -t type,...` `dccd -K type` `dccproc -g type` */
157 DCC_CK_TYPES
158 dcc_str2type_db(const char *str,
159 int len0) /* length or -1 */
160 {
161 DCC_CK_TYPES type;
162
163 type = dcc_str2type_base(str, len0);
164 if (!DCC_CK_THOLD_OK(type))
165 return DCC_CK_INVALID;
166 return type;
167 }
168
169
170
171 /* for `dbclean -t type,...` and dccm, dccifd, and dccproc thresholds */
172 DCC_CK_TYPES
173 dcc_str2type_thold(const char *str,
174 int len0) /* length or -1 */
175 {
176 DCC_CK_TYPES type;
177
178 type = dcc_str2type_base(str, len0);
179 if (!DCC_CK_THOLD_OK(type)
180 && type != SET_ALL_THOLDS
181 && type != SET_CMN_THOLDS)
182 return DCC_CK_INVALID;
183 return type;
184 }