]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/formats.c
Merge default.
[irc/quakenet/newserv.git] / trusts / formats.c
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_cidr2str(struct irc_in_addr *ip, unsigned char bits) {
11 static char buf[100];
12 struct irc_in_addr iptemp;
13 int i;
14
15 for(i=0;i<8;i++) {
16 int curbits = bits - i * 16;
17
18 if (curbits<0)
19 curbits = 0;
20 else if (curbits>16)
21 curbits = 16;
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);
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
40 char *dumpth(trusthost *th, int oformat) {
41 static char buf[512];
42
43 if(oformat) {
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);
45 } else {
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);
47 }
48
49 return buf;
50 }
51
52 char *dumptg(trustgroup *tg, int oformat) {
53 static char buf[512];
54
55 if(oformat) {
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);
57 } else {
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);
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
73 ,flags
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
99 if(oformat && (id[0] == '#'))
100 id++;
101
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",
114 /*current, */ &tg->trustedfor, &tg->flags, &tg->maxperident,
115 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
116
117 if(tg->maxperident > 0)
118 tg->flags |= TRUST_RELIABLE_USERNAME;
119 } else {
120 r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
121 &tg->trustedfor, &tg->flags, &tg->maxperident,
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;
129 tg->lastmaxusereset = (time_t)lastmaxusereset;
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);
143 tg->createdby = getsstring(createdby, CREATEDBYLEN);
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) {
158 unsigned long lastseen, created;
159 int maxpernode, nodebits;
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
173 if(oformat && (id[0] == '#'))
174 id++;
175
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
186 if(!ipmask_parse(ip, &th->ip, &th->bits))
187 return 0;
188
189 if(oformat) {
190 if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
191 return 0;
192 created = getnettime();
193 maxpernode = 0;
194 nodebits = 128;
195 } else {
196 if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6)
197 return 0;
198 }
199
200 th->lastseen = (time_t)lastseen;
201 th->created = (time_t)created;
202 th->maxpernode = maxpernode;
203 th->nodebits = nodebits;
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)) {
213 int i;
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 }