7887ef6075260dd670d907a5e86e8fe322e8b247
[ngircd-alex.git] / src / ngircd / irc-oper.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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
13 #include "portab.h"
14
15 /**
16  * @file
17  * IRC operator commands
18  */
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26
27 #include "ngircd.h"
28 #include "conn-func.h"
29 #include "conf.h"
30 #include "channel.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 /**
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         IRC_SetPenalty(Client, 3);
52         return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
53                                   Client_ID(Client));
54 } /* Bad_OperPass */
55
56
57 GLOBAL bool
58 IRC_OPER( CLIENT *Client, REQUEST *Req )
59 {
60         struct Conf_Oper *op;
61         size_t len, i;
62
63         assert( Client != NULL );
64         assert( Req != NULL );
65
66         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
67
68         len = array_length(&Conf_Opers, sizeof(*op));
69         op = array_start(&Conf_Opers);
70         for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
71                 ;
72         if (i >= len)
73                 return Bad_OperPass(Client, Req->argv[0], "not configured");
74
75         if (strcmp(op[i].pwd, Req->argv[1]) != 0)
76                 return Bad_OperPass(Client, op[i].name, "bad password");
77
78         if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
79                 return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
80
81         if( ! Client_HasMode( Client, 'o' ))
82         {
83                 Client_ModeAdd( Client, 'o' );
84                 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
85                 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
86         }
87
88         if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
89
90         Client_SetOperByMe( Client, true);
91         return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
92 } /* IRC_OPER */
93
94
95 GLOBAL bool
96 IRC_DIE(CLIENT * Client, REQUEST * Req)
97 {
98         /* Shut down server */
99
100         CONN_ID c;
101         CLIENT *cl;
102
103         assert(Client != NULL);
104         assert(Req != NULL);
105
106         if (!Op_Check(Client, Req))
107                 return Op_NoPrivileges(Client, Req);
108
109         /* Bad number of parameters? */
110 #ifdef STRICT_RFC
111         if (Req->argc != 0)
112 #else
113         if (Req->argc > 1)
114 #endif
115                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
116                                           Client_ID(Client), Req->command);
117
118         /* Is a message given? */
119         if (Req->argc > 0) {
120                 c = Conn_First();
121                 while (c != NONE) {
122                         cl = Conn_GetClient(c);
123                         if (Client_Type(cl) == CLIENT_USER)
124                                 IRC_WriteStrClient(cl, "NOTICE %s :%s",
125                                                 Client_ID(cl), Req->argv[0]);
126                         c = Conn_Next(c);
127                 }
128         }
129
130         Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
131             Client_Mask(Client));
132         NGIRCd_SignalQuit = true;
133
134         return CONNECTED;
135 } /* IRC_DIE */
136
137
138 GLOBAL bool
139 IRC_REHASH( CLIENT *Client, REQUEST *Req )
140 {
141         /* Reload configuration file */
142
143         assert( Client != NULL );
144         assert( Req != NULL );
145
146         if (!Op_Check(Client, Req))
147                 return Op_NoPrivileges(Client, Req);
148
149         /* Bad number of parameters? */
150         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
151
152         Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
153         raise(SIGHUP);
154
155         return CONNECTED;
156 } /* IRC_REHASH */
157
158
159 GLOBAL bool
160 IRC_RESTART( CLIENT *Client, REQUEST *Req )
161 {
162         /* Restart IRC server (fork a new process) */
163
164         assert( Client != NULL );
165         assert( Req != NULL );
166
167         if (!Op_Check(Client, Req))
168                 return Op_NoPrivileges(Client, Req);
169
170         /* Bad number of parameters? */
171         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
172
173         Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
174         NGIRCd_SignalRestart = true;
175         return CONNECTED;
176 } /* IRC_RESTART */
177
178
179 /**
180  * Connect configured or new server.
181  */
182 GLOBAL bool
183 IRC_CONNECT(CLIENT * Client, REQUEST * Req)
184 {
185         CLIENT *from, *target;
186
187         assert(Client != NULL);
188         assert(Req != NULL);
189
190         if (Client_Type(Client) != CLIENT_SERVER
191             && !Client_HasMode(Client, 'o'))
192                 return Op_NoPrivileges(Client, Req);
193
194         /* Bad number of parameters? */
195         if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
196             Req->argc != 5 && Req->argc != 6)
197                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
198                                           Client_ID(Client), Req->command);
199
200         /* Invalid port number? */
201         if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
202                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
203                                           Client_ID(Client), Req->command);
204
205         from = Client;
206         target = Client_ThisServer();
207
208         if (Req->argc == 3 || Req->argc == 6) {
209                 /* This CONNECT has a target parameter */
210                 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
211                         from = Client_Search(Req->prefix);
212                 if (! from)
213                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
214                                         Client_ID(Client), Req->prefix);
215
216                 target = (Req->argc == 3) ? Client_Search(Req->argv[2])
217                                           : Client_Search(Req->argv[5]);
218                 if (! target || Client_Type(target) != CLIENT_SERVER)
219                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
220                                         Client_ID(from), Req->argv[0]);
221         }
222
223         if (target != Client_ThisServer()) {
224                 /* Forward CONNECT command ... */
225                 if (Req->argc == 3)
226                         IRC_WriteStrClientPrefix(target, from,
227                                  "CONNECT %s %s :%s", Req->argv[0],
228                                  Req->argv[1], Req->argv[2]);
229                 else
230                         IRC_WriteStrClientPrefix(target, from,
231                                 "CONNECT %s %s %s %s %s :%s", Req->argv[0],
232                                 Req->argv[1], Req->argv[2], Req->argv[3],
233                                 Req->argv[4], Req->argv[5]);
234                 return CONNECTED;
235         }
236
237         if (!Op_Check(from, Req))
238                 return Op_NoPrivileges(Client, Req);
239
240         switch (Req->argc) {
241         case 1:
242                 if (!Conf_EnablePassiveServer(Req->argv[0]))
243                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
244                                                   Client_ID(from),
245                                                   Req->argv[0]);
246                 break;
247         case 2:
248         case 3:
249                 /* Connect configured server */
250                 if (!Conf_EnableServer
251                     (Req->argv[0], (UINT16) atoi(Req->argv[1])))
252                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
253                                                   Client_ID(from),
254                                                   Req->argv[0]);
255                 break;
256         default:
257                 /* Add server */
258                 if (!Conf_AddServer
259                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
260                      Req->argv[3], Req->argv[4]))
261                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
262                                                   Client_ID(from),
263                                                   Req->argv[0]);
264         }
265
266         Log(LOG_NOTICE | LOG_snotice,
267             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
268             Req->argv[0]);
269         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
270                         "Received CONNECT %s from %s",
271                         Req->argv[0], Client_ID(from));
272
273         return CONNECTED;
274 } /* IRC_CONNECT */
275
276
277 /**
278  * Disconnect (and disable) configured server.
279  */
280 GLOBAL bool
281 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
282 {
283         CONN_ID my_conn;
284
285         assert(Client != NULL);
286         assert(Req != NULL);
287
288         if (!Op_Check(Client, Req))
289                 return Op_NoPrivileges(Client, Req);
290
291         /* Bad number of parameters? */
292         if (Req->argc != 1)
293                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
294                                           Client_ID(Client), Req->command);
295
296         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
297                         "Received DISCONNECT %s from %s",
298                         Req->argv[0], Client_ID(Client));
299
300         Log(LOG_NOTICE | LOG_snotice,
301             "Got DISCONNECT command from \"%s\" for \"%s\".",
302             Client_Mask(Client), Req->argv[0]);
303
304         /* Save ID of this connection */
305         my_conn = Client_Conn(Client);
306
307         /* Disconnect configured server */
308         if (!Conf_DisableServer(Req->argv[0]))
309                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
310                                           Client_ID(Client), Req->argv[0]);
311
312         /* Are we still connected or were we killed, too? */
313         if (Conn_GetClient(my_conn))
314                 return CONNECTED;
315         else
316                 return DISCONNECTED;
317 } /* IRC_DISCONNECT */
318
319
320 GLOBAL bool
321 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
322 {
323         CLIENT *from;
324
325         assert( Client != NULL );
326         assert( Req != NULL );
327
328         if (Req->argc != 1)
329                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
330
331         switch (Client_Type(Client)) {
332         case CLIENT_USER:
333                 if (!Client_OperByMe(Client))
334                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
335                 from = Client;
336                 break;
337         case CLIENT_SERVER:
338                 from = Client_Search(Req->prefix);
339                 break;
340         default:
341                 return CONNECTED;
342         }
343
344         if (!from)
345                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
346
347         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
348         return CONNECTED;
349 } /* IRC_WALLOPS */
350
351
352 /* -eof- */