]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
irc-oper.c: code cleanup; more documentation
[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 "irc-write.h"
31 #include "log.h"
32 #include "match.h"
33 #include "messages.h"
34 #include "parse.h"
35 #include "op.h"
36
37 #include <exp.h>
38 #include "irc-oper.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         IRC_SetPenalty(Client, 3);
50         return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
51                                   Client_ID(Client));
52 } /* Bad_OperPass */
53
54 /**
55  * Handler for the IRC "OPER" command.
56  *
57  * See RFC 2812, 3.1.4 "Oper message".
58  *
59  * @param Client The client from which this command has been received.
60  * @param Req Request structure with prefix and all parameters.
61  * @return CONNECTED or DISCONNECTED.
62  */
63 GLOBAL bool
64 IRC_OPER( CLIENT *Client, REQUEST *Req )
65 {
66         struct Conf_Oper *op;
67         size_t len, i;
68
69         assert( Client != NULL );
70         assert( Req != NULL );
71
72         if (Req->argc != 2)
73                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
74                                           Client_ID(Client), Req->command);
75
76         len = array_length(&Conf_Opers, sizeof(*op));
77         op = array_start(&Conf_Opers);
78         for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
79                 ;
80         if (i >= len)
81                 return Bad_OperPass(Client, Req->argv[0], "not configured");
82
83         if (strcmp(op[i].pwd, Req->argv[1]) != 0)
84                 return Bad_OperPass(Client, op[i].name, "bad password");
85
86         if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
87                 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
88
89         if (!Client_HasMode(Client, 'o')) {
90                 Client_ModeAdd(Client, 'o');
91                 if (!IRC_WriteStrClient(Client, "MODE %s :+o",
92                                         Client_ID(Client)))
93                         return DISCONNECTED;
94                 IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
95                                           Client_ID(Client));
96         }
97
98         if (!Client_OperByMe(Client))
99                 Log(LOG_NOTICE|LOG_snotice,
100                     "Got valid OPER from \"%s\", user is an IRC operator now.",
101                     Client_Mask(Client));
102
103         Client_SetOperByMe(Client, true);
104         return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
105 } /* IRC_OPER */
106
107 /**
108  * Handler for the IRC "DIE" command.
109  *
110  * See RFC 2812, 4.3 "Die message".
111  *
112  * @param Client The client from which this command has been received.
113  * @param Req Request structure with prefix and all parameters.
114  * @return CONNECTED or DISCONNECTED.
115  */
116 GLOBAL bool
117 IRC_DIE(CLIENT * Client, REQUEST * Req)
118 {
119         /* Shut down server */
120
121         CONN_ID c;
122         CLIENT *cl;
123
124         assert(Client != NULL);
125         assert(Req != NULL);
126
127         if (!Op_Check(Client, Req))
128                 return Op_NoPrivileges(Client, Req);
129
130         /* Bad number of parameters? */
131 #ifdef STRICT_RFC
132         if (Req->argc != 0)
133 #else
134         if (Req->argc > 1)
135 #endif
136                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
137                                           Client_ID(Client), Req->command);
138
139         /* Is a message given? */
140         if (Req->argc > 0) {
141                 c = Conn_First();
142                 while (c != NONE) {
143                         cl = Conn_GetClient(c);
144                         if (Client_Type(cl) == CLIENT_USER)
145                                 IRC_WriteStrClient(cl, "NOTICE %s :%s",
146                                                 Client_ID(cl), Req->argv[0]);
147                         c = Conn_Next(c);
148                 }
149         }
150
151         Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
152             Client_Mask(Client));
153         NGIRCd_SignalQuit = true;
154
155         return CONNECTED;
156 } /* IRC_DIE */
157
158 /**
159  * Handler for the IRC "REHASH" command.
160  *
161  * See RFC 2812, 4.2 "Rehash message".
162  *
163  * @param Client The client from which this command has been received.
164  * @param Req Request structure with prefix and all parameters.
165  * @return CONNECTED or DISCONNECTED.
166  */
167 GLOBAL bool
168 IRC_REHASH( CLIENT *Client, REQUEST *Req )
169 {
170         /* Reload configuration file */
171
172         assert( Client != NULL );
173         assert( Req != NULL );
174
175         if (!Op_Check(Client, Req))
176                 return Op_NoPrivileges(Client, Req);
177
178         /* Bad number of parameters? */
179         if (Req->argc != 0)
180                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
181                                           Client_ID(Client), Req->command );
182
183         Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
184             Client_Mask(Client));
185         raise(SIGHUP);
186
187         return CONNECTED;
188 } /* IRC_REHASH */
189
190 /**
191  * Handler for the IRC "RESTART" command.
192  *
193  * See RFC 2812, 4.4 "Restart message".
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_RESTART( CLIENT *Client, REQUEST *Req )
201 {
202         /* Restart IRC server (fork a new process) */
203
204         assert( Client != NULL );
205         assert( Req != NULL );
206
207         if (!Op_Check(Client, Req))
208                 return Op_NoPrivileges(Client, Req);
209
210         /* Bad number of parameters? */
211         if (Req->argc != 0)
212                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
213                                           Client_ID(Client), Req->command);
214
215         Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
216             Client_Mask(Client));
217         NGIRCd_SignalRestart = true;
218
219         return CONNECTED;
220 } /* IRC_RESTART */
221
222 /**
223  * Handler for the IRC "CONNECT" command.
224  *
225  * See RFC 2812, 3.4.7 "Connect message".
226  *
227  * @param Client The client from which this command has been received.
228  * @param Req Request structure with prefix and all parameters.
229  * @return CONNECTED or DISCONNECTED.
230  */
231 GLOBAL bool
232 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
233 {
234         CLIENT *from, *target;
235
236         assert(Client != NULL);
237         assert(Req != NULL);
238
239         if (Client_Type(Client) != CLIENT_SERVER
240             && !Client_HasMode(Client, 'o'))
241                 return Op_NoPrivileges(Client, Req);
242
243         /* Bad number of parameters? */
244         if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
245             Req->argc != 5 && Req->argc != 6)
246                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
247                                           Client_ID(Client), Req->command);
248
249         /* Invalid port number? */
250         if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
251                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
252                                           Client_ID(Client), Req->command);
253
254         from = Client;
255         target = Client_ThisServer();
256
257         if (Req->argc == 3 || Req->argc == 6) {
258                 /* This CONNECT has a target parameter */
259                 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
260                         from = Client_Search(Req->prefix);
261                 if (! from)
262                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
263                                         Client_ID(Client), Req->prefix);
264
265                 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
266                                           : Client_Search(Req->argv[5]);
267                 if (! target || Client_Type(target) != CLIENT_SERVER)
268                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
269                                         Client_ID(from), Req->argv[0]);
270         }
271
272         if (target != Client_ThisServer()) {
273                 /* Forward CONNECT command ... */
274                 if (Req->argc == 3)
275                         IRC_WriteStrClientPrefix(target, from,
276                                  "CONNECT %s %s :%s", Req->argv[0],
277                                  Req->argv[1], Req->argv[2]);
278                 else
279                         IRC_WriteStrClientPrefix(target, from,
280                                 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
281                                 Req->argv[1], Req->argv[2], Req->argv[3],
282                                 Req->argv[4], Req->argv[5]);
283                 return CONNECTED;
284         }
285
286         if (!Op_Check(from, Req))
287                 return Op_NoPrivileges(Client, Req);
288
289         switch (Req->argc) {
290         case 1:
291                 if (!Conf_EnablePassiveServer(Req->argv[0]))
292                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
293                                                   Client_ID(from),
294                                                   Req->argv[0]);
295                 break;
296         case 2:
297         case 3:
298                 /* Connect configured server */
299                 if (!Conf_EnableServer
300                     (Req->argv[0], (UINT16) atoi(Req->argv[1])))
301                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
302                                                   Client_ID(from),
303                                                   Req->argv[0]);
304                 break;
305         default:
306                 /* Add server */
307                 if (!Conf_AddServer
308                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
309                      Req->argv[3], Req->argv[4]))
310                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
311                                                   Client_ID(from),
312                                                   Req->argv[0]);
313         }
314
315         Log(LOG_NOTICE | LOG_snotice,
316             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
317             Req->argv[0]);
318         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
319                         "Received CONNECT %s from %s",
320                         Req->argv[0], Client_ID(from));
321
322         return CONNECTED;
323 } /* IRC_CONNECT */
324
325 /**
326  * Handler for the IRC "DISCONNECT" command.
327  *
328  * This command is not specified in the IRC RFCs, it is an extension
329  * of ngIRCd: it shuts down and disables a configured server connection.
330  *
331  * @param Client The client from which this command has been received.
332  * @param Req Request structure with prefix and all parameters.
333  * @return CONNECTED or DISCONNECTED.
334  */
335 GLOBAL bool
336 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
337 {
338         CONN_ID my_conn;
339
340         assert(Client != NULL);
341         assert(Req != NULL);
342
343         if (!Op_Check(Client, Req))
344                 return Op_NoPrivileges(Client, Req);
345
346         /* Bad number of parameters? */
347         if (Req->argc != 1)
348                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
349                                           Client_ID(Client), Req->command);
350
351         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
352                         "Received DISCONNECT %s from %s",
353                         Req->argv[0], Client_ID(Client));
354
355         Log(LOG_NOTICE | LOG_snotice,
356             "Got DISCONNECT command from \"%s\" for \"%s\".",
357             Client_Mask(Client), Req->argv[0]);
358
359         /* Save ID of this connection */
360         my_conn = Client_Conn(Client);
361
362         /* Disconnect configured server */
363         if (!Conf_DisableServer(Req->argv[0]))
364                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
365                                           Client_ID(Client), Req->argv[0]);
366
367         /* Are we still connected or were we killed, too? */
368         if (Conn_GetClient(my_conn))
369                 return CONNECTED;
370         else
371                 return DISCONNECTED;
372 } /* IRC_DISCONNECT */
373
374 /**
375  * Handler for the IRC "WALLOPS" command.
376  *
377  * See RFC 2812, 4.7 "Operwall message".
378  *
379  * @param Client The client from which this command has been received.
380  * @param Req Request structure with prefix and all parameters.
381  * @return CONNECTED or DISCONNECTED.
382  */
383 GLOBAL bool
384 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
385 {
386         CLIENT *from;
387
388         assert( Client != NULL );
389         assert( Req != NULL );
390
391         if (Req->argc != 1)
392                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
393                                           Client_ID(Client), Req->command);
394
395         switch (Client_Type(Client)) {
396         case CLIENT_USER:
397                 if (!Client_OperByMe(Client))
398                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
399                                                   Client_ID(Client));
400                 from = Client;
401                 break;
402         case CLIENT_SERVER:
403                 from = Client_Search(Req->prefix);
404                 break;
405         default:
406                 return CONNECTED;
407         }
408
409         if (!from)
410                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
411                                           Client_ID(Client), Req->prefix);
412
413         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
414         return CONNECTED;
415 } /* IRC_WALLOPS */
416
417
418 /* -eof- */