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 for \"%s\" from \"%s\", user is an IRC operator now.",
102 Req->argv[0], 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));
186 IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
194 * Handler for the IRC "RESTART" command.
196 * See RFC 2812, 4.4 "Restart message".
198 * @param Client The client from which this command has been received.
199 * @param Req Request structure with prefix and all parameters.
200 * @return CONNECTED or DISCONNECTED.
203 IRC_RESTART( CLIENT *Client, REQUEST *Req )
205 /* Restart IRC server (fork a new process) */
207 assert( Client != NULL );
208 assert( Req != NULL );
210 if (!Op_Check(Client, Req))
211 return Op_NoPrivileges(Client, Req);
213 /* Bad number of parameters? */
215 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
216 Client_ID(Client), Req->command);
218 Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
219 Client_Mask(Client));
220 NGIRCd_SignalRestart = true;
226 * Handler for the IRC "CONNECT" command.
228 * See RFC 2812, 3.4.7 "Connect message".
230 * @param Client The client from which this command has been received.
231 * @param Req Request structure with prefix and all parameters.
232 * @return CONNECTED or DISCONNECTED.
235 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
237 CLIENT *from, *target;
239 assert(Client != NULL);
242 if (Client_Type(Client) != CLIENT_SERVER
243 && !Client_HasMode(Client, 'o'))
244 return Op_NoPrivileges(Client, Req);
246 /* Bad number of parameters? */
247 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
248 Req->argc != 5 && Req->argc != 6)
249 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
250 Client_ID(Client), Req->command);
252 /* Invalid port number? */
253 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
254 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
255 Client_ID(Client), Req->command);
258 target = Client_ThisServer();
260 if (Req->argc == 3 || Req->argc == 6) {
261 /* This CONNECT has a target parameter */
262 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
263 from = Client_Search(Req->prefix);
265 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
266 Client_ID(Client), Req->prefix);
268 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
269 : Client_Search(Req->argv[5]);
270 if (! target || Client_Type(target) != CLIENT_SERVER)
271 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
272 Client_ID(from), Req->argv[0]);
275 if (target != Client_ThisServer()) {
276 /* Forward CONNECT command ... */
278 IRC_WriteStrClientPrefix(target, from,
279 "CONNECT %s %s :%s", Req->argv[0],
280 Req->argv[1], Req->argv[2]);
282 IRC_WriteStrClientPrefix(target, from,
283 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
284 Req->argv[1], Req->argv[2], Req->argv[3],
285 Req->argv[4], Req->argv[5]);
289 if (!Op_Check(from, Req))
290 return Op_NoPrivileges(Client, Req);
294 if (!Conf_EnablePassiveServer(Req->argv[0]))
295 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
301 /* Connect configured server */
302 if (!Conf_EnableServer
303 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
304 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
311 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
312 Req->argv[3], Req->argv[4]))
313 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
318 Log(LOG_NOTICE | LOG_snotice,
319 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
321 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
322 "Received CONNECT %s from %s",
323 Req->argv[0], Client_ID(from));
329 * Handler for the IRC "DISCONNECT" command.
331 * This command is not specified in the IRC RFCs, it is an extension
332 * of ngIRCd: it shuts down and disables a configured server connection.
334 * @param Client The client from which this command has been received.
335 * @param Req Request structure with prefix and all parameters.
336 * @return CONNECTED or DISCONNECTED.
339 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
343 assert(Client != NULL);
346 if (!Op_Check(Client, Req))
347 return Op_NoPrivileges(Client, Req);
349 /* Bad number of parameters? */
351 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
352 Client_ID(Client), Req->command);
354 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
355 "Received DISCONNECT %s from %s",
356 Req->argv[0], Client_ID(Client));
358 Log(LOG_NOTICE | LOG_snotice,
359 "Got DISCONNECT command from \"%s\" for \"%s\".",
360 Client_Mask(Client), Req->argv[0]);
362 /* Save ID of this connection */
363 my_conn = Client_Conn(Client);
365 /* Disconnect configured server */
366 if (!Conf_DisableServer(Req->argv[0]))
367 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
368 Client_ID(Client), Req->argv[0]);
370 /* Are we still connected or were we killed, too? */
371 if (Conn_GetClient(my_conn))
375 } /* IRC_DISCONNECT */
378 * Handler for the IRC "WALLOPS" command.
380 * See RFC 2812, 4.7 "Operwall message".
382 * @param Client The client from which this command has been received.
383 * @param Req Request structure with prefix and all parameters.
384 * @return CONNECTED or DISCONNECTED.
387 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
391 assert( Client != NULL );
392 assert( Req != NULL );
395 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
396 Client_ID(Client), Req->command);
398 switch (Client_Type(Client)) {
400 if (!Client_OperByMe(Client))
401 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
406 from = Client_Search(Req->prefix);
413 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
414 Client_ID(Client), Req->prefix);
416 IRC_SendWallops(Client, from, "%s", Req->argv[0]);
421 * Handle <?>LINE commands (GLINE, KLINE).
423 * @param Client The client from which this command has been received.
424 * @param Req Request structure with prefix and all parameters.
425 * @return CONNECTED or DISCONNECTED.
428 IRC_xLINE(CLIENT *Client, REQUEST *Req)
434 assert(Client != NULL);
437 from = Op_Check(Client, Req);
439 return Op_NoPrivileges(Client, Req);
441 /* Bad number of parameters? */
442 if (Req->argc != 1 && Req->argc != 3)
443 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
444 Client_ID(Client), Req->command);
446 switch(Req->command[0]) {
449 class = CLASS_GLINE; class_c = 'G';
453 class = CLASS_KLINE; class_c = 'K';
457 "IRC_xLINE() called for unknown line: %c!? Ignored.",
462 if (Req->argc == 1) {
463 /* Delete mask from list */
464 Class_DeleteMask(class, Req->argv[0]);
465 Log(LOG_NOTICE|LOG_snotice,
466 "\"%s\" deleted \"%s\" from %c-Line list.",
467 Client_Mask(from), Req->argv[0], class_c);
468 if (class == CLASS_GLINE) {
469 /* Inform other servers */
470 IRC_WriteStrServersPrefix(Client, from, "%s %s",
471 Req->command, Req->argv[0]);
475 /* Add new mask to list */
476 if (Class_AddMask(class, Req->argv[0],
477 time(NULL) + atol(Req->argv[1]),
479 Log(LOG_NOTICE|LOG_snotice,
480 "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
481 Client_Mask(from), Req->argv[0], class_c,
482 Req->argv[2], atol(Req->argv[1]));
483 if (class == CLASS_GLINE) {
484 /* Inform other servers */
485 IRC_WriteStrServersPrefix(Client, from,
486 "%s %s %s :%s", Req->command,
487 Req->argv[0], Req->argv[1],