]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
Bad_OperPass(): code cleanup.
[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 GLOBAL bool
54 IRC_OPER( CLIENT *Client, REQUEST *Req )
55 {
56         unsigned int i;
57
58         assert( Client != NULL );
59         assert( Req != NULL );
60
61         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
62
63         for( i = 0; i < Conf_Oper_Count; i++)
64         {
65                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
66         }
67         if( i >= Conf_Oper_Count )
68                 return Bad_OperPass(Client, Req->argv[0], "not configured");
69
70         if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
71                 return Bad_OperPass(Client, Conf_Oper[i].name, "bad password");
72
73         if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
74                 return Bad_OperPass(Client, Conf_Oper[i].mask, "hostmask check failed" );
75
76         if( ! Client_HasMode( Client, 'o' ))
77         {
78                 Client_ModeAdd( Client, 'o' );
79                 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
80                 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
81         }
82
83         if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
84
85         Client_SetOperByMe( Client, true);
86         return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
87 } /* IRC_OPER */
88
89
90 GLOBAL bool
91 IRC_DIE(CLIENT * Client, REQUEST * Req)
92 {
93         /* Shut down server */
94
95         CONN_ID c;
96         CLIENT *cl;
97
98         assert(Client != NULL);
99         assert(Req != NULL);
100
101         /* Not a local IRC operator? */
102         if ((!Client_HasMode(Client, 'o')) || (!Client_OperByMe(Client)))
103                 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
104                                           Client_ID(Client));
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         /* Not a local IRC operator? */
144         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
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         /* Not a local IRC operator? */
165         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
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
183         assert(Client != NULL);
184         assert(Req != NULL);
185
186         /* Not a local IRC operator? */
187         if ((!Client_HasMode(Client, 'o')) || (!Client_OperByMe(Client)))
188                 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
189                                           Client_ID(Client));
190
191         /* Bad number of parameters? */
192         if ((Req->argc != 1) && (Req->argc != 2) && (Req->argc != 5))
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         Log(LOG_NOTICE | LOG_snotice,
202             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
203             Req->argv[0]);
204
205         switch (Req->argc) {
206         case 1:
207                 if (!Conf_EnablePassiveServer(Req->argv[0]))
208                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
209                                                   Client_ID(Client),
210                                                   Req->argv[0]);
211         break;
212         case 2:
213                 /* Connect configured server */
214                 if (!Conf_EnableServer
215                     (Req->argv[0], (UINT16) atoi(Req->argv[1])))
216                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
217                                                   Client_ID(Client),
218                                                   Req->argv[0]);
219         break;
220         default:
221                 /* Add server */
222                 if (!Conf_AddServer
223                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
224                      Req->argv[3], Req->argv[4]))
225                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
226                                                   Client_ID(Client),
227                                                   Req->argv[0]);
228         }
229
230         return CONNECTED;
231 } /* IRC_CONNECT */
232
233
234 GLOBAL bool
235 IRC_DISCONNECT(CLIENT *Client, REQUEST *Req )
236 {
237         /* Disconnect and disable configured server */
238
239         CONN_ID my_conn;
240
241         assert( Client != NULL );
242         assert( Req != NULL );
243
244         /* Not a local IRC operator? */
245         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
246
247         /* Bad number of parameters? */
248         if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
249
250         Log( LOG_NOTICE|LOG_snotice, "Got DISCONNECT command from \"%s\" for0 \"%s\".", Client_Mask( Client ), Req->argv[0]);
251
252         /* Save ID of this connection */
253         my_conn = Client_Conn( Client );
254
255         /* Connect configured server */
256         if( ! Conf_DisableServer( Req->argv[0] )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
257
258         /* Are we still connected or were we killed, too? */
259         if( Conn_GetClient( my_conn )) return CONNECTED;
260         else return DISCONNECTED;
261 } /* IRC_CONNECT */
262
263
264 GLOBAL bool
265 IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
266 {
267         CLIENT *to, *from;
268         int client_type;
269
270         assert( Client != NULL );
271         assert( Req != NULL );
272
273         if (Req->argc != 1)
274                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
275
276         client_type = Client_Type(Client);
277         switch (client_type) {
278         case CLIENT_USER:
279                 if (!Client_OperByMe(Client))
280                         return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
281                 from = Client;
282                 break;
283         case CLIENT_SERVER:
284                 from = Client_Search(Req->prefix);
285                 break;
286         default:
287                 return CONNECTED;
288         }
289
290         if (!from)
291                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
292
293         for (to=Client_First(); to != NULL; to=Client_Next(to)) {
294                 if (Client_Conn(to) < 0) /* no local connection or WALLOPS origin */
295                         continue;
296
297                 client_type = Client_Type(to);
298                 switch (client_type) {
299                 case CLIENT_USER:
300                         if (Client_HasMode(to, 'w'))
301                                 IRC_WriteStrClientPrefix(to, from, "WALLOPS :%s", Req->argv[0]);
302                         break;
303                 case CLIENT_SERVER:
304                         if (to != Client)
305                                 IRC_WriteStrClientPrefix(to, from, "WALLOPS :%s", Req->argv[0]);
306                         break;
307                 }
308         }
309         return CONNECTED;
310 }
311
312
313
314 /* -eof- */