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"
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 * Check that the client is an IRC operator allowed to administer this server.
58 Check_Oper(CLIENT * Client, REQUEST * Req)
62 assert(Client != NULL);
65 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
66 c = Client_Search(Req->prefix);
71 if (!Client_HasMode(c, 'o'))
73 if (!Client_OperByMe(c) && !Conf_AllowRemoteOper)
75 /* The client is an local IRC operator, or this server is configured
76 * to trust remote operators. */
82 * Return and log a "no privileges" message.
85 No_Privileges(CLIENT * Client, REQUEST * Req)
90 from = Client_Search(Req->prefix);
93 Log(LOG_NOTICE, "No privileges: client \"%s\" (%s), command \"%s\"",
94 Req->prefix, Client_Mask(Client), Req->command);
95 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
98 Log(LOG_NOTICE, "No privileges: client \"%s\", command \"%s\"",
99 Client_Mask(Client), Req->command);
100 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
103 } /* PermissionDenied */
107 IRC_OPER( CLIENT *Client, REQUEST *Req )
111 assert( Client != NULL );
112 assert( Req != NULL );
114 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
116 for( i = 0; i < Conf_Oper_Count; i++)
118 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
120 if( i >= Conf_Oper_Count )
121 return Bad_OperPass(Client, Req->argv[0], "not configured");
123 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
124 return Bad_OperPass(Client, Conf_Oper[i].name, "bad password");
126 if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
127 return Bad_OperPass(Client, Conf_Oper[i].mask, "hostmask check failed" );
129 if( ! Client_HasMode( Client, 'o' ))
131 Client_ModeAdd( Client, 'o' );
132 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
133 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
136 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
138 Client_SetOperByMe( Client, true);
139 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
144 IRC_DIE(CLIENT * Client, REQUEST * Req)
146 /* Shut down server */
151 assert(Client != NULL);
154 if (!Check_Oper(Client, Req))
155 return No_Privileges(Client, Req);
157 /* Bad number of parameters? */
163 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
164 Client_ID(Client), Req->command);
166 /* Is a message given? */
170 cl = Conn_GetClient(c);
171 if (Client_Type(cl) == CLIENT_USER)
172 IRC_WriteStrClient(cl, "NOTICE %s :%s",
173 Client_ID(cl), Req->argv[0]);
178 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
179 Client_Mask(Client));
180 NGIRCd_SignalQuit = true;
187 IRC_REHASH( CLIENT *Client, REQUEST *Req )
189 /* Reload configuration file */
191 assert( Client != NULL );
192 assert( Req != NULL );
194 if (!Check_Oper(Client, Req))
195 return No_Privileges(Client, Req);
197 /* Bad number of parameters? */
198 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
200 Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
201 NGIRCd_SignalRehash = true;
208 IRC_RESTART( CLIENT *Client, REQUEST *Req )
210 /* Restart IRC server (fork a new process) */
212 assert( Client != NULL );
213 assert( Req != NULL );
215 if (!Check_Oper(Client, Req))
216 return No_Privileges(Client, Req);
218 /* Bad number of parameters? */
219 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
221 Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
222 NGIRCd_SignalRestart = true;
228 * Connect configured or new server.
231 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
233 assert(Client != NULL);
236 if (!Check_Oper(Client, Req))
237 return No_Privileges(Client, Req);
239 /* Bad number of parameters? */
240 if ((Req->argc != 1) && (Req->argc != 2) && (Req->argc != 5))
241 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
242 Client_ID(Client), Req->command);
244 /* Invalid port number? */
245 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
246 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
247 Client_ID(Client), Req->command);
249 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
250 "Received CONNECT %s from %s",
251 Req->argv[0], Client_ID(Client));
253 Log(LOG_NOTICE | LOG_snotice,
254 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
259 if (!Conf_EnablePassiveServer(Req->argv[0]))
260 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
265 /* Connect configured server */
266 if (!Conf_EnableServer
267 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
268 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
275 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
276 Req->argv[3], Req->argv[4]))
277 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
287 * Disconnect (and disable) configured server.
290 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
294 assert(Client != NULL);
297 if (!Check_Oper(Client, Req))
298 return No_Privileges(Client, Req);
300 /* Bad number of parameters? */
302 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
303 Client_ID(Client), Req->command);
305 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
306 "Received DISCONNECT %s from %s",
307 Req->argv[0], Client_ID(Client));
309 Log(LOG_NOTICE | LOG_snotice,
310 "Got DISCONNECT command from \"%s\" for \"%s\".",
311 Client_Mask(Client), Req->argv[0]);
313 /* Save ID of this connection */
314 my_conn = Client_Conn(Client);
316 /* Disconnect configured server */
317 if (!Conf_DisableServer(Req->argv[0]))
318 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
319 Client_ID(Client), Req->argv[0]);
321 /* Are we still connected or were we killed, too? */
322 if (Conn_GetClient(my_conn))
326 } /* IRC_DISCONNECT */
330 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
334 assert( Client != NULL );
335 assert( Req != NULL );
338 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
340 switch (Client_Type(Client)) {
342 if (!Client_OperByMe(Client))
343 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
347 from = Client_Search(Req->prefix);
354 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
356 IRC_SendWallops(Client, from, "%s", Req->argv[0]);