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