]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-oper.c
Allow DIE to send a message to all locally connected clients. Closes bug #48.
[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 static char UNUSED id[] = "$Id: irc-oper.c,v 1.27 2006/07/23 15:43:18 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.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
36 #include <exp.h>
37 #include "irc-oper.h"
38
39
40 static bool
41 Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
42 {
43         Log( LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s", Client_Mask( Client ),
44                                                                                 errtoken, errmsg);
45         IRC_SetPenalty(Client, 3);
46         return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
47 }
48
49
50 GLOBAL bool
51 IRC_OPER( CLIENT *Client, REQUEST *Req )
52 {
53         unsigned int i;
54
55         assert( Client != NULL );
56         assert( Req != NULL );
57
58         /* Falsche Anzahl Parameter? */
59         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
60
61         /* Operator suchen */
62         for( i = 0; i < Conf_Oper_Count; i++)
63         {
64                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
65         }
66         if( i >= Conf_Oper_Count )
67                 return Bad_OperPass(Client, Req->argv[0], "not configured");
68
69         /* Stimmt das Passwort? */
70         if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
71                 return Bad_OperPass(Client, Conf_Oper[i].name, "Bad password");
72
73         /* Authorized Mask? */
74         if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
75                 return Bad_OperPass(Client, Conf_Oper[i].mask, "hostmask check failed" );
76
77         if( ! Client_HasMode( Client, 'o' ))
78         {
79                 /* noch kein o-Mode gesetzt */
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         /* Not a local IRC operator? */
104         if ((!Client_HasMode(Client, 'o')) || (!Client_OperByMe(Client)))
105                 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
106                                           Client_ID(Client));
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         /* Not a local IRC operator? */
146         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
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         NGIRCd_SignalRehash = true;
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         /* Not a local IRC operator? */
167         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
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
185         assert(Client != NULL);
186         assert(Req != NULL);
187
188         /* Not a local IRC operator? */
189         if ((!Client_HasMode(Client, 'o')) || (!Client_OperByMe(Client)))
190                 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
191                                           Client_ID(Client));
192
193         /* Bad number of parameters? */
194         if ((Req->argc != 2) && (Req->argc != 5))
195                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
196                                           Client_ID(Client), Req->command);
197
198         /* Invalid port number? */
199         if (atoi(Req->argv[1]) < 1)
200                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
201                                           Client_ID(Client), Req->command);
202
203         Log(LOG_NOTICE | LOG_snotice,
204             "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
205             Req->argv[0]);
206
207         if (Req->argc == 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         } else {
215                 /* Add server */
216                 if (!Conf_AddServer
217                     (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
218                      Req->argv[3], Req->argv[4]))
219                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
220                                                   Client_ID(Client),
221                                                   Req->argv[0]);
222         }
223
224         return CONNECTED;
225 } /* IRC_CONNECT */
226
227
228 GLOBAL bool
229 IRC_DISCONNECT(CLIENT *Client, REQUEST *Req )
230 {
231         /* Disconnect and disable configured server */
232
233         CONN_ID my_conn;
234
235         assert( Client != NULL );
236         assert( Req != NULL );
237
238         /* Not a local IRC operator? */
239         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
240
241         /* Bad number of parameters? */
242         if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
243
244         Log( LOG_NOTICE|LOG_snotice, "Got DISCONNECT command from \"%s\" for0 \"%s\".", Client_Mask( Client ), Req->argv[0]);
245
246         /* Save ID of this connection */
247         my_conn = Client_Conn( Client );
248
249         /* Connect configured server */
250         if( ! Conf_DisableServer( Req->argv[0] )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
251
252         /* Are we still connected or were we killed, too? */
253         if( Conn_GetClient( my_conn )) return CONNECTED;
254         else return DISCONNECTED;
255 } /* IRC_CONNECT */
256
257
258 /* -eof- */