]>
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" | |
acd5f58f | 7 | #include "../irc/irc.h" |
82a316e7 | 8 | #include "trusts.h" |
8a95d3e4 | 9 | |
6e6e98da | 10 | char *trusts_cidr2str(struct irc_in_addr *ip, unsigned char bits) { |
8a95d3e4 | 11 | static char buf[100]; |
110ab792 GB |
12 | struct irc_in_addr iptemp; |
13 | int i; | |
8a95d3e4 | 14 | |
110ab792 | 15 | for(i=0;i<8;i++) { |
d06f8b79 | 16 | int curbits = bits - i * 16; |
110ab792 | 17 | |
d06f8b79 | 18 | if (curbits<0) |
110ab792 | 19 | curbits = 0; |
d06f8b79 GB |
20 | else if (curbits>16) |
21 | curbits = 16; | |
110ab792 GB |
22 | |
23 | uint16_t mask = 0xffff & ~((1 << (16 - curbits)) - 1); | |
24 | iptemp.in6_16[i] = htons(ntohs(ip->in6_16[i]) & mask); | |
25 | } | |
26 | ||
27 | snprintf(buf, sizeof(buf), "%s/%u", IPtostr(iptemp), (irc_in_addr_is_ipv4(&iptemp))?bits-96:bits); | |
8a95d3e4 CP |
28 | |
29 | return buf; | |
30 | } | |
31 | ||
32 | char *trusts_timetostr(time_t t) { | |
33 | static char buf[100]; | |
34 | ||
35 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t)); | |
36 | ||
37 | return buf; | |
38 | } | |
39 | ||
82a316e7 CP |
40 | char *dumpth(trusthost *th, int oformat) { |
41 | static char buf[512]; | |
42 | ||
43 | if(oformat) { | |
34110ca7 | 44 | snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(&th->ip, th->bits), th->count, th->maxusage, (intmax_t)th->lastseen); |
82a316e7 | 45 | } else { |
34110ca7 | 46 | snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd,%jd,%u,%u", th->group->id, trusts_cidr2str(&th->ip, th->bits), th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits); |
82a316e7 CP |
47 | } |
48 | ||
49 | return buf; | |
50 | } | |
51 | ||
52 | char *dumptg(trustgroup *tg, int oformat) { | |
53 | static char buf[512]; | |
54 | ||
55 | if(oformat) { | |
de723023 | 56 | 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->flags & TRUST_ENFORCE_IDENT, 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 | 57 | } else { |
de723023 | 58 | snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->flags, 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 |
59 | } |
60 | ||
61 | return buf; | |
62 | } | |
63 | ||
64 | int parsetg(char *buf, trustgroup *tg, int oformat) { | |
65 | char *line, *createdby, *contact, *comment, *name, *id; | |
66 | unsigned long expires, lastseen, lastmaxusereset; | |
67 | char xbuf[1024]; | |
68 | int pos; | |
69 | ||
70 | /* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie) | |
71 | ,name ,current | |
72 | ,trustedfor | |
de723023 | 73 | ,flags |
82a316e7 CP |
74 | ,maxperident |
75 | ,maxusage | |
76 | ,expires ,lastseen ,lastmaxusereset | |
77 | ,createdby,contact ,comment | |
78 | */ | |
79 | int r; | |
80 | ||
81 | strlcpy(xbuf, buf, sizeof(xbuf)); | |
82 | line = xbuf; | |
83 | ||
84 | if(!*line) | |
85 | return 0; | |
86 | ||
87 | if(oformat) { | |
88 | if(line[0] != '#') | |
89 | return 0; | |
90 | line++; | |
91 | } | |
92 | ||
93 | id = line; | |
94 | line = strchr(xbuf, ','); | |
95 | if(!line) | |
96 | return 0; | |
97 | *line++ = '\0'; | |
98 | ||
ce7bce4c CP |
99 | if(oformat && (id[0] == '#')) |
100 | id++; | |
101 | ||
82a316e7 CP |
102 | tg->id = strtoul(id, NULL, 10); |
103 | if(!tg->id) | |
104 | return 0; | |
105 | ||
106 | name = line; | |
107 | line = strchr(line, ','); | |
108 | if(!line) | |
109 | return 0; | |
110 | *line++ = '\0'; | |
111 | ||
112 | if(oformat) { | |
113 | r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n", | |
de723023 | 114 | /*current, */ &tg->trustedfor, &tg->flags, &tg->maxperident, |
82a316e7 | 115 | &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos); |
3a8c35c9 GB |
116 | |
117 | if(tg->maxperident > 0) | |
118 | tg->flags |= TRUST_RELIABLE_USERNAME; | |
82a316e7 CP |
119 | } else { |
120 | r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n", | |
de723023 | 121 | &tg->trustedfor, &tg->flags, &tg->maxperident, |
82a316e7 CP |
122 | &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos); |
123 | } | |
124 | if(r != 7) | |
125 | return 0; | |
126 | ||
127 | tg->expires = (time_t)expires; | |
128 | tg->lastseen = (time_t)lastseen; | |
1f685425 | 129 | tg->lastmaxusereset = (time_t)lastmaxusereset; |
82a316e7 CP |
130 | |
131 | createdby = &line[pos]; | |
132 | contact = strchr(createdby, ','); | |
133 | if(!contact) | |
134 | return 0; | |
135 | *contact++ = '\0'; | |
136 | ||
137 | comment = strchr(contact, ','); | |
138 | if(!comment) | |
139 | return 0; | |
140 | *comment++ = '\0'; | |
141 | ||
142 | tg->name = getsstring(name, TRUSTNAMELEN); | |
1f685425 | 143 | tg->createdby = getsstring(createdby, CREATEDBYLEN); |
82a316e7 CP |
144 | tg->comment = getsstring(comment, COMMENTLEN); |
145 | tg->contact = getsstring(contact, CONTACTLEN); | |
146 | if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) { | |
147 | freesstring(tg->name); | |
148 | freesstring(tg->createdby); | |
149 | freesstring(tg->comment); | |
150 | freesstring(tg->contact); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | return 1; | |
155 | } | |
156 | ||
157 | int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) { | |
caf2d02a | 158 | unsigned long lastseen, created; |
cebc4cab | 159 | int maxpernode, nodebits; |
82a316e7 CP |
160 | char *ip, xbuf[1024], *id; |
161 | ||
162 | /* #id,213.230.192.128/26,20,23,1222732944 | |
163 | ip ,cur,max,lastseen */ | |
164 | ||
165 | strlcpy(xbuf, line, sizeof(xbuf)); | |
166 | id = line = xbuf; | |
167 | ||
168 | line = strchr(line, ','); | |
169 | if(!line) | |
170 | return 0; | |
171 | *line++ = '\0'; | |
172 | ||
ce7bce4c CP |
173 | if(oformat && (id[0] == '#')) |
174 | id++; | |
175 | ||
82a316e7 CP |
176 | *tgid = strtoul(id, NULL, 10); |
177 | if(!*tgid) | |
178 | return 0; | |
179 | ||
180 | ip = line; | |
181 | line = strchr(line, ','); | |
182 | if(!line) | |
183 | return 0; | |
184 | *line++ = '\0'; | |
185 | ||
6e6e98da | 186 | if(!ipmask_parse(ip, &th->ip, &th->bits)) |
82a316e7 CP |
187 | return 0; |
188 | ||
189 | if(oformat) { | |
190 | if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2) | |
191 | return 0; | |
acd5f58f | 192 | created = getnettime(); |
de76d9bd GB |
193 | maxpernode = 0; |
194 | nodebits = 128; | |
82a316e7 | 195 | } else { |
cebc4cab | 196 | if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6) |
82a316e7 CP |
197 | return 0; |
198 | } | |
199 | ||
200 | th->lastseen = (time_t)lastseen; | |
caf2d02a | 201 | th->created = (time_t)created; |
cebc4cab GB |
202 | th->maxpernode = maxpernode; |
203 | th->nodebits = nodebits; | |
82a316e7 CP |
204 | |
205 | return 1; | |
206 | } | |
207 | ||
208 | char *rtrim(char *buf) { | |
209 | static char obuf[1024]; | |
210 | size_t len = strlcpy(obuf, buf, sizeof(obuf)); | |
211 | ||
212 | if((len < sizeof(obuf)) && (len > 0)) { | |
0555113a | 213 | int i; |
82a316e7 CP |
214 | for(i=len-1;i>=0;i--) { |
215 | if(obuf[i] != ' ') | |
216 | break; | |
217 | ||
218 | obuf[i] = '\0'; | |
219 | } | |
220 | } | |
221 | ||
222 | return obuf; | |
223 | } |