]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
Allow forwarding of CONNECT commands.
[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         CLIENT *from, *target;
234
235         assert(Client != NULL);
236         assert(Req != NULL);
237
238         if (!Check_Oper(Client, Req))
239                 return No_Privileges(Client, Req);
240
241         /* Bad number of parameters? */
242         if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
243             Req->argc != 5 && Req->argc != 6)
244                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
245                                           Client_ID(Client), Req->command);
246
247         /* Invalid port number? */
248         if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
249                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
250                                           Client_ID(Client), Req->command);
251
252         from = Client;
253         target = Client_ThisServer();
254
255         if (Req->argc == 3 || Req->argc == 6) {
256                 /* This CONNECT has a target parameter */
257                 if (Client_Type(Client) == CLIENT_SERVER)
258                         from = Client_Search(Req->prefix);
259                 if (! from)
260                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
261                                         Client_ID(Client), Req->prefix);
262
263                 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
264                                           : Client_Search(Req->argv[5]);
265                 if (! target || Client_Type(target) != CLIENT_SERVER)
266                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
267                                         Client_ID(from), Req->argv[0]);
268         }
269
270         if (target != Client_ThisServer()) {
271                 /* Forward CONNECT command ... */
272                 if (Req->argc == 3)
273                         IRC_WriteStrClientPrefix(target, from,
274                                  "CONNECT %s %s :%s", Req->argv[0],
275                                  Req->argv[1], Req->argv[2]);
276                 else
277                         IRC_WriteStrClientPrefix(target, from,
278                                 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
279                                 Req->argv[1], Req->argv[2], Req->argv[3],
280                                 Req->argv[4], Req->argv[5]);
281                 return CONNECTED;
282         }
283
284         Log(LOG_NOTICE | LOG_snotice,
285             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
286             Req->argv[0]);
287         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
288                         "Received CONNECT %s from %s",
289                         Req->argv[0], Client_ID(from));
290
291         switch (Req->argc) {
292         case 1:
293                 if (!Conf_EnablePassiveServer(Req->argv[0]))
294                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
295                                                   Client_ID(from),
296                                                   Req->argv[0]);
297                 break;
298         case 2:
299         case 3:
300                 /* Connect configured server */
301                 if (!Conf_EnableServer
302                     (Req->argv[0], (UINT16) atoi(Req->argv[1])))
303                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
304                                                   Client_ID(from),
305                                                   Req->argv[0]);
306                 break;
307         default:
308                 /* Add server */
309                 if (!Conf_AddServer
310                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
311                      Req->argv[3], Req->argv[4]))
312                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
313                                                   Client_ID(from),
314                                                   Req->argv[0]);
315         }
316
317         return CONNECTED;
318 } /* IRC_CONNECT */
319
320
321 /**
322  * Disconnect (and disable) configured server.
323  */
324 GLOBAL bool
325 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
326 {
327         CONN_ID my_conn;
328
329         assert(Client != NULL);
330         assert(Req != NULL);
331
332         if (!Check_Oper(Client, Req))
333                 return No_Privileges(Client, Req);
334
335         /* Bad number of parameters? */
336         if (Req->argc != 1)
337                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
338                                           Client_ID(Client), Req->command);
339
340         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
341                         "Received DISCONNECT %s from %s",
342                         Req->argv[0], Client_ID(Client));
343
344         Log(LOG_NOTICE | LOG_snotice,
345             "Got DISCONNECT command from \"%s\" for \"%s\".",
346             Client_Mask(Client), Req->argv[0]);
347
348         /* Save ID of this connection */
349         my_conn = Client_Conn(Client);
350
351         /* Disconnect configured server */
352         if (!Conf_DisableServer(Req->argv[0]))
353                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
354                                           Client_ID(Client), Req->argv[0]);
355
356         /* Are we still connected or were we killed, too? */
357         if (Conn_GetClient(my_conn))
358                 return CONNECTED;
359         else
360                 return DISCONNECTED;
361 } /* IRC_DISCONNECT */
362
363
364 GLOBAL bool
365 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
366 {
367         CLIENT *from;
368
369         assert( Client != NULL );
370         assert( Req != NULL );
371
372         if (Req->argc != 1)
373                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
374
375         switch (Client_Type(Client)) {
376         case CLIENT_USER:
377                 if (!Client_OperByMe(Client))
378                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
379                 from = Client;
380                 break;
381         case CLIENT_SERVER:
382                 from = Client_Search(Req->prefix);
383                 break;
384         default:
385                 return CONNECTED;
386         }
387
388         if (!from)
389                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
390
391         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
392         return CONNECTED;
393 } /* IRC_WALLOPS */
394
395
396 /* -eof- */