]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
Implement asynchronous user authentication using PAM
[ngircd-alex.git] / src / ngircd / irc-login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 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  * Login and logout
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <unistd.h>
24
25 #include "ngircd.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "channel.h"
29 #include "io.h"
30 #include "log.h"
31 #include "messages.h"
32 #include "pam.h"
33 #include "parse.h"
34 #include "irc.h"
35 #include "irc-info.h"
36 #include "irc-write.h"
37
38 #include "exp.h"
39 #include "irc-login.h"
40
41
42 static bool Hello_User PARAMS(( CLIENT *Client ));
43 static bool Hello_User_PostAuth PARAMS(( CLIENT *Client ));
44 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
45 static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
46 static void Reject_Client PARAMS((CLIENT *Client));
47
48 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
49                                        void *i));
50
51 #ifdef PAM
52 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
53 #endif
54
55 /**
56  * Handler for the IRC command "PASS".
57  * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
58  */
59 GLOBAL bool
60 IRC_PASS( CLIENT *Client, REQUEST *Req )
61 {
62         char *type, *orig_flags;
63         int protohigh, protolow;
64
65         assert( Client != NULL );
66         assert( Req != NULL );
67
68         /* Return an error if this is not a local client */
69         if (Client_Conn(Client) <= NONE)
70                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
71                                           Client_ID(Client), Req->command);
72
73         if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
74                 /* Not yet registered "unknown" connection, PASS with one
75                  * argument: either a regular client, service, or server
76                  * using the old RFC 1459 section 4.1.1 syntax. */
77                 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
78                          Client_Conn(Client));
79         } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
80                     Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
81                    (Req->argc == 3 || Req->argc == 4)) {
82                 /* Not yet registered "unknown" connection or outgoing server
83                  * link, PASS with three or four argument: server using the
84                  * RFC 2813 section 4.1.1 syntax. */
85                 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
86                          Client_Conn(Client));
87         } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
88                    Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
89                 /* Unregistered connection, but wrong number of arguments: */
90                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
91                                           Client_ID(Client), Req->command);
92         } else {
93                 /* Registered connection, PASS command is not allowed! */
94                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
95                                           Client_ID(Client));
96         }
97
98         Client_SetPassword(Client, Req->argv[0]);
99
100         /* Protocol version */
101         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
102                 int c2, c4;
103
104                 c2 = Req->argv[1][2];
105                 c4 = Req->argv[1][4];
106
107                 Req->argv[1][4] = '\0';
108                 protolow = atoi(&Req->argv[1][2]);
109                 Req->argv[1][2] = '\0';
110                 protohigh = atoi(Req->argv[1]);
111
112                 Req->argv[1][2] = c2;
113                 Req->argv[1][4] = c4;
114
115                 Client_SetType(Client, CLIENT_GOTPASS_2813);
116         } else {
117                 protohigh = protolow = 0;
118                 Client_SetType(Client, CLIENT_GOTPASS);
119         }
120
121         /* Protocol type, see doc/Protocol.txt */
122         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
123                 type = &Req->argv[1][4];
124         else
125                 type = NULL;
126
127         /* Protocol flags/options */
128         if (Req->argc >= 4)
129                 orig_flags = Req->argv[3];
130         else
131                 orig_flags = "";
132
133         /* Implementation, version and IRC+ flags */
134         if (Req->argc >= 3) {
135                 char *impl, *ptr, *serverver, *flags;
136
137                 impl = Req->argv[2];
138                 ptr = strchr(impl, '|');
139                 if (ptr)
140                         *ptr = '\0';
141
142                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
143                         /* The peer seems to be a server which supports the
144                          * IRC+ protocol (see doc/Protocol.txt). */
145                         serverver = ptr + 1;
146                         flags = strchr(serverver, ':');
147                         if (flags) {
148                                 *flags = '\0';
149                                 flags++;
150                         } else
151                                 flags = "";
152                         Log(LOG_INFO,
153                             "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
154                             impl, serverver, protohigh, protolow, flags);
155                 } else {
156                         /* The peer seems to be a server supporting the
157                          * "original" IRC protocol (RFC 2813). */
158                         if (strchr(orig_flags, 'Z'))
159                                 flags = "Z";
160                         else
161                                 flags = "";
162                         Log(LOG_INFO,
163                             "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
164                             impl, protohigh, protolow, flags);
165                 }
166                 Client_SetFlags(Client, flags);
167         }
168
169         return CONNECTED;
170 } /* IRC_PASS */
171
172
173 /**
174  * IRC "NICK" command.
175  * This function implements the IRC command "NICK" which is used to register
176  * with the server, to change already registered nicknames and to introduce
177  * new users which are connected to other servers.
178  */
179 GLOBAL bool
180 IRC_NICK( CLIENT *Client, REQUEST *Req )
181 {
182         CLIENT *intr_c, *target, *c;
183         char *nick, *user, *hostname, *modes, *info;
184         int token, hops;
185
186         assert( Client != NULL );
187         assert( Req != NULL );
188
189         /* Some IRC clients, for example BitchX, send the NICK and USER
190          * commands in the wrong order ... */
191         if(Client_Type(Client) == CLIENT_UNKNOWN
192             || Client_Type(Client) == CLIENT_GOTPASS
193             || Client_Type(Client) == CLIENT_GOTNICK
194 #ifndef STRICT_RFC
195             || Client_Type(Client) == CLIENT_GOTUSER
196 #endif
197             || Client_Type(Client) == CLIENT_USER
198             || Client_Type(Client) == CLIENT_SERVICE
199             || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
200         {
201                 /* User registration or change of nickname */
202
203                 /* Wrong number of arguments? */
204                 if( Req->argc != 1 )
205                         return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
206                                                    Client_ID( Client ),
207                                                    Req->command );
208
209                 /* Search "target" client */
210                 if( Client_Type( Client ) == CLIENT_SERVER )
211                 {
212                         target = Client_Search( Req->prefix );
213                         if( ! target )
214                                 return IRC_WriteStrClient( Client,
215                                                            ERR_NOSUCHNICK_MSG,
216                                                            Client_ID( Client ),
217                                                            Req->argv[0] );
218                 }
219                 else
220                 {
221                         /* Is this a restricted client? */
222                         if( Client_HasMode( Client, 'r' ))
223                                 return IRC_WriteStrClient( Client,
224                                                            ERR_RESTRICTED_MSG,
225                                                            Client_ID( Client ));
226
227                         target = Client;
228                 }
229
230 #ifndef STRICT_RFC
231                 /* If the clients tries to change to its own nickname we won't
232                  * do anything. This is how the original ircd behaves and some
233                  * clients (for example Snak) expect it to be like this.
234                  * But I doubt that this is "really the right thing" ... */
235                 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
236                         return CONNECTED;
237 #endif
238
239                 /* Check that the new nickname is available. Special case:
240                  * the client only changes from/to upper to lower case. */
241                 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
242                 {
243                         if( ! Client_CheckNick( target, Req->argv[0] ))
244                                 return CONNECTED;
245                 }
246
247                 if (Client_Type(target) != CLIENT_USER &&
248                     Client_Type(target) != CLIENT_SERVICE &&
249                     Client_Type(target) != CLIENT_SERVER) {
250                         /* New client */
251                         LogDebug("Connection %d: got valid NICK command ...",
252                              Client_Conn( Client ));
253
254                         /* Register new nickname of this client */
255                         Client_SetID( target, Req->argv[0] );
256
257                         /* If we received a valid USER command already then
258                          * register the new client! */
259                         if( Client_Type( Client ) == CLIENT_GOTUSER )
260                                 return Hello_User( Client );
261                         else
262                                 Client_SetType( Client, CLIENT_GOTNICK );
263                 } else {
264                         /* Nickname change */
265                         if (Client_Conn(target) > NONE) {
266                                 /* Local client */
267                                 Log(LOG_INFO,
268                                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
269                                     Client_TypeText(target), Client_Mask(target),
270                                     Client_Conn(target), Client_ID(target),
271                                     Req->argv[0]);
272                                 Conn_UpdateIdle(Client_Conn(target));
273                         } else {
274                                 /* Remote client */
275                                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
276                                          Client_TypeText(target),
277                                          Client_Mask(target), Client_ID(target),
278                                          Req->argv[0]);
279                         }
280
281                         /* Inform all users and servers (which have to know)
282                          * of this nickname change */
283                         if( Client_Type( Client ) == CLIENT_USER )
284                                 IRC_WriteStrClientPrefix( Client, Client,
285                                                           "NICK :%s",
286                                                           Req->argv[0] );
287                         IRC_WriteStrServersPrefix( Client, target,
288                                                    "NICK :%s", Req->argv[0] );
289                         IRC_WriteStrRelatedPrefix( target, target, false,
290                                                    "NICK :%s", Req->argv[0] );
291
292                         /* Register old nickname for WHOWAS queries */
293                         Client_RegisterWhowas( target );
294
295                         /* Save new nickname */
296                         Client_SetID( target, Req->argv[0] );
297
298                         IRC_SetPenalty( target, 2 );
299                 }
300
301                 return CONNECTED;
302         } else if(Client_Type(Client) == CLIENT_SERVER ||
303                   Client_Type(Client) == CLIENT_SERVICE) {
304                 /* Server or service introduces new client */
305
306                 /* Bad number of parameters? */
307                 if (Req->argc != 2 && Req->argc != 7)
308                         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
309                                                   Client_ID(Client), Req->command);
310
311                 if (Req->argc >= 7) {
312                         /* RFC 2813 compatible syntax */
313                         nick = Req->argv[0];
314                         hops = atoi(Req->argv[1]);
315                         user = Req->argv[2];
316                         hostname = Req->argv[3];
317                         token = atoi(Req->argv[4]);
318                         modes = Req->argv[5] + 1;
319                         info = Req->argv[6];
320                 } else {
321                         /* RFC 1459 compatible syntax */
322                         nick = Req->argv[0];
323                         hops = 1;
324                         user = Req->argv[0];
325                         hostname = Client_ID(Client);
326                         token = atoi(Req->argv[1]);
327                         modes = "";
328                         info = Req->argv[0];
329                 }
330
331                 c = Client_Search(nick);
332                 if(c) {
333                         /*
334                          * the new nick is already present on this server:
335                          * the new and the old one have to be disconnected now.
336                          */
337                         Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
338                         Kill_Nick( Req->argv[0], "Nick collision" );
339                         return CONNECTED;
340                 }
341
342                 /* Find the Server this client is connected to */
343                 intr_c = Client_GetFromToken(Client, token);
344                 if( ! intr_c )
345                 {
346                         Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
347                         Kill_Nick( Req->argv[0], "Unknown server" );
348                         return CONNECTED;
349                 }
350
351                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
352                                          token, modes, info, true);
353                 if( ! c )
354                 {
355                         /* out of memory, need to disconnect client to keep network state consistent */
356                         Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
357                         Kill_Nick( Req->argv[0], "Server error" );
358                         return CONNECTED;
359                 }
360
361                 /* RFC 2813: client is now fully registered, inform all the
362                  * other servers about the new user.
363                  * RFC 1459: announce the new client only after receiving the
364                  * USER command, first we need more information! */
365                 if (Req->argc < 7) {
366                         LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
367                                  Client_Mask(c));
368                         Client_SetType(c, CLIENT_GOTNICK);
369                 } else
370                         Introduce_Client(Client, c, CLIENT_USER);
371
372                 return CONNECTED;
373         }
374         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
375 } /* IRC_NICK */
376
377
378 /**
379  * Handler for the IRC command "USER".
380  */
381 GLOBAL bool
382 IRC_USER(CLIENT * Client, REQUEST * Req)
383 {
384         CLIENT *c;
385 #ifdef IDENTAUTH
386         char *ptr;
387 #endif
388
389         assert(Client != NULL);
390         assert(Req != NULL);
391
392         if (Client_Type(Client) == CLIENT_GOTNICK ||
393 #ifndef STRICT_RFC
394             Client_Type(Client) == CLIENT_UNKNOWN ||
395 #endif
396             Client_Type(Client) == CLIENT_GOTPASS)
397         {
398                 /* New connection */
399                 if (Req->argc != 4)
400                         return IRC_WriteStrClient(Client,
401                                                   ERR_NEEDMOREPARAMS_MSG,
402                                                   Client_ID(Client),
403                                                   Req->command);
404
405                 /* User name */
406 #ifdef IDENTAUTH
407                 ptr = Client_User(Client);
408                 if (!ptr || !*ptr || *ptr == '~')
409                         Client_SetUser(Client, Req->argv[0], false);
410 #else
411                 Client_SetUser(Client, Req->argv[0], false);
412 #endif
413                 Client_SetOrigUser(Client, Req->argv[0]);
414
415                 /* "Real name" or user info text: Don't set it to the empty
416                  * string, the original ircd can't deal with such "real names"
417                  * (e. g. "USER user * * :") ... */
418                 if (*Req->argv[3])
419                         Client_SetInfo(Client, Req->argv[3]);
420                 else
421                         Client_SetInfo(Client, "-");
422
423                 LogDebug("Connection %d: got valid USER command ...",
424                     Client_Conn(Client));
425                 if (Client_Type(Client) == CLIENT_GOTNICK)
426                         return Hello_User(Client);
427                 else
428                         Client_SetType(Client, CLIENT_GOTUSER);
429                 return CONNECTED;
430
431         } else if (Client_Type(Client) == CLIENT_SERVER ||
432                    Client_Type(Client) == CLIENT_SERVICE) {
433                 /* Server/service updating an user */
434                 if (Req->argc != 4)
435                         return IRC_WriteStrClient(Client,
436                                                   ERR_NEEDMOREPARAMS_MSG,
437                                                   Client_ID(Client),
438                                                   Req->command);
439                 c = Client_Search(Req->prefix);
440                 if (!c)
441                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
442                                                   Client_ID(Client),
443                                                   Req->prefix);
444
445                 Client_SetUser(c, Req->argv[0], true);
446                 Client_SetOrigUser(c, Req->argv[0]);
447                 Client_SetHostname(c, Req->argv[1]);
448                 Client_SetInfo(c, Req->argv[3]);
449
450                 LogDebug("Connection %d: got valid USER command for \"%s\".",
451                          Client_Conn(Client), Client_Mask(c));
452
453                 /* RFC 1459 style user registration?
454                  * Introduce client to network: */
455                 if (Client_Type(c) == CLIENT_GOTNICK)
456                         Introduce_Client(Client, c, CLIENT_USER);
457
458                 return CONNECTED;
459         } else if (Client_Type(Client) == CLIENT_USER) {
460                 /* Already registered connection */
461                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
462                                           Client_ID(Client));
463         } else {
464                 /* Unexpected/invalid connection state? */
465                 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
466                                           Client_ID(Client));
467         }
468 } /* IRC_USER */
469
470
471 /**
472  * Handler for the IRC command "SERVICE".
473  * This function implements IRC Services registration using the SERVICE command
474  * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
475  * At the moment ngIRCd doesn't support directly linked services, so this
476  * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
477  * received from a peer server.
478  */
479 GLOBAL bool
480 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
481 {
482         CLIENT *c, *intr_c;
483         char *nick, *user, *host, *info, *modes, *ptr;
484         int token, hops;
485
486         assert(Client != NULL);
487         assert(Req != NULL);
488
489         if (Client_Type(Client) != CLIENT_GOTPASS &&
490             Client_Type(Client) != CLIENT_SERVER)
491                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
492                                           Client_ID(Client));
493
494         if (Req->argc != 6)
495                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
496                                           Client_ID(Client), Req->command);
497
498         if (Client_Type(Client) != CLIENT_SERVER)
499                 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
500                                   Client_ID(Client), Req->argv[0]);
501
502         /* Bad number of parameters? */
503         if (Req->argc != 6)
504                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
505                                           Client_ID(Client), Req->command);
506
507         nick = Req->argv[0];
508         user = NULL; host = NULL;
509         token = atoi(Req->argv[1]);
510         hops = atoi(Req->argv[4]);
511         info = Req->argv[5];
512
513         /* Validate service name ("nick name") */
514         c = Client_Search(nick);
515         if(c) {
516                 /* Nick name collission: disconnect (KILL) both clients! */
517                 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
518                     Client_ID(Client), nick);
519                 Kill_Nick(nick, "Nick collision");
520                 return CONNECTED;
521         }
522
523         /* Get the server to which the service is connected */
524         intr_c = Client_GetFromToken(Client, token);
525         if (! intr_c) {
526                 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
527                     Client_ID(Client), nick);
528                 Kill_Nick(nick, "Unknown server");
529                 return CONNECTED;
530         }
531
532         /* Get user and host name */
533         ptr = strchr(nick, '@');
534         if (ptr) {
535                 *ptr = '\0';
536                 host = ++ptr;
537         }
538         if (!host)
539                 host = Client_Hostname(intr_c);
540         ptr = strchr(nick, '!');
541         if (ptr) {
542                 *ptr = '\0';
543                 user = ++ptr;
544         }
545         if (!user)
546                 user = nick;
547
548         /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
549          * for future usage"; but we use it to transfer the modes and check
550          * that the first character is a '+' sign and ignore it otherwise. */
551         modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
552
553         c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
554                                  token, modes, info, true);
555         if (! c) {
556                 /* Couldn't create client structure, so KILL the service to
557                  * keep network status consistent ... */
558                 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
559                     Client_Conn(Client));
560                 Kill_Nick(nick, "Server error");
561                 return CONNECTED;
562         }
563
564         Introduce_Client(Client, c, CLIENT_SERVICE);
565         return CONNECTED;
566 } /* IRC_SERVICE */
567
568
569 /**
570  * Handler for the IRC command "WEBIRC".
571  * Syntax: WEBIRC <password> <username> <real-hostname> <real-IP-address>
572  */
573 GLOBAL bool
574 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
575 {
576         /* Exactly 4 parameters are requited */
577         if (Req->argc != 4)
578                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
579                                           Client_ID(Client), Req->command);
580
581         if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
582                 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
583                                           Client_ID(Client));
584
585         LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
586                  Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
587
588         Client_SetUser(Client, Req->argv[1], true);
589         Client_SetOrigUser(Client, Req->argv[1]);
590         Client_SetHostname(Client, Req->argv[2]);
591         return CONNECTED;
592 } /* IRC_WEBIRC */
593
594
595 GLOBAL bool
596 IRC_QUIT( CLIENT *Client, REQUEST *Req )
597 {
598         CLIENT *target;
599         char quitmsg[LINE_LEN];
600
601         assert( Client != NULL );
602         assert( Req != NULL );
603
604         /* Wrong number of arguments? */
605         if( Req->argc > 1 )
606                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
607
608         if (Req->argc == 1)
609                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
610
611         if ( Client_Type( Client ) == CLIENT_SERVER )
612         {
613                 /* Server */
614                 target = Client_Search( Req->prefix );
615                 if( ! target )
616                 {
617                         Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
618                         return CONNECTED;
619                 }
620
621                 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
622
623                 return CONNECTED;
624         }
625         else
626         {
627                 if (Req->argc == 1 && quitmsg[0] != '\"') {
628                         /* " " to avoid confusion */
629                         strlcpy(quitmsg, "\"", sizeof quitmsg);
630                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
631                         strlcat(quitmsg, "\"", sizeof quitmsg );
632                 }
633
634                 /* User, Service, or not yet registered */
635                 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
636
637                 return DISCONNECTED;
638         }
639 } /* IRC_QUIT */
640
641
642 GLOBAL bool
643 IRC_PING(CLIENT *Client, REQUEST *Req)
644 {
645         CLIENT *target, *from;
646
647         assert(Client != NULL);
648         assert(Req != NULL);
649
650         if (Req->argc < 1)
651                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
652                                           Client_ID(Client));
653 #ifdef STRICT_RFC
654         /* Don't ignore additional arguments when in "strict" mode */
655         if (Req->argc > 2)
656                  return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
657                                            Client_ID(Client), Req->command);
658 #endif
659
660         if (Req->argc > 1) {
661                 /* A target has been specified ... */
662                 target = Client_Search(Req->argv[1]);
663
664                 if (!target || Client_Type(target) != CLIENT_SERVER)
665                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
666                                         Client_ID(Client), Req->argv[1]);
667
668                 if (target != Client_ThisServer()) {
669                         /* Ok, we have to forward the PING */
670                         if (Client_Type(Client) == CLIENT_SERVER)
671                                 from = Client_Search(Req->prefix);
672                         else
673                                 from = Client;
674                         if (!from)
675                                 return IRC_WriteStrClient(Client,
676                                                 ERR_NOSUCHSERVER_MSG,
677                                                 Client_ID(Client), Req->prefix);
678
679                         return IRC_WriteStrClientPrefix(target, from,
680                                         "PING %s :%s", Req->argv[0],
681                                         Req->argv[1] );
682                 }
683         }
684
685         if (Client_Type(Client) == CLIENT_SERVER) {
686                 if (Req->prefix)
687                         from = Client_Search(Req->prefix);
688                 else
689                         from = Client;
690         } else
691                 from = Client_ThisServer();
692         if (!from)
693                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
694                                         Client_ID(Client), Req->prefix);
695
696         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
697             Client_Conn(Client));
698
699 #ifdef STRICT_RFC
700         return IRC_WriteStrClient(Client, "PONG %s :%s",
701                 Client_ID(from), Client_ID(Client));
702 #else
703         /* Some clients depend on the argument being returned in the PONG
704          * reply (not mentioned in any RFC, though) */
705         return IRC_WriteStrClient(Client, "PONG %s :%s",
706                 Client_ID(from), Req->argv[0]);
707 #endif
708 } /* IRC_PING */
709
710
711 GLOBAL bool
712 IRC_PONG(CLIENT *Client, REQUEST *Req)
713 {
714         CLIENT *target, *from;
715         char *s;
716
717         assert(Client != NULL);
718         assert(Req != NULL);
719
720         /* Wrong number of arguments? */
721         if (Req->argc < 1)
722                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
723                                           Client_ID(Client));
724         if (Req->argc > 2)
725                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
726                                           Client_ID(Client), Req->command);
727
728         /* Forward? */
729         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
730                 target = Client_Search(Req->argv[0]);
731                 if (!target)
732                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
733                                         Client_ID(Client), Req->argv[0]);
734
735                 from = Client_Search(Req->prefix);
736
737                 if (target != Client_ThisServer() && target != from) {
738                         /* Ok, we have to forward the message. */
739                         if (!from)
740                                 return IRC_WriteStrClient(Client,
741                                                 ERR_NOSUCHSERVER_MSG,
742                                                 Client_ID(Client), Req->prefix);
743
744                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
745                                 s = Client_ID(from);
746                         else
747                                 s = Req->argv[0];
748                         return IRC_WriteStrClientPrefix(target, from,
749                                  "PONG %s :%s", s, Req->argv[1]);
750                 }
751         }
752
753         /* The connection timestamp has already been updated when the data has
754          * been read from so socket, so we don't need to update it here. */
755 #ifdef DEBUG
756         if (Client_Conn(Client) > NONE)
757                 Log(LOG_DEBUG,
758                         "Connection %d: received PONG. Lag: %ld seconds.",
759                         Client_Conn(Client),
760                         time(NULL) - Conn_LastPing(Client_Conn(Client)));
761         else
762                  Log(LOG_DEBUG,
763                         "Connection %d: received PONG.", Client_Conn(Client));
764 #endif
765         return CONNECTED;
766 } /* IRC_PONG */
767
768
769 static bool
770 Hello_User(CLIENT * Client)
771 {
772 #ifdef PAM
773         int pipefd[2], result;
774         CONN_ID conn;
775         pid_t pid;
776
777         assert(Client != NULL);
778         conn = Client_Conn(Client);
779
780         pid = Proc_Fork(Conn_GetProcStat(conn), pipefd, cb_Read_Auth_Result);
781         if (pid > 0) {
782                 LogDebug("Authenticator for connection %d created (PID %d).",
783                          conn, pid);
784                 return CONNECTED;
785         } else {
786                 /* Sub process */
787                 signal(SIGTERM, Proc_GenericSignalHandler);
788                 Log_Init_Subprocess("Auth");
789                 result = PAM_Authenticate(Client);
790                 write(pipefd[1], &result, sizeof(result));
791                 Log_Exit_Subprocess("Auth");
792                 exit(0);
793         }
794 #else
795         assert(Client != NULL);
796
797         /* Check global server password ... */
798         if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
799                 /* Bad password! */
800                 Reject_Client(Client);
801                 return DISCONNECTED;
802         }
803         return Hello_User_PostAuth(Client);
804 #endif
805 }
806
807
808 #ifdef PAM
809
810 /**
811  * Read result of the authenticatior sub-process from pipe
812  */
813 static void
814 cb_Read_Auth_Result(int r_fd, UNUSED short events)
815 {
816         CONN_ID conn;
817         CLIENT *client;
818         int result;
819         size_t len;
820         PROC_STAT *proc;
821
822         LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
823         conn = Conn_GetFromProc(r_fd);
824         if (conn == NONE) {
825                 /* Ops, none found? Probably the connection has already
826                  * been closed!? We'll ignore that ... */
827                 io_close(r_fd);
828                 LogDebug("Auth: Got callback for unknown connection!?");
829                 return;
830         }
831         proc = Conn_GetProcStat(conn);
832         client = Conn_GetClient(conn);
833
834         /* Read result from pipe */
835         len = Proc_Read(proc, &result, sizeof(result));
836         if (len == 0)
837                 return;
838
839         /* Make sure authenticator sub-process is dead now ... */
840         Proc_Kill(proc);
841
842         if (len != sizeof(result)) {
843                 Log(LOG_CRIT, "Auth: Got malformed result!");
844                 Reject_Client(client);
845                 return;
846         }
847
848         if (result == true)
849                 (void)Hello_User_PostAuth(client);
850         else
851                 Reject_Client(client);
852 }
853
854 #endif
855
856
857 static void
858 Reject_Client(CLIENT *Client)
859 {
860         Log(LOG_ERR,
861             "User \"%s\" rejected (connection %d): Access denied!",
862             Client_Mask(Client), Client_Conn(Client));
863         Conn_Close(Client_Conn(Client), NULL,
864                    "Access denied! Bad password?", true);
865 }
866
867
868 static bool
869 Hello_User_PostAuth(CLIENT *Client)
870 {
871         Introduce_Client(NULL, Client, CLIENT_USER);
872
873         if (!IRC_WriteStrClient
874             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
875                 return false;
876         if (!IRC_WriteStrClient
877             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
878              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
879              TARGET_VENDOR, TARGET_OS))
880                 return false;
881         if (!IRC_WriteStrClient
882             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
883                 return false;
884         if (!IRC_WriteStrClient
885             (Client, RPL_MYINFO_MSG, Client_ID(Client),
886              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
887              CHANMODES))
888                 return false;
889
890         /* Features supported by this server (005 numeric, ISUPPORT),
891          * see <http://www.irc.org/tech_docs/005.html> for details. */
892         if (!IRC_Send_ISUPPORT(Client))
893                 return DISCONNECTED;
894
895         if (!IRC_Send_LUSERS(Client))
896                 return DISCONNECTED;
897         if (!IRC_Show_MOTD(Client))
898                 return DISCONNECTED;
899
900         /* Suspend the client for a second ... */
901         IRC_SetPenalty(Client, 1);
902
903         return CONNECTED;
904 }
905
906
907 static void
908 Kill_Nick( char *Nick, char *Reason )
909 {
910         REQUEST r;
911
912         assert( Nick != NULL );
913         assert( Reason != NULL );
914
915         r.prefix = (char *)Client_ThisServer( );
916         r.argv[0] = Nick;
917         r.argv[1] = Reason;
918         r.argc = 2;
919
920         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
921         IRC_KILL( Client_ThisServer( ), &r );
922 } /* Kill_Nick */
923
924
925 static void
926 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
927 {
928         /* Set client type (user or service) */
929         Client_SetType(Client, Type);
930
931         if (From) {
932                 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
933                                    Client_ID(Client)))
934                         Client_SetType(Client, CLIENT_SERVICE);
935                 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
936                          Client_TypeText(Client), Client_Mask(Client),
937                          Client_Modes(Client), Client_ID(From),
938                          Client_ID(Client_Introducer(Client)),
939                          Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
940         } else {
941                 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
942                     Client_TypeText(Client), Client_Mask(Client),
943                     Client_Conn(Client));
944                 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
945                                  Client_ID(Client), Client_User(Client),
946                                  Client_Hostname(Client),
947                                  Conn_IPA(Client_Conn(Client)),
948                                  Client_TypeText(Client));
949         }
950
951         /* Inform other servers */
952         IRC_WriteStrServersPrefixFlag_CB(From,
953                                 From != NULL ? From : Client_ThisServer(),
954                                 '\0', cb_introduceClient, (void *)Client);
955 } /* Introduce_Client */
956
957
958 static void
959 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
960 {
961         CLIENT *c = (CLIENT *)data;
962         CONN_ID conn;
963         char *modes, *user, *host;
964
965         modes = Client_Modes(c);
966         user = Client_User(c) ? Client_User(c) : "-";
967         host = Client_Hostname(c) ? Client_Hostname(c) : "-";
968
969         conn = Client_Conn(To);
970         if (Conn_Options(conn) & CONN_RFC1459) {
971                 /* RFC 1459 mode: separate NICK and USER commands */
972                 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
973                               Client_Hops(c) + 1);
974                 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
975                               Client_ID(c), user, host,
976                               Client_ID(Client_Introducer(c)), Client_Info(c));
977                 if (modes[0])
978                         Conn_WriteStr(conn, ":%s MODE %s +%s",
979                                       Client_ID(c), Client_ID(c), modes);
980         } else {
981                 /* RFC 2813 mode: one combined NICK or SERVICE command */
982                 if (Client_Type(c) == CLIENT_SERVICE
983                     && strchr(Client_Flags(To), 'S'))
984                         IRC_WriteStrClientPrefix(To, Prefix,
985                                          "SERVICE %s %d * +%s %d :%s",
986                                          Client_Mask(c),
987                                          Client_MyToken(Client_Introducer(c)),
988                                          Client_Modes(c), Client_Hops(c) + 1,
989                                          Client_Info(c));
990                 else
991                         IRC_WriteStrClientPrefix(To, Prefix,
992                                          "NICK %s %d %s %s %d +%s :%s",
993                                          Client_ID(c), Client_Hops(c) + 1,
994                                          user, host,
995                                          Client_MyToken(Client_Introducer(c)),
996                                          modes, Client_Info(c));
997         }
998 } /* cb_introduceClient */
999
1000
1001 /* -eof- */