2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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-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 /* Increase penalty to slow down possible brute force attacks */
52 IRC_SetPenalty(Client, 10);
53 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
58 * Handler for the IRC "OPER" command.
60 * @param Client The client from which this command has been received.
61 * @param Req Request structure with prefix and all parameters.
62 * @return CONNECTED or DISCONNECTED.
65 IRC_OPER( CLIENT *Client, REQUEST *Req )
70 assert( Client != NULL );
71 assert( Req != NULL );
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 Log(LOG_NOTICE|LOG_snotice,
96 "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
97 Req->argv[0], Client_Mask(Client));
99 return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
103 * Handler for the IRC "DIE" command.
105 * @param Client The client from which this command has been received.
106 * @param Req Request structure with prefix and all parameters.
107 * @return CONNECTED or DISCONNECTED.
110 IRC_DIE(CLIENT * Client, REQUEST * Req)
112 /* Shut down server */
117 assert(Client != NULL);
120 if (!Op_Check(Client, Req))
121 return Op_NoPrivileges(Client, Req);
123 /* Is a message given? */
127 cl = Conn_GetClient(c);
128 if (Client_Type(cl) == CLIENT_USER)
129 IRC_WriteStrClient(cl, "NOTICE %s :%s",
130 Client_ID(cl), Req->argv[0]);
135 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
136 Client_Mask(Client));
137 NGIRCd_SignalQuit = true;
143 * Handler for the IRC "REHASH" command.
145 * @param Client The client from which this command has been received.
146 * @param Req Request structure with prefix and all parameters.
147 * @return CONNECTED or DISCONNECTED.
150 IRC_REHASH( CLIENT *Client, REQUEST *Req )
152 /* Reload configuration file */
154 assert( Client != NULL );
155 assert( Req != NULL );
157 if (!Op_Check(Client, Req))
158 return Op_NoPrivileges(Client, Req);
160 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
161 Client_Mask(Client));
162 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
170 * Handler for the IRC "RESTART" command.
172 * @param Client The client from which this command has been received.
173 * @param Req Request structure with prefix and all parameters.
174 * @return CONNECTED or DISCONNECTED.
177 IRC_RESTART( CLIENT *Client, REQUEST *Req )
179 /* Restart IRC server (fork a new process) */
181 assert( Client != NULL );
182 assert( Req != NULL );
184 if (!Op_Check(Client, Req))
185 return Op_NoPrivileges(Client, Req);
187 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
188 Client_Mask(Client));
189 NGIRCd_SignalRestart = true;
195 * Handler for the IRC "CONNECT" command.
197 * @param Client The client from which this command has been received.
198 * @param Req Request structure with prefix and all parameters.
199 * @return CONNECTED or DISCONNECTED.
202 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
204 CLIENT *from, *target;
206 assert(Client != NULL);
209 /* Bad number of parameters? */
210 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
211 Req->argc != 5 && Req->argc != 6)
212 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
213 Client_ID(Client), Req->command);
215 /* Invalid port number? */
216 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
217 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
218 Client_ID(Client), Req->command);
220 if (Client_Type(Client) != CLIENT_SERVER
221 && !Client_HasMode(Client, 'o'))
222 return Op_NoPrivileges(Client, Req);
225 target = Client_ThisServer();
227 if (Req->argc == 3 || Req->argc == 6) {
228 /* This CONNECT has a target parameter */
229 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
230 from = Client_Search(Req->prefix);
232 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
233 Client_ID(Client), Req->prefix);
235 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
236 : Client_Search(Req->argv[5]);
237 if (! target || Client_Type(target) != CLIENT_SERVER)
238 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
239 Client_ID(from), Req->argv[0]);
242 if (target != Client_ThisServer()) {
243 /* Forward CONNECT command ... */
245 IRC_WriteStrClientPrefix(target, from,
246 "CONNECT %s %s :%s", Req->argv[0],
247 Req->argv[1], Req->argv[2]);
249 IRC_WriteStrClientPrefix(target, from,
250 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
251 Req->argv[1], Req->argv[2], Req->argv[3],
252 Req->argv[4], Req->argv[5]);
256 if (!Op_Check(from, Req))
257 return Op_NoPrivileges(Client, Req);
261 if (!Conf_EnablePassiveServer(Req->argv[0]))
262 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
268 /* Connect configured server */
269 if (!Conf_EnableServer
270 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
271 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
278 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
279 Req->argv[3], Req->argv[4]))
280 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
285 Log(LOG_NOTICE | LOG_snotice,
286 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
288 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
289 "Received CONNECT %s from %s",
290 Req->argv[0], Client_ID(from));
296 * Handler for the IRC "DISCONNECT" command.
298 * This command is not specified in the IRC RFCs, it is an extension
299 * of ngIRCd: it shuts down and disables a configured server connection.
301 * @param Client The client from which this command has been received.
302 * @param Req Request structure with prefix and all parameters.
303 * @return CONNECTED or DISCONNECTED.
306 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
310 assert(Client != NULL);
313 if (!Op_Check(Client, Req))
314 return Op_NoPrivileges(Client, Req);
316 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
317 "Received DISCONNECT %s from %s",
318 Req->argv[0], Client_ID(Client));
320 Log(LOG_NOTICE | LOG_snotice,
321 "Got DISCONNECT command from \"%s\" for \"%s\".",
322 Client_Mask(Client), Req->argv[0]);
324 /* Save ID of this connection */
325 my_conn = Client_Conn(Client);
327 /* Disconnect configured server */
328 if (!Conf_DisableServer(Req->argv[0]))
329 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
330 Client_ID(Client), Req->argv[0]);
332 /* Are we still connected or were we killed, too? */
333 if (Conn_GetClient(my_conn))
337 } /* IRC_DISCONNECT */
340 * Handler for the IRC "WALLOPS" command.
342 * @param Client The client from which this command has been received.
343 * @param Req Request structure with prefix and all parameters.
344 * @return CONNECTED or DISCONNECTED.
347 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
351 assert( Client != NULL );
352 assert( Req != NULL );
354 switch (Client_Type(Client)) {
356 if (!Op_Check(Client, Req))
357 return Op_NoPrivileges(Client, Req);
361 from = Client_Search(Req->prefix);
368 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
369 Client_ID(Client), Req->prefix);
371 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
376 * Handle <?>LINE commands (GLINE, KLINE).
378 * @param Client The client from which this command has been received.
379 * @param Req Request structure with prefix and all parameters.
380 * @return CONNECTED or DISCONNECTED.
383 IRC_xLINE(CLIENT *Client, REQUEST *Req)
385 CLIENT *from, *c, *c_next;
386 char reason[COMMAND_LEN], class_c;
387 struct list_head *list;
391 assert(Client != NULL);
394 /* Bad number of parameters? */
395 if (Req->argc != 1 && Req->argc != 3)
396 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
397 Client_ID(Client), Req->command);
399 from = Op_Check(Client, Req);
401 return Op_NoPrivileges(Client, Req);
403 switch(Req->command[0]) {
406 class = CLASS_GLINE; class_c = 'G';
410 class = CLASS_KLINE; class_c = 'K';
414 "IRC_xLINE() called for unknown line: %c!? Ignored.",
419 if (Req->argc == 1) {
420 /* Delete mask from list */
421 Class_DeleteMask(class, Req->argv[0]);
422 Log(LOG_NOTICE|LOG_snotice,
423 "\"%s\" deleted \"%s\" from %c-Line list.",
424 Client_Mask(from), Req->argv[0], class_c);
425 if (class == CLASS_GLINE) {
426 /* Inform other servers */
427 IRC_WriteStrServersPrefix(Client, from, "%s %s",
428 Req->command, Req->argv[0]);
432 /* Add new mask to list */
433 timeout = atol(Req->argv[1]);
435 timeout += time(NULL);
436 if (Class_AddMask(class, Req->argv[0],
439 Log(LOG_NOTICE|LOG_snotice,
440 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
441 Client_Mask(from), Req->argv[0], class_c,
442 Req->argv[2], atol(Req->argv[1]));
443 if (class == CLASS_GLINE) {
444 /* Inform other servers */
445 IRC_WriteStrServersPrefix(Client, from,
446 "%s %s %s :%s", Req->command,
447 Req->argv[0], Req->argv[1],
451 /* Check currently connected clients */
452 snprintf(reason, sizeof(reason), "%c-Line by \"%s\": \"%s\"",
453 class_c, Client_ID(from), Req->argv[2]);
454 list = Class_GetList(class);
457 c_next = Client_Next(c);
458 if ((class == CLASS_GLINE || Client_Conn(c) > NONE)
459 && Lists_Check(list, c))
460 IRC_KillClient(Client, NULL,
461 Client_ID(c), reason);