fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[ircu2.10.12-pk.git] / ircd / m_nick.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_nick.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id: m_nick.c 1729 2006-11-04 21:42:00Z entrope $
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "IPcheck.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_chattr.h"
89 #include "ircd_features.h"
90 #include "ircd_log.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_debug.h"
97 #include "s_misc.h"
98 #include "s_user.h"
99 #include "send.h"
100 #include "sys.h"
101
102 /* #include <assert.h> -- Now using assert in ircd_log.h */
103 #include <stdlib.h>
104 #include <string.h>
105
106  /*
107 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
108 * string for a nickname (note, the 'nick' may be modified in the process...)
109 *
110 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
111 *
112 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
113 *  anything outside the above set will terminate nickname.
114 * In addition, the first character cannot be '-' or a Digit.
115 *
116 * Note:
117 *  The '~'-character should be allowed, but a change should be global,
118 *  some confusion would result if only few servers allowed it...
119 */
120 static int do_nick_name(char* nick)
121 {
122   char* ch  = nick;
123   char* end = ch + NICKLEN;
124   assert(0 != ch);
125   
126   /* first character in [0..9-] */
127   if (*ch == '-' || IsDigit(*ch))
128     return 0;
129   for ( ; (ch < end) && *ch; ++ch)
130     if (!IsNickChar(*ch))
131       break;
132
133   *ch = '\0';
134
135   return (ch - nick);
136 }
137
138 /*
139  * m_nick - message handler for local clients
140  * parv[0] = sender prefix
141  * parv[1] = nickname
142  */
143 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
144 {
145   struct Client* acptr;
146   char           nick[NICKLEN + 2];
147   char*          arg;
148   char*          s;
149   const char*    client_name;
150
151   assert(0 != cptr);
152   assert(cptr == sptr);
153
154   if (IsServerPort(cptr))
155     return exit_client(cptr, cptr, &me, "Use a different port");
156
157   /*
158    * parv[0] will be empty for clients connecting for the first time
159    */
160   client_name = (*(cli_name(sptr))) ? cli_name(sptr) : "*";
161
162   if (parc < 2) {
163     send_reply(sptr, ERR_NONICKNAMEGIVEN);
164     return 0;
165   }
166
167   /*
168    * Don't let them send make us send back a really long string of
169    * garbage
170    */
171   arg = parv[1];
172   if (strlen(arg) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)))
173     arg[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))] = '\0';
174
175   if ((s = strchr(arg, '~')))
176     *s = '\0';
177
178   strcpy(nick, arg);
179
180   /*
181    * If do_nick_name() returns a null name then reject it.
182    */
183   if (0 == do_nick_name(nick)) {
184     send_reply(sptr, ERR_ERRONEUSNICKNAME, arg);
185     return 0;
186   }
187
188   /* 
189    * Check if this is a LOCAL user trying to use a reserved (Juped)
190    * nick, if so tell him that it's a nick in use...
191    */
192   if (isNickJuped(nick) && !HasPriv(sptr, PRIV_UMODE_CHSERV)) {
193     send_reply(sptr, ERR_NICKNAMEINUSE, nick);
194     return 0;                        /* NICK message ignored */
195   }
196
197   if (!(acptr = FindClient(nick))) {
198     /*
199      * No collisions, all clear...
200      */
201     return set_nick_name(cptr, sptr, nick, parc, parv, 0);
202   }
203   if (IsServer(acptr)) {
204     send_reply(sptr, ERR_NICKNAMEINUSE, nick);
205     return 0;                        /* NICK message ignored */
206   }
207   /*
208    * If acptr == sptr, then we have a client doing a nick
209    * change between *equivalent* nicknames as far as server
210    * is concerned (user is changing the case of his/her
211    * nickname or somesuch)
212    */
213   if (acptr == sptr) {
214     /*
215      * If acptr == sptr, then we have a client doing a nick
216      * change between *equivalent* nicknames as far as server
217      * is concerned (user is changing the case of his/her
218      * nickname or somesuch)
219      */
220     if (0 != strcmp(cli_name(acptr), nick)) {
221       /*
222        * Allows change of case in his/her nick
223        */
224       return set_nick_name(cptr, sptr, nick, parc, parv, 0);
225     }
226     /*
227      * This is just ':old NICK old' type thing.
228      * Just forget the whole thing here. There is
229      * no point forwarding it to anywhere,
230      * especially since servers prior to this
231      * version would treat it as nick collision.
232      */
233     return 0;
234   }
235   /*
236    * Note: From this point forward it can be assumed that
237    * acptr != sptr (point to different client structures).
238    */
239   assert(acptr != sptr);
240   /*
241    * If the older one is "non-person", the new entry is just
242    * allowed to overwrite it. Just silently drop non-person,
243    * and proceed with the nick. This should take care of the
244    * "dormant nick" way of generating collisions...
245    *
246    * XXX - hmmm can this happen after one is registered?
247    *
248    * Yes, client 1 connects to IRC and registers, client 2 connects and
249    * sends "NICK foo" but doesn't send anything more.  client 1 now does
250    * /nick foo, they should succeed and client 2 gets disconnected with
251    * the message below.
252    */
253   if (IsUnknown(acptr) && MyConnect(acptr)) {
254     ServerStats->is_ref++;
255     IPcheck_connect_fail(acptr);
256     exit_client(cptr, acptr, &me, "Overridden by other sign on");
257     return set_nick_name(cptr, sptr, nick, parc, parv, 0);
258   }
259   /*
260    * NICK is coming from local client connection. Just
261    * send error reply and ignore the command.
262    */
263   send_reply(sptr, ERR_NICKNAMEINUSE, nick);
264   return 0;                        /* NICK message ignored */
265 }
266
267
268 /*
269  * ms_nick - server message handler for nicks
270  * parv[0] = sender prefix
271  * parv[1] = nickname
272  *
273  * If from server, source is client:
274  *   parv[2] = timestamp
275  *
276  * Source is server:
277  *   parv[2] = hopcount
278  *   parv[3] = timestamp
279  *   parv[4] = username
280  *   parv[5] = hostname
281  *   parv[6] = umode (optional)
282  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
283  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
284  *   parv[parc-1] = info
285  *   parv[0] = server
286  */
287 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
288 {
289   struct Client *acptr;
290   char nick[NICKLEN + 2];
291   time_t lastnick = 0;
292   int differ = 1;
293   const char *type;
294
295   assert(0 != cptr);
296   assert(0 != sptr);
297   assert(IsServer(cptr));
298
299   if ((IsServer(sptr) && parc < 8) || parc < 3)
300   {
301     sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
302                          parv[1], cptr);
303     return need_more_params(sptr, "NICK");
304   }
305
306   ircd_strncpy(nick, parv[1], NICKLEN);
307   nick[NICKLEN] = '\0';
308
309   if (IsServer(sptr))
310   {
311     lastnick = atoi(parv[3]);
312     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr)) 
313       cli_serv(sptr)->lag = TStime() - lastnick;
314   }
315   else
316   {
317     lastnick = atoi(parv[2]); 
318     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
319       cli_serv(cli_user(sptr)->server)->lag = TStime() - lastnick;
320   }
321   /*
322    * If do_nick_name() returns a null name OR if the server sent a nick
323    * name and do_nick_name() changed it in some way (due to rules of nick
324    * creation) then reject it. If from a server and we reject it,
325    * and KILL it. -avalon 4/4/92
326    */
327   if (!do_nick_name(nick) || strcmp(nick, parv[1]))
328   {
329     send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
330     
331     ++ServerStats->is_kill;
332     sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
333                          parv[0], cptr);
334     sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
335                   IsServer(sptr) ? parv[parc - 2] : parv[0], cli_name(&me), parv[1],
336                   nick, cli_name(cptr));
337     if (!IsServer(sptr))
338     {
339       /*
340        * bad nick _change_
341        */
342       sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
343                             parv[0], cli_name(&me), cli_name(cptr), parv[0],
344                             cli_user(sptr) ? cli_username(sptr) : "",
345                             cli_user(sptr) ? cli_name(cli_user(sptr)->server) :
346                             cli_name(cptr));
347     }
348     return 0;
349   }
350   /* Check against nick name collisions. */
351   if ((acptr = FindClient(nick)) == NULL)
352     /* No collisions, all clear... */
353     return set_nick_name(cptr, sptr, nick, parc, parv, 0);
354
355   /*
356    * If acptr == sptr, then we have a client doing a nick
357    * change between *equivalent* nicknames as far as server
358    * is concerned (user is changing the case of his/her
359    * nickname or somesuch)
360    */
361   if (acptr == sptr)
362   {
363     if (strcmp(cli_name(acptr), nick) != 0)
364       /* Allows change of case in his/her nick */
365       return set_nick_name(cptr, sptr, nick, parc, parv, 0);
366     else
367       /* Setting their nick to what it already is? Ignore it. */
368       return 0;
369   }
370   /* now we know we have a real collision. */
371   /*
372    * Note: From this point forward it can be assumed that
373    * acptr != sptr (point to different client structures).
374    */
375   assert(acptr != sptr);
376   /*
377    * If the older one is "non-person", the new entry is just
378    * allowed to overwrite it. Just silently drop non-person,
379    * and proceed with the nick. This should take care of the
380    * "dormant nick" way of generating collisions...
381    */
382   if (IsUnknown(acptr) && MyConnect(acptr))
383   {
384     ServerStats->is_ref++;
385     IPcheck_connect_fail(acptr);
386     exit_client(cptr, acptr, &me, "Overridden by other sign on");
387     return set_nick_name(cptr, sptr, nick, parc, parv, 0);
388   }
389   /*
390    * Decide, we really have a nick collision and deal with it
391    */
392   /*
393    * NICK was coming from a server connection.
394    * This means we have a race condition (two users signing on
395    * at the same time), or two net fragments reconnecting with the same nick.
396    * The latter can happen because two different users connected
397    * or because one and the same user switched server during a net break.
398    * If the TimeStamps are equal, we kill both (or only 'new'
399    * if it was a ":server NICK new ...").
400    * Otherwise we kill the youngest when user@host differ,
401    * or the oldest when they are the same.
402    * We treat user and ~user as different, because if it wasn't
403    * a faked ~user the AUTH wouldn't have added the '~'.
404    * --Run
405    *
406    */
407   if (IsServer(sptr))
408   {
409     struct irc_in_addr ip;
410     /*
411      * A new NICK being introduced by a neighbouring
412      * server (e.g. message type ":server NICK new ..." received)
413      *
414      * compare IP address and username
415      */
416     base64toip(parv[parc - 3], &ip);
417     differ =  (0 != memcmp(&cli_ip(acptr), &ip, sizeof(cli_ip(acptr)))) ||
418               (0 != ircd_strcmp(cli_user(acptr)->username, parv[4]));
419     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
420                          "%C %Tu (%s user@host))", acptr, cli_from(acptr),
421                          cli_lastnick(acptr), cptr, lastnick,
422                          differ ? "Different" : "Same");
423   }
424   else
425   {
426     /*
427      * A NICK change has collided (e.g. message type ":old NICK new").
428      *
429      * compare IP address and username
430      */
431     differ =  (0 != memcmp(&cli_ip(acptr), &cli_ip(sptr), sizeof(cli_ip(acptr)))) ||
432               (0 != ircd_strcmp(cli_user(acptr)->username, cli_user(sptr)->username));
433     sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
434                          "%C (%C %Tu <- %C %Tu)", sptr, acptr, cli_from(acptr),
435                          cli_lastnick(acptr), cptr, lastnick);
436   }
437   type = differ ? "overruled by older nick" : "nick collision from same user@host";
438   /*
439    * Now remove (kill) the nick on our side if it is the youngest.
440    * If no timestamp was received, we ignore the incoming nick
441    * (and expect a KILL for our legit nick soon ):
442    * When the timestamps are equal we kill both nicks. --Run
443    * acptr->from != cptr should *always* be true (?).
444    *
445    * This exits the client sending the NICK message
446    */
447   if ((differ && lastnick >= cli_lastnick(acptr)) ||
448       (!differ && lastnick <= cli_lastnick(acptr)))
449   {
450     ServerStats->is_kill++;
451     if (!IsServer(sptr))
452     {
453       /* If this was a nick change and not a nick introduction, we
454        * need to ensure that we remove our record of the client, and
455        * send a KILL to the whole network.
456        */
457       assert(!MyConnect(sptr));
458       /* Inform the rest of the net... */
459       sendcmdto_serv_butone(&me, CMD_KILL, 0, "%C :%s (%s)",
460                             sptr, cli_name(&me), type);
461       /* Don't go sending off a QUIT message... */
462       SetFlag(sptr, FLAG_KILLED);
463       /* Remove them locally. */
464       exit_client_msg(cptr, sptr, &me,
465                       "Killed (%s (%s))",
466                       feature_str(FEAT_HIS_SERVERNAME), type);
467     }
468     else
469     {
470       /* If the origin is a server, this was a new client, so we only
471        * send the KILL in the direction it came from.  We have no
472        * client record that we would have to clean up.
473        */
474       sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s)",
475                     parv[parc - 2], cli_name(&me), type);
476     }
477     /* If the timestamps differ and we just killed sptr, we don't need to kill
478      * acptr as well.
479      */
480     if (lastnick != cli_lastnick(acptr))
481       return 0;
482   }
483   /* Tell acptr why we are killing it. */
484   send_reply(acptr, ERR_NICKCOLLISION, nick);
485
486   ServerStats->is_kill++;
487   SetFlag(acptr, FLAG_KILLED);
488   /*
489    * This exits the client we had before getting the NICK message
490    */
491   sendcmdto_serv_butone(&me, CMD_KILL, NULL, "%C :%s (%s)",
492                         acptr, feature_str(FEAT_HIS_SERVERNAME),
493                         type);
494   exit_client_msg(cptr, acptr, &me, "Killed (%s (%s))",
495                   feature_str(FEAT_HIS_SERVERNAME), type);
496   if (lastnick == cli_lastnick(acptr))
497     return 0;
498   if (sptr == NULL)
499     return 0;
500   return set_nick_name(cptr, sptr, nick, parc, parv, 0);
501 }