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