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