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.
16 * IRC operator commands
27 #include "conn-func.h"
30 #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,
57 IRC_OPER( CLIENT *Client, REQUEST *Req )
62 assert( Client != NULL );
63 assert( Req != NULL );
65 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
67 len = array_length(&Conf_Opers, sizeof(*op));
68 op = array_start(&Conf_Opers);
69 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
72 return Bad_OperPass(Client, Req->argv[0], "not configured");
74 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
75 return Bad_OperPass(Client, op[i].name, "bad password");
77 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
78 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
80 if( ! Client_HasMode( Client, 'o' ))
82 Client_ModeAdd( Client, 'o' );
83 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
84 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
87 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
89 Client_SetOperByMe( Client, true);
90 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
95 IRC_DIE(CLIENT * Client, REQUEST * Req)
97 /* Shut down server */
102 assert(Client != NULL);
105 if (!Op_Check(Client, Req))
106 return Op_NoPrivileges(Client, Req);
108 /* Bad number of parameters? */
114 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
115 Client_ID(Client), Req->command);
117 /* Is a message given? */
121 cl = Conn_GetClient(c);
122 if (Client_Type(cl) == CLIENT_USER)
123 IRC_WriteStrClient(cl, "NOTICE %s :%s",
124 Client_ID(cl), Req->argv[0]);
129 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
130 Client_Mask(Client));
131 NGIRCd_SignalQuit = true;
138 IRC_REHASH( CLIENT *Client, REQUEST *Req )
140 /* Reload configuration file */
142 assert( Client != NULL );
143 assert( Req != NULL );
145 if (!Op_Check(Client, Req))
146 return Op_NoPrivileges(Client, Req);
148 /* Bad number of parameters? */
149 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
151 Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
159 IRC_RESTART( CLIENT *Client, REQUEST *Req )
161 /* Restart IRC server (fork a new process) */
163 assert( Client != NULL );
164 assert( Req != NULL );
166 if (!Op_Check(Client, Req))
167 return Op_NoPrivileges(Client, Req);
169 /* Bad number of parameters? */
170 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
172 Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
173 NGIRCd_SignalRestart = true;
179 * Connect configured or new server.
182 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
184 CLIENT *from, *target;
186 assert(Client != NULL);
189 if (Client_Type(Client) != CLIENT_SERVER
190 && !Client_HasMode(Client, 'o'))
191 return Op_NoPrivileges(Client, Req);
193 /* Bad number of parameters? */
194 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
195 Req->argc != 5 && Req->argc != 6)
196 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
197 Client_ID(Client), Req->command);
199 /* Invalid port number? */
200 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
201 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
202 Client_ID(Client), Req->command);
205 target = Client_ThisServer();
207 if (Req->argc == 3 || Req->argc == 6) {
208 /* This CONNECT has a target parameter */
209 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
210 from = Client_Search(Req->prefix);
212 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
213 Client_ID(Client), Req->prefix);
215 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
216 : Client_Search(Req->argv[5]);
217 if (! target || Client_Type(target) != CLIENT_SERVER)
218 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
219 Client_ID(from), Req->argv[0]);
222 if (target != Client_ThisServer()) {
223 /* Forward CONNECT command ... */
225 IRC_WriteStrClientPrefix(target, from,
226 "CONNECT %s %s :%s", Req->argv[0],
227 Req->argv[1], Req->argv[2]);
229 IRC_WriteStrClientPrefix(target, from,
230 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
231 Req->argv[1], Req->argv[2], Req->argv[3],
232 Req->argv[4], Req->argv[5]);
236 if (!Op_Check(from, Req))
237 return Op_NoPrivileges(Client, Req);
241 if (!Conf_EnablePassiveServer(Req->argv[0]))
242 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
248 /* Connect configured server */
249 if (!Conf_EnableServer
250 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
251 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
258 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
259 Req->argv[3], Req->argv[4]))
260 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
265 Log(LOG_NOTICE | LOG_snotice,
266 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
268 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
269 "Received CONNECT %s from %s",
270 Req->argv[0], Client_ID(from));
277 * Disconnect (and disable) configured server.
280 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
284 assert(Client != NULL);
287 if (!Op_Check(Client, Req))
288 return Op_NoPrivileges(Client, Req);
290 /* Bad number of parameters? */
292 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
293 Client_ID(Client), Req->command);
295 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
296 "Received DISCONNECT %s from %s",
297 Req->argv[0], Client_ID(Client));
299 Log(LOG_NOTICE | LOG_snotice,
300 "Got DISCONNECT command from \"%s\" for \"%s\".",
301 Client_Mask(Client), Req->argv[0]);
303 /* Save ID of this connection */
304 my_conn = Client_Conn(Client);
306 /* Disconnect configured server */
307 if (!Conf_DisableServer(Req->argv[0]))
308 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
309 Client_ID(Client), Req->argv[0]);
311 /* Are we still connected or were we killed, too? */
312 if (Conn_GetClient(my_conn))
316 } /* IRC_DISCONNECT */
320 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
324 assert( Client != NULL );
325 assert( Req != NULL );
328 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
330 switch (Client_Type(Client)) {
332 if (!Client_OperByMe(Client))
333 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
337 from = Client_Search(Req->prefix);
344 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
346 IRC_SendWallops(Client, from, "%s", Req->argv[0]);