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