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 assert(Client != NULL);
213 if (!Check_Oper(Client))
214 return No_Privileges(Client, Req);
216 /* Bad number of parameters? */
217 if ((Req->argc != 1) && (Req->argc != 2) && (Req->argc != 5))
218 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
219 Client_ID(Client), Req->command);
221 /* Invalid port number? */
222 if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
223 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
224 Client_ID(Client), Req->command);
226 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
227 "Received CONNECT %s from %s",
228 Req->argv[0], Client_ID(Client));
230 Log(LOG_NOTICE | LOG_snotice,
231 "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
236 if (!Conf_EnablePassiveServer(Req->argv[0]))
237 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
242 /* Connect configured server */
243 if (!Conf_EnableServer
244 (Req->argv[0], (UINT16) atoi(Req->argv[1])))
245 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
252 (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
253 Req->argv[3], Req->argv[4]))
254 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
264 * Disconnect (and disable) configured server.
267 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
271 assert(Client != NULL);
274 if (!Check_Oper(Client))
275 return No_Privileges(Client, Req);
277 /* Bad number of parameters? */
279 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
280 Client_ID(Client), Req->command);
282 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
283 "Received DISCONNECT %s from %s",
284 Req->argv[0], Client_ID(Client));
286 Log(LOG_NOTICE | LOG_snotice,
287 "Got DISCONNECT command from \"%s\" for \"%s\".",
288 Client_Mask(Client), Req->argv[0]);
290 /* Save ID of this connection */
291 my_conn = Client_Conn(Client);
293 /* Disconnect configured server */
294 if (!Conf_DisableServer(Req->argv[0]))
295 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
296 Client_ID(Client), Req->argv[0]);
298 /* Are we still connected or were we killed, too? */
299 if (Conn_GetClient(my_conn))
303 } /* IRC_DISCONNECT */
307 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
311 assert( Client != NULL );
312 assert( Req != NULL );
315 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
317 switch (Client_Type(Client)) {
319 if (!Client_OperByMe(Client))
320 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
324 from = Client_Search(Req->prefix);
331 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
333 IRC_SendWallops(Client, from, "%s", Req->argv[0]);