2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2015 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"
33 #include "irc-macros.h"
34 #include "irc-write.h"
44 * Handle invalid received OPER command.
45 * Log OPER attempt and send error message to client.
48 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
50 Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s!",
51 Client_Mask(Client), errtoken, errmsg);
52 /* Increase penalty to slow down possible brute force attacks */
53 IRC_SetPenalty(Client, 10);
54 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
59 * Handler for the IRC "OPER" command.
61 * @param Client The client from which this command has been received.
62 * @param Req Request structure with prefix and all parameters.
63 * @return CONNECTED or DISCONNECTED.
66 IRC_OPER( CLIENT *Client, REQUEST *Req )
71 assert( Client != NULL );
72 assert( Req != NULL );
74 len = array_length(&Conf_Opers, sizeof(*op));
75 op = array_start(&Conf_Opers);
76 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
79 return Bad_OperPass(Client, Req->argv[0], "not configured");
81 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
82 return Bad_OperPass(Client, op[i].name, "bad password");
84 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
85 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
87 if (!Client_HasMode(Client, 'o')) {
88 Client_ModeAdd(Client, 'o');
89 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
92 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
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 return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
104 * Handler for the IRC "DIE" command.
106 * @param Client The client from which this command has been received.
107 * @param Req Request structure with prefix and all parameters.
108 * @return CONNECTED or DISCONNECTED.
111 IRC_DIE(CLIENT * Client, REQUEST * Req)
113 /* Shut down server */
118 assert(Client != NULL);
121 if (!Op_Check(Client, Req))
122 return Op_NoPrivileges(Client, Req);
124 /* Is a message given? */
128 cl = Conn_GetClient(c);
129 if (Client_Type(cl) == CLIENT_USER)
130 IRC_WriteStrClient(cl, "NOTICE %s :%s",
131 Client_ID(cl), Req->argv[0]);
136 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
137 Client_Mask(Client));
138 NGIRCd_SignalQuit = true;
144 * Handler for the IRC "REHASH" command.
146 * @param Client The client from which this command has been received.
147 * @param Req Request structure with prefix and all parameters.
148 * @return CONNECTED or DISCONNECTED.
151 IRC_REHASH( CLIENT *Client, REQUEST *Req )
153 /* Reload configuration file */
155 assert( Client != NULL );
156 assert( Req != NULL );
158 if (!Op_Check(Client, Req))
159 return Op_NoPrivileges(Client, Req);
161 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
162 Client_Mask(Client));
163 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
171 * Handler for the IRC "RESTART" command.
173 * @param Client The client from which this command has been received.
174 * @param Req Request structure with prefix and all parameters.
175 * @return CONNECTED or DISCONNECTED.
178 IRC_RESTART( CLIENT *Client, REQUEST *Req )
180 /* Restart IRC server (fork a new process) */
182 assert( Client != NULL );
183 assert( Req != NULL );
185 if (!Op_Check(Client, Req))
186 return Op_NoPrivileges(Client, Req);
188 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
189 Client_Mask(Client));
190 NGIRCd_SignalRestart = true;
196 * Handler for the IRC "CONNECT" command.
198 * @param Client The client from which this command has been received.
199 * @param Req Request structure with prefix and all parameters.
200 * @return CONNECTED or DISCONNECTED.
203 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
205 CLIENT *from, *target;
207 assert(Client != NULL);
210 /* Bad number of parameters? */
211 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
212 Req->argc != 5 && Req->argc != 6)
213 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
214 Client_ID(Client), Req->command);
216 /* Invalid port number? */
217 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
218 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
219 Client_ID(Client), Req->command);
221 if (Client_Type(Client) != CLIENT_SERVER
222 && !Client_HasMode(Client, 'o'))
223 return Op_NoPrivileges(Client, Req);
226 target = Client_ThisServer();
228 if (Req->argc == 3 || Req->argc == 6) {
229 /* This CONNECT has a target parameter */
230 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
231 from = Client_Search(Req->prefix);
233 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
234 Client_ID(Client), Req->prefix);
236 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
237 : Client_Search(Req->argv[5]);
238 if (! target || Client_Type(target) != CLIENT_SERVER)
239 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
240 Client_ID(from), Req->argv[0]);
243 if (target != Client_ThisServer()) {
244 /* Forward CONNECT command ... */
246 IRC_WriteStrClientPrefix(target, from,
247 "CONNECT %s %s :%s", Req->argv[0],
248 Req->argv[1], Req->argv[2]);
250 IRC_WriteStrClientPrefix(target, from,
251 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
252 Req->argv[1], Req->argv[2], Req->argv[3],
253 Req->argv[4], Req->argv[5]);
257 if (!Op_Check(from, Req))
258 return Op_NoPrivileges(Client, Req);
262 if (!Conf_EnablePassiveServer(Req->argv[0]))
263 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
269 /* Connect configured server */
270 if (!Conf_EnableServer
271 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
272 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
279 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
280 Req->argv[3], Req->argv[4]))
281 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
286 Log(LOG_NOTICE | LOG_snotice,
287 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
289 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
290 "Received CONNECT %s from %s",
291 Req->argv[0], Client_ID(from));
297 * Handler for the IRC "DISCONNECT" command.
299 * This command is not specified in the IRC RFCs, it is an extension
300 * of ngIRCd: it shuts down and disables a configured server connection.
302 * @param Client The client from which this command has been received.
303 * @param Req Request structure with prefix and all parameters.
304 * @return CONNECTED or DISCONNECTED.
307 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
311 assert(Client != NULL);
314 if (!Op_Check(Client, Req))
315 return Op_NoPrivileges(Client, Req);
317 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
318 "Received DISCONNECT %s from %s",
319 Req->argv[0], Client_ID(Client));
321 Log(LOG_NOTICE | LOG_snotice,
322 "Got DISCONNECT command from \"%s\" for \"%s\".",
323 Client_Mask(Client), Req->argv[0]);
325 /* Save ID of this connection */
326 my_conn = Client_Conn(Client);
328 /* Disconnect configured server */
329 if (!Conf_DisableServer(Req->argv[0]))
330 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
331 Client_ID(Client), Req->argv[0]);
333 /* Are we still connected or were we killed, too? */
334 if (Conn_GetClient(my_conn))
338 } /* IRC_DISCONNECT */
341 * Handler for the IRC "WALLOPS" command.
343 * @param Client The client from which this command has been received.
344 * @param Req Request structure with prefix and all parameters.
345 * @return CONNECTED or DISCONNECTED.
348 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
352 assert( Client != NULL );
353 assert( Req != NULL );
355 switch (Client_Type(Client)) {
357 if (!Op_Check(Client, Req))
358 return Op_NoPrivileges(Client, Req);
362 _IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
363 from = Client_Search(Req->prefix);
370 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
371 Client_ID(Client), Req->prefix);
373 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
378 * Handle <?>LINE commands (GLINE, KLINE).
380 * @param Client The client from which this command has been received.
381 * @param Req Request structure with prefix and all parameters.
382 * @return CONNECTED or DISCONNECTED.
385 IRC_xLINE(CLIENT *Client, REQUEST *Req)
387 CLIENT *from, *c, *c_next;
388 char reason[COMMAND_LEN], class_c;
389 struct list_head *list;
393 assert(Client != NULL);
396 /* Bad number of parameters? */
397 if (Req->argc != 1 && Req->argc != 3)
398 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
399 Client_ID(Client), Req->command);
401 from = Op_Check(Client, Req);
403 return Op_NoPrivileges(Client, Req);
405 switch(Req->command[0]) {
408 class = CLASS_GLINE; class_c = 'G';
412 class = CLASS_KLINE; class_c = 'K';
416 "IRC_xLINE() called for unknown line: %c!? Ignored.",
421 if (Req->argc == 1) {
422 /* Delete mask from list */
423 Class_DeleteMask(class, Req->argv[0]);
424 Log(LOG_NOTICE|LOG_snotice,
425 "\"%s\" deleted \"%s\" from %c-Line list.",
426 Client_Mask(from), Req->argv[0], class_c);
427 if (class == CLASS_GLINE) {
428 /* Inform other servers */
429 IRC_WriteStrServersPrefix(Client, from, "%s %s",
430 Req->command, Req->argv[0]);
434 /* Add new mask to list */
435 timeout = atol(Req->argv[1]);
437 timeout += time(NULL);
438 if (Class_AddMask(class, Req->argv[0],
441 Log(LOG_NOTICE|LOG_snotice,
442 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
443 Client_Mask(from), Req->argv[0], class_c,
444 Req->argv[2], atol(Req->argv[1]));
445 if (class == CLASS_GLINE) {
446 /* Inform other servers */
447 IRC_WriteStrServersPrefix(Client, from,
448 "%s %s %s :%s", Req->command,
449 Req->argv[0], Req->argv[1],
453 /* Check currently connected clients */
454 snprintf(reason, sizeof(reason), "%c-Line by \"%s\": \"%s\"",
455 class_c, Client_ID(from), Req->argv[2]);
456 list = Class_GetList(class);
459 c_next = Client_Next(c);
460 if ((class == CLASS_GLINE || Client_Conn(c) > NONE)
461 && Lists_Check(list, c))
462 IRC_KillClient(Client, NULL,
463 Client_ID(c), reason);