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