2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * IRC operator commands
27 #include "conn-func.h"
31 #include "irc-macros.h"
32 #include "irc-write.h"
43 * Handle invalid received OPER command.
44 * Log OPER attempt and send error message to client.
47 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
49 Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
50 Client_Mask(Client), errtoken, errmsg);
51 return IRC_WriteErrClient(Client, ERR_PASSWDMISMATCH_MSG,
56 * Handler for the IRC "OPER" command.
58 * @param Client The client from which this command has been received.
59 * @param Req Request structure with prefix and all parameters.
60 * @return CONNECTED or DISCONNECTED.
63 IRC_OPER( CLIENT *Client, REQUEST *Req )
68 assert( Client != NULL );
69 assert( Req != NULL );
71 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
73 len = array_length(&Conf_Opers, sizeof(*op));
74 op = array_start(&Conf_Opers);
75 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
78 return Bad_OperPass(Client, Req->argv[0], "not configured");
80 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
81 return Bad_OperPass(Client, op[i].name, "bad password");
83 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
84 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
86 if (!Client_HasMode(Client, 'o')) {
87 Client_ModeAdd(Client, 'o');
88 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
91 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
95 if (!Client_OperByMe(Client))
96 Log(LOG_NOTICE|LOG_snotice,
97 "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
98 Req->argv[0], Client_Mask(Client));
100 Client_SetOperByMe(Client, true);
101 return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
105 * Handler for the IRC "DIE" command.
107 * @param Client The client from which this command has been received.
108 * @param Req Request structure with prefix and all parameters.
109 * @return CONNECTED or DISCONNECTED.
112 IRC_DIE(CLIENT * Client, REQUEST * Req)
114 /* Shut down server */
119 assert(Client != NULL);
122 if (!Op_Check(Client, Req))
123 return Op_NoPrivileges(Client, Req);
126 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 0)
128 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
131 /* Is a message given? */
135 cl = Conn_GetClient(c);
136 if (Client_Type(cl) == CLIENT_USER)
137 IRC_WriteStrClient(cl, "NOTICE %s :%s",
138 Client_ID(cl), Req->argv[0]);
143 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
144 Client_Mask(Client));
145 NGIRCd_SignalQuit = true;
151 * Handler for the IRC "REHASH" command.
153 * @param Client The client from which this command has been received.
154 * @param Req Request structure with prefix and all parameters.
155 * @return CONNECTED or DISCONNECTED.
158 IRC_REHASH( CLIENT *Client, REQUEST *Req )
160 /* Reload configuration file */
162 assert( Client != NULL );
163 assert( Req != NULL );
165 if (!Op_Check(Client, Req))
166 return Op_NoPrivileges(Client, Req);
168 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 0)
170 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
171 Client_Mask(Client));
172 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
180 * Handler for the IRC "RESTART" command.
182 * @param Client The client from which this command has been received.
183 * @param Req Request structure with prefix and all parameters.
184 * @return CONNECTED or DISCONNECTED.
187 IRC_RESTART( CLIENT *Client, REQUEST *Req )
189 /* Restart IRC server (fork a new process) */
191 assert( Client != NULL );
192 assert( Req != NULL );
194 if (!Op_Check(Client, Req))
195 return Op_NoPrivileges(Client, Req);
197 /* Bad number of parameters? */
199 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
200 Client_ID(Client), Req->command);
202 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
203 Client_Mask(Client));
204 NGIRCd_SignalRestart = true;
210 * Handler for the IRC "CONNECT" command.
212 * @param Client The client from which this command has been received.
213 * @param Req Request structure with prefix and all parameters.
214 * @return CONNECTED or DISCONNECTED.
217 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
219 CLIENT *from, *target;
221 assert(Client != NULL);
224 if (Client_Type(Client) != CLIENT_SERVER
225 && !Client_HasMode(Client, 'o'))
226 return Op_NoPrivileges(Client, Req);
228 /* Bad number of parameters? */
229 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
230 Req->argc != 5 && Req->argc != 6)
231 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
232 Client_ID(Client), Req->command);
234 /* Invalid port number? */
235 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
236 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
237 Client_ID(Client), Req->command);
240 target = Client_ThisServer();
242 if (Req->argc == 3 || Req->argc == 6) {
243 /* This CONNECT has a target parameter */
244 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
245 from = Client_Search(Req->prefix);
247 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
248 Client_ID(Client), Req->prefix);
250 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
251 : Client_Search(Req->argv[5]);
252 if (! target || Client_Type(target) != CLIENT_SERVER)
253 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
254 Client_ID(from), Req->argv[0]);
257 if (target != Client_ThisServer()) {
258 /* Forward CONNECT command ... */
260 IRC_WriteStrClientPrefix(target, from,
261 "CONNECT %s %s :%s", Req->argv[0],
262 Req->argv[1], Req->argv[2]);
264 IRC_WriteStrClientPrefix(target, from,
265 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
266 Req->argv[1], Req->argv[2], Req->argv[3],
267 Req->argv[4], Req->argv[5]);
271 if (!Op_Check(from, Req))
272 return Op_NoPrivileges(Client, Req);
276 if (!Conf_EnablePassiveServer(Req->argv[0]))
277 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
283 /* Connect configured server */
284 if (!Conf_EnableServer
285 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
286 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
293 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
294 Req->argv[3], Req->argv[4]))
295 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
300 Log(LOG_NOTICE | LOG_snotice,
301 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
303 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
304 "Received CONNECT %s from %s",
305 Req->argv[0], Client_ID(from));
311 * Handler for the IRC "DISCONNECT" command.
313 * This command is not specified in the IRC RFCs, it is an extension
314 * of ngIRCd: it shuts down and disables a configured server connection.
316 * @param Client The client from which this command has been received.
317 * @param Req Request structure with prefix and all parameters.
318 * @return CONNECTED or DISCONNECTED.
321 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
325 assert(Client != NULL);
328 if (!Op_Check(Client, Req))
329 return Op_NoPrivileges(Client, Req);
331 /* Bad number of parameters? */
333 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
334 Client_ID(Client), Req->command);
336 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
337 "Received DISCONNECT %s from %s",
338 Req->argv[0], Client_ID(Client));
340 Log(LOG_NOTICE | LOG_snotice,
341 "Got DISCONNECT command from \"%s\" for \"%s\".",
342 Client_Mask(Client), Req->argv[0]);
344 /* Save ID of this connection */
345 my_conn = Client_Conn(Client);
347 /* Disconnect configured server */
348 if (!Conf_DisableServer(Req->argv[0]))
349 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
350 Client_ID(Client), Req->argv[0]);
352 /* Are we still connected or were we killed, too? */
353 if (Conn_GetClient(my_conn))
357 } /* IRC_DISCONNECT */
360 * Handler for the IRC "WALLOPS" command.
362 * @param Client The client from which this command has been received.
363 * @param Req Request structure with prefix and all parameters.
364 * @return CONNECTED or DISCONNECTED.
367 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
371 assert( Client != NULL );
372 assert( Req != NULL );
374 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
376 switch (Client_Type(Client)) {
378 if (!Client_OperByMe(Client))
379 return IRC_WriteErrClient(Client, ERR_NOPRIVILEGES_MSG,
384 from = Client_Search(Req->prefix);
391 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
392 Client_ID(Client), Req->prefix);
394 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
399 * Handle <?>LINE commands (GLINE, KLINE).
401 * @param Client The client from which this command has been received.
402 * @param Req Request structure with prefix and all parameters.
403 * @return CONNECTED or DISCONNECTED.
406 IRC_xLINE(CLIENT *Client, REQUEST *Req)
412 assert(Client != NULL);
415 from = Op_Check(Client, Req);
417 return Op_NoPrivileges(Client, Req);
419 /* Bad number of parameters? */
420 if (Req->argc != 1 && Req->argc != 3)
421 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
422 Client_ID(Client), Req->command);
424 switch(Req->command[0]) {
427 class = CLASS_GLINE; class_c = 'G';
431 class = CLASS_KLINE; class_c = 'K';
435 "IRC_xLINE() called for unknown line: %c!? Ignored.",
440 if (Req->argc == 1) {
441 /* Delete mask from list */
442 Class_DeleteMask(class, Req->argv[0]);
443 Log(LOG_NOTICE|LOG_snotice,
444 "\"%s\" deleted \"%s\" from %c-Line list.",
445 Client_Mask(from), Req->argv[0], class_c);
446 if (class == CLASS_GLINE) {
447 /* Inform other servers */
448 IRC_WriteStrServersPrefix(Client, from, "%s %s",
449 Req->command, Req->argv[0]);
453 /* Add new mask to list */
454 if (Class_AddMask(class, Req->argv[0],
455 time(NULL) + atol(Req->argv[1]),
457 Log(LOG_NOTICE|LOG_snotice,
458 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
459 Client_Mask(from), Req->argv[0], class_c,
460 Req->argv[2], atol(Req->argv[1]));
461 if (class == CLASS_GLINE) {
462 /* Inform other servers */
463 IRC_WriteStrServersPrefix(Client, from,
464 "%s %s %s :%s", Req->command,
465 Req->argv[0], Req->argv[1],