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