2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 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-write.h"
42 * Handle invalid received OPER command.
43 * Log OPER attempt and send error message to client.
46 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
48 Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
49 Client_Mask(Client), errtoken, errmsg);
50 IRC_SetPenalty(Client, 3);
51 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
56 * Handler for the IRC "OPER" command.
58 * See RFC 2812, 3.1.4 "Oper message".
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 );
74 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
75 Client_ID(Client), Req->command);
77 len = array_length(&Conf_Opers, sizeof(*op));
78 op = array_start(&Conf_Opers);
79 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
82 return Bad_OperPass(Client, Req->argv[0], "not configured");
84 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
85 return Bad_OperPass(Client, op[i].name, "bad password");
87 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
88 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
90 if (!Client_HasMode(Client, 'o')) {
91 Client_ModeAdd(Client, 'o');
92 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
95 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
99 if (!Client_OperByMe(Client))
100 Log(LOG_NOTICE|LOG_snotice,
101 "Got valid OPER from \"%s\", user is an IRC operator now.",
102 Client_Mask(Client));
104 Client_SetOperByMe(Client, true);
105 return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
109 * Handler for the IRC "DIE" command.
111 * See RFC 2812, 4.3 "Die message".
113 * @param Client The client from which this command has been received.
114 * @param Req Request structure with prefix and all parameters.
115 * @return CONNECTED or DISCONNECTED.
118 IRC_DIE(CLIENT * Client, REQUEST * Req)
120 /* Shut down server */
125 assert(Client != NULL);
128 if (!Op_Check(Client, Req))
129 return Op_NoPrivileges(Client, Req);
131 /* Bad number of parameters? */
137 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
138 Client_ID(Client), Req->command);
140 /* Is a message given? */
144 cl = Conn_GetClient(c);
145 if (Client_Type(cl) == CLIENT_USER)
146 IRC_WriteStrClient(cl, "NOTICE %s :%s",
147 Client_ID(cl), Req->argv[0]);
152 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
153 Client_Mask(Client));
154 NGIRCd_SignalQuit = true;
160 * Handler for the IRC "REHASH" command.
162 * See RFC 2812, 4.2 "Rehash message".
164 * @param Client The client from which this command has been received.
165 * @param Req Request structure with prefix and all parameters.
166 * @return CONNECTED or DISCONNECTED.
169 IRC_REHASH( CLIENT *Client, REQUEST *Req )
171 /* Reload configuration file */
173 assert( Client != NULL );
174 assert( Req != NULL );
176 if (!Op_Check(Client, Req))
177 return Op_NoPrivileges(Client, Req);
179 /* Bad number of parameters? */
181 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
182 Client_ID(Client), Req->command );
184 Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
185 Client_Mask(Client));
192 * Handler for the IRC "RESTART" command.
194 * See RFC 2812, 4.4 "Restart message".
196 * @param Client The client from which this command has been received.
197 * @param Req Request structure with prefix and all parameters.
198 * @return CONNECTED or DISCONNECTED.
201 IRC_RESTART( CLIENT *Client, REQUEST *Req )
203 /* Restart IRC server (fork a new process) */
205 assert( Client != NULL );
206 assert( Req != NULL );
208 if (!Op_Check(Client, Req))
209 return Op_NoPrivileges(Client, Req);
211 /* Bad number of parameters? */
213 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
214 Client_ID(Client), Req->command);
216 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
217 Client_Mask(Client));
218 NGIRCd_SignalRestart = true;
224 * Handler for the IRC "CONNECT" command.
226 * See RFC 2812, 3.4.7 "Connect message".
228 * @param Client The client from which this command has been received.
229 * @param Req Request structure with prefix and all parameters.
230 * @return CONNECTED or DISCONNECTED.
233 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
235 CLIENT *from, *target;
237 assert(Client != NULL);
240 if (Client_Type(Client) != CLIENT_SERVER
241 && !Client_HasMode(Client, 'o'))
242 return Op_NoPrivileges(Client, Req);
244 /* Bad number of parameters? */
245 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
246 Req->argc != 5 && Req->argc != 6)
247 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
248 Client_ID(Client), Req->command);
250 /* Invalid port number? */
251 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
252 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
253 Client_ID(Client), Req->command);
256 target = Client_ThisServer();
258 if (Req->argc == 3 || Req->argc == 6) {
259 /* This CONNECT has a target parameter */
260 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
261 from = Client_Search(Req->prefix);
263 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
264 Client_ID(Client), Req->prefix);
266 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
267 : Client_Search(Req->argv[5]);
268 if (! target || Client_Type(target) != CLIENT_SERVER)
269 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
270 Client_ID(from), Req->argv[0]);
273 if (target != Client_ThisServer()) {
274 /* Forward CONNECT command ... */
276 IRC_WriteStrClientPrefix(target, from,
277 "CONNECT %s %s :%s", Req->argv[0],
278 Req->argv[1], Req->argv[2]);
280 IRC_WriteStrClientPrefix(target, from,
281 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
282 Req->argv[1], Req->argv[2], Req->argv[3],
283 Req->argv[4], Req->argv[5]);
287 if (!Op_Check(from, Req))
288 return Op_NoPrivileges(Client, Req);
292 if (!Conf_EnablePassiveServer(Req->argv[0]))
293 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
299 /* Connect configured server */
300 if (!Conf_EnableServer
301 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
302 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
309 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
310 Req->argv[3], Req->argv[4]))
311 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
316 Log(LOG_NOTICE | LOG_snotice,
317 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
319 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
320 "Received CONNECT %s from %s",
321 Req->argv[0], Client_ID(from));
327 * Handler for the IRC "DISCONNECT" command.
329 * This command is not specified in the IRC RFCs, it is an extension
330 * of ngIRCd: it shuts down and disables a configured server connection.
332 * @param Client The client from which this command has been received.
333 * @param Req Request structure with prefix and all parameters.
334 * @return CONNECTED or DISCONNECTED.
337 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
341 assert(Client != NULL);
344 if (!Op_Check(Client, Req))
345 return Op_NoPrivileges(Client, Req);
347 /* Bad number of parameters? */
349 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
350 Client_ID(Client), Req->command);
352 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
353 "Received DISCONNECT %s from %s",
354 Req->argv[0], Client_ID(Client));
356 Log(LOG_NOTICE | LOG_snotice,
357 "Got DISCONNECT command from \"%s\" for \"%s\".",
358 Client_Mask(Client), Req->argv[0]);
360 /* Save ID of this connection */
361 my_conn = Client_Conn(Client);
363 /* Disconnect configured server */
364 if (!Conf_DisableServer(Req->argv[0]))
365 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
366 Client_ID(Client), Req->argv[0]);
368 /* Are we still connected or were we killed, too? */
369 if (Conn_GetClient(my_conn))
373 } /* IRC_DISCONNECT */
376 * Handler for the IRC "WALLOPS" command.
378 * See RFC 2812, 4.7 "Operwall message".
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_WALLOPS( CLIENT *Client, REQUEST *Req )
389 assert( Client != NULL );
390 assert( Req != NULL );
393 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
394 Client_ID(Client), Req->command);
396 switch (Client_Type(Client)) {
398 if (!Client_OperByMe(Client))
399 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
404 from = Client_Search(Req->prefix);
411 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
412 Client_ID(Client), Req->prefix);
414 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
419 * Handle <?>LINE commands (GLINE, KLINE).
421 * @param Client The client from which this command has been received.
422 * @param Req Request structure with prefix and all parameters.
423 * @return CONNECTED or DISCONNECTED.
426 IRC_xLINE(CLIENT *Client, REQUEST *Req)
432 assert(Client != NULL);
435 from = Op_Check(Client, Req);
437 return Op_NoPrivileges(Client, Req);
439 /* Bad number of parameters? */
440 if (Req->argc != 1 && Req->argc != 3)
441 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
442 Client_ID(Client), Req->command);
444 switch(Req->command[0]) {
447 class = CLASS_GLINE; class_c = 'G';
451 class = CLASS_KLINE; class_c = 'K';
455 "IRC_xLINE() called for unknown line: %c!? Ignored.",
460 if (Req->argc == 1) {
461 /* Delete mask from list */
462 Class_DeleteMask(class, Req->argv[0]);
463 Log(LOG_NOTICE|LOG_snotice,
464 "\"%s\" deleted \"%s\" from %c-Line list.",
465 Client_Mask(from), Req->argv[0], class_c);
466 if (class == CLASS_GLINE) {
467 /* Inform other servers */
468 IRC_WriteStrServersPrefix(Client, from, "%s %s",
469 Req->command, Req->argv[0]);
473 /* Add new mask to list */
474 if (Class_AddMask(class, Req->argv[0],
475 time(NULL) + atol(Req->argv[1]),
477 Log(LOG_NOTICE|LOG_snotice,
478 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
479 Client_Mask(from), Req->argv[0], class_c,
480 Req->argv[2], atol(Req->argv[1]));
481 if (class == CLASS_GLINE) {
482 /* Inform other servers */
483 IRC_WriteStrServersPrefix(Client, from,
484 "%s %s %s :%s", Req->command,
485 Req->argv[0], Req->argv[1],