]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
Remove CLIENT.oper_by_my, Client_SetOperByMe() and Client_OperByMe()
[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 "irc-macros.h"
32 #include "irc-write.h"
33 #include "log.h"
34 #include "match.h"
35 #include "messages.h"
36 #include "parse.h"
37 #include "op.h"
38
39 #include <exp.h>
40 #include "irc-oper.h"
41
42 /**
43  * Handle invalid received OPER command.
44  * Log OPER attempt and send error message to client.
45  */
46 static bool
47 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
48 {
49         Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
50             Client_Mask(Client), errtoken, errmsg);
51         return IRC_WriteErrClient(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                 IRC_SetPenalty(Client, 2);
211                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
212                                           Client_ID(Client), Req->command);
213         }
214
215         /* Invalid port number? */
216         if ((Req->argc > 1) && atoi(Req->argv[1]) < 1) {
217                 IRC_SetPenalty(Client, 2);
218                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
219                                           Client_ID(Client), Req->command);
220         }
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;
388         int class;
389         char class_c;
390
391         assert(Client != NULL);
392         assert(Req != NULL);
393
394         /* Bad number of parameters? */
395         if (Req->argc != 1 && Req->argc != 3) {
396                 IRC_SetPenalty(Client, 2);
397                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
398                                           Client_ID(Client), Req->command);
399         }
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                 if (Class_AddMask(class, Req->argv[0],
436                                   time(NULL) + atol(Req->argv[1]),
437                                   Req->argv[2])) {
438                         Log(LOG_NOTICE|LOG_snotice,
439                             "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
440                             Client_Mask(from), Req->argv[0], class_c,
441                             Req->argv[2], atol(Req->argv[1]));
442                         if (class == CLASS_GLINE) {
443                                 /* Inform other servers */
444                                 IRC_WriteStrServersPrefix(Client, from,
445                                                 "%s %s %s :%s", Req->command,
446                                                 Req->argv[0], Req->argv[1],
447                                                 Req->argv[2]);
448                         }
449                 }
450         }
451
452         return CONNECTED;
453 }
454
455 /* -eof- */