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 len = array_length(&Conf_Opers, sizeof(*op));
72 op = array_start(&Conf_Opers);
73 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
76 return Bad_OperPass(Client, Req->argv[0], "not configured");
78 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
79 return Bad_OperPass(Client, op[i].name, "bad password");
81 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
82 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
84 if (!Client_HasMode(Client, 'o')) {
85 Client_ModeAdd(Client, 'o');
86 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
89 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
93 Log(LOG_NOTICE|LOG_snotice,
94 "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
95 Req->argv[0], Client_Mask(Client));
97 return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
101 * Handler for the IRC "DIE" command.
103 * @param Client The client from which this command has been received.
104 * @param Req Request structure with prefix and all parameters.
105 * @return CONNECTED or DISCONNECTED.
108 IRC_DIE(CLIENT * Client, REQUEST * Req)
110 /* Shut down server */
115 assert(Client != NULL);
118 if (!Op_Check(Client, Req))
119 return Op_NoPrivileges(Client, Req);
121 /* Is a message given? */
125 cl = Conn_GetClient(c);
126 if (Client_Type(cl) == CLIENT_USER)
127 IRC_WriteStrClient(cl, "NOTICE %s :%s",
128 Client_ID(cl), Req->argv[0]);
133 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
134 Client_Mask(Client));
135 NGIRCd_SignalQuit = true;
141 * Handler for the IRC "REHASH" command.
143 * @param Client The client from which this command has been received.
144 * @param Req Request structure with prefix and all parameters.
145 * @return CONNECTED or DISCONNECTED.
148 IRC_REHASH( CLIENT *Client, REQUEST *Req )
150 /* Reload configuration file */
152 assert( Client != NULL );
153 assert( Req != NULL );
155 if (!Op_Check(Client, Req))
156 return Op_NoPrivileges(Client, Req);
158 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
159 Client_Mask(Client));
160 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
168 * Handler for the IRC "RESTART" command.
170 * @param Client The client from which this command has been received.
171 * @param Req Request structure with prefix and all parameters.
172 * @return CONNECTED or DISCONNECTED.
175 IRC_RESTART( CLIENT *Client, REQUEST *Req )
177 /* Restart IRC server (fork a new process) */
179 assert( Client != NULL );
180 assert( Req != NULL );
182 if (!Op_Check(Client, Req))
183 return Op_NoPrivileges(Client, Req);
185 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
186 Client_Mask(Client));
187 NGIRCd_SignalRestart = true;
193 * Handler for the IRC "CONNECT" command.
195 * @param Client The client from which this command has been received.
196 * @param Req Request structure with prefix and all parameters.
197 * @return CONNECTED or DISCONNECTED.
200 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
202 CLIENT *from, *target;
204 assert(Client != NULL);
207 /* Bad number of parameters? */
208 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
209 Req->argc != 5 && Req->argc != 6) {
210 IRC_SetPenalty(Client, 2);
211 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
212 Client_ID(Client), Req->command);
215 /* Invalid port number? */
216 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1) {
217 IRC_SetPenalty(Client, 2);
218 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
219 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)
391 assert(Client != NULL);
394 /* Bad number of parameters? */
395 if (Req->argc != 1 && Req->argc != 3) {
396 IRC_SetPenalty(Client, 2);
397 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
398 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 if (Class_AddMask(class, Req->argv[0],
436 time(NULL) + atol(Req->argv[1]),
438 Log(LOG_NOTICE|LOG_snotice,
439 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
440 Client_Mask(from), Req->argv[0], class_c,
441 Req->argv[2], atol(Req->argv[1]));
442 if (class == CLASS_GLINE) {
443 /* Inform other servers */
444 IRC_WriteStrServersPrefix(Client, from,
445 "%s %s %s :%s", Req->command,
446 Req->argv[0], Req->argv[1],