]>
Commit | Line | Data |
---|---|---|
8a95d3e4 CP |
1 | #include <stdio.h> |
2 | #include <stdint.h> | |
3 | #include <time.h> | |
82a316e7 CP |
4 | #include <string.h> |
5 | #include <stdlib.h> | |
6 | #include "../lib/strlfunc.h" | |
7 | #include "trusts.h" | |
8a95d3e4 CP |
8 | |
9 | int trusts_parsecidr(const char *host, uint32_t *ip, short *mask) { | |
10 | unsigned int octet1 = 0, octet2 = 0, octet3 = 0, octet4 = 0, umask = 32; | |
11 | ||
12 | if(sscanf(host, "%u.%u.%u.%u/%u", &octet1, &octet2, &octet3, &octet4, &umask) != 5) | |
13 | if(sscanf(host, "%u.%u.%u/%u", &octet1, &octet2, &octet3, &umask) != 4) | |
14 | if(sscanf(host, "%u.%u/%u", &octet1, &octet2, &umask) != 3) | |
15 | if(sscanf(host, "%u/%u", &octet1, &umask) != 2) | |
16 | if(sscanf(host, "%u.%u.%u.%u", &octet1, &octet2, &octet3, &octet4) != 4) | |
17 | return 0; | |
18 | ||
19 | if(octet1 > 255 || octet2 > 255 || octet3 > 255 || octet4 > 255 || umask > 32) | |
20 | return 0; | |
21 | ||
22 | *ip = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; | |
23 | *mask = umask; | |
24 | ||
25 | return 1; | |
26 | } | |
27 | ||
28 | /* returns mask pre-anded */ | |
29 | int trusts_str2cidr(const char *host, uint32_t *ip, uint32_t *mask) { | |
30 | uint32_t result; | |
31 | short smask; | |
32 | ||
33 | if(!trusts_parsecidr(host, &result, &smask)) | |
34 | return 0; | |
35 | ||
36 | if(smask == 0) { | |
37 | *mask = 0; | |
38 | } else { | |
39 | *mask = 0xffffffff << (32 - smask); | |
40 | } | |
41 | *ip = result & *mask; | |
42 | ||
43 | return 1; | |
44 | } | |
45 | ||
46 | char *trusts_cidr2str(uint32_t ip, uint32_t mask) { | |
47 | static char buf[100]; | |
48 | char maskbuf[10]; | |
49 | ||
50 | if(mask != 0) { | |
51 | /* count number of trailing zeros */ | |
52 | float f = (float)(mask & -mask); | |
53 | ||
54 | mask = 32 - ((*(unsigned int *)&f >> 23) - 0x7f); | |
55 | } | |
56 | ||
57 | if(mask < 32) { | |
58 | snprintf(maskbuf, sizeof(maskbuf), "/%u", mask); | |
59 | } else { | |
60 | maskbuf[0] = '\0'; | |
61 | } | |
62 | ||
63 | snprintf(buf, sizeof(buf), "%u.%u.%u.%u%s", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, maskbuf); | |
64 | ||
65 | return buf; | |
66 | } | |
67 | ||
68 | char *trusts_timetostr(time_t t) { | |
69 | static char buf[100]; | |
70 | ||
71 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t)); | |
72 | ||
73 | return buf; | |
74 | } | |
75 | ||
82a316e7 CP |
76 | char *dumpth(trusthost *th, int oformat) { |
77 | static char buf[512]; | |
78 | ||
79 | if(oformat) { | |
80 | snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (intmax_t)th->lastseen); | |
81 | } else { | |
cebc4cab | 82 | snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd,%jd,%u,%u", th->group->id, trusts_cidr2str(th->ip, th->mask), th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits); |
82a316e7 CP |
83 | } |
84 | ||
85 | return buf; | |
86 | } | |
87 | ||
88 | char *dumptg(trustgroup *tg, int oformat) { | |
89 | static char buf[512]; | |
90 | ||
91 | if(oformat) { | |
1f685425 | 92 | snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->count, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content); |
82a316e7 | 93 | } else { |
1f685425 | 94 | snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content); |
82a316e7 CP |
95 | } |
96 | ||
97 | return buf; | |
98 | } | |
99 | ||
100 | int parsetg(char *buf, trustgroup *tg, int oformat) { | |
101 | char *line, *createdby, *contact, *comment, *name, *id; | |
102 | unsigned long expires, lastseen, lastmaxusereset; | |
103 | char xbuf[1024]; | |
104 | int pos; | |
105 | ||
106 | /* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie) | |
107 | ,name ,current | |
108 | ,trustedfor | |
109 | ,mode | |
110 | ,maxperident | |
111 | ,maxusage | |
112 | ,expires ,lastseen ,lastmaxusereset | |
113 | ,createdby,contact ,comment | |
114 | */ | |
115 | int r; | |
116 | ||
117 | strlcpy(xbuf, buf, sizeof(xbuf)); | |
118 | line = xbuf; | |
119 | ||
120 | if(!*line) | |
121 | return 0; | |
122 | ||
123 | if(oformat) { | |
124 | if(line[0] != '#') | |
125 | return 0; | |
126 | line++; | |
127 | } | |
128 | ||
129 | id = line; | |
130 | line = strchr(xbuf, ','); | |
131 | if(!line) | |
132 | return 0; | |
133 | *line++ = '\0'; | |
134 | ||
ce7bce4c CP |
135 | if(oformat && (id[0] == '#')) |
136 | id++; | |
137 | ||
82a316e7 CP |
138 | tg->id = strtoul(id, NULL, 10); |
139 | if(!tg->id) | |
140 | return 0; | |
141 | ||
142 | name = line; | |
143 | line = strchr(line, ','); | |
144 | if(!line) | |
145 | return 0; | |
146 | *line++ = '\0'; | |
147 | ||
148 | if(oformat) { | |
149 | r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n", | |
150 | /*current, */ &tg->trustedfor, &tg->mode, &tg->maxperident, | |
151 | &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos); | |
152 | } else { | |
153 | r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n", | |
154 | &tg->trustedfor, &tg->mode, &tg->maxperident, | |
155 | &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos); | |
156 | } | |
157 | if(r != 7) | |
158 | return 0; | |
159 | ||
160 | tg->expires = (time_t)expires; | |
161 | tg->lastseen = (time_t)lastseen; | |
1f685425 | 162 | tg->lastmaxusereset = (time_t)lastmaxusereset; |
82a316e7 CP |
163 | |
164 | createdby = &line[pos]; | |
165 | contact = strchr(createdby, ','); | |
166 | if(!contact) | |
167 | return 0; | |
168 | *contact++ = '\0'; | |
169 | ||
170 | comment = strchr(contact, ','); | |
171 | if(!comment) | |
172 | return 0; | |
173 | *comment++ = '\0'; | |
174 | ||
175 | tg->name = getsstring(name, TRUSTNAMELEN); | |
1f685425 | 176 | tg->createdby = getsstring(createdby, CREATEDBYLEN); |
82a316e7 CP |
177 | tg->comment = getsstring(comment, COMMENTLEN); |
178 | tg->contact = getsstring(contact, CONTACTLEN); | |
179 | if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) { | |
180 | freesstring(tg->name); | |
181 | freesstring(tg->createdby); | |
182 | freesstring(tg->comment); | |
183 | freesstring(tg->contact); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | return 1; | |
188 | } | |
189 | ||
190 | int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) { | |
caf2d02a | 191 | unsigned long lastseen, created; |
cebc4cab | 192 | int maxpernode, nodebits; |
82a316e7 CP |
193 | char *ip, xbuf[1024], *id; |
194 | ||
195 | /* #id,213.230.192.128/26,20,23,1222732944 | |
196 | ip ,cur,max,lastseen */ | |
197 | ||
198 | strlcpy(xbuf, line, sizeof(xbuf)); | |
199 | id = line = xbuf; | |
200 | ||
201 | line = strchr(line, ','); | |
202 | if(!line) | |
203 | return 0; | |
204 | *line++ = '\0'; | |
205 | ||
ce7bce4c CP |
206 | if(oformat && (id[0] == '#')) |
207 | id++; | |
208 | ||
82a316e7 CP |
209 | *tgid = strtoul(id, NULL, 10); |
210 | if(!*tgid) | |
211 | return 0; | |
212 | ||
213 | ip = line; | |
214 | line = strchr(line, ','); | |
215 | if(!line) | |
216 | return 0; | |
217 | *line++ = '\0'; | |
218 | ||
219 | if(!trusts_str2cidr(ip, &th->ip, &th->mask)) | |
220 | return 0; | |
221 | ||
222 | if(oformat) { | |
223 | if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2) | |
224 | return 0; | |
caf2d02a | 225 | created = time(NULL); |
82a316e7 | 226 | } else { |
cebc4cab | 227 | if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6) |
82a316e7 CP |
228 | return 0; |
229 | } | |
230 | ||
231 | th->lastseen = (time_t)lastseen; | |
caf2d02a | 232 | th->created = (time_t)created; |
cebc4cab GB |
233 | th->maxpernode = maxpernode; |
234 | th->nodebits = nodebits; | |
82a316e7 CP |
235 | |
236 | return 1; | |
237 | } | |
238 | ||
239 | char *rtrim(char *buf) { | |
240 | static char obuf[1024]; | |
241 | size_t len = strlcpy(obuf, buf, sizeof(obuf)); | |
242 | ||
243 | if((len < sizeof(obuf)) && (len > 0)) { | |
0555113a | 244 | int i; |
82a316e7 CP |
245 | for(i=len-1;i>=0;i--) { |
246 | if(obuf[i] != ' ') | |
247 | break; | |
248 | ||
249 | obuf[i] = '\0'; | |
250 | } | |
251 | } | |
252 | ||
253 | return obuf; | |
254 | } |