]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
88a9e43ed4b1d3b17312bca597802663e7c06783
[ngircd-alex.git] / src / ngircd / irc-oper.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC operator commands
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25
26 #include "ngircd.h"
27 #include "conn-func.h"
28 #include "conf.h"
29 #include "channel.h"
30 #include "class.h"
31 #include "parse.h"
32 #include "irc.h"
33 #include "irc-macros.h"
34 #include "irc-write.h"
35 #include "lists.h"
36 #include "log.h"
37 #include "match.h"
38 #include "messages.h"
39 #include "op.h"
40
41 #include <exp.h>
42 #include "irc-oper.h"
43
44 /**
45  * Handle invalid received OPER command.
46  * Log OPER attempt and send error message to client.
47  */
48 static bool
49 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
50 {
51         Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s!",
52             Client_Mask(Client), errtoken, errmsg);
53         /* Increase penalty to slow down possible brute force attacks */
54         IRC_SetPenalty(Client, 10);
55         return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
56                                   Client_ID(Client));
57 } /* Bad_OperPass */
58
59 /**
60  * Handler for the IRC "OPER" command.
61  *
62  * @param Client The client from which this command has been received.
63  * @param Req Request structure with prefix and all parameters.
64  * @return CONNECTED or DISCONNECTED.
65  */
66 GLOBAL bool
67 IRC_OPER( CLIENT *Client, REQUEST *Req )
68 {
69         struct Conf_Oper *op;
70         size_t len, i;
71
72         assert( Client != NULL );
73         assert( Req != NULL );
74
75         len = array_length(&Conf_Opers, sizeof(*op));
76         op = array_start(&Conf_Opers);
77         for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
78                 ;
79         if (i >= len)
80                 return Bad_OperPass(Client, Req->argv[0], "not configured");
81
82         if (strcmp(op[i].pwd, Req->argv[1]) != 0)
83                 return Bad_OperPass(Client, op[i].name, "bad password");
84
85         if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
86                 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
87
88         if (!Client_HasMode(Client, 'o')) {
89                 Client_ModeAdd(Client, 'o');
90                 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
91                                         Client_ID(Client)))
92                         return DISCONNECTED;
93                 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
94                                           Client_ID(Client));
95         }
96
97         Log(LOG_NOTICE|LOG_snotice,
98             "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
99             Req->argv[0], Client_Mask(Client));
100
101         return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
102 } /* IRC_OPER */
103
104 /**
105  * Handler for the IRC "DIE" command.
106  *
107  * @param Client The client from which this command has been received.
108  * @param Req Request structure with prefix and all parameters.
109  * @return CONNECTED or DISCONNECTED.
110  */
111 GLOBAL bool
112 IRC_DIE(CLIENT * Client, REQUEST * Req)
113 {
114         /* Shut down server */
115
116         CONN_ID c;
117         CLIENT *cl;
118
119         assert(Client != NULL);
120         assert(Req != NULL);
121
122         if (!Op_Check(Client, Req))
123                 return Op_NoPrivileges(Client, Req);
124
125         /* Is a message given? */
126         if (Req->argc > 0) {
127                 c = Conn_First();
128                 while (c != NONE) {
129                         cl = Conn_GetClient(c);
130                         if (Client_Type(cl) == CLIENT_USER)
131                                 IRC_WriteStrClient(cl, "NOTICE %s :%s",
132                                                 Client_ID(cl), Req->argv[0]);
133                         c = Conn_Next(c);
134                 }
135         }
136
137         Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
138             Client_Mask(Client));
139         NGIRCd_SignalQuit = true;
140
141         return CONNECTED;
142 } /* IRC_DIE */
143
144 /**
145  * Handler for the IRC "REHASH" command.
146  *
147  * @param Client The client from which this command has been received.
148  * @param Req Request structure with prefix and all parameters.
149  * @return CONNECTED or DISCONNECTED.
150  */
151 GLOBAL bool
152 IRC_REHASH( CLIENT *Client, REQUEST *Req )
153 {
154         /* Reload configuration file */
155
156         assert( Client != NULL );
157         assert( Req != NULL );
158
159         if (!Op_Check(Client, Req))
160                 return Op_NoPrivileges(Client, Req);
161
162         Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
163             Client_Mask(Client));
164         IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
165
166         raise(SIGHUP);
167
168         return CONNECTED;
169 } /* IRC_REHASH */
170
171 /**
172  * Handler for the IRC "RESTART" command.
173  *
174  * @param Client The client from which this command has been received.
175  * @param Req Request structure with prefix and all parameters.
176  * @return CONNECTED or DISCONNECTED.
177  */
178 GLOBAL bool
179 IRC_RESTART( CLIENT *Client, REQUEST *Req )
180 {
181         /* Restart IRC server (fork a new process) */
182
183         assert( Client != NULL );
184         assert( Req != NULL );
185
186         if (!Op_Check(Client, Req))
187                 return Op_NoPrivileges(Client, Req);
188
189         Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
190             Client_Mask(Client));
191         NGIRCd_SignalRestart = true;
192
193         return CONNECTED;
194 } /* IRC_RESTART */
195
196 /**
197  * Handler for the IRC "CONNECT" command.
198  *
199  * @param Client The client from which this command has been received.
200  * @param Req Request structure with prefix and all parameters.
201  * @return CONNECTED or DISCONNECTED.
202  */
203 GLOBAL bool
204 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
205 {
206         CLIENT *from, *target;
207
208         assert(Client != NULL);
209         assert(Req != NULL);
210
211         /* Bad number of parameters? */
212         if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
213             Req->argc != 5 && Req->argc != 6)
214                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
215                                           Client_ID(Client), Req->command);
216
217         /* Invalid port number? */
218         if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
219                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
220                                           Client_ID(Client), Req->command);
221
222         if (Client_Type(Client) != CLIENT_SERVER
223             && !Client_HasMode(Client, 'o'))
224                 return Op_NoPrivileges(Client, Req);
225
226         from = Client;
227         target = Client_ThisServer();
228
229         if (Req->argc == 3 || Req->argc == 6) {
230                 /* This CONNECT has a target parameter */
231                 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
232                         from = Client_Search(Req->prefix);
233                 if (! from)
234                         return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
235                                                   Client_ID(Client), Req->prefix);
236
237                 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
238                                           : Client_Search(Req->argv[5]);
239                 if (! target || Client_Type(target) != CLIENT_SERVER)
240                         return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
241                                                   Client_ID(from), Req->argv[0]);
242         }
243
244         if (target != Client_ThisServer()) {
245                 /* Forward CONNECT command ... */
246                 if (Req->argc == 3)
247                         IRC_WriteStrClientPrefix(target, from,
248                                  "CONNECT %s %s :%s", Req->argv[0],
249                                  Req->argv[1], Req->argv[2]);
250                 else
251                         IRC_WriteStrClientPrefix(target, from,
252                                 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
253                                 Req->argv[1], Req->argv[2], Req->argv[3],
254                                 Req->argv[4], Req->argv[5]);
255                 return CONNECTED;
256         }
257
258         if (!Op_Check(from, Req))
259                 return Op_NoPrivileges(Client, Req);
260
261         switch (Req->argc) {
262         case 1:
263                 if (!Conf_EnablePassiveServer(Req->argv[0]))
264                         return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
265                                                   Client_ID(from),
266                                                   Req->argv[0]);
267                 break;
268         case 2:
269         case 3:
270                 /* Connect configured server */
271                 if (!Conf_EnableServer
272                     (Req->argv[0], (UINT16) atoi(Req->argv[1])))
273                         return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
274                                                   Client_ID(from),
275                                                   Req->argv[0]);
276                 break;
277         default:
278                 /* Add server */
279                 if (!Conf_AddServer
280                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
281                      Req->argv[3], Req->argv[4]))
282                         return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
283                                                   Client_ID(from),
284                                                   Req->argv[0]);
285         }
286
287         Log(LOG_NOTICE | LOG_snotice,
288             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
289             Req->argv[0]);
290         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
291                         "Received CONNECT %s from %s",
292                         Req->argv[0], Client_ID(from));
293
294         return CONNECTED;
295 } /* IRC_CONNECT */
296
297 /**
298  * Handler for the IRC "DISCONNECT" command.
299  *
300  * This command is not specified in the IRC RFCs, it is an extension
301  * of ngIRCd: it shuts down and disables a configured server connection.
302  *
303  * @param Client The client from which this command has been received.
304  * @param Req Request structure with prefix and all parameters.
305  * @return CONNECTED or DISCONNECTED.
306  */
307 GLOBAL bool
308 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
309 {
310         CONN_ID my_conn;
311
312         assert(Client != NULL);
313         assert(Req != NULL);
314
315         if (!Op_Check(Client, Req))
316                 return Op_NoPrivileges(Client, Req);
317
318         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
319                         "Received DISCONNECT %s from %s",
320                         Req->argv[0], Client_ID(Client));
321
322         Log(LOG_NOTICE | LOG_snotice,
323             "Got DISCONNECT command from \"%s\" for \"%s\".",
324             Client_Mask(Client), Req->argv[0]);
325
326         /* Save ID of this connection */
327         my_conn = Client_Conn(Client);
328
329         /* Disconnect configured server */
330         if (!Conf_DisableServer(Req->argv[0]))
331                 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
332                                           Client_ID(Client), Req->argv[0]);
333
334         /* Are we still connected or were we killed, too? */
335         if (Conn_GetClient(my_conn))
336                 return CONNECTED;
337         else
338                 return DISCONNECTED;
339 } /* IRC_DISCONNECT */
340
341 /**
342  * Handler for the IRC "WALLOPS" command.
343  *
344  * @param Client The client from which this command has been received.
345  * @param Req Request structure with prefix and all parameters.
346  * @return CONNECTED or DISCONNECTED.
347  */
348 GLOBAL bool
349 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
350 {
351         CLIENT *from;
352
353         assert( Client != NULL );
354         assert( Req != NULL );
355
356         switch (Client_Type(Client)) {
357         case CLIENT_USER:
358                 if (!Op_Check(Client, Req))
359                         return Op_NoPrivileges(Client, Req);
360                 from = Client;
361                 break;
362         case CLIENT_SERVER:
363                 from = Client_Search(Req->prefix);
364                 break;
365         default:
366                 return CONNECTED;
367         }
368
369         if (!from)
370                 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
371                                           Client_ID(Client), Req->prefix);
372
373         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
374         return CONNECTED;
375 } /* IRC_WALLOPS */
376
377 /**
378  * Handle <?>LINE commands (GLINE, KLINE).
379  *
380  * @param Client The client from which this command has been received.
381  * @param Req Request structure with prefix and all parameters.
382  * @return CONNECTED or DISCONNECTED.
383  */
384 GLOBAL bool
385 IRC_xLINE(CLIENT *Client, REQUEST *Req)
386 {
387         CLIENT *from, *c, *c_next;
388         char reason[COMMAND_LEN], class_c;
389         struct list_head *list;
390         time_t timeout;
391         int class;
392
393         assert(Client != NULL);
394         assert(Req != NULL);
395
396         /* Bad number of parameters? */
397         if (Req->argc != 1 && Req->argc != 3)
398                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
399                                           Client_ID(Client), Req->command);
400
401         from = Op_Check(Client, Req);
402         if (!from)
403                 return Op_NoPrivileges(Client, Req);
404
405         switch(Req->command[0]) {
406                 case 'g':
407                 case 'G':
408                         class = CLASS_GLINE; class_c = 'G';
409                         break;
410                 case 'k':
411                 case 'K':
412                         class = CLASS_KLINE; class_c = 'K';
413                         break;
414                 default:
415                         Log(LOG_CRIT,
416                             "IRC_xLINE() called for unknown line: %c!? Ignored.",
417                             Req->command[0]);
418                         return CONNECTED;
419         }
420
421         if (Req->argc == 1) {
422                 /* Delete mask from list */
423                 Class_DeleteMask(class, Req->argv[0]);
424                 Log(LOG_NOTICE|LOG_snotice,
425                     "\"%s\" deleted \"%s\" from %c-Line list.",
426                     Client_Mask(from), Req->argv[0], class_c);
427                 if (class == CLASS_GLINE) {
428                         /* Inform other servers */
429                         IRC_WriteStrServersPrefix(Client, from, "%s %s",
430                                                   Req->command, Req->argv[0]);
431
432                 }
433         } else {
434                 /* Add new mask to list */
435                 timeout = atol(Req->argv[1]);
436                 if (timeout > 0)
437                         timeout += time(NULL);
438                 if (Class_AddMask(class, Req->argv[0],
439                                   timeout,
440                                   Req->argv[2])) {
441                         Log(LOG_NOTICE|LOG_snotice,
442                             "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
443                             Client_Mask(from), Req->argv[0], class_c,
444                             Req->argv[2], atol(Req->argv[1]));
445                         if (class == CLASS_GLINE) {
446                                 /* Inform other servers */
447                                 IRC_WriteStrServersPrefix(Client, from,
448                                                 "%s %s %s :%s", Req->command,
449                                                 Req->argv[0], Req->argv[1],
450                                                 Req->argv[2]);
451                         }
452
453                         /* Check currently connected clients */
454                         snprintf(reason, sizeof(reason), "%c-Line by \"%s\": \"%s\"",
455                                  class_c, Client_ID(from), Req->argv[2]);
456                         list = Class_GetList(class);
457                         c = Client_First();
458                         while (c) {
459                                 c_next = Client_Next(c);
460                                 if ((class == CLASS_GLINE || Client_Conn(c) > NONE)
461                                     && Lists_Check(list, c))
462                                         IRC_KillClient(Client, NULL,
463                                                        Client_ID(c), reason);
464                                 c = c_next;
465                         }
466                 }
467         }
468
469         return CONNECTED;
470 }
471
472 /* -eof- */