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 if (!Client_OperByMe(Client))
94 Log(LOG_NOTICE|LOG_snotice,
95 "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
96 Req->argv[0], Client_Mask(Client));
98 Client_SetOperByMe(Client, true);
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 IRC_SetPenalty(Client, 2);
213 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
214 Client_ID(Client), Req->command);
217 /* Invalid port number? */
218 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1) {
219 IRC_SetPenalty(Client, 2);
220 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
221 Client_ID(Client), Req->command);
224 if (Client_Type(Client) != CLIENT_SERVER
225 && !Client_HasMode(Client, 'o'))
226 return Op_NoPrivileges(Client, Req);
229 target = Client_ThisServer();
231 if (Req->argc == 3 || Req->argc == 6) {
232 /* This CONNECT has a target parameter */
233 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
234 from = Client_Search(Req->prefix);
236 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
237 Client_ID(Client), Req->prefix);
239 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
240 : Client_Search(Req->argv[5]);
241 if (! target || Client_Type(target) != CLIENT_SERVER)
242 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
243 Client_ID(from), Req->argv[0]);
246 if (target != Client_ThisServer()) {
247 /* Forward CONNECT command ... */
249 IRC_WriteStrClientPrefix(target, from,
250 "CONNECT %s %s :%s", Req->argv[0],
251 Req->argv[1], Req->argv[2]);
253 IRC_WriteStrClientPrefix(target, from,
254 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
255 Req->argv[1], Req->argv[2], Req->argv[3],
256 Req->argv[4], Req->argv[5]);
260 if (!Op_Check(from, Req))
261 return Op_NoPrivileges(Client, Req);
265 if (!Conf_EnablePassiveServer(Req->argv[0]))
266 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
272 /* Connect configured server */
273 if (!Conf_EnableServer
274 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
275 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
282 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
283 Req->argv[3], Req->argv[4]))
284 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
289 Log(LOG_NOTICE | LOG_snotice,
290 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
292 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
293 "Received CONNECT %s from %s",
294 Req->argv[0], Client_ID(from));
300 * Handler for the IRC "DISCONNECT" command.
302 * This command is not specified in the IRC RFCs, it is an extension
303 * of ngIRCd: it shuts down and disables a configured server connection.
305 * @param Client The client from which this command has been received.
306 * @param Req Request structure with prefix and all parameters.
307 * @return CONNECTED or DISCONNECTED.
310 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
314 assert(Client != NULL);
317 if (!Op_Check(Client, Req))
318 return Op_NoPrivileges(Client, Req);
320 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
321 "Received DISCONNECT %s from %s",
322 Req->argv[0], Client_ID(Client));
324 Log(LOG_NOTICE | LOG_snotice,
325 "Got DISCONNECT command from \"%s\" for \"%s\".",
326 Client_Mask(Client), Req->argv[0]);
328 /* Save ID of this connection */
329 my_conn = Client_Conn(Client);
331 /* Disconnect configured server */
332 if (!Conf_DisableServer(Req->argv[0]))
333 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
334 Client_ID(Client), Req->argv[0]);
336 /* Are we still connected or were we killed, too? */
337 if (Conn_GetClient(my_conn))
341 } /* IRC_DISCONNECT */
344 * Handler for the IRC "WALLOPS" command.
346 * @param Client The client from which this command has been received.
347 * @param Req Request structure with prefix and all parameters.
348 * @return CONNECTED or DISCONNECTED.
351 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
355 assert( Client != NULL );
356 assert( Req != NULL );
358 switch (Client_Type(Client)) {
360 if (!Client_OperByMe(Client))
361 return IRC_WriteErrClient(Client, ERR_NOPRIVILEGES_MSG,
366 from = Client_Search(Req->prefix);
373 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
374 Client_ID(Client), Req->prefix);
376 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
381 * Handle <?>LINE commands (GLINE, KLINE).
383 * @param Client The client from which this command has been received.
384 * @param Req Request structure with prefix and all parameters.
385 * @return CONNECTED or DISCONNECTED.
388 IRC_xLINE(CLIENT *Client, REQUEST *Req)
394 assert(Client != NULL);
397 /* Bad number of parameters? */
398 if (Req->argc != 1 && Req->argc != 3) {
399 IRC_SetPenalty(Client, 2);
400 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
401 Client_ID(Client), Req->command);
404 from = Op_Check(Client, Req);
406 return Op_NoPrivileges(Client, Req);
408 switch(Req->command[0]) {
411 class = CLASS_GLINE; class_c = 'G';
415 class = CLASS_KLINE; class_c = 'K';
419 "IRC_xLINE() called for unknown line: %c!? Ignored.",
424 if (Req->argc == 1) {
425 /* Delete mask from list */
426 Class_DeleteMask(class, Req->argv[0]);
427 Log(LOG_NOTICE|LOG_snotice,
428 "\"%s\" deleted \"%s\" from %c-Line list.",
429 Client_Mask(from), Req->argv[0], class_c);
430 if (class == CLASS_GLINE) {
431 /* Inform other servers */
432 IRC_WriteStrServersPrefix(Client, from, "%s %s",
433 Req->command, Req->argv[0]);
437 /* Add new mask to list */
438 if (Class_AddMask(class, Req->argv[0],
439 time(NULL) + atol(Req->argv[1]),
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],