]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
Use functions provided by op.c "module"
[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         unsigned int 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         for( i = 0; i < Conf_Oper_Count; i++)
66         {
67                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
68         }
69         if( i >= Conf_Oper_Count )
70                 return Bad_OperPass(Client, Req->argv[0], "not configured");
71
72         if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
73                 return Bad_OperPass(Client, Conf_Oper[i].name, "bad password");
74
75         if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
76                 return Bad_OperPass(Client, Conf_Oper[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         NGIRCd_SignalRehash = true;
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 (!Op_Check(Client, Req))
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)
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         Log(LOG_NOTICE | LOG_snotice,
234             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
235             Req->argv[0]);
236         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
237                         "Received CONNECT %s from %s",
238                         Req->argv[0], Client_ID(from));
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         return CONNECTED;
267 } /* IRC_CONNECT */
268
269
270 /**
271  * Disconnect (and disable) configured server.
272  */
273 GLOBAL bool
274 IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
275 {
276         CONN_ID my_conn;
277
278         assert(Client != NULL);
279         assert(Req != NULL);
280
281         if (!Op_Check(Client, Req))
282                 return Op_NoPrivileges(Client, Req);
283
284         /* Bad number of parameters? */
285         if (Req->argc != 1)
286                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
287                                           Client_ID(Client), Req->command);
288
289         IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
290                         "Received DISCONNECT %s from %s",
291                         Req->argv[0], Client_ID(Client));
292
293         Log(LOG_NOTICE | LOG_snotice,
294             "Got DISCONNECT command from \"%s\" for \"%s\".",
295             Client_Mask(Client), Req->argv[0]);
296
297         /* Save ID of this connection */
298         my_conn = Client_Conn(Client);
299
300         /* Disconnect configured server */
301         if (!Conf_DisableServer(Req->argv[0]))
302                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
303                                           Client_ID(Client), Req->argv[0]);
304
305         /* Are we still connected or were we killed, too? */
306         if (Conn_GetClient(my_conn))
307                 return CONNECTED;
308         else
309                 return DISCONNECTED;
310 } /* IRC_DISCONNECT */
311
312
313 GLOBAL bool
314 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
315 {
316         CLIENT *from;
317
318         assert( Client != NULL );
319         assert( Req != NULL );
320
321         if (Req->argc != 1)
322                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
323
324         switch (Client_Type(Client)) {
325         case CLIENT_USER:
326                 if (!Client_OperByMe(Client))
327                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
328                 from = Client;
329                 break;
330         case CLIENT_SERVER:
331                 from = Client_Search(Req->prefix);
332                 break;
333         default:
334                 return CONNECTED;
335         }
336
337         if (!from)
338                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
339
340         IRC_SendWallops(Client, from, "%s", Req->argv[0]);
341         return CONNECTED;
342 } /* IRC_WALLOPS */
343
344
345 /* -eof- */