2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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.
17 * IRC operator commands
28 #include "conn-func.h"
31 #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,
58 IRC_OPER( CLIENT *Client, REQUEST *Req )
63 assert( Client != NULL );
64 assert( Req != NULL );
66 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
68 len = array_length(&Conf_Opers, sizeof(*op));
69 op = array_start(&Conf_Opers);
70 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
73 return Bad_OperPass(Client, Req->argv[0], "not configured");
75 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
76 return Bad_OperPass(Client, op[i].name, "bad password");
78 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
79 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
81 if( ! Client_HasMode( Client, 'o' ))
83 Client_ModeAdd( Client, 'o' );
84 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
85 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
88 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
90 Client_SetOperByMe( Client, true);
91 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
96 IRC_DIE(CLIENT * Client, REQUEST * Req)
98 /* Shut down server */
103 assert(Client != NULL);
106 if (!Op_Check(Client, Req))
107 return Op_NoPrivileges(Client, Req);
109 /* Bad number of parameters? */
115 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
116 Client_ID(Client), Req->command);
118 /* Is a message given? */
122 cl = Conn_GetClient(c);
123 if (Client_Type(cl) == CLIENT_USER)
124 IRC_WriteStrClient(cl, "NOTICE %s :%s",
125 Client_ID(cl), Req->argv[0]);
130 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
131 Client_Mask(Client));
132 NGIRCd_SignalQuit = true;
139 IRC_REHASH( CLIENT *Client, REQUEST *Req )
141 /* Reload configuration file */
143 assert( Client != NULL );
144 assert( Req != NULL );
146 if (!Op_Check(Client, Req))
147 return Op_NoPrivileges(Client, Req);
149 /* Bad number of parameters? */
150 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
152 Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
160 IRC_RESTART( CLIENT *Client, REQUEST *Req )
162 /* Restart IRC server (fork a new process) */
164 assert( Client != NULL );
165 assert( Req != NULL );
167 if (!Op_Check(Client, Req))
168 return Op_NoPrivileges(Client, Req);
170 /* Bad number of parameters? */
171 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
173 Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
174 NGIRCd_SignalRestart = true;
180 * Connect configured or new server.
183 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
185 CLIENT *from, *target;
187 assert(Client != NULL);
190 if (Client_Type(Client) != CLIENT_SERVER
191 && !Client_HasMode(Client, 'o'))
192 return Op_NoPrivileges(Client, Req);
194 /* Bad number of parameters? */
195 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
196 Req->argc != 5 && Req->argc != 6)
197 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
198 Client_ID(Client), Req->command);
200 /* Invalid port number? */
201 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
202 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
203 Client_ID(Client), Req->command);
206 target = Client_ThisServer();
208 if (Req->argc == 3 || Req->argc == 6) {
209 /* This CONNECT has a target parameter */
210 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
211 from = Client_Search(Req->prefix);
213 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
214 Client_ID(Client), Req->prefix);
216 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
217 : Client_Search(Req->argv[5]);
218 if (! target || Client_Type(target) != CLIENT_SERVER)
219 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
220 Client_ID(from), Req->argv[0]);
223 if (target != Client_ThisServer()) {
224 /* Forward CONNECT command ... */
226 IRC_WriteStrClientPrefix(target, from,
227 "CONNECT %s %s :%s", Req->argv[0],
228 Req->argv[1], Req->argv[2]);
230 IRC_WriteStrClientPrefix(target, from,
231 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
232 Req->argv[1], Req->argv[2], Req->argv[3],
233 Req->argv[4], Req->argv[5]);
237 if (!Op_Check(from, Req))
238 return Op_NoPrivileges(Client, Req);
242 if (!Conf_EnablePassiveServer(Req->argv[0]))
243 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
249 /* Connect configured server */
250 if (!Conf_EnableServer
251 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
252 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
259 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
260 Req->argv[3], Req->argv[4]))
261 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
266 Log(LOG_NOTICE | LOG_snotice,
267 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
269 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
270 "Received CONNECT %s from %s",
271 Req->argv[0], Client_ID(from));
278 * Disconnect (and disable) configured server.
281 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
285 assert(Client != NULL);
288 if (!Op_Check(Client, Req))
289 return Op_NoPrivileges(Client, Req);
291 /* Bad number of parameters? */
293 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
294 Client_ID(Client), Req->command);
296 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
297 "Received DISCONNECT %s from %s",
298 Req->argv[0], Client_ID(Client));
300 Log(LOG_NOTICE | LOG_snotice,
301 "Got DISCONNECT command from \"%s\" for \"%s\".",
302 Client_Mask(Client), Req->argv[0]);
304 /* Save ID of this connection */
305 my_conn = Client_Conn(Client);
307 /* Disconnect configured server */
308 if (!Conf_DisableServer(Req->argv[0]))
309 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
310 Client_ID(Client), Req->argv[0]);
312 /* Are we still connected or were we killed, too? */
313 if (Conn_GetClient(my_conn))
317 } /* IRC_DISCONNECT */
321 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
325 assert( Client != NULL );
326 assert( Req != NULL );
329 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
331 switch (Client_Type(Client)) {
333 if (!Client_OperByMe(Client))
334 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
338 from = Client_Search(Req->prefix);
345 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
347 IRC_SendWallops(Client, from, "%s", Req->argv[0]);