]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-server.c
Merge branch 'cmode-M'
[ngircd-alex.git] / src / ngircd / irc-server.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2007 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 commands for server links
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25
26 #include "defines.h"
27 #include "conn.h"
28 #include "conn-func.h"
29 #include "conn-zip.h"
30 #include "conf.h"
31 #include "channel.h"
32 #include "irc-write.h"
33 #include "lists.h"
34 #include "log.h"
35 #include "messages.h"
36 #include "parse.h"
37 #include "numeric.h"
38 #include "ngircd.h"
39 #include "irc-info.h"
40 #include "op.h"
41
42 #include "exp.h"
43 #include "irc-server.h"
44
45
46 /**
47  * Handler for the IRC command "SERVER".
48  * See RFC 2813 section 4.1.2.
49  */
50 GLOBAL bool
51 IRC_SERVER( CLIENT *Client, REQUEST *Req )
52 {
53         char str[LINE_LEN];
54         CLIENT *from, *c;
55         int i;
56         CONN_ID con;
57         
58         assert( Client != NULL );
59         assert( Req != NULL );
60
61         /* Return an error if this is not a local client */
62         if (Client_Conn(Client) <= NONE)
63                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64                                           Client_ID(Client), Req->command);
65
66         if (Client_Type(Client) == CLIENT_GOTPASS ||
67             Client_Type(Client) == CLIENT_GOTPASS_2813) {
68                 /* We got a PASS command from the peer, and now a SERVER
69                  * command: the peer tries to register itself as a server. */
70                 LogDebug("Connection %d: got SERVER command (new server link) ...",
71                         Client_Conn(Client));
72
73                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
74
75                 /* Ist this server configured on out side? */
76                 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
77                 if( i >= MAX_SERVERS )
78                 {
79                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
80                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
81                         return DISCONNECTED;
82                 }
83                 if( strcmp( Conn_Password( Client_Conn( Client ) ),
84                             Conf_Server[i].pwd_in ) != 0 )
85                 {
86                         /* wrong password */
87                         Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
88                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
89                         return DISCONNECTED;
90                 }
91                 
92                 /* Is there a registered server with this ID? */
93                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
94
95                 Client_SetID( Client, Req->argv[0] );
96                 Client_SetHops( Client, 1 );
97                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
98
99                 /* Is this server registering on our side, or are we connecting to
100                  * a remote server? */
101                 con = Client_Conn(Client);
102                 if (Client_Token(Client) != TOKEN_OUTBOUND) {
103                         /* Incoming connection, send user/pass */
104                         if (!IRC_WriteStrClient(Client, "PASS %s %s",
105                                                 Conf_Server[i].pwd_out,
106                                                 NGIRCd_ProtoID)
107                             || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
108                                                    Conf_ServerName,
109                                                    Conf_ServerInfo)) {
110                                     Conn_Close(con, "Unexpected server behavior!",
111                                                NULL, false);
112                                     return DISCONNECTED;
113                         }
114                         Client_SetIntroducer(Client, Client);
115                         Client_SetToken(Client, 1);
116                 } else {
117                         /* outgoing connect, we already sent a SERVER and PASS
118                          * command to the peer */
119                         Client_SetToken(Client, atoi(Req->argv[1]));
120                 }
121
122                 /* Mark this connection as belonging to an configured server */
123                 Conf_SetServer(i, con);
124
125                 /* Check protocol level */
126                 if (Client_Type(Client) == CLIENT_GOTPASS) {
127                         /* We got a "simple" PASS command, so the peer is
128                          * using the protocol as defined in RFC 1459. */
129                         if (! (Conn_Options(con) & CONN_RFC1459))
130                                 Log(LOG_INFO,
131                                     "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
132                                     con, Client_ID(Client));
133                         Conn_SetOption(con, CONN_RFC1459);
134                 }
135
136                 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
137
138 #ifdef ZLIB
139                 if (strchr(Client_Flags(Client), 'Z') && !Zip_InitConn(con)) {
140                         Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
141                         return DISCONNECTED;
142                 }
143 #endif
144
145 #ifdef IRCPLUS
146                 if (strchr(Client_Flags(Client), 'H')) {
147                         LogDebug("Peer supports IRC+ extended server handshake ...");
148                         if (!IRC_Send_ISUPPORT(Client))
149                                 return DISCONNECTED;
150                         return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
151                                                   Client_ID(Client));
152                 } else {
153 #endif
154                         if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
155                                 Log(LOG_CRIT,
156                                     "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
157 #ifdef IRCPLUS
158                 }
159 #endif
160
161                 return IRC_Num_ENDOFMOTD(Client, Req);
162         }
163         else if( Client_Type( Client ) == CLIENT_SERVER )
164         {
165                 /* New server is being introduced to the network */
166
167                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
168
169                 /* check for existing server with same ID */
170                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
171
172                 from = Client_Search( Req->prefix );
173                 if( ! from )
174                 {
175                         /* Uh, Server, that introduced the new server is unknown?! */
176                         Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
177                         Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
178                         return DISCONNECTED;
179                 }
180
181                 c = Client_NewRemoteServer(Client, Req->argv[0], from, atoi(Req->argv[1]), atoi(Req->argv[2]), Req->argv[3], true);
182                 if (!c) {
183                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
184                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
185                         return DISCONNECTED;
186                 }
187
188                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
189                 else strcpy( str, "" );
190                 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (via %s, %s%d hop%s).", Client_ID( c ), Client_ID( Client ), str, Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
191
192                 /* notify other servers */
193                 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
194
195                 return CONNECTED;
196         } else
197                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
198                                           Client_ID(Client), Req->command);
199 } /* IRC_SERVER */
200
201
202 GLOBAL bool
203 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
204 {
205         char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
206         bool is_op, is_voiced;
207         CHANNEL *chan;
208         CLIENT *c;
209         
210         assert( Client != NULL );
211         assert( Req != NULL );
212
213         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
214
215         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
216         strcpy( nick_out, "" );
217
218         channame = Req->argv[0];
219         ptr = strtok( nick_in, "," );
220         while( ptr )
221         {
222                 is_op = is_voiced = false;
223                 
224                 /* cut off prefixes */
225                 while(( *ptr == '@' ) || ( *ptr == '+' ))
226                 {
227                         if( *ptr == '@' ) is_op = true;
228                         if( *ptr == '+' ) is_voiced = true;
229                         ptr++;
230                 }
231
232                 c = Client_Search( ptr );
233                 if( c )
234                 {
235                         Channel_Join( c, channame );
236                         chan = Channel_Search( channame );
237                         assert( chan != NULL );
238                         
239                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
240                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
241
242                         /* announce to channel... */
243                         IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
244
245                         /* set Channel-User-Modes */
246                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
247                         if( modes[0] )
248                         {
249                                 /* send modes to channel */
250                                 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
251                         }
252
253                         if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
254                         if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
255                         if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
256                         strlcat( nick_out, ptr, sizeof( nick_out ));
257                 }
258                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
259                 
260                 /* search for next Nick */
261                 ptr = strtok( NULL, "," );
262         }
263
264         /* forward to other servers */
265         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
266
267         return CONNECTED;
268 } /* IRC_NJOIN */
269
270
271 /**
272  * Handler for the IRC command "SQUIT".
273  * See RFC 2813 section 4.1.2 and RFC 2812 section 3.1.8.
274  */
275 GLOBAL bool
276 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
277 {
278         char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
279         CLIENT *from, *target;
280         CONN_ID con;
281         int loglevel;
282
283         assert(Client != NULL);
284         assert(Req != NULL);
285
286         if (Client_Type(Client) != CLIENT_SERVER
287             && !Client_HasMode(Client, 'o'))
288                 return Op_NoPrivileges(Client, Req);
289
290         /* Bad number of arguments? */
291         if (Req->argc != 2)
292                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
293                                           Client_ID(Client), Req->command);
294
295         if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
296                 from = Client_Search(Req->prefix);
297                 if (Client_Type(from) != CLIENT_SERVER
298                     && !Op_Check(Client, Req))
299                         return Op_NoPrivileges(Client, Req);
300         } else
301                 from = Client;
302         if (!from)
303                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
304                                           Client_ID(Client), Req->prefix);
305
306         if (Client_Type(Client) == CLIENT_USER)
307                 loglevel = LOG_NOTICE | LOG_snotice;
308         else
309                 loglevel = LOG_DEBUG;
310         Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
311             Client_ID(from), Req->argv[0], Req->argv[1]);
312
313         target = Client_Search(Req->argv[0]);
314         if (Client_Type(Client) != CLIENT_SERVER &&
315             target == Client_ThisServer())
316                 return Op_NoPrivileges(Client, Req);
317         if (!target) {
318                 /* The server is (already) unknown */
319                 Log(LOG_WARNING,
320                     "Got SQUIT from %s for unknown server \"%s\"!?",
321                     Client_ID(Client), Req->argv[0]);
322                 return CONNECTED;
323         }
324
325         con = Client_Conn(target);
326
327         if (Req->argv[1][0])
328                 if (Client_NextHop(from) != Client || con > NONE)
329                         snprintf(msg, sizeof(msg), "%s (SQUIT from %s)",
330                                  Req->argv[1], Client_ID(from));
331                 else
332                         strlcpy(msg, Req->argv[1], sizeof(msg));
333         else
334                 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
335                          Client_ID(from));
336
337         if (con > NONE) {
338                 /* We are directly connected to the target server, so we
339                  * have to tear down the connection and to inform all the
340                  * other remaining servers in the network */
341                 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
342                                 "Received SQUIT %s from %s: %s",
343                                 Req->argv[0], Client_ID(from),
344                                 Req->argv[1][0] ? Req->argv[1] : "-");
345                 Conn_Close(con, NULL, msg, true);
346                 if (con == Client_Conn(Client))
347                         return DISCONNECTED;
348         } else {
349                 /* This server is not directly connected, so the SQUIT must
350                  * be forwarded ... */
351                 if (Client_Type(from) != CLIENT_SERVER) {
352                         /* The origin is not an IRC server, so don't evaluate
353                          * this SQUIT but simply forward it */
354                         IRC_WriteStrClientPrefix(Client_NextHop(target),
355                             from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
356                 } else {
357                         /* SQUIT has been generated by another server, so
358                          * remove the target server from the network! */
359                         logmsg[0] = '\0';
360                         if (!strchr(msg, '('))
361                                 snprintf(logmsg, sizeof(logmsg),
362                                          "%s (SQUIT from %s)", Req->argv[1],
363                                          Client_ID(from));
364                         Client_Destroy(target, logmsg[0] ? logmsg : msg,
365                                        msg, false);
366                 }
367         }
368         return CONNECTED;
369 } /* IRC_SQUIT */
370
371 /* -eof- */