irc-login.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2008 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. #include "portab.h"
  14. #include "imp.h"
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include "ngircd.h"
  21. #include "resolve.h"
  22. #include "conn-func.h"
  23. #include "conf.h"
  24. #include "client.h"
  25. #include "channel.h"
  26. #include "log.h"
  27. #include "messages.h"
  28. #include "parse.h"
  29. #include "irc.h"
  30. #include "irc-info.h"
  31. #include "irc-write.h"
  32. #include "exp.h"
  33. #include "irc-login.h"
  34. static bool Hello_User PARAMS(( CLIENT *Client ));
  35. static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
  36. static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
  37. static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
  38. void *i));
  39. /**
  40. * Handler for the IRC command "PASS".
  41. * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
  42. */
  43. GLOBAL bool
  44. IRC_PASS( CLIENT *Client, REQUEST *Req )
  45. {
  46. char *type, *orig_flags;
  47. int protohigh, protolow;
  48. assert( Client != NULL );
  49. assert( Req != NULL );
  50. /* Return an error if this is not a local client */
  51. if (Client_Conn(Client) <= NONE)
  52. return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
  53. Client_ID(Client), Req->command);
  54. if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
  55. /* Not yet registered "unknown" connection, PASS with one
  56. * argument: either a regular client, service, or server
  57. * using the old RFC 1459 section 4.1.1 syntax. */
  58. LogDebug("Connection %d: got PASS command (RFC 1459) ...",
  59. Client_Conn(Client));
  60. } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
  61. Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
  62. (Req->argc == 3 || Req->argc == 4)) {
  63. /* Not yet registered "unknown" connection or outgoing server
  64. * link, PASS with three or four argument: server using the
  65. * RFC 2813 section 4.1.1 syntax. */
  66. LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
  67. Client_Conn(Client));
  68. } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
  69. Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
  70. /* Unregistered connection, but wrong number of arguments: */
  71. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  72. Client_ID(Client), Req->command);
  73. } else {
  74. /* Registered connection, PASS command is not allowed! */
  75. return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
  76. Client_ID(Client));
  77. }
  78. Client_SetPassword(Client, Req->argv[0]);
  79. /* Protocol version */
  80. if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
  81. int c2, c4;
  82. c2 = Req->argv[1][2];
  83. c4 = Req->argv[1][4];
  84. Req->argv[1][4] = '\0';
  85. protolow = atoi(&Req->argv[1][2]);
  86. Req->argv[1][2] = '\0';
  87. protohigh = atoi(Req->argv[1]);
  88. Req->argv[1][2] = c2;
  89. Req->argv[1][4] = c4;
  90. Client_SetType(Client, CLIENT_GOTPASS_2813);
  91. } else {
  92. protohigh = protolow = 0;
  93. Client_SetType(Client, CLIENT_GOTPASS);
  94. }
  95. /* Protocol type, see doc/Protocol.txt */
  96. if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
  97. type = &Req->argv[1][4];
  98. else
  99. type = NULL;
  100. /* Protocol flags/options */
  101. if (Req->argc >= 4)
  102. orig_flags = Req->argv[3];
  103. else
  104. orig_flags = "";
  105. /* Implementation, version and IRC+ flags */
  106. if (Req->argc >= 3) {
  107. char *impl, *ptr, *serverver, *flags;
  108. impl = Req->argv[2];
  109. ptr = strchr(impl, '|');
  110. if (ptr)
  111. *ptr = '\0';
  112. if (type && strcmp(type, PROTOIRCPLUS) == 0) {
  113. /* The peer seems to be a server which supports the
  114. * IRC+ protocol (see doc/Protocol.txt). */
  115. serverver = ptr + 1;
  116. flags = strchr(serverver, ':');
  117. if (flags) {
  118. *flags = '\0';
  119. flags++;
  120. } else
  121. flags = "";
  122. Log(LOG_INFO,
  123. "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
  124. impl, serverver, protohigh, protolow, flags);
  125. } else {
  126. /* The peer seems to be a server supporting the
  127. * "original" IRC protocol (RFC 2813). */
  128. if (strchr(orig_flags, 'Z'))
  129. flags = "Z";
  130. else
  131. flags = "";
  132. Log(LOG_INFO,
  133. "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
  134. impl, protohigh, protolow, flags);
  135. }
  136. Client_SetFlags(Client, flags);
  137. }
  138. return CONNECTED;
  139. } /* IRC_PASS */
  140. /**
  141. * IRC "NICK" command.
  142. * This function implements the IRC command "NICK" which is used to register
  143. * with the server, to change already registered nicknames and to introduce
  144. * new users which are connected to other servers.
  145. */
  146. GLOBAL bool
  147. IRC_NICK( CLIENT *Client, REQUEST *Req )
  148. {
  149. CLIENT *intr_c, *target, *c;
  150. char *nick, *user, *hostname, *modes, *info;
  151. int token, hops;
  152. assert( Client != NULL );
  153. assert( Req != NULL );
  154. /* Some IRC clients, for example BitchX, send the NICK and USER
  155. * commands in the wrong order ... */
  156. if(Client_Type(Client) == CLIENT_UNKNOWN
  157. || Client_Type(Client) == CLIENT_GOTPASS
  158. || Client_Type(Client) == CLIENT_GOTNICK
  159. #ifndef STRICT_RFC
  160. || Client_Type(Client) == CLIENT_GOTUSER
  161. #endif
  162. || Client_Type(Client) == CLIENT_USER
  163. || Client_Type(Client) == CLIENT_SERVICE
  164. || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
  165. {
  166. /* User registration or change of nickname */
  167. /* Wrong number of arguments? */
  168. if( Req->argc != 1 )
  169. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  170. Client_ID( Client ),
  171. Req->command );
  172. /* Search "target" client */
  173. if( Client_Type( Client ) == CLIENT_SERVER )
  174. {
  175. target = Client_Search( Req->prefix );
  176. if( ! target )
  177. return IRC_WriteStrClient( Client,
  178. ERR_NOSUCHNICK_MSG,
  179. Client_ID( Client ),
  180. Req->argv[0] );
  181. }
  182. else
  183. {
  184. /* Is this a restricted client? */
  185. if( Client_HasMode( Client, 'r' ))
  186. return IRC_WriteStrClient( Client,
  187. ERR_RESTRICTED_MSG,
  188. Client_ID( Client ));
  189. target = Client;
  190. }
  191. #ifndef STRICT_RFC
  192. /* If the clients tries to change to its own nickname we won't
  193. * do anything. This is how the original ircd behaves and some
  194. * clients (for example Snak) expect it to be like this.
  195. * But I doubt that this is "really the right thing" ... */
  196. if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
  197. return CONNECTED;
  198. #endif
  199. /* Check that the new nickname is available. Special case:
  200. * the client only changes from/to upper to lower case. */
  201. if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
  202. {
  203. if( ! Client_CheckNick( target, Req->argv[0] ))
  204. return CONNECTED;
  205. }
  206. if (Client_Type(target) != CLIENT_USER &&
  207. Client_Type(target) != CLIENT_SERVICE &&
  208. Client_Type(target) != CLIENT_SERVER) {
  209. /* New client */
  210. LogDebug("Connection %d: got valid NICK command ...",
  211. Client_Conn( Client ));
  212. /* Register new nickname of this client */
  213. Client_SetID( target, Req->argv[0] );
  214. /* If we received a valid USER command already then
  215. * register the new client! */
  216. if( Client_Type( Client ) == CLIENT_GOTUSER )
  217. return Hello_User( Client );
  218. else
  219. Client_SetType( Client, CLIENT_GOTNICK );
  220. } else {
  221. /* Nickname change */
  222. if (Client_Conn(target) > NONE) {
  223. /* Local client */
  224. Log(LOG_INFO,
  225. "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
  226. Client_TypeText(target), Client_Mask(target),
  227. Client_Conn(target), Client_ID(target),
  228. Req->argv[0]);
  229. Conn_UpdateIdle(Client_Conn(target));
  230. } else {
  231. /* Remote client */
  232. LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
  233. Client_TypeText(target),
  234. Client_Mask(target), Client_ID(target),
  235. Req->argv[0]);
  236. }
  237. /* Inform all users and servers (which have to know)
  238. * of this nickname change */
  239. if( Client_Type( Client ) == CLIENT_USER )
  240. IRC_WriteStrClientPrefix( Client, Client,
  241. "NICK :%s",
  242. Req->argv[0] );
  243. IRC_WriteStrServersPrefix( Client, target,
  244. "NICK :%s", Req->argv[0] );
  245. IRC_WriteStrRelatedPrefix( target, target, false,
  246. "NICK :%s", Req->argv[0] );
  247. /* Register old nickname for WHOWAS queries */
  248. Client_RegisterWhowas( target );
  249. /* Save new nickname */
  250. Client_SetID( target, Req->argv[0] );
  251. IRC_SetPenalty( target, 2 );
  252. }
  253. return CONNECTED;
  254. } else if(Client_Type(Client) == CLIENT_SERVER ||
  255. Client_Type(Client) == CLIENT_SERVICE) {
  256. /* Server or service introduces new client */
  257. /* Bad number of parameters? */
  258. if (Req->argc != 2 && Req->argc != 7)
  259. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  260. Client_ID(Client), Req->command);
  261. if (Req->argc >= 7) {
  262. /* RFC 2813 compatible syntax */
  263. nick = Req->argv[0];
  264. hops = atoi(Req->argv[1]);
  265. user = Req->argv[2];
  266. hostname = Req->argv[3];
  267. token = atoi(Req->argv[4]);
  268. modes = Req->argv[5] + 1;
  269. info = Req->argv[6];
  270. } else {
  271. /* RFC 1459 compatible syntax */
  272. nick = Req->argv[0];
  273. hops = 1;
  274. user = Req->argv[0];
  275. hostname = Client_ID(Client);
  276. token = atoi(Req->argv[1]);
  277. modes = "";
  278. info = Req->argv[0];
  279. }
  280. c = Client_Search(nick);
  281. if(c) {
  282. /*
  283. * the new nick is already present on this server:
  284. * the new and the old one have to be disconnected now.
  285. */
  286. Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
  287. Kill_Nick( Req->argv[0], "Nick collision" );
  288. return CONNECTED;
  289. }
  290. /* Find the Server this client is connected to */
  291. intr_c = Client_GetFromToken(Client, token);
  292. if( ! intr_c )
  293. {
  294. Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
  295. Kill_Nick( Req->argv[0], "Unknown server" );
  296. return CONNECTED;
  297. }
  298. c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
  299. token, modes, info, true);
  300. if( ! c )
  301. {
  302. /* out of memory, need to disconnect client to keep network state consistent */
  303. Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
  304. Kill_Nick( Req->argv[0], "Server error" );
  305. return CONNECTED;
  306. }
  307. /* RFC 2813: client is now fully registered, inform all the
  308. * other servers about the new user.
  309. * RFC 1459: announce the new client only after receiving the
  310. * USER command, first we need more information! */
  311. if (Req->argc < 7) {
  312. LogDebug("Client \"%s\" is beeing registered (RFC 1459) ...",
  313. Client_Mask(c));
  314. Client_SetType(c, CLIENT_GOTNICK);
  315. } else
  316. Introduce_Client(Client, c, CLIENT_USER);
  317. return CONNECTED;
  318. }
  319. else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
  320. } /* IRC_NICK */
  321. /**
  322. * Handler for the IRC command "USER".
  323. */
  324. GLOBAL bool
  325. IRC_USER(CLIENT * Client, REQUEST * Req)
  326. {
  327. CLIENT *c;
  328. #ifdef IDENTAUTH
  329. char *ptr;
  330. #endif
  331. assert(Client != NULL);
  332. assert(Req != NULL);
  333. if (Client_Type(Client) == CLIENT_GOTNICK ||
  334. #ifndef STRICT_RFC
  335. Client_Type(Client) == CLIENT_UNKNOWN ||
  336. #endif
  337. Client_Type(Client) == CLIENT_GOTPASS)
  338. {
  339. /* New connection */
  340. if (Req->argc != 4)
  341. return IRC_WriteStrClient(Client,
  342. ERR_NEEDMOREPARAMS_MSG,
  343. Client_ID(Client),
  344. Req->command);
  345. /* User name */
  346. #ifdef IDENTAUTH
  347. ptr = Client_User(Client);
  348. if (!ptr || !*ptr || *ptr == '~')
  349. Client_SetUser(Client, Req->argv[0], false);
  350. #else
  351. Client_SetUser(Client, Req->argv[0], false);
  352. #endif
  353. /* "Real name" or user info text: Don't set it to the empty
  354. * string, the original ircd can't deal with such "real names"
  355. * (e. g. "USER user * * :") ... */
  356. if (*Req->argv[3])
  357. Client_SetInfo(Client, Req->argv[3]);
  358. else
  359. Client_SetInfo(Client, "-");
  360. LogDebug("Connection %d: got valid USER command ...",
  361. Client_Conn(Client));
  362. if (Client_Type(Client) == CLIENT_GOTNICK)
  363. return Hello_User(Client);
  364. else
  365. Client_SetType(Client, CLIENT_GOTUSER);
  366. return CONNECTED;
  367. } else if (Client_Type(Client) == CLIENT_SERVER ||
  368. Client_Type(Client) == CLIENT_SERVICE) {
  369. /* Server/service updating an user */
  370. if (Req->argc != 4)
  371. return IRC_WriteStrClient(Client,
  372. ERR_NEEDMOREPARAMS_MSG,
  373. Client_ID(Client),
  374. Req->command);
  375. c = Client_Search(Req->prefix);
  376. if (!c)
  377. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  378. Client_ID(Client),
  379. Req->prefix);
  380. Client_SetUser(c, Req->argv[0], true);
  381. Client_SetHostname(c, Req->argv[1]);
  382. Client_SetInfo(c, Req->argv[3]);
  383. LogDebug("Connection %d: got valid USER command for \"%s\".",
  384. Client_Conn(Client), Client_Mask(c));
  385. /* RFC 1459 style user registration?
  386. * Introduce client to network: */
  387. if (Client_Type(c) == CLIENT_GOTNICK)
  388. Introduce_Client(Client, c, CLIENT_USER);
  389. return CONNECTED;
  390. } else if (Client_Type(Client) == CLIENT_USER) {
  391. /* Already registered connection */
  392. return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
  393. Client_ID(Client));
  394. } else {
  395. /* Unexpected/invalid connection state? */
  396. return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
  397. Client_ID(Client));
  398. }
  399. } /* IRC_USER */
  400. /**
  401. * Handler for the IRC command "SERVICE".
  402. * This function implements IRC Services registration using the SERVICE command
  403. * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
  404. * At the moment ngIRCd doesn't support directly linked services, so this
  405. * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
  406. * received from a peer server.
  407. */
  408. GLOBAL bool
  409. IRC_SERVICE(CLIENT *Client, REQUEST *Req)
  410. {
  411. CLIENT *c, *intr_c;
  412. char *nick, *user, *host, *info, *modes, *ptr;
  413. int token, hops;
  414. assert(Client != NULL);
  415. assert(Req != NULL);
  416. if (Client_Type(Client) != CLIENT_GOTPASS &&
  417. Client_Type(Client) != CLIENT_SERVER)
  418. return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
  419. Client_ID(Client));
  420. if (Req->argc != 6)
  421. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  422. Client_ID(Client), Req->command);
  423. if (Client_Type(Client) != CLIENT_SERVER)
  424. return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
  425. Client_ID(Client), Req->argv[0]);
  426. /* Bad number of parameters? */
  427. if (Req->argc != 6)
  428. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  429. Client_ID(Client), Req->command);
  430. nick = Req->argv[0];
  431. user = NULL; host = NULL;
  432. token = atoi(Req->argv[1]);
  433. hops = atoi(Req->argv[4]);
  434. info = Req->argv[5];
  435. /* Validate service name ("nick name") */
  436. c = Client_Search(nick);
  437. if(c) {
  438. /* Nick name collission: disconnect (KILL) both clients! */
  439. Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
  440. Client_ID(Client), nick);
  441. Kill_Nick(nick, "Nick collision");
  442. return CONNECTED;
  443. }
  444. /* Get the server to which the service is connected */
  445. intr_c = Client_GetFromToken(Client, token);
  446. if (! intr_c) {
  447. Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
  448. Client_ID(Client), nick);
  449. Kill_Nick(nick, "Unknown server");
  450. return CONNECTED;
  451. }
  452. /* Get user and host name */
  453. ptr = strchr(nick, '@');
  454. if (ptr) {
  455. *ptr = '\0';
  456. host = ++ptr;
  457. }
  458. if (!host)
  459. host = Client_Hostname(intr_c);
  460. ptr = strchr(nick, '!');
  461. if (ptr) {
  462. *ptr = '\0';
  463. user = ++ptr;
  464. }
  465. if (!user)
  466. user = nick;
  467. /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
  468. * for future usage"; but we use it to transfer the modes and check
  469. * that the first character is a '+' sign and ignore it otherwise. */
  470. modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
  471. c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
  472. token, modes, info, true);
  473. if (! c) {
  474. /* Couldn't create client structure, so KILL the service to
  475. * keep network status consistent ... */
  476. Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
  477. Client_Conn(Client));
  478. Kill_Nick(nick, "Server error");
  479. return CONNECTED;
  480. }
  481. Introduce_Client(Client, c, CLIENT_SERVICE);
  482. return CONNECTED;
  483. } /* IRC_SERVICE */
  484. GLOBAL bool
  485. IRC_QUIT( CLIENT *Client, REQUEST *Req )
  486. {
  487. CLIENT *target;
  488. char quitmsg[LINE_LEN];
  489. assert( Client != NULL );
  490. assert( Req != NULL );
  491. /* Wrong number of arguments? */
  492. if( Req->argc > 1 )
  493. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  494. if (Req->argc == 1)
  495. strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
  496. if ( Client_Type( Client ) == CLIENT_SERVER )
  497. {
  498. /* Server */
  499. target = Client_Search( Req->prefix );
  500. if( ! target )
  501. {
  502. Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
  503. return CONNECTED;
  504. }
  505. Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
  506. return CONNECTED;
  507. }
  508. else
  509. {
  510. if (Req->argc == 1 && quitmsg[0] != '\"') {
  511. /* " " to avoid confusion */
  512. strlcpy(quitmsg, "\"", sizeof quitmsg);
  513. strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
  514. strlcat(quitmsg, "\"", sizeof quitmsg );
  515. }
  516. /* User, Service, or not yet registered */
  517. Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
  518. return DISCONNECTED;
  519. }
  520. } /* IRC_QUIT */
  521. GLOBAL bool
  522. IRC_PING(CLIENT *Client, REQUEST *Req)
  523. {
  524. CLIENT *target, *from;
  525. assert(Client != NULL);
  526. assert(Req != NULL);
  527. if (Req->argc < 1)
  528. return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
  529. Client_ID(Client));
  530. #ifdef STRICT_RFC
  531. /* Don't ignore additional arguments when in "strict" mode */
  532. if (Req->argc > 2)
  533. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  534. Client_ID(Client), Req->command);
  535. #endif
  536. if (Req->argc > 1) {
  537. /* A target has been specified ... */
  538. target = Client_Search(Req->argv[1]);
  539. if (!target || Client_Type(target) != CLIENT_SERVER)
  540. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  541. Client_ID(Client), Req->argv[1]);
  542. if (target != Client_ThisServer()) {
  543. /* Ok, we have to forward the PING */
  544. if (Client_Type(Client) == CLIENT_SERVER)
  545. from = Client_Search(Req->prefix);
  546. else
  547. from = Client;
  548. if (!from)
  549. return IRC_WriteStrClient(Client,
  550. ERR_NOSUCHSERVER_MSG,
  551. Client_ID(Client), Req->prefix);
  552. return IRC_WriteStrClientPrefix(target, from,
  553. "PING %s :%s", Req->argv[0],
  554. Req->argv[1] );
  555. }
  556. }
  557. if (Client_Type(Client) == CLIENT_SERVER) {
  558. if (Req->prefix)
  559. from = Client_Search(Req->prefix);
  560. else
  561. from = Client;
  562. } else
  563. from = Client_ThisServer();
  564. if (!from)
  565. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  566. Client_ID(Client), Req->prefix);
  567. Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
  568. Client_Conn(Client));
  569. #ifdef STRICT_RFC
  570. return IRC_WriteStrClient(Client, "PONG %s :%s",
  571. Client_ID(from), Client_ID(Client));
  572. #else
  573. /* Some clients depend on the argument being returned in the PONG
  574. * reply (not mentioned in any RFC, though) */
  575. return IRC_WriteStrClient(Client, "PONG %s :%s",
  576. Client_ID(from), Req->argv[0]);
  577. #endif
  578. } /* IRC_PING */
  579. GLOBAL bool
  580. IRC_PONG(CLIENT *Client, REQUEST *Req)
  581. {
  582. CLIENT *target, *from;
  583. char *s;
  584. assert(Client != NULL);
  585. assert(Req != NULL);
  586. /* Wrong number of arguments? */
  587. if (Req->argc < 1)
  588. return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
  589. Client_ID(Client));
  590. if (Req->argc > 2)
  591. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  592. Client_ID(Client), Req->command);
  593. /* Forward? */
  594. if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
  595. target = Client_Search(Req->argv[0]);
  596. if (!target)
  597. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  598. Client_ID(Client), Req->argv[0]);
  599. from = Client_Search(Req->prefix);
  600. if (target != Client_ThisServer() && target != from) {
  601. /* Ok, we have to forward the message. */
  602. if (!from)
  603. return IRC_WriteStrClient(Client,
  604. ERR_NOSUCHSERVER_MSG,
  605. Client_ID(Client), Req->prefix);
  606. if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
  607. s = Client_ID(from);
  608. else
  609. s = Req->argv[0];
  610. return IRC_WriteStrClientPrefix(target, from,
  611. "PONG %s :%s", s, Req->argv[1]);
  612. }
  613. }
  614. /* The connection timestamp has already been updated when the data has
  615. * been read from so socket, so we don't need to update it here. */
  616. #ifdef DEBUG
  617. if (Client_Conn(Client) > NONE)
  618. Log(LOG_DEBUG,
  619. "Connection %d: received PONG. Lag: %ld seconds.",
  620. Client_Conn(Client),
  621. time(NULL) - Conn_LastPing(Client_Conn(Client)));
  622. else
  623. Log(LOG_DEBUG,
  624. "Connection %d: received PONG.", Client_Conn(Client));
  625. #endif
  626. return CONNECTED;
  627. } /* IRC_PONG */
  628. static bool
  629. Hello_User(CLIENT * Client)
  630. {
  631. assert(Client != NULL);
  632. /* Check password ... */
  633. if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
  634. /* Bad password! */
  635. Log(LOG_ERR,
  636. "Client \"%s\" rejected (connection %d): Bad password!",
  637. Client_Mask(Client), Client_Conn(Client));
  638. Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
  639. return DISCONNECTED;
  640. }
  641. Introduce_Client(NULL, Client, CLIENT_USER);
  642. if (!IRC_WriteStrClient
  643. (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
  644. return false;
  645. if (!IRC_WriteStrClient
  646. (Client, RPL_YOURHOST_MSG, Client_ID(Client),
  647. Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
  648. TARGET_VENDOR, TARGET_OS))
  649. return false;
  650. if (!IRC_WriteStrClient
  651. (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
  652. return false;
  653. if (!IRC_WriteStrClient
  654. (Client, RPL_MYINFO_MSG, Client_ID(Client),
  655. Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
  656. CHANMODES))
  657. return false;
  658. /* Features supported by this server (005 numeric, ISUPPORT),
  659. * see <http://www.irc.org/tech_docs/005.html> for details. */
  660. if (!IRC_Send_ISUPPORT(Client))
  661. return DISCONNECTED;
  662. if (!IRC_Send_LUSERS(Client))
  663. return DISCONNECTED;
  664. if (!IRC_Show_MOTD(Client))
  665. return DISCONNECTED;
  666. /* Suspend the client for a second ... */
  667. IRC_SetPenalty(Client, 1);
  668. return CONNECTED;
  669. } /* Hello_User */
  670. static void
  671. Kill_Nick( char *Nick, char *Reason )
  672. {
  673. REQUEST r;
  674. assert( Nick != NULL );
  675. assert( Reason != NULL );
  676. r.prefix = (char *)Client_ThisServer( );
  677. r.argv[0] = Nick;
  678. r.argv[1] = Reason;
  679. r.argc = 2;
  680. Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
  681. IRC_KILL( Client_ThisServer( ), &r );
  682. } /* Kill_Nick */
  683. static void
  684. Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
  685. {
  686. /* Set client type (user or service) */
  687. Client_SetType(Client, Type);
  688. if (From) {
  689. if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
  690. Client_ID(Client)))
  691. Client_SetType(Client, CLIENT_SERVICE);
  692. LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
  693. Client_TypeText(Client), Client_Mask(Client),
  694. Client_Modes(Client), Client_ID(From),
  695. Client_ID(Client_Introducer(Client)),
  696. Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
  697. } else
  698. Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
  699. Client_TypeText(Client), Client_Mask(Client),
  700. Client_Conn(Client));
  701. /* Inform other servers */
  702. IRC_WriteStrServersPrefixFlag_CB(From,
  703. From != NULL ? From : Client_ThisServer(),
  704. '\0', cb_introduceClient, (void *)Client);
  705. } /* Introduce_Client */
  706. static void
  707. cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
  708. {
  709. CLIENT *c = (CLIENT *)data;
  710. CONN_ID conn;
  711. char *modes, *user, *host;
  712. modes = Client_Modes(c);
  713. user = Client_User(c) ? Client_User(c) : "-";
  714. host = Client_Hostname(c) ? Client_Hostname(c) : "-";
  715. conn = Client_Conn(To);
  716. if (Conn_Options(conn) & CONN_RFC1459) {
  717. /* RFC 1459 mode: separate NICK and USER commands */
  718. Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
  719. Client_Hops(c) + 1);
  720. Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
  721. Client_ID(c), user, host,
  722. Client_ID(Client_Introducer(c)), Client_Info(c));
  723. if (modes[0])
  724. Conn_WriteStr(conn, ":%s MODE %s +%s",
  725. Client_ID(c), Client_ID(c), modes);
  726. } else {
  727. /* RFC 2813 mode: one combined NICK or SERVICE command */
  728. if (Client_Type(c) == CLIENT_SERVICE
  729. && strchr(Client_Flags(To), 'S'))
  730. IRC_WriteStrClientPrefix(To, Prefix,
  731. "SERVICE %s %d * +%s %d :%s",
  732. Client_Mask(c),
  733. Client_MyToken(Client_Introducer(c)),
  734. Client_Modes(c), Client_Hops(c) + 1,
  735. Client_Info(c));
  736. else
  737. IRC_WriteStrClientPrefix(To, Prefix,
  738. "NICK %s %d %s %s %d +%s :%s",
  739. Client_ID(c), Client_Hops(c) + 1,
  740. user, host,
  741. Client_MyToken(Client_Introducer(c)),
  742. modes, Client_Info(c));
  743. }
  744. } /* cb_introduceClient */
  745. /* -eof- */