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"
29 #include "irc-write.h"
41 * Handle invalid received OPER command.
42 * Log OPER attempt and send error message to client.
45 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
47 Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
48 Client_Mask(Client), errtoken, errmsg);
49 IRC_SetPenalty(Client, 3);
50 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
56 IRC_OPER( CLIENT *Client, REQUEST *Req )
61 assert( Client != NULL );
62 assert( Req != NULL );
64 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
66 len = array_length(&Conf_Opers, sizeof(*op));
67 op = array_start(&Conf_Opers);
68 for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
71 return Bad_OperPass(Client, Req->argv[0], "not configured");
73 if (strcmp(op[i].pwd, Req->argv[1]) != 0)
74 return Bad_OperPass(Client, op[i].name, "bad password");
76 if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
77 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
79 if( ! Client_HasMode( Client, 'o' ))
81 Client_ModeAdd( Client, 'o' );
82 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
83 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
86 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
88 Client_SetOperByMe( Client, true);
89 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
94 IRC_DIE(CLIENT * Client, REQUEST * Req)
96 /* Shut down server */
101 assert(Client != NULL);
104 if (!Op_Check(Client, Req))
105 return Op_NoPrivileges(Client, Req);
107 /* Bad number of parameters? */
113 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
114 Client_ID(Client), Req->command);
116 /* Is a message given? */
120 cl = Conn_GetClient(c);
121 if (Client_Type(cl) == CLIENT_USER)
122 IRC_WriteStrClient(cl, "NOTICE %s :%s",
123 Client_ID(cl), Req->argv[0]);
128 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
129 Client_Mask(Client));
130 NGIRCd_SignalQuit = true;
137 IRC_REHASH( CLIENT *Client, REQUEST *Req )
139 /* Reload configuration file */
141 assert( Client != NULL );
142 assert( Req != NULL );
144 if (!Op_Check(Client, Req))
145 return Op_NoPrivileges(Client, Req);
147 /* Bad number of parameters? */
148 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
150 Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
151 NGIRCd_SignalRehash = true;
158 IRC_RESTART( CLIENT *Client, REQUEST *Req )
160 /* Restart IRC server (fork a new process) */
162 assert( Client != NULL );
163 assert( Req != NULL );
165 if (!Op_Check(Client, Req))
166 return Op_NoPrivileges(Client, Req);
168 /* Bad number of parameters? */
169 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
171 Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
172 NGIRCd_SignalRestart = true;
178 * Connect configured or new server.
181 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
183 CLIENT *from, *target;
185 assert(Client != NULL);
188 if (Client_Type(Client) != CLIENT_SERVER
189 && !Client_HasMode(Client, 'o'))
190 return Op_NoPrivileges(Client, Req);
192 /* Bad number of parameters? */
193 if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
194 Req->argc != 5 && Req->argc != 6)
195 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
196 Client_ID(Client), Req->command);
198 /* Invalid port number? */
199 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
200 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
201 Client_ID(Client), Req->command);
204 target = Client_ThisServer();
206 if (Req->argc == 3 || Req->argc == 6) {
207 /* This CONNECT has a target parameter */
208 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
209 from = Client_Search(Req->prefix);
211 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
212 Client_ID(Client), Req->prefix);
214 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
215 : Client_Search(Req->argv[5]);
216 if (! target || Client_Type(target) != CLIENT_SERVER)
217 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
218 Client_ID(from), Req->argv[0]);
221 if (target != Client_ThisServer()) {
222 /* Forward CONNECT command ... */
224 IRC_WriteStrClientPrefix(target, from,
225 "CONNECT %s %s :%s", Req->argv[0],
226 Req->argv[1], Req->argv[2]);
228 IRC_WriteStrClientPrefix(target, from,
229 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
230 Req->argv[1], Req->argv[2], Req->argv[3],
231 Req->argv[4], Req->argv[5]);
235 if (!Op_Check(from, Req))
236 return Op_NoPrivileges(Client, Req);
240 if (!Conf_EnablePassiveServer(Req->argv[0]))
241 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
247 /* Connect configured server */
248 if (!Conf_EnableServer
249 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
250 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
257 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
258 Req->argv[3], Req->argv[4]))
259 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
264 Log(LOG_NOTICE | LOG_snotice,
265 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
267 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
268 "Received CONNECT %s from %s",
269 Req->argv[0], Client_ID(from));
276 * Disconnect (and disable) configured server.
279 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
283 assert(Client != NULL);
286 if (!Op_Check(Client, Req))
287 return Op_NoPrivileges(Client, Req);
289 /* Bad number of parameters? */
291 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
292 Client_ID(Client), Req->command);
294 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
295 "Received DISCONNECT %s from %s",
296 Req->argv[0], Client_ID(Client));
298 Log(LOG_NOTICE | LOG_snotice,
299 "Got DISCONNECT command from \"%s\" for \"%s\".",
300 Client_Mask(Client), Req->argv[0]);
302 /* Save ID of this connection */
303 my_conn = Client_Conn(Client);
305 /* Disconnect configured server */
306 if (!Conf_DisableServer(Req->argv[0]))
307 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
308 Client_ID(Client), Req->argv[0]);
310 /* Are we still connected or were we killed, too? */
311 if (Conn_GetClient(my_conn))
315 } /* IRC_DISCONNECT */
319 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
323 assert( Client != NULL );
324 assert( Req != NULL );
327 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
329 switch (Client_Type(Client)) {
331 if (!Client_OperByMe(Client))
332 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
336 from = Client_Search(Req->prefix);
343 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
345 IRC_SendWallops(Client, from, "%s", Req->argv[0]);