]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
No_Privileges(): handle forwarded messages.
[ngircd-alex.git] / src / ngircd / irc-oper.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
4  *
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.
10  *
11  * IRC operator commands
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "ngircd.h"
24 #include "resolve.h"
25 #include "conn-func.h"
26 #include "conf.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "irc-write.h"
30 #include "log.h"
31 #include "match.h"
32 #include "messages.h"
33 #include "parse.h"
34
35 #include <exp.h>
36 #include "irc-oper.h"
37
38
39 /**
40  * Handle invalid received OPER command.
41  * Log OPER attempt and send error message to client.
42  */
43 static bool
44 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
45 {
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,
50                                   Client_ID(Client));
51 } /* Bad_OperPass */
52
53
54 /**
55  * Check that the client is an IRC operator allowed to administer this server.
56  */
57 static bool
58 Check_Oper(CLIENT * Client)
59 {
60         if (!Client_HasMode(Client, 'o'))
61                 return false;
62         if (!Client_OperByMe(Client) && !Conf_AllowRemoteOper)
63                 return false;
64         /* The client is an local IRC operator, or this server is configured
65          * to trust remote operators. */
66         return true;
67 } /* CheckOper */
68
69
70 /**
71  * Return and log a "no privileges" message.
72  */
73 static bool
74 No_Privileges(CLIENT * Client, REQUEST * Req)
75 {
76         CLIENT *from = NULL;
77
78         if (Req->prefix) 
79                 from = Client_Search(Req->prefix);
80
81         if (from) {
82                 Log(LOG_NOTICE, "No privileges: client \"%s\" (%s), command \"%s\"",
83                     Req->prefix, Client_Mask(Client), Req->command);
84                 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
85                                           Client_ID(from));
86         } else {
87                 Log(LOG_NOTICE, "No privileges: client \"%s\", command \"%s\"",
88                     Client_Mask(Client), Req->command);
89                 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
90                                           Client_ID(Client));
91         }
92 } /* PermissionDenied */
93
94
95 GLOBAL bool
96 IRC_OPER( CLIENT *Client, REQUEST *Req )
97 {
98         unsigned int i;
99
100         assert( Client != NULL );
101         assert( Req != NULL );
102
103         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
104
105         for( i = 0; i < Conf_Oper_Count; i++)
106         {
107                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
108         }
109         if( i >= Conf_Oper_Count )
110                 return Bad_OperPass(Client, Req->argv[0], "not configured");
111
112         if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
113                 return Bad_OperPass(Client, Conf_Oper[i].name, "bad password");
114
115         if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
116                 return Bad_OperPass(Client, Conf_Oper[i].mask, "hostmask check failed" );
117
118         if( ! Client_HasMode( Client, 'o' ))
119         {
120                 Client_ModeAdd( Client, 'o' );
121                 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
122                 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
123         }
124
125         if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
126
127         Client_SetOperByMe( Client, true);
128         return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
129 } /* IRC_OPER */
130
131
132 GLOBAL bool
133 IRC_DIE(CLIENT * Client, REQUEST * Req)
134 {
135         /* Shut down server */
136
137         CONN_ID c;
138         CLIENT *cl;
139
140         assert(Client != NULL);
141         assert(Req != NULL);
142
143         if (!Check_Oper(Client))
144                 return No_Privileges(Client, Req);
145
146         /* Bad number of parameters? */
147 #ifdef STRICT_RFC
148         if (Req->argc != 0)
149 #else
150         if (Req->argc > 1)
151 #endif
152                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
153                                           Client_ID(Client), Req->command);
154
155         /* Is a message given? */
156         if (Req->argc > 0) {
157                 c = Conn_First();
158                 while (c != NONE) {
159                         cl = Conn_GetClient(c);
160                         if (Client_Type(cl) == CLIENT_USER)
161                                 IRC_WriteStrClient(cl, "NOTICE %s :%s",
162                                                 Client_ID(cl), Req->argv[0]);
163                         c = Conn_Next(c);
164                 }
165         }
166
167         Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
168             Client_Mask(Client));
169         NGIRCd_SignalQuit = true;
170
171         return CONNECTED;
172 } /* IRC_DIE */
173
174
175 GLOBAL bool
176 IRC_REHASH( CLIENT *Client, REQUEST *Req )
177 {
178         /* Reload configuration file */
179
180         assert( Client != NULL );
181         assert( Req != NULL );
182
183         if (!Check_Oper(Client))
184                 return No_Privileges(Client, Req);
185
186         /* Bad number of parameters? */
187         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
188
189         Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
190         NGIRCd_SignalRehash = true;
191         
192         return CONNECTED;
193 } /* IRC_REHASH */
194
195
196 GLOBAL bool
197 IRC_RESTART( CLIENT *Client, REQUEST *Req )
198 {
199         /* Restart IRC server (fork a new process) */
200
201         assert( Client != NULL );
202         assert( Req != NULL );
203
204         if (!Check_Oper(Client))
205                 return No_Privileges(Client, Req);
206
207         /* Bad number of parameters? */
208         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
209
210         Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
211         NGIRCd_SignalRestart = true;
212         return CONNECTED;
213 } /* IRC_RESTART */
214
215
216 /**
217  * Connect configured or new server.
218  */
219 GLOBAL bool
220 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
221 {
222         assert(Client != NULL);
223         assert(Req != NULL);
224
225         if (!Check_Oper(Client))
226                 return No_Privileges(Client, Req);
227
228         /* Bad number of parameters? */
229         if ((Req->argc != 1) && (Req->argc != 2) && (Req->argc != 5))
230                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
231                                           Client_ID(Client), Req->command);
232
233         /* Invalid port number? */
234         if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
235                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
236                                           Client_ID(Client), Req->command);
237
238         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
239                         "Received CONNECT %s from %s",
240                         Req->argv[0], Client_ID(Client));
241
242         Log(LOG_NOTICE | LOG_snotice,
243             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
244             Req->argv[0]);
245
246         switch (Req->argc) {
247         case 1:
248                 if (!Conf_EnablePassiveServer(Req->argv[0]))
249                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
250                                                   Client_ID(Client),
251                                                   Req->argv[0]);
252         break;
253         case 2:
254                 /* Connect configured server */
255                 if (!Conf_EnableServer
256                     (Req->argv[0], (UINT16) atoi(Req->argv[1])))
257                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
258                                                   Client_ID(Client),
259                                                   Req->argv[0]);
260         break;
261         default:
262                 /* Add server */
263                 if (!Conf_AddServer
264                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
265                      Req->argv[3], Req->argv[4]))
266                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
267                                                   Client_ID(Client),
268                                                   Req->argv[0]);
269         }
270
271         return CONNECTED;
272 } /* IRC_CONNECT */
273
274
275 /**
276  * Disconnect (and disable) configured server.
277  */
278 GLOBAL bool
279 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
280 {
281         CONN_ID my_conn;
282
283         assert(Client != NULL);
284         assert(Req != NULL);
285
286         if (!Check_Oper(Client))
287                 return No_Privileges(Client, Req);
288
289         /* Bad number of parameters? */
290         if (Req->argc != 1)
291                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
292                                           Client_ID(Client), Req->command);
293
294         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
295                         "Received DISCONNECT %s from %s",
296                         Req->argv[0], Client_ID(Client));
297
298         Log(LOG_NOTICE | LOG_snotice,
299             "Got DISCONNECT command from \"%s\" for \"%s\".",
300             Client_Mask(Client), Req->argv[0]);
301
302         /* Save ID of this connection */
303         my_conn = Client_Conn(Client);
304
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]);
309
310         /* Are we still connected or were we killed, too? */
311         if (Conn_GetClient(my_conn))
312                 return CONNECTED;
313         else
314                 return DISCONNECTED;
315 } /* IRC_DISCONNECT */
316
317
318 GLOBAL bool
319 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
320 {
321         CLIENT *from;
322
323         assert( Client != NULL );
324         assert( Req != NULL );
325
326         if (Req->argc != 1)
327                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
328
329         switch (Client_Type(Client)) {
330         case CLIENT_USER:
331                 if (!Client_OperByMe(Client))
332                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
333                 from = Client;
334                 break;
335         case CLIENT_SERVER:
336                 from = Client_Search(Req->prefix);
337                 break;
338         default:
339                 return CONNECTED;
340         }
341
342         if (!from)
343                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
344
345         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
346         return CONNECTED;
347 } /* IRC_WALLOPS */
348
349
350 /* -eof- */