]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/formats.c
Use the canonicalized IP address to look up the TH.
[irc/quakenet/newserv.git] / trusts / formats.c
CommitLineData
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 10char *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
32char *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
40char *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
52char *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
64int 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
CP
115 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
116 } else {
117 r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
de723023 118 &tg->trustedfor, &tg->flags, &tg->maxperident,
82a316e7
CP
119 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
120 }
121 if(r != 7)
122 return 0;
123
124 tg->expires = (time_t)expires;
125 tg->lastseen = (time_t)lastseen;
1f685425 126 tg->lastmaxusereset = (time_t)lastmaxusereset;
82a316e7
CP
127
128 createdby = &line[pos];
129 contact = strchr(createdby, ',');
130 if(!contact)
131 return 0;
132 *contact++ = '\0';
133
134 comment = strchr(contact, ',');
135 if(!comment)
136 return 0;
137 *comment++ = '\0';
138
139 tg->name = getsstring(name, TRUSTNAMELEN);
1f685425 140 tg->createdby = getsstring(createdby, CREATEDBYLEN);
82a316e7
CP
141 tg->comment = getsstring(comment, COMMENTLEN);
142 tg->contact = getsstring(contact, CONTACTLEN);
143 if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
144 freesstring(tg->name);
145 freesstring(tg->createdby);
146 freesstring(tg->comment);
147 freesstring(tg->contact);
148 return 0;
149 }
150
151 return 1;
152}
153
154int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
caf2d02a 155 unsigned long lastseen, created;
cebc4cab 156 int maxpernode, nodebits;
82a316e7
CP
157 char *ip, xbuf[1024], *id;
158
159/* #id,213.230.192.128/26,20,23,1222732944
160 ip ,cur,max,lastseen */
161
162 strlcpy(xbuf, line, sizeof(xbuf));
163 id = line = xbuf;
164
165 line = strchr(line, ',');
166 if(!line)
167 return 0;
168 *line++ = '\0';
169
ce7bce4c
CP
170 if(oformat && (id[0] == '#'))
171 id++;
172
82a316e7
CP
173 *tgid = strtoul(id, NULL, 10);
174 if(!*tgid)
175 return 0;
176
177 ip = line;
178 line = strchr(line, ',');
179 if(!line)
180 return 0;
181 *line++ = '\0';
182
6e6e98da 183 if(!ipmask_parse(ip, &th->ip, &th->bits))
82a316e7
CP
184 return 0;
185
186 if(oformat) {
187 if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
188 return 0;
acd5f58f 189 created = getnettime();
de76d9bd
GB
190 maxpernode = 0;
191 nodebits = 128;
82a316e7 192 } else {
cebc4cab 193 if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6)
82a316e7
CP
194 return 0;
195 }
196
197 th->lastseen = (time_t)lastseen;
caf2d02a 198 th->created = (time_t)created;
cebc4cab
GB
199 th->maxpernode = maxpernode;
200 th->nodebits = nodebits;
82a316e7
CP
201
202 return 1;
203}
204
205char *rtrim(char *buf) {
206 static char obuf[1024];
207 size_t len = strlcpy(obuf, buf, sizeof(obuf));
208
209 if((len < sizeof(obuf)) && (len > 0)) {
0555113a 210 int i;
82a316e7
CP
211 for(i=len-1;i>=0;i--) {
212 if(obuf[i] != ' ')
213 break;
214
215 obuf[i] = '\0';
216 }
217 }
218
219 return obuf;
220}