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