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