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