]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
Check_Oper(): check origin of forwarded messages instead of server.
[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, REQUEST * Req)
59 {
60         CLIENT *c;
61
62         assert(Client != NULL);
63         assert(Req != NULL);
64
65         if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
66                 c = Client_Search(Req->prefix);
67         else
68                 c = Client;
69         if (!c)
70                 return false;
71         if (!Client_HasMode(c, 'o'))
72                 return false;
73         if (!Client_OperByMe(c) && !Conf_AllowRemoteOper)
74                 return false;
75         /* The client is an local IRC operator, or this server is configured
76          * to trust remote operators. */
77         return true;
78 } /* CheckOper */
79
80
81 /**
82  * Return and log a "no privileges" message.
83  */
84 static bool
85 No_Privileges(CLIENT * Client, REQUEST * Req)
86 {
87         CLIENT *from = NULL;
88
89         if (Req->prefix) 
90                 from = Client_Search(Req->prefix);
91
92         if (from) {
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,
96                                           Client_ID(from));
97         } else {
98                 Log(LOG_NOTICE, "No privileges: client \"%s\", command \"%s\"",
99                     Client_Mask(Client), Req->command);
100                 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
101                                           Client_ID(Client));
102         }
103 } /* PermissionDenied */
104
105
106 GLOBAL bool
107 IRC_OPER( CLIENT *Client, REQUEST *Req )
108 {
109         unsigned int i;
110
111         assert( Client != NULL );
112         assert( Req != NULL );
113
114         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
115
116         for( i = 0; i < Conf_Oper_Count; i++)
117         {
118                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
119         }
120         if( i >= Conf_Oper_Count )
121                 return Bad_OperPass(Client, Req->argv[0], "not configured");
122
123         if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
124                 return Bad_OperPass(Client, Conf_Oper[i].name, "bad password");
125
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" );
128
129         if( ! Client_HasMode( Client, 'o' ))
130         {
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 ));
134         }
135
136         if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
137
138         Client_SetOperByMe( Client, true);
139         return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
140 } /* IRC_OPER */
141
142
143 GLOBAL bool
144 IRC_DIE(CLIENT * Client, REQUEST * Req)
145 {
146         /* Shut down server */
147
148         CONN_ID c;
149         CLIENT *cl;
150
151         assert(Client != NULL);
152         assert(Req != NULL);
153
154         if (!Check_Oper(Client, Req))
155                 return No_Privileges(Client, Req);
156
157         /* Bad number of parameters? */
158 #ifdef STRICT_RFC
159         if (Req->argc != 0)
160 #else
161         if (Req->argc > 1)
162 #endif
163                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
164                                           Client_ID(Client), Req->command);
165
166         /* Is a message given? */
167         if (Req->argc > 0) {
168                 c = Conn_First();
169                 while (c != NONE) {
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]);
174                         c = Conn_Next(c);
175                 }
176         }
177
178         Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
179             Client_Mask(Client));
180         NGIRCd_SignalQuit = true;
181
182         return CONNECTED;
183 } /* IRC_DIE */
184
185
186 GLOBAL bool
187 IRC_REHASH( CLIENT *Client, REQUEST *Req )
188 {
189         /* Reload configuration file */
190
191         assert( Client != NULL );
192         assert( Req != NULL );
193
194         if (!Check_Oper(Client, Req))
195                 return No_Privileges(Client, Req);
196
197         /* Bad number of parameters? */
198         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
199
200         Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
201         NGIRCd_SignalRehash = true;
202         
203         return CONNECTED;
204 } /* IRC_REHASH */
205
206
207 GLOBAL bool
208 IRC_RESTART( CLIENT *Client, REQUEST *Req )
209 {
210         /* Restart IRC server (fork a new process) */
211
212         assert( Client != NULL );
213         assert( Req != NULL );
214
215         if (!Check_Oper(Client, Req))
216                 return No_Privileges(Client, Req);
217
218         /* Bad number of parameters? */
219         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
220
221         Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
222         NGIRCd_SignalRestart = true;
223         return CONNECTED;
224 } /* IRC_RESTART */
225
226
227 /**
228  * Connect configured or new server.
229  */
230 GLOBAL bool
231 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
232 {
233         assert(Client != NULL);
234         assert(Req != NULL);
235
236         if (!Check_Oper(Client, Req))
237                 return No_Privileges(Client, Req);
238
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);
243
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);
248
249         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
250                         "Received CONNECT %s from %s",
251                         Req->argv[0], Client_ID(Client));
252
253         Log(LOG_NOTICE | LOG_snotice,
254             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
255             Req->argv[0]);
256
257         switch (Req->argc) {
258         case 1:
259                 if (!Conf_EnablePassiveServer(Req->argv[0]))
260                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
261                                                   Client_ID(Client),
262                                                   Req->argv[0]);
263         break;
264         case 2:
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,
269                                                   Client_ID(Client),
270                                                   Req->argv[0]);
271         break;
272         default:
273                 /* Add server */
274                 if (!Conf_AddServer
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,
278                                                   Client_ID(Client),
279                                                   Req->argv[0]);
280         }
281
282         return CONNECTED;
283 } /* IRC_CONNECT */
284
285
286 /**
287  * Disconnect (and disable) configured server.
288  */
289 GLOBAL bool
290 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
291 {
292         CONN_ID my_conn;
293
294         assert(Client != NULL);
295         assert(Req != NULL);
296
297         if (!Check_Oper(Client, Req))
298                 return No_Privileges(Client, Req);
299
300         /* Bad number of parameters? */
301         if (Req->argc != 1)
302                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
303                                           Client_ID(Client), Req->command);
304
305         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
306                         "Received DISCONNECT %s from %s",
307                         Req->argv[0], Client_ID(Client));
308
309         Log(LOG_NOTICE | LOG_snotice,
310             "Got DISCONNECT command from \"%s\" for \"%s\".",
311             Client_Mask(Client), Req->argv[0]);
312
313         /* Save ID of this connection */
314         my_conn = Client_Conn(Client);
315
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]);
320
321         /* Are we still connected or were we killed, too? */
322         if (Conn_GetClient(my_conn))
323                 return CONNECTED;
324         else
325                 return DISCONNECTED;
326 } /* IRC_DISCONNECT */
327
328
329 GLOBAL bool
330 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
331 {
332         CLIENT *from;
333
334         assert( Client != NULL );
335         assert( Req != NULL );
336
337         if (Req->argc != 1)
338                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
339
340         switch (Client_Type(Client)) {
341         case CLIENT_USER:
342                 if (!Client_OperByMe(Client))
343                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
344                 from = Client;
345                 break;
346         case CLIENT_SERVER:
347                 from = Client_Search(Req->prefix);
348                 break;
349         default:
350                 return CONNECTED;
351         }
352
353         if (!from)
354                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
355
356         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
357         return CONNECTED;
358 } /* IRC_WALLOPS */
359
360
361 /* -eof- */