]>
Commit | Line | Data |
---|---|---|
be2823bc | 1 | #include "../control/control.h" |
2d4ba67d | 2 | #include "../lib/irc_string.h" |
be2823bc CP |
3 | #include "trusts.h" |
4 | ||
5 | int trusts_migration_start(TrustDBMigrationCallback, void *); | |
6 | void trusts_migration_stop(void); | |
a99a2041 | 7 | static void registercommands(int hooknum, void *arg); |
be2823bc CP |
8 | static void deregistercommands(void); |
9 | ||
be2823bc CP |
10 | static void migrate_status(int errcode, void *tag) { |
11 | long sender = (long)tag; | |
12 | nick *np = getnickbynumeric(sender); | |
13 | ||
14 | if(!np) | |
15 | return; | |
16 | ||
17 | if(!errcode) { | |
45e8ce62 CP |
18 | controlreply(np, "Migration complete."); |
19 | ||
20 | /* stops flush destroying the database */ | |
21 | trusts_freeall(); | |
22 | ||
23 | trusts_unload(); | |
24 | controlreply(np, "All functionality disabled, database unloaded -- please reload the module."); | |
be2823bc | 25 | } else { |
bf5b66e5 | 26 | controlreply(np, "Error %d occured during migration, commands reregistered.", errcode); |
a99a2041 | 27 | registercommands(0, NULL); |
be2823bc CP |
28 | } |
29 | } | |
30 | ||
31 | static int trusts_cmdmigrate(void *source, int cargc, char **cargv) { | |
32 | nick *sender = source; | |
33 | int ret; | |
34 | ||
35 | /* iffy but temporary */ | |
36 | ret = trusts_migration_start(migrate_status, (void *)(sender->numeric)); | |
37 | if(!ret) { | |
bf5b66e5 | 38 | controlreply(sender, "Migration started, commands deregistered."); |
be2823bc CP |
39 | deregistercommands(); |
40 | } else { | |
41 | controlreply(sender, "Error %d starting migration.", ret); | |
42 | } | |
43 | ||
44 | return CMD_OK; | |
45 | } | |
46 | ||
2d4ba67d CP |
47 | static trustgroup *tg_strtotg(char *name) { |
48 | unsigned long id; | |
49 | trustgroup *tg; | |
50 | ||
51 | /* legacy format */ | |
52 | if(name[0] == '#') { | |
53 | id = strtoul(&name[1], NULL, 10); | |
54 | if(id == ULONG_MAX) | |
55 | return NULL; | |
56 | ||
57 | for(tg=tglist;tg;tg=tg->next) | |
58 | if(tg->id == id) | |
59 | return tg; | |
60 | } | |
61 | ||
62 | for(tg=tglist;tg;tg=tg->next) | |
4b003d19 | 63 | if(!match(name, tg->name->content)) |
2d4ba67d CP |
64 | return tg; |
65 | ||
66 | id = strtoul(name, NULL, 10); | |
67 | if(id == ULONG_MAX) | |
68 | return NULL; | |
69 | ||
70 | /* legacy format */ | |
71 | for(tg=tglist;tg;tg=tg->next) | |
72 | if(tg->id == id) | |
73 | return tg; | |
74 | ||
75 | return NULL; | |
76 | } | |
77 | ||
78 | static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) { | |
79 | nick *sender = source; | |
80 | trustgroup *tg; | |
81 | trusthost *th; | |
82 | time_t t; | |
83 | ||
84 | if(cargc < 1) | |
85 | return CMD_USAGE; | |
86 | ||
87 | tg = tg_strtotg(cargv[0]); | |
88 | if(!tg) { | |
89 | controlreply(sender, "Couldn't find a trustgroup with that id."); | |
90 | return CMD_ERROR; | |
91 | } | |
92 | ||
93 | t = time(NULL); | |
94 | ||
4b003d19 | 95 | controlreply(sender, "Name: : %s", tg->name->content); |
2d4ba67d | 96 | controlreply(sender, "Trusted for : %d", tg->trustedfor); |
1bbe1ac3 | 97 | controlreply(sender, "Currently using : %d", tg->count); |
2d4ba67d | 98 | controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not "); |
4b003d19 | 99 | controlreply(sender, "Contact: : %s", tg->contact->content); |
2d4ba67d | 100 | controlreply(sender, "Expires in : %s", (tg->expires>t)?longtoduration(tg->expires - t, 2):"(in the past)"); |
4b003d19 CP |
101 | controlreply(sender, "Last changed by : %s", tg->createdby->content); |
102 | controlreply(sender, "Comment: : %s", tg->comment->content); | |
2d4ba67d | 103 | controlreply(sender, "ID: : %u", tg->id); |
4be1aaf2 CP |
104 | controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":trusts_timetostr(tg->lastseen)); |
105 | controlreply(sender, "Max usage : %d", tg->maxusage); | |
2d4ba67d CP |
106 | controlreply(sender, "Last max reset : %s", tg->lastmaxuserreset?trusts_timetostr(tg->lastmaxuserreset):"(never)"); |
107 | ||
4be1aaf2 | 108 | controlreply(sender, "Host Current Max Last seen"); |
2d4ba67d CP |
109 | |
110 | for(th=tg->hosts;th;th=th->next) | |
4be1aaf2 | 111 | controlreply(sender, " %-20s %-10d %-10d %s", trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (th->count>0)?"(now)":trusts_timetostr(th->lastseen)); |
2d4ba67d CP |
112 | |
113 | controlreply(sender, "End of list."); | |
114 | ||
115 | return CMD_OK; | |
116 | } | |
117 | ||
a99a2041 CP |
118 | static int commandsregistered; |
119 | ||
120 | static void registercommands(int hooknum, void *arg) { | |
121 | if(commandsregistered) | |
122 | return; | |
123 | commandsregistered = 1; | |
124 | ||
125 | registercontrolhelpcmd("trustmigrate", NO_DEVELOPER, 0, trusts_cmdmigrate, "Usage: trustmigrate\nCopies trust data from O and reloads the database."); | |
126 | registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|id>\nShows trust data for the specified trust group."); | |
127 | } | |
128 | ||
129 | static void deregistercommands(void) { | |
130 | if(!commandsregistered) | |
131 | return; | |
132 | commandsregistered = 0; | |
133 | ||
134 | deregistercontrolcmd("trustmigrate", trusts_cmdmigrate); | |
135 | deregistercontrolcmd("trustlist", trusts_cmdtrustlist); | |
be2823bc CP |
136 | } |
137 | ||
138 | void _init(void) { | |
a99a2041 | 139 | registerhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
be2823bc CP |
140 | |
141 | if(trustsdbloaded) | |
a99a2041 | 142 | registercommands(0, NULL); |
be2823bc CP |
143 | } |
144 | ||
145 | void _fini(void) { | |
a99a2041 | 146 | deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
be2823bc CP |
147 | deregistercommands(); |
148 | ||
149 | trusts_migration_stop(); | |
150 | } | |
151 |