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