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)
60 if (!Client_HasMode(Client, 'o'))
62 if (!Client_OperByMe(Client) && !Conf_AllowRemoteOper)
64 /* The client is an local IRC operator, or this server is configured
65 * to trust remote operators. */
71 * Return and log a "no privileges" message.
74 No_Privileges(CLIENT * Client, REQUEST * Req)
76 Log(LOG_NOTICE, "No privileges: client \"%s\", command \"%s\"",
77 Client_Mask(Client), Req->command);
78 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
80 } /* PermissionDenied */
84 IRC_OPER( CLIENT *Client, REQUEST *Req )
88 assert( Client != NULL );
89 assert( Req != NULL );
91 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
93 for( i = 0; i < Conf_Oper_Count; i++)
95 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
97 if( i >= Conf_Oper_Count )
98 return Bad_OperPass(Client, Req->argv[0], "not configured");
100 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
101 return Bad_OperPass(Client, Conf_Oper[i].name, "bad password");
103 if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
104 return Bad_OperPass(Client, Conf_Oper[i].mask, "hostmask check failed" );
106 if( ! Client_HasMode( Client, 'o' ))
108 Client_ModeAdd( Client, 'o' );
109 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
110 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
113 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
115 Client_SetOperByMe( Client, true);
116 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
121 IRC_DIE(CLIENT * Client, REQUEST * Req)
123 /* Shut down server */
128 assert(Client != NULL);
131 if (!Check_Oper(Client))
132 return No_Privileges(Client, Req);
134 /* Bad number of parameters? */
140 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
141 Client_ID(Client), Req->command);
143 /* Is a message given? */
147 cl = Conn_GetClient(c);
148 if (Client_Type(cl) == CLIENT_USER)
149 IRC_WriteStrClient(cl, "NOTICE %s :%s",
150 Client_ID(cl), Req->argv[0]);
155 Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
156 Client_Mask(Client));
157 NGIRCd_SignalQuit = true;
164 IRC_REHASH( CLIENT *Client, REQUEST *Req )
166 /* Reload configuration file */
168 assert( Client != NULL );
169 assert( Req != NULL );
171 if (!Check_Oper(Client))
172 return No_Privileges(Client, Req);
174 /* Bad number of parameters? */
175 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
177 Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
178 NGIRCd_SignalRehash = true;
185 IRC_RESTART( CLIENT *Client, REQUEST *Req )
187 /* Restart IRC server (fork a new process) */
189 assert( Client != NULL );
190 assert( Req != NULL );
192 if (!Check_Oper(Client))
193 return No_Privileges(Client, Req);
195 /* Bad number of parameters? */
196 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
198 Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
199 NGIRCd_SignalRestart = true;
205 * Connect configured or new server.
208 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
210 char msg[LINE_LEN + 64];
212 assert(Client != NULL);
215 if (!Check_Oper(Client))
216 return No_Privileges(Client, Req);
218 /* Bad number of parameters? */
219 if ((Req->argc != 1) && (Req->argc != 2) && (Req->argc != 5))
220 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
221 Client_ID(Client), Req->command);
223 /* Invalid port number? */
224 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
225 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
226 Client_ID(Client), Req->command);
228 snprintf(msg, sizeof(msg), "Received CONNECT %s from %s",
229 Req->argv[0], Client_ID(Client));
230 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(), msg);
232 Log(LOG_NOTICE | LOG_snotice,
233 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
238 if (!Conf_EnablePassiveServer(Req->argv[0]))
239 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
244 /* Connect configured server */
245 if (!Conf_EnableServer
246 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
247 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
254 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
255 Req->argv[3], Req->argv[4]))
256 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
266 * Disconnect (and disable) configured server.
269 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
272 char msg[LINE_LEN + 64];
274 assert(Client != NULL);
277 if (!Check_Oper(Client))
278 return No_Privileges(Client, Req);
280 /* Bad number of parameters? */
282 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
283 Client_ID(Client), Req->command);
285 snprintf(msg, sizeof(msg), "Received DISCONNECT %s from %s",
286 Req->argv[0], Client_ID(Client));
287 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(), msg);
289 Log(LOG_NOTICE | LOG_snotice,
290 "Got DISCONNECT command from \"%s\" for \"%s\".",
291 Client_Mask(Client), Req->argv[0]);
293 /* Save ID of this connection */
294 my_conn = Client_Conn(Client);
296 /* Disconnect configured server */
297 if (!Conf_DisableServer(Req->argv[0]))
298 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
299 Client_ID(Client), Req->argv[0]);
301 /* Are we still connected or were we killed, too? */
302 if (Conn_GetClient(my_conn))
306 } /* IRC_DISCONNECT */
310 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
314 assert( Client != NULL );
315 assert( Req != NULL );
318 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
320 switch (Client_Type(Client)) {
322 if (!Client_OperByMe(Client))
323 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
327 from = Client_Search(Req->prefix);
334 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
336 IRC_SendWallops(Client, from, Req->argv[0]);