]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/formats.c
Merge.
[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"
7#include "trusts.h"
8a95d3e4
CP
8
9int trusts_parsecidr(const char *host, uint32_t *ip, short *mask) {
10 unsigned int octet1 = 0, octet2 = 0, octet3 = 0, octet4 = 0, umask = 32;
11
12 if(sscanf(host, "%u.%u.%u.%u/%u", &octet1, &octet2, &octet3, &octet4, &umask) != 5)
13 if(sscanf(host, "%u.%u.%u/%u", &octet1, &octet2, &octet3, &umask) != 4)
14 if(sscanf(host, "%u.%u/%u", &octet1, &octet2, &umask) != 3)
15 if(sscanf(host, "%u/%u", &octet1, &umask) != 2)
16 if(sscanf(host, "%u.%u.%u.%u", &octet1, &octet2, &octet3, &octet4) != 4)
17 return 0;
18
19 if(octet1 > 255 || octet2 > 255 || octet3 > 255 || octet4 > 255 || umask > 32)
20 return 0;
21
22 *ip = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4;
23 *mask = umask;
24
25 return 1;
26}
27
28/* returns mask pre-anded */
29int trusts_str2cidr(const char *host, uint32_t *ip, uint32_t *mask) {
30 uint32_t result;
31 short smask;
32
33 if(!trusts_parsecidr(host, &result, &smask))
34 return 0;
35
36 if(smask == 0) {
37 *mask = 0;
38 } else {
39 *mask = 0xffffffff << (32 - smask);
40 }
41 *ip = result & *mask;
42
43 return 1;
44}
45
46char *trusts_cidr2str(uint32_t ip, uint32_t mask) {
47 static char buf[100];
48 char maskbuf[10];
49
50 if(mask != 0) {
51 /* count number of trailing zeros */
52 float f = (float)(mask & -mask);
53
54 mask = 32 - ((*(unsigned int *)&f >> 23) - 0x7f);
55 }
56
57 if(mask < 32) {
58 snprintf(maskbuf, sizeof(maskbuf), "/%u", mask);
59 } else {
60 maskbuf[0] = '\0';
61 }
62
63 snprintf(buf, sizeof(buf), "%u.%u.%u.%u%s", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, maskbuf);
64
65 return buf;
66}
67
68char *trusts_timetostr(time_t t) {
69 static char buf[100];
70
71 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
72
73 return buf;
74}
75
82a316e7
CP
76char *dumpth(trusthost *th, int oformat) {
77 static char buf[512];
78
79 if(oformat) {
80 snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (intmax_t)th->lastseen);
81 } else {
82 snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->id, th->maxusage, (intmax_t)th->lastseen);
83 }
84
85 return buf;
86}
87
88char *dumptg(trustgroup *tg, int oformat) {
89 static char buf[512];
90
91 if(oformat) {
92 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->lastmaxuserreset, tg->createdby->content, tg->contact->content, tg->comment->content);
93 } else {
94 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->lastmaxuserreset, tg->createdby->content, tg->contact->content, tg->comment->content);
95 }
96
97 return buf;
98}
99
100int parsetg(char *buf, trustgroup *tg, int oformat) {
101 char *line, *createdby, *contact, *comment, *name, *id;
102 unsigned long expires, lastseen, lastmaxusereset;
103 char xbuf[1024];
104 int pos;
105
106/* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
107 ,name ,current
108 ,trustedfor
109 ,mode
110 ,maxperident
111 ,maxusage
112 ,expires ,lastseen ,lastmaxusereset
113 ,createdby,contact ,comment
114*/
115 int r;
116
117 strlcpy(xbuf, buf, sizeof(xbuf));
118 line = xbuf;
119
120 if(!*line)
121 return 0;
122
123 if(oformat) {
124 if(line[0] != '#')
125 return 0;
126 line++;
127 }
128
129 id = line;
130 line = strchr(xbuf, ',');
131 if(!line)
132 return 0;
133 *line++ = '\0';
134
135 tg->id = strtoul(id, NULL, 10);
136 if(!tg->id)
137 return 0;
138
139 name = line;
140 line = strchr(line, ',');
141 if(!line)
142 return 0;
143 *line++ = '\0';
144
145 if(oformat) {
146 r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
147 /*current, */ &tg->trustedfor, &tg->mode, &tg->maxperident,
148 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
149 } else {
150 r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
151 &tg->trustedfor, &tg->mode, &tg->maxperident,
152 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
153 }
154 if(r != 7)
155 return 0;
156
157 tg->expires = (time_t)expires;
158 tg->lastseen = (time_t)lastseen;
159 tg->lastmaxuserreset = (time_t)lastmaxusereset;
160
161 createdby = &line[pos];
162 contact = strchr(createdby, ',');
163 if(!contact)
164 return 0;
165 *contact++ = '\0';
166
167 comment = strchr(contact, ',');
168 if(!comment)
169 return 0;
170 *comment++ = '\0';
171
172 tg->name = getsstring(name, TRUSTNAMELEN);
173 tg->createdby = getsstring(createdby, NICKLEN);
174 tg->comment = getsstring(comment, COMMENTLEN);
175 tg->contact = getsstring(contact, CONTACTLEN);
176 if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
177 freesstring(tg->name);
178 freesstring(tg->createdby);
179 freesstring(tg->comment);
180 freesstring(tg->contact);
181 return 0;
182 }
183
184 return 1;
185}
186
187int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
188 unsigned long lastseen;
189 char *ip, xbuf[1024], *id;
190
191/* #id,213.230.192.128/26,20,23,1222732944
192 ip ,cur,max,lastseen */
193
194 strlcpy(xbuf, line, sizeof(xbuf));
195 id = line = xbuf;
196
197 line = strchr(line, ',');
198 if(!line)
199 return 0;
200 *line++ = '\0';
201
202 *tgid = strtoul(id, NULL, 10);
203 if(!*tgid)
204 return 0;
205
206 ip = line;
207 line = strchr(line, ',');
208 if(!line)
209 return 0;
210 *line++ = '\0';
211
212 if(!trusts_str2cidr(ip, &th->ip, &th->mask))
213 return 0;
214
215 if(oformat) {
216 if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
217 return 0;
218 } else {
219 if(sscanf(line, "%u,%u,%lu", &th->id, &th->maxusage, &lastseen) != 3)
220 return 0;
221 }
222
223 th->lastseen = (time_t)lastseen;
224
225 return 1;
226}
227
228char *rtrim(char *buf) {
229 static char obuf[1024];
230 size_t len = strlcpy(obuf, buf, sizeof(obuf));
231
232 if((len < sizeof(obuf)) && (len > 0)) {
233 size_t i;
234 for(i=len-1;i>=0;i--) {
235 if(obuf[i] != ' ')
236 break;
237
238 obuf[i] = '\0';
239 }
240 }
241
242 return obuf;
243}