2 * Written by David Herrmann.
3 * Dedicated to the Public Domain.
13 char iauth_servname[IAUTH_DATALEN + 1];
15 static char *iauth_next_data(char **arg) {
19 while(**arg == ' ') ++*arg;
20 if(!**arg) return NULL;
21 if(**arg == ':' || !(sep = strchr(*arg, ' '))) {
22 if(**arg == ':') ++*arg;
24 ret = iauth_malloc(len + 1);
27 if(len > IAUTH_DATALEN) ret[IAUTH_DATALEN] = 0;
34 ret = iauth_malloc(len + 1);
36 if(len > IAUTH_DATALEN) ret[IAUTH_DATALEN] = 0;
44 /* Client Introduction: <id> C <remoteip> <remoteport> <localip> <localport> */
45 void iauth_cmd_C(signed int id, char *arg) {
47 struct iauth_client *cli;
51 cli = &iauth_clients[id];
53 str = iauth_next_data(&arg);
54 if(!str) goto parse_error;
55 iauth_set(cli->ip, str);
57 str = iauth_next_data(&arg);
58 if(!str) goto parse_error;
59 cli->port = atoi(str);
62 str = iauth_next_data(&arg);
63 if(!str) goto parse_error;
64 iauth_set(cli->lo_ip, str);
66 str = iauth_next_data(&arg);
67 if(!str) goto parse_error;
68 cli->lo_port = atoi(str);
71 iauth_flog(IAUTH_INFO, "New client (%d) from '%s':%hu to '%s':%hu.", id, cli->ip, cli->port, cli->lo_ip, cli->lo_port);
75 iauth_flog(IAUTH_WARNING, "Parse error: Invalid C line.");
80 /* Client Disconnect: <id> D */
81 void iauth_cmd_D(struct iauth_client *client) {
82 iauth_delid(client->id);
85 /* Login On Connect: <id> L <account>[:<accountstamp>][ <fakehost>] */
86 void iauth_cmd_L(struct iauth_client *client, char *arg) {
88 const struct iauth_result *res;
90 str = iauth_next_data(&arg);
91 if(!str) goto parse_error;
92 iauth_set(client->account, str);
94 str = iauth_next_data(&arg);
95 if(str) iauth_set(client->fakehost, str);
97 /* Query database for account query. */
98 res = iauth_query(client);
100 iauth_query_reject(client->id, "Access denied.");
101 iauth_stats_loc_deny();
104 if(strcmp(res->cclass, "error") == 0) {
105 iauth_query_reject(client->id, res->str);
106 iauth_stats_loc_deny();
108 if(*res->ident) iauth_query_set_username(client->id, res->ident);
109 if(*res->host) iauth_query_sethost(client->id, res->host);
110 if(*res->ip) iauth_query_setip(client->id, res->ip);
111 if(*res->modes) iauth_query_setmodes(client->id, res->modes);
112 iauth_query_assign(client->id, (*res->cclass)?res->cclass:NULL);
113 iauth_stats_loc_allow();
117 iauth_delid(client->id);
121 iauth_flog(IAUTH_WARNING, "Parse error: Invalid L line.");
125 /* Hurry Up: <id> H <class> */
126 void iauth_cmd_H(struct iauth_client *client, char *arg) {
128 const struct iauth_result *res;
130 str = iauth_next_data(&arg);
131 if(!str) goto parse_error;
132 iauth_set(client->cclass, str);
135 /* Query database for account query. */
136 res = iauth_query(client);
138 iauth_query_reject(client->id, "Access denied.");
139 iauth_stats_def_deny();
142 if(strcmp(res->cclass, "error") == 0) {
143 iauth_query_reject(client->id, res->str);
144 iauth_stats_loc_deny();
146 if(*res->ident) iauth_query_set_username(client->id, res->ident);
147 if(*res->host) iauth_query_sethost(client->id, res->host);
148 if(*res->ip) iauth_query_setip(client->id, res->ip);
149 if(*res->modes) iauth_query_setmodes(client->id, res->modes);
150 iauth_query_assign(client->id, (*res->cclass)?res->cclass:NULL);
151 iauth_stats_def_allow();
154 iauth_delid(client->id);
158 iauth_flog(IAUTH_WARNING, "Parse error: Invalid H line.");
162 /* Server Name and Capacity: <id> M <servername> <capacity> */
163 void iauth_cmd_M(char *arg) {
164 char *server = NULL, *str = NULL;
167 server = iauth_next_data(&arg);
168 if(!server) goto parse_error;
170 str = iauth_next_data(&arg);
171 if(!str || !*str || (cap = atoi(str)) == 0) goto parse_error;
173 iauth_flog(IAUTH_INFO, "Setting server (%s) capacity to: %u.", server, cap);
174 strcpy(iauth_servname, server);
180 iauth_flog(IAUTH_WARNING, "Parse error: Invalid M line.");
186 /* Hostname Received: <id> N <hostname> */
187 void iauth_cmd_N(struct iauth_client *client, char *arg) {
190 str = iauth_next_data(&arg);
191 if(!str) goto parse_error;
192 iauth_set(client->host, str);
196 iauth_flog(IAUTH_WARNING, "Parse error: Invalid N line.");
200 /* Hostname Timeout: <id> d */
201 void iauth_cmd_d(struct iauth_client *client) {
204 str = iauth_malloc(1);
205 iauth_set(client->host, str);
210 /* Client Password: <id> P :<password ...> */
211 void iauth_cmd_P(struct iauth_client *client, char *arg) {
214 str = iauth_next_data(&arg);
215 if(!str) goto parse_error;
216 iauth_set(client->password, str);
220 iauth_flog(IAUTH_WARNING, "Parse error: Invalid P line.");
224 /* Client Username: <id> U <username> <hostname> <servername> :<userinfo ...> */
225 void iauth_cmd_U(struct iauth_client *client, char *arg) {
228 str = iauth_next_data(&arg);
229 if(!str) goto parse_error;
230 iauth_set(client->username, str);
232 str = iauth_next_data(&arg);
233 if(!str) goto parse_error;
234 iauth_set(client->c_host, str);
236 str = iauth_next_data(&arg);
237 if(!str) goto parse_error;
238 iauth_set(client->c_serv, str);
240 str = iauth_next_data(&arg);
241 if(!str) goto parse_error;
242 iauth_set(client->realname, str);
246 iauth_flog(IAUTH_WARNING, "Parse error: Invalid U line.");
250 /* Client Username: <id> u <username> */
251 void iauth_cmd_u(struct iauth_client *client, char *arg) {
254 str = iauth_next_data(&arg);
255 if(!str) goto parse_error;
256 iauth_set(client->ident, str);
260 iauth_flog(IAUTH_WARNING, "Parse error: Invalid u line.");
264 /* Client Nickname: <id> n <nickname> */
265 void iauth_cmd_n(struct iauth_client *client, char *arg) {
268 str = iauth_next_data(&arg);
269 if(!str) goto parse_error;
270 iauth_set(client->nick, str);
274 iauth_flog(IAUTH_WARNING, "Parse error: Invalid n line.");
278 /* Client Registered: <id> T */
279 void iauth_cmd_T(struct iauth_client *client) {
280 iauth_flog(IAUTH_INFO, "Client %d was registered without IAuth answer.", client->id);
281 iauth_delid(client->id);
284 /* Error: <id> E <type> :<additional text> */
285 void iauth_cmd_E(signed int id, char *arg) {
286 iauth_flog(IAUTH_WARNING, "Received IRCd error: %s", arg);