]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/formats.c
fixes for clang
[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 "trusts.h"
8
9 int 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 */
29 int 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
46 char *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
68 char *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
76 char *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
88 char *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->lastmaxusereset, 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->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
95 }
96
97 return buf;
98 }
99
100 int 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 if(oformat && (id[0] == '#'))
136 id++;
137
138 tg->id = strtoul(id, NULL, 10);
139 if(!tg->id)
140 return 0;
141
142 name = line;
143 line = strchr(line, ',');
144 if(!line)
145 return 0;
146 *line++ = '\0';
147
148 if(oformat) {
149 r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
150 /*current, */ &tg->trustedfor, &tg->mode, &tg->maxperident,
151 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
152 } else {
153 r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
154 &tg->trustedfor, &tg->mode, &tg->maxperident,
155 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
156 }
157 if(r != 7)
158 return 0;
159
160 tg->expires = (time_t)expires;
161 tg->lastseen = (time_t)lastseen;
162 tg->lastmaxusereset = (time_t)lastmaxusereset;
163
164 createdby = &line[pos];
165 contact = strchr(createdby, ',');
166 if(!contact)
167 return 0;
168 *contact++ = '\0';
169
170 comment = strchr(contact, ',');
171 if(!comment)
172 return 0;
173 *comment++ = '\0';
174
175 tg->name = getsstring(name, TRUSTNAMELEN);
176 tg->createdby = getsstring(createdby, CREATEDBYLEN);
177 tg->comment = getsstring(comment, COMMENTLEN);
178 tg->contact = getsstring(contact, CONTACTLEN);
179 if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
180 freesstring(tg->name);
181 freesstring(tg->createdby);
182 freesstring(tg->comment);
183 freesstring(tg->contact);
184 return 0;
185 }
186
187 return 1;
188 }
189
190 int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
191 unsigned long lastseen;
192 char *ip, xbuf[1024], *id;
193
194 /* #id,213.230.192.128/26,20,23,1222732944
195 ip ,cur,max,lastseen */
196
197 strlcpy(xbuf, line, sizeof(xbuf));
198 id = line = xbuf;
199
200 line = strchr(line, ',');
201 if(!line)
202 return 0;
203 *line++ = '\0';
204
205 if(oformat && (id[0] == '#'))
206 id++;
207
208 *tgid = strtoul(id, NULL, 10);
209 if(!*tgid)
210 return 0;
211
212 ip = line;
213 line = strchr(line, ',');
214 if(!line)
215 return 0;
216 *line++ = '\0';
217
218 if(!trusts_str2cidr(ip, &th->ip, &th->mask))
219 return 0;
220
221 if(oformat) {
222 if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
223 return 0;
224 } else {
225 if(sscanf(line, "%u,%u,%lu", &th->id, &th->maxusage, &lastseen) != 3)
226 return 0;
227 }
228
229 th->lastseen = (time_t)lastseen;
230
231 return 1;
232 }
233
234 char *rtrim(char *buf) {
235 static char obuf[1024];
236 size_t len = strlcpy(obuf, buf, sizeof(obuf));
237
238 if((len < sizeof(obuf)) && (len > 0)) {
239 int i;
240 for(i=len-1;i>=0;i--) {
241 if(obuf[i] != ' ')
242 break;
243
244 obuf[i] = '\0';
245 }
246 }
247
248 return obuf;
249 }