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.
11 * IRC operator commands
25 #include "conn-func.h"
28 #include "irc-write.h"
40 * Handle invalid received OPER command.
41 * Log OPER attempt and send error message to client.
44 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
46 Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
47 Client_Mask(Client), errtoken, errmsg);
48 IRC_SetPenalty(Client, 3);
49 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
55 IRC_OPER( CLIENT *Client, REQUEST *Req )
60 assert( Client != NULL );
61 assert( Req != NULL );
63 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
65 len = array_length(&Conf_Opers, sizeof(*op));
66 op = array_start(&Conf_Opers);
67 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
70 return Bad_OperPass(Client, Req->argv[0], "not configured");
72 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
73 return Bad_OperPass(Client, op[i].name, "bad password");
75 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
76 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
78 if( ! Client_HasMode( Client, 'o' ))
80 Client_ModeAdd( Client, 'o' );
81 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
82 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
85 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
87 Client_SetOperByMe( Client, true);
88 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
93 IRC_DIE(CLIENT * Client, REQUEST * Req)
95 /* Shut down server */
100 assert(Client != NULL);
103 if (!Op_Check(Client, Req))
104 return Op_NoPrivileges(Client, Req);
106 /* Bad number of parameters? */
112 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
113 Client_ID(Client), Req->command);
115 /* Is a message given? */
119 cl = Conn_GetClient(c);
120 if (Client_Type(cl) == CLIENT_USER)
121 IRC_WriteStrClient(cl, "NOTICE %s :%s",
122 Client_ID(cl), Req->argv[0]);
127 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
128 Client_Mask(Client));
129 NGIRCd_SignalQuit = true;
136 IRC_REHASH( CLIENT *Client, REQUEST *Req )
138 /* Reload configuration file */
140 assert( Client != NULL );
141 assert( Req != NULL );
143 if (!Op_Check(Client, Req))
144 return Op_NoPrivileges(Client, Req);
146 /* Bad number of parameters? */
147 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
149 Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
157 IRC_RESTART( CLIENT *Client, REQUEST *Req )
159 /* Restart IRC server (fork a new process) */
161 assert( Client != NULL );
162 assert( Req != NULL );
164 if (!Op_Check(Client, Req))
165 return Op_NoPrivileges(Client, Req);
167 /* Bad number of parameters? */
168 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
170 Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
171 NGIRCd_SignalRestart = true;
177 * Connect configured or new server.
180 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
182 CLIENT *from, *target;
184 assert(Client != NULL);
187 if (Client_Type(Client) != CLIENT_SERVER
188 && !Client_HasMode(Client, 'o'))
189 return Op_NoPrivileges(Client, Req);
191 /* Bad number of parameters? */
192 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
193 Req->argc != 5 && Req->argc != 6)
194 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
195 Client_ID(Client), Req->command);
197 /* Invalid port number? */
198 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
199 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
200 Client_ID(Client), Req->command);
203 target = Client_ThisServer();
205 if (Req->argc == 3 || Req->argc == 6) {
206 /* This CONNECT has a target parameter */
207 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
208 from = Client_Search(Req->prefix);
210 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
211 Client_ID(Client), Req->prefix);
213 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
214 : Client_Search(Req->argv[5]);
215 if (! target || Client_Type(target) != CLIENT_SERVER)
216 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
217 Client_ID(from), Req->argv[0]);
220 if (target != Client_ThisServer()) {
221 /* Forward CONNECT command ... */
223 IRC_WriteStrClientPrefix(target, from,
224 "CONNECT %s %s :%s", Req->argv[0],
225 Req->argv[1], Req->argv[2]);
227 IRC_WriteStrClientPrefix(target, from,
228 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
229 Req->argv[1], Req->argv[2], Req->argv[3],
230 Req->argv[4], Req->argv[5]);
234 if (!Op_Check(from, Req))
235 return Op_NoPrivileges(Client, Req);
239 if (!Conf_EnablePassiveServer(Req->argv[0]))
240 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
246 /* Connect configured server */
247 if (!Conf_EnableServer
248 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
249 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
256 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
257 Req->argv[3], Req->argv[4]))
258 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
263 Log(LOG_NOTICE | LOG_snotice,
264 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
266 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
267 "Received CONNECT %s from %s",
268 Req->argv[0], Client_ID(from));
275 * Disconnect (and disable) configured server.
278 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
282 assert(Client != NULL);
285 if (!Op_Check(Client, Req))
286 return Op_NoPrivileges(Client, Req);
288 /* Bad number of parameters? */
290 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
291 Client_ID(Client), Req->command);
293 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
294 "Received DISCONNECT %s from %s",
295 Req->argv[0], Client_ID(Client));
297 Log(LOG_NOTICE | LOG_snotice,
298 "Got DISCONNECT command from \"%s\" for \"%s\".",
299 Client_Mask(Client), Req->argv[0]);
301 /* Save ID of this connection */
302 my_conn = Client_Conn(Client);
304 /* Disconnect configured server */
305 if (!Conf_DisableServer(Req->argv[0]))
306 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
307 Client_ID(Client), Req->argv[0]);
309 /* Are we still connected or were we killed, too? */
310 if (Conn_GetClient(my_conn))
314 } /* IRC_DISCONNECT */
318 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
322 assert( Client != NULL );
323 assert( Req != NULL );
326 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
328 switch (Client_Type(Client)) {
330 if (!Client_OperByMe(Client))
331 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
335 from = Client_Search(Req->prefix);
342 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
344 IRC_SendWallops(Client, from, "%s", Req->argv[0]);