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