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