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 IRC_SetPenalty(Client, 3);
52 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
57 * Handler for the IRC "OPER" command.
59 * @param Client The client from which this command has been received.
60 * @param Req Request structure with prefix and all parameters.
61 * @return CONNECTED or DISCONNECTED.
64 IRC_OPER( CLIENT *Client, REQUEST *Req )
69 assert( Client != NULL );
70 assert( Req != NULL );
72 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
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 if (!Client_OperByMe(Client))
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 Client_SetOperByMe(Client, true);
102 return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
106 * Handler for the IRC "DIE" command.
108 * @param Client The client from which this command has been received.
109 * @param Req Request structure with prefix and all parameters.
110 * @return CONNECTED or DISCONNECTED.
113 IRC_DIE(CLIENT * Client, REQUEST * Req)
115 /* Shut down server */
120 assert(Client != NULL);
123 if (!Op_Check(Client, Req))
124 return Op_NoPrivileges(Client, Req);
127 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 0)
129 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
132 /* Is a message given? */
136 cl = Conn_GetClient(c);
137 if (Client_Type(cl) == CLIENT_USER)
138 IRC_WriteStrClient(cl, "NOTICE %s :%s",
139 Client_ID(cl), Req->argv[0]);
144 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
145 Client_Mask(Client));
146 NGIRCd_SignalQuit = true;
152 * Handler for the IRC "REHASH" command.
154 * @param Client The client from which this command has been received.
155 * @param Req Request structure with prefix and all parameters.
156 * @return CONNECTED or DISCONNECTED.
159 IRC_REHASH( CLIENT *Client, REQUEST *Req )
161 /* Reload configuration file */
163 assert( Client != NULL );
164 assert( Req != NULL );
166 if (!Op_Check(Client, Req))
167 return Op_NoPrivileges(Client, Req);
169 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 0)
171 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
172 Client_Mask(Client));
173 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
181 * Handler for the IRC "RESTART" command.
183 * @param Client The client from which this command has been received.
184 * @param Req Request structure with prefix and all parameters.
185 * @return CONNECTED or DISCONNECTED.
188 IRC_RESTART( CLIENT *Client, REQUEST *Req )
190 /* Restart IRC server (fork a new process) */
192 assert( Client != NULL );
193 assert( Req != NULL );
195 if (!Op_Check(Client, Req))
196 return Op_NoPrivileges(Client, Req);
198 /* Bad number of parameters? */
200 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
201 Client_ID(Client), Req->command);
203 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
204 Client_Mask(Client));
205 NGIRCd_SignalRestart = true;
211 * Handler for the IRC "CONNECT" command.
213 * @param Client The client from which this command has been received.
214 * @param Req Request structure with prefix and all parameters.
215 * @return CONNECTED or DISCONNECTED.
218 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
220 CLIENT *from, *target;
222 assert(Client != NULL);
225 if (Client_Type(Client) != CLIENT_SERVER
226 && !Client_HasMode(Client, 'o'))
227 return Op_NoPrivileges(Client, Req);
229 /* Bad number of parameters? */
230 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
231 Req->argc != 5 && Req->argc != 6)
232 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
233 Client_ID(Client), Req->command);
235 /* Invalid port number? */
236 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
237 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
238 Client_ID(Client), Req->command);
241 target = Client_ThisServer();
243 if (Req->argc == 3 || Req->argc == 6) {
244 /* This CONNECT has a target parameter */
245 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
246 from = Client_Search(Req->prefix);
248 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
249 Client_ID(Client), Req->prefix);
251 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
252 : Client_Search(Req->argv[5]);
253 if (! target || Client_Type(target) != CLIENT_SERVER)
254 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
255 Client_ID(from), Req->argv[0]);
258 if (target != Client_ThisServer()) {
259 /* Forward CONNECT command ... */
261 IRC_WriteStrClientPrefix(target, from,
262 "CONNECT %s %s :%s", Req->argv[0],
263 Req->argv[1], Req->argv[2]);
265 IRC_WriteStrClientPrefix(target, from,
266 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
267 Req->argv[1], Req->argv[2], Req->argv[3],
268 Req->argv[4], Req->argv[5]);
272 if (!Op_Check(from, Req))
273 return Op_NoPrivileges(Client, Req);
277 if (!Conf_EnablePassiveServer(Req->argv[0]))
278 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
284 /* Connect configured server */
285 if (!Conf_EnableServer
286 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
287 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
294 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
295 Req->argv[3], Req->argv[4]))
296 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
301 Log(LOG_NOTICE | LOG_snotice,
302 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
304 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
305 "Received CONNECT %s from %s",
306 Req->argv[0], Client_ID(from));
312 * Handler for the IRC "DISCONNECT" command.
314 * This command is not specified in the IRC RFCs, it is an extension
315 * of ngIRCd: it shuts down and disables a configured server connection.
317 * @param Client The client from which this command has been received.
318 * @param Req Request structure with prefix and all parameters.
319 * @return CONNECTED or DISCONNECTED.
322 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
326 assert(Client != NULL);
329 if (!Op_Check(Client, Req))
330 return Op_NoPrivileges(Client, Req);
332 /* Bad number of parameters? */
334 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
335 Client_ID(Client), Req->command);
337 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
338 "Received DISCONNECT %s from %s",
339 Req->argv[0], Client_ID(Client));
341 Log(LOG_NOTICE | LOG_snotice,
342 "Got DISCONNECT command from \"%s\" for \"%s\".",
343 Client_Mask(Client), Req->argv[0]);
345 /* Save ID of this connection */
346 my_conn = Client_Conn(Client);
348 /* Disconnect configured server */
349 if (!Conf_DisableServer(Req->argv[0]))
350 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
351 Client_ID(Client), Req->argv[0]);
353 /* Are we still connected or were we killed, too? */
354 if (Conn_GetClient(my_conn))
358 } /* IRC_DISCONNECT */
361 * Handler for the IRC "WALLOPS" command.
363 * @param Client The client from which this command has been received.
364 * @param Req Request structure with prefix and all parameters.
365 * @return CONNECTED or DISCONNECTED.
368 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
372 assert( Client != NULL );
373 assert( Req != NULL );
375 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
377 switch (Client_Type(Client)) {
379 if (!Client_OperByMe(Client))
380 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
385 from = Client_Search(Req->prefix);
392 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
393 Client_ID(Client), Req->prefix);
395 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
400 * Handle <?>LINE commands (GLINE, KLINE).
402 * @param Client The client from which this command has been received.
403 * @param Req Request structure with prefix and all parameters.
404 * @return CONNECTED or DISCONNECTED.
407 IRC_xLINE(CLIENT *Client, REQUEST *Req)
413 assert(Client != NULL);
416 from = Op_Check(Client, Req);
418 return Op_NoPrivileges(Client, Req);
420 /* Bad number of parameters? */
421 if (Req->argc != 1 && Req->argc != 3)
422 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
423 Client_ID(Client), Req->command);
425 switch(Req->command[0]) {
428 class = CLASS_GLINE; class_c = 'G';
432 class = CLASS_KLINE; class_c = 'K';
436 "IRC_xLINE() called for unknown line: %c!? Ignored.",
441 if (Req->argc == 1) {
442 /* Delete mask from list */
443 Class_DeleteMask(class, Req->argv[0]);
444 Log(LOG_NOTICE|LOG_snotice,
445 "\"%s\" deleted \"%s\" from %c-Line list.",
446 Client_Mask(from), Req->argv[0], class_c);
447 if (class == CLASS_GLINE) {
448 /* Inform other servers */
449 IRC_WriteStrServersPrefix(Client, from, "%s %s",
450 Req->command, Req->argv[0]);
454 /* Add new mask to list */
455 if (Class_AddMask(class, Req->argv[0],
456 time(NULL) + atol(Req->argv[1]),
458 Log(LOG_NOTICE|LOG_snotice,
459 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
460 Client_Mask(from), Req->argv[0], class_c,
461 Req->argv[2], atol(Req->argv[1]));
462 if (class == CLASS_GLINE) {
463 /* Inform other servers */
464 IRC_WriteStrServersPrefix(Client, from,
465 "%s %s %s :%s", Req->command,
466 Req->argv[0], Req->argv[1],