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"
33 #include "irc-macros.h"
34 #include "irc-write.h"
45 * Handle invalid received OPER command.
46 * Log OPER attempt and send error message to client.
49 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
51 Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
52 Client_Mask(Client), errtoken, errmsg);
53 /* Increase penalty to slow down possible brute force attacks */
54 IRC_SetPenalty(Client, 10);
55 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
60 * Handler for the IRC "OPER" command.
62 * @param Client The client from which this command has been received.
63 * @param Req Request structure with prefix and all parameters.
64 * @return CONNECTED or DISCONNECTED.
67 IRC_OPER( CLIENT *Client, REQUEST *Req )
72 assert( Client != NULL );
73 assert( Req != NULL );
75 len = array_length(&Conf_Opers, sizeof(*op));
76 op = array_start(&Conf_Opers);
77 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
80 return Bad_OperPass(Client, Req->argv[0], "not configured");
82 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
83 return Bad_OperPass(Client, op[i].name, "bad password");
85 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
86 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
88 if (!Client_HasMode(Client, 'o')) {
89 Client_ModeAdd(Client, 'o');
90 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
93 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
97 Log(LOG_NOTICE|LOG_snotice,
98 "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
99 Req->argv[0], Client_Mask(Client));
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);
125 /* Is a message given? */
129 cl = Conn_GetClient(c);
130 if (Client_Type(cl) == CLIENT_USER)
131 IRC_WriteStrClient(cl, "NOTICE %s :%s",
132 Client_ID(cl), Req->argv[0]);
137 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
138 Client_Mask(Client));
139 NGIRCd_SignalQuit = true;
145 * Handler for the IRC "REHASH" command.
147 * @param Client The client from which this command has been received.
148 * @param Req Request structure with prefix and all parameters.
149 * @return CONNECTED or DISCONNECTED.
152 IRC_REHASH( CLIENT *Client, REQUEST *Req )
154 /* Reload configuration file */
156 assert( Client != NULL );
157 assert( Req != NULL );
159 if (!Op_Check(Client, Req))
160 return Op_NoPrivileges(Client, Req);
162 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
163 Client_Mask(Client));
164 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
172 * Handler for the IRC "RESTART" command.
174 * @param Client The client from which this command has been received.
175 * @param Req Request structure with prefix and all parameters.
176 * @return CONNECTED or DISCONNECTED.
179 IRC_RESTART( CLIENT *Client, REQUEST *Req )
181 /* Restart IRC server (fork a new process) */
183 assert( Client != NULL );
184 assert( Req != NULL );
186 if (!Op_Check(Client, Req))
187 return Op_NoPrivileges(Client, Req);
189 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
190 Client_Mask(Client));
191 NGIRCd_SignalRestart = true;
197 * Handler for the IRC "CONNECT" command.
199 * @param Client The client from which this command has been received.
200 * @param Req Request structure with prefix and all parameters.
201 * @return CONNECTED or DISCONNECTED.
204 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
206 CLIENT *from, *target;
208 assert(Client != NULL);
211 /* Bad number of parameters? */
212 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
213 Req->argc != 5 && Req->argc != 6)
214 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
215 Client_ID(Client), Req->command);
217 /* Invalid port number? */
218 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
219 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
220 Client_ID(Client), Req->command);
222 if (Client_Type(Client) != CLIENT_SERVER
223 && !Client_HasMode(Client, 'o'))
224 return Op_NoPrivileges(Client, Req);
227 target = Client_ThisServer();
229 if (Req->argc == 3 || Req->argc == 6) {
230 /* This CONNECT has a target parameter */
231 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
232 from = Client_Search(Req->prefix);
234 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
235 Client_ID(Client), Req->prefix);
237 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
238 : Client_Search(Req->argv[5]);
239 if (! target || Client_Type(target) != CLIENT_SERVER)
240 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
241 Client_ID(from), Req->argv[0]);
244 if (target != Client_ThisServer()) {
245 /* Forward CONNECT command ... */
247 IRC_WriteStrClientPrefix(target, from,
248 "CONNECT %s %s :%s", Req->argv[0],
249 Req->argv[1], Req->argv[2]);
251 IRC_WriteStrClientPrefix(target, from,
252 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
253 Req->argv[1], Req->argv[2], Req->argv[3],
254 Req->argv[4], Req->argv[5]);
258 if (!Op_Check(from, Req))
259 return Op_NoPrivileges(Client, Req);
263 if (!Conf_EnablePassiveServer(Req->argv[0]))
264 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
270 /* Connect configured server */
271 if (!Conf_EnableServer
272 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
273 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
280 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
281 Req->argv[3], Req->argv[4]))
282 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
287 Log(LOG_NOTICE | LOG_snotice,
288 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
290 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
291 "Received CONNECT %s from %s",
292 Req->argv[0], Client_ID(from));
298 * Handler for the IRC "DISCONNECT" command.
300 * This command is not specified in the IRC RFCs, it is an extension
301 * of ngIRCd: it shuts down and disables a configured server connection.
303 * @param Client The client from which this command has been received.
304 * @param Req Request structure with prefix and all parameters.
305 * @return CONNECTED or DISCONNECTED.
308 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
312 assert(Client != NULL);
315 if (!Op_Check(Client, Req))
316 return Op_NoPrivileges(Client, Req);
318 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
319 "Received DISCONNECT %s from %s",
320 Req->argv[0], Client_ID(Client));
322 Log(LOG_NOTICE | LOG_snotice,
323 "Got DISCONNECT command from \"%s\" for \"%s\".",
324 Client_Mask(Client), Req->argv[0]);
326 /* Save ID of this connection */
327 my_conn = Client_Conn(Client);
329 /* Disconnect configured server */
330 if (!Conf_DisableServer(Req->argv[0]))
331 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
332 Client_ID(Client), Req->argv[0]);
334 /* Are we still connected or were we killed, too? */
335 if (Conn_GetClient(my_conn))
339 } /* IRC_DISCONNECT */
342 * Handler for the IRC "WALLOPS" command.
344 * @param Client The client from which this command has been received.
345 * @param Req Request structure with prefix and all parameters.
346 * @return CONNECTED or DISCONNECTED.
349 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
353 assert( Client != NULL );
354 assert( Req != NULL );
356 switch (Client_Type(Client)) {
358 if (!Op_Check(Client, Req))
359 return Op_NoPrivileges(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);