1 #include "../nterfacer/nterfacer.h"
2 #include "../lib/strlfunc.h"
3 #include "../core/nsmalloc.h"
8 #define Stringify(x) __Stringify(x)
9 #define __Stringify(x) #x
11 static void tm_trustdump(trustmigration
*tm
);
13 static void tm_fini(trustmigration
*tm
, int errcode
) {
14 tm
->fini(tm
->tag
, errcode
);
17 nterfacer_freeline(tm
->schedule
);
21 nsfree(POOL_TRUSTS
, tm
);
24 void migration_stop(trustmigration
*tm
) {
25 tm_fini(tm
, MIGRATION_STOPPED
);
28 static int tm_parsegroup(trustmigration
*tm
, unsigned int id
, const char *oline
) {
29 char *line
, *createdby
, *contact
, *comment
, *name
;
30 unsigned int trustedfor
, maxperident
, mode
, maxusage
;
31 unsigned long expires
, lastseen
, lastmaxusereset
;
35 /* ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
41 ,expires ,lastseen ,lastmaxusereset
42 ,createdby,contact ,comment
46 strlcpy(xbuf
, oline
, sizeof(xbuf
));
49 line
= strchr(name
, ',');
54 r
= sscanf(line
, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
55 /*current, */ &trustedfor
, &mode
, &maxperident
,
56 &maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
60 createdby
= &line
[pos
];
61 contact
= strchr(createdby
, ',');
66 comment
= strchr(contact
, ',');
71 tm
->group(tm
->tag
, id
, name
, trustedfor
, mode
, maxperident
, maxusage
, (time_t)expires
, (time_t)lastseen
, (time_t)lastmaxusereset
, createdby
, contact
, comment
);
75 static int tm_parsehost(trustmigration
*tm
, unsigned int id
, char *line
) {
77 unsigned long lastseen
;
80 /* 213.230.192.128/26,20,23,1222732944
81 ip ,cur,max,lastseen */
83 strlcpy(xbuf
, line
, sizeof(xbuf
));
86 line
= strchr(line
, ',');
91 if(sscanf(line
, "%*u,%u,%lu", /*current, */&max
, &lastseen
) != 2)
94 tm
->host(tm
->tag
, id
, ip
, max
, lastseen
);
98 static void tm_stage2(int failure
, int linec
, char **linev
, void *tag
) {
99 trustmigration
*tm
= tag
;
101 unsigned int groupcount
, totallines
, i
, dummy
;
103 #ifdef TRUSTS_MIGRATION_DEBUG
104 Error("trusts", ERR_INFO
, "Migration total lines: %d", linec
);
107 Error("trusts", ERR_INFO
, "Migration line %d: |%s|", i
, linev
[i
]);
111 if(failure
|| (linec
< 1)) {
116 finishline
= linev
[linec
- 1];
117 if(sscanf(finishline
, "Start ID cannot exceed current maximum group ID (#%u).", &dummy
) == 1) {
118 /* the list was truncated while we were reading it, we're done! */
123 if(sscanf(finishline
, "End of list, %u groups and %u lines returned.", &groupcount
, &totallines
) != 2) {
128 if(totallines
!= linec
- 1) {
133 for(i
=0;i
<linec
-1;i
++) {
134 char *linestart
= &linev
[i
][2], type
= linev
[i
][0];
135 if(type
== 'G' || type
== 'H') {
140 if(sscanf(linestart
, "#%u,%n", &id
, &pos
) != 1) {
146 /* this one is missing and we've received a later one instead, update tm->cur to point to this one */
148 } else if(id
< tm
->cur
) {
153 realline
= &linestart
[pos
];
155 ret
= tm_parsegroup(tm
, id
, realline
);
157 ret
= tm_parsehost(tm
, id
, realline
);
172 static void tm_trustdump(trustmigration
*tm
) {
175 if(tm
->cur
>= tm
->count
) {
182 snprintf(buf
, sizeof(buf
), "trustdump #%d 1", tm
->cur
);
183 tm
->schedule
= nterfacer_sendline("R", "relay", 4, (char *[]){"2", "^(Start ID cannot exceed current maximum group ID \\(#\\d+\\)|End of list, 1 groups and \\d+ lines returned\\.)$", "O", buf
}, tm_stage2
, tm
);
186 static void tm_stage1(int failure
, int linec
, char **linev
, void *tag
) {
188 trustmigration
*tm
= tag
;
192 if(failure
|| (linec
!= 1)) {
197 if(sscanf(linev
[0], "Start ID cannot exceed current maximum group ID (#%u).", &count
) != 1) {
202 Error("trusts", ERR_INFO
, "Migration in progress, total groups: %d", count
);
209 trustmigration
*migration_start(TrustMigrationGroup group
, TrustMigrationHost host
, TrustMigrationFini fini
, void *tag
) {
210 trustmigration
*tm
= nsmalloc(POOL_TRUSTS
, sizeof(trustmigration
));
221 tm
->schedule
= nterfacer_sendline("R", "relay", 4, (char *[]){"1", "1", "O", "trustdump #9999999 1"}, tm_stage1
, tm
);